diff options
author | Ron Yorston <rmy@pobox.com> | 2015-04-22 21:22:22 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2015-04-22 21:22:22 +0100 |
commit | 42463758ee0334dc180bc624a3d7035cf88d6bea (patch) | |
tree | 9a2a36287fa210365639c2259f6dd3ff8aa49752 | |
parent | 683b593c838db1fc2c2e303bb3811a4503e98a9b (diff) | |
download | busybox-w32-42463758ee0334dc180bc624a3d7035cf88d6bea.tar.gz busybox-w32-42463758ee0334dc180bc624a3d7035cf88d6bea.tar.bz2 busybox-w32-42463758ee0334dc180bc624a3d7035cf88d6bea.zip |
mingw: import select from gnulib
There are two changes from gnulib:
Treat a broken pipe as readable. If nc doesn't try to read from
the pipe it'll never find out it's broken.
Only allow console key press events to indicate that stdin is
readable. read_key leaves a key release event in the buffer
at the end of a shell command. This caused nc to block, thinking
that the console had input available.
-rw-r--r-- | win32/Kbuild | 1 | ||||
-rw-r--r-- | win32/select.c | 573 |
2 files changed, 574 insertions, 0 deletions
diff --git a/win32/Kbuild b/win32/Kbuild index ca44a4134..48cd027e7 100644 --- a/win32/Kbuild +++ b/win32/Kbuild | |||
@@ -12,6 +12,7 @@ lib-$(CONFIG_PLATFORM_MINGW32) += process.o | |||
12 | lib-$(CONFIG_PLATFORM_MINGW32) += regex.o | 12 | lib-$(CONFIG_PLATFORM_MINGW32) += regex.o |
13 | lib-$(CONFIG_PLATFORM_MINGW32) += net.o | 13 | lib-$(CONFIG_PLATFORM_MINGW32) += net.o |
14 | lib-$(CONFIG_PLATFORM_MINGW32) += poll.o | 14 | lib-$(CONFIG_PLATFORM_MINGW32) += poll.o |
15 | lib-$(CONFIG_PLATFORM_MINGW32) += select.o | ||
15 | lib-$(CONFIG_PLATFORM_MINGW32) += popen.o | 16 | lib-$(CONFIG_PLATFORM_MINGW32) += popen.o |
16 | lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o | 17 | lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o |
17 | lib-$(CONFIG_PLATFORM_MINGW32) += mempcpy.o | 18 | lib-$(CONFIG_PLATFORM_MINGW32) += mempcpy.o |
diff --git a/win32/select.c b/win32/select.c new file mode 100644 index 000000000..416174b3e --- /dev/null +++ b/win32/select.c | |||
@@ -0,0 +1,573 @@ | |||
1 | /* Emulation for select(2) | ||
2 | Contributed by Paolo Bonzini. | ||
3 | |||
4 | Copyright 2008-2015 Free Software Foundation, Inc. | ||
5 | |||
6 | This file is part of gnulib. | ||
7 | |||
8 | This program is free software; you can redistribute it and/or modify | ||
9 | it under the terms of the GNU General Public License as published by | ||
10 | the Free Software Foundation; either version 2, or (at your option) | ||
11 | any later version. | ||
12 | |||
13 | This program is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | GNU General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU General Public License along | ||
19 | with this program; if not, see <http://www.gnu.org/licenses/>. */ | ||
20 | |||
21 | #include <malloc.h> | ||
22 | #include <assert.h> | ||
23 | |||
24 | #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
25 | /* Native Windows. */ | ||
26 | |||
27 | #include <sys/types.h> | ||
28 | #include <errno.h> | ||
29 | #include <limits.h> | ||
30 | |||
31 | #include <winsock2.h> | ||
32 | #include <windows.h> | ||
33 | #include <io.h> | ||
34 | #include <stdio.h> | ||
35 | #include <conio.h> | ||
36 | #include <time.h> | ||
37 | |||
38 | /* Get the overridden 'struct timeval'. */ | ||
39 | #include <sys/time.h> | ||
40 | |||
41 | #undef select | ||
42 | |||
43 | struct bitset { | ||
44 | unsigned char in[FD_SETSIZE / CHAR_BIT]; | ||
45 | unsigned char out[FD_SETSIZE / CHAR_BIT]; | ||
46 | }; | ||
47 | |||
48 | /* Declare data structures for ntdll functions. */ | ||
49 | typedef struct _FILE_PIPE_LOCAL_INFORMATION { | ||
50 | ULONG NamedPipeType; | ||
51 | ULONG NamedPipeConfiguration; | ||
52 | ULONG MaximumInstances; | ||
53 | ULONG CurrentInstances; | ||
54 | ULONG InboundQuota; | ||
55 | ULONG ReadDataAvailable; | ||
56 | ULONG OutboundQuota; | ||
57 | ULONG WriteQuotaAvailable; | ||
58 | ULONG NamedPipeState; | ||
59 | ULONG NamedPipeEnd; | ||
60 | } FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION; | ||
61 | |||
62 | typedef struct _IO_STATUS_BLOCK | ||
63 | { | ||
64 | union { | ||
65 | DWORD Status; | ||
66 | PVOID Pointer; | ||
67 | } u; | ||
68 | ULONG_PTR Information; | ||
69 | } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; | ||
70 | |||
71 | typedef enum _FILE_INFORMATION_CLASS { | ||
72 | FilePipeLocalInformation = 24 | ||
73 | } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS; | ||
74 | |||
75 | typedef DWORD (WINAPI *PNtQueryInformationFile) | ||
76 | (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS); | ||
77 | |||
78 | #ifndef PIPE_BUF | ||
79 | #define PIPE_BUF 512 | ||
80 | #endif | ||
81 | |||
82 | static BOOL IsConsoleHandle (HANDLE h) | ||
83 | { | ||
84 | DWORD mode; | ||
85 | return GetConsoleMode (h, &mode) != 0; | ||
86 | } | ||
87 | |||
88 | static BOOL | ||
89 | IsSocketHandle (HANDLE h) | ||
90 | { | ||
91 | WSANETWORKEVENTS ev; | ||
92 | |||
93 | if (IsConsoleHandle (h)) | ||
94 | return FALSE; | ||
95 | |||
96 | /* Under Wine, it seems that getsockopt returns 0 for pipes too. | ||
97 | WSAEnumNetworkEvents instead distinguishes the two correctly. */ | ||
98 | ev.lNetworkEvents = 0xDEADBEEF; | ||
99 | WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); | ||
100 | return ev.lNetworkEvents != 0xDEADBEEF; | ||
101 | } | ||
102 | |||
103 | /* Compute output fd_sets for libc descriptor FD (whose Windows handle is | ||
104 | H). */ | ||
105 | |||
106 | static int | ||
107 | windows_poll_handle (HANDLE h, int fd, | ||
108 | struct bitset *rbits, | ||
109 | struct bitset *wbits, | ||
110 | struct bitset *xbits) | ||
111 | { | ||
112 | BOOL read, write, except; | ||
113 | int i, ret; | ||
114 | INPUT_RECORD *irbuffer; | ||
115 | DWORD avail, nbuffer; | ||
116 | BOOL bRet; | ||
117 | IO_STATUS_BLOCK iosb; | ||
118 | FILE_PIPE_LOCAL_INFORMATION fpli; | ||
119 | static PNtQueryInformationFile NtQueryInformationFile; | ||
120 | static BOOL once_only; | ||
121 | |||
122 | read = write = except = FALSE; | ||
123 | switch (GetFileType (h)) | ||
124 | { | ||
125 | case FILE_TYPE_DISK: | ||
126 | read = TRUE; | ||
127 | write = TRUE; | ||
128 | break; | ||
129 | |||
130 | case FILE_TYPE_PIPE: | ||
131 | if (!once_only) | ||
132 | { | ||
133 | NtQueryInformationFile = (PNtQueryInformationFile) | ||
134 | GetProcAddress (GetModuleHandle ("ntdll.dll"), | ||
135 | "NtQueryInformationFile"); | ||
136 | once_only = TRUE; | ||
137 | } | ||
138 | |||
139 | if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0) | ||
140 | { | ||
141 | if (avail) | ||
142 | read = TRUE; | ||
143 | } | ||
144 | else if (GetLastError () == ERROR_BROKEN_PIPE) | ||
145 | read = TRUE; | ||
146 | |||
147 | else | ||
148 | { | ||
149 | /* It was the write-end of the pipe. Check if it is writable. | ||
150 | If NtQueryInformationFile fails, optimistically assume the pipe is | ||
151 | writable. This could happen on Windows 9x, where | ||
152 | NtQueryInformationFile is not available, or if we inherit a pipe | ||
153 | that doesn't permit FILE_READ_ATTRIBUTES access on the write end | ||
154 | (I think this should not happen since Windows XP SP2; WINE seems | ||
155 | fine too). Otherwise, ensure that enough space is available for | ||
156 | atomic writes. */ | ||
157 | memset (&iosb, 0, sizeof (iosb)); | ||
158 | memset (&fpli, 0, sizeof (fpli)); | ||
159 | |||
160 | if (!NtQueryInformationFile | ||
161 | || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli), | ||
162 | FilePipeLocalInformation) | ||
163 | || fpli.WriteQuotaAvailable >= PIPE_BUF | ||
164 | || (fpli.OutboundQuota < PIPE_BUF && | ||
165 | fpli.WriteQuotaAvailable == fpli.OutboundQuota)) | ||
166 | write = TRUE; | ||
167 | } | ||
168 | break; | ||
169 | |||
170 | case FILE_TYPE_CHAR: | ||
171 | write = TRUE; | ||
172 | if (!(rbits->in[fd / CHAR_BIT] & (1 << (fd & (CHAR_BIT - 1))))) | ||
173 | break; | ||
174 | |||
175 | ret = WaitForSingleObject (h, 0); | ||
176 | if (ret == WAIT_OBJECT_0) | ||
177 | { | ||
178 | if (!IsConsoleHandle (h)) | ||
179 | { | ||
180 | read = TRUE; | ||
181 | break; | ||
182 | } | ||
183 | |||
184 | nbuffer = avail = 0; | ||
185 | bRet = GetNumberOfConsoleInputEvents (h, &nbuffer); | ||
186 | |||
187 | /* Screen buffers handles are filtered earlier. */ | ||
188 | assert (bRet); | ||
189 | if (nbuffer == 0) | ||
190 | { | ||
191 | except = TRUE; | ||
192 | break; | ||
193 | } | ||
194 | |||
195 | irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD)); | ||
196 | bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail); | ||
197 | if (!bRet || avail == 0) | ||
198 | { | ||
199 | except = TRUE; | ||
200 | break; | ||
201 | } | ||
202 | |||
203 | for (i = 0; i < avail; i++) | ||
204 | if (irbuffer[i].EventType == KEY_EVENT && | ||
205 | irbuffer[i].Event.KeyEvent.bKeyDown) | ||
206 | read = TRUE; | ||
207 | } | ||
208 | break; | ||
209 | |||
210 | default: | ||
211 | ret = WaitForSingleObject (h, 0); | ||
212 | write = TRUE; | ||
213 | if (ret == WAIT_OBJECT_0) | ||
214 | read = TRUE; | ||
215 | |||
216 | break; | ||
217 | } | ||
218 | |||
219 | ret = 0; | ||
220 | if (read && (rbits->in[fd / CHAR_BIT] & (1 << (fd & (CHAR_BIT - 1))))) | ||
221 | { | ||
222 | rbits->out[fd / CHAR_BIT] |= (1 << (fd & (CHAR_BIT - 1))); | ||
223 | ret++; | ||
224 | } | ||
225 | |||
226 | if (write && (wbits->in[fd / CHAR_BIT] & (1 << (fd & (CHAR_BIT - 1))))) | ||
227 | { | ||
228 | wbits->out[fd / CHAR_BIT] |= (1 << (fd & (CHAR_BIT - 1))); | ||
229 | ret++; | ||
230 | } | ||
231 | |||
232 | if (except && (xbits->in[fd / CHAR_BIT] & (1 << (fd & (CHAR_BIT - 1))))) | ||
233 | { | ||
234 | xbits->out[fd / CHAR_BIT] |= (1 << (fd & (CHAR_BIT - 1))); | ||
235 | ret++; | ||
236 | } | ||
237 | |||
238 | return ret; | ||
239 | } | ||
240 | |||
241 | int | ||
242 | mingw_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds, | ||
243 | struct timeval *timeout) | ||
244 | #undef timeval | ||
245 | { | ||
246 | static struct timeval tv0; | ||
247 | static HANDLE hEvent; | ||
248 | HANDLE h, handle_array[FD_SETSIZE + 2]; | ||
249 | fd_set handle_rfds, handle_wfds, handle_xfds; | ||
250 | struct bitset rbits, wbits, xbits; | ||
251 | unsigned char anyfds_in[FD_SETSIZE / CHAR_BIT]; | ||
252 | DWORD ret, wait_timeout, nhandles, nsock, nbuffer; | ||
253 | MSG msg; | ||
254 | int i, fd, rc; | ||
255 | clock_t tend; | ||
256 | |||
257 | if (nfds > FD_SETSIZE) | ||
258 | nfds = FD_SETSIZE; | ||
259 | |||
260 | if (!timeout) | ||
261 | wait_timeout = INFINITE; | ||
262 | else | ||
263 | { | ||
264 | wait_timeout = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; | ||
265 | |||
266 | /* select is also used as a portable usleep. */ | ||
267 | if (!rfds && !wfds && !xfds) | ||
268 | { | ||
269 | Sleep (wait_timeout); | ||
270 | return 0; | ||
271 | } | ||
272 | } | ||
273 | |||
274 | if (!hEvent) | ||
275 | hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | ||
276 | |||
277 | handle_array[0] = hEvent; | ||
278 | nhandles = 1; | ||
279 | nsock = 0; | ||
280 | |||
281 | /* Copy descriptors to bitsets. At the same time, eliminate | ||
282 | bits in the "wrong" direction for console input buffers | ||
283 | and screen buffers, because screen buffers are waitable | ||
284 | and they will block until a character is available. */ | ||
285 | memset (&rbits, 0, sizeof (rbits)); | ||
286 | memset (&wbits, 0, sizeof (wbits)); | ||
287 | memset (&xbits, 0, sizeof (xbits)); | ||
288 | memset (anyfds_in, 0, sizeof (anyfds_in)); | ||
289 | if (rfds) | ||
290 | for (i = 0; i < rfds->fd_count; i++) | ||
291 | { | ||
292 | fd = rfds->fd_array[i]; | ||
293 | h = (HANDLE) _get_osfhandle (fd); | ||
294 | if (IsConsoleHandle (h) | ||
295 | && !GetNumberOfConsoleInputEvents (h, &nbuffer)) | ||
296 | continue; | ||
297 | |||
298 | rbits.in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1)); | ||
299 | anyfds_in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1)); | ||
300 | } | ||
301 | else | ||
302 | rfds = (fd_set *) alloca (sizeof (fd_set)); | ||
303 | |||
304 | if (wfds) | ||
305 | for (i = 0; i < wfds->fd_count; i++) | ||
306 | { | ||
307 | fd = wfds->fd_array[i]; | ||
308 | h = (HANDLE) _get_osfhandle (fd); | ||
309 | if (IsConsoleHandle (h) | ||
310 | && GetNumberOfConsoleInputEvents (h, &nbuffer)) | ||
311 | continue; | ||
312 | |||
313 | wbits.in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1)); | ||
314 | anyfds_in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1)); | ||
315 | } | ||
316 | else | ||
317 | wfds = (fd_set *) alloca (sizeof (fd_set)); | ||
318 | |||
319 | if (xfds) | ||
320 | for (i = 0; i < xfds->fd_count; i++) | ||
321 | { | ||
322 | fd = xfds->fd_array[i]; | ||
323 | xbits.in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1)); | ||
324 | anyfds_in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1)); | ||
325 | } | ||
326 | else | ||
327 | xfds = (fd_set *) alloca (sizeof (fd_set)); | ||
328 | |||
329 | /* Zero all the fd_sets, including the application's. */ | ||
330 | FD_ZERO (rfds); | ||
331 | FD_ZERO (wfds); | ||
332 | FD_ZERO (xfds); | ||
333 | FD_ZERO (&handle_rfds); | ||
334 | FD_ZERO (&handle_wfds); | ||
335 | FD_ZERO (&handle_xfds); | ||
336 | |||
337 | /* Classify handles. Create fd sets for sockets, poll the others. */ | ||
338 | for (i = 0; i < nfds; i++) | ||
339 | { | ||
340 | if ((anyfds_in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) == 0) | ||
341 | continue; | ||
342 | |||
343 | h = (HANDLE) _get_osfhandle (i); | ||
344 | if (!h) | ||
345 | { | ||
346 | errno = EBADF; | ||
347 | return -1; | ||
348 | } | ||
349 | |||
350 | if (IsSocketHandle (h)) | ||
351 | { | ||
352 | int requested = FD_CLOSE; | ||
353 | |||
354 | /* See above; socket handles are mapped onto select, but we | ||
355 | need to map descriptors to handles. */ | ||
356 | if (rbits.in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) | ||
357 | { | ||
358 | requested |= FD_READ | FD_ACCEPT; | ||
359 | FD_SET ((SOCKET) h, rfds); | ||
360 | FD_SET ((SOCKET) h, &handle_rfds); | ||
361 | } | ||
362 | if (wbits.in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) | ||
363 | { | ||
364 | requested |= FD_WRITE | FD_CONNECT; | ||
365 | FD_SET ((SOCKET) h, wfds); | ||
366 | FD_SET ((SOCKET) h, &handle_wfds); | ||
367 | } | ||
368 | if (xbits.in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) | ||
369 | { | ||
370 | requested |= FD_OOB; | ||
371 | FD_SET ((SOCKET) h, xfds); | ||
372 | FD_SET ((SOCKET) h, &handle_xfds); | ||
373 | } | ||
374 | |||
375 | WSAEventSelect ((SOCKET) h, hEvent, requested); | ||
376 | nsock++; | ||
377 | } | ||
378 | else | ||
379 | { | ||
380 | handle_array[nhandles++] = h; | ||
381 | |||
382 | /* Poll now. If we get an event, do not wait below. */ | ||
383 | if (wait_timeout != 0 | ||
384 | && windows_poll_handle (h, i, &rbits, &wbits, &xbits)) | ||
385 | wait_timeout = 0; | ||
386 | } | ||
387 | } | ||
388 | |||
389 | /* Place a sentinel at the end of the array. */ | ||
390 | handle_array[nhandles] = NULL; | ||
391 | |||
392 | /* When will the waiting period expire? */ | ||
393 | if (wait_timeout != INFINITE) | ||
394 | tend = clock () + wait_timeout; | ||
395 | |||
396 | restart: | ||
397 | if (wait_timeout == 0 || nsock == 0) | ||
398 | rc = 0; | ||
399 | else | ||
400 | { | ||
401 | /* See if we need to wait in the loop below. If any select is ready, | ||
402 | do MsgWaitForMultipleObjects anyway to dispatch messages, but | ||
403 | no need to call select again. */ | ||
404 | rc = select (0, &handle_rfds, &handle_wfds, &handle_xfds, &tv0); | ||
405 | if (rc == 0) | ||
406 | { | ||
407 | /* Restore the fd_sets for the other select we do below. */ | ||
408 | memcpy (&handle_rfds, rfds, sizeof (fd_set)); | ||
409 | memcpy (&handle_wfds, wfds, sizeof (fd_set)); | ||
410 | memcpy (&handle_xfds, xfds, sizeof (fd_set)); | ||
411 | } | ||
412 | else | ||
413 | wait_timeout = 0; | ||
414 | } | ||
415 | |||
416 | /* How much is left to wait? */ | ||
417 | if (wait_timeout != INFINITE) | ||
418 | { | ||
419 | clock_t tnow = clock (); | ||
420 | if (tend >= tnow) | ||
421 | wait_timeout = tend - tnow; | ||
422 | else | ||
423 | wait_timeout = 0; | ||
424 | } | ||
425 | |||
426 | for (;;) | ||
427 | { | ||
428 | ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE, | ||
429 | wait_timeout, QS_ALLINPUT); | ||
430 | |||
431 | if (ret == WAIT_OBJECT_0 + nhandles) | ||
432 | { | ||
433 | /* new input of some other kind */ | ||
434 | BOOL bRet; | ||
435 | while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0) | ||
436 | { | ||
437 | TranslateMessage (&msg); | ||
438 | DispatchMessage (&msg); | ||
439 | } | ||
440 | } | ||
441 | else | ||
442 | break; | ||
443 | } | ||
444 | |||
445 | /* If we haven't done it yet, check the status of the sockets. */ | ||
446 | if (rc == 0 && nsock > 0) | ||
447 | rc = select (0, &handle_rfds, &handle_wfds, &handle_xfds, &tv0); | ||
448 | |||
449 | if (nhandles > 1) | ||
450 | { | ||
451 | /* Count results that are not counted in the return value of select. */ | ||
452 | nhandles = 1; | ||
453 | for (i = 0; i < nfds; i++) | ||
454 | { | ||
455 | if ((anyfds_in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) == 0) | ||
456 | continue; | ||
457 | |||
458 | h = (HANDLE) _get_osfhandle (i); | ||
459 | if (h == handle_array[nhandles]) | ||
460 | { | ||
461 | /* Not a socket. */ | ||
462 | nhandles++; | ||
463 | windows_poll_handle (h, i, &rbits, &wbits, &xbits); | ||
464 | if (rbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))) | ||
465 | || wbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))) | ||
466 | || xbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) | ||
467 | rc++; | ||
468 | } | ||
469 | } | ||
470 | |||
471 | if (rc == 0 | ||
472 | && (wait_timeout == INFINITE | ||
473 | /* If NHANDLES > 1, but no bits are set, it means we've | ||
474 | been told incorrectly that some handle was signaled. | ||
475 | This happens with anonymous pipes, which always cause | ||
476 | MsgWaitForMultipleObjects to exit immediately, but no | ||
477 | data is found ready to be read by windows_poll_handle. | ||
478 | To avoid a total failure (whereby we return zero and | ||
479 | don't wait at all), let's poll in a more busy loop. */ | ||
480 | || (wait_timeout != 0 && nhandles > 1))) | ||
481 | { | ||
482 | /* Sleep 1 millisecond to avoid busy wait and retry with the | ||
483 | original fd_sets. */ | ||
484 | memcpy (&handle_rfds, rfds, sizeof (fd_set)); | ||
485 | memcpy (&handle_wfds, wfds, sizeof (fd_set)); | ||
486 | memcpy (&handle_xfds, xfds, sizeof (fd_set)); | ||
487 | SleepEx (1, TRUE); | ||
488 | goto restart; | ||
489 | } | ||
490 | if (timeout && wait_timeout == 0 && rc == 0) | ||
491 | timeout->tv_sec = timeout->tv_usec = 0; | ||
492 | } | ||
493 | |||
494 | /* Now fill in the results. */ | ||
495 | FD_ZERO (rfds); | ||
496 | FD_ZERO (wfds); | ||
497 | FD_ZERO (xfds); | ||
498 | nhandles = 1; | ||
499 | for (i = 0; i < nfds; i++) | ||
500 | { | ||
501 | if ((anyfds_in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) == 0) | ||
502 | continue; | ||
503 | |||
504 | h = (HANDLE) _get_osfhandle (i); | ||
505 | if (h != handle_array[nhandles]) | ||
506 | { | ||
507 | /* Perform handle->descriptor mapping. */ | ||
508 | WSAEventSelect ((SOCKET) h, NULL, 0); | ||
509 | if (FD_ISSET (h, &handle_rfds)) | ||
510 | FD_SET (i, rfds); | ||
511 | if (FD_ISSET (h, &handle_wfds)) | ||
512 | FD_SET (i, wfds); | ||
513 | if (FD_ISSET (h, &handle_xfds)) | ||
514 | FD_SET (i, xfds); | ||
515 | } | ||
516 | else | ||
517 | { | ||
518 | /* Not a socket. */ | ||
519 | nhandles++; | ||
520 | if (rbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) | ||
521 | FD_SET (i, rfds); | ||
522 | if (wbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) | ||
523 | FD_SET (i, wfds); | ||
524 | if (xbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) | ||
525 | FD_SET (i, xfds); | ||
526 | } | ||
527 | } | ||
528 | |||
529 | return rc; | ||
530 | } | ||
531 | |||
532 | #else /* ! Native Windows. */ | ||
533 | |||
534 | #include <sys/select.h> | ||
535 | #include <stddef.h> /* NULL */ | ||
536 | #include <errno.h> | ||
537 | #include <unistd.h> | ||
538 | |||
539 | #undef select | ||
540 | |||
541 | int | ||
542 | rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds, | ||
543 | struct timeval *timeout) | ||
544 | { | ||
545 | int i; | ||
546 | |||
547 | /* FreeBSD 8.2 has a bug: it does not always detect invalid fds. */ | ||
548 | if (nfds < 0 || nfds > FD_SETSIZE) | ||
549 | { | ||
550 | errno = EINVAL; | ||
551 | return -1; | ||
552 | } | ||
553 | for (i = 0; i < nfds; i++) | ||
554 | { | ||
555 | if (((rfds && FD_ISSET (i, rfds)) | ||
556 | || (wfds && FD_ISSET (i, wfds)) | ||
557 | || (xfds && FD_ISSET (i, xfds))) | ||
558 | && dup2 (i, i) != i) | ||
559 | return -1; | ||
560 | } | ||
561 | |||
562 | /* Interix 3.5 has a bug: it does not support nfds == 0. */ | ||
563 | if (nfds == 0) | ||
564 | { | ||
565 | nfds = 1; | ||
566 | rfds = NULL; | ||
567 | wfds = NULL; | ||
568 | xfds = NULL; | ||
569 | } | ||
570 | return select (nfds, rfds, wfds, xfds, timeout); | ||
571 | } | ||
572 | |||
573 | #endif | ||