--- hurdselect.c-38+patches 2013-01-22 16:09:25.000000000 +0100 +++ hurdselect_step1_2.c 2013-01-23 17:23:30.000000000 +0100 @@ -33,6 +33,179 @@ from SELECT_ALL (which better not have the high bit set). */ #define SELECT_RETURNED ((SELECT_ALL << 1) & ~SELECT_ALL) +struct dfd +{ + struct hurd_userlink ulink; + struct hurd_fd *cell; + mach_port_t io_port; + int type; + mach_port_t reply_port; +}; + +/* Helper function */ +int _wait_for_replies (int nfds, int firstfd, int lastfd, mach_msg_timeout_t to, + error_t err, mach_port_t portset, struct dfd *d, + const struct timespec *timeout, const sigset_t *sigmask, + sigset_t *oset) +{ + int i, got = 0; + + union typeword /* Use this to avoid unkosher casts. */ + { + mach_msg_type_t type; + uint32_t word; + }; + assert (sizeof (union typeword) == sizeof (mach_msg_type_t)); + assert (sizeof (uint32_t) == sizeof (mach_msg_type_t)); + + union + { + mach_msg_header_t head; +#ifdef MACH_MSG_TRAILER_MINIMUM_SIZE + struct + { + mach_msg_header_t head; + NDR_record_t ndr; + error_t err; + } error; + struct + { + mach_msg_header_t head; + NDR_record_t ndr; + error_t err; + int result; + mach_msg_trailer_t trailer; + } success; +#else + struct + { + mach_msg_header_t head; + union typeword err_type; + error_t err; + } error; + struct + { + mach_msg_header_t head; + union typeword err_type; + error_t err; + union typeword result_type; + int result; + } success; +#endif + } msg; + mach_msg_option_t options = (timeout == NULL ? 0 : MACH_RCV_TIMEOUT); + error_t msgerr; + +#define IO_SELECT_REPLY_MSGID (21012 + 100) /* XXX */ +#ifdef MACH_MSG_TYPE_BIT + const union typeword inttype = + { type: + { MACH_MSG_TYPE_INTEGER_T, sizeof (integer_t) * 8, 1, 1, 0, 0 } + }; +#endif + + /* Now wait for reply messages. */ + if (!err && got == 0) + { + /* Now wait for io_select_reply messages on PORT, + timing out as appropriate. */ + while ((msgerr = __mach_msg (&msg.head, + MACH_RCV_MSG | MACH_RCV_INTERRUPT | options, + 0, sizeof msg, portset, to, + MACH_PORT_NULL)) == MACH_MSG_SUCCESS) + { + /* We got a message. Decode it. */ + if (msg.head.msgh_id == IO_SELECT_REPLY_MSGID && + msg.head.msgh_size >= sizeof msg.error && + !(msg.head.msgh_bits & MACH_MSGH_BITS_COMPLEX) && +#ifdef MACH_MSG_TYPE_BIT + msg.error.err_type.word == inttype.word +#endif + ) + { + /* This is a properly formatted message so far. + See if it is a success or a failure. */ + if (msg.error.err == EINTR && + msg.head.msgh_size == sizeof msg.error) + { + /* EINTR response; poll for further responses + and then return quickly. */ + err = EINTR; + goto poll; + } + if (msg.error.err || + msg.head.msgh_size != sizeof msg.success || +#ifdef MACH_MSG_TYPE_BIT + msg.success.result_type.word != inttype.word || +#endif + (msg.success.result & SELECT_ALL) == 0) + { + /* Error or bogus reply. Simulate readiness. */ + __mach_msg_destroy (&msg.head); + msg.success.result = SELECT_ALL; + } + + /* Look up the respondent's reply port and record its + readiness. */ + { + int had = got; + if (firstfd != -1) + for (i = firstfd; i <= lastfd; ++i) + if (d[i].type + && d[i].reply_port == msg.head.msgh_local_port) + { + d[i].type &= msg.success.result; + d[i].type |= SELECT_RETURNED; + ++got; + } + assert (got > had); + } + } /* ) { */ + + if (msg.head.msgh_remote_port != MACH_PORT_NULL) + __mach_port_deallocate (__mach_task_self (), + msg.head.msgh_remote_port); + + if (got) + poll: + { + /* Poll for another message. */ + to = 0; + options |= MACH_RCV_TIMEOUT; + } + } /* while */ + + if (msgerr == MACH_RCV_INTERRUPTED) + /* Interruption on our side (e.g. signal reception). */ + err = EINTR; + + if (got) + /* At least one descriptor is known to be ready now, so we will + return success. */ + err = 0; + } /* if (!err && got == 0) */ + + if (firstfd != -1) + for (i = firstfd; i <= lastfd; ++i) + if (d[i].type) + __mach_port_destroy (__mach_task_self (), d[i].reply_port); + if (firstfd == -1 || (firstfd != lastfd && portset != MACH_PORT_NULL)) + /* Destroy PORTSET, but only if it's not actually the reply port for a + single descriptor (in which case it's destroyed in the previous loop; + not doing it here is just a bit more efficient). */ + __mach_port_destroy (__mach_task_self (), portset); + + if (err) + { + if (sigmask) + __sigprocmask (SIG_SETMASK, oset, NULL); + errno = err; + return -1; + } + else + return got; + } /* _wait_for_replies() */ + /* Check the first NFDS descriptors either in POLLFDS (if nonnnull) or in each of READFDS, WRITEFDS, EXCEPTFDS that is nonnull. If TIMEOUT is not NULL, time out after waiting the interval specified therein. Returns @@ -50,23 +223,21 @@ _hurd_select (int nfds, fd_set rfds, wfds, xfds; int firstfd, lastfd; mach_msg_timeout_t to = 0; - struct - { - struct hurd_userlink ulink; - struct hurd_fd *cell; - mach_port_t io_port; - int type; - mach_port_t reply_port; - } d[nfds]; + struct dfd d[nfds]; sigset_t oset; - union typeword /* Use this to avoid unkosher casts. */ - { - mach_msg_type_t type; - uint32_t word; - }; - assert (sizeof (union typeword) == sizeof (mach_msg_type_t)); - assert (sizeof (uint32_t) == sizeof (mach_msg_type_t)); + enum { + DELAY = -1, + SELECT = 0, + POLL = 1 + } ispoll; + + if (nfds == 0) + ispoll = DELAY; + else if (pollfds) + ispoll = POLL; + else + ispoll = SELECT; if (nfds < 0 || nfds > FD_SETSIZE) { @@ -152,7 +323,7 @@ _hurd_select (int nfds, lastfd = i - 1; firstfd = i == 0 ? lastfd : 0; - } + } /* POLL */ else { /* Collect interested descriptors from the user's fd_set arguments. @@ -218,7 +389,7 @@ _hurd_select (int nfds, errno = EBADF; return -1; } - } + } /* SELECT */ err = 0; @@ -226,11 +397,11 @@ _hurd_select (int nfds, /* Send them all io_select request messages. */ - if (firstfd == -1) + if (firstfd == -1) /* DELAY */ /* But not if there were no ports to deal with at all. We are just a pure timeout. */ portset = __mach_reply_port (); - else + else /* POLL || SELECT */ { portset = MACH_PORT_NULL; @@ -289,152 +460,15 @@ _hurd_select (int nfds, } _hurd_port_free (&d[i].cell->port, &d[i].ulink, d[i].io_port); } - } + } /* POLL || SELECT */ - /* Now wait for reply messages. */ - if (!err && got == 0) - { - /* Now wait for io_select_reply messages on PORT, - timing out as appropriate. */ - - union - { - mach_msg_header_t head; -#ifdef MACH_MSG_TRAILER_MINIMUM_SIZE - struct - { - mach_msg_header_t head; - NDR_record_t ndr; - error_t err; - } error; - struct - { - mach_msg_header_t head; - NDR_record_t ndr; - error_t err; - int result; - mach_msg_trailer_t trailer; - } success; -#else - struct - { - mach_msg_header_t head; - union typeword err_type; - error_t err; - } error; - struct - { - mach_msg_header_t head; - union typeword err_type; - error_t err; - union typeword result_type; - int result; - } success; -#endif - } msg; - mach_msg_option_t options = (timeout == NULL ? 0 : MACH_RCV_TIMEOUT); - error_t msgerr; - while ((msgerr = __mach_msg (&msg.head, - MACH_RCV_MSG | MACH_RCV_INTERRUPT | options, - 0, sizeof msg, portset, to, - MACH_PORT_NULL)) == MACH_MSG_SUCCESS) - { - /* We got a message. Decode it. */ -#define IO_SELECT_REPLY_MSGID (21012 + 100) /* XXX */ -#ifdef MACH_MSG_TYPE_BIT - const union typeword inttype = - { type: - { MACH_MSG_TYPE_INTEGER_T, sizeof (integer_t) * 8, 1, 1, 0, 0 } - }; -#endif - if (msg.head.msgh_id == IO_SELECT_REPLY_MSGID && - msg.head.msgh_size >= sizeof msg.error && - !(msg.head.msgh_bits & MACH_MSGH_BITS_COMPLEX) && -#ifdef MACH_MSG_TYPE_BIT - msg.error.err_type.word == inttype.word -#endif - ) - { - /* This is a properly formatted message so far. - See if it is a success or a failure. */ - if (msg.error.err == EINTR && - msg.head.msgh_size == sizeof msg.error) - { - /* EINTR response; poll for further responses - and then return quickly. */ - err = EINTR; - goto poll; - } - if (msg.error.err || - msg.head.msgh_size != sizeof msg.success || -#ifdef MACH_MSG_TYPE_BIT - msg.success.result_type.word != inttype.word || -#endif - (msg.success.result & SELECT_ALL) == 0) - { - /* Error or bogus reply. Simulate readiness. */ - __mach_msg_destroy (&msg.head); - msg.success.result = SELECT_ALL; - } - - /* Look up the respondent's reply port and record its - readiness. */ - { - int had = got; - if (firstfd != -1) - for (i = firstfd; i <= lastfd; ++i) - if (d[i].type - && d[i].reply_port == msg.head.msgh_local_port) - { - d[i].type &= msg.success.result; - d[i].type |= SELECT_RETURNED; - ++got; - } - assert (got > had); - } - } - - if (msg.head.msgh_remote_port != MACH_PORT_NULL) - __mach_port_deallocate (__mach_task_self (), - msg.head.msgh_remote_port); - - if (got) - poll: - { - /* Poll for another message. */ - to = 0; - options |= MACH_RCV_TIMEOUT; - } - } - - if (msgerr == MACH_RCV_INTERRUPTED) - /* Interruption on our side (e.g. signal reception). */ - err = EINTR; - - if (got) - /* At least one descriptor is known to be ready now, so we will - return success. */ - err = 0; - } - - if (firstfd != -1) - for (i = firstfd; i <= lastfd; ++i) - if (d[i].type) - __mach_port_destroy (__mach_task_self (), d[i].reply_port); - if (firstfd == -1 || (firstfd != lastfd && portset != MACH_PORT_NULL)) - /* Destroy PORTSET, but only if it's not actually the reply port for a - single descriptor (in which case it's destroyed in the previous loop; - not doing it here is just a bit more efficient). */ - __mach_port_destroy (__mach_task_self (), portset); - - if (err) - { - if (sigmask) - __sigprocmask (SIG_SETMASK, &oset, NULL); - return __hurd_fail (err); - } + got = _wait_for_replies (nfds, firstfd, lastfd, to, + err, portset, d, + timeout, sigmask, &oset); + if (got == -1) + return -1; - if (pollfds) + if (pollfds) /* POLL */ /* Fill in the `revents' members of the user's array. */ for (i = 0; i < nfds; ++i) { @@ -453,7 +487,7 @@ _hurd_select (int nfds, pollfds[i].revents = revents; } - else + else /* SELECT */ { /* Below we recalculate GOT to include an increment for each operation allowed on each fd. */ @@ -482,7 +516,7 @@ _hurd_select (int nfds, else if (exceptfds) FD_CLR (i, exceptfds); } - } + } /* SELECT */ if (sigmask && __sigprocmask (SIG_SETMASK, &oset, NULL)) return -1;