]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-22-POLLHUP-on-listening-socket.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / io / test / run-22-POLLHUP-on-listening-socket.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 #define PORT "65022"
10
11 static int make_listen_fd(const char *port, struct addrinfo **info)
12 {
13         int fd, on = 1;
14         struct addrinfo *addrinfo, hints;
15
16         memset(&hints, 0, sizeof(hints));
17         hints.ai_family = AF_UNSPEC;
18         hints.ai_socktype = SOCK_STREAM;
19         hints.ai_flags = AI_PASSIVE;
20         hints.ai_protocol = 0;
21
22         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
23                 return -1;
24
25         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
26                     addrinfo->ai_protocol);
27         if (fd < 0)
28                 return -1;
29
30         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
31         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
32                 close(fd);
33                 return -1;
34         }
35         if (listen(fd, 1) != 0) {
36                 close(fd);
37                 return -1;
38         }
39         *info = addrinfo;
40         return fd;
41 }
42
43 int main(void)
44 {
45         struct addrinfo *addrinfo = NULL;
46         int fd;
47
48         /* This is how many tests you plan to run */
49         plan_tests(1);
50         fd = make_listen_fd(PORT, &addrinfo);
51         freeaddrinfo(addrinfo);
52         io_new_listener(NULL, fd, io_never, NULL);
53
54         /* Anyone could do this; a child doing it will cause poll to return
55          * POLLHUP only! */
56         shutdown(fd, SHUT_RDWR);
57         ok1(io_loop(NULL, NULL) == NULL);
58
59         /* This exits depending on whether all tests passed */
60         return exit_status();
61 }