?? ckupty.c
字號:
/* C K U P T Y -- C-Kermit pseudoterminal control functions for UNIX *//* Copyright 1995 by the Massachusetts Institute of Technology. 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 of M.I.T. not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. Furthermore if you modify this software you must label your software as modified software and not distribute it in such a fashion that it might be confused with the original M.I.T. software. M.I.T. makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. Modified for use in C-Kermit, and new material added, by: Jeffrey Altman <jaltman@secure-endpoints.com> Secure Endpoints Inc., New York City November 1999*//* Built and tested successully on: . 4.4BSD, including BSDI/OS, NetBSD, FreeBSD, OpenBSD, Mac OS X . AIX 4.1 and later . DG/UX 5.4R4.11 . Digital UNIX 3.2 and 4.0 . HP-UX 9.00 and later . IRIX 6.0 and later . Linux . NeXTSTEP 3.x . QNX 4.25 (except PTY process termination not detected) . SCO OSR5.0.5 . SCO Unixware 7 . SINIX 5.42 . Solaris 2.x and 7 . SunOS 4.1.3 Included but not tested yet in: . Macintosh OSX, OpenBSD, and any other BSD44-based system not listed above Failures include: . SCO UNIX 3.2v4.2 (compile fails with syntax error in <memory.h>) . HP-UX 8.00 and earlier (no vhangup or ptsname routines)*/#include "ckcsym.h"#include "ckcdeb.h" /* To pick up NETPTY definition */#ifndef NETPTY /* Selector for PTY support */char * ptyver = "No PTY support";#else /* (rest of this module...) */char * ptyver = "PTY support 8.0.014, 20 Aug 2002";/* These will no doubt need adjustment... */#ifndef NEXT#define HAVE_SETSID#endif /* NEXT */#define HAVE_KILLPG#define HAVE_TTYNAME#define HAVE_WAITPID#ifndef USE_TERMIO#ifdef LINUX#define USE_TERMIO#else#ifdef ATTSV#define USE_TERMIO#else#ifdef HPUX#define USE_TERMIO#else#ifdef AIX#define USE_TERMIO#else#ifdef BSD44ORPOSIX#define USE_TERMIO#else#ifdef IRIX60#define USE_TERMIO#else#ifdef QNX#define USE_TERMIO#endif /* QNX */#endif /* IRIX60 */#endif /* BSD44ORPOSIX */#endif /* AIX */#endif /* HPUX */#endif /* ATTSV */#endif /* LINUX */#endif /* USE_TERMIO */#ifdef QNX#include <fcntl.h>#endif /* QNX */#ifdef USE_TERMIO#define POSIX_TERMIOS /* Seems to be a misnomer */#endif /* USE_TERMIO */#ifdef NEXT#ifndef GETPGRP_ONEARG#define GETPGRP_ONEARG#endif /* GETPGRP_ONEARG */#endif /* NEXT */#ifdef WANT_UTMP /* See ckupty.h *//* WANT_UTMP is not defined because (a) the utmp/wtmp junk is the most nonportable part of this module, and (b) we're not logging anybody in, we're just running a process, and don't need to write utmp/wtmp records.*/#ifndef HAVE_SETUTXENT /* Who has <utmpx.h> */#ifdef SOLARIS#define HAVE_SETUTXENT#else#ifdef IRIX60#define HAVE_SETUTXENT#else#ifdef CK_SCOV5#define HAVE_SETUTXENT#else#ifdef HPUX10#define HAVE_SETUTXENT#else#ifdef UNIXWARE#define HAVE_SETUTXENT#else#ifdef IRIX60#define HAVE_SETUTXENT#endif /* IRIX60 */#endif /* UNIXWARE */#endif /* HPUX10 */#endif /* CK_SCOV5 */#endif /* IRIX60 */#endif /* SOLARIS */#endif /* HAVE_SETUTXENT */#ifndef HAVE_UTHOST /* Does utmp include ut_host[]? */#ifdef HAVE_SETUTXENT /* utmpx always does */#define HAVE_UTHOST#else#ifdef LINUX /* Linux does */#define HAVE_UTHOST#else#ifdef SUNOS4 /* SunOS does */#define HAVE_UTHOST#else#ifdef AIX41 /* AIX 4.1 and later do */#define HAVE_UTHOST#endif /* AIX41 */#endif /* SUNOS4 */#endif /* LINUX */#endif /* HAVE_SETUTXENT */#endif /* HAVE_UTHOST */#ifndef HAVE_UT_HOST#ifndef NO_UT_HOST#define NO_UT_HOST#endif /* NO_UT_HOST */#endif /* HAVE_UT_HOST */#endif /* WANT_UTMP */#ifdef LINUX#define CK_VHANGUP#define HAVE_SYS_SELECT_H#define HAVE_GETUTENT#define HAVE_SETUTENT#define HAVE_UPDWTMP#endif /* LINUX */#ifdef HPUX10#define CK_VHANGUP#define VHANG_FIRST#define HAVE_PTSNAME#ifndef HAVE_PTYTRAP#define HAVE_PTYTRAP#endif /* HAVE_PTYTRAP */#else#ifdef HPUX9#define CK_VHANGUP#define VHANG_FIRST#define HAVE_PTSNAME#ifndef HAVE_PTYTRAP#define HAVE_PTYTRAP#endif /* HAVE_PTYTRAP */#endif /* HPUX9 */#endif /* HPUX10 */#ifdef SUNOS4#define CK_VHANGUP#define NO_UT_PID#define VHANG_FIRST#endif /* SUNOS4 */#ifdef IRIX60#define CK_VHANGUP#define HAVE__GETPTY#endif /* IRIX60 */#ifdef SINIX#define HAVE_STREAMS#define HAVE_GRANTPT#define HAVE_PTSNAME#define PUSH_PTEM#define PUSH_LDTERM#define PUSH_TTCOMPAT#endif /* SINIX */#ifdef ultrix#define MUST_SETPGRP#endif /* ultrix */#ifdef QNX#define MUST_SETPGRP#define NO_DEVTTY#define INIT_SPTY#endif /* QNX */#ifdef LINUX#ifdef HAVE_PTMX#define HAVE_GRANTPT#define HAVE_PTSNAME#endif /* HAVE_PTMX */#else#ifdef HAVE_STREAMS#define HAVE_PTMX#endif /* HAVE_STREAMS */#endif /* LINUX */#include "ckupty.h"#ifdef PTYNOBLOCK#ifndef O_NDELAY#ifdef O_NONBLOCK#define O_NDELAY O_NONBLOCK#endif /* O_NONBLOCK */#endif /* O_NDELAY */#else /* PTYNOBLOCK */#ifdef O_NDELAY#undef O_NDELAY#endif /* O_NDELAY */#define O_NDELAY 0#endif /* PTYNOBLOCK */#ifndef ONLCR#define ONLCR 0#endif /* ONLCR */#ifdef CK_WAIT_H#include <sys/wait.h>#endif /* CK_WAIT_H */#ifdef STREAMSPTY#ifndef INIT_SPTY#define INIT_SPTY#endif /* INIT_SPTY */#include <sys/stream.h>#include <stropts.h>#include <termio.h>/* Make sure we don't get the BSD version */#ifdef HAVE_SYS_TTY_H#include "/usr/include/sys/tty.h"#endif /* HAVE_SYS_TTY_H */#ifdef HAS_PTYVAR /* Where is this set? */#include <sys/ptyvar.h>#else /* HAS_PTYVAR */#ifndef TIOCPKT_FLUSHWRITE#define TIOCPKT_FLUSHWRITE 0x02#define TIOCPKT_NOSTOP 0x10#define TIOCPKT_DOSTOP 0x20#define TIOCPKT_IOCTL 0x40#endif /* TIOCPKT_FLUSHWRITE */#endif /* HAS_PTYVAR */#ifdef HAVE_TTY_H#include <tty.h>#endif /* HAVE_TTY_H *//* Because of the way ptyibuf is used with streams messages, we need ptyibuf+1 to be on a full-word boundary. The following weirdness is simply to make that happen.*/long ptyibufbuf[BUFSIZ/sizeof(long)+1];char *ptyibuf = ((char *)&ptyibufbuf[1])-1;char *ptyip = ((char *)&ptyibufbuf[1])-1;char ptyibuf2[BUFSIZ];unsigned char ctlbuf[BUFSIZ];struct strbuf strbufc, strbufd;int readstream();#else /* ! STREAMSPTY *//* I/O data buffers, pointers, and counters. */char ptyibuf[BUFSIZ], *ptyip = ptyibuf;char ptyibuf2[BUFSIZ];#endif /* ! STREAMSPTY */#ifndef USE_TERMIOstruct termbuf { struct sgttyb sg; struct tchars tc; struct ltchars ltc; int state; int lflags;} termbuf, termbuf2;#define cfsetospeed(tp,val) (tp)->sg.sg_ospeed = (val)#define cfsetispeed(tp,val) (tp)->sg.sg_ispeed = (val)#define cfgetospeed(tp) (tp)->sg.sg_ospeed#define cfgetispeed(tp) (tp)->sg.sg_ispeed#else /* USE_TERMIO */#ifdef SYSV_TERMIO#define termios termio#endif /* SYSV_TERMIO */#ifndef TCSANOW#ifdef TCSETS#define TCSANOW TCSETS#define TCSADRAIN TCSETSW#define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)#else /* TCSETS */#ifdef TCSETA#define TCSANOW TCSETA#define TCSADRAIN TCSETAW#define tcgetattr(f,t) ioctl(f,TCGETA,(char *)t)#else /* TCSETA */#define TCSANOW TIOCSETA#define TCSADRAIN TIOCSETAW#define tcgetattr(f,t) ioctl(f,TIOCGETA,(char *)t)#endif /* TCSETA */#endif /* TCSETS */#define tcsetattr(f,a,t) ioctl(f,a,t)#define cfsetospeed(tp,val) (tp)->c_cflag &= ~CBAUD;(tp)->c_cflag|=(val)#define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)#ifdef CIBAUD#define cfsetispeed(tp,val) \ (tp)->c_cflag &= ~CIBAUD; (tp)->c_cflag |= ((val)<<IBSHIFT)#define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)#else /* CIBAUD */#define cfsetispeed(tp,val) (tp)->c_cflag &= ~CBAUD; (tp)->c_cflag|=(val)#define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)#endif /* CIBAUD */#endif /* TCSANOW */struct termios termbuf, termbuf2; /* pty control structure */#ifdef INIT_SPTYstatic int spty = -1;#endif /* INIT_SPTY */#endif /* USE_TERMIO */extern int ttyfd; /* Standard Kermit usage */static int msg = 0;/* termbuf routines (begin) *//* init_termbuf() copy_termbuf(cp) set_termbuf() These three routines are used to get and set the "termbuf" structure to and from the kernel. init_termbuf() gets the current settings. copy_termbuf() hands in a new "termbuf" to write to the kernel, and set_termbuf() writes the structure into the kernel.*/VOIDinit_termbuf() { int rc = 0; memset(&termbuf,0,sizeof(termbuf)); memset(&termbuf2,0,sizeof(termbuf2));#ifndef USE_TERMIO rc = ioctl(ttyfd, TIOCGETP, (char *)&termbuf.sg); rc |= ioctl(ttyfd, TIOCGETC, (char *)&termbuf.tc); rc |= ioctl(ttyfd, TIOCGLTC, (char *)&termbuf.ltc);#ifdef TIOCGSTATE rc |= ioctl(ttyfd, TIOCGSTATE, (char *)&termbuf.state);#endif /* TIOCGSTATE */#else /* USE_TERMIO */ errno = 0;#ifdef INIT_SPTY rc = tcgetattr(spty, &termbuf); debug(F111,"init_termbuf() tcgetattr(spty)",ckitoa(rc),errno);#else rc = tcgetattr(ttyfd, &termbuf); debug(F111,"init_termbuf() tcgetattr(ttyfd)",ckitoa(rc),errno);#endif /* INIT_SPTY */#endif /* USE_TERMIO */ if (!rc) termbuf2 = termbuf;}#ifdef TIOCPKT_IOCTLVOIDcopy_termbuf(cp, len) char *cp; int len; { if (len > sizeof(termbuf)) len = sizeof(termbuf); memcpy((char *)&termbuf, cp, len); termbuf2 = termbuf;}#endif /* TIOCPKT_IOCTL */VOIDset_termbuf() { /* Only make the necessary changes. */#ifndef USE_TERMIO if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg, sizeof(termbuf.sg))) ioctl(ttyfd, TIOCSETN, (char *)&termbuf.sg); if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc, sizeof(termbuf.tc))) ioctl(ttyfd, TIOCSETC, (char *)&termbuf.tc); if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc, sizeof(termbuf.ltc))) ioctl(ttyfd, TIOCSLTC, (char *)&termbuf.ltc); if (termbuf.lflags != termbuf2.lflags) ioctl(ttyfd, TIOCLSET, (char *)&termbuf.lflags);#else /* USE_TERMIO */ if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf))) { int x; errno = 0;#ifdef INIT_SPTY x = tcsetattr(spty, TCSANOW, &termbuf); debug(F111,"set_termbuf tcsetattr(spty)",ckitoa(x),errno);#else x = tcsetattr(ttyfd, TCSANOW, &termbuf); debug(F111,"set_termbuf tcsetattr(ttyfd)",ckitoa(x),errno);#endif /* INIT_SPTY */ }#endif /* USE_TERMIO */}/* termbuf routines (end) */VOIDptyint_vhangup() {#ifdef CK_VHANGUP#ifdef CK_POSIX_SIG struct sigaction sa; /* Initialize "sa" structure. */ sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = SIG_IGN; sigaction(SIGHUP, &sa, (struct sigaction *)0); vhangup(); sa.sa_handler = SIG_DFL; sigaction(SIGHUP, &sa, (struct sigaction *)0);#else /* CK_POSIX_SIG */ signal(SIGHUP,SIG_IGN); vhangup(); signal(SIGHUP,SIG_DFL);#endif /* CK_POSIX_SIG */#endif /* CK_VHANGUP */}/* This routine is called twice. It's not particularly important that the setsid() or TIOCSTTY ioctls succeed (they may not the second time), but rather that we have a controlling terminal at the end. It is assumed that vhangup doesn't exist and confuse the process's notion of controlling terminal on any system without TIOCNOTTY. That is, either vhangup() leaves the controlling terminal in tact, breaks the association completely, or the system provides TIOCNOTTY to get things back into a reasonable state. In practice, vhangup() either breaks the association completely or doesn't effect controlling terminals, so this condition is met.*/longptyint_void_association() { int con_fd;#ifdef HAVE_SETSID debug(F110, "ptyint_void_association()", "setsid()", 0 ); setsid();#endif /* HAVE_SETSID */#ifndef NO_DEVTTY /* Void tty association first */#ifdef TIOCNOTTY con_fd = open("/dev/tty", O_RDWR); debug(F111, "ptyint_void_association() open(/dev/tty,O_RDWR)", "/dev/tty", con_fd); if (con_fd >= 0) { ioctl(con_fd, TIOCNOTTY, 0); close(con_fd); }#ifdef DEBUG else debug(F101, "ptyint_void_association() open() errno","",errno);#endif /* DEBUG */#endif /* TIOCNOTTY */#endif /* NO_DEVTTY */ return(0);}/* PID may be zero for unknown.*/longpty_cleanup(slave, pid, update_utmp) char *slave; int pid; int update_utmp; {#ifdef VHANG_LAST int retval, fd;#endif /* VHANG_LAST */ debug(F111,"pty_cleanup()",slave,pid);#ifdef WANT_UTMP if (update_utmp) pty_update_utmp(PTY_DEAD_PROCESS, 0, "", slave, (char *)0, PTY_UTMP_USERNAME_VALID );#endif /* WANT_UTMP */#ifdef SETUID chmod(slave, 0666); chown(slave, 0, 0);#endif /* SETUID */#ifdef HAVE_REVOKE revoke(slave); /* Revoke isn't guaranteed to send a SIGHUP to the processes it dissociates from the terminal. The best solution without a Posix mechanism for forcing a hangup is to killpg() the process group of the pty. This will at least kill the shell and hopefully, the child processes. This is not always the case, however. If the shell puts each job in a process group and doesn't pass along SIGHUP, all processes may not die. */ if (pid > 0) {#ifdef HAVE_KILLPG killpg(pid, SIGHUP);#else kill(-(pid), SIGHUP);#endif /*HAVE_KILLPG*/ }#else /* HAVE_REVOKE*/#ifdef VHANG_LAST { int status;#ifdef CK_POSIX_SIG sigset_t old, new; sigemptyset(&new); sigaddset(&new, SIGCHLD);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -