]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/main.c
remove a couple of ansi-C-isms.
[ppp.git] / pppd / main.c
index c3d72a2dae9840b83ce13552721b50f2748267a4..7ad8d3f1d29fd52c683ccdbcd0369bb27ae5976f 100644 (file)
@@ -18,7 +18,7 @@
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: main.c,v 1.59 1999/03/16 02:50:15 paulus Exp $";
+static char rcsid[] = "$Id: main.c,v 1.63 1999/03/19 04:23:40 paulus Exp $";
 #endif
 
 #include <stdio.h>
@@ -88,7 +88,7 @@ int hungup;                   /* terminal has been hung up */
 int privileged;                        /* we're running as real uid root */
 int need_holdoff;              /* need holdoff period before restarting */
 int detached;                  /* have detached from terminal */
-int log_to_stderr;             /* send log messages to stderr too */
+int log_to_fd;                 /* send log messages to this fd too */
 
 static int fd_ppp;             /* fd for talking PPP */
 static int fd_loop;            /* fd for getting demand-dial packets */
@@ -115,11 +115,17 @@ char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n";
 GIDSET_TYPE groups[NGROUPS_MAX];/* groups the user is in */
 int ngroups;                   /* How many groups valid in groups */
 
+static struct timeval start_time;      /* Time when link was started. */
+
+struct ppp_stats link_stats;
+int link_stats_valid;
+
 /* Prototypes for procedures local to this file. */
 
 static void create_pidfile __P((void));
 static void cleanup __P((void));
 static void close_tty __P((void));
+static void hangup_modem __P((int));
 static void get_input __P((void));
 static void calltimeout __P((void));
 static struct timeval *timeleft __P((struct timeval *));
@@ -131,11 +137,13 @@ static void toggle_debug __P((int));
 static void open_ccp __P((int));
 static void bad_signal __P((int));
 static void holdoff_end __P((void *));
-static int device_script __P((char *, int, int));
+static int device_script __P((char *, int));
 static void reap_kids __P((void));
 static void pr_log __P((void *, char *, ...));
 static void logit __P((int, char *, va_list));
 static void vslp_printer __P((void *, char *, ...));
+static void format_packet __P((u_char *, int, void (*) (void *, char *, ...),
+                              void *));
 
 extern char    *ttyname __P((int));
 extern char    *getlogin __P((void));
@@ -187,12 +195,13 @@ main(argc, argv)
     struct protent *protp;
     struct stat statbuf;
     char numbuf[16];
+    struct timeval now;
 
     phase = PHASE_INITIALIZE;
     p = ttyname(0);
     if (p)
-       strlcpy(devnam, sizeof(devnam), p);
-    strlcpy(default_devnam, sizeof(default_devnam), devnam);
+       strlcpy(devnam, p, sizeof(devnam));
+    strlcpy(default_devnam, devnam, sizeof(default_devnam));
 
     script_env = NULL;
 
@@ -275,7 +284,7 @@ main(argc, argv)
        default_device = 1;
     if (default_device)
        nodetach = 1;
-    log_to_stderr = !default_device;
+    log_to_fd = !default_device? 1: -1;        /* default to stdout */
 
     /*
      * Initialize system-dependent stuff and magic number package.
@@ -511,7 +520,7 @@ main(argc, argv)
             */
            set_up_tty(ttyfd, 1);
 
-           if (device_script(connector, ttyfd, ttyfd) < 0) {
+           if (device_script(connector, ttyfd) < 0) {
                error("Connect script failed");
                goto fail;
            }
@@ -538,7 +547,7 @@ main(argc, argv)
 
        /* run welcome script, if any */
        if (welcomer && welcomer[0]) {
-           if (device_script(welcomer, ttyfd, ttyfd) < 0)
+           if (device_script(welcomer, ttyfd) < 0)
                warn("Welcome script failed");
        }
 
@@ -559,6 +568,7 @@ main(argc, argv)
         * incoming events (reply, timeout, etc.).
         */
        notice("Connect: %s <--> %s", ifname, devnam);
+       gettimeofday(&start_time, NULL);
        lcp_lowerup(0);
        lcp_open(0);            /* Start protocol */
        open_ccp_flag = 0;
@@ -576,11 +586,11 @@ main(argc, argv)
            }
            waiting = 0;
            calltimeout();
+           get_input();
            if (kill_link) {
                lcp_close(0, "User request");
                kill_link = 0;
            }
-           get_input();
            if (open_ccp_flag) {
                if (phase == PHASE_NETWORK) {
                    ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
@@ -591,6 +601,19 @@ main(argc, argv)
            reap_kids();        /* Don't leave dead kids lying around */
        }
 
+       /*
+        * Print connect time and statistics.
+        */
+       if (gettimeofday(&now, NULL) >= 0) {
+           int t = now.tv_sec - start_time.tv_sec;
+           t = (t + 5) / 6;    /* now in 1/10ths of minutes */
+           info("Connect time %d.%d minutes", t/10, t%10);
+       }
+       if (link_stats_valid) {
+           info("Send %d bytes, received %d bytes",
+                link_stats.p.ppp_obytes, link_stats.p.ppp_ibytes);
+       }
+
        /*
         * If we may want to bring the link up again, transfer
         * the ppp unit back to the loopback.  Set the
@@ -608,12 +631,14 @@ main(argc, argv)
         */
        if (disconnector && !hungup) {
            set_up_tty(ttyfd, 1);
-           if (device_script(disconnector, ttyfd, ttyfd) < 0) {
+           if (device_script(disconnector, ttyfd) < 0) {
                warn("disconnect script failed");
            } else {
                info("Serial link disconnected.");
            }
        }
+       if (!hungup)
+           lcp_lowerdown(0);
 
     fail:
        if (ttyfd >= 0)
@@ -684,7 +709,7 @@ detach()
        die(1);
     }
     detached = 1;
-    log_to_stderr = 0;
+    log_to_fd = -1;
     pid = getpid();
     /* update pid file if it has been written already */
     if (pidfilename[0])
@@ -802,16 +827,7 @@ get_input()
 
 
 /*
- * quit - Clean up state and exit (with an error indication).
- */
-void 
-quit()
-{
-    die(1);
-}
-
-/*
- * die - like quit, except we can specify an exit status.
+ * die - clean up state and exit with the specified status.
  */
 void
 die(status)
@@ -876,7 +892,8 @@ close_tty()
 /*
  * hangup_modem - hang up the modem by clearing DTR.
  */
-void hangup_modem(ttyfd)
+static void
+hangup_modem(ttyfd)
     int ttyfd;
 {
     setdtr(ttyfd, 0);
@@ -1138,9 +1155,9 @@ bad_signal(sig)
  * serial device.
  */
 static int
-device_script(program, inout)
+device_script(program, inout)
     char *program;
-    int inout;
+    int inout;
 {
     int pid;
     int status;
@@ -1157,25 +1174,16 @@ device_script(program, in, out)
     if (pid == 0) {
        sys_close();
        closelog();
-       if (in == out) {
-           if (in != 0) {
-               dup2(in, 0);
-               close(in);
-           }
-           dup2(0, 1);
-       } else {
-           if (out == 0)
-               out = dup(out);
-           if (in != 0) {
-               dup2(in, 0);
-               close(in);
-           }
-           if (out != 1) {
-               dup2(out, 1);
-               close(out);
-           }
+       if (inout == 2) {
+           /* aargh!!! */
+           int newio = dup(inout);
+           close(inout);
+           inout = newio;
        }
-       if (!nodetach && !updetach) {
+       if (log_to_fd >= 0) {
+           if (log_to_fd != 2)
+               dup2(log_to_fd, 2);
+       } else {
            close(2);
            errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600);
            if (errfd >= 0 && errfd != 2) {
@@ -1183,6 +1191,11 @@ device_script(program, in, out)
                close(errfd);
            }
        }
+       if (inout != 0) {
+           dup2(inout, 0);
+           close(inout);
+       }
+       dup2(0, 1);
        setuid(uid);
        if (getuid() != uid) {
            error("setuid failed");
@@ -1369,7 +1382,7 @@ log_packet(p, len, prefix, level)
     char *prefix;
     int level;
 {
-    strlcpy(line, sizeof(line), prefix);
+    strlcpy(line, prefix, sizeof(line));
     linep = line + strlen(line);
     format_packet(p, len, pr_log, NULL);
     if (linep != line)
@@ -1380,7 +1393,7 @@ log_packet(p, len, prefix, level)
  * format_packet - make a readable representation of a packet,
  * calling `printer(arg, format, ...)' to output it.
  */
-void
+static void
 format_packet(p, len, printer, arg)
     u_char *p;
     int len;
@@ -1389,7 +1402,6 @@ format_packet(p, len, printer, arg)
 {
     int i, n;
     u_short proto;
-    u_char x;
     struct protent *protp;
 
     if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
@@ -1406,16 +1418,25 @@ format_packet(p, len, printer, arg)
            p += n;
            len -= n;
        } else {
-           printer(arg, "[proto=0x%x]", proto);
+           for (i = 0; (protp = protocols[i]) != NULL; ++i)
+               if (proto == (protp->protocol & ~0x8000))
+                   break;
+           if (protp != 0 && protp->data_name != 0) {
+               printer(arg, "[%s data]", protp->data_name);
+               if (len > 8)
+                   printer(arg, "%.8B ...", p);
+               else
+                   printer(arg, "%.*B", len, p);
+               len = 0;
+           } else
+               printer(arg, "[proto=0x%x]", proto);
        }
     }
 
-    for (i = 0; i < len && i < 32; ++i) {
-       GETCHAR(x, p);
-       printer(arg, " %.2x", x);
-    }
-    if (i < len)
-       printer(arg, " ...");
+    if (len > 32)
+       printer(arg, "%.32B ...", p);
+    else
+       printer(arg, "%.*B", len, p);
 }
 
 static void
@@ -1442,7 +1463,7 @@ pr_log __V((void *arg, char *fmt, ...))
        syslog(LOG_DEBUG, "%s", line);
        linep = line;
     }
-    strlcpy(linep, line + sizeof(line) - linep, buf);
+    strlcpy(linep, buf, line + sizeof(line) - linep);
     linep += n;
 }
 
@@ -1881,33 +1902,38 @@ script_unsetenv(var)
  * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer,
  * always leaves destination null-terminated (for len > 0).
  */
-void
-strlcpy(char *dest, size_t len, const char *src)
+size_t
+strlcpy(dest, src, len)
+    char *dest;
+    const char *src;
+    size_t len;
 {
-    if (len == 0)
-       return;
-    if (strlen(src) < len)
-       strcpy(dest, src);
-    else {
-       strncpy(dest, src, len - 1);
-       dest[len-1] = 0;
+    size_t ret = strlen(src);
+
+    if (len != 0) {
+       if (ret < len)
+           strcpy(dest, src);
+       else {
+           strncpy(dest, src, len - 1);
+           dest[len-1] = 0;
+       }
     }
+    return ret;
 }
 
 /*
  * strlcat - like strcat/strncat, doesn't overflow destination buffer,
  * always leaves destination null-terminated (for len > 0).
  */
-void
-strlcat(char *dest, size_t len, const char *src)
+size_t
+strlcat(dest, src, len)
+    char *dest;
+    const char *src;
+    size_t len;
 {
-    size_t dlen;
+    size_t dlen = strlen(dest);
 
-    if (len == 0)
-       return;
-    dlen = strlen(dest);
-    if (dlen < len - 1)
-       strlcpy(dest + dlen, len - dlen, src);
+    return dlen + strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0));
 }
 
 /*
@@ -1921,14 +1947,14 @@ logit(level, fmt, args)
 {
     int n;
     char buf[256];
-    static char nl = '\n';
 
     n = vslprintf(buf, sizeof(buf), fmt, args);
     syslog(level, "%s", buf);
-    if (log_to_stderr) {
-       if (write(2, buf, n) != n
-           || (buf[n-1] != '\n' && write(2, &nl, 1) != 1))
-           log_to_stderr = 0;
+    if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
+       if (buf[n-1] != '\n')
+           buf[n++] = '\n';
+       if (write(log_to_fd, buf, n) != n)
+           log_to_fd = -1;
     }
 }