home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 036 / emxfix02.zip / emx / src / os2 / select.c < prev    next >
C/C++ Source or Header  |  1994-12-21  |  11KB  |  428 lines

  1. /* select.c -- Implement select()
  2.    Copyright (c) 1993-1994 by Eberhard Mattes
  3.  
  4. This file is part of emx.
  5.  
  6. emx is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. emx is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with emx; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. As special exception, emx.dll can be distributed without source code
  21. unless it has been changed.  If you modify emx.dll, this exception
  22. no longer applies and you must remove this paragraph from all source
  23. files for emx.dll.  */
  24.  
  25.  
  26. #define INCL_DOSDEVIOCTL
  27. #define INCL_DOSSEMAPHORES
  28. #define INCL_DOSPROCESS
  29. #define INCL_DOSMISC
  30. #define INCL_DOSERRORS
  31. #include <os2emx.h>
  32. #include <sys/emx.h>
  33. #include <sys/types.h>
  34. #include <sys/termio.h>
  35. #include <sys/time.h>
  36. #include <sys/errno.h>
  37. #include "emxdll.h"
  38. #include "files.h"
  39. #include "tcpip.h"
  40. #include "select.h"
  41. #include "clib.h"
  42.  
  43. static HEV npipe_sem;
  44. static BYTE npipe_sem_open;
  45.  
  46. HEV socket_sem;
  47. HEV socket_done_sem;
  48. HEV socket_start_sem;
  49. BYTE select_socket_done;
  50. static BYTE socket_sem_open;
  51.  
  52.  
  53. static int select_add_read (struct select_data *d, int fd)
  54. {
  55.   ULONG rc, ht;
  56.  
  57.   if (fd >= handle_count)
  58.     return (EBADF);
  59.   ht = handle_flags[fd];
  60.   if (!(ht & HF_OPEN))
  61.     return (EBADF);
  62.   if (ht & HF_NPIPE)
  63.     {
  64.       if (!d->sem_npipe_flag)
  65.         {
  66.           if (!npipe_sem_open)
  67.             {
  68.               if (create_event_sem (&npipe_sem, DC_SEM_SHARED) != 0)
  69.                 return (EINVAL);
  70.               npipe_sem_open = TRUE;
  71.             }
  72.           if (reset_event_sem (npipe_sem) != 0)
  73.             return (EINVAL);
  74.           d->list[d->sem_count].hsemCur = (HSEM)npipe_sem;
  75.           d->list[d->sem_count].ulUser = 0;
  76.           ++d->sem_count;
  77.           d->sem_npipe_flag = TRUE;
  78.         }
  79.       rc = DosSetNPipeSem (fd, (HSEM)npipe_sem, 0);
  80.       if (rc != 0)
  81.         {
  82.           error (rc, "DosSetNPipeSem");
  83.           return (EACCES);
  84.         }
  85.     }
  86.   else if (ht & HF_CON)
  87.     {
  88.       if (IS_VALID_FILE (fd) && !(GET_FILE (fd)->c_lflag & IDEFAULT)
  89.           && !d->sem_kbd_flag)
  90.         {
  91.           if (reset_event_sem (kbd_sem_new) != 0)
  92.             return (EINVAL);
  93.           d->list[d->sem_count].hsemCur = (HSEM)kbd_sem_new;
  94.           d->list[d->sem_count].ulUser = 0;
  95.           ++d->sem_count;
  96.           d->sem_kbd_flag = TRUE;
  97.         }
  98.     }
  99.   else if (ht & HF_SOCKET)
  100.     {
  101.       if (d->socket_count >= SELECT_MAX_SOCKETS || !IS_VALID_FILE (fd))
  102.         return (EBADF);
  103.       d->sockets[d->socket_count] = GET_FILE (fd)->x.socket.handle;
  104.       d->socketh[d->socket_count] = fd;
  105.       ++d->socket_count; ++d->socket_nread;
  106.     }
  107.   else
  108.     {
  109.       /* Ignore other types of handles */
  110.     }
  111.   return (0);
  112. }
  113.  
  114.  
  115. static int select_add_write (struct select_data *d, int fd)
  116. {
  117.   ULONG ht;
  118.  
  119.   if (fd >= handle_count)
  120.     return (EBADF);
  121.   ht = handle_flags[fd];
  122.   if (!(ht & HF_OPEN))
  123.     return (EBADF);
  124.   if (ht & HF_SOCKET)
  125.     {
  126.       if (d->socket_count >= SELECT_MAX_SOCKETS || !IS_VALID_FILE (fd))
  127.         return (EBADF);
  128.       d->sockets[d->socket_count] = GET_FILE (fd)->x.socket.handle;
  129.       d->socketh[d->socket_count] = fd;
  130.       ++d->socket_count; ++d->socket_nwrite;
  131.     }
  132.   else
  133.     {
  134.       /* Ignore other types of handles */
  135.     }
  136.   return (0);
  137. }
  138.  
  139.  
  140. static int select_add_except (struct select_data *d, int fd)
  141. {
  142.   ULONG ht;
  143.  
  144.   if (fd >= handle_count)
  145.     return (EBADF);
  146.   ht = handle_flags[fd];
  147.   if (!(ht & HF_OPEN))
  148.     return (EBADF);
  149.   if (ht & HF_SOCKET)
  150.     {
  151.       if (d->socket_count >= SELECT_MAX_SOCKETS || !IS_VALID_FILE (fd))
  152.         return (EBADF);
  153.       d->sockets[d->socket_count] = GET_FILE (fd)->x.socket.handle;
  154.       d->socketh[d->socket_count] = fd;
  155.       ++d->socket_count; ++d->socket_nexcept;
  156.     }
  157.   else
  158.     {
  159.       /* Ignore other types of handles */
  160.     }
  161.   return (0);
  162. }
  163.  
  164.  
  165. static int select_init (struct select_data *d, struct _select *args)
  166. {
  167.   int fd, e;
  168.   ULONG rc;
  169.   struct timeval *tv;
  170.  
  171.   d->timeout = SEM_INDEFINITE_WAIT;
  172.   tv = args->timeout;
  173.   if (tv != NULL)
  174.     {
  175.       if (tv->tv_sec < 0 || tv->tv_sec >= 4294967 || tv->tv_usec < 0)
  176.         return (EINVAL);
  177.       d->timeout = tv->tv_sec * 1000;
  178.       d->timeout += tv->tv_usec / 1000; /* TODO: overflow? rounding? */
  179.     }
  180.   if (args->readfds != NULL)
  181.     for (fd = 0; fd < args->nfds; ++fd)
  182.       if (FD_ISSET (fd, args->readfds))
  183.         {
  184.           e = select_add_read (d, fd);
  185.           if (e != 0)
  186.             return (e);
  187.         }
  188.   if (args->writefds != NULL)
  189.     for (fd = 0; fd < args->nfds; ++fd)
  190.       if (FD_ISSET (fd, args->writefds))
  191.         {
  192.           e = select_add_write (d, fd);
  193.           if (e != 0)
  194.             return (e);
  195.         }
  196.   if (args->exceptfds != NULL)
  197.     for (fd = 0; fd < args->nfds; ++fd)
  198.       if (FD_ISSET (fd, args->exceptfds))
  199.         {
  200.           e = select_add_except (d, fd);
  201.           if (e != 0)
  202.             return (e);
  203.         }
  204.  
  205.   /* If socket handles are involved and the time-out is non-zero, add
  206.      a semaphore which will be posted when a socket handle becomes
  207.      ready. */
  208.  
  209.   if (d->socket_count != 0 && d->timeout != 0)
  210.     {
  211.       if (!socket_sem_open)
  212.         {
  213.           if (create_event_sem (&socket_sem, DC_SEM_SHARED) != 0
  214.               || create_event_sem (&socket_done_sem, 0) != 0
  215.               || create_event_sem (&socket_start_sem, 0) != 0)
  216.             return (EINVAL);
  217.           socket_sem_open = TRUE;
  218.         }
  219.       if (reset_event_sem (socket_sem) != 0
  220.           || reset_event_sem (socket_done_sem) != 0
  221.           || reset_event_sem (socket_start_sem) != 0)
  222.         return (EINVAL);
  223.       d->list[d->sem_count].hsemCur = (HSEM)socket_sem;
  224.       d->list[d->sem_count].ulUser = 0;
  225.       ++d->sem_count;
  226.     }
  227.  
  228.   d->list[d->sem_count].hsemCur = (HSEM)signal_sem;
  229.   d->list[d->sem_count].ulUser = 1;
  230.   ++d->sem_count;
  231.   rc = DosCreateMuxWaitSem (NULL, &d->sem_mux, d->sem_count, d->list,
  232.                             DCMW_WAIT_ANY);
  233.   if (rc != 0)
  234.     {
  235.       error (rc, "DosCreateMuxWaitSem");
  236.       return (EINVAL);
  237.     }
  238.   d->sem_mux_flag = TRUE;
  239.   return (0);
  240. }
  241.  
  242.  
  243. static void select_check_read (struct select_data *d, int fd)
  244. {
  245.   ULONG rc, ht;
  246.  
  247.   ht = handle_flags[fd];
  248.   if (!(ht & HF_OPEN))
  249.     return;
  250.   if (ht & HF_NPIPE)
  251.     {
  252.       ULONG buffer, nread, state;
  253.       AVAILDATA avail;
  254.  
  255.       rc = DosPeekNPipe (fd, &buffer, 0, &nread, &avail, &state);
  256.       if (rc != 0)
  257.         {
  258.           /* Ignore */
  259.         }
  260.       else if (avail.cbpipe != 0)
  261.         {
  262.           FD_SET (fd, &d->rbits);
  263.           ++d->ready_count;
  264.         }
  265.     }
  266.   else if (ht & HF_CON)
  267.     {
  268.       if (IS_VALID_FILE (fd) && !(GET_FILE (fd)->c_lflag & IDEFAULT)
  269.           && termio_avail (fd) != 0)
  270.         {
  271.           FD_SET (fd, &d->rbits);
  272.           ++d->ready_count;
  273.         }
  274.     }
  275.   else if (ht & HF_ASYNC)
  276.     {
  277.       if (async_avail (fd) != 0)
  278.         {
  279.           FD_SET (fd, &d->rbits);
  280.           ++d->ready_count;
  281.         }
  282.     }
  283. }
  284.  
  285.  
  286. static int select_poll (struct select_data *d, struct _select *args)
  287. {
  288.   int fd;
  289.  
  290.   FD_ZERO (&d->rbits);
  291.   FD_ZERO (&d->wbits);
  292.   FD_ZERO (&d->ebits);
  293.  
  294.   if (args->readfds != 0)
  295.     {
  296.       for (fd = 0; fd < args->nfds; ++fd)
  297.         if (FD_ISSET (fd, args->readfds))
  298.           select_check_read (d, fd);
  299.     }
  300.  
  301.   if (d->socket_count != 0)
  302.     {
  303.       int n, e;
  304.  
  305.       n = tcpip_select_poll (d, &e);
  306.       if (n < 0)
  307.         return (e);
  308.     }
  309.  
  310.   if (d->ready_count != 0)
  311.     {
  312.       if (args->readfds != 0)
  313.         *args->readfds = d->rbits;
  314.       if (args->writefds != 0)
  315.         *args->writefds = d->wbits;
  316.       if (args->exceptfds != 0)
  317.         *args->exceptfds = d->ebits;
  318.       d->return_value = d->ready_count;
  319.     }
  320.   return (0);
  321. }
  322.  
  323.  
  324. /* Wait until a semaphore is posted (pipe ready, keyboard data
  325.    available, socket ready, signal) or until the timeout elapses,
  326.    whichever comes first.  Return -1 on timeout.  Otherwise return 0
  327.    or errno. */
  328.  
  329. static int select_wait (struct select_data *d)
  330. {
  331.   ULONG rc, user;
  332.   TID socket_tid;
  333.  
  334.   if (d->timeout == 0)
  335.     return (-1);                /* Timed out */
  336.  
  337.   if (d->socket_count != 0)
  338.     {
  339.       select_socket_done = FALSE;
  340.       rc = DosCreateThread (&socket_tid, tcpip_select_thread,
  341.                             (ULONG)d, 0, 0x4000);
  342.       if (rc != 0)
  343.         {
  344.           error (rc, "DosCreateThread");
  345.           return (EINVAL);
  346.         }
  347.  
  348.       /* Wait until tcpip_select_thread() has installed its exception
  349.          handler. */
  350.       do
  351.         {
  352.           rc = DosWaitEventSem (socket_start_sem, SEM_INDEFINITE_WAIT);
  353.         } while (rc == ERROR_INTERRUPT);
  354.       if (rc != 0)
  355.         error (rc, "DosWaitEventSem");
  356.     }
  357.  
  358.   rc = DosWaitMuxWaitSem (d->sem_mux, d->timeout, &user);
  359.  
  360.   if (d->socket_count != 0)
  361.     {
  362.       ULONG rc2;
  363.  
  364.       /* Fortunately, a thread blocked in the select() function of IBM
  365.          TCP/IP for OS/2 can be killed.  At least under OS/2 3.0.
  366.          Unfortunately, DosKillThread is reported to not work
  367.          correctly under OS/2 2.1 and older. */
  368.  
  369.       select_socket_done = TRUE;
  370.       if (!dont_doskillthread)
  371.         DosKillThread (socket_tid);
  372.       do
  373.         {
  374.           rc2 = DosWaitEventSem (socket_done_sem, SEM_INDEFINITE_WAIT);
  375.         } while (rc2 == ERROR_INTERRUPT);
  376.       if (rc2 != 0)
  377.         error (rc2, "DosWaitEventSem");
  378.     }
  379.  
  380.   if (rc == ERROR_INTERRUPT || sig_flag)
  381.     return (EINTR);
  382.   if (rc == ERROR_TIMEOUT)
  383.     return (-1);
  384.   if (rc != 0)
  385.     {
  386.       error (rc, "DosWaitMuxWaitSem");
  387.       return (EINVAL);
  388.     }
  389.   return (0);
  390. }
  391.  
  392.  
  393. static void select_cleanup (struct select_data *d)
  394. {
  395.   if (d->sem_mux_flag)
  396.     DosCloseMuxWaitSem (d->sem_mux);
  397. }
  398.  
  399.  
  400. int do_select (struct _select *args, int *errnop)
  401. {
  402.   struct select_data d;
  403.   int e;
  404.  
  405.   sig_block_start ();
  406.   d.td = get_thread ();
  407.   d.sem_npipe_flag = FALSE; d.sem_kbd_flag = FALSE; d.sem_mux_flag = FALSE;
  408.   d.sem_count = 0; d.ready_count = 0; d.return_value = 0;
  409.   d.socket_count = 0; d.socket_nread = d.socket_nwrite = d.socket_nexcept = 0;
  410.   e = select_init (&d, args);
  411.   if (e == 0)
  412.     {
  413.       e = select_poll (&d, args);
  414.       if (e == 0 && d.return_value == 0) /* No handles ready */
  415.         {
  416.           e = select_wait (&d);
  417.           if (e == -1)
  418.             e = 0;
  419.           else if (e == 0)
  420.             e = select_poll (&d, args);
  421.         }
  422.     }
  423.   select_cleanup (&d);
  424.   sig_block_end ();
  425.   *errnop = e;
  426.   return (e == 0 ? d.return_value : -1);
  427. }
  428.