io: io_always, and zero-length operations support.
[ccan] / ccan / io / test / run-18-errno.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 "65018"
11 #endif
12
13 static void finish_100(struct io_conn *conn, int *state)
14 {
15         ok1(errno == 100);
16         ok1(*state == 1);
17         (*state)++;
18 }
19
20 static void finish_EBADF(struct io_conn *conn, int *state)
21 {
22         ok1(errno == EBADF);
23         ok1(*state == 3);
24         (*state)++;
25         io_break(state + 1, io_close());
26 }
27
28 static void init_conn(int fd, int *state)
29 {
30         if (*state == 0) {
31                 (*state)++;
32                 errno = 100;
33                 io_set_finish(io_new_conn(fd, io_close()), finish_100, state);
34         } else {
35                 ok1(*state == 2);
36                 (*state)++;
37                 close(fd);
38                 errno = 0;
39                 io_set_finish(io_new_conn(fd, io_read(state, 1,
40                                                       io_close_cb, NULL)),
41                               finish_EBADF, state);
42         }
43 }
44
45 static int make_listen_fd(const char *port, struct addrinfo **info)
46 {
47         int fd, on = 1;
48         struct addrinfo *addrinfo, hints;
49
50         memset(&hints, 0, sizeof(hints));
51         hints.ai_family = AF_UNSPEC;
52         hints.ai_socktype = SOCK_STREAM;
53         hints.ai_flags = AI_PASSIVE;
54         hints.ai_protocol = 0;
55
56         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
57                 return -1;
58
59         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
60                     addrinfo->ai_protocol);
61         if (fd < 0)
62                 return -1;
63
64         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
65         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
66                 close(fd);
67                 return -1;
68         }
69         if (listen(fd, 1) != 0) {
70                 close(fd);
71                 return -1;
72         }
73         *info = addrinfo;
74         return fd;
75 }
76
77 int main(void)
78 {
79         int state = 0;
80         struct addrinfo *addrinfo;
81         struct io_listener *l;
82         int fd;
83
84         /* This is how many tests you plan to run */
85         plan_tests(12);
86         fd = make_listen_fd(PORT, &addrinfo);
87         ok1(fd >= 0);
88         l = io_new_listener(fd, init_conn, &state);
89         ok1(l);
90         fflush(stdout);
91         if (!fork()) {
92                 io_close_listener(l);
93                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
94                             addrinfo->ai_protocol);
95                 if (fd < 0)
96                         exit(1);
97                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
98                         exit(2);
99                 close(fd);
100                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
101                             addrinfo->ai_protocol);
102                 if (fd < 0)
103                         exit(3);
104                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
105                         exit(4);
106                 close(fd);
107                 freeaddrinfo(addrinfo);
108                 exit(0);
109         }
110         freeaddrinfo(addrinfo);
111         ok1(io_loop() == &state + 1);
112         ok1(state == 4);
113         io_close_listener(l);
114         ok1(wait(&state));
115         ok1(WIFEXITED(state));
116         ok1(WEXITSTATUS(state) == 0);
117
118         /* This exits depending on whether all tests passed */
119         return exit_status();
120 }