home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / os / connection.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  26.3 KB  |  1,029 lines

  1. /***********************************************************
  2. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: connection.c,v 1.146 92/06/11 10:38:45 rws Exp $ */
  25. /*****************************************************************
  26.  *  Stuff to create connections --- OS dependent
  27.  *
  28.  *      EstablishNewConnections, CreateWellKnownSockets, ResetWellKnownSockets,
  29.  *      CloseDownConnection, CheckConnections, AddEnabledDevice,
  30.  *    RemoveEnabledDevice, OnlyListToOneClient,
  31.  *      ListenToAllClients,
  32.  *
  33.  *      (WaitForSomething is in its own file)
  34.  *
  35.  *      In this implementation, a client socket table is not kept.
  36.  *      Instead, what would be the index into the table is just the
  37.  *      file descriptor of the socket.  This won't work for if the
  38.  *      socket ids aren't small nums (0 - 2^8)
  39.  *
  40.  *****************************************************************/
  41.  
  42. #include "X.h"
  43. #include "Xproto.h"
  44. #include <sys/param.h>
  45. #include <errno.h>
  46. #include "Xos.h"            /* for strings, file, time */
  47. #include <sys/socket.h>
  48.  
  49. #include <signal.h>
  50. #include <setjmp.h>
  51.  
  52. #ifdef hpux
  53. #include <sys/utsname.h>
  54. #include <sys/ioctl.h>
  55. #endif
  56.  
  57. #ifdef AIXV3
  58. #include <sys/ioctl.h>
  59. #endif
  60.  
  61. #ifdef TCPCONN
  62. # include <netinet/in.h>
  63. # ifndef hpux
  64. #  ifdef apollo
  65. #   ifndef NO_TCP_H
  66. #    include <netinet/tcp.h>
  67. #   endif
  68. #  else
  69. #   include <netinet/tcp.h>
  70. #  endif
  71. # endif
  72. #endif
  73.  
  74. #ifdef UNIXCONN
  75. /*
  76.  * sites should be careful to have separate /tmp directories for diskless nodes
  77.  */
  78. #include <sys/un.h>
  79. #include <sys/stat.h>
  80. static int unixDomainConnection = -1;
  81. #endif
  82.  
  83. #include <stdio.h>
  84. #include <sys/uio.h>
  85. #include "osstruct.h"
  86. #include "osdep.h"
  87. #include "opaque.h"
  88. #include "dixstruct.h"
  89.  
  90. #ifdef DNETCONN
  91. #include <netdnet/dn.h>
  92. #endif /* DNETCONN */
  93.  
  94. #ifdef SIGNALRETURNSINT
  95. #define SIGVAL int
  96. #else
  97. #define SIGVAL void
  98. #endif
  99.  
  100. typedef long CCID;      /* mask of indices into client socket table */
  101.  
  102. #ifndef X_UNIX_PATH
  103. #ifdef hpux
  104. #define X_UNIX_DIR    "/usr/spool/sockets/X11"
  105. #define X_UNIX_PATH    "/usr/spool/sockets/X11/"
  106. #define OLD_UNIX_DIR    "/tmp/.X11-unix"
  107. #else
  108. #define X_UNIX_DIR    "/tmp/.X11-unix"
  109. #define X_UNIX_PATH    "/tmp/.X11-unix/X"
  110. #endif
  111. #endif
  112.  
  113. extern char *display;        /* The display number */
  114. int lastfdesc;            /* maximum file descriptor */
  115.  
  116. long WellKnownConnections;    /* Listener mask */
  117. long EnabledDevices[mskcnt];    /* mask for input devices that are on */
  118. long AllSockets[mskcnt];    /* select on this */
  119. long AllClients[mskcnt];    /* available clients */
  120. long LastSelectMask[mskcnt];    /* mask returned from last select call */
  121. long ClientsWithInput[mskcnt];    /* clients with FULL requests in buffer */
  122. long ClientsWriteBlocked[mskcnt];/* clients who cannot receive output */
  123. long OutputPending[mskcnt];    /* clients with reply/event data ready to go */
  124. long MaxClients = MAXSOCKS ;
  125. long NConnBitArrays = mskcnt;
  126. Bool NewOutputPending;        /* not yet attempted to write some new output */
  127. Bool AnyClientsWriteBlocked;    /* true if some client blocked on write */
  128.  
  129. Bool RunFromSmartParent;    /* send SIGUSR1 to parent process */
  130. Bool PartialNetwork;        /* continue even if unable to bind all addrs */
  131. static int ParentProcess;
  132.  
  133. static Bool debug_conns = FALSE;
  134.  
  135. static int SavedAllClients[mskcnt];
  136. static int SavedAllSockets[mskcnt];
  137. static int SavedClientsWithInput[mskcnt];
  138. int GrabInProgress = 0;
  139.  
  140. int ConnectionTranslation[MAXSOCKS];
  141. extern ClientPtr NextAvailableClient();
  142.  
  143. extern SIGVAL AutoResetServer();
  144. extern SIGVAL GiveUp();
  145. extern XID CheckAuthorization();
  146. static void CloseDownFileDescriptor(), ErrorConnMax();
  147. extern void FreeOsBuffers(), ResetOsBuffers();
  148.  
  149. #ifdef XDMCP
  150. void XdmcpOpenDisplay(), XdmcpInit(), XdmcpReset(), XdmcpCloseDisplay();
  151. #endif
  152.  
  153. #ifdef TCPCONN
  154. static int
  155. open_tcp_socket ()
  156. {
  157.     struct sockaddr_in insock;
  158.     int request;
  159.     int retry;
  160. #ifndef SO_DONTLINGER
  161. #ifdef SO_LINGER
  162.     static int linger[2] = { 0, 0 };
  163. #endif /* SO_LINGER */
  164. #endif /* SO_DONTLINGER */
  165.  
  166. #ifdef AIXV3
  167. #ifndef FORCE_DISPLAY_NUM
  168.     extern int AIXTCPSocket;
  169.     if (AIXTCPSocket>=0) {
  170.         request= AIXTCPSocket;
  171.     } else
  172. #endif /* FORCE_DISPLAY_NUM */
  173. #endif /* AIX && etc. */
  174.     if ((request = socket (AF_INET, SOCK_STREAM, 0)) < 0) 
  175.     {
  176.     Error ("Creating TCP socket");
  177.     return -1;
  178.     } 
  179. #ifdef SO_REUSEADDR
  180.     /* Necesary to restart the server without a reboot */
  181.     {
  182.     int one = 1;
  183.     setsockopt(request, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
  184.     }
  185. #endif /* SO_REUSEADDR */
  186. #ifdef AIXV3
  187. #ifndef FORCE_DISPLAY_NUMBER
  188.     if (AIXTCPSocket<0)
  189. #endif
  190. #endif
  191.     {
  192.     bzero ((char *)&insock, sizeof (insock));
  193.     insock.sin_family = AF_INET;
  194.     insock.sin_port = htons ((unsigned short)(X_TCP_PORT + atoi (display)));
  195.     insock.sin_addr.s_addr = htonl(INADDR_ANY);
  196.     retry = 20;
  197.     while (bind(request, (struct sockaddr *) &insock, sizeof (insock)))
  198.     {
  199.     if (--retry == 0) {
  200.         Error ("Binding TCP socket");
  201.         close (request);
  202.         return -1;
  203.     }
  204. #ifdef SO_REUSEADDR
  205.     sleep (1);
  206. #else
  207.     sleep (10);
  208. #endif /* SO_REUSEDADDR */
  209.     }
  210.     }
  211. #ifdef SO_DONTLINGER
  212.     if(setsockopt (request, SOL_SOCKET, SO_DONTLINGER, (char *)NULL, 0))
  213.     Error ("Setting TCP SO_DONTLINGER");
  214. #else
  215. #ifdef SO_LINGER
  216.     if(setsockopt (request, SOL_SOCKET, SO_LINGER,
  217.            (char *)linger, sizeof(linger)))
  218.     Error ("Setting TCP SO_LINGER");
  219. #endif /* SO_LINGER */
  220. #endif /* SO_DONTLINGER */
  221.     if (listen (request, 5)) {
  222.     Error ("TCP Listening");
  223.     close (request);
  224.     return -1;
  225.     }
  226.     return request;
  227. }
  228. #endif /* TCPCONN */
  229.  
  230. #ifdef UNIXCONN
  231.  
  232. static struct sockaddr_un unsock;
  233.  
  234. static int
  235. open_unix_socket ()
  236. {
  237.     int oldUmask;
  238.     int request;
  239.  
  240.     bzero ((char *) &unsock, sizeof (unsock));
  241.     unsock.sun_family = AF_UNIX;
  242.     oldUmask = umask (0);
  243. #ifdef X_UNIX_DIR
  244.     if (!mkdir (X_UNIX_DIR, 0777))
  245.     chmod (X_UNIX_DIR, 0777);
  246. #endif
  247.     strcpy (unsock.sun_path, X_UNIX_PATH);
  248.     strcat (unsock.sun_path, display);
  249. #ifdef hpux
  250.     {  
  251.         /*    The following is for backwards compatibility
  252.          *    with old HP clients. This old scheme predates the use
  253.       *    of the /usr/spool/sockets directory, and uses hostname:display
  254.       *    in the /tmp/.X11-unix directory
  255.          */
  256.         struct utsname systemName;
  257.     static char oldLinkName[256];
  258.  
  259.         uname(&systemName);
  260.         strcpy(oldLinkName, OLD_UNIX_DIR);
  261.         if (!mkdir(oldLinkName, 0777))
  262.         chown(oldLinkName, 2, 3);
  263.         strcat(oldLinkName, "/");
  264.         strcat(oldLinkName, systemName.nodename);
  265.         strcat(oldLinkName, display);
  266.         unlink(oldLinkName);
  267.         symlink(unsock.sun_path, oldLinkName);
  268.     }
  269. #endif    /* hpux */
  270.     unlink (unsock.sun_path);
  271.     if ((request = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) 
  272.     {
  273.     Error ("Creating Unix socket");
  274.     return -1;
  275.     } 
  276.     if (bind(request, (struct sockaddr *)&unsock, strlen(unsock.sun_path)+2))
  277.     {
  278.     Error ("Binding Unix socket");
  279.     close (request);
  280.     return -1;
  281.     }
  282.     if (listen (request, 5))
  283.     {
  284.     Error ("Unix Listening");
  285.     close (request);
  286.     return -1;
  287.     }
  288.     (void)umask(oldUmask);
  289.     return request;
  290. }
  291. #endif /*UNIXCONN */
  292.  
  293. #ifdef hpux
  294. /*
  295.  * hpux returns EOPNOTSUPP when using getpeername on a unix-domain
  296.  * socket.  In this case, smash the socket address with the address
  297.  * used to bind the connection socket and return success.
  298.  */
  299. hpux_getpeername(fd, from, fromlen)
  300.     int    fd;
  301.     struct sockaddr *from;
  302.     int            *fromlen;
  303. {
  304.     int        ret;
  305.     int        len;
  306.  
  307.     ret = getpeername(fd, from, fromlen);
  308.     if (ret == -1 && errno == EOPNOTSUPP)
  309.     {
  310.     ret = 0;
  311.     len = strlen(unsock.sun_path)+2;
  312.     if (len > *fromlen)
  313.         len = *fromlen;
  314.     bcopy ((char *) &unsock, (char *) from, len);
  315.     *fromlen = len;
  316.     }
  317.     return ret;
  318. }
  319.  
  320. #define getpeername(fd, from, fromlen)    hpux_getpeername(fd, from, fromlen)
  321.  
  322. #endif
  323.  
  324. #ifdef DNETCONN
  325. static int
  326. open_dnet_socket ()
  327. {
  328.     int request;
  329.     struct sockaddr_dn dnsock;
  330.  
  331.     if ((request = socket (AF_DECnet, SOCK_STREAM, 0)) < 0) 
  332.     {
  333.     Error ("Creating DECnet socket");
  334.     return -1;
  335.     } 
  336.     bzero ((char *)&dnsock, sizeof (dnsock));
  337.     dnsock.sdn_family = AF_DECnet;
  338.     sprintf(dnsock.sdn_objname, "X$X%d", atoi (display));
  339.     dnsock.sdn_objnamel = strlen(dnsock.sdn_objname);
  340.     if (bind (request, (struct sockaddr *) &dnsock, sizeof (dnsock)))
  341.     {
  342.     Error ("Binding DECnet socket");
  343.     close (request);
  344.     return -1;
  345.     }
  346.     if (listen (request, 5))
  347.     {
  348.     Error ("DECnet Listening");
  349.     close (request);
  350.     return -1;
  351.     }
  352.     return request;
  353. }
  354. #endif /* DNETCONN */
  355.  
  356. /*****************
  357.  * CreateWellKnownSockets
  358.  *    At initialization, create the sockets to listen on for new clients.
  359.  *****************/
  360.  
  361. void
  362. CreateWellKnownSockets()
  363. {
  364.     int        request, i;
  365.  
  366.     CLEARBITS(AllSockets);
  367.     CLEARBITS(AllClients);
  368.     CLEARBITS(LastSelectMask);
  369.     CLEARBITS(ClientsWithInput);
  370.  
  371.     for (i=0; i<MAXSOCKS; i++) ConnectionTranslation[i] = 0;
  372.     
  373. #if defined(hpux) || defined(SVR4)
  374.     lastfdesc = _NFILE - 1;
  375. #else
  376.     lastfdesc = getdtablesize() - 1;
  377. #endif    /* hpux */
  378.  
  379.     if (lastfdesc > MAXSOCKS)
  380.     {
  381.     lastfdesc = MAXSOCKS;
  382.     if (debug_conns)
  383.         ErrorF( "GOT TO END OF SOCKETS %d\n", MAXSOCKS);
  384.     }
  385.  
  386.     WellKnownConnections = 0;
  387. #ifdef TCPCONN
  388.     if ((request = open_tcp_socket ()) != -1) {
  389.     WellKnownConnections |= (1L << request);
  390.     DefineSelf (request);
  391.     }
  392.     else if (!PartialNetwork) 
  393.     {
  394.     FatalError ("Cannot establish tcp listening socket");
  395.     }
  396. #endif /* TCPCONN */
  397. #ifdef DNETCONN
  398.     if ((request = open_dnet_socket ()) != -1) {
  399.     WellKnownConnections |= (1L << request);
  400.     DefineSelf (request);
  401.     }
  402.     else if (!PartialNetwork) 
  403.     {
  404.     FatalError ("Cannot establish dnet listening socket");
  405.     }
  406. #endif /* DNETCONN */
  407. #ifdef UNIXCONN
  408.     if ((request = open_unix_socket ()) != -1) {
  409.     WellKnownConnections |= (1L << request);
  410.     unixDomainConnection = request;
  411.     }
  412.     else if (!PartialNetwork) 
  413.     {
  414.     FatalError ("Cannot establish unix listening socket");
  415.     }
  416. #endif /* UNIXCONN */
  417.     if (WellKnownConnections == 0)
  418.         FatalError ("Cannot establish any listening sockets");
  419.     signal (SIGPIPE, SIG_IGN);
  420.     signal (SIGHUP, AutoResetServer);
  421.     signal (SIGINT, GiveUp);
  422.     signal (SIGTERM, GiveUp);
  423.     AllSockets[0] = WellKnownConnections;
  424.     ResetHosts(display);
  425.     /*
  426.      * Magic:  If SIGUSR1 was set to SIG_IGN when
  427.      * the server started, assume that either
  428.      *
  429.      *  a- The parent process is ignoring SIGUSR1
  430.      *
  431.      * or
  432.      *
  433.      *  b- The parent process is expecting a SIGUSR1
  434.      *     when the server is ready to accept connections
  435.      *
  436.      * In the first case, the signal will be harmless,
  437.      * in the second case, the signal will be quite
  438.      * useful
  439.      */
  440.     if (signal (SIGUSR1, SIG_IGN) == SIG_IGN)
  441.     RunFromSmartParent = TRUE;
  442.     ParentProcess = getppid ();
  443.     if (RunFromSmartParent) {
  444.     if (ParentProcess > 0) {
  445.         kill (ParentProcess, SIGUSR1);
  446.     }
  447.     }
  448. #ifdef XDMCP
  449.     XdmcpInit ();
  450. #endif
  451. }
  452.  
  453. void
  454. ResetWellKnownSockets ()
  455. {
  456.     ResetOsBuffers();
  457. #if defined(UNIXCONN) && !defined(SVR4)
  458.     if (unixDomainConnection != -1)
  459.     {
  460.     /*
  461.      * see if the unix domain socket has disappeared
  462.      */
  463.     struct stat    statb;
  464.  
  465.     if (stat (unsock.sun_path, &statb) == -1 ||
  466.         (statb.st_mode & S_IFMT) != S_IFSOCK)
  467.     {
  468.         ErrorF ("Unix domain socket %s trashed, recreating\n",
  469.             unsock.sun_path);
  470.         (void) unlink (unsock.sun_path);
  471.         (void) close (unixDomainConnection);
  472.         WellKnownConnections &= ~(1L << unixDomainConnection);
  473.         unixDomainConnection = open_unix_socket ();
  474.         if (unixDomainConnection != -1)
  475.         WellKnownConnections |= (1L << unixDomainConnection);
  476.     }
  477.     }
  478. #endif /* UNIXCONN */
  479.     ResetAuthorization ();
  480.     ResetHosts(display);
  481.     /*
  482.      * See above in CreateWellKnownSockets about SIGUSR1
  483.      */
  484.     if (RunFromSmartParent) {
  485.     if (ParentProcess > 0) {
  486.         kill (ParentProcess, SIGUSR1);
  487.     }
  488.     }
  489.     /*
  490.      * restart XDMCP
  491.      */
  492. #ifdef XDMCP
  493.     XdmcpReset ();
  494. #endif
  495. }
  496.  
  497. /*****************************************************************
  498.  * ClientAuthorized
  499.  *
  500.  *    Sent by the client at connection setup:
  501.  *                typedef struct _xConnClientPrefix {
  502.  *                   CARD8    byteOrder;
  503.  *                   BYTE    pad;
  504.  *                   CARD16    majorVersion, minorVersion;
  505.  *                   CARD16    nbytesAuthProto;    
  506.  *                   CARD16    nbytesAuthString;   
  507.  *                 } xConnClientPrefix;
  508.  *
  509.  *         It is hoped that eventually one protocol will be agreed upon.  In the
  510.  *        mean time, a server that implements a different protocol than the
  511.  *        client expects, or a server that only implements the host-based
  512.  *        mechanism, will simply ignore this information.
  513.  *
  514.  *****************************************************************/
  515.  
  516. char * 
  517. ClientAuthorized(client, proto_n, auth_proto, string_n, auth_string)
  518.     ClientPtr client;
  519.     char *auth_proto, *auth_string;
  520.     unsigned short proto_n, string_n;
  521. {
  522.     register OsCommPtr priv;
  523.     union {
  524.     struct sockaddr sa;
  525. #ifdef UNIXCONN
  526.     struct sockaddr_un un;
  527. #endif /* UNIXCONN */
  528. #ifdef TCPCONN
  529.     struct sockaddr_in in;
  530. #endif /* TCPCONN */
  531. #ifdef DNETCONN
  532.     struct sockaddr_dn dn;
  533. #endif /* DNETCONN */
  534.     } from;
  535.     int    fromlen = sizeof (from);
  536.     XID     auth_id;
  537.  
  538.     auth_id = CheckAuthorization (proto_n, auth_proto,
  539.                   string_n, auth_string);
  540.  
  541.     priv = (OsCommPtr)client->osPrivate;
  542.     if (auth_id == (XID) ~0L && 
  543.        getpeername (priv->fd, &from.sa, &fromlen) != -1 &&
  544.         !InvalidHost (&from.sa, fromlen))
  545.     {
  546.     auth_id = (XID) 0;
  547.     }
  548.  
  549.     if (auth_id == (XID) ~0L)
  550.     return "Client is not authorized to connect to Server";
  551.  
  552.     priv->auth_id = auth_id;
  553.     priv->conn_time = 0;
  554.  
  555. #ifdef XDMCP
  556.     /* indicate to Xdmcp protocol that we've opened new client */
  557.     XdmcpOpenDisplay(priv->fd);
  558. #endif /* XDMCP */
  559.     /* At this point, if the client is authorized to change the access control
  560.      * list, we should getpeername() information, and add the client to
  561.      * the selfhosts list.  It's not really the host machine, but the
  562.      * true purpose of the selfhosts list is to see who may change the
  563.      * access control list.
  564.      */
  565.     return((char *)NULL);
  566. }
  567.  
  568. /*****************
  569.  * EstablishNewConnections
  570.  *    If anyone is waiting on listened sockets, accept them.
  571.  *    Returns a mask with indices of new clients.  Updates AllClients
  572.  *    and AllSockets.
  573.  *****************/
  574.  
  575. /*ARGSUSED*/
  576. Bool
  577. EstablishNewConnections(clientUnused, closure)
  578.     ClientPtr clientUnused;
  579.     pointer closure;
  580. {
  581.     long readyconnections;     /* mask of listeners that are ready */
  582.     int curconn;                  /* fd of listener that's ready */
  583.     register int newconn;         /* fd of new client */
  584.     long connect_time;
  585.     register int i;
  586.     register ClientPtr client;
  587.     register OsCommPtr oc;
  588.  
  589. #ifdef TCP_NODELAY
  590.     union {
  591.     struct sockaddr sa;
  592. #ifdef UNIXCONN
  593.     struct sockaddr_un un;
  594. #endif /* UNIXCONN */
  595. #ifdef TCPCONN
  596.     struct sockaddr_in in;
  597. #endif /* TCPCONN */
  598. #ifdef DNETCONN
  599.     struct sockaddr_dn dn;
  600. #endif /* DNETCONN */
  601.     } from;
  602.     int    fromlen;
  603. #endif /* TCP_NODELAY */
  604.  
  605.     readyconnections = (((long)closure) & WellKnownConnections);
  606.     if (!readyconnections)
  607.     return TRUE;
  608.     connect_time = GetTimeInMillis();
  609.     /* kill off stragglers */
  610.     for (i=1; i<currentMaxClients; i++)
  611.     {
  612.     if (client = clients[i])
  613.     {
  614.         oc = (OsCommPtr)(client->osPrivate);
  615.         if (oc && (oc->conn_time != 0) &&
  616.         (connect_time - oc->conn_time) >= TimeOutValue)
  617.         CloseDownClient(client);     
  618.     }
  619.     }
  620.     while (readyconnections) 
  621.     {
  622.     curconn = ffs (readyconnections) - 1;
  623.     readyconnections &= ~(1 << curconn);
  624.     if ((newconn = accept (curconn,
  625.                   (struct sockaddr *) NULL, 
  626.                   (int *)NULL)) < 0) 
  627.         continue;
  628. #ifdef TCP_NODELAY
  629.     fromlen = sizeof (from);
  630.     if (!getpeername (newconn, &from.sa, &fromlen))
  631.     {
  632.         if (fromlen && (from.sa.sa_family == AF_INET)) 
  633.         {
  634.         int mi = 1;
  635.         setsockopt (newconn, IPPROTO_TCP, TCP_NODELAY,
  636.                (char *)&mi, sizeof (int));
  637.         }
  638.     }
  639. #endif /* TCP_NODELAY */
  640.     /* ultrix reads hang on Unix sockets, hpux reads fail, AIX fails too */
  641. #if defined(O_NONBLOCK) && (!defined(ultrix) && !defined(hpux) && !defined(AIXV3))
  642.     (void) fcntl (newconn, F_SETFL, O_NONBLOCK);
  643. #else
  644. #ifdef FIOSNBIO
  645.     {
  646.         int    arg;
  647.         arg = 1;
  648.         ioctl(newconn, FIOSNBIO, &arg);
  649.     }
  650. #else
  651. #if defined(AIXV3) && defined(FIONBIO)
  652.     {
  653.         int arg;
  654.         arg = 1;
  655.         ioctl(newconn, FIONBIO, &arg);
  656.     }
  657. #else
  658.     fcntl (newconn, F_SETFL, FNDELAY);
  659. #endif
  660. #endif
  661. #endif
  662.     oc = (OsCommPtr)xalloc(sizeof(OsCommRec));
  663.     if (!oc)
  664.     {
  665.         ErrorConnMax(newconn);
  666.         close(newconn);
  667.         continue;
  668.     }
  669.     if (GrabInProgress)
  670.     {
  671.         BITSET(SavedAllClients, newconn);
  672.         BITSET(SavedAllSockets, newconn);
  673.     }
  674.     else
  675.     {
  676.         BITSET(AllClients, newconn);
  677.         BITSET(AllSockets, newconn);
  678.     }
  679.     oc->fd = newconn;
  680.     oc->input = (ConnectionInputPtr)NULL;
  681.     oc->output = (ConnectionOutputPtr)NULL;
  682.     oc->conn_time = connect_time;
  683.     if ((newconn < lastfdesc) &&
  684.         (client = NextAvailableClient((pointer)oc)))
  685.     {
  686.         ConnectionTranslation[newconn] = client->index;
  687.     }
  688.     else
  689.     {
  690.         ErrorConnMax(newconn);
  691.         CloseDownFileDescriptor(oc);
  692.     }
  693.     }
  694.     return TRUE;
  695. }
  696.  
  697. #define NOROOM "Maximum number of clients reached"
  698.  
  699. /************
  700.  *   ErrorConnMax
  701.  *     Fail a connection due to lack of client or file descriptor space
  702.  ************/
  703.  
  704. static void
  705. ErrorConnMax(fd)
  706.     register int fd;
  707. {
  708.     xConnSetupPrefix csp;
  709.     char pad[3];
  710.     struct iovec iov[3];
  711.     char byteOrder = 0;
  712.     int whichbyte = 1;
  713.     struct timeval waittime;
  714.     long mask[mskcnt];
  715.  
  716.     /* if these seems like a lot of trouble to go to, it probably is */
  717.     waittime.tv_sec = BOTIMEOUT / MILLI_PER_SECOND;
  718.     waittime.tv_usec = (BOTIMEOUT % MILLI_PER_SECOND) *
  719.                (1000000 / MILLI_PER_SECOND);
  720.     CLEARBITS(mask);
  721.     BITSET(mask, fd);
  722.     (void)select(fd + 1, (int *) mask, (int *) NULL, (int *) NULL, &waittime);
  723.     /* try to read the byte-order of the connection */
  724.     (void)read(fd, &byteOrder, 1);
  725.     if ((byteOrder == 'l') || (byteOrder == 'B'))
  726.     {
  727.     csp.success = xFalse;
  728.     csp.lengthReason = sizeof(NOROOM) - 1;
  729.     csp.length = (sizeof(NOROOM) + 2) >> 2;
  730.     csp.majorVersion = X_PROTOCOL;
  731.     csp.minorVersion = X_PROTOCOL_REVISION;
  732.     if (((*(char *) &whichbyte) && (byteOrder == 'B')) ||
  733.         (!(*(char *) &whichbyte) && (byteOrder == 'l')))
  734.     {
  735.         swaps(&csp.majorVersion, whichbyte);
  736.         swaps(&csp.minorVersion, whichbyte);
  737.         swaps(&csp.length, whichbyte);
  738.     }
  739.     iov[0].iov_len = sz_xConnSetupPrefix;
  740.     iov[0].iov_base = (char *) &csp;
  741.     iov[1].iov_len = csp.lengthReason;
  742.     iov[1].iov_base = NOROOM;
  743.     iov[2].iov_len = (4 - (csp.lengthReason & 3)) & 3;
  744.     iov[2].iov_base = pad;
  745.     (void)writev(fd, iov, 3);
  746.     }
  747. }
  748.  
  749. /************
  750.  *   CloseDownFileDescriptor:
  751.  *     Remove this file descriptor and it's I/O buffers, etc.
  752.  ************/
  753.  
  754. static void
  755. CloseDownFileDescriptor(oc)
  756.     register OsCommPtr oc;
  757. {
  758.     int connection = oc->fd;
  759.  
  760.     close(connection);
  761.     FreeOsBuffers(oc);
  762.     BITCLEAR(AllSockets, connection);
  763.     BITCLEAR(AllClients, connection);
  764.     BITCLEAR(ClientsWithInput, connection);
  765.     if (GrabInProgress)
  766.     {
  767.     BITCLEAR(SavedAllSockets, connection);
  768.     BITCLEAR(SavedAllClients, connection);
  769.     BITCLEAR(SavedClientsWithInput, connection);
  770.     }
  771.     BITCLEAR(ClientsWriteBlocked, connection);
  772.     if (!ANYSET(ClientsWriteBlocked))
  773.         AnyClientsWriteBlocked = FALSE;
  774.     BITCLEAR(OutputPending, connection);
  775.     xfree(oc);
  776. }
  777.  
  778. /*****************
  779.  * CheckConections
  780.  *    Some connection has died, go find which one and shut it down 
  781.  *    The file descriptor has been closed, but is still in AllClients.
  782.  *    If would truly be wonderful if select() would put the bogus
  783.  *    file descriptors in the exception mask, but nooooo.  So we have
  784.  *    to check each and every socket individually.
  785.  *****************/
  786.  
  787. void
  788. CheckConnections()
  789. {
  790.     long        mask;
  791.     long        tmask[mskcnt]; 
  792.     register int    curclient, curoff;
  793.     int            i;
  794.     struct timeval    notime;
  795.     int r;
  796.  
  797.     notime.tv_sec = 0;
  798.     notime.tv_usec = 0;
  799.  
  800.     for (i=0; i<mskcnt; i++)
  801.     {
  802.     mask = AllClients[i];
  803.         while (mask)
  804.         {
  805.         curoff = ffs (mask) - 1;
  806.          curclient = curoff + (i << 5);
  807.             CLEARBITS(tmask);
  808.             BITSET(tmask, curclient);
  809.             r = select (curclient + 1, (int *)tmask, (int *)NULL, (int *)NULL, 
  810.             ¬ime);
  811.             if (r < 0)
  812.         CloseDownClient(clients[ConnectionTranslation[curclient]]);
  813.         mask &= ~(1 << curoff);
  814.     }
  815.     }    
  816. }
  817.  
  818.  
  819. /*****************
  820.  * CloseDownConnection
  821.  *    Delete client from AllClients and free resources 
  822.  *****************/
  823.  
  824. CloseDownConnection(client)
  825.     ClientPtr client;
  826. {
  827.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  828.  
  829.     if (oc->output && oc->output->count)
  830.     FlushClient(client, oc, (char *)NULL, 0);
  831.     ConnectionTranslation[oc->fd] = 0;
  832. #ifdef XDMCP
  833.     XdmcpCloseDisplay(oc->fd);
  834. #endif
  835.     CloseDownFileDescriptor(oc);
  836.     client->osPrivate = (pointer)NULL;
  837. }
  838.  
  839.  
  840. AddEnabledDevice(fd)
  841.     int fd;
  842. {
  843.     BITSET(EnabledDevices, fd);
  844.     BITSET(AllSockets, fd);
  845. }
  846.  
  847.  
  848. RemoveEnabledDevice(fd)
  849.     int fd;
  850. {
  851.     BITCLEAR(EnabledDevices, fd);
  852.     BITCLEAR(AllSockets, fd);
  853. }
  854.  
  855. /*****************
  856.  * OnlyListenToOneClient:
  857.  *    Only accept requests from  one client.  Continue to handle new
  858.  *    connections, but don't take any protocol requests from the new
  859.  *    ones.  Note that if GrabInProgress is set, EstablishNewConnections
  860.  *    needs to put new clients into SavedAllSockets and SavedAllClients.
  861.  *    Note also that there is no timeout for this in the protocol.
  862.  *    This routine is "undone" by ListenToAllClients()
  863.  *****************/
  864.  
  865. OnlyListenToOneClient(client)
  866.     ClientPtr client;
  867. {
  868.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  869.     int connection = oc->fd;
  870.  
  871.     if (! GrabInProgress)
  872.     {
  873.     COPYBITS (ClientsWithInput, SavedClientsWithInput);
  874.         BITCLEAR (SavedClientsWithInput, connection);
  875.     if (GETBIT(ClientsWithInput, connection))
  876.     {
  877.         CLEARBITS(ClientsWithInput);        
  878.         BITSET(ClientsWithInput, connection);
  879.     }
  880.     else
  881.         {
  882.         CLEARBITS(ClientsWithInput);        
  883.     }
  884.     COPYBITS(AllSockets, SavedAllSockets);
  885.     COPYBITS(AllClients, SavedAllClients);
  886.  
  887.     UNSETBITS(AllSockets, AllClients);
  888.     BITSET(AllSockets, connection);
  889.     CLEARBITS(AllClients);
  890.     BITSET(AllClients, connection);
  891.     GrabInProgress = client->index;
  892.     }
  893. }
  894.  
  895. /****************
  896.  * ListenToAllClients:
  897.  *    Undoes OnlyListentToOneClient()
  898.  ****************/
  899.  
  900. ListenToAllClients()
  901. {
  902.     if (GrabInProgress)
  903.     {
  904.     ORBITS(AllSockets, AllSockets, SavedAllSockets);
  905.     ORBITS(AllClients, AllClients, SavedAllClients);
  906.     ORBITS(ClientsWithInput, ClientsWithInput, SavedClientsWithInput);
  907.     GrabInProgress = 0;
  908.     }    
  909. }
  910.  
  911. /****************
  912.  * IgnoreClient
  913.  *    Removes one client from input masks.
  914.  *    Must have cooresponding call to AttendClient.
  915.  ****************/
  916.  
  917. static long IgnoredClientsWithInput[mskcnt];
  918.  
  919. IgnoreClient (client)
  920.     ClientPtr    client;
  921. {
  922.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  923.     int connection = oc->fd;
  924.  
  925.     if (!GrabInProgress || GrabInProgress == client->index)
  926.     {
  927.         if (GETBIT (ClientsWithInput, connection))
  928.         BITSET(IgnoredClientsWithInput, connection);
  929.         else
  930.         BITCLEAR(IgnoredClientsWithInput, connection);
  931.         BITCLEAR(ClientsWithInput, connection);
  932.         BITCLEAR(AllSockets, connection);
  933.         BITCLEAR(AllClients, connection);
  934.     BITCLEAR(LastSelectMask, connection);
  935.     }
  936.     else
  937.     {
  938.         if (GETBIT (SavedClientsWithInput, connection))
  939.         BITSET(IgnoredClientsWithInput, connection);
  940.         else
  941.         BITCLEAR(IgnoredClientsWithInput, connection);
  942.     BITCLEAR(SavedClientsWithInput, connection);
  943.     BITCLEAR(SavedAllSockets, connection);
  944.     BITCLEAR(SavedAllClients, connection);
  945.     }
  946.     isItTimeToYield = TRUE;
  947. }
  948.  
  949. /****************
  950.  * AttendClient
  951.  *    Adds one client back into the input masks.
  952.  ****************/
  953.  
  954. AttendClient (client)
  955.     ClientPtr    client;
  956. {
  957.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  958.     int connection = oc->fd;
  959.  
  960.     if (!GrabInProgress || GrabInProgress == client->index)
  961.     {
  962.         BITSET(AllClients, connection);
  963.         BITSET(AllSockets, connection);
  964.     BITSET(LastSelectMask, connection);
  965.         if (GETBIT (IgnoredClientsWithInput, connection))
  966.         BITSET(ClientsWithInput, connection);
  967.     }
  968.     else
  969.     {
  970.     BITSET(SavedAllClients, connection);
  971.     BITSET(SavedAllSockets, connection);
  972.     if (GETBIT(IgnoredClientsWithInput, connection))
  973.         BITSET(SavedClientsWithInput, connection);
  974.     }
  975. }
  976.  
  977. #ifdef AIXV3
  978.  
  979. static int grabbingClient;
  980. static int reallyGrabbed;
  981.  
  982. /****************
  983. * DontListenToAnybody:
  984. *   Don't listen to requests from any clients. Continue to handle new
  985. *   connections, but don't take any protocol requests from anybody.
  986. *   We have to take care if there is already a grab in progress, though.
  987. *   Undone by PayAttentionToClientsAgain. We also have to be careful
  988. *   not to accept any more input from the currently dispatched client.
  989. *   we do this be telling dispatch it is time to yield.
  990.  
  991. *   We call this when the server loses access to the glass
  992. *   (user hot-keys away).  This looks like a grab by the 
  993. *   server itself, but gets a little tricky if there is already
  994. *   a grab in progress.
  995. ******************/
  996.  
  997. void
  998. DontListenToAnybody()
  999. {
  1000.     if (!GrabInProgress) {
  1001.     COPYBITS(ClientsWithInput, SavedClientsWithInput);
  1002.     COPYBITS(AllSockets, SavedAllSockets);
  1003.     COPYBITS(AllClients, SavedAllClients);
  1004.     GrabInProgress = TRUE;
  1005.     reallyGrabbed = FALSE;
  1006.     } else {
  1007.     grabbingClient = ((OsCommPtr)clients[GrabInProgress]->osPrivate)->fd;
  1008.     reallyGrabbed = TRUE;
  1009.     }
  1010.     CLEARBITS(ClientsWithInput);
  1011.     UNSETBITS(AllSockets, AllClients);
  1012.     CLEARBITS(AllClients);
  1013.     isItTimeToYield = TRUE;
  1014. }
  1015.  
  1016. void
  1017. PayAttentionToClientsAgain()
  1018. {
  1019.     if (reallyGrabbed) {
  1020.     BITSET(AllSockets, grabbingClient);
  1021.     BITSET(AllClients, grabbingClient);
  1022.     } else {
  1023.     ListenToAllClients();
  1024.     }
  1025.     reallyGrabbed = FALSE;
  1026. }
  1027.  
  1028. #endif
  1029.