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

  1. /*
  2.  * callback.h - Common include file for callback system.
  3.  *
  4.  * Copyright 1990 Howard Lee Gayle
  5.  *
  6.  * $Header: callback.h,v 1.6 89/12/28 16:43:44 howard Exp $
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License version 1,
  10.  * as published by the Free Software Foundation.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * Prerequisites: howard/port.h.
  22.  */
  23.  
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include <sys/termios.h>
  28. #include <howard/malf.h>
  29. #include <howard/registers.i>
  30.  
  31. #define ABORTSEC 120 /* Abort connection attempt after this many seconds.*/
  32. #define DROPSEC   10 /* Time to leave line hung up (seconds).*/
  33.  
  34. typedef struct /* One entry in the modem reply code table mrctab[].*/
  35.    {
  36.    bStrT mrcStr;   /* Prefix of string from modem.*/
  37.    boolT mrcRetry; /* True iff retry on this string.*/
  38.    } mrcT;
  39.  
  40. typedef struct /* Contents of a state file. */
  41.    {
  42.    ulongT stCflag;   /* c_cflag as set by getty(8).*/
  43.    char   stName[9]; /* Login name.*/
  44.    } stateT;
  45.  
  46. /* Full path names of tty-specific files.  A suffix is added by initTty().*/
  47. PRIVATE char ctlfn  [MFILE] = "/etc/local/callback/control/ttyd"; /* Control.*/
  48. PRIVATE char lfn    [MFILE] = "/etc/local/callback/log/ttyd";     /* Log.*/
  49. PRIVATE char statefn[MFILE] = "/etc/local/callback/state/ttyd";   /* State.*/
  50. PRIVATE char cun    [MFILE] = "/dev/cua"; /* Dial-out device.*/
  51. PRIVATE char tn     [MFILE] = "/dev/ttyd"; /* TTY name.*/
  52.  
  53. /* Executable of getty that execs callback0 instead of login: */
  54. PRIVATE char getty[] = "/usr/local/free/callback/0/bin/getty-cb";
  55.  
  56. PRIVATE char    login[] = "/bin/login"; /* Login executable.*/
  57. PRIVATE streamT ls;                     /* Log file stream.*/
  58.  
  59. PRIVATE mrcT mrctab[] = /* Modem reply code table.*/
  60.    {
  61.    S("BUSY"),        TRUE,
  62.    S("CONNECT"),     FALSE,
  63.    S("NO ANSWER"),   TRUE,
  64.    S("NO CARRIER"),  TRUE,
  65.    S("NO DIALTONE"), TRUE,
  66.    NULBSTR,          FALSE,
  67.    };
  68.  
  69. /* User messages: */
  70. PRIVATE char umBye[] = "BYE bye\n"; /* Correct symbolic phone #.*/
  71. PRIVATE char umNo [] = "NO no\n";   /* Unknown symbolic phone #.*/
  72.  
  73. PRIVATE cStrT speeds[] = /* Line speed as string.*/
  74.    {
  75.    "0",
  76.    "50",
  77.    "75",
  78.    "110",
  79.    "134.5",
  80.    "150",
  81.    "200",
  82.    "300",
  83.    "600",
  84.    "1200",
  85.    "1800",
  86.    "2400",
  87.    "4800",
  88.    "9600",
  89.    "19200",
  90.    "38400"
  91.    };
  92.  
  93. extern long time(); /* (3C).*/
  94.  
  95. PRIVATE void gts();
  96. PRIVATE void initLog();
  97. PRIVATE void initTty();
  98. PRIVATE void logArgs();
  99. PRIVATE void logMMSS();
  100. PRIVATE void sts();
  101.  
  102. /* gts - get tty status */
  103.  
  104. PRIVATE void gts (tp)
  105. R1 struct termios *tp; /* Points to place to store result.*/
  106.  
  107. /* Function:
  108.  *    Call the TCGETS ioctl.  Log the results and return them.
  109.  * Algorithm:
  110.  *    
  111.  * Notes:
  112.  *    
  113.  */
  114. {
  115. int i;   /* Process group.*/
  116. int mcl; /* Modem control lines.*/
  117.  
  118. logMMSS();
  119. if (-1 == ioctl (0, TCGETS, tp)) malf1 ("TCGETS failed");
  120. if (-1 == ioctl (0, TIOCMGET, &mcl)) malf1 ("TIOCMGET failed");
  121. if (-1 == ioctl (0, TIOCGPGRP, &i)) malf1 ("TIOCGPGRP failed");
  122. FPRINTF (ls, "i: %5lo o: %6lo c: %7lo l: %6lo line: %d m: %3o pgrp: %5d\n",
  123.          tp->c_iflag, tp->c_oflag, tp->c_cflag, tp->c_lflag, tp->c_line,
  124.          mcl, i);
  125. FFLUSH (ls);
  126. }
  127.  
  128. /* initLog - start logging */
  129.  
  130. PRIVATE void initLog (cn)
  131. bStrT cn; /* Command name.*/
  132.  
  133. /* Function:
  134.  *    Open log file.  Write initial message.  Make standard error
  135.  *    go to log file.
  136.  * Algorithm:
  137.  *    Open the log file for appending to the end, and exit on error.
  138.  *    Print an initial message.  Flush.  Close file descriptor 2,
  139.  *    then dup the log file's descriptor to be file descriptor 2.
  140.  *    Test malf0().  Set the close-on-exec bit.
  141.  * Notes:
  142.  *    1) Once stderr corresponds to the log file, malf can be used.
  143.  *    2) The close-on-exec bit is set on the file descriptor from
  144.  *       the original open on the log file, but not on file descriptor 2.
  145.  */
  146. {
  147.    long       ut;  /* Current system time.*/
  148. R1 struct tm *tmp; /* Returned by localtime().*/
  149.  
  150. ls = fopen (lfn, "a+");
  151. if (NULSTRM == ls) exit (2);
  152. ut = time ((long *) NULL);
  153. tmp = localtime (&ut);
  154. FPRINTF (ls,
  155.          "\n\f\n%d-%02d-%02d %02d:%02d:%02d %s pid: %d pgrp: %d\n",
  156.          1900 + tmp->tm_year, 1 + tmp->tm_mon, tmp->tm_mday,
  157.          tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
  158.          cn, getpid(), getpgrp (0));
  159. FFLUSH (ls);
  160. if (close (2))
  161.    {
  162.    FPRINTF (ls, "Can not close 2");
  163.    exit (1);
  164.    }
  165. if (2 != dup (fileno (ls)))
  166.    {
  167.    FPRINTF (ls, "Can not dup %d", fileno (ls));
  168.    exit (1);
  169.    }
  170. errno = 0;
  171. malf0 ("stderr OK");
  172. if (-1 == fcntl (fileno (ls), F_SETFD, 1)) malf1 ("Can not SETFD");
  173. }
  174.  
  175. /* initTty - initialize tty-dependent file names */
  176.  
  177. PRIVATE void initTty (pfx, ftn)
  178. bStrT pfx; /* Expected prefix of tty name.*/
  179. bStrT ftn; /* Full tty name.*/
  180.  
  181. /* Function:
  182.  *    Append argument to the tty-dependent file names.
  183.  * Algorithm:
  184.  *    
  185.  * Notes:
  186.  *    1) There is no checking for overflow.
  187.  */
  188. {
  189. R1 bStrT s = prefix (pfx, ftn); /* Suffix of tty name.*/
  190.  
  191. if ((NULBSTR == s) || (EOS == B(*s))) exit (8);
  192. STRCAT (ctlfn,   (cStrT) s);
  193. STRCAT (cun,     (cStrT) s);
  194. STRCAT (lfn,     (cStrT) s);
  195. STRCAT (statefn, (cStrT) s);
  196. STRCAT (tn,      (cStrT) s);
  197. }
  198.  
  199. /* logArgs - write vector of command arguments to log file */
  200.  
  201. PRIVATE void logArgs (cn, v)
  202. bStrT  cn; /* Command name.*/
  203. bStrT *v;  /* Argument vector.*/
  204.  
  205. /* Function:
  206.  *    Write command name followed by arguments to log file.
  207.  * Algorithm:
  208.  *    
  209.  * Notes:
  210.  *    
  211.  */
  212. {
  213. FPUTS (cn, ls);
  214. for (; NULBSTR != *v; ++v)
  215.    {
  216.    PUTC (' ', ls);
  217.    FPUTS (*v, ls);
  218.    }
  219. PUTC ('\n', ls);
  220. FFLUSH (ls);
  221. }
  222.  
  223. /* logMMSS - log minutes & seconds */
  224.  
  225. PRIVATE void logMMSS()
  226.  
  227. /* Function:
  228.  *    Write current minutes and seconds to log file.
  229.  * Algorithm:
  230.  *    
  231.  * Notes:
  232.  *    1) This function is typically called to start each line written
  233.  *       to the log file.  The initial log message gives the complete
  234.  *       date and time, so it's only necessary to log minutes & seconds.
  235.  */
  236. {
  237.    long       ut;  /* Current system time.*/
  238. R1 struct tm *tmp; /* Returned by localtime().*/
  239.  
  240. ut = time ((long *) NULL);
  241. tmp = localtime (&ut);
  242. FPRINTF (ls, "%02d%02d ", tmp->tm_min, tmp->tm_sec);
  243. }
  244.  
  245. /* sts - set tty status */
  246.  
  247. PRIVATE void sts (tp)
  248. struct termios *tp;
  249.  
  250. /* Function:
  251.  *    Call TCSETS ioctl.  Flush.  Call gts().
  252.  * Algorithm:
  253.  *    
  254.  * Notes:
  255.  *    1) Calling gts() at the end verifies and logs what was set.
  256.  */
  257. {
  258. if (-1 == ioctl (0, TCSETS, tp)) malf1 ("TCSETS failed");
  259. if (-1 == ioctl (0, TCFLSH, 2)) malf1 ("TCFLSH failed");
  260. gts (tp);
  261. }
  262.