]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-05-write.c
ccan/io: simplify I/O callbacks.
[ccan] / ccan / io / test / run-05-write.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 #ifndef PORT
10 #define PORT "65005"
11 #endif
12
13 struct data {
14         int state;
15         size_t bytes;
16         char *buf;
17 };
18
19 static void finish_ok(struct io_conn *conn, struct data *d)
20 {
21         ok1(d->state == 1);
22         d->state++;
23         io_break(d, io_idle());
24 }
25
26 static void init_conn(int fd, struct data *d)
27 {
28         ok1(d->state == 0);
29         d->state++;
30         io_set_finish(io_new_conn(fd, io_write(d->buf, d->bytes, io_close, d)),
31                       finish_ok, d);
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 static void read_from_socket(size_t bytes, const struct addrinfo *addrinfo)
67 {
68         int fd, done, r;
69         char buf[100];
70
71         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
72                     addrinfo->ai_protocol);
73         if (fd < 0)
74                 exit(1);
75         if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
76                 exit(2);
77
78         for (done = 0; done < bytes; done += r) {
79                 r = read(fd, buf, sizeof(buf));
80                 if (r < 0)
81                         exit(3);
82                 done += r;
83         }
84         close(fd);
85 }
86
87 int main(void)
88 {
89         struct data *d = malloc(sizeof(*d));
90         struct addrinfo *addrinfo;
91         struct io_listener *l;
92         int fd, status;
93
94         /* This is how many tests you plan to run */
95         plan_tests(9);
96         d->state = 0;
97         d->bytes = 1024*1024;
98         d->buf = malloc(d->bytes);
99         memset(d->buf, 'a', d->bytes);
100         fd = make_listen_fd(PORT, &addrinfo);
101         ok1(fd >= 0);
102         l = io_new_listener(fd, init_conn, d);
103         ok1(l);
104         fflush(stdout);
105         if (!fork()) {
106                 io_close_listener(l);
107                 read_from_socket(d->bytes, addrinfo);
108                 freeaddrinfo(addrinfo);
109                 free(d->buf);
110                 free(d);
111                 exit(0);
112         }
113         ok1(io_loop() == d);
114         ok1(d->state == 2);
115
116         ok1(wait(&status));
117         ok1(WIFEXITED(status));
118         ok1(WEXITSTATUS(status) == 0);
119
120         freeaddrinfo(addrinfo);
121         free(d->buf);
122         free(d);
123         io_close_listener(l);
124
125         /* This exits depending on whether all tests passed */
126         return exit_status();
127 }