home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part03 / ptyslave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  2.7 KB  |  131 lines

  1. #include <signal.h>
  2. #include <sys/types.h>
  3. #include <sys/file.h>
  4. #include "fmt.h"
  5. #include "ptyerr.h"
  6. #include "ptyslave.h"
  7. #include "ralloc.h"
  8.  
  9. void slave(fdsty,ext,program,flagxerrwo,flagsameerr,uid,flagverbose,flagxexcl)
  10. int fdsty;
  11. char *ext;
  12. char **program;
  13. int flagxerrwo;
  14. int flagsameerr;
  15. int uid;
  16. int flagverbose;
  17. int flagxexcl;
  18. {
  19.  char ptyext[10];
  20.  int fdout;
  21.  int fdtty;
  22.  char *t;
  23.  
  24.  /* we're already dissociated and reassociated */
  25.  close(0);
  26.  close(1);
  27.  if (flagsameerr < 2)
  28.    close(2);
  29.  if (flagsameerr < 1)
  30.   {
  31.    close(3);
  32.    for (fdout = getdtablesize();fdout > 3;--fdout)
  33.      if (fdout != fdsty)
  34.        close(fdout);
  35.   }
  36.  if (dup(fdsty) != 0)
  37.   {
  38.    warn("fatal","cannot dup slave tty descriptor");
  39.    die(1);
  40.   }
  41.  if (dup(fdsty) != 1)
  42.   {
  43.    warn("fatal","cannot dup slave tty descriptor");
  44.    die(1);
  45.   }
  46.  if (flagsameerr < 2)
  47.    if (dup(fdsty) != 2) /* XXX: what about flagxerrwo? */
  48.     {
  49.      warn("fatal","cannot dup slave tty descriptor");
  50.      die(1);
  51.     }
  52.  if (flagsameerr < 1)
  53.    if (open("/dev/tty",O_RDWR) != 3)
  54.     {
  55.      warn("fatal","cannot open /dev/tty under pseudo-tty");
  56.      die(1);
  57.     }
  58.  close(fdsty);
  59.  
  60.  if ((fdtty = open("/dev/tty",O_RDWR)) == -1)
  61.   {
  62.    warn("fatal","cannot open /dev/tty second time under pseudo-tty");
  63.    die(1);
  64.   }
  65.  if (flagxexcl)
  66.    if (tty_setexcl(0) == -1)
  67.      warn("warning","cannot set exclusive use on pseudo-tty");
  68.  if (setpgrp(0,getpid()) == -1)
  69.   {
  70.    warn("fatal","cannot setpgrp");
  71.    die(1);
  72.   }
  73.  signal(SIGTTOU,SIG_IGN);
  74.  if (tctpgrp(fdtty,getpid()) == -1)
  75.   {
  76.    warn("warning","cannot set pseudo-tty pgrp");
  77.    /* XXX: this always seems to happen on an IRIS. why? */
  78.   }
  79.  signal(SIGTTOU,SIG_DFL);
  80.  /* pty modes are already set */
  81.  /* pty inode protection, including chmod and chown, is done */
  82.  close(fdtty);
  83.  
  84.  /* logs, including utmp and wtmp, are already set up */
  85.  
  86.  /* master's comm file is already set up. end of that race! */
  87.  
  88.  if (setreuid(uid,uid) == -1)
  89.    /* This syscall shouldn't ever fail, so most programs don't check it. */
  90.    /* But we absolutely refuse to exec while setuid. */
  91.   {
  92.    warn("fatal","cannot setreuid");
  93.    die(1);
  94.   }
  95.  
  96.  t = ptyext;
  97.  t += fmt_strncpy(ptyext,"PTY=",0);
  98.  *t++ = ext[0];
  99.  *t++ = ext[1];
  100.  *t = 0;
  101.  if (env_put(ptyext) == -1)
  102.   {
  103.    warn("fatal","cannot set up PTY in environment");
  104.    die(1);
  105.   }
  106.  
  107.  sigsetmask(0); /*XXX: restore exactly? */
  108.  
  109.  if (flagverbose > 1)
  110.    warn("executing program",program[0]);
  111.  execvp(program[0],program);
  112.   
  113.   {
  114.    char *buf;
  115.    buf = ralloc(strlen(program) + 30);
  116.    if (!buf)
  117.      warn("fatal","cannot exec");
  118.    else
  119.     {
  120.      t = buf;
  121.      t += fmt_strncpy(t,"cannot exec ",0);
  122.      t += fmt_strncpy(t,program[0],0);
  123.      *t = 0;
  124.      warn("fatal",buf);
  125.      rfree(buf);
  126.     }
  127.   }
  128.  die(1);
  129.  /*NOTREACHED*/
  130. }
  131.