home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1621 / slave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.6 KB  |  101 lines

  1. /* Copyright 1990, Daniel J. Bernstein. All rights reserved. */
  2.  
  3. #include "file.h"
  4. #include "config.h"
  5. #include "pty.h"
  6. #include "tty.h"
  7. #include "err.h"
  8. #include "sig.h"
  9. #include "slave.h"
  10. #include "logs.h"
  11.  
  12. void slave(fnsty,arg)
  13. char *fnsty;
  14. char **arg;
  15. {
  16.  sig_ignore(SIGTTOU);
  17.  sig_ignore(SIGTTIN);
  18.  
  19.  if (fdtty > -1)
  20.   {
  21.    (void) tty_dissoc(fdtty); /* must succeed */
  22.    (void) close(fdtty);
  23.   }
  24.  if (fdre > -1)
  25.    (void) close(fdre);
  26.  (void) close(fdin);
  27.  (void) close(fdout);
  28.  (void) close(fdmty);
  29.  (void) close(fdsty);
  30.  (void) close(0);
  31.  (void) close(1);
  32.  if (flagsameerr < 2)
  33.    (void) close(2);
  34.  if (flagsameerr < 1)
  35.   {
  36.    (void) close(3);
  37.    for (fdout = getdtablesize();fdout > 3;fdout--)
  38.      (void) close(fdout);
  39.   }
  40.  
  41.  if (open(fnsty,O_RDONLY) != 0)
  42.    fatalerrp(1,"pty: fatal: cannot reopen pty for input\n",errno);
  43.  if (open(fnsty,O_WRONLY) != 1)
  44.      fatalerrp(1,"pty: fatal: cannot reopen pty for output\n",errno);
  45.  if (flagsameerr < 2)
  46.    if (open(fnsty,(flagxerrwo ? O_WRONLY : O_RDWR)) != 2)
  47.      fatalerrp(1,"pty: fatal: cannot reopen pty for stderr\n",errno);
  48.  if (flagsameerr < 1)
  49.    if (open("/dev/tty",O_RDWR) != 3)
  50.      fatalerrp(1,"pty: fatal: cannot reopen pty for /dev/tty\n",errno);
  51.  
  52.  if ((fdtty = open("/dev/tty",O_RDWR)) == -1)
  53.    fatalerrp(1,"pty: fatal: cannot reopen pty for temporary /dev/tty\n",errno);
  54.  
  55.  if (setpgrp(0,getpid()) == -1)
  56.    fatalerr(1,"pty: fatal: cannot set process group\n");
  57.  if (tty_setpgrp(fdtty,getpid()) == -1)
  58.    fatalerrp(1,"pty: fatal: cannot set pty process group",errno);
  59.  if (tty_setmodes(fdtty,&tmopty) == -1)
  60.    fatalerr(1,"pty: fatal: cannot set pty modes\n");
  61.  if (flagxexcl)
  62.    if (tty_setexcl(fdtty) == -1)
  63.      fatalerr(1,"pty: fatal: cannot set exclusive use on pty\n");
  64.  
  65.  (void) fchmod(fdtty,USEDPTYMODE);
  66.  
  67.  if (flagxchown)
  68.    (void) fchown(fdtty,uid,PTYGROUP);
  69.  
  70.  (void) close(fdtty);
  71.  
  72.  date = now(); /* could use original date instead */
  73.  
  74.  if (flagxutmp)
  75.   {
  76.    if (flagverbose)
  77.      warnerr2("%s","pty: writing utmp entry\n");
  78.    if (utmp(fnsty + PTYUTMP_OFFSET,username,PTYUTMP_HOST,date) == -1)
  79.      warnerr2("pty: warning: cannot write %s utmp entry",fnsty);
  80.   }
  81.  
  82.  if (flagxwtmp)
  83.   {
  84.    if (flagverbose)
  85.      warnerr2("%s","pty: writing wtmp entry\n");
  86.    if (wtmp(fnsty + PTYWTMP_OFFSET,username,PTYWTMP_HOST,date) == -1)
  87.      warnerr2("pty: warning: cannot write %s wtmp entry",fnsty);
  88.   }
  89.  
  90.  if (setreuid(uid,uid) == -1)
  91.    /* We absolutely refuse to exec while setuid. */
  92.    fatalerrp(1,"pty: fatal: cannot setreuid",errno);
  93.  
  94.  sig_restore();
  95.  if (flagverbose)
  96.    warnerr2("pty: executing program %s\n",arg[0]);
  97.  (void) execvp(arg[0],arg);
  98.  fatalerr2p(1,"pty: fatal: cannot execute %s",arg[0],errno);
  99.  /*NOTREACHED*/
  100. }
  101.