Debian Codegraph @

contrib/poll_win.c

raw symbols SCIP
1 /*
2 * Public domain
3 *
4 * poll(2) emulation for Windows
5 *
6 * This emulates just-enough poll functionality on Windows to work in the
7 * context of the openssl(1) program. This is not a replacement for
8 * POSIX.1-2001 poll(2), though it may come closer than I care to admit.
9 *
10 * Dongsheng Song <[email protected]>
11 * Brent Cook <[email protected]>
12 */
13 /* librist. Copyright © 2019 SipRadius LLC. All right reserved.
14 * Author: Sergio Ammirata, Ph.D. <[email protected]>
15 * Adapted for librist
16 *
17 * SPDX-License-Identifier: BSD-2-Clause
18 */
19
20 #include <conio.h>
21 #include <errno.h>
22 #include <io.h>
23 #include <ws2tcpip.h>
24
25 static int
26 conn_is_closed(int fd)
27 {
28 char buf[1];
29 int ret = recv(fd, buf, 1, MSG_PEEK);
30 if (ret == -1) {
31 switch (WSAGetLastError()) {
32 case WSAECONNABORTED:
33 case WSAECONNRESET:
34 case WSAENETRESET:
35 case WSAESHUTDOWN:
36 return 1;
37 }
38 }
39 return 0;
40 }
41
42 static int
43 conn_has_oob_data(int fd)
44 {
45 char buf[1];
46 return (recv(fd, buf, 1, MSG_PEEK | MSG_OOB) == 1);
47 }
48
49 static int
50 is_socket(int fd)
51 {
52 if (fd < 3)
53 return 0;
54 WSANETWORKEVENTS events;
55 return (WSAEnumNetworkEvents((SOCKET)fd, NULL, &events) == 0);
56 }
57
58 static int
59 compute_select_revents(int fd, short events,
60 fd_set *rfds, fd_set *wfds, fd_set *efds)
61 {
62 int rc = 0;
63
64 if ((events & (POLLIN | POLLRDNORM | POLLRDBAND)) &&
65 FD_ISSET(fd, rfds)) {
66 if (conn_is_closed(fd))
67 rc |= POLLHUP;
68 else
69 rc |= POLLIN | POLLRDNORM;
70 }
71
72 if ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
73 FD_ISSET(fd, wfds))
74 rc |= POLLOUT;
75
76 if (FD_ISSET(fd, efds)) {
77 if (conn_is_closed(fd))
78 rc |= POLLHUP;
79 else if (conn_has_oob_data(fd))
80 rc |= POLLRDBAND | POLLPRI;
81 }
82
83 return rc;
84 }
85
86 static int
87 compute_wait_revents(HANDLE h, short events, int object, int wait_rc)
88 {
89 int rc = 0;
90 INPUT_RECORD record;
91 DWORD num_read;
92
93 /*
94 * Assume we can always write to file handles (probably a bad
95 * assumption but works for now, at least it doesn't block).
96 */
97 if (events & (POLLOUT | POLLWRNORM))
98 rc |= POLLOUT;
99
100 /*
101 * Check if this handle was signaled by WaitForMultipleObjects
102 */
103 if ((object == (int)(wait_rc - WAIT_OBJECT_0))
104 && (events & (POLLIN | POLLRDNORM))) {
105
106 /*
107 * Check if this file is stdin, and if so, if it is a console.
108 */
109 if (h == GetStdHandle(STD_INPUT_HANDLE) &&
110 PeekConsoleInput(h, &record, 1, &num_read) == 1) {
111
112 /*
113 * Handle the input console buffer differently,
114 * since it can signal on other events like
115 * window and mouse, but read can still block.
116 */
117 if (record.EventType == KEY_EVENT &&
118 record.Event.KeyEvent.bKeyDown) {
119 rc |= POLLIN;
120 } else {
121 /*
122 * Flush non-character events from the
123 * console buffer.
124 */
125 ReadConsoleInput(h, &record, 1, &num_read);
126 }
127 } else {
128 rc |= POLLIN;
129 }
130 }
131
132 return rc;
133 }
134
135 static int
136 wsa_select_errno(int err)
137 {
138 switch (err) {
139 case WSAEINTR:
140 case WSAEINPROGRESS:
141 errno = EINTR;
142 break;
143 case WSAEFAULT:
144 /*
145 * Windows uses WSAEFAULT for both resource allocation failures
146 * and arguments not being contained in the user's address
147 * space. So, we have to choose EFAULT or ENOMEM.
148 */
149 errno = EFAULT;
150 break;
151 case WSAEINVAL:
152 errno = EINVAL;
153 break;
154 case WSANOTINITIALISED:
155 errno = EPERM;
156 break;
157 case WSAENETDOWN:
158 errno = ENOMEM;
159 break;
160 }
161 return -1;
162 }
163
164 static int
165 poll(struct pollfd *pfds, nfds_t nfds, int timeout_ms)
166 {
167 nfds_t i;
168 int timespent_ms, looptime_ms;
169
170 /*
171 * select machinery
172 */
173 fd_set rfds, wfds, efds;
174 int rc;
175 int num_sockets;
176
177 /*
178 * wait machinery
179 */
180 DWORD wait_rc;
181 HANDLE handles[FD_SETSIZE];
182 int num_handles;
183
184 if (pfds == NULL) {
185 errno = EINVAL;
186 return -1;
187 }
188
189 if (nfds <= 0) {
190 return 0;
191 }
192
193 FD_ZERO(&rfds);
194 FD_ZERO(&wfds);
195 FD_ZERO(&efds);
196 num_sockets = 0;
197 num_handles = 0;
198
199 for (i = 0; i < nfds; i++) {
200 if ((int)pfds[i].fd < 0)
201 continue;
202
203 if (is_socket(pfds[i].fd)) {
204 if (num_sockets >= FD_SETSIZE) {
205 errno = EINVAL;
206 return -1;
207 }
208
209 FD_SET(pfds[i].fd, &efds);
210
211 if (pfds[i].events &
212 (POLLIN | POLLRDNORM | POLLRDBAND)) {
213 FD_SET(pfds[i].fd, &rfds);
214 }
215
216 if (pfds[i].events &
217 (POLLOUT | POLLWRNORM | POLLWRBAND)) {
218 FD_SET(pfds[i].fd, &wfds);
219 }
220 num_sockets++;
221
222 } else {
223 if (num_handles >= FD_SETSIZE) {
224 errno = EINVAL;
225 return -1;
226 }
227
228 handles[num_handles++] =
229 (HANDLE)_get_osfhandle(pfds[i].fd);
230 }
231 }
232
233 /*
234 * Determine if the files, pipes, sockets, consoles, etc. have signaled.
235 *
236 * Do this by alternating a loop between WaitForMultipleObjects for
237 * non-sockets and and select for sockets.
238 *
239 * I tried to implement this all in terms of WaitForMultipleObjects
240 * with a select-based 'poll' of the sockets at the end to get extra
241 * specific socket status.
242 *
243 * However, the cost of setting up an event handle for each socket and
244 * cleaning them up reliably was pretty high. Since the event handle
245 * associated with a socket is also global, creating a new one here
246 * cancels one that may exist externally to this function.
247 *
248 * At any rate, even if global socket event handles were not an issue,
249 * the 'FD_WRITE' status of a socket event handle does not behave in an
250 * expected fashion, being triggered by an edge on a write buffer rather
251 * than simply triggering if there is space available.
252 */
253 timespent_ms = 0;
254 wait_rc = WAIT_FAILED;
255
256 if (timeout_ms < 0)
257 timeout_ms = INFINITE;
258 looptime_ms = timeout_ms > 100 ? 100 : timeout_ms;
259
260 /*
261 * Winsock select() mutates the fd_sets in place, leaving only
262 * descriptors that were ready. Keep pristine copies so each loop
263 * iteration watches the full set the caller asked about.
264 */
265 fd_set rfds_save = rfds;
266 fd_set wfds_save = wfds;
267 fd_set efds_save = efds;
268
269 do {
270 struct timeval tv;
271 tv.tv_sec = 0;
272 tv.tv_usec = looptime_ms * 1000;
273 int handle_signaled = 0;
274
275 rfds = rfds_save;
276 wfds = wfds_save;
277 efds = efds_save;
278
279 /*
280 * Check if any file handles have signaled
281 */
282 if (num_handles) {
283 wait_rc = WaitForMultipleObjects(num_handles, handles,
284 FALSE, 0);
285 if (wait_rc == WAIT_FAILED) {
286 /*
287 * The documentation for WaitForMultipleObjects
288 * does not specify what values GetLastError
289 * may return here. Rather than enumerate
290 * badness like for wsa_select_errno, assume a
291 * general errno value.
292 */
293 errno = ENOMEM;
294 return 0;
295 }
296 }
297
298 /*
299 * If we signaled on a file handle, don't wait on the sockets.
300 * The num_handles guard is required because wait_rc starts out
301 * as WAIT_FAILED (0xFFFFFFFF) and the comparison is unsigned:
302 * with num_handles == 0 the right-hand side underflows to
303 * 0xFFFFFFFF and the condition would always be true, turning
304 * every poll() into a non-blocking spin.
305 */
306 if (num_handles > 0 &&
307 wait_rc <= WAIT_OBJECT_0 + num_handles - 1) {
308 tv.tv_usec = 0;
309 handle_signaled = 1;
310 }
311
312 /*
313 * Check if any sockets have signaled
314 */
315 rc = select(0, &rfds, &wfds, &efds, &tv);
316 if (!handle_signaled && rc == SOCKET_ERROR)
317 return wsa_select_errno(WSAGetLastError());
318
319 if (handle_signaled || (num_sockets && rc > 0))
320 break;
321
322 timespent_ms += looptime_ms;
323
324 } while (timespent_ms < timeout_ms);
325
326 rc = 0;
327 num_handles = 0;
328 for (i = 0; i < nfds; i++) {
329 pfds[i].revents = 0;
330
331 if ((int)pfds[i].fd < 0)
332 continue;
333
334 if (is_socket(pfds[i].fd)) {
335
336 pfds[i].revents = compute_select_revents(pfds[i].fd,
337 pfds[i].events, &rfds, &wfds, &efds);
338
339 } else {
340 pfds[i].revents = compute_wait_revents(
341 handles[num_handles], pfds[i].events, num_handles,
342 wait_rc);
343 num_handles++;
344 }
345
346 if (pfds[i].revents)
347 rc++;
348 }
349
350 return rc;
351 }
352