1 | #include <sys/types.h>
|
---|
2 | #include <sys/socket.h>
|
---|
3 | #include <poll.h>
|
---|
4 | #include <netinet/in.h>
|
---|
5 | #include <unistd.h>
|
---|
6 | #include <errno.h>
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <string.h>
|
---|
9 | #include <stdlib.h>
|
---|
10 |
|
---|
11 | static int wrap_err(int ret)
|
---|
12 | {
|
---|
13 | if (ret < 0) {
|
---|
14 | fprintf(stderr, "ERROR: %s\n", strerror(errno));
|
---|
15 | exit(-1);
|
---|
16 | }
|
---|
17 | return ret;
|
---|
18 | }
|
---|
19 |
|
---|
20 | int main()
|
---|
21 | {
|
---|
22 | struct sockaddr_in local_addr = {
|
---|
23 | .sin_family = AF_INET,
|
---|
24 | .sin_port = 0, // Pick whatever free local port
|
---|
25 | .sin_addr = 0x0100007F, // 127.0.0.1
|
---|
26 | };
|
---|
27 | socklen_t addr_len = sizeof(local_addr);
|
---|
28 | int listener = wrap_err(socket(AF_INET, SOCK_STREAM, 0));
|
---|
29 | int conn = wrap_err(socket(AF_INET, SOCK_STREAM, 0));
|
---|
30 |
|
---|
31 | wrap_err(bind(listener, (void*)&local_addr, addr_len));
|
---|
32 | wrap_err(listen(listener, SOMAXCONN));
|
---|
33 | wrap_err(getsockname(listener, (void*)&local_addr, &addr_len));
|
---|
34 | wrap_err(connect(conn, (void*)&local_addr, addr_len));
|
---|
35 |
|
---|
36 | int client = wrap_err(accept(listener, (void*)&local_addr, &addr_len));
|
---|
37 |
|
---|
38 | // At this point we have 2 connected socket and no longer need a listener
|
---|
39 | close(listener);
|
---|
40 |
|
---|
41 | struct pollfd pfd = {
|
---|
42 | .fd = client,
|
---|
43 | .events = POLLIN | POLLERR | POLLHUP,
|
---|
44 | };
|
---|
45 |
|
---|
46 | close(conn); // Simulate a disconnect, implicity does shutdown()
|
---|
47 |
|
---|
48 | printf("Calling poll(), this will hang on Haiku\n");
|
---|
49 | poll(&pfd, 1, -1);
|
---|
50 | // Reports POLLIN on Linux? This is not standardized well, anyways...
|
---|
51 | printf("Events:%s%s%s\n", (pfd.revents & POLLIN) ? " POLLIN" : "",
|
---|
52 | (pfd.revents & POLLERR) ? " POLLERR" : "",
|
---|
53 | (pfd.revents & POLLHUP) ? " POLLHUP" : "");
|
---|
54 |
|
---|
55 | close(client); // I hope we get here eventually..
|
---|
56 |
|
---|
57 | printf("Test passed!\n");
|
---|
58 | }
|
---|