home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume10 / callback_hg / part01 / callback.c next >
Encoding:
C/C++ Source or Header  |  1990-01-06  |  3.7 KB  |  138 lines

  1. /*
  2.  * callback - forked from init process for dialup lines
  3.  */
  4.  
  5. #ifndef lint
  6. static char _cpyrgt[] = "Copyright 1990 Howard Lee Gayle";
  7. #endif lint
  8.  
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License version 1,
  12.  * as published by the Free Software Foundation.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <howard/port.h>
  26. #include <howard/version.h>
  27. #include <howard/usage.h>
  28.  
  29. MAINVER ("@(#)$Header: callback.c,v 1.11 89/12/28 10:34:24 howard Exp $");
  30. USAGE ("speed ttyd*");
  31.  
  32. #include <fcntl.h>
  33. #include "callback.h"
  34.  
  35. /* close01 - close fd 0 & 1 & report errors */
  36.  
  37. PRIVATE void close01()
  38.  
  39. /* Function:
  40.  *    Close standard input and standard output.  Call malf1() on error.
  41.  * Algorithm:
  42.  *    
  43.  * Notes:
  44.  *    
  45.  */
  46. {
  47. if (close (0)) malf1 ("Can not close 0");
  48. if (close (1)) malf1 ("Can not close 1");
  49. }
  50.  
  51. /* main - main function                            */
  52.  
  53. PUBLIC int main (argc, argv)
  54.    int    argc; /* Number of arguments.*/
  55. R1 bStrT *argv; /* Points to array of argument strings.*/
  56.  
  57. /* Function:
  58.  *    Exec either the special getty or login.
  59.  * Algorithm:
  60.  *    Set real & effective group IDs to 0.  Close standard input,
  61.  *    output, and error.  Get suffix of ttyd* argument and call
  62.  *    initTty() to append it to the tty-dependent file names.
  63.  *    Open log file and make standard error go to it.  Write
  64.  *    initial log file message.  Try to open state file.  If there
  65.  *    is no state file, exec the special getty.  But if there is
  66.  *    a state file, read it in and remove it.  Open standard input
  67.  *    and output as the tty.  Set the tty to the
  68.  *    correct modes for login.  Make standard error correspond to
  69.  *    the tty.  Exec login.
  70.  * Notes:
  71.  *    
  72.  */
  73.  
  74. {
  75. R2 streamT         sis; /* State file input stream.*/
  76.    stateT          st;  /* State file contents.*/
  77.    struct termios  ts;  /* TTY status.*/
  78.  
  79. if (-1 == setregid (0, 0)) exit (3);
  80. (void) close (0);
  81. (void) close (1);
  82. (void) close (2);
  83. if (argc < 3) exit (7);
  84. initTty (S("ttyd"), argv[2]);
  85. if (0 != open (lfn, O_APPEND | O_CREAT, 0640)) exit (4);
  86. if (1 != dup (0)) exit (5);
  87. if (2 != dup (0)) exit (6);
  88. initLog ("callback");
  89. sis = fopen (statefn, "r");
  90. if (NULSTRM == sis)
  91.    {
  92.    FPRINTF (ls, "No %s\n", statefn);
  93.    FFLUSH (ls);
  94.    close01();
  95.    if (fclose (stderr))
  96.       {
  97.       FPRINTF (ls, "Can not close stderr\n");
  98.       exit (1);
  99.       }
  100.    logArgs ((bStrT) getty, argv);
  101.    execv (getty, argv);
  102.    FPRINTF (ls, "%s: can not exec\n", getty);
  103.    exit (1);
  104.    }
  105. if ((1 != fread ((cStrT) &st, sizeof (stateT), 1, sis)) || ferror (sis))
  106.    malf1 ("%s: Read error", statefn);
  107. mfclose (sis, statefn);
  108. if (unlink (statefn)) malf1 ("%s: Can not unlink", statefn);
  109. FPRINTF (ls, "Rm %s\n", statefn);
  110. FFLUSH (ls);
  111. close01();
  112. if (0 != open (tn, O_RDWR)) malf1 ("%s: Can not open read/write", tn);
  113. if (1 != dup (0)) malf1 ("Can not dup 1");
  114. gts (&ts);
  115. ts.c_iflag = (IGNPAR | IMAXBEL);
  116. ts.c_cflag = st.stCflag;
  117. ts.c_lflag |= (ECHO | ECHOE | ECHOK | TOSTOP | ECHOCTL | ECHOKE);
  118. sts (&ts);
  119. if (fclose (stderr))
  120.    {
  121.    FPRINTF (ls, "Can not close stderr\n");
  122.    exit (1);
  123.    }
  124. if (2 != dup (0))
  125.    {
  126.    FPRINTF (ls, "Can not dup 2\n");
  127.    exit (1);
  128.    }
  129. FPRINTF (ls, "%s -p %s\n", login, st.stName);
  130. FFLUSH (ls);
  131. execl (login, "login", "-p", st.stName, NULCSTR);
  132. FPRINTF (ls, "%s: can not exec\n", login);
  133. exit (1);
  134. #ifdef lint
  135. return (SUCCESS);
  136. #endif
  137. }
  138.