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