]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-06-idle.c
ccan/io: generic init function for listening connections.
[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 void init_conn(int fd, struct data *d)
65 {
66         if (!io_new_conn(fd, start_idle, finish_idle, d))
67                 abort();
68 }
69
70 static int make_listen_fd(const char *port, struct addrinfo **info)
71 {
72         int fd, on = 1;
73         struct addrinfo *addrinfo, hints;
74
75         memset(&hints, 0, sizeof(hints));
76         hints.ai_family = AF_UNSPEC;
77         hints.ai_socktype = SOCK_STREAM;
78         hints.ai_flags = AI_PASSIVE;
79         hints.ai_protocol = 0;
80
81         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
82                 return -1;
83
84         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
85                     addrinfo->ai_protocol);
86         if (fd < 0)
87                 return -1;
88
89         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
90         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
91                 close(fd);
92                 return -1;
93         }
94         if (listen(fd, 1) != 0) {
95                 close(fd);
96                 return -1;
97         }
98         *info = addrinfo;
99         return fd;
100 }
101
102 int main(void)
103 {
104         struct data *d = malloc(sizeof(*d));
105         struct addrinfo *addrinfo;
106         struct io_listener *l;
107         int fd, status;
108
109         /* This is how many tests you plan to run */
110         plan_tests(15);
111         d->state = 0;
112         fd = make_listen_fd("65006", &addrinfo);
113         ok1(fd >= 0);
114         l = io_new_listener(fd, init_conn, d);
115         ok1(l);
116         fflush(stdout);
117         if (!fork()) {
118                 int i;
119
120                 io_close_listener(l);
121                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
122                             addrinfo->ai_protocol);
123                 if (fd < 0)
124                         exit(1);
125                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
126                         exit(2);
127                 signal(SIGPIPE, SIG_IGN);
128                 for (i = 0; i < strlen("hellothere"); i++) {
129                         if (write(fd, "hellothere" + i, 1) != 1)
130                                 break;
131                 }
132                 close(fd);
133                 freeaddrinfo(addrinfo);
134                 free(d);
135                 exit(0);
136         }
137         freeaddrinfo(addrinfo);
138
139         ok1(io_loop() == d);
140         ok1(d->state == 5);
141         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
142         free(d);
143         io_close_listener(l);
144
145         ok1(wait(&status));
146         ok1(WIFEXITED(status));
147         ok1(WEXITSTATUS(status) == 0);
148
149         /* This exits depending on whether all tests passed */
150         return exit_status();
151 }