home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / mach / hurd / __select.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-20  |  5.7 KB  |  204 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library 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 GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <sys/types.h>
  21. #include <hurd.h>
  22. #include <gnu-stabs.h>
  23.  
  24. /* Check the first NFDS descriptors each in READFDS (if not NULL) for read
  25.    readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
  26.    (if not NULL) for exceptional conditions.  If TIMEOUT is not NULL, time out
  27.    after waiting the interval specified therein.  Returns the number of ready
  28.    descriptors, or -1 for errors.  */
  29. int
  30. DEFUN(__select, (nfds, readfds, writefds, exceptfds, timeout),
  31.       int nfds AND fd_set *readfds AND fd_set *writefds AND
  32.       fd_set *exceptfds AND struct timeval *timeout)
  33. {
  34.   int i;
  35.   mach_port_t port;
  36.   int got;
  37.   struct _hurd_dtable dtable;
  38.   int dealloc_dtable; *dealloc, *types;
  39.   mach_port_t *ports;
  40.   struct _hurd_fd *cells;
  41.   error_t err;
  42.  
  43.  
  44.   /* If we're going to bomb, do it before we acquire any locks.  */
  45.   if (readfds != NULL)
  46.     *(volatile fd_set *) readfds;
  47.   if (writefds != NULL)
  48.     *(volatile fd_set *) writefds;
  49.   if (exceptfds != NULL)
  50.     *(volatile fd_set *) exceptfds;
  51.  
  52.   if (timeout != NULL && timeout->tv_sec == 0 && timeval->tv_usec == 0)
  53.     port = MACH_PORT_NULL;
  54.   else
  55.     {
  56.       /* Get a port to receive io_select_done messages on.  */
  57.       __mach_port_allocate (__mach_task_self (),
  58.                 MACH_PORT_RIGHT_RECEIVE, &port);
  59.     }
  60.  
  61.   dtable = _hurd_dtable_use (&dealloc_dtable);
  62.  
  63.   if (nfds > _hurd_dtable.size)
  64.     nfds = _hurd_dtable.size;
  65.  
  66.   /* Collect the ports for interesting FDs.  */
  67.   cells = __alloca (nfds * sizeof (*cells));
  68.   ports = __alloca (nfds * sizeof (*ports));
  69.   types = __alloca (nfds * sizeof (*types));
  70.   dealloc = __alloca (nfds * sizeof (*dealloc));
  71.   for (i = 0; i < nfds; ++i)
  72.     {
  73.       int type = 0;
  74.       if (readfds != NULL && FD_ISSET (i, readfds))
  75.     type |= SELECT_READ;
  76.       if (writefds != NULL && FD_ISSET (i, writefds))
  77.     type |= SELECT_WRITE;
  78.       if (exceptfds != NULL && FD_ISSET (i, exceptfds))
  79.     type |= SELECT_URG;
  80.       types[i] = type;
  81.       if (type)
  82.     {
  83.       cells[i] = _hurd_dtable_fd (i, dtable);
  84.       ports[i] = _hurd_port_locked_get (&cells[i]->port, &dealloc[i]);
  85.       if (ports[i] == MACH_PORT_NULL)
  86.         {
  87.           while (i-- > 0)
  88.         _hurd_port_free (&cells[i]->port, &dealloc[i]);
  89.           _hurd_dtable_done (dtable, &dealloc_dtable);
  90.           errno = EBADF;
  91.           return -1;
  92.         }
  93.     }
  94.     }
  95.  
  96.   /* Send them all io_select calls.  */
  97.   got = 0;
  98.   err = 0;
  99.   for (i = 0; i < nfds; ++i)
  100.     if (types[i])
  101.       {
  102.     if (!err)
  103.       {
  104.         err = __io_select (ports[i], types[i], port, i, &types[i]);
  105.         if (types[i])
  106.           ++got;
  107.       }
  108.     _hurd_port_free (&cells[i]->port, ports[i], &dealloc[i]);
  109.       }
  110.  
  111.   if (!err && got == 0 && port != MACH_PORT_NULL)
  112.     {
  113.       struct
  114.     {
  115.       mach_msg_header_t head;
  116.       mach_msg_type_t result_type;
  117.       int result;
  118.       mach_msg_type_t tag_type;
  119.       int tag;
  120.     } msg;
  121.       mach_msg_timeout_t timeout = (timeval != NULL ?
  122.                      (timeval->tv_sec * 1000 +
  123.                      timeval->tv_usec / 1000) :
  124.                      0);
  125.       mach_msg_option_t options = (timeval == NULL ? 0 : MACH_RCV_TIMEOUT);
  126.     receive:
  127.       switch (err = __mach_msg (&msg, MACH_RCV_MSG | options, 0, sizeof (msg),
  128.                 port, timeout, MACH_PORT_NULL))
  129.     {
  130.     case MACH_MSG_SUCCESS:
  131.       {
  132.         const mach_msg_type_t inttype =
  133.           { MACH_MSG_TYPE_INTEGER_32, 32, 1, 1, 0, 0 };
  134.         if (msg.head.msgh_id == SELECT_DONE_MSGID &&
  135.         !(msg.head.msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
  136.         *(int *) &msg.result_type == *(int *) &inttype &&
  137.         *(int *) &msg.tag_type == *(int *) &inttype &&
  138.         (msg.result & (SELECT_READ|SELECT_WRITE|SELECT_URG)) &&
  139.         msg.tag >= 0 && msg.tag < nfds)
  140.           {
  141.         types[i] = msg.result;
  142.         ++got;
  143.         if (msg.msgh_remote_port != MACH_PORT_NULL)
  144.           {
  145.             msg.head.msgh_id += 100;
  146.             msg.result_type = inttype;
  147.             msg.result = POSIX_SUCCESS;
  148.             options = MACH_SEND_MSG | MACH_RCV_TIMEOUT;
  149.           }
  150.           }
  151.         else
  152.           {
  153.         /* Randomness.  */
  154.         __mach_msg_destroy (msg);
  155.         options = MACH_RCV_TIMEOUT;
  156.           }
  157.  
  158.         /* Poll for another message.  */
  159.         timeout = 0;
  160.         goto receive;
  161.       }
  162.  
  163.     case MACH_RCV_TIMED_OUT:
  164.       err = 0;
  165.       break;
  166.     }
  167.     }
  168.  
  169.   if (port != MACH_PORT_NULL)
  170.     /* We must destroy the port if we made some select requests
  171.        that might send notification on that port after we no longer care.
  172.        If the port were reused, that notification could confuse the next
  173.        select call to use the port.  The notification might be valid,
  174.        but the descriptor may have changed to a different server.  */
  175.     __mach_port_destroy (port);
  176.  
  177.   if (err)
  178.     return __hurd_fail (err);
  179.  
  180.   /* Set the user bitarrays.  */
  181.   for (i = 0; i < nfds; ++i)
  182.     if (types[i] & (SELECT_READ|SELECT_WRITE|SELECT_URG))
  183.       {
  184.     if (readfds != NULL)
  185.       if (types[i] & SELECT_READ)
  186.         FD_SET (i, readfds);
  187.       else
  188.         FD_CLR (i, readfds);
  189.     if (writefds != NULL)
  190.       if (types[i] & SELECT_WRITE)
  191.         FD_SET (i, writefds);
  192.       else
  193.         FD_CLR (i, writefds);
  194.     if (exceptfds != NULL)
  195.       if (types[i] & SELECT_URG)
  196.         FD_SET (i, exceptfds);
  197.       else
  198.         FD_CLR (i, exceptfds);
  199.     ++got;
  200.       }
  201.  
  202.   return got;
  203. }
  204.