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