?? xtranslcl.c
字號:
/* $XConsortium: Xtranslcl.c /main/27 1996/09/28 16:50:14 rws $ *//* $XFree86: xc/lib/xtrans/Xtranslcl.c,v 3.21.2.3 1998/02/01 16:04:34 robin Exp $ *//*Copyright (c) 1993, 1994 X ConsortiumPermission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:The above copyright notice and this permission notice shall be includedin all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OROTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of the X Consortium shallnot be used in advertising or otherwise to promote the sale, use orother dealings in this Software without prior written authorizationfrom the X Consortium.*//* Copyright (c) 1993, 1994 NCR Corporation - Dayton, Ohio, USA * * All Rights Reserved * * Permission 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 that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name NCR not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. NCR makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * * NCR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN * NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY 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 THIS SOFTWARE. *//* * * The connection code/ideas in lib/X and server/os for SVR4/Intel * environments was contributed by the following companies/groups: * * MetroLink Inc * NCR * Pittsburgh Powercomputing Corporation (PPc)/Quarterdeck Office Systems * SGCS * Unix System Laboratories (USL) / Novell * XFree86 * * The goal is to have common connection code among all SVR4/Intel vendors. * * ALL THE ABOVE COMPANIES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, * IN NO EVENT SHALL THESE COMPANIES * BE LIABLE FOR ANY SPECIAL, INDIRECT * OR CONSEQUENTIAL DAMAGES OR ANY 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 THIS SOFTWARE. */#include <errno.h>#include <ctype.h>#include <sys/signal.h>#include <sys/ioctl.h>#include <sys/stat.h>#ifdef SVR4#include <sys/filio.h>#endif#include <sys/stropts.h>#include <sys/wait.h>/* * The local transports should be treated the same as a UNIX domain socket * wrt authentication, etc. Because of this, we will use struct sockaddr_un * for the address format. This will simplify the code in other places like * The X Server. */#include <sys/socket.h>#ifndef X_NO_SYS_UN#include <sys/un.h>#endif#if defined(ISC) && !defined(_POSIX_SOURCE)typedef unsigned short mode_t;/* POSIX needed for mode_t define in sys/types.h */#endif/* * These functions actually implement the local connection mechanisms. *//* Type Not Supported */static intTRANS(OpenFail)(ciptr, port)XtransConnInfo ciptr;char *port;{ return -1;}#ifdef TRANS_REOPENstatic intTRANS(ReopenFail)(ciptr, fd, port)XtransConnInfo ciptr;int fd;char *port;{ return 0;}#endif /* TRANS_REOPEN */static intTRANS(FillAddrInfo)(ciptr, sun_path, peer_sun_path)XtransConnInfo ciptr;char *sun_path;char *peer_sun_path;{ struct sockaddr_un *sunaddr; struct sockaddr_un *p_sunaddr; ciptr->family = AF_UNIX; ciptr->addrlen = sizeof (struct sockaddr_un); if ((sunaddr = (struct sockaddr_un *) xalloc (ciptr->addrlen)) == NULL) { PRMSG(1,"FillAddrInfo: failed to allocate memory for addr\n", 0,0,0); return 0; } sunaddr->sun_family = AF_UNIX; strcpy (sunaddr->sun_path, sun_path);#if defined(BSD44SOCKETS) && !defined(Lynx) sunaddr->sun_len = strlen (sunaddr->sun_path);#endif ciptr->addr = (char *) sunaddr; ciptr->peeraddrlen = sizeof (struct sockaddr_un); if ((p_sunaddr = (struct sockaddr_un *) xalloc ( ciptr->peeraddrlen)) == NULL) { PRMSG(1, "FillAddrInfo: failed to allocate memory for peer addr\n", 0,0,0); xfree ((char *) sunaddr); ciptr->addr = NULL; return 0; } p_sunaddr->sun_family = AF_UNIX; strcpy (p_sunaddr->sun_path, peer_sun_path);#if defined(BSD44SOCKETS) && !defined(Lynx) p_sunaddr->sun_len = strlen (p_sunaddr->sun_path);#endif ciptr->peeraddr = (char *) p_sunaddr; return 1;}/* PTS */#if defined(SYSV) && !defined(sco) && !defined(SCO) && !defined(ISC)#define SIGNAL_T int#else#define SIGNAL_T void#endif /* SYSV */typedef SIGNAL_T (*PFV)();extern PFV signal();extern char *ptsname(#if NeedFunctionPrototypes int#endif);static void _dummy(sig)int sig;{}#define X_STREAMS_DIR "/dev/X"#define DEV_PTMX "/dev/ptmx"#define DEV_SPX "/dev/spx"#if defined(X11_t)#define PTSNODENAME "/dev/X/server."#define NAMEDNODENAME "/dev/X/Nserver."/* * ISC and SCO are only defined for X11 since they are there for * backwards binary compatability only. */#define X_ISC_DIR "/dev/X/ISCCONN"#define ISCDEVNODENAME "/dev/X/ISCCONN/X%s"#define ISCTMPNODENAME "/tmp/.X11-unix/X%s"#define SCORNODENAME "/dev/X%1sR"#define SCOSNODENAME "/dev/X%1sS"#endif#if defined(XIM_t)#define PTSNODENAME "/dev/X/XIM."#define NAMEDNODENAME "/dev/X/NXIM."#endif#if defined(FS_t) || defined (FONT_t)/* * USL has already defined something here. We need to check with them * and see if their choice is usable here. */#define PTSNODENAME "/dev/X/fontserver."#define NAMEDNODENAME "/dev/X/Nfontserver."#endif#if defined(ICE_t)#define PTSNODENAME "/dev/X/ICE."#define NAMEDNODENAME "/dev/X/NICE."#endif#if defined(TEST_t)#define PTSNODENAME "/dev/X/transtest."#define NAMEDNODENAME "/dev/X/Ntranstest."#endif#ifdef TRANS_CLIENTstatic intTRANS(PTSOpenClient)(ciptr, port)XtransConnInfo ciptr;char *port;{ int fd,server,exitval,alarm_time,ret; char server_path[64]; char *slave, namelen; char buf[20]; /* MAX_PATH_LEN?? */ PFV savef; PRMSG(2,"PTSOpenClient(%s)\n", port, 0,0 );#if !defined(PTSNODENAME) PRMSG(1,"PTSOpenClient: Protocol is not supported by a pts connection\n", 0,0,0); return -1;#else if (port && *port ) { if( *port == '/' ) { /* A full pathname */ (void) sprintf(server_path, "%s", port); } else { (void) sprintf(server_path, "%s%s", PTSNODENAME, port); } } else { (void) sprintf(server_path, "%s%d", PTSNODENAME, getpid()); } /* * Open the node the on which the server is listening. */ if ((server = open (server_path, O_RDWR)) < 0) { PRMSG(1,"PTSOpenClient: failed to open %s\n", server_path, 0,0); return -1; } /* * Open the streams based pipe that will be this connection. */ if ((fd = open("/dev/ptmx", O_RDWR)) < 0) { PRMSG(1,"PTSOpenClient: failed to open /dev/ptmx\n", 0,0,0); return -1; } (void) grantpt(fd); (void) unlockpt(fd); slave = ptsname(fd); /* get name */ if( slave == NULL ) { PRMSG(1,"PTSOpenClient: failed to get ptsname()\n", 0,0,0); close(fd); close(server); return -1; } /* * This is neccesary for the case where a program is setuid to non-root. * grantpt() calls /usr/lib/pt_chmod which is set-uid root. This program will * set the owner of the pt device incorrectly if the uid is not restored * before it is called. The problem is that once it gets restored, it * cannot be changed back to its original condition, hence the fork(). */ if( !fork()) { uid_t saved_euid; saved_euid = geteuid(); setuid( getuid() ); /** sets the euid to the actual/real uid **/ if( chown( slave, saved_euid, -1 ) < 0 ) { exit( 1 ); } exit( 0 ); } wait( &exitval ); if (chmod(slave, 0666) < 0) { close(fd); close(server); PRMSG(1,"PTSOpenClient: Cannot chmod %s\n", slave, 0,0); return(-1); } /* * write slave name to server */ namelen = strlen(slave); buf[0] = namelen; (void) sprintf(&buf[1], slave); (void) write(server, buf, namelen+1); (void) close(server); /* * wait for server to respond */ savef = signal(SIGALRM, _dummy); alarm_time = alarm (30); /* CONNECT_TIMEOUT */ ret = read(fd, buf, 1); (void) alarm(alarm_time); (void) signal(SIGALRM, savef); if (ret != 1) { PRMSG(1, "PTSOpenClient: failed to get acknoledgement from server\n", 0,0,0); (void) close(fd); fd = -1; } /* * Everything looks good: fill in the XtransConnInfo structure. */ if (TRANS(FillAddrInfo) (ciptr, slave, server_path) == 0) { PRMSG(1,"PTSOpenClient: failed to fill in addr info\n", 0,0,0); close(fd); return -1; } return(fd);#endif /* !PTSNODENAME */}#endif /* TRANS_CLIENT */#ifdef TRANS_SERVERstatic intTRANS(PTSOpenServer)(ciptr, port)XtransConnInfo ciptr;char *port;{ int fd, server; char server_path[64], *slave; PRMSG(2,"PTSOpenServer(%s)\n", port, 0,0 );#if !defined(PTSNODENAME) PRMSG(1,"PTSOpenServer: Protocol is not supported by a pts connection\n", 0,0,0); return -1;#else if (port && *port ) { if( *port == '/' ) { /* A full pathname */ (void) sprintf(server_path, "%s", port); } else { (void) sprintf(server_path, "%s%s", PTSNODENAME, port); } } else { (void) sprintf(server_path, "%s%d", PTSNODENAME, getpid()); } mkdir(X_STREAMS_DIR, 0777); chmod(X_STREAMS_DIR, 0777); if( (fd=open(server_path, O_RDWR)) >= 0 ) {#if 0 /* * This doesn't prevent the server from starting up, and doesn't * prevent clients from trying to connect to the in-use PTS (which * is often in use by something other than another server). */ PRMSG(1, "PTSOpenServer: A server is already running on port %s\n", port, 0,0 ); PRMSG(1, "PTSOpenServer: Remove %s if this is incorrect.\n", server_path, 0,0 ); close(fd); return(-1);#else /* Just remove the old path (which is what happens with UNIXCONN) */ ;#endif } unlink(server_path); if( (fd=open(DEV_PTMX, O_RDWR)) < 0) { PRMSG(1, "PTSOpenServer: Unable to open %s\n", DEV_PTMX, 0,0 ); return(-1); } grantpt(fd); unlockpt(fd); if( (slave=ptsname(fd)) == NULL) { PRMSG(1, "PTSOpenServer: Unable to get slave device name\n", 0,0,0 ); close(fd); return(-1); } if( link(slave,server_path) < 0 ) { PRMSG(1, "PTSOpenServer: Unable to link %s to %s\n", slave, server_path,0 ); close(fd); return(-1); } if( chmod(server_path, 0666) < 0 ) { PRMSG(1, "PTSOpenServer: Unable to chmod %s to 0666\n", server_path,0,0 ); close(fd); return(-1); } if( (server=open(server_path, O_RDWR)) < 0 ) { PRMSG(1, "PTSOpenServer: Unable to open server device %s\n", server_path,0,0 ); close(fd); return(-1); } close(server); /* * Everything looks good: fill in the XtransConnInfo structure. */ if (TRANS(FillAddrInfo) (ciptr, server_path, server_path) == 0) { PRMSG(1,"PTSOpenServer: failed to fill in addr info\n", 0,0,0); close(fd); return -1; } return fd;#endif /* !PTSNODENAME */}static intTRANS(PTSAccept)(ciptr, newciptr, status)XtransConnInfo ciptr;XtransConnInfo newciptr;int *status;{ int newfd; int in; unsigned char length; char buf[256]; struct sockaddr_un *sunaddr; PRMSG(2,"PTSAccept(%x->%d)\n",ciptr,ciptr->fd,0); if( (in=read(ciptr->fd,&length,1)) <= 0 ){ if( !in ) { PRMSG(2, "PTSAccept: Incoming connection closed\n",0,0,0); } else { PRMSG(1, "PTSAccept: Error reading incoming connection. errno=%d \n", errno,0,0); } *status = TRANS_ACCEPT_MISC_ERROR; return -1; } if( (in=read(ciptr->fd,buf,length)) <= 0 ){ if( !in ) { PRMSG(2, "PTSAccept: Incoming connection closed\n",0,0,0); } else { PRMSG(1,"PTSAccept: Error reading device name for new connection. errno=%d \n", errno,0,0); } *status = TRANS_ACCEPT_MISC_ERROR; return -1; } buf[length] = '\0'; if( (newfd=open(buf,O_RDWR)) < 0 ) { PRMSG(1, "PTSAccept: Failed to open %s\n",buf,0,0); *status = TRANS_ACCEPT_MISC_ERROR; return -1; } write(newfd,"1",1); /* * Everything looks good: fill in the XtransConnInfo structure. */ newciptr->addrlen=ciptr->addrlen; if( (newciptr->addr=(char *)xalloc(newciptr->addrlen)) == NULL ) { PRMSG(1,"PTSAccept: failed to allocate memory for peer addr\n", 0,0,0); close(newfd); *status = TRANS_ACCEPT_BAD_MALLOC; return -1; } memcpy(newciptr->addr,ciptr->addr,newciptr->addrlen); newciptr->peeraddrlen=sizeof(struct sockaddr_un); if( (sunaddr=(struct sockaddr_un *)xalloc(newciptr->peeraddrlen)) == NULL ) { PRMSG(1,"PTSAccept: failed to allocate memory for peer addr\n", 0,0,0); xfree(newciptr->addr); close(newfd); *status = TRANS_ACCEPT_BAD_MALLOC; return -1; } sunaddr->sun_family=AF_UNIX; strcpy(sunaddr->sun_path,buf);#if defined(BSD44SOCKETS) && !defined(Lynx) sunaddr->sun_len=strlen(sunaddr->sun_path);#endif newciptr->peeraddr=(char *)sunaddr; *status = 0; return newfd;}#endif /* TRANS_SERVER */#ifdef SVR4/* NAMED */#ifdef TRANS_CLIENTstatic intTRANS(NAMEDOpenClient)(ciptr, port)XtransConnInfo ciptr;char *port;{ int fd; char server_path[64]; struct stat filestat; extern int isastream(); PRMSG(2,"NAMEDOpenClient(%s)\n", port, 0,0 );#if !defined(NAMEDNODENAME) PRMSG(1,"NAMEDOpenClient: Protocol is not supported by a NAMED connection\n", 0,0,0); return -1;#else if ( port && *port ) { if( *port == '/' ) { /* A full pathname */ (void) sprintf(server_path, "%s", port); } else { (void) sprintf(server_path, "%s%s", NAMEDNODENAME, port); } } else { (void) sprintf(server_path, "%s%d", NAMEDNODENAME, getpid()); } if (stat(server_path, &filestat) < 0 ) { PRMSG(1,"NAMEDOpenClient: No device %s for NAMED connection\n", server_path, 0,0 ); return -1; } if ((filestat.st_mode & S_IFMT) != S_IFIFO) { PRMSG(1,"NAMEDOpenClient: Device %s is not a FIFO\n", server_path, 0,0 ); /* Is this really a failure? */ return -1; } if ((fd = open(server_path, O_RDWR)) < 0) { PRMSG(1,"NAMEDOpenClient: Cannot open %s for NAMED connection\n", server_path, 0,0 ); return -1; } if (isastream(fd) <= 0) { PRMSG(1,"NAMEDOpenClient: %s is not a streams device\n", server_path, 0,0 ); (void) close(fd); return -1; } /* * Everything looks good: fill in the XtransConnInfo structure. */ if (TRANS(FillAddrInfo) (ciptr, server_path, server_path) == 0) { PRMSG(1,"NAMEDOpenClient: failed to fill in addr info\n", 0,0,0);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -