]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-06-idle.c
ccan/io: initialize connection with an explicit I/O plan.
[ccan] / ccan / io / test / run-06-idle.c
1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
6 #include <sys/wait.h>
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11
12 static struct io_conn *idler;
13
14 struct data {
15         int state;
16         char buf[4];
17 };
18
19 static struct io_plan read_done(struct io_conn *conn, struct data *d)
20 {
21         ok1(d->state == 2 || d->state == 3);
22         d->state++;
23         return io_close(conn, NULL);
24 }
25
26 static void finish_waker(struct io_conn *conn, struct data *d)
27 {
28         io_wake(idler, io_read(d->buf, sizeof(d->buf), read_done, d));
29         ok1(d->state == 1);
30         d->state++;
31 }
32
33 static void finish_idle(struct io_conn *conn, struct data *d)
34 {
35         ok1(d->state == 3);
36         d->state++;
37         io_break(d, io_idle());
38 }
39
40 static void init_conn(int fd, struct data *d)
41 {
42         int fd2;
43
44         ok1(d->state == 0);
45         d->state++;
46         idler = io_new_conn(fd, io_idle(), finish_idle, d);
47
48         /* This will wake us up, as read will fail. */
49         fd2 = open("/dev/null", O_RDONLY);
50         ok1(fd2 >= 0);
51         ok1(io_new_conn(fd2, io_read(idler, 1, NULL, NULL), finish_waker, d));
52 }
53
54 static int make_listen_fd(const char *port, struct addrinfo **info)
55 {
56         int fd, on = 1;
57         struct addrinfo *addrinfo, hints;
58
59         memset(&hints, 0, sizeof(hints));
60         hints.ai_family = AF_UNSPEC;
61         hints.ai_socktype = SOCK_STREAM;
62         hints.ai_flags = AI_PASSIVE;
63         hints.ai_protocol = 0;
64
65         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
66                 return -1;
67
68         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
69                     addrinfo->ai_protocol);
70         if (fd < 0)
71                 return -1;
72
73         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
74         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
75                 close(fd);
76                 return -1;
77         }
78         if (listen(fd, 1) != 0) {
79                 close(fd);
80                 return -1;
81         }
82         *info = addrinfo;
83         return fd;
84 }
85
86 int main(void)
87 {
88         struct data *d = malloc(sizeof(*d));
89         struct addrinfo *addrinfo;
90         struct io_listener *l;
91         int fd, status;
92
93         /* This is how many tests you plan to run */
94         plan_tests(14);
95         d->state = 0;
96         fd = make_listen_fd("65006", &addrinfo);
97         ok1(fd >= 0);
98         l = io_new_listener(fd, init_conn, d);
99         ok1(l);
100         fflush(stdout);
101         if (!fork()) {
102                 int i;
103
104                 io_close_listener(l);
105                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
106                             addrinfo->ai_protocol);
107                 if (fd < 0)
108                         exit(1);
109                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
110                         exit(2);
111                 signal(SIGPIPE, SIG_IGN);
112                 for (i = 0; i < strlen("hellothere"); i++) {
113                         if (write(fd, "hellothere" + i, 1) != 1)
114                                 break;
115                 }
116                 close(fd);
117                 freeaddrinfo(addrinfo);
118                 free(d);
119                 exit(0);
120         }
121         freeaddrinfo(addrinfo);
122
123         ok1(io_loop() == d);
124         ok1(d->state == 4);
125         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
126         free(d);
127         io_close_listener(l);
128
129         ok1(wait(&status));
130         ok1(WIFEXITED(status));
131         ok1(WEXITSTATUS(status) == 0);
132
133         /* This exits depending on whether all tests passed */
134         return exit_status();
135 }