PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/bin/coreutils/lib/select.c

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