]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-16-duplex-test.c
io: io_close_taken_fd to steal fd from conn.
[ccan] / ccan / io / test / run-16-duplex-test.c
1 /* Tests when the last connection is a duplex, and poll.c moves it over
2  * deleted fd. */
3 #include <ccan/io/io.h>
4 /* Include the C files directly. */
5 #include <ccan/io/poll.c>
6 #include <ccan/io/io.c>
7 #include <ccan/tap/tap.h>
8 #include <sys/wait.h>
9 #include <stdio.h>
10
11 #define PORT "65016"
12
13 struct data {
14         struct io_listener *l;
15         int state;
16         char buf[4];
17         char wbuf[32];
18 };
19
20 static void finish_ok(struct io_conn *conn, struct data *d)
21 {
22         d->state++;
23 }
24
25 static struct io_plan *io_done(struct io_conn *conn, struct data *d)
26 {
27         d->state++;
28         return io_halfclose(conn);
29 }
30
31 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
32 {
33         ok1(d->state == 0);
34         d->state++;
35
36         memset(d->wbuf, 7, sizeof(d->wbuf));
37
38         io_set_finish(conn, finish_ok, d);
39
40         io_close_listener(d->l);
41
42         return io_duplex(conn,
43                          io_read(conn, d->buf, sizeof(d->buf), io_done, d),
44                          io_write(conn, d->wbuf, sizeof(d->wbuf), io_done, d));
45 }
46
47 static int make_listen_fd(const char *port, struct addrinfo **info)
48 {
49         int fd, on = 1;
50         struct addrinfo *addrinfo, hints;
51
52         memset(&hints, 0, sizeof(hints));
53         hints.ai_family = AF_UNSPEC;
54         hints.ai_socktype = SOCK_STREAM;
55         hints.ai_flags = AI_PASSIVE;
56         hints.ai_protocol = 0;
57
58         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
59                 return -1;
60
61         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
62                     addrinfo->ai_protocol);
63         if (fd < 0)
64                 return -1;
65
66         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
67         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
68                 close(fd);
69                 return -1;
70         }
71         if (listen(fd, 1) != 0) {
72                 close(fd);
73                 return -1;
74         }
75         *info = addrinfo;
76         return fd;
77 }
78
79 int main(void)
80 {
81         struct data *d = malloc(sizeof(*d));
82         struct addrinfo *addrinfo;
83         int fd, status;
84
85         /* This is how many tests you plan to run */
86         plan_tests(9);
87         d->state = 0;
88         fd = make_listen_fd(PORT, &addrinfo);
89         ok1(fd >= 0);
90         d->l = io_new_listener(NULL, fd, init_conn, d);
91         ok1(d->l);
92         fflush(stdout);
93         if (!fork()) {
94                 int i;
95                 char buf[32];
96
97                 io_close_listener(d->l);
98                 free(d);
99                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
100                             addrinfo->ai_protocol);
101                 if (fd < 0)
102                         exit(1);
103                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
104                         exit(2);
105                 signal(SIGPIPE, SIG_IGN);
106                 for (i = 0; i < 32; i++) {
107                         if (read(fd, buf+i, 1) != 1)
108                                 break;
109                 }
110                 for (i = 0; i < strlen("hellothere"); i++) {
111                         if (write(fd, "hellothere" + i, 1) != 1)
112                                 break;
113                 }
114                 close(fd);
115                 freeaddrinfo(addrinfo);
116                 exit(0);
117         }
118         freeaddrinfo(addrinfo);
119         ok1(io_loop(NULL, NULL) == NULL);
120         ok1(d->state == 4);
121         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
122         free(d);
123
124         ok1(wait(&status));
125         ok1(WIFEXITED(status));
126         ok1(WEXITSTATUS(status) == 0);
127
128         /* This exits depending on whether all tests passed */
129         return exit_status();
130 }