PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wget-1.13.4/lib/select.c

#
C | 493 lines | 372 code | 64 blank | 57 comment | 92 complexity | 7bb748a4eaed7d897bcb25dec6d7d7cc MD5 | raw file
Possible License(s): LGPL-2.0, GPL-3.0
  1. /* -*- buffer-read-only: t -*- vi: set ro: */
  2. /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
  3. /* Emulation for select(2)
  4. Contributed by Paolo Bonzini.
  5. Copyright 2008-2011 Free Software Foundation, Inc.
  6. This file is part of gnulib.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along
  16. with this program; if not, write to the Free Software Foundation,
  17. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  18. #include <config.h>
  19. #include <alloca.h>
  20. #include <assert.h>
  21. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  22. /* Native Win32. */
  23. #include <sys/types.h>
  24. #include <stdbool.h>
  25. #include <errno.h>
  26. #include <limits.h>
  27. #include <winsock2.h>
  28. #include <windows.h>
  29. #include <io.h>
  30. #include <stdio.h>
  31. #include <conio.h>
  32. #include <time.h>
  33. struct bitset {
  34. unsigned char in[FD_SETSIZE / CHAR_BIT];
  35. unsigned char out[FD_SETSIZE / CHAR_BIT];
  36. };
  37. /* Declare data structures for ntdll functions. */
  38. typedef struct _FILE_PIPE_LOCAL_INFORMATION {
  39. ULONG NamedPipeType;
  40. ULONG NamedPipeConfiguration;
  41. ULONG MaximumInstances;
  42. ULONG CurrentInstances;
  43. ULONG InboundQuota;
  44. ULONG ReadDataAvailable;
  45. ULONG OutboundQuota;
  46. ULONG WriteQuotaAvailable;
  47. ULONG NamedPipeState;
  48. ULONG NamedPipeEnd;
  49. } FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION;
  50. typedef struct _IO_STATUS_BLOCK
  51. {
  52. union {
  53. DWORD Status;
  54. PVOID Pointer;
  55. } u;
  56. ULONG_PTR Information;
  57. } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
  58. typedef enum _FILE_INFORMATION_CLASS {
  59. FilePipeLocalInformation = 24
  60. } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;
  61. typedef DWORD (WINAPI *PNtQueryInformationFile)
  62. (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS);
  63. #ifndef PIPE_BUF
  64. #define PIPE_BUF 512
  65. #endif
  66. #define IsConsoleHandle(h) (((long) (h) & 3) == 3)
  67. static BOOL
  68. IsSocketHandle (HANDLE h)
  69. {
  70. WSANETWORKEVENTS ev;
  71. if (IsConsoleHandle (h))
  72. return FALSE;
  73. /* Under Wine, it seems that getsockopt returns 0 for pipes too.
  74. WSAEnumNetworkEvents instead distinguishes the two correctly. */
  75. ev.lNetworkEvents = 0xDEADBEEF;
  76. WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
  77. return ev.lNetworkEvents != 0xDEADBEEF;
  78. }
  79. /* Compute output fd_sets for libc descriptor FD (whose Win32 handle is H). */
  80. static int
  81. win32_poll_handle (HANDLE h, int fd, struct bitset *rbits, struct bitset *wbits,
  82. struct bitset *xbits)
  83. {
  84. BOOL read, write, except;
  85. int i, ret;
  86. INPUT_RECORD *irbuffer;
  87. DWORD avail, nbuffer;
  88. BOOL bRet;
  89. IO_STATUS_BLOCK iosb;
  90. FILE_PIPE_LOCAL_INFORMATION fpli;
  91. static PNtQueryInformationFile NtQueryInformationFile;
  92. static BOOL once_only;
  93. read = write = except = FALSE;
  94. switch (GetFileType (h))
  95. {
  96. case FILE_TYPE_DISK:
  97. read = TRUE;
  98. write = TRUE;
  99. break;
  100. case FILE_TYPE_PIPE:
  101. if (!once_only)
  102. {
  103. NtQueryInformationFile = (PNtQueryInformationFile)
  104. GetProcAddress (GetModuleHandle ("ntdll.dll"),
  105. "NtQueryInformationFile");
  106. once_only = TRUE;
  107. }
  108. if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0)
  109. {
  110. if (avail)
  111. read = TRUE;
  112. }
  113. else if (GetLastError () == ERROR_BROKEN_PIPE)
  114. ;
  115. else
  116. {
  117. /* It was the write-end of the pipe. Check if it is writable.
  118. If NtQueryInformationFile fails, optimistically assume the pipe is
  119. writable. This could happen on Win9x, where NtQueryInformationFile
  120. is not available, or if we inherit a pipe that doesn't permit
  121. FILE_READ_ATTRIBUTES access on the write end (I think this should
  122. not happen since WinXP SP2; WINE seems fine too). Otherwise,
  123. ensure that enough space is available for atomic writes. */
  124. memset (&iosb, 0, sizeof (iosb));
  125. memset (&fpli, 0, sizeof (fpli));
  126. if (!NtQueryInformationFile
  127. || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli),
  128. FilePipeLocalInformation)
  129. || fpli.WriteQuotaAvailable >= PIPE_BUF
  130. || (fpli.OutboundQuota < PIPE_BUF &&
  131. fpli.WriteQuotaAvailable == fpli.OutboundQuota))
  132. write = TRUE;
  133. }
  134. break;
  135. case FILE_TYPE_CHAR:
  136. write = TRUE;
  137. if (!(rbits->in[fd / CHAR_BIT] & (1 << (fd & (CHAR_BIT - 1)))))
  138. break;
  139. ret = WaitForSingleObject (h, 0);
  140. if (ret == WAIT_OBJECT_0)
  141. {
  142. if (!IsConsoleHandle (h))
  143. {
  144. read = TRUE;
  145. break;
  146. }
  147. nbuffer = avail = 0;
  148. bRet = GetNumberOfConsoleInputEvents (h, &nbuffer);
  149. /* Screen buffers handles are filtered earlier. */
  150. assert (bRet);
  151. if (nbuffer == 0)
  152. {
  153. except = TRUE;
  154. break;
  155. }
  156. irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD));
  157. bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail);
  158. if (!bRet || avail == 0)
  159. {
  160. except = TRUE;
  161. break;
  162. }
  163. for (i = 0; i < avail; i++)
  164. if (irbuffer[i].EventType == KEY_EVENT)
  165. read = TRUE;
  166. }
  167. break;
  168. default:
  169. ret = WaitForSingleObject (h, 0);
  170. write = TRUE;
  171. if (ret == WAIT_OBJECT_0)
  172. read = TRUE;
  173. break;
  174. }
  175. ret = 0;
  176. if (read && (rbits->in[fd / CHAR_BIT] & (1 << (fd & (CHAR_BIT - 1)))))
  177. {
  178. rbits->out[fd / CHAR_BIT] |= (1 << (fd & (CHAR_BIT - 1)));
  179. ret++;
  180. }
  181. if (write && (wbits->in[fd / CHAR_BIT] & (1 << (fd & (CHAR_BIT - 1)))))
  182. {
  183. wbits->out[fd / CHAR_BIT] |= (1 << (fd & (CHAR_BIT - 1)));
  184. ret++;
  185. }
  186. if (except && (xbits->in[fd / CHAR_BIT] & (1 << (fd & (CHAR_BIT - 1)))))
  187. {
  188. xbits->out[fd / CHAR_BIT] |= (1 << (fd & (CHAR_BIT - 1)));
  189. ret++;
  190. }
  191. return ret;
  192. }
  193. int
  194. rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds,
  195. struct timeval *timeout)
  196. {
  197. static struct timeval tv0;
  198. static HANDLE hEvent;
  199. HANDLE h, handle_array[FD_SETSIZE + 2];
  200. fd_set handle_rfds, handle_wfds, handle_xfds;
  201. struct bitset rbits, wbits, xbits;
  202. unsigned char anyfds_in[FD_SETSIZE / CHAR_BIT];
  203. DWORD ret, wait_timeout, nhandles, nsock, nbuffer;
  204. MSG msg;
  205. int i, fd, rc;
  206. if (nfds > FD_SETSIZE)
  207. nfds = FD_SETSIZE;
  208. if (!timeout)
  209. wait_timeout = INFINITE;
  210. else
  211. {
  212. wait_timeout = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  213. /* select is also used as a portable usleep. */
  214. if (!rfds && !wfds && !xfds)
  215. {
  216. Sleep (wait_timeout);
  217. return 0;
  218. }
  219. }
  220. if (!hEvent)
  221. hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
  222. handle_array[0] = hEvent;
  223. nhandles = 1;
  224. nsock = 0;
  225. /* Copy descriptors to bitsets. At the same time, eliminate
  226. bits in the "wrong" direction for console input buffers
  227. and screen buffers, because screen buffers are waitable
  228. and they will block until a character is available. */
  229. memset (&rbits, 0, sizeof (rbits));
  230. memset (&wbits, 0, sizeof (wbits));
  231. memset (&xbits, 0, sizeof (xbits));
  232. memset (anyfds_in, 0, sizeof (anyfds_in));
  233. if (rfds)
  234. for (i = 0; i < rfds->fd_count; i++)
  235. {
  236. fd = rfds->fd_array[i];
  237. h = (HANDLE) _get_osfhandle (fd);
  238. if (IsConsoleHandle (h)
  239. && !GetNumberOfConsoleInputEvents (h, &nbuffer))
  240. continue;
  241. rbits.in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1));
  242. anyfds_in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1));
  243. }
  244. else
  245. rfds = (fd_set *) alloca (sizeof (fd_set));
  246. if (wfds)
  247. for (i = 0; i < wfds->fd_count; i++)
  248. {
  249. fd = wfds->fd_array[i];
  250. h = (HANDLE) _get_osfhandle (fd);
  251. if (IsConsoleHandle (h)
  252. && GetNumberOfConsoleInputEvents (h, &nbuffer))
  253. continue;
  254. wbits.in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1));
  255. anyfds_in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1));
  256. }
  257. else
  258. wfds = (fd_set *) alloca (sizeof (fd_set));
  259. if (xfds)
  260. for (i = 0; i < xfds->fd_count; i++)
  261. {
  262. fd = xfds->fd_array[i];
  263. xbits.in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1));
  264. anyfds_in[fd / CHAR_BIT] |= 1 << (fd & (CHAR_BIT - 1));
  265. }
  266. else
  267. xfds = (fd_set *) alloca (sizeof (fd_set));
  268. /* Zero all the fd_sets, including the application's. */
  269. FD_ZERO (rfds);
  270. FD_ZERO (wfds);
  271. FD_ZERO (xfds);
  272. FD_ZERO (&handle_rfds);
  273. FD_ZERO (&handle_wfds);
  274. FD_ZERO (&handle_xfds);
  275. /* Classify handles. Create fd sets for sockets, poll the others. */
  276. for (i = 0; i < nfds; i++)
  277. {
  278. if ((anyfds_in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) == 0)
  279. continue;
  280. h = (HANDLE) _get_osfhandle (i);
  281. if (!h)
  282. {
  283. errno = EBADF;
  284. return -1;
  285. }
  286. if (IsSocketHandle (h))
  287. {
  288. int requested = FD_CLOSE;
  289. /* See above; socket handles are mapped onto select, but we
  290. need to map descriptors to handles. */
  291. if (rbits.in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
  292. {
  293. requested |= FD_READ | FD_ACCEPT;
  294. FD_SET ((SOCKET) h, rfds);
  295. FD_SET ((SOCKET) h, &handle_rfds);
  296. }
  297. if (wbits.in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
  298. {
  299. requested |= FD_WRITE | FD_CONNECT;
  300. FD_SET ((SOCKET) h, wfds);
  301. FD_SET ((SOCKET) h, &handle_wfds);
  302. }
  303. if (xbits.in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
  304. {
  305. requested |= FD_OOB;
  306. FD_SET ((SOCKET) h, xfds);
  307. FD_SET ((SOCKET) h, &handle_xfds);
  308. }
  309. WSAEventSelect ((SOCKET) h, hEvent, requested);
  310. nsock++;
  311. }
  312. else
  313. {
  314. handle_array[nhandles++] = h;
  315. /* Poll now. If we get an event, do not wait below. */
  316. if (wait_timeout != 0
  317. && win32_poll_handle (h, i, &rbits, &wbits, &xbits))
  318. wait_timeout = 0;
  319. }
  320. }
  321. if (wait_timeout == 0 || nsock == 0)
  322. rc = 0;
  323. else
  324. {
  325. /* See if we need to wait in the loop below. If any select is ready,
  326. do MsgWaitForMultipleObjects anyway to dispatch messages, but
  327. no need to call select again. */
  328. rc = select (0, &handle_rfds, &handle_wfds, &handle_xfds, &tv0);
  329. if (rc == 0)
  330. {
  331. /* Restore the fd_sets for the other select we do below. */
  332. memcpy (&handle_rfds, rfds, sizeof (fd_set));
  333. memcpy (&handle_wfds, wfds, sizeof (fd_set));
  334. memcpy (&handle_xfds, xfds, sizeof (fd_set));
  335. }
  336. else
  337. wait_timeout = 0;
  338. }
  339. for (;;)
  340. {
  341. ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE,
  342. wait_timeout, QS_ALLINPUT);
  343. if (ret == WAIT_OBJECT_0 + nhandles)
  344. {
  345. /* new input of some other kind */
  346. BOOL bRet;
  347. while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0)
  348. {
  349. TranslateMessage (&msg);
  350. DispatchMessage (&msg);
  351. }
  352. }
  353. else
  354. break;
  355. }
  356. /* If we haven't done it yet, check the status of the sockets. */
  357. if (rc == 0 && nsock > 0)
  358. rc = select (0, &handle_rfds, &handle_wfds, &handle_xfds, &tv0);
  359. /* Now fill in the results. */
  360. FD_ZERO (rfds);
  361. FD_ZERO (wfds);
  362. FD_ZERO (xfds);
  363. /* Place a sentinel at the end of the array. */
  364. handle_array[nhandles] = NULL;
  365. nhandles = 1;
  366. for (i = 0; i < nfds; i++)
  367. {
  368. if ((anyfds_in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) == 0)
  369. continue;
  370. h = (HANDLE) _get_osfhandle (i);
  371. if (h != handle_array[nhandles])
  372. {
  373. /* Perform handle->descriptor mapping. Don't update rc, as these
  374. results are counted in the return value of Winsock's select. */
  375. WSAEventSelect ((SOCKET) h, NULL, 0);
  376. if (FD_ISSET (h, &handle_rfds))
  377. FD_SET (i, rfds);
  378. if (FD_ISSET (h, &handle_wfds))
  379. FD_SET (i, wfds);
  380. if (FD_ISSET (h, &handle_xfds))
  381. FD_SET (i, xfds);
  382. }
  383. else
  384. {
  385. /* Not a socket. */
  386. nhandles++;
  387. win32_poll_handle (h, i, &rbits, &wbits, &xbits);
  388. if (rbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
  389. {
  390. rc++;
  391. FD_SET (i, rfds);
  392. }
  393. if (wbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
  394. {
  395. rc++;
  396. FD_SET (i, wfds);
  397. }
  398. if (xbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
  399. {
  400. rc++;
  401. FD_SET (i, xfds);
  402. }
  403. }
  404. }
  405. return rc;
  406. }
  407. #else /* ! Native Win32. */
  408. #include <sys/select.h>
  409. #undef select
  410. int
  411. rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds,
  412. struct timeval *timeout)
  413. {
  414. /* Interix 3.5 has a bug: it does not support nfds == 0. */
  415. if (nfds == 0)
  416. {
  417. nfds = 1;
  418. rfds = NULL;
  419. wfds = NULL;
  420. xfds = NULL;
  421. }
  422. return select (nfds, rfds, wfds, xfds, timeout);
  423. }
  424. #endif