hurdselect.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /* Guts of both `select' and `poll' for Hurd.
  2. Copyright (C) 1991-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <sys/poll.h>
  18. #include <hurd.h>
  19. #include <hurd/fd.h>
  20. #include <hurd/io_request.h>
  21. #include <mach_rpc.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <stdint.h>
  26. #include <limits.h>
  27. #include <time.h>
  28. #include <sysdep-cancel.h>
  29. /* All user select types. */
  30. #define SELECT_ALL (SELECT_READ | SELECT_WRITE | SELECT_URG)
  31. /* Used to record that a particular select rpc returned. Must be distinct
  32. from SELECT_ALL (which better not have the high bit set). */
  33. #define SELECT_RETURNED ((SELECT_ALL << 1) & ~SELECT_ALL)
  34. #define SELECT_ERROR (SELECT_RETURNED << 1)
  35. /* Check the first NFDS descriptors either in POLLFDS (if nonnnull) or in
  36. each of READFDS, WRITEFDS, EXCEPTFDS that is nonnull. If TIMEOUT is not
  37. NULL, time out after waiting the interval specified therein. Returns
  38. the number of ready descriptors, or -1 for errors. */
  39. int
  40. _hurd_select (int nfds,
  41. struct pollfd *pollfds,
  42. fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  43. const struct timespec *timeout, const sigset_t *sigmask)
  44. {
  45. int i;
  46. mach_port_t portset, sigport;
  47. int got, ready;
  48. error_t err;
  49. fd_set rfds, wfds, xfds;
  50. int firstfd, lastfd;
  51. mach_msg_id_t reply_msgid;
  52. mach_msg_timeout_t to;
  53. struct timespec ts;
  54. struct
  55. {
  56. struct hurd_userlink ulink;
  57. struct hurd_fd *cell;
  58. mach_port_t io_port;
  59. int type;
  60. mach_port_t reply_port;
  61. int error;
  62. } d[nfds];
  63. sigset_t oset;
  64. struct hurd_sigstate *ss = NULL;
  65. if (nfds < 0 || (pollfds == NULL && nfds > FD_SETSIZE))
  66. return __hurd_fail (EINVAL);
  67. #define IO_SELECT_REPLY_MSGID (21012 + 100) /* XXX */
  68. #define IO_SELECT_TIMEOUT_REPLY_MSGID (21031 + 100) /* XXX */
  69. if (timeout == NULL)
  70. reply_msgid = IO_SELECT_REPLY_MSGID;
  71. else
  72. {
  73. struct timespec now;
  74. if (timeout->tv_sec < 0 || ! valid_nanoseconds (timeout->tv_nsec))
  75. return __hurd_fail (EINVAL);
  76. err = __clock_gettime (CLOCK_REALTIME, &now);
  77. if (err)
  78. return -1;
  79. ts.tv_sec = now.tv_sec + timeout->tv_sec;
  80. ts.tv_nsec = now.tv_nsec + timeout->tv_nsec;
  81. if (ts.tv_nsec >= 1000000000)
  82. {
  83. ts.tv_sec++;
  84. ts.tv_nsec -= 1000000000;
  85. }
  86. if (ts.tv_sec < 0)
  87. ts.tv_sec = LONG_MAX; /* XXX */
  88. reply_msgid = IO_SELECT_TIMEOUT_REPLY_MSGID;
  89. }
  90. if (sigmask)
  91. {
  92. /* Add a port to the portset for the case when we get the signal even
  93. before calling __mach_msg. */
  94. sigport = __mach_reply_port ();
  95. ss = _hurd_self_sigstate ();
  96. _hurd_sigstate_lock (ss);
  97. /* And tell the signal thread to message us when a signal arrives. */
  98. ss->suspended = sigport;
  99. _hurd_sigstate_unlock (ss);
  100. if (__sigprocmask (SIG_SETMASK, sigmask, &oset))
  101. {
  102. _hurd_sigstate_lock (ss);
  103. ss->suspended = MACH_PORT_NULL;
  104. _hurd_sigstate_unlock (ss);
  105. __mach_port_destroy (__mach_task_self (), sigport);
  106. return -1;
  107. }
  108. }
  109. else
  110. sigport = MACH_PORT_NULL;
  111. if (pollfds)
  112. {
  113. int error = 0;
  114. /* Collect interesting descriptors from the user's `pollfd' array.
  115. We do a first pass that reads the user's array before taking
  116. any locks. The second pass then only touches our own stack,
  117. and gets the port references. */
  118. for (i = 0; i < nfds; ++i)
  119. if (pollfds[i].fd >= 0)
  120. {
  121. int type = 0;
  122. if (pollfds[i].events & POLLIN)
  123. type |= SELECT_READ;
  124. if (pollfds[i].events & POLLOUT)
  125. type |= SELECT_WRITE;
  126. if (pollfds[i].events & POLLPRI)
  127. type |= SELECT_URG;
  128. d[i].io_port = pollfds[i].fd;
  129. d[i].type = type;
  130. }
  131. else
  132. d[i].type = 0;
  133. HURD_CRITICAL_BEGIN;
  134. __mutex_lock (&_hurd_dtable_lock);
  135. for (i = 0; i < nfds; ++i)
  136. if (d[i].type != 0)
  137. {
  138. const int fd = (int) d[i].io_port;
  139. if (fd < _hurd_dtablesize)
  140. {
  141. d[i].cell = _hurd_dtable[fd];
  142. if (d[i].cell != NULL)
  143. {
  144. d[i].io_port = _hurd_port_get (&d[i].cell->port,
  145. &d[i].ulink);
  146. if (d[i].io_port != MACH_PORT_NULL)
  147. continue;
  148. }
  149. }
  150. /* Bogus descriptor, make it EBADF already. */
  151. d[i].error = EBADF;
  152. d[i].type = SELECT_ERROR;
  153. error = 1;
  154. }
  155. __mutex_unlock (&_hurd_dtable_lock);
  156. HURD_CRITICAL_END;
  157. if (error)
  158. {
  159. /* Set timeout to 0. */
  160. err = __clock_gettime (CLOCK_REALTIME, &ts);
  161. if (err)
  162. {
  163. /* Really bad luck. */
  164. err = errno;
  165. HURD_CRITICAL_BEGIN;
  166. __mutex_lock (&_hurd_dtable_lock);
  167. while (i-- > 0)
  168. if (d[i].type & ~SELECT_ERROR != 0)
  169. _hurd_port_free (&d[i].cell->port, &d[i].ulink,
  170. d[i].io_port);
  171. __mutex_unlock (&_hurd_dtable_lock);
  172. HURD_CRITICAL_END;
  173. if (sigmask)
  174. __sigprocmask (SIG_SETMASK, &oset, NULL);
  175. errno = err;
  176. return -1;
  177. }
  178. reply_msgid = IO_SELECT_TIMEOUT_REPLY_MSGID;
  179. }
  180. lastfd = i - 1;
  181. firstfd = i == 0 ? lastfd : 0;
  182. }
  183. else
  184. {
  185. /* Collect interested descriptors from the user's fd_set arguments.
  186. Use local copies so we can't crash from user bogosity. */
  187. if (readfds == NULL)
  188. FD_ZERO (&rfds);
  189. else
  190. rfds = *readfds;
  191. if (writefds == NULL)
  192. FD_ZERO (&wfds);
  193. else
  194. wfds = *writefds;
  195. if (exceptfds == NULL)
  196. FD_ZERO (&xfds);
  197. else
  198. xfds = *exceptfds;
  199. HURD_CRITICAL_BEGIN;
  200. __mutex_lock (&_hurd_dtable_lock);
  201. /* Collect the ports for interesting FDs. */
  202. firstfd = lastfd = -1;
  203. for (i = 0; i < nfds; ++i)
  204. {
  205. int type = 0;
  206. if (readfds != NULL && FD_ISSET (i, &rfds))
  207. type |= SELECT_READ;
  208. if (writefds != NULL && FD_ISSET (i, &wfds))
  209. type |= SELECT_WRITE;
  210. if (exceptfds != NULL && FD_ISSET (i, &xfds))
  211. type |= SELECT_URG;
  212. d[i].type = type;
  213. if (type)
  214. {
  215. if (i < _hurd_dtablesize)
  216. {
  217. d[i].cell = _hurd_dtable[i];
  218. if (d[i].cell != NULL)
  219. d[i].io_port = _hurd_port_get (&d[i].cell->port,
  220. &d[i].ulink);
  221. }
  222. if (i >= _hurd_dtablesize || d[i].cell == NULL ||
  223. d[i].io_port == MACH_PORT_NULL)
  224. {
  225. /* If one descriptor is bogus, we fail completely. */
  226. while (i-- > 0)
  227. if (d[i].type != 0)
  228. _hurd_port_free (&d[i].cell->port, &d[i].ulink,
  229. d[i].io_port);
  230. break;
  231. }
  232. lastfd = i;
  233. if (firstfd == -1)
  234. firstfd = i;
  235. }
  236. }
  237. __mutex_unlock (&_hurd_dtable_lock);
  238. HURD_CRITICAL_END;
  239. if (i < nfds)
  240. {
  241. if (sigmask)
  242. __sigprocmask (SIG_SETMASK, &oset, NULL);
  243. return __hurd_fail (EBADF);
  244. }
  245. if (nfds > _hurd_dtablesize)
  246. nfds = _hurd_dtablesize;
  247. }
  248. err = 0;
  249. got = 0;
  250. /* Send them all io_select request messages. */
  251. if (firstfd == -1)
  252. {
  253. if (sigport == MACH_PORT_NULL)
  254. /* But not if there were no ports to deal with at all.
  255. We are just a pure timeout. */
  256. portset = __mach_reply_port ();
  257. else
  258. portset = sigport;
  259. }
  260. else
  261. {
  262. portset = MACH_PORT_NULL;
  263. for (i = firstfd; i <= lastfd; ++i)
  264. if (!(d[i].type & ~SELECT_ERROR))
  265. d[i].reply_port = MACH_PORT_NULL;
  266. else
  267. {
  268. int type = d[i].type;
  269. d[i].reply_port = __mach_reply_port ();
  270. if (timeout == NULL)
  271. err = __io_select_request (d[i].io_port, d[i].reply_port, type);
  272. else
  273. err = __io_select_timeout_request (d[i].io_port, d[i].reply_port,
  274. ts, type);
  275. if (!err)
  276. {
  277. if (firstfd == lastfd && sigport == MACH_PORT_NULL)
  278. /* When there's a single descriptor, we don't need a
  279. portset, so just pretend we have one, but really
  280. use the single reply port. */
  281. portset = d[i].reply_port;
  282. else if (got == 0)
  283. /* We've got multiple reply ports, so we need a port set to
  284. multiplex them. */
  285. {
  286. /* We will wait again for a reply later. */
  287. if (portset == MACH_PORT_NULL)
  288. /* Create the portset to receive all the replies on. */
  289. err = __mach_port_allocate (__mach_task_self (),
  290. MACH_PORT_RIGHT_PORT_SET,
  291. &portset);
  292. if (! err)
  293. /* Put this reply port in the port set. */
  294. __mach_port_move_member (__mach_task_self (),
  295. d[i].reply_port, portset);
  296. }
  297. }
  298. else
  299. {
  300. /* No error should happen, but record it for later
  301. processing. */
  302. d[i].error = err;
  303. d[i].type |= SELECT_ERROR;
  304. ++got;
  305. }
  306. _hurd_port_free (&d[i].cell->port, &d[i].ulink, d[i].io_port);
  307. }
  308. if (got == 0 && sigport != MACH_PORT_NULL)
  309. {
  310. if (portset == MACH_PORT_NULL)
  311. /* Create the portset to receive the signal message on. */
  312. __mach_port_allocate (__mach_task_self (), MACH_PORT_RIGHT_PORT_SET,
  313. &portset);
  314. /* Put the signal reply port in the port set. */
  315. __mach_port_move_member (__mach_task_self (), sigport, portset);
  316. }
  317. }
  318. /* GOT is the number of replies (or errors), while READY is the number of
  319. replies with at least one type bit set. */
  320. ready = 0;
  321. /* Now wait for reply messages. */
  322. if (!err && got == 0)
  323. {
  324. /* Now wait for io_select_reply messages on PORT,
  325. timing out as appropriate. */
  326. union
  327. {
  328. mach_msg_header_t head;
  329. #ifdef MACH_MSG_TRAILER_MINIMUM_SIZE
  330. struct
  331. {
  332. mach_msg_header_t head;
  333. NDR_record_t ndr;
  334. error_t err;
  335. } error;
  336. struct
  337. {
  338. mach_msg_header_t head;
  339. NDR_record_t ndr;
  340. error_t err;
  341. int result;
  342. mach_msg_trailer_t trailer;
  343. } success;
  344. #else
  345. struct
  346. {
  347. mach_msg_header_t head;
  348. mach_msg_type_t err_type;
  349. error_t err;
  350. } error;
  351. struct
  352. {
  353. mach_msg_header_t head;
  354. mach_msg_type_t err_type;
  355. error_t err;
  356. mach_msg_type_t result_type;
  357. int result;
  358. } success;
  359. #endif
  360. } msg;
  361. mach_msg_option_t options;
  362. error_t msgerr;
  363. /* We rely on servers to implement the timeout, but when there are none,
  364. do it on the client side. */
  365. if (timeout != NULL && firstfd == -1)
  366. {
  367. options = MACH_RCV_TIMEOUT;
  368. to = timeout->tv_sec * 1000 + (timeout->tv_nsec + 999999) / 1000000;
  369. }
  370. else
  371. {
  372. options = 0;
  373. to = MACH_MSG_TIMEOUT_NONE;
  374. }
  375. int cancel_oldtype = LIBC_CANCEL_ASYNC();
  376. while ((msgerr = __mach_msg (&msg.head,
  377. MACH_RCV_MSG | MACH_RCV_INTERRUPT | options,
  378. 0, sizeof msg, portset, to,
  379. MACH_PORT_NULL)) == MACH_MSG_SUCCESS)
  380. {
  381. LIBC_CANCEL_RESET (cancel_oldtype);
  382. /* We got a message. Decode it. */
  383. #ifdef MACH_MSG_TYPE_BIT
  384. static const mach_msg_type_t inttype = {
  385. .msgt_name = MACH_MSG_TYPE_INTEGER_T,
  386. .msgt_size = sizeof (integer_t) * 8,
  387. .msgt_number = 1,
  388. .msgt_inline = TRUE,
  389. .msgt_longform = FALSE,
  390. .msgt_deallocate = FALSE,
  391. .msgt_unused = 0
  392. };
  393. #endif
  394. if (sigport != MACH_PORT_NULL && sigport == msg.head.msgh_local_port)
  395. {
  396. /* We actually got interrupted by a signal before
  397. __mach_msg; poll for further responses and then
  398. return quickly. */
  399. err = EINTR;
  400. goto poll;
  401. }
  402. if (msg.head.msgh_id == reply_msgid
  403. && msg.head.msgh_size >= sizeof msg.error
  404. && !(msg.head.msgh_bits & MACH_MSGH_BITS_COMPLEX)
  405. #ifdef MACH_MSG_TYPE_BIT
  406. && !BAD_TYPECHECK (&msg.error.err_type, &inttype)
  407. #endif
  408. )
  409. {
  410. /* This is a properly formatted message so far.
  411. See if it is a success or a failure. */
  412. if (msg.error.err == EINTR
  413. && msg.head.msgh_size == sizeof msg.error)
  414. {
  415. /* EINTR response; poll for further responses
  416. and then return quickly. */
  417. err = EINTR;
  418. goto poll;
  419. }
  420. /* Keep in mind msg.success.result can be 0 if a timeout
  421. occurred. */
  422. if (msg.error.err
  423. #ifdef MACH_MSG_TYPE_BIT
  424. || BAD_TYPECHECK (&msg.success.result_type, &inttype)
  425. #endif
  426. || msg.head.msgh_size != sizeof msg.success)
  427. {
  428. /* Error or bogus reply. */
  429. if (!msg.error.err)
  430. msg.error.err = EIO;
  431. __mach_msg_destroy (&msg.head);
  432. }
  433. /* Look up the respondent's reply port and record its
  434. readiness. */
  435. {
  436. int had = got;
  437. if (firstfd != -1)
  438. for (i = firstfd; i <= lastfd; ++i)
  439. if (d[i].type
  440. && d[i].reply_port == msg.head.msgh_local_port)
  441. {
  442. if (msg.error.err)
  443. {
  444. d[i].error = msg.error.err;
  445. d[i].type = SELECT_ERROR;
  446. ++ready;
  447. }
  448. else
  449. {
  450. d[i].type &= msg.success.result;
  451. if (d[i].type)
  452. ++ready;
  453. }
  454. d[i].type |= SELECT_RETURNED;
  455. ++got;
  456. }
  457. assert (got > had);
  458. }
  459. }
  460. if (msg.head.msgh_remote_port != MACH_PORT_NULL)
  461. __mach_port_deallocate (__mach_task_self (),
  462. msg.head.msgh_remote_port);
  463. if (got)
  464. poll:
  465. {
  466. /* Poll for another message. */
  467. to = 0;
  468. options |= MACH_RCV_TIMEOUT;
  469. }
  470. }
  471. LIBC_CANCEL_RESET (cancel_oldtype);
  472. if (msgerr == MACH_RCV_INTERRUPTED)
  473. /* Interruption on our side (e.g. signal reception). */
  474. err = EINTR;
  475. if (ready)
  476. /* At least one descriptor is known to be ready now, so we will
  477. return success. */
  478. err = 0;
  479. }
  480. if (firstfd != -1)
  481. for (i = firstfd; i <= lastfd; ++i)
  482. if (d[i].reply_port != MACH_PORT_NULL)
  483. __mach_port_destroy (__mach_task_self (), d[i].reply_port);
  484. if (sigport != MACH_PORT_NULL)
  485. {
  486. _hurd_sigstate_lock (ss);
  487. ss->suspended = MACH_PORT_NULL;
  488. _hurd_sigstate_unlock (ss);
  489. __mach_port_destroy (__mach_task_self (), sigport);
  490. }
  491. if ((firstfd == -1 && sigport == MACH_PORT_NULL)
  492. || ((firstfd != lastfd || sigport != MACH_PORT_NULL) && portset != MACH_PORT_NULL))
  493. /* Destroy PORTSET, but only if it's not actually the reply port for a
  494. single descriptor (in which case it's destroyed in the previous loop;
  495. not doing it here is just a bit more efficient). */
  496. __mach_port_destroy (__mach_task_self (), portset);
  497. if (err)
  498. {
  499. if (sigmask)
  500. __sigprocmask (SIG_SETMASK, &oset, NULL);
  501. return __hurd_fail (err);
  502. }
  503. if (pollfds)
  504. /* Fill in the `revents' members of the user's array. */
  505. for (i = 0; i < nfds; ++i)
  506. {
  507. int type = d[i].type;
  508. int revents = 0;
  509. if (type & SELECT_ERROR)
  510. switch (d[i].error)
  511. {
  512. case EPIPE:
  513. revents = POLLHUP;
  514. break;
  515. case EBADF:
  516. revents = POLLNVAL;
  517. break;
  518. default:
  519. revents = POLLERR;
  520. break;
  521. }
  522. else
  523. if (type & SELECT_RETURNED)
  524. {
  525. if (type & SELECT_READ)
  526. revents |= POLLIN;
  527. if (type & SELECT_WRITE)
  528. revents |= POLLOUT;
  529. if (type & SELECT_URG)
  530. revents |= POLLPRI;
  531. }
  532. pollfds[i].revents = revents;
  533. }
  534. else
  535. {
  536. /* Below we recalculate READY to include an increment for each operation
  537. allowed on each fd. */
  538. ready = 0;
  539. /* Set the user bitarrays. We only ever have to clear bits, as all
  540. desired ones are initially set. */
  541. if (firstfd != -1)
  542. for (i = firstfd; i <= lastfd; ++i)
  543. {
  544. int type = d[i].type;
  545. if ((type & SELECT_RETURNED) == 0)
  546. type = 0;
  547. /* Callers of select don't expect to see errors, so we simulate
  548. readiness of the erring object and the next call hopefully
  549. will get the error again. */
  550. if (type & SELECT_ERROR)
  551. {
  552. type = 0;
  553. if (readfds != NULL && FD_ISSET (i, readfds))
  554. type |= SELECT_READ;
  555. if (writefds != NULL && FD_ISSET (i, writefds))
  556. type |= SELECT_WRITE;
  557. if (exceptfds != NULL && FD_ISSET (i, exceptfds))
  558. type |= SELECT_URG;
  559. }
  560. if (type & SELECT_READ)
  561. ready++;
  562. else if (readfds)
  563. FD_CLR (i, readfds);
  564. if (type & SELECT_WRITE)
  565. ready++;
  566. else if (writefds)
  567. FD_CLR (i, writefds);
  568. if (type & SELECT_URG)
  569. ready++;
  570. else if (exceptfds)
  571. FD_CLR (i, exceptfds);
  572. }
  573. }
  574. if (sigmask && __sigprocmask (SIG_SETMASK, &oset, NULL))
  575. return -1;
  576. return ready;
  577. }