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