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