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