Mercurial > hg > octave-jordi > gnulib-hg
annotate lib/poll.c @ 17924:68876b89b3ad
poll: fixes for large fds
* lib/poll.c (poll): Don't check directly for NFD too large.
Don't rely on undefined behavior in FD_SET when an arg exceeds
FD_SETSIZE. Always set revents afterwards, even if to zero.
* tests/test-poll.c (poll1): Set revents to -1 instead of 0,
as that makes the test a bit stricter.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Fri, 20 Feb 2015 10:37:49 -0800 |
parents | ab58d4870664 |
children | 5aeafb603ed9 |
rev | line source |
---|---|
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Emulation for poll(2) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
2 Contributed by Paolo Bonzini. |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
17848 | 4 Copyright 2001-2003, 2006-2015 Free Software Foundation, Inc. |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 This file is part of gnulib. |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 |
4435 | 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) | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 any later version. |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 |
4435 | 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. | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
4435 | 18 You should have received a copy of the GNU General Public License along |
16366
bb182ee4a09d
maint: replace FSF snail-mail addresses with URLs
Paul Eggert <eggert@cs.ucla.edu>
parents:
16324
diff
changeset
|
19 with this program; if not, see <http://www.gnu.org/licenses/>. */ |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 |
11020
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
21 /* Tell gcc not to warn about the (nfd < 0) tests, below. */ |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
22 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__ |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
23 # pragma GCC diagnostic ignored "-Wtype-limits" |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
24 #endif |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
25 |
9644
42367c277240
Prefer <config.h> over "config.h". See autoconf doc for explanation.
Jim Meyering <meyering@redhat.com>
parents:
8500
diff
changeset
|
26 #include <config.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
27 #include <alloca.h> |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 #include <sys/types.h> |
13736 | 30 |
31 /* Specification. */ | |
32 #include <poll.h> | |
33 | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 #include <errno.h> |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 #include <limits.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
36 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
37 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
38 # define WINDOWS_NATIVE |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
39 # include <winsock2.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
40 # include <windows.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
41 # include <io.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
42 # include <stdio.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
43 # include <conio.h> |
15752
b86e9061a6d0
New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
15353
diff
changeset
|
44 # include "msvc-nothrow.h" |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
45 #else |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
46 # include <sys/time.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
47 # include <sys/socket.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
48 # include <sys/select.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
49 # include <unistd.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
50 #endif |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
52 #ifdef HAVE_SYS_IOCTL_H |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
53 # include <sys/ioctl.h> |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
54 #endif |
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
55 #ifdef HAVE_SYS_FILIO_H |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
56 # include <sys/filio.h> |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
57 #endif |
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
58 |
7908
d41d48e822b4
* lib/poll.c: Include sys/time.h and time.h unconditionally,
Paul Eggert <eggert@cs.ucla.edu>
parents:
7776
diff
changeset
|
59 #include <time.h> |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 |
17840 | 61 #include "assure.h" |
62 | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 #ifndef INFTIM |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
64 # define INFTIM (-1) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 #endif |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 |
8500
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
67 /* BeOS does not have MSG_PEEK. */ |
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
68 #ifndef MSG_PEEK |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
69 # define MSG_PEEK 0 |
8500
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
70 #endif |
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
71 |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
72 #ifdef WINDOWS_NATIVE |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
73 |
17702
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17587
diff
changeset
|
74 static BOOL IsConsoleHandle (HANDLE h) |
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17587
diff
changeset
|
75 { |
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17587
diff
changeset
|
76 DWORD mode; |
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17587
diff
changeset
|
77 return GetConsoleMode (h, &mode) != 0; |
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17587
diff
changeset
|
78 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
79 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
80 static BOOL |
13051 | 81 IsSocketHandle (HANDLE h) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
82 { |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
83 WSANETWORKEVENTS ev; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
84 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
85 if (IsConsoleHandle (h)) |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
86 return FALSE; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
87 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
88 /* Under Wine, it seems that getsockopt returns 0 for pipes too. |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
89 WSAEnumNetworkEvents instead distinguishes the two correctly. */ |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
90 ev.lNetworkEvents = 0xDEADBEEF; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
91 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
92 return ev.lNetworkEvents != 0xDEADBEEF; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
93 } |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
94 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
95 /* Declare data structures for ntdll functions. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
96 typedef struct _FILE_PIPE_LOCAL_INFORMATION { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
97 ULONG NamedPipeType; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
98 ULONG NamedPipeConfiguration; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
99 ULONG MaximumInstances; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
100 ULONG CurrentInstances; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
101 ULONG InboundQuota; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
102 ULONG ReadDataAvailable; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
103 ULONG OutboundQuota; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
104 ULONG WriteQuotaAvailable; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
105 ULONG NamedPipeState; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
106 ULONG NamedPipeEnd; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
107 } FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
108 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
109 typedef struct _IO_STATUS_BLOCK |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
110 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
111 union { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
112 DWORD Status; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
113 PVOID Pointer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
114 } u; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
115 ULONG_PTR Information; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
116 } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
117 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
118 typedef enum _FILE_INFORMATION_CLASS { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
119 FilePipeLocalInformation = 24 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
120 } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
121 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
122 typedef DWORD (WINAPI *PNtQueryInformationFile) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
123 (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
124 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
125 # ifndef PIPE_BUF |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
126 # define PIPE_BUF 512 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
127 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
128 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
129 /* Compute revents values for file handle H. If some events cannot happen |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
130 for the handle, eliminate them from *P_SOUGHT. */ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
131 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
132 static int |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
133 windows_compute_revents (HANDLE h, int *p_sought) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
134 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
135 int i, ret, happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
136 INPUT_RECORD *irbuffer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
137 DWORD avail, nbuffer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
138 BOOL bRet; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
139 IO_STATUS_BLOCK iosb; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
140 FILE_PIPE_LOCAL_INFORMATION fpli; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
141 static PNtQueryInformationFile NtQueryInformationFile; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
142 static BOOL once_only; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
143 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
144 switch (GetFileType (h)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
145 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
146 case FILE_TYPE_PIPE: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
147 if (!once_only) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
148 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
149 NtQueryInformationFile = (PNtQueryInformationFile) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
150 GetProcAddress (GetModuleHandle ("ntdll.dll"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
151 "NtQueryInformationFile"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
152 once_only = TRUE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
153 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
154 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
155 happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
156 if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
157 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
158 if (avail) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
159 happened |= *p_sought & (POLLIN | POLLRDNORM); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
160 } |
13565
69913822a0f7
poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents:
13051
diff
changeset
|
161 else if (GetLastError () == ERROR_BROKEN_PIPE) |
69913822a0f7
poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents:
13051
diff
changeset
|
162 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
163 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
164 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
165 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
166 /* It was the write-end of the pipe. Check if it is writable. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
167 If NtQueryInformationFile fails, optimistically assume the pipe is |
16218
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
168 writable. This could happen on Windows 9x, where |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
169 NtQueryInformationFile is not available, or if we inherit a pipe |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
170 that doesn't permit FILE_READ_ATTRIBUTES access on the write end |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
171 (I think this should not happen since Windows XP SP2; WINE seems |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
172 fine too). Otherwise, ensure that enough space is available for |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
173 atomic writes. */ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
174 memset (&iosb, 0, sizeof (iosb)); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
175 memset (&fpli, 0, sizeof (fpli)); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
176 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
177 if (!NtQueryInformationFile |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
178 || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli), |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
179 FilePipeLocalInformation) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
180 || fpli.WriteQuotaAvailable >= PIPE_BUF |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
181 || (fpli.OutboundQuota < PIPE_BUF && |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
182 fpli.WriteQuotaAvailable == fpli.OutboundQuota)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
183 happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
184 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
185 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
186 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
187 case FILE_TYPE_CHAR: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
188 ret = WaitForSingleObject (h, 0); |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
189 if (!IsConsoleHandle (h)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
190 return ret == WAIT_OBJECT_0 ? *p_sought & ~(POLLPRI | POLLRDBAND) : 0; |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
191 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
192 nbuffer = avail = 0; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
193 bRet = GetNumberOfConsoleInputEvents (h, &nbuffer); |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
194 if (bRet) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
195 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
196 /* Input buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
197 *p_sought &= POLLIN | POLLRDNORM; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
198 if (nbuffer == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
199 return POLLHUP; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
200 if (!*p_sought) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
201 return 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
202 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
203 irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
204 bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
205 if (!bRet || avail == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
206 return POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
207 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
208 for (i = 0; i < avail; i++) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
209 if (irbuffer[i].EventType == KEY_EVENT) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
210 return *p_sought; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
211 return 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
212 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
213 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
214 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
215 /* Screen buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
216 *p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
217 return *p_sought; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
218 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
219 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
220 default: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
221 ret = WaitForSingleObject (h, 0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
222 if (ret == WAIT_OBJECT_0) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
223 return *p_sought & ~(POLLPRI | POLLRDBAND); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
224 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
225 return *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
226 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
227 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
228 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
229 /* Convert fd_sets returned by select into revents values. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
230 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
231 static int |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
232 windows_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
233 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
234 int happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
235 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
236 if ((lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) == FD_ACCEPT) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
237 happened |= (POLLIN | POLLRDNORM) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
238 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
239 else if (lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
240 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
241 int r, error; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
242 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
243 char data[64]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
244 WSASetLastError (0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
245 r = recv (h, data, sizeof (data), MSG_PEEK); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
246 error = WSAGetLastError (); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
247 WSASetLastError (0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
248 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
249 if (r > 0 || error == WSAENOTCONN) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
250 happened |= (POLLIN | POLLRDNORM) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
251 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
252 /* Distinguish hung-up sockets from other errors. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
253 else if (r == 0 || error == WSAESHUTDOWN || error == WSAECONNRESET |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
254 || error == WSAECONNABORTED || error == WSAENETRESET) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
255 happened |= POLLHUP; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
256 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
257 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
258 happened |= POLLERR; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
259 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
260 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
261 if (lNetworkEvents & (FD_WRITE | FD_CONNECT)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
262 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
263 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
264 if (lNetworkEvents & FD_OOB) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
265 happened |= (POLLPRI | POLLRDBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
266 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
267 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
268 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
269 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
270 #else /* !MinGW */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
271 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
272 /* Convert select(2) returned fd_sets into poll(2) revents values. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
273 static int |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
274 compute_revents (int fd, int sought, fd_set *rfds, fd_set *wfds, fd_set *efds) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
275 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
276 int happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
277 if (FD_ISSET (fd, rfds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
278 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
279 int r; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
280 int socket_errno; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
281 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
282 # if defined __MACH__ && defined __APPLE__ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
283 /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
284 for some kinds of descriptors. Detect if this descriptor is a |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
285 connected socket, a server socket, or something else using a |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
286 0-byte recv, and use ioctl(2) to detect POLLHUP. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
287 r = recv (fd, NULL, 0, MSG_PEEK); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
288 socket_errno = (r < 0) ? errno : 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
289 if (r == 0 || socket_errno == ENOTSOCK) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
290 ioctl (fd, FIONREAD, &r); |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
291 # else |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
292 char data[64]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
293 r = recv (fd, data, sizeof (data), MSG_PEEK); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
294 socket_errno = (r < 0) ? errno : 0; |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
295 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
296 if (r == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
297 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
298 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
299 /* If the event happened on an unconnected server socket, |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
300 that's fine. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
301 else if (r > 0 || ( /* (r == -1) && */ socket_errno == ENOTCONN)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
302 happened |= (POLLIN | POLLRDNORM) & sought; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
303 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
304 /* Distinguish hung-up sockets from other errors. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
305 else if (socket_errno == ESHUTDOWN || socket_errno == ECONNRESET |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
306 || socket_errno == ECONNABORTED || socket_errno == ENETRESET) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
307 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
308 |
17083
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
309 /* some systems can't use recv() on non-socket, including HP NonStop */ |
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
310 else if (socket_errno == ENOTSOCK) |
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
311 happened |= (POLLIN | POLLRDNORM) & sought; |
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
312 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
313 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
314 happened |= POLLERR; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
315 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
316 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
317 if (FD_ISSET (fd, wfds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
318 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
319 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
320 if (FD_ISSET (fd, efds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
321 happened |= (POLLPRI | POLLRDBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
322 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
323 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
324 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
325 #endif /* !MinGW */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
326 |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
327 int |
13741 | 328 poll (struct pollfd *pfd, nfds_t nfd, int timeout) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
329 { |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
330 #ifndef WINDOWS_NATIVE |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
331 fd_set rfds, wfds, efds; |
10310
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
332 struct timeval tv; |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
333 struct timeval *ptv; |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
334 int maxfd, rc; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
335 nfds_t i; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
336 |
17924
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
337 if (nfd < 0) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
338 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
339 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
340 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
341 } |
17924
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
342 /* Don't check directly for NFD too large. Any practical use of a |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
343 too-large NFD is caught by one of the other checks below, and |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
344 checking directly for getdtablesize is too much of a portability |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
345 and/or performance and/or correctness hassle. */ |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
346 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
347 /* EFAULT is not necessary to implement, but let's do it in the |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
348 simplest case. */ |
17084
723cfa7546c0
poll: fix poll(0, NULL, msec)
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
17083
diff
changeset
|
349 if (!pfd && nfd) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
350 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
351 errno = EFAULT; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
352 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
353 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
354 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
355 /* convert timeout number into a timeval structure */ |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
356 if (timeout == 0) |
10310
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
357 { |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
358 ptv = &tv; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
359 ptv->tv_sec = 0; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
360 ptv->tv_usec = 0; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
361 } |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
362 else if (timeout > 0) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
363 { |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
364 ptv = &tv; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
365 ptv->tv_sec = timeout / 1000; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
366 ptv->tv_usec = (timeout % 1000) * 1000; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
367 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
368 else if (timeout == INFTIM) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
369 /* wait forever */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
370 ptv = NULL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
371 else |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
372 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
373 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
374 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
375 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
376 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
377 /* create fd sets and determine max fd */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
378 maxfd = -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
379 FD_ZERO (&rfds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
380 FD_ZERO (&wfds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
381 FD_ZERO (&efds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
382 for (i = 0; i < nfd; i++) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
383 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
384 if (pfd[i].fd < 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
385 continue; |
17924
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
386 if (maxfd < pfd[i].fd) |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
387 { |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
388 maxfd = pfd[i].fd; |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
389 if (FD_SETSIZE <= maxfd) |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
390 { |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
391 errno = EINVAL; |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
392 return -1; |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
393 } |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
394 } |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
395 if (pfd[i].events & (POLLIN | POLLRDNORM)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
396 FD_SET (pfd[i].fd, &rfds); |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
397 /* see select(2): "the only exceptional condition detectable |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
398 is out-of-band data received on a socket", hence we push |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
399 POLLWRBAND events onto wfds instead of efds. */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
400 if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
401 FD_SET (pfd[i].fd, &wfds); |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
402 if (pfd[i].events & (POLLPRI | POLLRDBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
403 FD_SET (pfd[i].fd, &efds); |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
404 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
405 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
406 /* examine fd sets */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
407 rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv); |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
408 if (rc < 0) |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
409 return rc; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
410 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
411 /* establish results */ |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
412 rc = 0; |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
413 for (i = 0; i < nfd; i++) |
17924
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
414 { |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
415 pfd[i].revents = (pfd[i].fd < 0 |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
416 ? 0 |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
417 : compute_revents (pfd[i].fd, pfd[i].events, |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
418 &rfds, &wfds, &efds)); |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
419 rc += pfd[i].revents != 0; |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
420 } |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
421 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
422 return rc; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
423 #else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
424 static struct timeval tv0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
425 static HANDLE hEvent; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
426 WSANETWORKEVENTS ev; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
427 HANDLE h, handle_array[FD_SETSIZE + 2]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
428 DWORD ret, wait_timeout, nhandles; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
429 fd_set rfds, wfds, xfds; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
430 BOOL poll_again; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
431 MSG msg; |
10996
724ce887ae63
poll: don't return uninitialized
Daniel P. Berrange <berrange@redhat.com>
parents:
10562
diff
changeset
|
432 int rc = 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
433 nfds_t i; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
434 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
435 if (nfd < 0 || timeout < -1) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
436 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
437 errno = EINVAL; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
438 return -1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
439 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
440 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
441 if (!hEvent) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
442 hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
443 |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
444 restart: |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
445 handle_array[0] = hEvent; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
446 nhandles = 1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
447 FD_ZERO (&rfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
448 FD_ZERO (&wfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
449 FD_ZERO (&xfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
450 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
451 /* Classify socket handles and create fd sets. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
452 for (i = 0; i < nfd; i++) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
453 { |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
454 int sought = pfd[i].events; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
455 pfd[i].revents = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
456 if (pfd[i].fd < 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
457 continue; |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
458 if (!(sought & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
459 | POLLPRI | POLLRDBAND))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
460 continue; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
461 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
462 h = (HANDLE) _get_osfhandle (pfd[i].fd); |
17840 | 463 assure (h != NULL); |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
464 if (IsSocketHandle (h)) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
465 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
466 int requested = FD_CLOSE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
467 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
468 /* see above; socket handles are mapped onto select. */ |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
469 if (sought & (POLLIN | POLLRDNORM)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
470 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
471 requested |= FD_READ | FD_ACCEPT; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
472 FD_SET ((SOCKET) h, &rfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
473 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
474 if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
475 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
476 requested |= FD_WRITE | FD_CONNECT; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
477 FD_SET ((SOCKET) h, &wfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
478 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
479 if (sought & (POLLPRI | POLLRDBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
480 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
481 requested |= FD_OOB; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
482 FD_SET ((SOCKET) h, &xfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
483 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
484 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
485 if (requested) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
486 WSAEventSelect ((SOCKET) h, hEvent, requested); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
487 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
488 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
489 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
490 /* Poll now. If we get an event, do not poll again. Also, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
491 screen buffer handles are waitable, and they'll block until |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
492 a character is available. windows_compute_revents eliminates |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
493 bits for the "wrong" direction. */ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
494 pfd[i].revents = windows_compute_revents (h, &sought); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
495 if (sought) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
496 handle_array[nhandles++] = h; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
497 if (pfd[i].revents) |
13574
ac05886b34b6
poll: return immediately on POLLHUP.
Erik Faye-Lund <kusmabite@gmail.com>
parents:
13565
diff
changeset
|
498 timeout = 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
499 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
500 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
501 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
502 if (select (0, &rfds, &wfds, &xfds, &tv0) > 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
503 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
504 /* Do MsgWaitForMultipleObjects anyway to dispatch messages, but |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
505 no need to call select again. */ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
506 poll_again = FALSE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
507 wait_timeout = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
508 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
509 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
510 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
511 poll_again = TRUE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
512 if (timeout == INFTIM) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
513 wait_timeout = INFINITE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
514 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
515 wait_timeout = timeout; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
516 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
517 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
518 for (;;) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
519 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
520 ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
521 wait_timeout, QS_ALLINPUT); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
522 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
523 if (ret == WAIT_OBJECT_0 + nhandles) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
524 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
525 /* new input of some other kind */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
526 BOOL bRet; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
527 while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
528 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
529 TranslateMessage (&msg); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
530 DispatchMessage (&msg); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
531 } |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
532 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
533 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
534 break; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
535 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
536 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
537 if (poll_again) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
538 select (0, &rfds, &wfds, &xfds, &tv0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
539 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
540 /* Place a sentinel at the end of the array. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
541 handle_array[nhandles] = NULL; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
542 nhandles = 1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
543 for (i = 0; i < nfd; i++) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
544 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
545 int happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
546 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
547 if (pfd[i].fd < 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
548 continue; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
549 if (!(pfd[i].events & (POLLIN | POLLRDNORM | |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
550 POLLOUT | POLLWRNORM | POLLWRBAND))) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
551 continue; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
552 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
553 h = (HANDLE) _get_osfhandle (pfd[i].fd); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
554 if (h != handle_array[nhandles]) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
555 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
556 /* It's a socket. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
557 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
558 WSAEventSelect ((SOCKET) h, 0, 0); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
559 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
560 /* If we're lucky, WSAEnumNetworkEvents already provided a way |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
561 to distinguish FD_READ and FD_ACCEPT; this saves a recv later. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
562 if (FD_ISSET ((SOCKET) h, &rfds) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
563 && !(ev.lNetworkEvents & (FD_READ | FD_ACCEPT))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
564 ev.lNetworkEvents |= FD_READ | FD_ACCEPT; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
565 if (FD_ISSET ((SOCKET) h, &wfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
566 ev.lNetworkEvents |= FD_WRITE | FD_CONNECT; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
567 if (FD_ISSET ((SOCKET) h, &xfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
568 ev.lNetworkEvents |= FD_OOB; |
10562
ebbb50a36a9f
remove trailing spaces
Jim Meyering <meyering@redhat.com>
parents:
10467
diff
changeset
|
569 |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
570 happened = windows_compute_revents_socket ((SOCKET) h, pfd[i].events, |
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
571 ev.lNetworkEvents); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
572 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
573 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
574 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
575 /* Not a socket. */ |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
576 int sought = pfd[i].events; |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
577 happened = windows_compute_revents (h, &sought); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
578 nhandles++; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
579 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
580 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
581 if ((pfd[i].revents |= happened) != 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
582 rc++; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
583 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
584 |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
585 if (!rc && timeout == INFTIM) |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
586 { |
16869
38e6d30d7e4d
poll/select: prevent busy-waiting
Paolo Bonzini <bonzini@gnu.org>
parents:
16366
diff
changeset
|
587 SleepEx (1, TRUE); |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
588 goto restart; |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
589 } |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
590 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
591 return rc; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
592 #endif |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
593 } |