annotate lib/poll.c @ 13565:69913822a0f7

poll, select: handle ERROR_BROKEN_PIPE. * lib/poll.c (win32_compute_revents): Return POLLHUP when PeekNamedPipe fails with ERROR_BROKEN_PIPE. * lib/select.c (win32_compute_revents): Do not mark a pipe as writeable if PeekNamedPipe fails with ERROR_BROKEN_PIPE.
author Paolo Bonzini <pbonzini@redhat.com>
date Mon, 23 Aug 2010 09:35:43 +0200
parents 094f6cfdb5c3
children ac05886b34b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
12559
c2cbabec01dd update nearly all FSF copyright year lists to include 2010
Jim Meyering <meyering@redhat.com>
parents: 12421
diff changeset
4 Copyright 2001-2003, 2006-2010 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
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
8 This program is free software; you can redistribute it and/or modify
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
9 it under the terms of the GNU General Public License as published by
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
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
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
13 This program is distributed in the hope that it will be useful,
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
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
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
18 You should have received a copy of the GNU General Public License along
d4efdb886b2a Switch from LGPL to GPL.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4242
diff changeset
19 with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 4435
diff changeset
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
11020
4494a99c424d poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents: 11019
diff changeset
22 /* 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
23 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
4494a99c424d poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents: 11019
diff changeset
24 # pragma GCC diagnostic ignored "-Wtype-limits"
4494a99c424d poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents: 11019
diff changeset
25 #endif
4494a99c424d poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents: 11019
diff changeset
26
9644
42367c277240 Prefer <config.h> over "config.h". See autoconf doc for explanation.
Jim Meyering <meyering@redhat.com>
parents: 8500
diff changeset
27 #include <config.h>
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
28 #include <alloca.h>
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <sys/types.h>
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include "poll.h"
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #include <errno.h>
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #include <limits.h>
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
34 #include <assert.h>
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
35
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
36 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
37 # define WIN32_NATIVE
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
38 # include <winsock2.h>
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
39 # include <windows.h>
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
40 # include <io.h>
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
41 # include <stdio.h>
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
42 # include <conio.h>
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
43 #else
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
44 # include <sys/time.h>
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
45 # include <sys/socket.h>
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
46 # include <sys/select.h>
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
47 # include <unistd.h>
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
48 #endif
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
7382
8eedfb060b79 2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 6259
diff changeset
50 #ifdef HAVE_SYS_IOCTL_H
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
51 # include <sys/ioctl.h>
7382
8eedfb060b79 2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 6259
diff changeset
52 #endif
8eedfb060b79 2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 6259
diff changeset
53 #ifdef HAVE_SYS_FILIO_H
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
54 # include <sys/filio.h>
7382
8eedfb060b79 2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 6259
diff changeset
55 #endif
8eedfb060b79 2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 6259
diff changeset
56
7908
d41d48e822b4 * lib/poll.c: Include sys/time.h and time.h unconditionally,
Paul Eggert <eggert@cs.ucla.edu>
parents: 7776
diff changeset
57 #include <time.h>
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 #ifndef INFTIM
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
60 # define INFTIM (-1)
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 #endif
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62
8500
6c3093c67379 Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 7921
diff changeset
63 /* BeOS does not have MSG_PEEK. */
6c3093c67379 Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 7921
diff changeset
64 #ifndef MSG_PEEK
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
65 # define MSG_PEEK 0
8500
6c3093c67379 Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 7921
diff changeset
66 #endif
6c3093c67379 Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 7921
diff changeset
67
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
68 #ifdef WIN32_NATIVE
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
69
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
70 #define IsConsoleHandle(h) (((long) (h) & 3) == 3)
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
71
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
72 static BOOL
13051
094f6cfdb5c3 Minor formatting changes.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
73 IsSocketHandle (HANDLE h)
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
74 {
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
75 WSANETWORKEVENTS ev;
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
76
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
77 if (IsConsoleHandle (h))
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
78 return FALSE;
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 /* 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
81 WSAEnumNetworkEvents instead distinguishes the two correctly. */
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
82 ev.lNetworkEvents = 0xDEADBEEF;
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
83 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
84 return ev.lNetworkEvents != 0xDEADBEEF;
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
85 }
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
86
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
87 /* Declare data structures for ntdll functions. */
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
88 typedef struct _FILE_PIPE_LOCAL_INFORMATION {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
89 ULONG NamedPipeType;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
90 ULONG NamedPipeConfiguration;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
91 ULONG MaximumInstances;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
92 ULONG CurrentInstances;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
93 ULONG InboundQuota;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
94 ULONG ReadDataAvailable;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
95 ULONG OutboundQuota;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
96 ULONG WriteQuotaAvailable;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
97 ULONG NamedPipeState;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
98 ULONG NamedPipeEnd;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
99 } 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
100
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
101 typedef struct _IO_STATUS_BLOCK
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
102 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
103 union {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
104 DWORD Status;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
105 PVOID Pointer;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
106 } u;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
107 ULONG_PTR Information;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
108 } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
109
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
110 typedef enum _FILE_INFORMATION_CLASS {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
111 FilePipeLocalInformation = 24
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
112 } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
113
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
114 typedef DWORD (WINAPI *PNtQueryInformationFile)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
115 (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
116
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
117 # ifndef PIPE_BUF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
118 # define PIPE_BUF 512
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
119 # endif
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
120
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
121 /* 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
122 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
123
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
124 static int
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
125 win32_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
126 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
127 int i, ret, happened;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
128 INPUT_RECORD *irbuffer;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
129 DWORD avail, nbuffer;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
130 BOOL bRet;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
131 IO_STATUS_BLOCK iosb;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
132 FILE_PIPE_LOCAL_INFORMATION fpli;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
133 static PNtQueryInformationFile NtQueryInformationFile;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
134 static BOOL once_only;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
135
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
136 switch (GetFileType (h))
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
137 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
138 case FILE_TYPE_PIPE:
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
139 if (!once_only)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
140 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
141 NtQueryInformationFile = (PNtQueryInformationFile)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
142 GetProcAddress (GetModuleHandle ("ntdll.dll"),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
143 "NtQueryInformationFile");
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
144 once_only = TRUE;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
145 }
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
146
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
147 happened = 0;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
148 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
149 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
150 if (avail)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
151 happened |= *p_sought & (POLLIN | POLLRDNORM);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
152 }
13565
69913822a0f7 poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents: 13051
diff changeset
153 else if (GetLastError () == ERROR_BROKEN_PIPE)
69913822a0f7 poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents: 13051
diff changeset
154 happened |= POLLHUP;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
155
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
156 else
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 /* 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
159 If NtQueryInformationFile fails, optimistically assume the pipe is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
160 writable. This could happen on Win9x, where NtQueryInformationFile
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
161 is not available, or if we inherit a pipe that doesn't permit
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
162 FILE_READ_ATTRIBUTES access on the write end (I think this should
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
163 not happen since WinXP SP2; WINE seems fine too). Otherwise,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
164 ensure that enough space is available for atomic writes. */
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
165 memset (&iosb, 0, sizeof (iosb));
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
166 memset (&fpli, 0, sizeof (fpli));
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
167
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
168 if (!NtQueryInformationFile
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
169 || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli),
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
170 FilePipeLocalInformation)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
171 || fpli.WriteQuotaAvailable >= PIPE_BUF
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
172 || (fpli.OutboundQuota < PIPE_BUF &&
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
173 fpli.WriteQuotaAvailable == fpli.OutboundQuota))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
174 happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
175 }
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
176 return happened;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
177
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
178 case FILE_TYPE_CHAR:
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
179 ret = WaitForSingleObject (h, 0);
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
180 if (!IsConsoleHandle (h))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
181 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
182
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
183 nbuffer = avail = 0;
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
184 bRet = GetNumberOfConsoleInputEvents (h, &nbuffer);
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
185 if (bRet)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
186 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
187 /* Input buffer. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
188 *p_sought &= POLLIN | POLLRDNORM;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
189 if (nbuffer == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
190 return POLLHUP;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
191 if (!*p_sought)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
192 return 0;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
193
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
194 irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
195 bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
196 if (!bRet || avail == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
197 return POLLHUP;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
198
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
199 for (i = 0; i < avail; i++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
200 if (irbuffer[i].EventType == KEY_EVENT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
201 return *p_sought;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
202 return 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
203 }
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
204 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
205 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
206 /* Screen buffer. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
207 *p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
208 return *p_sought;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
209 }
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
210
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
211 default:
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
212 ret = WaitForSingleObject (h, 0);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
213 if (ret == WAIT_OBJECT_0)
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
214 return *p_sought & ~(POLLPRI | POLLRDBAND);
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
215
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
216 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
217 }
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
218 }
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 /* 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
221
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
222 static int
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
223 win32_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
224 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
225 int happened = 0;
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 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
228 happened |= (POLLIN | POLLRDNORM) & sought;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
229
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
230 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
231 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
232 int r, error;
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 char data[64];
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
235 WSASetLastError (0);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
236 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
237 error = WSAGetLastError ();
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
238 WSASetLastError (0);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
239
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
240 if (r > 0 || error == WSAENOTCONN)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
241 happened |= (POLLIN | POLLRDNORM) & sought;
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 /* 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
244 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
245 || error == WSAECONNABORTED || error == WSAENETRESET)
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
246 happened |= POLLHUP;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
247
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
248 else
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
249 happened |= POLLERR;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
250 }
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 if (lNetworkEvents & (FD_WRITE | FD_CONNECT))
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
253 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
254
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
255 if (lNetworkEvents & FD_OOB)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
256 happened |= (POLLPRI | POLLRDBAND) & sought;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
257
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
258 return happened;
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 #else /* !MinGW */
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
262
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
263 /* 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
264 static int
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
265 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
266 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
267 int happened = 0;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
268 if (FD_ISSET (fd, rfds))
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 int r;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
271 int socket_errno;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
272
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
273 # if defined __MACH__ && defined __APPLE__
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
274 /* 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
275 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
276 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
277 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
278 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
279 socket_errno = (r < 0) ? errno : 0;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
280 if (r == 0 || socket_errno == ENOTSOCK)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
281 ioctl (fd, FIONREAD, &r);
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
282 # else
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
283 char data[64];
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
284 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
285 socket_errno = (r < 0) ? errno : 0;
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
286 # endif
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
287 if (r == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
288 happened |= POLLHUP;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
289
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
290 /* 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
291 that's fine. */
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
292 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
293 happened |= (POLLIN | POLLRDNORM) & sought;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
294
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
295 /* 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
296 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
297 || socket_errno == ECONNABORTED || socket_errno == ENETRESET)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
298 happened |= POLLHUP;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
299
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
300 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
301 happened |= POLLERR;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
302 }
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 if (FD_ISSET (fd, wfds))
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
305 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
306
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
307 if (FD_ISSET (fd, efds))
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
308 happened |= (POLLPRI | POLLRDBAND) & sought;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
309
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
310 return happened;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
311 }
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
312 #endif /* !MinGW */
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
313
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 int
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 poll (pfd, nfd, timeout)
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 struct pollfd *pfd;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 nfds_t nfd;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 int timeout;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 {
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
320 #ifndef WIN32_NATIVE
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 fd_set rfds, wfds, efds;
10310
b48c3c82f4d9 Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents: 10308
diff changeset
322 struct timeval tv;
10308
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
323 struct timeval *ptv;
7776
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
324 int maxfd, rc;
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 nfds_t i;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
327 # ifdef _SC_OPEN_MAX
10308
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
328 static int sc_open_max = -1;
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
329
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
330 if (nfd < 0
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
331 || (nfd > sc_open_max
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
332 && (sc_open_max != -1
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
333 || nfd > (sc_open_max = sysconf (_SC_OPEN_MAX)))))
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 {
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335 errno = EINVAL;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 return -1;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 }
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
338 # else /* !_SC_OPEN_MAX */
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
339 # ifdef OPEN_MAX
10308
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
340 if (nfd < 0 || nfd > OPEN_MAX)
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 {
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 errno = EINVAL;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 return -1;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 }
10997
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
345 # endif /* OPEN_MAX -- else, no check is needed */
92c82a6f26db poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents: 10996
diff changeset
346 # endif /* !_SC_OPEN_MAX */
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 /* 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
349 simplest case. */
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 if (!pfd)
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 {
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 errno = EFAULT;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 return -1;
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
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 /* convert timeout number into a timeval structure */
10308
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
357 if (timeout == 0)
10310
b48c3c82f4d9 Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents: 10308
diff changeset
358 {
b48c3c82f4d9 Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents: 10308
diff changeset
359 ptv = &tv;
b48c3c82f4d9 Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents: 10308
diff changeset
360 ptv->tv_sec = 0;
b48c3c82f4d9 Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents: 10308
diff changeset
361 ptv->tv_usec = 0;
b48c3c82f4d9 Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents: 10308
diff changeset
362 }
10308
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
363 else if (timeout > 0)
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 {
10308
c3ecbe083a26 microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents: 9831
diff changeset
365 ptv = &tv;
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 ptv->tv_sec = timeout / 1000;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 ptv->tv_usec = (timeout % 1000) * 1000;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 }
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 else if (timeout == INFTIM)
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 /* wait forever */
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 ptv = NULL;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 else
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 {
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 errno = EINVAL;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 return -1;
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
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 /* 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
379 maxfd = -1;
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 FD_ZERO (&rfds);
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 FD_ZERO (&wfds);
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 FD_ZERO (&efds);
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 for (i = 0; i < nfd; i++)
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 {
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385 if (pfd[i].fd < 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
386 continue;
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 if (pfd[i].events & (POLLIN | POLLRDNORM))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
389 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
390
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391 /* 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
392 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
393 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
394 if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
395 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
396 if (pfd[i].events & (POLLPRI | POLLRDBAND))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
397 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
398 if (pfd[i].fd >= maxfd
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
399 && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
400 | POLLRDNORM | POLLRDBAND
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
401 | POLLWRNORM | POLLWRBAND)))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
402 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
403 maxfd = pfd[i].fd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
404 if (maxfd > FD_SETSIZE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
405 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
406 errno = EOVERFLOW;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
407 return -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
408 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
409 }
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
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 /* examine fd sets */
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 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
414 if (rc < 0)
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
415 return rc;
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 /* establish results */
7776
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
418 rc = 0;
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
419 for (i = 0; i < nfd; i++)
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
420 if (pfd[i].fd < 0)
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
421 pfd[i].revents = 0;
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
422 else
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
423 {
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
424 int happened = compute_revents (pfd[i].fd, pfd[i].events,
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
425 &rfds, &wfds, &efds);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
426 if (happened)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
427 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
428 pfd[i].revents = happened;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
429 rc++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
430 }
7776
35d644ee8b48 2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents: 7382
diff changeset
431 }
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
433 return rc;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
434 #else
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
435 static struct timeval tv0;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
436 static HANDLE hEvent;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
437 WSANETWORKEVENTS ev;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
438 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
439 DWORD ret, wait_timeout, nhandles;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
440 fd_set rfds, wfds, xfds;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
441 BOOL poll_again;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
442 MSG msg;
10996
724ce887ae63 poll: don't return uninitialized
Daniel P. Berrange <berrange@redhat.com>
parents: 10562
diff changeset
443 int rc = 0;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
444 nfds_t i;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
445
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
446 if (nfd < 0 || timeout < -1)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
447 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
448 errno = EINVAL;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
449 return -1;
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
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
452 if (!hEvent)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
453 hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
454
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
455 handle_array[0] = hEvent;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
456 nhandles = 1;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
457 FD_ZERO (&rfds);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
458 FD_ZERO (&wfds);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
459 FD_ZERO (&xfds);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
460
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
461 /* 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
462 for (i = 0; i < nfd; i++)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
463 {
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
464 int sought = pfd[i].events;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
465 pfd[i].revents = 0;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
466 if (pfd[i].fd < 0)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
467 continue;
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
468 if (!(sought & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
469 | POLLPRI | POLLRDBAND)))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
470 continue;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
471
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
472 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
473 assert (h != NULL);
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
474 if (IsSocketHandle (h))
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
475 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
476 int requested = FD_CLOSE;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
477
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
478 /* 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
479 if (sought & (POLLIN | POLLRDNORM))
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_READ | FD_ACCEPT;
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
482 FD_SET ((SOCKET) h, &rfds);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
483 }
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
484 if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
485 {
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
486 requested |= FD_WRITE | FD_CONNECT;
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
487 FD_SET ((SOCKET) h, &wfds);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
488 }
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
489 if (sought & (POLLPRI | POLLRDBAND))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
490 {
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
491 requested |= FD_OOB;
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
492 FD_SET ((SOCKET) h, &xfds);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
493 }
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
494
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
495 if (requested)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
496 WSAEventSelect ((SOCKET) h, hEvent, requested);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
497 }
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
498 else
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
499 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
500 /* 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
501 screen buffer handles are waitable, and they'll block until
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
502 a character is available. win32_compute_revents eliminates
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
503 bits for the "wrong" direction. */
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
504 pfd[i].revents = win32_compute_revents (h, &sought);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
505 if (sought)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
506 handle_array[nhandles++] = h;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
507 if (pfd[i].revents)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
508 wait_timeout = 0;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
509 }
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
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
512 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
513 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
514 /* Do MsgWaitForMultipleObjects anyway to dispatch messages, but
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
515 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
516 poll_again = FALSE;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
517 wait_timeout = 0;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
518 }
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
519 else
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
520 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
521 poll_again = TRUE;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
522 if (timeout == INFTIM)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
523 wait_timeout = INFINITE;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
524 else
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
525 wait_timeout = timeout;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
526 }
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
527
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
528 for (;;)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
529 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
530 ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
531 wait_timeout, QS_ALLINPUT);
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
532
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
533 if (ret == WAIT_OBJECT_0 + nhandles)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
534 {
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
535 /* new input of some other kind */
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
536 BOOL bRet;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
537 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
538 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
539 TranslateMessage (&msg);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
540 DispatchMessage (&msg);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
541 }
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
542 }
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
543 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
544 break;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
545 }
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 (poll_again)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
548 select (0, &rfds, &wfds, &xfds, &tv0);
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
549
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
550 /* 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
551 handle_array[nhandles] = NULL;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
552 nhandles = 1;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
553 for (i = 0; i < nfd; i++)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
554 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
555 int happened;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
556
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
557 if (pfd[i].fd < 0)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
558 continue;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
559 if (!(pfd[i].events & (POLLIN | POLLRDNORM |
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
560 POLLOUT | POLLWRNORM | POLLWRBAND)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
561 continue;
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
562
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
563 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
564 if (h != handle_array[nhandles])
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
565 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
566 /* It's a socket. */
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
567 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
568 WSAEventSelect ((SOCKET) h, 0, 0);
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
569
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
570 /* 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
571 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
572 if (FD_ISSET ((SOCKET) h, &rfds)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
573 && !(ev.lNetworkEvents & (FD_READ | FD_ACCEPT)))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
574 ev.lNetworkEvents |= FD_READ | FD_ACCEPT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
575 if (FD_ISSET ((SOCKET) h, &wfds))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
576 ev.lNetworkEvents |= FD_WRITE | FD_CONNECT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
577 if (FD_ISSET ((SOCKET) h, &xfds))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
578 ev.lNetworkEvents |= FD_OOB;
10562
ebbb50a36a9f remove trailing spaces
Jim Meyering <meyering@redhat.com>
parents: 10467
diff changeset
579
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
580 happened = win32_compute_revents_socket ((SOCKET) h, pfd[i].events,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11852
diff changeset
581 ev.lNetworkEvents);
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
582 }
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
583 else
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
584 {
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
585 /* Not a socket. */
11852
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
586 int sought = pfd[i].events;
c9032834d889 Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents: 11020
diff changeset
587 happened = win32_compute_revents (h, &sought);
10467
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
588 nhandles++;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
589 }
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
590
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
591 if ((pfd[i].revents |= happened) != 0)
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
592 rc++;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
593 }
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
594
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
595 return rc;
f96117307f4a implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents: 10414
diff changeset
596 #endif
4242
eec6ba9ed532 New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
597 }