home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 239.lha / Unix2src.shar / dnet / internal.c < prev    next >
C/C++ Source or Header  |  1989-05-02  |  4KB  |  219 lines

  1.  
  2. /*
  3.  *  INTERNAL.C
  4.  *
  5.  *    DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved
  6.  *
  7.  *    Usually SCMD_OPEN requests attempt to connect() to the UNIX
  8.  *    domain socket of the server.  However, some 'ports' are designated
  9.  *    as internal to DNET.  They reside here.
  10.  *
  11.  *    -IALPHATERM
  12.  */
  13.  
  14. #include "dnet.h"
  15. #include "../server/servers.h"
  16. #include <sys/stat.h>
  17. #include <sys/wait.h>
  18. #include <sys/resource.h>
  19. #include <sys/uio.h>
  20. #include <strings.h>
  21.  
  22. extern char *getenv();
  23.  
  24. isinternalport(port)
  25. uword port;
  26. {
  27.     if (port == PORT_IALPHATERM)
  28.     return(1);
  29.     return(0);
  30. }
  31.  
  32. iconnect(ps, port)
  33. int *ps;
  34. uword port;
  35. {
  36.     if (port == PORT_IALPHATERM)
  37.     return(ialphaterm_connect(ps, port));
  38.     return(-1);
  39. }
  40.  
  41. ialphaterm_connect(pmaster)
  42. int *pmaster;
  43. {
  44.     struct sgttyb sg;
  45.     struct tchars tc;
  46.     struct ltchars ltc;
  47. #ifdef TIOCGSIZE
  48.     struct ttysize ts;
  49. #else
  50. #ifdef TIOCGWINSZ
  51.     struct winsize ws;
  52. #endif
  53. #endif
  54.     int lmode;
  55.     int fdmaster;
  56.     int fdslave;
  57.     int pid;
  58.     char *slavename;
  59.  
  60.     ioctl(0, TIOCGETP, (char *)&sg);
  61.     ioctl(0, TIOCGETC, (char *)&tc);
  62.     ioctl(0, TIOCGLTC, (char *)<c);
  63.     ioctl(0, TIOCLGET, (char *)&lmode);
  64. #ifdef TIOCGSIZE
  65.     ioctl(0, TIOCGSIZE, &ts);
  66. #else
  67. #ifdef TIOCGWINSZ
  68.     ioctl(0, TIOCGWINSZ, &ws);
  69. #endif
  70. #endif
  71.  
  72.     sg.sg_flags &= ~(RAW);
  73.     sg.sg_flags |= ECHO;
  74. #ifdef TIOCGSIZE
  75.     ts.ts_lines = 23;
  76.     ts.ts_cols = 77;
  77. #else
  78. #ifdef TIOCGWINSZ
  79.     ws.ws_row = 23;
  80.     ws.ws_col = 77;
  81. #endif
  82. #endif
  83.  
  84.     if (DDebug)
  85.     fprintf(stderr, "PTY openning internal pty\n");
  86.     if (openpty(&fdmaster, &fdslave, &slavename) >= 0) {
  87.     if (DDebug)
  88.         fprintf(stderr, "PTY open successfull\n");
  89.     if ((pid = fork()) == NULL) {
  90.         int i;
  91.         setenv("DNET=", "IALPHATERM");
  92.         setuid(getuid());
  93.         signal(SIGHUP, SIG_DFL);
  94.         signal(SIGINT, SIG_DFL);
  95.         signal(SIGQUIT, SIG_DFL);
  96.         signal(SIGTERM, SIG_DFL);
  97.         signal(SIGCHLD, SIG_DFL);
  98.         signal(SIGTSTP, SIG_IGN);
  99.         ioctl(open("/dev/tty", 2), TIOCNOTTY, NULL);
  100.         close(open(slavename, 0));
  101.         dup2(fdslave, 0);
  102.         dup2(0, 1);
  103.         dup2(0, 2);
  104.         for (i = 3; i < 256; ++i)
  105.         close(i);
  106.         ioctl(0, TIOCSETN, &sg);
  107.         ioctl(0, TIOCSETC, &tc);
  108.         ioctl(0, TIOCSLTC, <c);
  109.         ioctl(0, TIOCLSET, &lmode);
  110. #ifdef TIOCSSIZE
  111.             ioctl(0, TIOCSSIZE, &ts);
  112. #else
  113. #ifdef TIOCSWINSZ
  114.             ioctl(0, TIOCSWINSZ, &ws);
  115. #endif
  116. #endif
  117.         {
  118.         char *shell = getenv("SHELL");
  119.         char *home = getenv("HOME");
  120.         if (!shell)
  121.             shell = "/bin/sh";
  122.         if (!home)
  123.             home = ".";
  124.         chdir(home);
  125.         execl(shell, "-fshell", NULL);
  126.         perror(shell);
  127.         }
  128.         _exit(1);
  129.     }
  130.     if (pid > 0) {
  131.         *pmaster = fdmaster;
  132.         close(fdslave);
  133.         if (DDebug)
  134.                 fprintf(stderr, "PTY OPEN OK.2\n");
  135.         return(1);
  136.     }
  137.     close(fdmaster);
  138.     close(fdslave);
  139.     if (DDebug)
  140.             fprintf(stderr, "PTY OPEN FAILURE.1\n");
  141.     }
  142.     if (DDebug)
  143.         fprintf(stderr, "PTY OPEN FAILURE.2\n");
  144.     return(-1);
  145. }
  146.  
  147. openpty(pfdm, pfds, pnames)
  148. int *pfdm;
  149. int *pfds;
  150. char **pnames;
  151. {
  152.     static char ptcs[] = { "0123456789abcdef" };
  153.     static char plate[] = { "/dev/ptyxx" };
  154.     struct stat stat;
  155.     int i;
  156.     int j;
  157.  
  158.     for (i = 'p';; ++i) {
  159.     plate[8] = i;
  160.     plate[9] = ptcs[0];
  161.     if (lstat(plate, &stat) < 0)
  162.         break;
  163.     for (j = 0; ptcs[j]; ++j) {
  164.         plate[9] = ptcs[j];
  165.         plate[5] = 'p';
  166.         if ((*pfdm = open(plate, O_RDWR)) >= 0) {
  167.         plate[5] = 't';
  168.         if ((*pfds = open(plate, O_RDWR)) >= 0) {
  169.             *pnames = plate;
  170.             if (DDebug)
  171.             fprintf(stderr, "PTY FOUND %s\n", *pnames);
  172.             return(1);
  173.         }
  174.         close(*pfdm);
  175.         }
  176.     }
  177.     }
  178.     return(-1);
  179. }
  180.  
  181. isetrows(fd, rows)
  182. {
  183. #ifdef TIOCSSIZE
  184.     struct ttysize ts;
  185.     if (ioctl(fd, TIOCGSIZE, &ts) >= 0) {
  186.     ts.ts_lines = rows;
  187.     ioctl(fd, TIOCSSIZE, &ts);
  188.     }
  189. #else
  190. #ifdef TIOCSWINSZ
  191.     struct winsize ws;
  192.     if (ioctl(fd, TIOCGWINSZ, &ws) >= 0) {
  193.     ws.ws_row = rows;
  194.     ioctl(fd, TIOCSWINSZ, &ws);
  195.     }
  196. #endif
  197. #endif
  198. }
  199.  
  200. isetcols(fd, cols)
  201. {
  202. #ifdef TIOCSSIZE
  203.     struct ttysize ts;
  204.     if (ioctl(fd, TIOCGSIZE, &ts) >= 0) {
  205.     ts.ts_cols = cols;
  206.     ioctl(fd, TIOCSSIZE, &ts);
  207.     }
  208. #else
  209. #ifdef TIOCSWINSZ
  210.     struct winsize ws;
  211.     if (ioctl(fd, TIOCSWINSZ, &ws) >= 0) {
  212.     ws.ws_col = cols;
  213.     ioctl(fd, TIOCSWINSZ, &ws);
  214.     }
  215. #endif
  216. #endif
  217. }
  218.  
  219.