home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / test / tar / x30.tar.gz / x30.tar / ckupty.c < prev    next >
C/C++ Source or Header  |  2010-02-09  |  50KB  |  1,973 lines

  1. char *ckptyv = "Pseudoterminal support, 9.0.100, 16 Oct 2009";
  2.  
  3. /*  C K U P T Y  --  C-Kermit pseudoterminal control functions for UNIX  */
  4.  
  5. /* Last update: Tue Feb  9 12:28:29 2010 */
  6.  
  7. /*
  8.   Copyright 1995 by the Massachusetts Institute of Technology.
  9.  
  10.   Permission to use, copy, modify, and distribute this software and its
  11.   documentation for any purpose and without fee is hereby granted, provided
  12.   that the above copyright notice appear in all copies and that both that
  13.   copyright notice and this permission notice appear in supporting
  14.   documentation, and that the name of M.I.T. not be used in advertising or
  15.   publicity pertaining to distribution of the software without specific,
  16.   written prior permission.  Furthermore if you modify this software you must
  17.   label your software as modified software and not distribute it in such a
  18.   fashion that it might be confused with the original M.I.T. software.
  19.   M.I.T. makes no representations about the suitability of this software for
  20.   any purpose.  It is provided "as is" without express or implied warranty.
  21.  
  22.   Modified for use in C-Kermit, and new material added, by:
  23.  
  24.   Jeffrey Altman <jaltman@secure-endpoints.com>
  25.   Secure Endpoints Inc., New York City
  26.   November 1999
  27.  
  28.   Parameterized for pty file descriptor and function code,
  29.   Frank da Cruz, Columbia University, New York City
  30.   Dec 2006 - Sep 2009
  31. */
  32.  
  33. /*
  34.   Built and tested successully on:
  35.    . 4.4BSD, including BSDI/OS, NetBSD, FreeBSD, OpenBSD, Mac OS X
  36.    . AIX 4.1 and later
  37.    . DG/UX 5.4R4.11
  38.    . Digital UNIX 3.2 and 4.0
  39.    . HP-UX 9.00 and later
  40.    . IRIX 6.0 and later
  41.    . Linux
  42.    . Mac OS X 10.4
  43.    . NeXTSTEP 3.x
  44.    . OpenBSD
  45.    . QNX 4.25 (except PTY process termination not detected)
  46.    . SCO OSR5.0.5
  47.    . SCO Unixware 7
  48.    . SINIX 5.42
  49.    . Solaris 2.x and 7
  50.    . SunOS 4.1.3
  51.  
  52.   Failures include:
  53.    . SCO UNIX 3.2v4.2 (compile fails with syntax error in <memory.h>)
  54.    . HP-UX 8.00 and earlier (no vhangup or ptsname routines)
  55. */
  56.  
  57. #include "ckcsym.h"
  58. #include "ckcdeb.h"            /* To pick up NETPTY definition */
  59.  
  60. #ifndef NETPTY                /* Selector for PTY support */
  61.  
  62. char * ptyver = "No PTY support";
  63.  
  64. #else  /* (rest of this module...) */
  65.  
  66. char * ptyver = "PTY support 8.0.016, 22 Aug 2007";
  67.  
  68. /* These will no doubt need adjustment... */
  69.  
  70. #ifndef NEXT
  71. #define HAVE_SETSID
  72. #endif /* NEXT */
  73. #define HAVE_KILLPG
  74. #define HAVE_TTYNAME
  75. #define HAVE_WAITPID
  76.  
  77. #ifdef SUNOS41
  78. #define BSD44ORPOSIX
  79. #endif    /* SUNOS41 */
  80.  
  81. #ifndef USE_TERMIO
  82. #ifdef LINUX
  83. #define USE_TERMIO
  84. #else
  85. #ifdef ATTSV
  86. #define USE_TERMIO
  87. #else
  88. #ifdef HPUX
  89. #define USE_TERMIO
  90. #else
  91. #ifdef AIX
  92. #define USE_TERMIO
  93. #else
  94. #ifdef BSD44ORPOSIX
  95. #define USE_TERMIO
  96. #else
  97. #ifdef IRIX60
  98. #define USE_TERMIO
  99. #else
  100. #ifdef QNX
  101. #define USE_TERMIO
  102. #endif /* QNX */
  103. #endif /* IRIX60 */
  104. #endif /* BSD44ORPOSIX */
  105. #endif /* AIX */
  106. #endif /* HPUX */
  107. #endif /* ATTSV */
  108. #endif /* LINUX */
  109. #endif /* USE_TERMIO */
  110.  
  111. #ifdef QNX
  112. #include <fcntl.h>
  113. #endif /* QNX */
  114.  
  115. #ifdef USE_TERMIO
  116. #define POSIX_TERMIOS            /* Seems to be a misnomer */
  117. #endif /* USE_TERMIO */
  118.  
  119. #ifdef NEXT
  120. #ifndef GETPGRP_ONEARG
  121. #define GETPGRP_ONEARG
  122. #endif /* GETPGRP_ONEARG */
  123. #endif /* NEXT */
  124.  
  125. #ifdef WANT_UTMP            /* See ckupty.h */
  126. /*
  127.   WANT_UTMP is not defined because (a) the utmp/wtmp junk is the most
  128.   nonportable part of this module, and (b) we're not logging anybody
  129.   in, we're just running a process, and don't need to write utmp/wtmp records.
  130. */
  131. #ifndef HAVE_SETUTXENT            /* Who has <utmpx.h> */
  132. #ifdef SOLARIS
  133. #define HAVE_SETUTXENT
  134. #else
  135. #ifdef IRIX60
  136. #define HAVE_SETUTXENT
  137. #else
  138. #ifdef CK_SCOV5
  139. #define HAVE_SETUTXENT
  140. #else
  141. #ifdef HPUX10
  142. #define HAVE_SETUTXENT
  143. #else
  144. #ifdef UNIXWARE
  145. #define HAVE_SETUTXENT
  146. #else
  147. #ifdef IRIX60
  148. #define HAVE_SETUTXENT
  149. #endif /* IRIX60 */
  150. #endif /* UNIXWARE */
  151. #endif /* HPUX10 */
  152. #endif /* CK_SCOV5 */
  153. #endif /* IRIX60 */
  154. #endif /* SOLARIS */
  155. #endif /* HAVE_SETUTXENT */
  156.  
  157. #ifndef HAVE_UTHOST            /* Does utmp include ut_host[]? */
  158. #ifdef HAVE_SETUTXENT            /* utmpx always does */
  159. #define HAVE_UTHOST
  160. #else
  161. #ifdef LINUX                /* Linux does */
  162. #define HAVE_UTHOST
  163. #else
  164. #ifdef SUNOS4                /* SunOS does */
  165. #define HAVE_UTHOST
  166. #else
  167. #ifdef AIX41                /* AIX 4.1 and later do */
  168. #define HAVE_UTHOST
  169. #endif /* AIX41 */
  170. #endif /* SUNOS4 */
  171. #endif /* LINUX */
  172. #endif /* HAVE_SETUTXENT */
  173. #endif /* HAVE_UTHOST */
  174.  
  175. #ifndef HAVE_UT_HOST
  176. #ifndef NO_UT_HOST
  177. #define NO_UT_HOST
  178. #endif /* NO_UT_HOST */
  179. #endif /* HAVE_UT_HOST */
  180.  
  181. #endif /* WANT_UTMP */
  182.  
  183. #ifdef LINUX
  184. #define CK_VHANGUP
  185. #define HAVE_SYS_SELECT_H
  186. #define HAVE_GETUTENT
  187. #define HAVE_SETUTENT
  188. #define HAVE_UPDWTMP
  189. #endif /* LINUX */
  190.  
  191. #ifdef HPUX10
  192. #define CK_VHANGUP
  193. #define VHANG_FIRST
  194. #define HAVE_PTSNAME
  195. #ifndef HAVE_PTYTRAP
  196. #define HAVE_PTYTRAP
  197. #endif /* HAVE_PTYTRAP */
  198. #else
  199. #ifdef HPUX9
  200. #define CK_VHANGUP
  201. #define VHANG_FIRST
  202. #define HAVE_PTSNAME
  203. #ifndef HAVE_PTYTRAP
  204. #define HAVE_PTYTRAP
  205. #endif /* HAVE_PTYTRAP */
  206. #endif /* HPUX9 */
  207. #endif /* HPUX10 */
  208.  
  209. #ifdef SUNOS4
  210. #define CK_VHANGUP
  211. #define NO_UT_PID
  212. #define VHANG_FIRST
  213. #endif /* SUNOS4 */
  214.  
  215. #ifdef IRIX60
  216. #define CK_VHANGUP
  217. #define HAVE__GETPTY
  218. #endif /* IRIX60 */
  219.  
  220. #ifdef SINIX
  221. #define HAVE_STREAMS
  222. #define HAVE_GRANTPT
  223. #define HAVE_PTSNAME
  224. #define PUSH_PTEM
  225. #define PUSH_LDTERM
  226. #define PUSH_TTCOMPAT
  227. #endif /* SINIX */
  228.  
  229. #ifdef ultrix
  230. #define MUST_SETPGRP
  231. #endif /* ultrix */
  232.  
  233. #ifdef QNX
  234. #define MUST_SETPGRP
  235. #define NO_DEVTTY
  236. #define INIT_SPTY
  237. #endif /* QNX */
  238.  
  239. #ifdef LINUX
  240. #ifdef HAVE_PTMX
  241. #define HAVE_GRANTPT
  242. #define HAVE_PTSNAME
  243. #endif /* HAVE_PTMX */
  244. #else
  245. #ifdef HAVE_STREAMS
  246. #define HAVE_PTMX
  247. #endif /* HAVE_STREAMS */
  248. #endif /* LINUX */
  249.  
  250. #include "ckupty.h"
  251.  
  252. #ifdef PTYNOBLOCK
  253. #ifndef O_NDELAY
  254. #ifdef O_NONBLOCK
  255. #define O_NDELAY O_NONBLOCK
  256. #endif /* O_NONBLOCK */
  257. #endif /* O_NDELAY */
  258. #else /* PTYNOBLOCK */
  259. #ifdef O_NDELAY
  260. #undef O_NDELAY
  261. #endif /* O_NDELAY */
  262. #define O_NDELAY 0
  263. #endif /* PTYNOBLOCK */
  264.  
  265. #ifndef ONLCR
  266. #define ONLCR 0
  267. #endif /* ONLCR */
  268.  
  269. #ifdef CK_WAIT_H
  270. #include <sys/wait.h>
  271. #endif /* CK_WAIT_H */
  272.  
  273. #ifdef STREAMSPTY
  274. #ifndef INIT_SPTY
  275. #define INIT_SPTY
  276. #endif /* INIT_SPTY */
  277.  
  278. #include <sys/stream.h>
  279. #include <stropts.h>
  280. #include <termio.h>
  281.  
  282. /* Make sure we don't get the BSD version */
  283.  
  284. #ifdef HAVE_SYS_TTY_H
  285. #include "/usr/include/sys/tty.h"
  286. #endif /* HAVE_SYS_TTY_H */
  287.  
  288. #ifdef HAS_PTYVAR            /* Where is this set? */
  289.  
  290. #include <sys/ptyvar.h>
  291.  
  292. #else /* HAS_PTYVAR */
  293.  
  294. #ifndef TIOCPKT_FLUSHWRITE
  295. #define TIOCPKT_FLUSHWRITE 0x02
  296. #define TIOCPKT_NOSTOP     0x10
  297. #define TIOCPKT_DOSTOP     0x20
  298. #define TIOCPKT_IOCTL      0x40
  299. #endif /* TIOCPKT_FLUSHWRITE */
  300.  
  301. #endif /* HAS_PTYVAR */
  302.  
  303. #ifdef HAVE_TTY_H
  304. #include <tty.h>
  305. #endif /* HAVE_TTY_H */
  306. /*
  307.   Because of the way ptyibuf is used with streams messages, we need
  308.   ptyibuf+1 to be on a full-word boundary.  The following weirdness
  309.   is simply to make that happen.
  310. */
  311. long ptyibufbuf[BUFSIZ/sizeof(long)+1];
  312. char *ptyibuf = ((char *)&ptyibufbuf[1])-1;
  313. char *ptyip = ((char *)&ptyibufbuf[1])-1;
  314. char ptyibuf2[BUFSIZ];
  315. unsigned char ctlbuf[BUFSIZ];
  316. struct strbuf strbufc, strbufd;
  317.  
  318. int readstream();
  319.  
  320. #else  /* ! STREAMSPTY */
  321.  
  322. /* I/O data buffers, pointers, and counters. */
  323.  
  324. char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
  325. char ptyibuf2[BUFSIZ];
  326.  
  327. #endif /* ! STREAMSPTY */
  328.  
  329. #ifndef USE_TERMIO
  330. struct termbuf {
  331.     struct sgttyb sg;
  332.     struct tchars tc;
  333.     struct ltchars ltc;
  334.     int state;
  335.     int lflags;
  336. } termbuf, termbuf2;
  337.  
  338. #define cfsetospeed(tp,val) (tp)->sg.sg_ospeed = (val)
  339. #define cfsetispeed(tp,val) (tp)->sg.sg_ispeed = (val)
  340. #define cfgetospeed(tp)     (tp)->sg.sg_ospeed
  341. #define cfgetispeed(tp)     (tp)->sg.sg_ispeed
  342.  
  343. #else  /* USE_TERMIO */
  344.  
  345. #ifdef SYSV_TERMIO
  346. #define termios termio
  347. #endif /* SYSV_TERMIO */
  348.  
  349. #ifndef TCSANOW
  350.  
  351. #ifdef TCSETS
  352.  
  353. #define TCSANOW TCSETS
  354. #define TCSADRAIN TCSETSW
  355. #define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
  356.  
  357. #else /* TCSETS */
  358.  
  359. #ifdef TCSETA
  360. #define TCSANOW TCSETA
  361. #define TCSADRAIN TCSETAW
  362. #define tcgetattr(f,t) ioctl(f,TCGETA,(char *)t)
  363. #else /* TCSETA */
  364. #define TCSANOW TIOCSETA
  365. #define TCSADRAIN TIOCSETAW
  366. #define tcgetattr(f,t) ioctl(f,TIOCGETA,(char *)t)
  367. #endif /* TCSETA */
  368.  
  369. #endif /* TCSETS */
  370.  
  371. #define tcsetattr(f,a,t) ioctl(f,a,t)
  372. #define cfsetospeed(tp,val) (tp)->c_cflag &= ~CBAUD;(tp)->c_cflag|=(val)
  373. #define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
  374.  
  375. #ifdef CIBAUD
  376. #define cfsetispeed(tp,val) \
  377.  (tp)->c_cflag &= ~CIBAUD; (tp)->c_cflag |= ((val)<<IBSHIFT)
  378. #define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
  379. #else /* CIBAUD */
  380. #define cfsetispeed(tp,val) (tp)->c_cflag &= ~CBAUD; (tp)->c_cflag|=(val)
  381. #define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
  382. #endif /* CIBAUD */
  383.  
  384. #endif /* TCSANOW */
  385.  
  386. struct termios termbuf, termbuf2;       /* pty control structure */
  387.  
  388. #ifdef INIT_SPTY
  389. static int spty = -1;
  390. #endif /* INIT_SPTY */
  391.  
  392. #endif /* USE_TERMIO */
  393.  
  394. static int msg = 0;
  395.  
  396. /* Variables available to other modules */
  397.  
  398. int pty_fork_active = 0;        /* pty fork is active */
  399. PID_T pty_fork_pid = -1;        /* pty fork pid */
  400. int pty_slave_fd = -1;            /* pty slave file descriptor */
  401. int pty_master_fd = -1;            /* pty master file descriptor */
  402.  
  403. /* termbuf routines (begin) */
  404. /*
  405.   init_termbuf()
  406.   copy_termbuf(cp)
  407.   set_termbuf()
  408.  
  409.   These three routines are used to get and set the "termbuf" structure
  410.   to and from the kernel.  init_termbuf() gets the current settings.
  411.   copy_termbuf() hands in a new "termbuf" to write to the kernel, and
  412.   set_termbuf() writes the structure into the kernel.
  413. */
  414. VOID
  415. init_termbuf(fd) int fd; {
  416.     int ttyfd;
  417.     int rc = 0;
  418.  
  419.     ttyfd = fd;
  420.  
  421. #ifdef HAVE_STREAMS
  422.     debug(F100,"init_termbuf HAVE_STREAMS","",0);
  423. #else
  424.     debug(F100,"init_termbuf HAVE_STREAMS NOT DEFINED","",0);
  425. #endif    /* HAVE_STREAMS */
  426. #ifdef STREAMSPTY
  427.     debug(F100,"init_termbuf STREAMSPTY","",0);
  428. #else
  429.     debug(F100,"init_termbuf STREAMSPTY NOT DEFINED","",0);
  430. #endif    /* STREAMSPTY */
  431. #ifdef INIT_SPTY
  432.     debug(F100,"init_termbuf INIT_SPTY","",0);
  433. #else
  434.     debug(F100,"init_termbuf INIT_SPTY NOT DEFINED","",0);
  435. #endif    /* INIT_SPTY */
  436.  
  437.     debug(F101,"init_termbuf ttyfd","",ttyfd);
  438. #ifdef INIT_SPTY
  439.     debug(F101,"init_termbuf spty","",spty);
  440. #endif    /* INIT_SPTY */
  441.  
  442.     memset(&termbuf,0,sizeof(termbuf));
  443.     memset(&termbuf2,0,sizeof(termbuf2));
  444. #ifndef    USE_TERMIO
  445.     rc = ioctl(ttyfd, TIOCGETP, (char *)&termbuf.sg);
  446.     rc |= ioctl(ttyfd, TIOCGETC, (char *)&termbuf.tc);
  447.     rc |= ioctl(ttyfd, TIOCGLTC, (char *)&termbuf.ltc);
  448. #ifdef TIOCGSTATE
  449.     rc |= ioctl(ttyfd, TIOCGSTATE, (char *)&termbuf.state);
  450. #endif /* TIOCGSTATE */
  451. #else /* USE_TERMIO */
  452.     errno = 0;
  453. #ifdef INIT_SPTY
  454.     rc = tcgetattr(spty, &termbuf);
  455.     debug(F111,"init_termbuf() tcgetattr(spty)",ckitoa(rc),errno);
  456. #else
  457.     rc = tcgetattr(ttyfd, &termbuf);
  458.     debug(F111,"init_termbuf() tcgetattr(ttyfd)",ckitoa(rc),errno);
  459. #endif /* INIT_SPTY */
  460. #endif /* USE_TERMIO */
  461.     if (!rc)
  462.       termbuf2 = termbuf;
  463. }
  464.  
  465. #ifdef TIOCPKT_IOCTL
  466. VOID
  467. copy_termbuf(cp, len) char *cp; int len; {
  468.     if (len > sizeof(termbuf))
  469.       len = sizeof(termbuf);
  470.     memcpy((char *)&termbuf, cp, len);
  471.     termbuf2 = termbuf;
  472. }
  473. #endif /* TIOCPKT_IOCTL */
  474.  
  475. VOID
  476. set_termbuf(fd) int fd; {        /* Only make the necessary changes. */
  477.     int x;
  478.     int ttyfd;
  479.     ttyfd = fd;
  480.  
  481.     debug(F101,"set_termbuf ttyfd","",ttyfd);
  482. #ifdef INIT_SPTY
  483.     debug(F101,"set_termbuf spty","",spty);
  484. #endif    /* INIT_SPTY */
  485.  
  486. #ifndef    USE_TERMIO
  487.     debug(F100,"set_termbuf USE_TERMIO","",0);
  488.     if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg, sizeof(termbuf.sg)))
  489.       ioctl(ttyfd, TIOCSETN, (char *)&termbuf.sg);
  490.     if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc, sizeof(termbuf.tc)))
  491.       ioctl(ttyfd, TIOCSETC, (char *)&termbuf.tc);
  492.     if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
  493.            sizeof(termbuf.ltc)))
  494.       ioctl(ttyfd, TIOCSLTC, (char *)&termbuf.ltc);
  495.     if (termbuf.lflags != termbuf2.lflags)
  496.       ioctl(ttyfd, TIOCLSET, (char *)&termbuf.lflags);
  497. #else  /* USE_TERMIO */
  498.     x = memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf));
  499.     debug(F101,"set_termbuf !USE_TERMIO memcmp","",x);
  500.     x = 1;                /* Force this */
  501.     if (x) {
  502.     int x;
  503.     errno = 0;
  504. #ifdef INIT_SPTY
  505.     debug(F100,"set_termbuf INIT_SPTY","",0);
  506.     x = tcsetattr(spty, TCSANOW, &termbuf);
  507.     debug(F111,"set_termbuf tcsetattr(spty)",ckitoa(x),errno);
  508. #else
  509.     debug(F100,"set_termbuf !INIT_SPTY","",0);
  510.     x = tcsetattr(ttyfd, TCSANOW, &termbuf);
  511.     debug(F111,"set_termbuf tcsetattr(ttyfd)",ckitoa(x),errno);
  512. #endif /* INIT_SPTY */
  513.     }
  514. #endif /* USE_TERMIO */
  515. }
  516. /* termbuf routines (end) */
  517.  
  518. VOID
  519. ptyint_vhangup() {
  520. #ifdef CK_VHANGUP
  521. #ifdef CK_POSIX_SIG
  522.     struct sigaction sa;
  523.     /* Initialize "sa" structure. */
  524.     sigemptyset(&sa.sa_mask);
  525.     sa.sa_flags = 0;
  526.     sa.sa_handler = SIG_IGN;
  527.     sigaction(SIGHUP, &sa, (struct sigaction *)0);
  528.     vhangup();
  529.     sa.sa_handler = SIG_DFL;
  530.     sigaction(SIGHUP, &sa, (struct sigaction *)0);
  531. #else /* CK_POSIX_SIG */
  532.     signal(SIGHUP,SIG_IGN);
  533.     vhangup();
  534.     signal(SIGHUP,SIG_DFL);
  535. #endif /* CK_POSIX_SIG */
  536. #endif /* CK_VHANGUP */
  537. }
  538.  
  539. /*
  540.   This routine is called twice.  It's not particularly important that the
  541.   setsid() or TIOCSCTTY ioctls succeed (they may not the second time), but
  542.   rather that we have a controlling terminal at the end.  It is assumed that
  543.   vhangup doesn't exist and confuse the process's notion of controlling
  544.   terminal on any system without TIOCNOTTY.  That is, either vhangup() leaves
  545.   the controlling terminal in tact, breaks the association completely, or the
  546.   system provides TIOCNOTTY to get things back into a reasonable state.  In
  547.   practice, vhangup() either breaks the association completely or doesn't
  548.   effect controlling terminals, so this condition is met.
  549. */
  550. long
  551. ptyint_void_association() {
  552.     int con_fd;
  553. #ifdef HAVE_SETSID
  554.     debug(F110,
  555.       "ptyint_void_association()",
  556.       "setsid()",
  557.       0
  558.       );
  559.     setsid();
  560. #endif /* HAVE_SETSID */
  561.  
  562. #ifndef NO_DEVTTY
  563.     /* Void tty association first */
  564. #ifdef TIOCNOTTY
  565.     con_fd = open("/dev/tty", O_RDWR);
  566.     debug(F111,
  567.       "ptyint_void_association() open(/dev/tty,O_RDWR)",
  568.       "/dev/tty",
  569.       con_fd);
  570.     if (con_fd >= 0) {
  571.         ioctl(con_fd, TIOCNOTTY, 0);
  572.         close(con_fd);
  573.     }
  574. #ifdef DEBUG
  575.     else debug(F101, "ptyint_void_association() open() errno","",errno);
  576. #endif /* DEBUG */
  577. #endif /* TIOCNOTTY */
  578. #endif /* NO_DEVTTY */
  579.     return(0);
  580. }
  581.  
  582. /* PID may be zero for unknown.*/
  583.  
  584. long
  585. pty_cleanup(slave, pid, update_utmp) char *slave; int pid; int update_utmp; {
  586. #ifdef VHANG_LAST
  587.     int retval, fd;
  588. #endif /* VHANG_LAST */
  589.  
  590.     debug(F111,"pty_cleanup()",slave,pid);
  591. #ifdef WANT_UTMP
  592.     if (update_utmp)
  593.       pty_update_utmp(PTY_DEAD_PROCESS,
  594.               0,
  595.               "",
  596.               slave,
  597.               (char *)0,
  598.               PTY_UTMP_USERNAME_VALID
  599.               );
  600. #endif /* WANT_UTMP */
  601.  
  602. #ifdef SETUID
  603.     chmod(slave, 0666);
  604.     chown(slave, 0, 0);
  605. #endif /* SETUID */
  606.  
  607. #ifdef HAVE_REVOKE
  608.     revoke(slave);
  609.     /*
  610.        Revoke isn't guaranteed to send a SIGHUP to the processes it
  611.        dissociates from the terminal.  The best solution without a Posix
  612.        mechanism for forcing a hangup is to killpg() the process group of the
  613.        pty.  This will at least kill the shell and hopefully, the child
  614.        processes.  This is not always the case, however.  If the shell puts
  615.        each job in a process group and doesn't pass along SIGHUP, all
  616.        processes may not die.
  617.     */
  618.     if (pid > 0) {
  619. #ifdef HAVE_KILLPG
  620.     killpg(pid, SIGHUP);
  621. #else
  622.     kill(-(pid), SIGHUP);
  623. #endif /*HAVE_KILLPG*/
  624.     }
  625. #else /* HAVE_REVOKE*/
  626. #ifdef VHANG_LAST
  627.     {
  628.         int status;
  629. #ifdef CK_POSIX_SIG
  630.         sigset_t old, new;
  631.         sigemptyset(&new);
  632.         sigaddset(&new, SIGCHLD);
  633.         sigprocmask(SIG_BLOCK, &new, &old);
  634. #else /*CK_POSIX_SIG*/
  635.         int mask = sigblock(sigmask(SIGCHLD));
  636. #endif /*CK_POSIX_SIG*/
  637.         switch (retval = fork()) {
  638.       case -1:
  639. #ifdef CK_POSIX_SIG
  640.             sigprocmask(SIG_SETMASK, &old, 0);
  641. #else /*CK_POSIX_SIG*/
  642.             sigsetmask(mask);
  643. #endif /*CK_POSIX_SIG*/
  644.             return errno;
  645.       case 0:
  646.             ptyint_void_association();
  647.             if (retval = (pty_open_ctty(slave, &fd, -1)))
  648.           exit(retval);
  649.             ptyint_vhangup();
  650.             exit(0);
  651.         break;
  652.       default:
  653. #ifdef HAVE_WAITPID
  654.             waitpid(retval, &status, 0);
  655. #else /*HAVE_WAITPID*/
  656.             wait(&status);
  657. #endif /* HAVE_WAITPID */
  658. #ifdef CK_POSIX_SIG
  659.             sigprocmask(SIG_SETMASK, &old, 0);
  660. #else /*CK_POSIX_SIG*/
  661.             sigsetmask(mask);
  662. #endif /*CK_POSIX_SIG*/
  663.             break;
  664.         }
  665.     }
  666. #endif /*VHANG_LAST*/
  667. #endif /* HAVE_REVOKE*/
  668. #ifndef HAVE_STREAMS
  669.     slave[strlen("/dev/")] = 'p';
  670. #ifdef SETUID
  671.     chmod(slave, 0666);
  672.     chown(slave, 0, 0);
  673. #endif /* SETUID */
  674. #endif /* HAVE_STREAMS */
  675.     return(0);
  676. }
  677.  
  678. long
  679. pty_getpty(fd, slave, slavelength) int slavelength; int *fd; char *slave; {
  680.     char *cp;
  681.     char *p;
  682.     int i, ptynum;
  683.     struct stat stb;
  684. #ifndef HAVE_OPENPTY
  685. #ifndef HAVE__GETPTY
  686.     char slavebuf[1024];
  687. #endif /* HAVE__GETPTY */
  688. #endif /* HAVE_OPENPTY */
  689. #ifdef HAVE__GETPTY
  690.     char *slaveret;            /* Temp to hold pointer to slave */
  691. #endif /*HAVE__GETPTY*/
  692.  
  693. #ifdef HAVE_OPENPTY
  694.     int slavefd;
  695.  
  696.     pty_master_fd = -1;
  697.     debug(F100,"HAVE_OPENPTY","",0);
  698.     if (openpty(fd,
  699.         &slavefd,
  700.         slave,
  701.         (struct termios *)0,
  702.         (struct winsize *)0
  703.         )
  704.     ) {
  705.     pty_master_fd = *fd;
  706.     return(1);
  707.     }
  708.     close(slavefd);
  709.     return(0);
  710.  
  711. #else /* HAVE_OPENPTY */
  712.  
  713. #ifdef HAVE__GETPTY
  714. /*
  715.   This code is included for Irix; as of version 5.3, Irix has /dev/ptmx, but
  716.   it fails to work properly; even after calling unlockpt, root gets permission
  717.   denied opening the pty.  The code to support _getpty should be removed if
  718.   Irix gets working streams ptys in favor of maintaining the least needed code
  719.   paths.
  720. */
  721.     debug(F100,"HAVE__GETPTY","",0);
  722.     if ((slaveret = _getpty(fd, O_RDWR | O_NDELAY, 0600, 0)) == 0) {
  723.     *fd = -1;
  724.     return(PTY_GETPTY_NOPTY);
  725.     }
  726.     if (strlen(slaveret) > slavelength - 1) {
  727.     close(*fd);
  728.     *fd = -1;
  729.     return(PTY_GETPTY_SLAVE_TOOLONG);
  730.     } else {
  731.     ckstrncpy(slave, slaveret, slavelength);
  732.     }
  733.     return(0);
  734.  
  735. #else /* HAVE__GETPTY */
  736.  
  737.     *fd = open("/dev/ptym/clone", O_RDWR|O_NDELAY); /* HPUX */
  738.     if (*fd >= 0) {
  739.         debug(F110,"pty_getpty()","open(/dev/ptym/clone) success",0);
  740.         goto have_fd;
  741.     }
  742.  
  743. #ifdef HAVE_PTMX
  744.     debug(F100,"HAVE_PTMX","",0);
  745.     *fd = open("/dev/ptmx",O_RDWR|O_NDELAY);
  746.     if (*fd >= 0) {
  747.         debug(F110,"pty_getpty()","open(/dev/ptmx) success",0);
  748.         goto have_fd;
  749.     }
  750. #endif /* HAVE_PTMX */
  751.  
  752.     *fd = open("/dev/ptc", O_RDWR|O_NDELAY); /* AIX */
  753.     if (*fd >= 0) {
  754.         debug(F110,"pty_getpty()","open(/dev/ptc) success",0);
  755.         goto have_fd;
  756.     }
  757.     *fd = open("/dev/pty", O_RDWR|O_NDELAY); /* sysvimp */
  758.     if (*fd >= 0)
  759.         debug(F110,"pty_getpty()","open(/dev/pty) success",0);
  760.  
  761.   have_fd:
  762.     /* This would be the pty master */
  763.     debug(F101,"pty_getpty fd(A)","",*fd);
  764.     if (*fd >= 0) {
  765.     pty_master_fd = *fd;
  766.  
  767. #ifdef HAVE_GRANTPT
  768. #ifdef HAVE_PTMX
  769.         debug(F100,"HAVE_GRANTPT","",0);
  770.     if (grantpt(*fd) || unlockpt(*fd))
  771.       return(PTY_GETPTY_STREAMS);
  772. #endif /* HAVE_PTMX */
  773. #endif /* HAVE_GRANTPT */
  774.  
  775. #ifdef HAVE_PTSNAME
  776.         debug(F100,"HAVE_PTSNAME","",0);
  777.     p = (char *)ptsname(*fd);
  778.         debug(F110,"pty_getpty() ptsname()",p,0);
  779. #else
  780. #ifdef HAVE_TTYNAME
  781.         debug(F100,"HAVE_TTYNAME","",0);
  782.     p = ttyname(*fd);
  783.         debug(F110,"pty_getpty() ttyname()",p,0);
  784. #else
  785.     /* If we don't have either what do we do? */
  786.       return(PTY_GETPTY_NOPTY);    /* punt */
  787. #endif /* HAVE_TTYNAME */
  788. #endif /* HAVE_PTSNAME */
  789.     if (p) {
  790.         if (strlen(p) > slavelength - 1) {
  791.                 close (*fd);
  792.                 *fd = -1;
  793.                 return(PTY_GETPTY_SLAVE_TOOLONG);
  794.         }
  795.         ckstrncpy(slave, p, slavelength);
  796.         return(0);
  797.     }
  798.     if (fstat(*fd, &stb) < 0) {
  799.         close(*fd);
  800.         return(PTY_GETPTY_FSTAT);
  801.     }
  802.     ptynum = (int)(stb.st_rdev&0xFF);
  803.     sprintf(slavebuf, "/dev/ttyp%x", ptynum); /* safe */
  804.     if (strlen(slavebuf) > slavelength - 1) {
  805.         close(*fd);
  806.         *fd = -1;
  807.         return(PTY_GETPTY_SLAVE_TOOLONG);
  808.     }
  809.         debug(F110,"pty_getpty() slavebuf",slavebuf,0);
  810.     ckstrncpy(slave, slavebuf, slavelength);
  811.     return(0);
  812.     } else {
  813.         for (cp = "pqrstuvwxyzPQRST";*cp; cp++) {
  814.         sprintf(slavebuf,"/dev/ptyXX"); /* safe */
  815.         slavebuf[sizeof("/dev/pty") - 1] = *cp;
  816.         slavebuf[sizeof("/dev/ptyp") - 1] = '0';
  817.         if (stat(slavebuf, &stb) < 0)
  818.           break;
  819.         for (i = 0; i < 16; i++) {
  820.         slavebuf[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i];
  821.         errno = 0;
  822.         *fd = open(slavebuf, O_RDWR|O_NDELAY);
  823.         if (*fd < 0) {
  824.             debug(F111,"pty_getpty() pty master open error",
  825.               slavebuf,errno);
  826.             continue;
  827.         }
  828.                 debug(F111,"pty_getpty() found pty master",slavebuf,*fd);
  829.         slavebuf[sizeof("/dev/") - 1] = 't'; /* got pty */
  830.         if (strlen(slavebuf) > slavelength -1) {
  831.             close(*fd);
  832.             *fd = -1;
  833.             return(PTY_GETPTY_SLAVE_TOOLONG);
  834.         }
  835.         ckstrncpy(slave, slavebuf, slavelength);
  836.                 debug(F110,"pty_getpty slave name",slave,0);
  837.         pty_master_fd = *fd;
  838.         return(0);
  839.         }
  840.     }
  841.     return(PTY_GETPTY_NOPTY);
  842.     }
  843. #endif /*HAVE__GETPTY*/
  844. #endif /* HAVE_OPENPTY */
  845. }
  846.  
  847. long
  848. pty_init() {
  849. #ifdef HAVE_PTYM
  850.     static char dummy;
  851.     debug(F100,"HAVE_PTYM","",0);
  852.     tty_bank =  &master_name[strlen("/dev/ptym/pty")];
  853.     tty_num  =  &master_name[strlen("/dev/ptym/ptyX")];
  854.     slave_bank = &slave_name[strlen("/dev/pty/tty")];
  855.     slave_num  = &slave_name[strlen("/dev/pty/ttyX")];
  856. #endif
  857.     return(0L);
  858. }
  859.  
  860. /*
  861.   The following is an array of modules that should be pushed on the stream.
  862.   See configure.in for caviats and notes about when this array is used and not
  863.   used.
  864. */
  865. #ifdef HAVE_STREAMS
  866. #ifndef HAVE_LINE_PUSH
  867. static char *push_list[] = {
  868. #ifdef PUSH_PTEM
  869.     "ptem",
  870. #endif
  871. #ifdef PUSH_LDTERM
  872.     "ldterm",
  873. #endif
  874. #ifdef PUSH_TTCOMPAT
  875.     "ttcompat",
  876. #endif
  877.     0
  878. };
  879. #endif /* HAVE_LINE_PUSH */
  880. #endif /* HAVE_STREAMS */
  881.  
  882. long
  883. pty_initialize_slave (fd) int fd; {
  884. #ifdef POSIX_TERMIOS
  885. #ifndef ultrix
  886.     struct termios new_termio;
  887. #else
  888.     struct sgttyb b;
  889. #endif /* ultrix */
  890. #else
  891.     struct sgttyb b;
  892. #endif /* POSIX_TERMIOS */
  893.     int pid;
  894. #ifdef POSIX_TERMIOS
  895. #ifndef ultrix
  896.     int rc;
  897. #endif /* ultrix */
  898. #endif /* POSIX_TERMIOS */
  899.  
  900.     debug(F111,"pty_initialize_slave()","fd",fd);
  901.  
  902. #ifdef HAVE_STREAMS
  903. #ifdef HAVE_LINE_PUSH
  904.     while (ioctl(fd,I_POP,0) == 0) ;    /* Clear out any old lined's */
  905.  
  906.     if (line_push(fd) < 0) {
  907.         debug(F110,"pty_initialize_slave()","line_push() failed",0);
  908.     close(fd);
  909.         fd = -1;
  910.         return(PTY_OPEN_SLAVE_LINE_PUSHFAIL);
  911.     }
  912. #else /*No line_push */
  913.     {
  914.         char **module = &push_list[0];
  915.         while (*module) {
  916.         if (ioctl(fd, I_PUSH, *(module++)) < 0) {
  917.                 debug(F110,"pty_initialize_slave()","ioctl(I_PUSH) failed",0);
  918.         return(PTY_OPEN_SLAVE_PUSH_FAIL);
  919.         }
  920.     }
  921.     }
  922. #endif /*LINE_PUSH*/
  923. #endif /*HAVE_STREAMS*/
  924. /*
  925.   Under Ultrix 3.0, the pgrp of the slave pty terminal needs to be set
  926.   explicitly.  Why rlogind works at all without this on 4.3BSD is a mystery.
  927. */
  928. #ifdef GETPGRP_ONEARG
  929.     pid = getpgrp(getpid());
  930. #else
  931.     pid = getpgrp();
  932. #endif /* GETPGRP_ONEARG */
  933.  
  934.     debug(F111,"pty_initialize_slave()","pid",pid);
  935.  
  936. #ifdef TIOCSPGRP
  937.     ioctl(fd, TIOCSPGRP, &pid);
  938. #endif /* TIOCSPGRP */
  939.  
  940. #ifdef POSIX_TERMIOS
  941. #ifndef ultrix
  942.     tcsetpgrp(fd, pid);
  943.     errno = 0;
  944.     rc = tcgetattr(fd,&new_termio);
  945.     debug(F111,"pty_initialize_slave tcgetattr(fd)",ckitoa(rc),errno);
  946.     if (rc == 0) {
  947.     new_termio.c_cc[VMIN] = 1;
  948.     new_termio.c_cc[VTIME] = 0;
  949.     rc = tcsetattr(fd,TCSANOW,&new_termio);
  950.     debug(F111,"pty_initialize_slave tcsetattr(fd)",ckitoa(rc),errno);
  951.     }
  952. #endif /* ultrix */
  953. #endif /* POSIX_TERMIOS */
  954.     return(0L);
  955. }
  956.  
  957. #ifdef WANT_UTMP
  958. long
  959. pty_logwtmp (tty, user, host) char *user, *tty, *host; {
  960. #ifdef HAVE_LOGWTMP
  961.     logwtmp(tty,user,host);
  962.     return(0);
  963. #else
  964.     struct utmp ut;
  965.     char *tmpx;
  966.     char utmp_id[5];
  967.     int loggingin = user[0];        /* Will be empty for logout */
  968.  
  969. #ifndef NO_UT_HOST
  970.     strncpy(ut.ut_host, host, sizeof(ut.ut_host));
  971. #endif /* NO_UT_HOST */
  972.  
  973.     strncpy(ut.ut_line, tty, sizeof(ut.ut_line));
  974.     ut.ut_time = time(0);
  975.  
  976. #ifndef NO_UT_PID
  977.     ut.ut_pid = getpid();
  978.     strncpy(ut.ut_user, user, sizeof(ut.ut_user));
  979.  
  980.     tmpx = tty + strlen(tty) - 2;
  981.     ckmakmsg(utmp_id,5,"kr",tmpx,NULL,NULL);
  982.     strncpy(ut.ut_id, utmp_id, sizeof(ut.ut_id));
  983.     ut.ut_pid = (loggingin ? getpid() : 0);
  984.     ut.ut_type = (loggingin ? USER_PROCESS : DEAD_PROCESS);
  985. #else
  986.     strncpy(ut.ut_name, user, sizeof(ut.ut_name));
  987. #endif /* NO_UT_PID */
  988.  
  989.     return(ptyint_update_wtmp(&ut, host, user));
  990.  
  991. #endif /* HAVE_LOGWTMP */
  992. }
  993. #endif /* WANT_UTMP */
  994.  
  995. /*
  996.   This routine is called twice.  It's not particularly important that the
  997.   setsid() or TIOCSCTTY ioctls succeed (they may not the second time), but
  998.   rather that we have a controlling terminal at the end.  It is assumed that
  999.   vhangup doesn't exist and confuse the process's notion of controlling
  1000.   terminal on any system without TIOCNOTTY.  That is, either vhangup() leaves
  1001.   the controlling terminal in tact, breaks the association completely, or the
  1002.   system provides TIOCNOTTY to get things back into a reasonable state.  In
  1003.   practice, vhangup() either breaks the association completely or doesn't
  1004.   effect controlling terminals, so this condition is met.
  1005. */
  1006. long
  1007. pty_open_ctty(slave, fd, fc) char * slave; int *fd; int fc; {
  1008.     int retval;
  1009.  
  1010.     debug(F110,"pty_open_ctty() slave",slave,0);
  1011.  
  1012. /* First, dissociate from previous terminal */
  1013.  
  1014.     if ((retval = ptyint_void_association()) != 0) {
  1015.         debug(F111,
  1016.           "pty_open_ctty()",
  1017.           "ptyint_void_association() failed",
  1018.           retval
  1019.           );
  1020.     return(retval);
  1021.     }
  1022. #ifdef MUST_SETPGRP
  1023. /*
  1024.   The Ultrix (and other BSD tty drivers) require the process group
  1025.   to be zero in order to acquire the new tty as a controlling tty.
  1026. */
  1027.     setpgrp(0,0);
  1028.     debug(F101,"pty_open_ctty MUST_SETPGRP setpgrp(0,0)","",errno);
  1029. #endif /* MUST_SETPGRP */
  1030.  
  1031.     errno = 0;
  1032.     *fd = open(slave, O_RDWR);
  1033.     debug(F111,"pty_open_ctty open(slave) fd",slave,*fd);
  1034.     if (*fd < 0) {
  1035.     debug(F111,"pty_open_ctty() open failure", slave, errno);
  1036.     return(PTY_OPEN_SLAVE_OPENFAIL);
  1037.     }
  1038. #ifdef SOLARIS
  1039.     /* This forces the job to have a controlling terminal. */
  1040.     close(*fd);
  1041.     *fd = open(slave, O_RDWR);
  1042.     debug(F111,"pty_open_ctty close/open(slave) fd",slave,*fd);
  1043. #ifdef DEBUG
  1044.     /* This shows that /dev/tty exists */
  1045. if (deblog) {
  1046.     int x;
  1047.     x = open("/dev/tty", O_RDWR);
  1048.     debug(F111,"pty_open_ctty open(/dev/tty) fd",slave,x);
  1049.     if (x < 0) debug(F111,"pty_open_ctty open(/dev/tty) errno","",errno);
  1050.     debug(F110,"pty_open_ctty ttyname(/dev/tty)",ttyname(x),0);
  1051.     if (x > -1) close(x);
  1052.     }
  1053. #endif    /* DEBUG */
  1054. #endif /* SOLARIS */
  1055.  
  1056. #ifdef MUST_SETPGRP
  1057.     setpgrp(0, getpid());
  1058. #endif /* MUST_SETPGRP */
  1059.  
  1060. #ifdef TIOCSCTTY
  1061.     if (
  1062. #ifdef COMMENT
  1063.     fc == 0
  1064. #else
  1065.     1
  1066. #endif    /* COMMENT */
  1067.     ) {
  1068.     /* TIOCSCTTY = Make this the job's controlling terminal */
  1069.     errno = 0;
  1070.     retval = ioctl(*fd, TIOCSCTTY, 0); /* Don't check return.*/
  1071.     debug(F111,"pty_open_ctty() ioctl TIOCSCTTY",ckitoa(retval),errno);
  1072.     }
  1073. #endif /* TIOCSCTTY */
  1074.     return(0L);
  1075. }
  1076.  
  1077. long
  1078. pty_open_slave(slave, fd, fc) char *slave; int *fd; int fc; {
  1079.     int vfd, testfd;
  1080.     long retval;
  1081. #ifdef CK_POSIX_SIG
  1082.     struct sigaction sa;
  1083.  
  1084.     sigemptyset(&sa.sa_mask);        /* Initialize "sa" structure. */
  1085.     sa.sa_flags = 0;
  1086. #endif /* CK_POSIX_SIG */
  1087.  
  1088. /*
  1089.   First, chmod and chown the slave.  If we have vhangup then we really need
  1090.   pty_open_ctty to make sure our controlling terminal is the pty we're
  1091.   opening.  However, if we are using revoke or nothing then we just need a
  1092.   file descriiptor for the pty.  Considering some OSes in this category break
  1093.   on the second call to open_ctty (currently OSF but others may), we simply
  1094.   use a descriptor if we can.
  1095. */
  1096. #ifdef VHANG_FIRST
  1097.     if ((retval = pty_open_ctty(slave, &vfd, fc)) != 0) {
  1098.         debug(F111,
  1099.           "pty_open_slave() VHANG_FIRST",
  1100.           "pty_open_ctty() failed",
  1101.           retval
  1102.           );
  1103.         return(retval);
  1104.     }
  1105.     if (vfd < 0) {
  1106.         debug(F111,
  1107.           "pty_open_slave() VHANG_FIRST",
  1108.           "PTY_OPEN_SLAVE_OPENFAIL",
  1109.           vfd
  1110.           );
  1111.     return(PTY_OPEN_SLAVE_OPENFAIL);
  1112.     }
  1113. #endif /* VHANG_FIRST */
  1114.  
  1115.     if (slave == NULL || *slave == '\0') {
  1116.         debug(F110,"pty_open_slave()","PTY_OPEN_SLAVE_TOOSHORT",0);
  1117.         return(PTY_OPEN_SLAVE_TOOSHORT);
  1118.     }
  1119.  
  1120. #ifdef SETUID
  1121.     if (chmod(slave, 0)) {
  1122.         debug(F110,"pty_open_slave()","PTY_OPEN_SLAVE_CHMODFAIL",0);
  1123.         return(PTY_OPEN_SLAVE_CHMODFAIL);
  1124.     }
  1125.     if (chown(slave, 0, 0 ) == -1 ) {
  1126.         debug(F110,"pty_open_slave()","PTY_OPEN_SLAVE_CHOWNFAIL",0);
  1127.         return(PTY_OPEN_SLAVE_CHOWNFAIL);
  1128.     }
  1129. #endif /* SETUID */
  1130. #ifdef VHANG_FIRST
  1131.     ptyint_vhangup();
  1132.     close(vfd);
  1133. #endif /* VHANG_FIRST */
  1134.  
  1135.     if ((retval = ptyint_void_association()) != 0) {
  1136.         debug(F111,
  1137.           "pty_open_slave()",
  1138.           "ptyint_void_association() failed",
  1139.           retval
  1140.           );
  1141.         return(retval);
  1142.     }
  1143.  
  1144. #ifdef HAVE_REVOKE
  1145.     if (revoke (slave) < 0 ) {
  1146.         debug(F110,"pty_open_slave()","PTY_OPEN_SLAVE_REVOKEFAIL",0);
  1147.     return(PTY_OPEN_SLAVE_REVOKEFAIL);
  1148.     }
  1149. #endif /* HAVE_REVOKE */
  1150.  
  1151. /* Open the pty for real. */
  1152.  
  1153.     retval = pty_open_ctty(slave, fd, fc);
  1154.     debug(F111,"pty_open_slave retval",slave,retval);
  1155.     debug(F111,"pty_open_slave fd",slave,*fd);
  1156.     if (retval != 0) {
  1157.         debug(F111,"pty_open_slave()","pty_open_ctty() failed",retval);
  1158.     return(PTY_OPEN_SLAVE_OPENFAIL);
  1159.     }
  1160.     pty_slave_fd = *fd;           /* This is not visible to the upper fork */
  1161.     debug(F111,"pty_open_slave fd ctty'd",slave,pty_slave_fd);
  1162.     retval = pty_initialize_slave(*fd);
  1163.     debug(F111,"pty_open_slave fd init'd",slave,pty_slave_fd);
  1164.     if (retval) {
  1165.         debug(F111,"pty_open_slave()","pty_initialize_slave() failed",retval);
  1166.         return(retval);
  1167.     }
  1168.     /* (VOID)pty_make_raw(*fd); */
  1169.  
  1170.     debug(F100,"pty_open_slave OK","",*fd);
  1171.     return(0L);
  1172. }
  1173.  
  1174. #ifdef WANT_UTMP
  1175.  
  1176. #ifndef UTMP_FILE
  1177. #ifdef _PATH_UTMP
  1178. #define UTMP_FILE _PATH_UTMP
  1179. #endif /* _PATH_UTMP */
  1180. #endif /*  UTMP_FILE */
  1181.  
  1182. /* If it is *still* missing, assume /etc/utmp */
  1183.  
  1184. #ifndef UTMP_FILE
  1185. #define    UTMP_FILE "/etc/utmp"
  1186. #endif /* UTMP_FILE */
  1187.  
  1188. #ifndef NO_UT_PID
  1189. #define WTMP_REQUIRES_USERNAME
  1190. #endif /* NO_UT_PID */
  1191.  
  1192. long
  1193. pty_update_utmp(process_type, pid, username, line, host, flags)
  1194.     int process_type;
  1195.     int pid;
  1196.     char *username, *line, *host;
  1197.     int flags;
  1198. /* pty_update_utmp */ {
  1199.     struct utmp ent, ut;
  1200. #ifndef HAVE_SETUTENT
  1201.     struct stat statb;
  1202.     int tty;
  1203. #endif /* HAVE_SETUTENT */
  1204. #ifdef HAVE_SETUTXENT
  1205.     struct utmpx utx;
  1206. #endif /* HAVE_SETUTXENT */
  1207. #ifndef NO_UT_PID
  1208.     char *tmpx;
  1209.     char utmp_id[5];
  1210. #endif /* NO_UT_PID */
  1211.     char userbuf[32];
  1212.     int fd;
  1213.  
  1214.     debug(F100,"pty_update_utmp()","",0);
  1215.     strncpy(ent.ut_line, line+sizeof("/dev/")-1, sizeof(ent.ut_line));
  1216.     ent.ut_time = time(0);
  1217.  
  1218. #ifdef NO_UT_PID
  1219.     if (process_type == PTY_LOGIN_PROCESS)
  1220.       return(0L);
  1221. #else /* NO_UT_PID */
  1222.  
  1223.     ent.ut_pid = pid;
  1224.  
  1225.     switch (process_type) {
  1226.       case PTY_LOGIN_PROCESS:
  1227.     ent.ut_type = LOGIN_PROCESS;
  1228.     break;
  1229.       case PTY_USER_PROCESS:
  1230.     ent.ut_type = USER_PROCESS;
  1231.     break;
  1232.       case PTY_DEAD_PROCESS:
  1233.     ent.ut_type = DEAD_PROCESS;
  1234.     break;
  1235.       default:
  1236.     return(PTY_UPDATE_UTMP_PROCTYPE_INVALID);
  1237.     }
  1238. #endif /*NO_UT_PID*/
  1239.  
  1240. #ifndef NO_UT_HOST
  1241.     if (host)
  1242.       strncpy(ent.ut_host, host, sizeof(ent.ut_host));
  1243.     else
  1244.       ent.ut_host[0] = '\0';
  1245. #endif /* NO_UT_HOST */
  1246.  
  1247. #ifndef NO_UT_PID
  1248.     if (!strcmp (line, "/dev/console")) {
  1249.     char * s = NULL;
  1250.  
  1251. #ifdef sun
  1252. #ifdef __SVR4
  1253.     s = "co";
  1254. #else
  1255.     s = "cons";
  1256. #endif /* __SVR4 */
  1257. #else
  1258.     s = "cons";
  1259. #endif /* sun */
  1260.  
  1261.     strncpy(ent.ut_id, s, 4);
  1262.  
  1263.     } else {
  1264.  
  1265.     tmpx = line + strlen(line)-1;
  1266.     if (*(tmpx-1) != '/') tmpx--;    /* last 2 chars unless it's a '/' */
  1267. #ifdef __hpux
  1268.     ckstrncpy(utmp_id, tmpx, 5);
  1269. #else
  1270.     ckmakmsg(utmp_id,5,"kl",tmpx,NULL,NULL);
  1271. #endif /* __hpux */
  1272.     strncpy(ent.ut_id, utmp_id, sizeof(ent.ut_id));
  1273.     }
  1274.     strncpy(ent.ut_user, username, sizeof(ent.ut_user));
  1275.  
  1276. #else
  1277.  
  1278.     strncpy(ent.ut_name, username, sizeof(ent.ut_name));
  1279.  
  1280. #endif /* NO_UT_PID */
  1281.  
  1282.     if (username[0])
  1283.       strncpy(userbuf, username, sizeof(userbuf));
  1284.     else
  1285.       userbuf[0] = '\0';
  1286.  
  1287. #ifdef HAVE_SETUTENT
  1288.  
  1289.     utmpname(UTMP_FILE);
  1290.     setutent();
  1291. /*
  1292.   If we need to preserve the user name in the wtmp structure and Our flags
  1293.   tell us we can obtain it from the utmp and we succeed in obtaining it, we
  1294.   then save the utmp structure we obtain, write out the utmp structure and
  1295.   change the username pointer so it is used by update_wtmp.
  1296. */
  1297.  
  1298. #ifdef WTMP_REQUIRES_USERNAME
  1299.     if ((!username[0]) && (flags&PTY_UTMP_USERNAME_VALID) &&line) {
  1300.     struct utmp *utptr;
  1301.     strncpy(ut.ut_line, line, sizeof(ut.ut_line));
  1302.     utptr = getutline(&ut);
  1303.     if (utptr)
  1304.       strncpy(userbuf,utptr->ut_user,sizeof(ut.ut_user));
  1305.     }
  1306. #endif /* WTMP_REQUIRES_USERNAME */
  1307.  
  1308.     pututline(&ent);
  1309.     endutent();
  1310.  
  1311. #ifdef HAVE_SETUTXENT
  1312.     setutxent();
  1313. #ifdef HAVE_GETUTMPX
  1314.     getutmpx(&ent, &utx);
  1315. #else /* HAVE_GETUTMPX */
  1316.     /* For platforms like HPUX and Dec Unix which don't have getutmpx */
  1317.     strncpy(utx.ut_user, ent.ut_user, sizeof(ent.ut_user));
  1318.     strncpy(utx.ut_id, ent.ut_id, sizeof(ent.ut_id));
  1319.     strncpy(utx.ut_line, ent.ut_line, sizeof(ent.ut_line));
  1320.     utx.ut_pid = pid;        /* kludge for Irix, etc. to avoid trunc. */
  1321.     utx.ut_type = ent.ut_type;
  1322. #ifdef UT_EXIT_STRUCTURE_DIFFER
  1323.     utx.ut_exit.ut_exit = ent.ut_exit.e_exit;
  1324. #else /* UT_EXIT_STRUCTURE_DIFFER */
  1325. /* KLUDGE for now; eventually this will be a feature test... See PR#[40] */
  1326. #ifdef __hpux
  1327.     utx.ut_exit.__e_termination = ent.ut_exit.e_termination;
  1328.     utx.ut_exit.__e_exit = ent.ut_exit.e_exit;
  1329. #else /* __hpux */
  1330.     /* XXX do nothing for now; we don't even know the struct member exists */
  1331. #endif /* __hpux */
  1332. #endif /* UT_EXIT_STRUCTURE_DIFFER */
  1333.     utx.ut_tv.tv_sec = ent.ut_time;
  1334.     utx.ut_tv.tv_usec = 0;
  1335. #endif /* HAVE_GETUTMPX */
  1336.     if (host)
  1337.       strncpy(utx.ut_host, host, sizeof(utx.ut_host));
  1338.     else
  1339.       utx.ut_host[0] = 0;
  1340.     pututxline(&utx);
  1341.     endutxent();
  1342. #endif /* HAVE_SETUTXENT */
  1343.  
  1344. #else /* HAVE_SETUTENT */
  1345.     if (flags&PTY_TTYSLOT_USABLE) {
  1346.     tty = ttyslot();
  1347.     } else {
  1348.     int lc;
  1349.     tty = -1;
  1350.     if ((fd = open(UTMP_FILE, O_RDWR)) < 0)
  1351.       return(errno);
  1352.     for (lc = 0;
  1353.          lseek(fd, (off_t)(lc * sizeof(struct utmp)), SEEK_SET) != -1;
  1354.          lc++
  1355.          ) {
  1356.         if (read(fd,
  1357.              (char *)&ut,
  1358.              sizeof(struct utmp)
  1359.              ) != sizeof(struct utmp)
  1360.         )
  1361.           break;
  1362.         if (strncmp(ut.ut_line, ent.ut_line, sizeof(ut.ut_line)) == 0) {
  1363.         tty = lc;
  1364. #ifdef WTMP_REQUIRES_USERNAME
  1365.         if (!username&&(flags&PTY_UTMP_USERNAME_VALID))
  1366.           strncpy(userbuf, ut.ut_user, sizeof(ut.ut_user));
  1367. #endif /* WTMP_REQUIRES_USERNAME */
  1368.         break;
  1369.         }
  1370.     }
  1371.     close(fd);
  1372.     }
  1373.     if (tty > 0 && (fd = open(UTMP_FILE, O_WRONLY, 0)) >= 0) {
  1374.     lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
  1375.     write(fd, (char *)&ent, sizeof(struct utmp));
  1376.     close(fd);
  1377.     }
  1378. #endif /* HAVE_SETUTENT */
  1379.  
  1380.     /* Don't record LOGIN_PROCESS entries. */
  1381.     if (process_type == PTY_LOGIN_PROCESS)
  1382.       return(0);
  1383.     else
  1384.       return(ptyint_update_wtmp(&ent, host, userbuf));
  1385. }
  1386. #ifndef WTMP_FILE
  1387. #ifdef _PATH_WTMP
  1388. #define WTMP_FILE _PATH_WTMP
  1389. #endif /* _PATH_WTMP */
  1390. #endif /* WTMP_FILE */
  1391.  
  1392. #ifndef WTMPX_FILE
  1393. #ifdef _PATH_WTMPX
  1394. #ifdef HAVE_UPDWTMPX
  1395. #define WTMPX_FILE _PATH_WTMPX
  1396. #endif /* HAVE_UPDWTMPX */
  1397. #endif /* _PATH_WTMPX */
  1398. #endif /* WTMPX_FILE */
  1399.  
  1400. /* If it is *still* missing, assume /usr/adm/wtmp */
  1401.  
  1402. #ifndef WTMP_FILE
  1403. #define    WTMP_FILE "/usr/adm/wtmp"
  1404. #endif /* WTMP_FILE */
  1405.  
  1406. #ifdef COMMENT
  1407. /* The following test can not be made portably */
  1408.  
  1409. /* #if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) */
  1410. /*
  1411.   This is ugly, but the lack of standardization in the utmp/utmpx space, and
  1412.   what glibc implements and doesn't make available, is even worse.
  1413. */
  1414. /* #undef HAVE_UPDWTMPX */    /* Don't use updwtmpx for glibc 2.1 */
  1415. /* #endif */ /* __GLIBC__ etc */
  1416.  
  1417. #else  /* COMMENT */
  1418.  
  1419. #ifdef __GLIBC__
  1420. #undef HAVE_UPDWTMPX        /* Don't use updwtmpx for glibc period */
  1421. #endif /* __GLIBC__ */
  1422. #endif /* COMMENT */
  1423.  
  1424. long
  1425. ptyint_update_wtmp(ent,host,user) struct utmp *ent; char *host; char *user; {
  1426.     struct utmp ut;
  1427.     struct stat statb;
  1428.     int fd;
  1429.     time_t uttime;
  1430. #ifdef HAVE_UPDWTMPX
  1431.     struct utmpx utx;
  1432.  
  1433.     getutmpx(ent, &utx);
  1434.     if (host)
  1435.       strncpy(utx.ut_host, host, sizeof(utx.ut_host) );
  1436.     else
  1437.       utx.ut_host[0] = 0;
  1438.     if (user)
  1439.       strncpy(utx.ut_user, user, sizeof(utx.ut_user));
  1440.     updwtmpx(WTMPX_FILE, &utx);
  1441. #endif /* HAVE_UPDWTMPX */
  1442.  
  1443. #ifdef HAVE_UPDWTMP
  1444. #ifndef HAVE_UPDWTMPX
  1445.     /* This is already performed byupdwtmpx if present.*/
  1446.     updwtmp(WTMP_FILE, ent);
  1447. #endif /* HAVE_UPDWTMPX*/
  1448. #else /* HAVE_UPDWTMP */
  1449.  
  1450.     if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) >= 0) {
  1451.     if (!fstat(fd, &statb)) {
  1452.         memset((char *)&ut, 0, sizeof(ut));
  1453. #ifdef __hpux
  1454.         strncpy(ut.ut_id, ent->ut_id, sizeof (ut.ut_id));
  1455. #endif /* __hpux */
  1456.         strncpy(ut.ut_line, ent->ut_line, sizeof(ut.ut_line));
  1457.         strncpy(ut.ut_name, ent->ut_name, sizeof(ut.ut_name));
  1458. #ifndef NO_UT_HOST
  1459.         strncpy(ut.ut_host, ent->ut_host, sizeof(ut.ut_host));
  1460. #endif /* NO_UT_HOST */
  1461.  
  1462.         time(&uttime);
  1463.         ut.ut_time = uttime;
  1464.  
  1465. #ifdef HAVE_GETUTENT
  1466. #ifdef USER_PROCESS
  1467.         if (ent->ut_name) {
  1468.         if (!ut.ut_pid)
  1469.           ut.ut_pid = getpid();
  1470. #ifndef __hpux
  1471.         ut.ut_type = USER_PROCESS;
  1472. #else  /* __hpux */
  1473.         ut.ut_type = ent->ut_type;
  1474. #endif /* __hpux */
  1475.  
  1476.         } else {
  1477.  
  1478. #ifdef EMPTY
  1479.         ut.ut_type = EMPTY;
  1480. #else
  1481.         ut.ut_type = DEAD_PROCESS; /* For Linux brokenness*/
  1482. #endif /* EMPTY */
  1483.  
  1484.         }
  1485. #endif /* USER_PROCESS */
  1486. #endif /* HAVE_GETUTENT */
  1487.  
  1488.         if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
  1489.         sizeof(struct utmp))
  1490. #ifndef COHERENT
  1491.           ftruncate(fd, statb.st_size);
  1492. #else
  1493.           chsize(fd, statb.st_size);
  1494. #endif /* COHERENT */
  1495.     }
  1496.     close(fd);
  1497.     }
  1498. #endif /* HAVE_UPDWTMP */
  1499.     return(0); /* no current failure cases; file not found is not failure! */
  1500. }
  1501. #endif /* WANT_UTMP */
  1502.  
  1503. /* This is for ancient Unixes that don't have these tty symbols defined. */
  1504.  
  1505. #ifndef PENDIN
  1506. #define PENDIN ICANON
  1507. #endif /* PENDIN */
  1508. #ifndef FLUSHO
  1509. #define FLUSHO ICANON
  1510. #endif /* FLUSHO */
  1511. #ifndef IMAXBEL
  1512. #define IMAXBEL ICANON
  1513. #endif /* IMAXBEL */
  1514. #ifndef EXTPROC
  1515. #define EXTPROC ICANON
  1516. #endif /* EXTPROC */
  1517.  
  1518. static char Xline[17] = { 0, 0 };
  1519. /*
  1520.   getptyslave()
  1521.   Open the slave side of the pty, and do any initialization that is necessary.
  1522.   The return value fd is a file descriptor for the slave side.
  1523.   fc = function code from do_pty() (q.v.)
  1524. */
  1525. int
  1526. getptyslave(fd, fc) int * fd, fc; {
  1527.     int ttyfd;
  1528.     int t = -1;
  1529.     long retval;
  1530. #ifdef TIOCGWINSZ
  1531.     struct winsize ws;
  1532.     extern int cmd_rows, cmd_cols;
  1533. #endif /* TIOCGWINSZ */
  1534.  
  1535.     ttyfd = *fd;
  1536.     debug(F111,"getptyslave()","ttyfd",ttyfd);
  1537.     /*
  1538.      * Opening the slave side may cause initilization of the
  1539.      * kernel tty structure.  We need remember the state of:
  1540.      *      if linemode was turned on
  1541.      *      terminal window size
  1542.      *      terminal speed
  1543.      * so that we can reset them if we need to.
  1544.      */
  1545.     if ((retval = pty_open_slave(Xline, &t, fc)) != 0) {
  1546.     perror(Xline);
  1547.     msg++;
  1548.         debug(F111,"getptyslave()","Unable to open slave",retval);
  1549.         return(-1);
  1550.     }
  1551.     debug(F111,"getptyslave","t",t);
  1552. #ifdef INIT_SPTY
  1553.     spty = t;
  1554.     debug(F111,"getptyslave","spty",spty);
  1555. #endif /* INIT_SPTY */
  1556. #ifdef STREAMSPTY
  1557.     if (ioctl(t,I_PUSH,"pckt") < 0) {
  1558.         debug(F111,"getptyslave()","ioctl(I_PUSH) failed",errno);
  1559. #ifndef _AIX
  1560.         fatal("I_PUSH pckt");
  1561. #endif /* _AIX */
  1562.     }
  1563. #endif /* STREAMSPTY */
  1564.  
  1565.     /* Set up the tty modes as we like them to be. */
  1566. #ifdef COMMENT
  1567.     /* Originally like this... But this is the master - we want the slave */
  1568.     /* Anyway, this fails on Solaris and probably other System V OS's */
  1569.     init_termbuf(ttyfd);
  1570. #else
  1571.     init_termbuf(t);
  1572. #endif    /* COMMENT */
  1573. #ifdef TIOCGWINSZ
  1574.     if (cmd_rows || cmd_cols) {
  1575.         memset((char *)&ws, 0, sizeof(ws));
  1576.         ws.ws_col = cmd_cols;
  1577.         ws.ws_row = cmd_rows;
  1578.     debug(F101,"getptyslave() doing TIOCSWINSZ...","",t);
  1579.         ioctl(t, TIOCSWINSZ, (char *)&ws);
  1580.     }
  1581. #endif /* TIOCGWINSZ */
  1582.  
  1583.     /* For external protocols, put the pty in no-echo mode */
  1584.     if (fc == 1) {
  1585.     debug(F100,"getptyslave() setting rawmode","",0);
  1586.     /* iflags */
  1587.     termbuf.c_iflag &= ~(PARMRK|ISTRIP|BRKINT|INLCR|IGNCR|ICRNL);
  1588.     termbuf.c_iflag &= ~(INPCK|IGNPAR|IMAXBEL|IXANY|IXON|IXOFF);
  1589.     termbuf.c_iflag |= IGNBRK;
  1590. #ifdef IUCLC
  1591.     termbuf.c_iflag &= ~IUCLC;
  1592. #endif /* IUCLC */
  1593.  
  1594.     /* oflags */
  1595.     termbuf.c_oflag &= ~OPOST;
  1596. #ifdef OXTABS
  1597.     termbuf.c_oflag &= ~OXTABS;
  1598. #endif /* OXTABS */
  1599. #ifdef ONOCR
  1600.     termbuf.c_oflag &= ~ONOCR;
  1601. #endif /* ONOCR */
  1602. #ifdef ONLRET
  1603.     termbuf.c_oflag &= ~ONLRET;
  1604. #endif /* ONLRET */
  1605. #ifdef ONLCR
  1606.     termbuf.c_oflag &= ~ONLCR;
  1607. #endif /* ONLCR */
  1608.  
  1609.     /* lflags */
  1610.     termbuf.c_lflag &= ~ECHO;
  1611. #ifdef ECHOE
  1612.     termbuf.c_lflag &= ~ECHOE;
  1613. #endif /* ECHOE */
  1614. #ifdef ECHONL
  1615.     termbuf.c_lflag &= ~ECHONL;
  1616. #endif /* ECHONL */
  1617. #ifdef ECHOPRT
  1618.     termbuf.c_lflag &= ~ECHOPRT;
  1619. #endif /* ECHOPRT */
  1620. #ifdef ECHOKE
  1621.     termbuf.c_lflag &= ~ECHOKE;
  1622. #endif /* ECHOKE */
  1623. #ifdef ECHOCTL
  1624.     termbuf.c_lflag &= ~ECHOCTL;
  1625. #endif /* ECHOCTL */
  1626. #ifdef ALTWERASE
  1627.     termbuf.c_lflag &= ~ALTWERASE;
  1628. #endif /* ALTWERASE */
  1629. #ifdef EXTPROC
  1630.     termbuf.c_lflag &= ~EXTPROC;
  1631. #endif /* EXTPROC */
  1632.     termbuf.c_lflag &= ~(ICANON|ISIG|IEXTEN|TOSTOP|FLUSHO|PENDIN);
  1633.  
  1634. #ifdef NOKERNINFO
  1635.     termbuf.c_lflag |= NOKERNINFO;
  1636. #endif    /* NOKERNINFO */
  1637.     /* termbuf.c_lflag |= NOFLSH; */
  1638.     termbuf.c_lflag &= ~NOFLSH;
  1639.  
  1640.     /* cflags */
  1641.     termbuf.c_cflag &= ~(CSIZE|PARENB|PARODD);
  1642.     termbuf.c_cflag |= CS8|CREAD;
  1643. #ifdef VMIN
  1644.     termbuf.c_cc[VMIN] = 1;
  1645. #endif    /* VMIN */
  1646.     } else {                /* Regular interactive use */
  1647.     debug(F100,"getptyslave() setting cooked mode","",0);
  1648.  
  1649.     /* Settings for sgtty based systems */
  1650.  
  1651. #ifndef USE_TERMIO
  1652.     termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
  1653. #endif /* USE_TERMIO */
  1654.  
  1655. #ifndef OXTABS
  1656. #define OXTABS 0
  1657. #endif /* OXTABS */
  1658.  
  1659.     /* Settings for UNICOS and HPUX */
  1660.  
  1661. #ifdef CRAY
  1662.     termbuf.c_oflag = OPOST|ONLCR|TAB3;
  1663.     termbuf.c_iflag = IGNPAR|ISTRIP|ICRNL|IXON;
  1664.     termbuf.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;
  1665.     termbuf.c_cflag = EXTB|HUPCL|CS8;
  1666. #else /* CRAY */
  1667. #ifdef HPUX
  1668.     termbuf.c_oflag = OPOST|ONLCR|TAB3;
  1669.     termbuf.c_iflag = IGNPAR|ISTRIP|ICRNL|IXON;
  1670.     termbuf.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;
  1671.     termbuf.c_cflag = EXTB|HUPCL|CS8;
  1672. #else /* HPUX */
  1673. #ifdef USE_TERMIO
  1674.     /*
  1675.       Settings for all other termios/termio based systems, other than 
  1676.       4.4BSD.  In 4.4BSD the kernel does the initial terminal setup.
  1677.     */
  1678. #ifdef BSD42
  1679. #ifndef BSD44
  1680.     termbuf.c_lflag |= ECHO|ICANON|IEXTEN|ISIG;
  1681.     termbuf.c_oflag |= ONLCR|OXTABS|OPOST;
  1682.     termbuf.c_iflag |= ICRNL|IGNPAR;
  1683.     termbuf.c_cflag |= HUPCL;
  1684.     termbuf.c_iflag &= ~IXOFF;
  1685. #endif /* BSD44 */
  1686. #else /* BSD42 */
  1687.     termbuf.c_lflag |= ECHO|ICANON|IEXTEN|ISIG;
  1688.     termbuf.c_oflag |= ONLCR|OXTABS|OPOST;
  1689.     termbuf.c_iflag |= ICRNL|IGNPAR;
  1690.     termbuf.c_cflag |= HUPCL;
  1691.     termbuf.c_iflag &= ~IXOFF;
  1692. #endif /* BSD42 */
  1693. #endif /* USE_TERMIO */
  1694. #endif /* HPUX */
  1695. #endif /* CRAY */
  1696.     }
  1697.  
  1698.     /* Set the tty modes, and make this our controlling tty. */
  1699. #ifdef COMMENT
  1700.     /* But this is the master - we want the slave */
  1701.     set_termbuf(ttyfd);
  1702. #else
  1703.     set_termbuf(t);
  1704. #endif    /* COMMENT */
  1705.  
  1706.     if (t != 0)
  1707.       dup2(t, 0);
  1708.     if (t != 1)
  1709.       dup2(t, 1);
  1710.     if (t != 2) {
  1711.     if (fc == 0) {
  1712.         dup2(t, 2);
  1713.     } else if (fc == 1) {
  1714.         /* For external protocols, send stderr to /dev/null */
  1715. #ifdef COMMENT
  1716.         int xx;
  1717. #ifndef COMMENT
  1718.         char * s = "/dev/null";
  1719.         errno = 0;
  1720.         xx = open(s, O_WRONLY);
  1721. #else
  1722.         char * s = "pty.log";
  1723.         errno = 0;
  1724.         xx = open(s, O_CREAT, 0644);
  1725. #endif /* COMMENT */
  1726.         debug(F111,"getptyslave redirect stderr",s,errno);
  1727.         dup2(xx,2);
  1728. #endif    /* COMMENT */
  1729.     }
  1730.     }
  1731.     if (t > 2)
  1732.       close(t);
  1733.  
  1734.     if (ttyfd > 2) {
  1735.     close(ttyfd);
  1736.         ttyfd = -1;
  1737.     *fd = ttyfd;
  1738.     }
  1739.     return(0);
  1740. }
  1741.  
  1742. #ifdef HAVE_PTYTRAP
  1743. /*
  1744.   To be called to determine if a trap is pending on a pty
  1745.   if and only if select() cannot be used.
  1746. */
  1747. int
  1748. pty_trap_pending(fd) int fd; {
  1749.     int pending;
  1750.     int rc;
  1751.  
  1752.     rc = ioctl(fd, TIOCTRAPSTATUS, (char *)&pending, sizeof(pending));
  1753.     if (rc == 0) {
  1754.         debug(F101,"pty_trap_pending()","",pending);
  1755.         return(pending);
  1756.     } else {
  1757.         debug(F111,"pty_trap_pending()","ioctl() failed",rc);
  1758.         return(-1);
  1759.     }
  1760. }
  1761.  
  1762. /*
  1763.   To be called after select() has returned indicating that an exception is
  1764.   waiting on a pty.  It should be called with the file descriptor of the pty.
  1765.   Returns -1 on error; 0 if pty is still open; 1 if pty has closed.
  1766. */
  1767. int
  1768. pty_trap_handler(fd) int fd; {
  1769.     struct request_info ri;
  1770.  
  1771.     memset(&ri,0,sizeof(ri));
  1772.     if (ioctl(fd,TIOCREQCHECK,(char *)&ri, sizeof(ri)) != 0) {
  1773.         debug(F111,"pty_trap_handler()","ioctl(TIOCREQCHECK) failed",errno);
  1774.         return(-1);
  1775.     }
  1776.     switch (ri.request) {
  1777.       case TIOCOPEN:
  1778.         debug(F110,"pty_trap_handler()","an open() call",0);
  1779.         break;
  1780.       case TIOCCLOSE:
  1781.         debug(F110,"pty_trap_handler()","a close() call",0);
  1782.         break;
  1783.       default:
  1784.         debug(F110,"pty_trap_handler()","an ioctl() call",0);
  1785.         ri.errno_error = EINVAL;
  1786.     }
  1787.     if (ioctl(fd, TIOCREQSET, (char *)&ri,sizeof(ri)) != 0) {
  1788.         debug(F111,"pty_trap_handler()","ioctl(TIOCREQSET) failed",errno);
  1789.         return(-1);
  1790.     }
  1791.     if (ri.request == TIOCCLOSE)
  1792.       return(1);
  1793.     else
  1794.       return(0);
  1795. }
  1796. #endif /* HAVE_PTYTRAP */
  1797.  
  1798. VOID
  1799. exec_cmd(s) char * s; {
  1800.     struct stringarray * q;
  1801.     char ** args = NULL;
  1802.  
  1803.     if (!s) return;
  1804.     if (!*s) return;
  1805.  
  1806.     q = cksplit(1,0,s,NULL,"\\%[]&$+-/=*^_@!{}/<>|.#~'`:;?",7,0,0);
  1807.     if (!q) return;
  1808.  
  1809.     args = q->a_head + 1;
  1810.  
  1811. #ifdef DEBUG    
  1812.     {
  1813.     int i, n;
  1814.     n = q->a_size;
  1815.     for (i = 0; i <= n; i++) {
  1816.         if (!args[i]) {
  1817.         debug(F111,"exec_cmd arg","NULL",i);
  1818.         break;
  1819.         } else {
  1820.         debug(F111,"exec_cmd arg",args[i],i);
  1821.         if (i == n && args[i]) {
  1822.             debug(F101,"exec_cmd SUBSTITUTING NULL","",i);
  1823.             if (strlen(args[i]) == 0)
  1824.               makestr(&(args[i]),NULL);
  1825.         }
  1826.  
  1827.         }
  1828.     }        
  1829.     }
  1830. #endif    /* DEBUG */
  1831.  
  1832.     execvp(args[0],args);
  1833. }
  1834.  
  1835. /* Get a pty, scan input lines. */
  1836. /* fc = 0 for interactive access; fc = 1 for running external protocols */
  1837.  
  1838. static int pty_fc = -1;            /* Global copy of fc */
  1839.  
  1840. int
  1841. do_pty(fd, cmd, fc) int * fd; char * cmd; int fc; {
  1842.     long retval;
  1843.     int syncpipe[2];
  1844.     int i, ttyfd;
  1845. #ifdef HAVE_PTYTRAP
  1846.     int x;
  1847. #endif /* HAVE_PTYTRAP */
  1848.  
  1849.     debug(F101,"CKUPTY.C do_pty fc","",fc);
  1850.  
  1851.     ttyfd = *fd;
  1852.  
  1853.     pty_master_fd = -2;
  1854.     pty_slave_fd = -2;
  1855.     pty_fork_pid = -2;
  1856.  
  1857.     msg = 0;                /* Message counter */
  1858.     pty_init();                /* Find an available pty to use. */
  1859.     errno = 0;
  1860.  
  1861.     if ((retval = pty_getpty(&ttyfd, Xline, 20)) != 0) {
  1862.     if (msg++ == 0)
  1863.       perror(Xline);
  1864.         debug(F111,"do_pty()","pty_getpty() fails",retval);
  1865.     *fd = ttyfd;
  1866.         return(-1);
  1867.     }
  1868.     *fd = ttyfd;
  1869.     debug(F111,"do_pty() Xline",Xline,ttyfd);
  1870.  
  1871. #ifdef SIGTTOU
  1872. /*
  1873.   Ignoring SIGTTOU keeps the kernel from blocking us.  we tweak the tty with
  1874.   an ioctl() (in ttioct() in /sys/tty.c in a BSD kernel)
  1875. */
  1876.      signal(SIGTTOU, SIG_IGN);
  1877. #endif /* SIGTTOU */
  1878.  
  1879. /* Start up the command on the slave side of the terminal */
  1880.  
  1881.     if (pipe(syncpipe) < 0) {
  1882.         debug(F110,"do_pty()","pipe() fails",0);
  1883.     perror("pipe() failed");
  1884.     msg++;
  1885.         debug(F111,"do_pty()","pipe fails",errno);
  1886.         return(-1);
  1887.     }
  1888.     if ((i = fork()) < 0) {
  1889.         /* XXX - need to clean up the allocated pty */
  1890.         perror("fork() failed");
  1891.     msg++;
  1892.         debug(F111,"do_pty()","fork fails",errno);
  1893.         return(-1);
  1894.     }
  1895.     if (i) {  /* Wait for child before writing to parent side of pty. */
  1896.         char c;
  1897. #ifdef HAVE_PTYTRAP
  1898.         int on = 1;
  1899. #endif /* HAVE_PTYTRAP */
  1900.     close(syncpipe[1]);
  1901.     errno = 0;
  1902.         if (read(syncpipe[0], &c, 1) == 0) { /* Slave side died */
  1903.         perror("Pipe read() failed");
  1904.         msg++;
  1905.             debug(F110,"do_pty()","Slave fails to initialize",0);
  1906.             close(syncpipe[0]);
  1907.             return(-1);
  1908.         }
  1909.         pty_fork_pid = i;        /* So we can clean it up later */
  1910.     pty_fork_active = 1;
  1911.     debug(F101,"do_pty pty_fork_pid","",pty_fork_pid);
  1912. #ifdef HAVE_PTYTRAP
  1913.         /* HPUX does not allow the master to read end of file.  */
  1914.         /* Therefore, we must determine that the slave has been */
  1915.         /* closed by trapping the call to close().              */
  1916.     errno = 0;
  1917.     x = ioctl(ttyfd, TIOCTRAP, (char *)&on);
  1918.     debug(F111,"do_pty ioctl(TIOCTRAP)",ckitoa(x),errno);
  1919. #endif /* HAVE_PTYTRAP */
  1920.         debug(F111,"do_pty()","synchronized - pty_fork_pid",pty_fork_pid);
  1921.         close(syncpipe[0]);
  1922.     } else {
  1923.     int x;
  1924.     debug(F101,"do_pty getptyslave ttyfd A","",ttyfd);
  1925.         debug(F110,"do_pty()","Slave starts",0);
  1926.     x = getptyslave(&ttyfd,fc);
  1927.     debug(F101,"do_pty getptyslave","",x);
  1928.         if (x == 0) {
  1929.         debug(F101,"do_pty getptyslave ttyfd B","",ttyfd);
  1930. #ifdef WANT_UTMP
  1931.             pty_update_utmp(PTY_USER_PROCESS,
  1932.                 getpid(),
  1933.                 "KERMIT",
  1934.                 Xline,
  1935.                 cmd,
  1936.                 PTY_TTYSLOT_USABLE
  1937.                 );
  1938. #endif /* WANT_UTMP */
  1939.             /* Notify our parent we're ready to continue.*/
  1940.             debug(F110,"do_pty()","slave synchronizing",0);
  1941.             write(syncpipe[1],"y",1);
  1942.             close(syncpipe[0]);
  1943.             close(syncpipe[1]);
  1944.  
  1945.         debug(F110,"do_pty cmd",cmd,"");
  1946.             exec_cmd(cmd);
  1947.             debug(F111,"do_pty()","exec_cmd() returns - why?",errno);
  1948.         }
  1949.     *fd = ttyfd;
  1950.         debug(F110,"do_pty()","getptyslave() fails - exiting",0);
  1951.         exit(1);
  1952.     }
  1953.     *fd = ttyfd;
  1954.     pty_fc = fc;
  1955.     return(getpid());
  1956. } /* end of do_pty() */
  1957.  
  1958.  
  1959. VOID
  1960. end_pty() {
  1961.     msg = 0;                /* Message counter */
  1962.     debug(F101,"end_pty pty_fork_pid","",pty_fork_pid);
  1963.     if (Xline[0] && pty_fork_pid >= 0) {
  1964.         pty_cleanup(Xline,pty_fork_pid,1);
  1965.         Xline[0] = '\0';
  1966.         pty_fork_pid = -1;
  1967.     pty_fork_active = 0;
  1968.     debug(F101,"end_pty pty_fork_active","",pty_fork_active);
  1969.     }
  1970.     pty_fc = -1;
  1971. }
  1972. #endif /* NETPTY */
  1973.