PageRenderTime 177ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ffmpeg/Win32/src/msvc_miss/poll/poll.c

https://github.com/ejgarcia/isabel
C | 598 lines | 445 code | 82 blank | 71 comment | 135 complexity | 45cf5c4fa1be0d336edaf3dd148a8fa0 MD5 | raw file
  1. /* Emulation for poll(2)
  2. Contributed by Paolo Bonzini.
  3. Copyright 2001-2003, 2006-2011 Free Software Foundation, Inc.
  4. This file is part of gnulib.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. /* Tell gcc not to warn about the (nfd < 0) tests, below. */
  17. #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
  18. # pragma GCC diagnostic ignored "-Wtype-limits"
  19. #endif
  20. #include <config.h>
  21. #include <alloca.h>
  22. #include <sys/types.h>
  23. /* Specification. */
  24. #include <poll.h>
  25. #include <errno.h>
  26. #include <limits.h>
  27. #include <assert.h>
  28. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  29. # define WIN32_NATIVE
  30. # include <winsock2.h>
  31. # include <windows.h>
  32. # include <io.h>
  33. # include <stdio.h>
  34. # include <conio.h>
  35. #else
  36. # include <sys/time.h>
  37. # include <sys/socket.h>
  38. # include <sys/select.h>
  39. # include <unistd.h>
  40. #endif
  41. #ifdef HAVE_SYS_IOCTL_H
  42. # include <sys/ioctl.h>
  43. #endif
  44. #ifdef HAVE_SYS_FILIO_H
  45. # include <sys/filio.h>
  46. #endif
  47. #include <time.h>
  48. #ifndef INFTIM
  49. # define INFTIM (-1)
  50. #endif
  51. /* BeOS does not have MSG_PEEK. */
  52. #ifndef MSG_PEEK
  53. # define MSG_PEEK 0
  54. #endif
  55. #ifdef WIN32_NATIVE
  56. #define IsConsoleHandle(h) (((long) (h) & 3) == 3)
  57. static BOOL
  58. IsSocketHandle (HANDLE h)
  59. {
  60. WSANETWORKEVENTS ev;
  61. if (IsConsoleHandle (h))
  62. return FALSE;
  63. /* Under Wine, it seems that getsockopt returns 0 for pipes too.
  64. WSAEnumNetworkEvents instead distinguishes the two correctly. */
  65. ev.lNetworkEvents = 0xDEADBEEF;
  66. WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
  67. return ev.lNetworkEvents != 0xDEADBEEF;
  68. }
  69. /* Declare data structures for ntdll functions. */
  70. typedef struct _FILE_PIPE_LOCAL_INFORMATION {
  71. ULONG NamedPipeType;
  72. ULONG NamedPipeConfiguration;
  73. ULONG MaximumInstances;
  74. ULONG CurrentInstances;
  75. ULONG InboundQuota;
  76. ULONG ReadDataAvailable;
  77. ULONG OutboundQuota;
  78. ULONG WriteQuotaAvailable;
  79. ULONG NamedPipeState;
  80. ULONG NamedPipeEnd;
  81. } FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION;
  82. typedef struct _IO_STATUS_BLOCK
  83. {
  84. union {
  85. DWORD Status;
  86. PVOID Pointer;
  87. } u;
  88. ULONG_PTR Information;
  89. } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
  90. typedef enum _FILE_INFORMATION_CLASS {
  91. FilePipeLocalInformation = 24
  92. } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;
  93. typedef DWORD (WINAPI *PNtQueryInformationFile)
  94. (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS);
  95. # ifndef PIPE_BUF
  96. # define PIPE_BUF 512
  97. # endif
  98. /* Compute revents values for file handle H. If some events cannot happen
  99. for the handle, eliminate them from *P_SOUGHT. */
  100. static int
  101. win32_compute_revents (HANDLE h, int *p_sought)
  102. {
  103. int i, ret, happened;
  104. INPUT_RECORD *irbuffer;
  105. DWORD avail, nbuffer;
  106. BOOL bRet;
  107. IO_STATUS_BLOCK iosb;
  108. FILE_PIPE_LOCAL_INFORMATION fpli;
  109. static PNtQueryInformationFile NtQueryInformationFile;
  110. static BOOL once_only;
  111. switch (GetFileType (h))
  112. {
  113. case FILE_TYPE_PIPE:
  114. if (!once_only)
  115. {
  116. NtQueryInformationFile = (PNtQueryInformationFile)
  117. GetProcAddress (GetModuleHandle ("ntdll.dll"),
  118. "NtQueryInformationFile");
  119. once_only = TRUE;
  120. }
  121. happened = 0;
  122. if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0)
  123. {
  124. if (avail)
  125. happened |= *p_sought & (POLLIN | POLLRDNORM);
  126. }
  127. else if (GetLastError () == ERROR_BROKEN_PIPE)
  128. happened |= POLLHUP;
  129. else
  130. {
  131. /* It was the write-end of the pipe. Check if it is writable.
  132. If NtQueryInformationFile fails, optimistically assume the pipe is
  133. writable. This could happen on Win9x, where NtQueryInformationFile
  134. is not available, or if we inherit a pipe that doesn't permit
  135. FILE_READ_ATTRIBUTES access on the write end (I think this should
  136. not happen since WinXP SP2; WINE seems fine too). Otherwise,
  137. ensure that enough space is available for atomic writes. */
  138. memset (&iosb, 0, sizeof (iosb));
  139. memset (&fpli, 0, sizeof (fpli));
  140. if (!NtQueryInformationFile
  141. || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli),
  142. FilePipeLocalInformation)
  143. || fpli.WriteQuotaAvailable >= PIPE_BUF
  144. || (fpli.OutboundQuota < PIPE_BUF &&
  145. fpli.WriteQuotaAvailable == fpli.OutboundQuota))
  146. happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
  147. }
  148. return happened;
  149. case FILE_TYPE_CHAR:
  150. ret = WaitForSingleObject (h, 0);
  151. if (!IsConsoleHandle (h))
  152. return ret == WAIT_OBJECT_0 ? *p_sought & ~(POLLPRI | POLLRDBAND) : 0;
  153. nbuffer = avail = 0;
  154. bRet = GetNumberOfConsoleInputEvents (h, &nbuffer);
  155. if (bRet)
  156. {
  157. /* Input buffer. */
  158. *p_sought &= POLLIN | POLLRDNORM;
  159. if (nbuffer == 0)
  160. return POLLHUP;
  161. if (!*p_sought)
  162. return 0;
  163. irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD));
  164. bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail);
  165. if (!bRet || avail == 0)
  166. return POLLHUP;
  167. for (i = 0; i < avail; i++)
  168. if (irbuffer[i].EventType == KEY_EVENT)
  169. return *p_sought;
  170. return 0;
  171. }
  172. else
  173. {
  174. /* Screen buffer. */
  175. *p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND;
  176. return *p_sought;
  177. }
  178. default:
  179. ret = WaitForSingleObject (h, 0);
  180. if (ret == WAIT_OBJECT_0)
  181. return *p_sought & ~(POLLPRI | POLLRDBAND);
  182. return *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
  183. }
  184. }
  185. /* Convert fd_sets returned by select into revents values. */
  186. static int
  187. win32_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents)
  188. {
  189. int happened = 0;
  190. if ((lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) == FD_ACCEPT)
  191. happened |= (POLLIN | POLLRDNORM) & sought;
  192. else if (lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
  193. {
  194. int r, error;
  195. char data[64];
  196. WSASetLastError (0);
  197. r = recv (h, data, sizeof (data), MSG_PEEK);
  198. error = WSAGetLastError ();
  199. WSASetLastError (0);
  200. if (r > 0 || error == WSAENOTCONN)
  201. happened |= (POLLIN | POLLRDNORM) & sought;
  202. /* Distinguish hung-up sockets from other errors. */
  203. else if (r == 0 || error == WSAESHUTDOWN || error == WSAECONNRESET
  204. || error == WSAECONNABORTED || error == WSAENETRESET)
  205. happened |= POLLHUP;
  206. else
  207. happened |= POLLERR;
  208. }
  209. if (lNetworkEvents & (FD_WRITE | FD_CONNECT))
  210. happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
  211. if (lNetworkEvents & FD_OOB)
  212. happened |= (POLLPRI | POLLRDBAND) & sought;
  213. return happened;
  214. }
  215. #else /* !MinGW */
  216. /* Convert select(2) returned fd_sets into poll(2) revents values. */
  217. static int
  218. compute_revents (int fd, int sought, fd_set *rfds, fd_set *wfds, fd_set *efds)
  219. {
  220. int happened = 0;
  221. if (FD_ISSET (fd, rfds))
  222. {
  223. int r;
  224. int socket_errno;
  225. # if defined __MACH__ && defined __APPLE__
  226. /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK
  227. for some kinds of descriptors. Detect if this descriptor is a
  228. connected socket, a server socket, or something else using a
  229. 0-byte recv, and use ioctl(2) to detect POLLHUP. */
  230. r = recv (fd, NULL, 0, MSG_PEEK);
  231. socket_errno = (r < 0) ? errno : 0;
  232. if (r == 0 || socket_errno == ENOTSOCK)
  233. ioctl (fd, FIONREAD, &r);
  234. # else
  235. char data[64];
  236. r = recv (fd, data, sizeof (data), MSG_PEEK);
  237. socket_errno = (r < 0) ? errno : 0;
  238. # endif
  239. if (r == 0)
  240. happened |= POLLHUP;
  241. /* If the event happened on an unconnected server socket,
  242. that's fine. */
  243. else if (r > 0 || ( /* (r == -1) && */ socket_errno == ENOTCONN))
  244. happened |= (POLLIN | POLLRDNORM) & sought;
  245. /* Distinguish hung-up sockets from other errors. */
  246. else if (socket_errno == ESHUTDOWN || socket_errno == ECONNRESET
  247. || socket_errno == ECONNABORTED || socket_errno == ENETRESET)
  248. happened |= POLLHUP;
  249. else
  250. happened |= POLLERR;
  251. }
  252. if (FD_ISSET (fd, wfds))
  253. happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
  254. if (FD_ISSET (fd, efds))
  255. happened |= (POLLPRI | POLLRDBAND) & sought;
  256. return happened;
  257. }
  258. #endif /* !MinGW */
  259. int
  260. poll (struct pollfd *pfd, nfds_t nfd, int timeout)
  261. {
  262. #ifndef WIN32_NATIVE
  263. fd_set rfds, wfds, efds;
  264. struct timeval tv;
  265. struct timeval *ptv;
  266. int maxfd, rc;
  267. nfds_t i;
  268. # ifdef _SC_OPEN_MAX
  269. static int sc_open_max = -1;
  270. if (nfd < 0
  271. || (nfd > sc_open_max
  272. && (sc_open_max != -1
  273. || nfd > (sc_open_max = sysconf (_SC_OPEN_MAX)))))
  274. {
  275. errno = EINVAL;
  276. return -1;
  277. }
  278. # else /* !_SC_OPEN_MAX */
  279. # ifdef OPEN_MAX
  280. if (nfd < 0 || nfd > OPEN_MAX)
  281. {
  282. errno = EINVAL;
  283. return -1;
  284. }
  285. # endif /* OPEN_MAX -- else, no check is needed */
  286. # endif /* !_SC_OPEN_MAX */
  287. /* EFAULT is not necessary to implement, but let's do it in the
  288. simplest case. */
  289. if (!pfd)
  290. {
  291. errno = EFAULT;
  292. return -1;
  293. }
  294. /* convert timeout number into a timeval structure */
  295. if (timeout == 0)
  296. {
  297. ptv = &tv;
  298. ptv->tv_sec = 0;
  299. ptv->tv_usec = 0;
  300. }
  301. else if (timeout > 0)
  302. {
  303. ptv = &tv;
  304. ptv->tv_sec = timeout / 1000;
  305. ptv->tv_usec = (timeout % 1000) * 1000;
  306. }
  307. else if (timeout == INFTIM)
  308. /* wait forever */
  309. ptv = NULL;
  310. else
  311. {
  312. errno = EINVAL;
  313. return -1;
  314. }
  315. /* create fd sets and determine max fd */
  316. maxfd = -1;
  317. FD_ZERO (&rfds);
  318. FD_ZERO (&wfds);
  319. FD_ZERO (&efds);
  320. for (i = 0; i < nfd; i++)
  321. {
  322. if (pfd[i].fd < 0)
  323. continue;
  324. if (pfd[i].events & (POLLIN | POLLRDNORM))
  325. FD_SET (pfd[i].fd, &rfds);
  326. /* see select(2): "the only exceptional condition detectable
  327. is out-of-band data received on a socket", hence we push
  328. POLLWRBAND events onto wfds instead of efds. */
  329. if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND))
  330. FD_SET (pfd[i].fd, &wfds);
  331. if (pfd[i].events & (POLLPRI | POLLRDBAND))
  332. FD_SET (pfd[i].fd, &efds);
  333. if (pfd[i].fd >= maxfd
  334. && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI
  335. | POLLRDNORM | POLLRDBAND
  336. | POLLWRNORM | POLLWRBAND)))
  337. {
  338. maxfd = pfd[i].fd;
  339. if (maxfd > FD_SETSIZE)
  340. {
  341. errno = EOVERFLOW;
  342. return -1;
  343. }
  344. }
  345. }
  346. /* examine fd sets */
  347. rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv);
  348. if (rc < 0)
  349. return rc;
  350. /* establish results */
  351. rc = 0;
  352. for (i = 0; i < nfd; i++)
  353. if (pfd[i].fd < 0)
  354. pfd[i].revents = 0;
  355. else
  356. {
  357. int happened = compute_revents (pfd[i].fd, pfd[i].events,
  358. &rfds, &wfds, &efds);
  359. if (happened)
  360. {
  361. pfd[i].revents = happened;
  362. rc++;
  363. }
  364. }
  365. return rc;
  366. #else
  367. static struct timeval tv0;
  368. static HANDLE hEvent;
  369. WSANETWORKEVENTS ev;
  370. HANDLE h, handle_array[FD_SETSIZE + 2];
  371. DWORD ret, wait_timeout, nhandles;
  372. fd_set rfds, wfds, xfds;
  373. BOOL poll_again;
  374. MSG msg;
  375. int rc = 0;
  376. nfds_t i;
  377. if (nfd < 0 || timeout < -1)
  378. {
  379. errno = EINVAL;
  380. return -1;
  381. }
  382. if (!hEvent)
  383. hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
  384. handle_array[0] = hEvent;
  385. nhandles = 1;
  386. FD_ZERO (&rfds);
  387. FD_ZERO (&wfds);
  388. FD_ZERO (&xfds);
  389. /* Classify socket handles and create fd sets. */
  390. for (i = 0; i < nfd; i++)
  391. {
  392. int sought = pfd[i].events;
  393. pfd[i].revents = 0;
  394. if (pfd[i].fd < 0)
  395. continue;
  396. if (!(sought & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND
  397. | POLLPRI | POLLRDBAND)))
  398. continue;
  399. h = (HANDLE) _get_osfhandle (pfd[i].fd);
  400. assert (h != NULL);
  401. if (IsSocketHandle (h))
  402. {
  403. int requested = FD_CLOSE;
  404. /* see above; socket handles are mapped onto select. */
  405. if (sought & (POLLIN | POLLRDNORM))
  406. {
  407. requested |= FD_READ | FD_ACCEPT;
  408. FD_SET ((SOCKET) h, &rfds);
  409. }
  410. if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND))
  411. {
  412. requested |= FD_WRITE | FD_CONNECT;
  413. FD_SET ((SOCKET) h, &wfds);
  414. }
  415. if (sought & (POLLPRI | POLLRDBAND))
  416. {
  417. requested |= FD_OOB;
  418. FD_SET ((SOCKET) h, &xfds);
  419. }
  420. if (requested)
  421. WSAEventSelect ((SOCKET) h, hEvent, requested);
  422. }
  423. else
  424. {
  425. /* Poll now. If we get an event, do not poll again. Also,
  426. screen buffer handles are waitable, and they'll block until
  427. a character is available. win32_compute_revents eliminates
  428. bits for the "wrong" direction. */
  429. pfd[i].revents = win32_compute_revents (h, &sought);
  430. if (sought)
  431. handle_array[nhandles++] = h;
  432. if (pfd[i].revents)
  433. timeout = 0;
  434. }
  435. }
  436. if (select (0, &rfds, &wfds, &xfds, &tv0) > 0)
  437. {
  438. /* Do MsgWaitForMultipleObjects anyway to dispatch messages, but
  439. no need to call select again. */
  440. poll_again = FALSE;
  441. wait_timeout = 0;
  442. }
  443. else
  444. {
  445. poll_again = TRUE;
  446. if (timeout == INFTIM)
  447. wait_timeout = INFINITE;
  448. else
  449. wait_timeout = timeout;
  450. }
  451. for (;;)
  452. {
  453. ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE,
  454. wait_timeout, QS_ALLINPUT);
  455. if (ret == WAIT_OBJECT_0 + nhandles)
  456. {
  457. /* new input of some other kind */
  458. BOOL bRet;
  459. while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0)
  460. {
  461. TranslateMessage (&msg);
  462. DispatchMessage (&msg);
  463. }
  464. }
  465. else
  466. break;
  467. }
  468. if (poll_again)
  469. select (0, &rfds, &wfds, &xfds, &tv0);
  470. /* Place a sentinel at the end of the array. */
  471. handle_array[nhandles] = NULL;
  472. nhandles = 1;
  473. for (i = 0; i < nfd; i++)
  474. {
  475. int happened;
  476. if (pfd[i].fd < 0)
  477. continue;
  478. if (!(pfd[i].events & (POLLIN | POLLRDNORM |
  479. POLLOUT | POLLWRNORM | POLLWRBAND)))
  480. continue;
  481. h = (HANDLE) _get_osfhandle (pfd[i].fd);
  482. if (h != handle_array[nhandles])
  483. {
  484. /* It's a socket. */
  485. WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
  486. WSAEventSelect ((SOCKET) h, 0, 0);
  487. /* If we're lucky, WSAEnumNetworkEvents already provided a way
  488. to distinguish FD_READ and FD_ACCEPT; this saves a recv later. */
  489. if (FD_ISSET ((SOCKET) h, &rfds)
  490. && !(ev.lNetworkEvents & (FD_READ | FD_ACCEPT)))
  491. ev.lNetworkEvents |= FD_READ | FD_ACCEPT;
  492. if (FD_ISSET ((SOCKET) h, &wfds))
  493. ev.lNetworkEvents |= FD_WRITE | FD_CONNECT;
  494. if (FD_ISSET ((SOCKET) h, &xfds))
  495. ev.lNetworkEvents |= FD_OOB;
  496. happened = win32_compute_revents_socket ((SOCKET) h, pfd[i].events,
  497. ev.lNetworkEvents);
  498. }
  499. else
  500. {
  501. /* Not a socket. */
  502. int sought = pfd[i].events;
  503. happened = win32_compute_revents (h, &sought);
  504. nhandles++;
  505. }
  506. if ((pfd[i].revents |= happened) != 0)
  507. rc++;
  508. }
  509. return rc;
  510. #endif
  511. }