]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-07-break.c
ccan/io: get rid of io_next(), pass callbacks directly.
[ccan] / ccan / io / test / run-07-break.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
9 struct data {
10         int state;
11         char buf[4];
12 };
13
14 static struct io_plan *do_read(struct io_conn *conn, struct data *d)
15 {
16         ok1(d->state == 1);
17         d->state++;
18         return io_read(conn, d->buf, sizeof(d->buf), io_close, d);
19 }
20
21 static struct io_plan *start_break(struct io_conn *conn, struct data *d)
22 {
23         ok1(d->state == 0);
24         d->state++;
25         return io_break(conn, d, do_read, d);
26 }
27
28 static void finish_ok(struct io_conn *conn, struct data *d)
29 {
30         ok1(d->state == 2);
31         d->state++;
32 }
33
34 static int make_listen_fd(const char *port, struct addrinfo **info)
35 {
36         int fd, on = 1;
37         struct addrinfo *addrinfo, hints;
38
39         memset(&hints, 0, sizeof(hints));
40         hints.ai_family = AF_UNSPEC;
41         hints.ai_socktype = SOCK_STREAM;
42         hints.ai_flags = AI_PASSIVE;
43         hints.ai_protocol = 0;
44
45         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
46                 return -1;
47
48         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
49                     addrinfo->ai_protocol);
50         if (fd < 0)
51                 return -1;
52
53         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
54         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
55                 close(fd);
56                 return -1;
57         }
58         if (listen(fd, 1) != 0) {
59                 close(fd);
60                 return -1;
61         }
62         *info = addrinfo;
63         return fd;
64 }
65
66 int main(void)
67 {
68         struct data *d = malloc(sizeof(*d));
69         struct addrinfo *addrinfo;
70         struct io_listener *l;
71         int fd, status;
72
73         /* This is how many tests you plan to run */
74         plan_tests(13);
75         d->state = 0;
76         fd = make_listen_fd("65007", &addrinfo);
77         ok1(fd >= 0);
78         l = io_new_listener(fd, start_break, finish_ok, d);
79         ok1(l);
80         fflush(stdout);
81         if (!fork()) {
82                 int i;
83
84                 io_close_listener(l);
85                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
86                             addrinfo->ai_protocol);
87                 if (fd < 0)
88                         exit(1);
89                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
90                         exit(2);
91                 signal(SIGPIPE, SIG_IGN);
92                 for (i = 0; i < strlen("hellothere"); i++) {
93                         if (write(fd, "hellothere" + i, 1) != 1)
94                                 break;
95                 }
96                 close(fd);
97                 freeaddrinfo(addrinfo);
98                 free(d);
99                 exit(0);
100         }
101         freeaddrinfo(addrinfo);
102         ok1(io_loop() == d);
103         ok1(d->state == 1);
104         io_close_listener(l);
105
106         ok1(io_loop() == NULL);
107         ok1(d->state == 3);
108         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
109         free(d);
110
111         ok1(wait(&status));
112         ok1(WIFEXITED(status));
113         ok1(WEXITSTATUS(status) == 0);
114
115         /* This exits depending on whether all tests passed */
116         return exit_status();
117 }