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