📄 connection.c
字号:
/* $TOG: connection.c /main/156 1997/06/05 18:43:01 sekhar $ *//***********************************************************Copyright (c) 1987, 1989 X ConsortiumPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEX CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER INAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of the X Consortium shall not beused in advertising or otherwise to promote the sale, use or other dealingsin this Software without prior written authorization from the X Consortium.Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts. All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of Digital not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************//* $XFree86: xc/programs/Xserver/os/connection.c,v 3.25.2.2 1997/07/05 15:55:45 dawes Exp $ *//***************************************************************** * Stuff to create connections --- OS dependent * * EstablishNewConnections, CreateWellKnownSockets, ResetWellKnownSockets, * CloseDownConnection, CheckConnections, AddEnabledDevice, * RemoveEnabledDevice, OnlyListToOneClient, * ListenToAllClients, * * (WaitForSomething is in its own file) * * In this implementation, a client socket table is not kept. * Instead, what would be the index into the table is just the * file descriptor of the socket. This won't work for if the * socket ids aren't small nums (0 - 2^8) * *****************************************************************/#ifdef WIN32#include <X11/Xwinsock.h>#endif#include "X.h"#include "Xproto.h"#include <X11/Xtrans.h>#include <errno.h>#ifdef X_NOT_STDC_ENVextern int errno;#endif#include <signal.h>#include <stdio.h>#ifndef WIN32#ifndef MINIX#ifndef Lynx#include <sys/socket.h>#else#include <socket.h>#endif#endif#ifdef hpux#include <sys/utsname.h>#include <sys/ioctl.h>#endif#ifdef AIXV3#include <sys/ioctl.h>#endif#ifdef MINIX#include <sys/nbio.h>#define select(n,r,w,x,t) nbio_select(n,r,w,x,t)#endif#ifdef __EMX__#define select(n,r,w,x,t) os2PseudoSelect(n,r,w,x,t)extern __const__ int _nfiles;#endif#if defined(TCPCONN) || defined(STREAMSCONN)# include <netinet/in.h>/*================================================================ BEGIN ORL VNC modification include arpa/inet.h for inet_ntoa function prototype */#include <arpa/inet.h>/* END ORL VNC modification================================================================*/# ifndef hpux# ifdef apollo# ifndef NO_TCP_H# include <netinet/tcp.h># endif# else# ifdef CSRG_BASED# include <sys/param.h># endif# ifndef __EMX__# include <netinet/tcp.h># endif# endif# endif#endif#ifdef AMTCPCONN#include <server/ip/types.h>#include <server/ip/gen/in.h>#include <server/ip/gen/inet.h>#endif#if !defined(AMOEBA) && !defined(_MINIX) && !defined(__EMX__)#ifndef Lynx#include <sys/uio.h>#else#include <uio.h>#endif#endif#endif /* WIN32 */#include "misc.h" /* for typedef of pointer */#include <X11/Xpoll.h>#include "osdep.h"#include "opaque.h"#include "dixstruct.h"#ifdef XAPPGROUP#include "extensions/Xagsrv.h"#endif#ifdef XCSECURITY#define _SECURITY_SERVER#include "extensions/security.h"#endif#ifdef LBX#include "lbxserve.h"#endif#ifdef X_NOT_POSIX#define Pid_t int#else#define Pid_t pid_t#endif#ifdef DNETCONN#include <netdnet/dn.h>#endif /* DNETCONN */extern char *display; /* The display number */int lastfdesc; /* maximum file descriptor */fd_set WellKnownConnections; /* Listener mask */fd_set EnabledDevices; /* mask for input devices that are on */fd_set AllSockets; /* select on this */fd_set AllClients; /* available clients */fd_set LastSelectMask; /* mask returned from last select call */fd_set ClientsWithInput; /* clients with FULL requests in buffer */fd_set ClientsWriteBlocked; /* clients who cannot receive output */fd_set OutputPending; /* clients with reply/event data ready to go */int MaxClients = MAXSOCKS;Bool NewOutputPending; /* not yet attempted to write some new output */Bool AnyClientsWriteBlocked; /* true if some client blocked on write */Bool RunFromSmartParent; /* send SIGUSR1 to parent process */Bool PartialNetwork; /* continue even if unable to bind all addrs */char *protNoListen; /* don't listen on this protocol */static Pid_t ParentProcess;static Bool debug_conns = FALSE;fd_set IgnoredClientsWithInput;static fd_set GrabImperviousClients;static fd_set SavedAllClients;static fd_set SavedAllSockets;static fd_set SavedClientsWithInput;int GrabInProgress = 0;#ifndef WIN32int ConnectionTranslation[MAXSOCKS];#else/* SPAM ALERT !!! * On NT fds are not between 0 and MAXSOCKS, they are unrelated, and there is * not even a known maximum value, so use something quite arbitrary for now. * This is clearly boggus and another form of storage which doesn't use the fd * as a direct index should really be implemented for NT. */#define MAXFD 500int ConnectionTranslation[MAXFD];#endifXtransConnInfo *ListenTransConns = NULL;int *ListenTransFds = NULL;int ListenTransCount;extern int auditTrailLevel;static void ErrorConnMax(#if NeedFunctionPrototypesXtransConnInfo /* trans_conn */#endif);#ifndef LBXstatic#endifvoid CloseDownFileDescriptor(#if NeedFunctionPrototypes#ifdef LBX ClientPtr client#else register OsCommPtr /*oc*/#endif#endif);#ifdef LBXextern int LbxFlushClient();extern void LbxCloseClient();#endif /* LBX */static XtransConnInfolookup_trans_conn (fd) int fd;{ if (ListenTransFds) { int i; for (i = 0; i < ListenTransCount; i++) if (ListenTransFds[i] == fd) return ListenTransConns[i]; } return (NULL);}#ifdef XDMCPvoid XdmcpOpenDisplay(), XdmcpInit(), XdmcpReset(), XdmcpCloseDisplay();#endif/***************** * CreateWellKnownSockets * At initialization, create the sockets to listen on for new clients. *****************/voidCreateWellKnownSockets(){ int request, i; int partial; char port[20]; FD_ZERO(&AllSockets); FD_ZERO(&AllClients); FD_ZERO(&LastSelectMask); FD_ZERO(&ClientsWithInput);#ifndef WIN32 for (i=0; i<MAXSOCKS; i++) ConnectionTranslation[i] = 0;#else for (i=0; i<MAXFD; i++) ConnectionTranslation[i] = 0;#endif#ifdef XNO_SYSCONF /* should only be on FreeBSD 1.x and NetBSD 0.x */#undef _SC_OPEN_MAX#endif#ifndef __EMX__#ifdef _SC_OPEN_MAX lastfdesc = sysconf(_SC_OPEN_MAX) - 1;#else#ifdef hpux /* || defined(__EMX__) ? */ lastfdesc = _NFILE - 1;#else lastfdesc = getdtablesize() - 1;#endif#endif#else lastfdesc = _nfiles - 1;#endif if (lastfdesc > MAXSOCKS) { lastfdesc = MAXSOCKS; if (debug_conns) ErrorF( "GOT TO END OF SOCKETS %d\n", MAXSOCKS); } FD_ZERO (&WellKnownConnections); sprintf (port, "%d", atoi (display)); if (protNoListen) if (_XSERVTransNoListen(protNoListen)) { FatalError ("Failed to disable listen for %s", protNoListen); } if ((_XSERVTransMakeAllCOTSServerListeners (port, &partial, &ListenTransCount, &ListenTransConns) >= 0) && (ListenTransCount >= 1)) { if (!PartialNetwork && partial) { FatalError ("Failed to establish all listening sockets"); } else { ListenTransFds = (int *) xalloc (ListenTransCount * sizeof (int)); for (i = 0; i < ListenTransCount; i++) { int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i]); ListenTransFds[i] = fd; FD_SET (fd, &WellKnownConnections); if (!_XSERVTransIsLocal (ListenTransConns[i])) { DefineSelf (fd); } } } } if (!XFD_ANYSET (&WellKnownConnections)) FatalError ("Cannot establish any listening sockets - Make sure an X server isn't already running");#ifndef WIN32 OsSignal (SIGPIPE, SIG_IGN); OsSignal (SIGHUP, AutoResetServer);#endif OsSignal (SIGINT, GiveUp); OsSignal (SIGTERM, GiveUp); XFD_COPYSET (&WellKnownConnections, &AllSockets); ResetHosts(display); /* * Magic: If SIGUSR1 was set to SIG_IGN when * the server started, assume that either * * a- The parent process is ignoring SIGUSR1 * * or * * b- The parent process is expecting a SIGUSR1 * when the server is ready to accept connections * * In the first case, the signal will be harmless, * in the second case, the signal will be quite * useful */#ifndef WIN32 if (OsSignal (SIGUSR1, SIG_IGN) == SIG_IGN) RunFromSmartParent = TRUE; ParentProcess = getppid (); if (RunFromSmartParent) { if (ParentProcess > 0) { kill (ParentProcess, SIGUSR1); } }#endif#ifdef XDMCP XdmcpInit ();#endif}voidResetWellKnownSockets (){ int i; ResetOsBuffers(); for (i = 0; i < ListenTransCount; i++) { int status = _XSERVTransResetListener (ListenTransConns[i]); if (status != TRANS_RESET_NOOP) { if (status == TRANS_RESET_FAILURE) { /* * ListenTransConns[i] freed by xtrans. * Remove it from out list. */ FD_CLR (ListenTransFds[i], &WellKnownConnections); ListenTransFds[i] = ListenTransFds[ListenTransCount - 1]; ListenTransConns[i] = ListenTransConns[ListenTransCount - 1]; ListenTransCount -= 1; i -= 1; } else if (status == TRANS_RESET_NEW_FD) { /* * A new file descriptor was allocated (the old one was closed) */ int newfd = _XSERVTransGetConnectionNumber (ListenTransConns[i]); FD_CLR (ListenTransFds[i], &WellKnownConnections); ListenTransFds[i] = newfd; FD_SET(newfd, &WellKnownConnections); } } } ResetAuthorization (); ResetHosts(display);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -