1 /* $OpenBSD: poll_close.c,v 1.4 2021/12/24 10:22:41 visa Exp $ */
2
3 /*
4 * Copyright (c) 2021 Visa Hankala
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /*
20 * Test behaviour when a monitored file descriptor is closed by another thread.
21 *
22 * Note that this case is not defined by POSIX.
23 */
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <assert.h>
28 #include <err.h>
29 #include <poll.h>
30 #include <pthread.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 static int barrier[2];
36 static int sock[2];
37
38 static void *
thread_main(void * arg)39 thread_main(void *arg)
40 {
41 struct pollfd pfd[1];
42 int ret;
43 char b;
44
45 memset(pfd, 0, sizeof(pfd));
46 pfd[0].fd = sock[1];
47 pfd[0].events = POLLIN;
48 ret = poll(pfd, 1, INFTIM);
49 assert(ret == 1);
50 assert(pfd[0].revents & POLLIN);
51
52 /* Drain data to prevent subsequent wakeups. */
53 read(sock[1], &b, 1);
54
55 /* Sync with parent thread. */
56 write(barrier[1], "y", 1);
57 read(barrier[1], &b, 1);
58
59 memset(pfd, 0, sizeof(pfd));
60 pfd[0].fd = sock[1];
61 pfd[0].events = POLLIN;
62 ret = poll(pfd, 1, INFTIM);
63 assert(ret == 1);
64 assert(pfd[0].revents & POLLNVAL);
65
66 return NULL;
67 }
68
69 int
main(void)70 main(void)
71 {
72 pthread_t t;
73 int ret, saved_fd;
74 char b;
75
76 /* Enforce test timeout. */
77 alarm(10);
78
79 if (socketpair(AF_UNIX, SOCK_STREAM, 0, barrier) == -1)
80 err(1, "can't create socket pair");
81
82 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1)
83 err(1, "can't create socket pair");
84
85 ret = pthread_create(&t, NULL, thread_main, NULL);
86 if (ret != 0) {
87 fprintf(stderr, "can't start thread: %s\n", strerror(ret));
88 return 1;
89 }
90
91 /* Let the thread settle in poll(). */
92 usleep(100000);
93
94 /* Awaken poll(). */
95 write(sock[0], "x", 1);
96
97 /* Wait until the thread has left poll(). */
98 read(barrier[0], &b, 1);
99
100 /*
101 * Close and restore the fd that the thread has polled.
102 * This creates a pending badfd knote in the kernel.
103 */
104 saved_fd = dup(sock[1]);
105 close(sock[1]);
106 dup2(saved_fd, sock[1]);
107 close(saved_fd);
108
109 /* Let the thread continue. */
110 write(barrier[0], "x", 1);
111
112 /* Let the thread settle in poll(). */
113 usleep(100000);
114
115 /* Close the fd to awaken poll(). */
116 close(sock[1]);
117
118 pthread_join(t, NULL);
119
120 close(sock[0]);
121
122 return 0;
123 }
124