]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/main.c
add handle_events() function
[ppp.git] / pppd / main.c
index 3d4c16acbad4db134f3ed5ea76eee7174327454d..e03b3621c94d3cd462015cf08d314766036c5487 100644 (file)
@@ -17,7 +17,7 @@
  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
-#define RCSID  "$Id: main.c,v 1.99 2000/06/30 04:54:20 paulus Exp $"
+#define RCSID  "$Id: main.c,v 1.102 2001/02/22 03:10:57 paulus Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
@@ -89,7 +89,6 @@ 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 */
-struct stat devstat;           /* result of stat() on devnam */
 volatile int status;           /* exit status for pppd */
 int unsuccess;                 /* # unsuccessful connection attempts */
 int do_callback;               /* != 0 if we should do callback next */
@@ -108,6 +107,10 @@ static int fd_loop;                /* fd for getting demand-dial packets */
 int phase;                     /* where the link is at */
 int kill_link;
 int open_ccp_flag;
+int listen_time;
+int got_sigusr2;
+int got_sigterm;
+int got_sighup;
 
 static int waiting;
 static sigjmp_buf sigjmp;
@@ -170,6 +173,7 @@ static void update_db_entry __P((void));
 static void add_db_key __P((const char *));
 static void delete_db_key __P((const char *));
 static void cleanup_db __P((void));
+static void handle_events __P((void));
 
 extern char    *ttyname __P((int));
 extern char    *getlogin __P((void));
@@ -222,13 +226,10 @@ main(argc, argv)
     int argc;
     char *argv[];
 {
-    int i, fdflags, t;
+    int i, t;
     char *p;
     struct passwd *pw;
-    struct timeval timo;
-    sigset_t mask;
     struct protent *protp;
-    struct stat statbuf;
     char numbuf[16];
 
     new_phase(PHASE_INITIALIZE);
@@ -277,40 +278,25 @@ main(argc, argv)
      */
     for (i = 0; (protp = protocols[i]) != NULL; ++i)
         (*protp->init)(0);
+    tty_init();
 
     progname = *argv;
 
     /*
      * Parse, in order, the system options file, the user's options file,
-     * the tty's options file, and the command line arguments.
+     * and the command line arguments.
      */
     if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1)
        || !options_from_user()
        || !parse_args(argc-1, argv+1))
        exit(EXIT_OPTION_ERROR);
+    devnam_fixed = 1;          /* can no longer change device name */
 
     /*
-     * Work out the device name, if it hasn't already been specified.
-     */
-    using_pty = notty || ptycommand != NULL || pty_socket != NULL;
-    if (!using_pty && default_device) {
-       char *p;
-       if (!isatty(0) || (p = ttyname(0)) == NULL) {
-           option_error("no device specified and stdin is not a tty");
-           exit(EXIT_OPTION_ERROR);
-       }
-       strlcpy(devnam, p, sizeof(devnam));
-       if (stat(devnam, &devstat) < 0)
-           fatal("Couldn't stat default device %s: %m", devnam);
-    }
-
-    /*
-     * Parse the tty options file.
-     * The per-tty options file should not change
-     * ptycommand, pty_socket, notty or devnam.
+     * Work out the device name, if it hasn't already been specified,
+     * and parse the tty's options file.
      */
-    if (!using_pty && !options_for_tty())
-       exit(EXIT_OPTION_ERROR);
+    tty_device_check();
 
     /*
      * Check that we are running as root.
@@ -322,7 +308,7 @@ main(argc, argv)
     }
 
     if (!ppp_available()) {
-       option_error(no_ppp_msg);
+       option_error("%s", no_ppp_msg);
        exit(EXIT_NO_KERNEL_SUPPORT);
     }
 
@@ -338,58 +324,7 @@ main(argc, argv)
     for (i = 0; (protp = protocols[i]) != NULL; ++i)
        if (protp->check_options != NULL)
            (*protp->check_options)();
-    if (demand && connect_script == 0) {
-       option_error("connect script is required for demand-dialling\n");
-       exit(EXIT_OPTION_ERROR);
-    }
-    /* default holdoff to 0 if no connect script has been given */
-    if (connect_script == 0 && !holdoff_specified)
-       holdoff = 0;
-
-    if (using_pty) {
-       if (!default_device) {
-           option_error("%s option precludes specifying device name",
-                        notty? "notty": "pty");
-           exit(EXIT_OPTION_ERROR);
-       }
-       if (ptycommand != NULL && notty) {
-           option_error("pty option is incompatible with notty option");
-           exit(EXIT_OPTION_ERROR);
-       }
-       if (pty_socket != NULL && (ptycommand != NULL || notty)) {
-           option_error("socket option is incompatible with pty and notty");
-           exit(EXIT_OPTION_ERROR);
-       }
-       default_device = notty;
-       lockflag = 0;
-       modem = 0;
-       if (notty && log_to_fd <= 1)
-           log_to_fd = -1;
-    } else {
-       /*
-        * If the user has specified a device which is the same as
-        * the one on stdin, pretend they didn't specify any.
-        * If the device is already open read/write on stdin,
-        * we assume we don't need to lock it, and we can open it as root.
-        */
-       if (fstat(0, &statbuf) >= 0 && S_ISCHR(statbuf.st_mode)
-           && statbuf.st_rdev == devstat.st_rdev) {
-           default_device = 1;
-           fdflags = fcntl(0, F_GETFL);
-           if (fdflags != -1 && (fdflags & O_ACCMODE) == O_RDWR)
-               privopen = 1;
-       }
-    }
-    if (default_device)
-       nodetach = 1;
-
-    /*
-     * Don't send log messages to the serial port, it tends to
-     * confuse the peer. :-)
-     */
-    if (log_to_fd >= 0 && fstat(log_to_fd, &statbuf) >= 0
-       && S_ISCHR(statbuf.st_mode) && statbuf.st_rdev == devstat.st_rdev)
-       log_to_fd = -1;
+    tty_check_options();
 
     /*
      * Initialize system-dependent stuff.
@@ -460,6 +395,7 @@ main(argc, argv)
     do_callback = 0;
     for (;;) {
 
+       listen_time = 0;
        need_holdoff = 1;
        devfd = -1;
        status = EXIT_OK;
@@ -471,32 +407,15 @@ main(argc, argv)
            /*
             * Don't do anything until we see some activity.
             */
-           kill_link = 0;
            new_phase(PHASE_DORMANT);
            demand_unblock();
            add_fd(fd_loop);
            for (;;) {
-               if (sigsetjmp(sigjmp, 1) == 0) {
-                   sigprocmask(SIG_BLOCK, &mask, NULL);
-                   if (kill_link || got_sigchld) {
-                       sigprocmask(SIG_UNBLOCK, &mask, NULL);
-                   } else {
-                       waiting = 1;
-                       sigprocmask(SIG_UNBLOCK, &mask, NULL);
-                       wait_input(timeleft(&timo));
-                   }
-               }
-               waiting = 0;
-               calltimeout();
-               if (kill_link) {
-                   if (!persist)
-                       break;
-                   kill_link = 0;
-               }
+               handle_events();
+               if (kill_link && !persist)
+                   break;
                if (get_loop_output())
                    break;
-               if (got_sigchld)
-                   reap_kids(0);
            }
            remove_fd(fd_loop);
            if (kill_link && !persist)
@@ -540,52 +459,21 @@ main(argc, argv)
        script_unsetenv("BYTES_RCVD");
        lcp_lowerup(0);
 
-       /*
-        * If we are initiating this connection, wait for a short
-        * time for something from the peer.  This can avoid bouncing
-        * our packets off his tty before he has it set up.
-        */
        add_fd(fd_ppp);
-#ifdef XXX
-       if (connect_delay != 0 && (connector != NULL || ptycommand != NULL)) {
-           struct timeval t;
-           t.tv_sec = connect_delay / 1000;
-           t.tv_usec = connect_delay % 1000;
-           wait_input(&t);
-       }
-#endif
-
        lcp_open(0);            /* Start protocol */
-       open_ccp_flag = 0;
        status = EXIT_NEGOTIATION_FAILED;
        new_phase(PHASE_ESTABLISH);
        while (phase != PHASE_DEAD) {
-           if (sigsetjmp(sigjmp, 1) == 0) {
-               sigprocmask(SIG_BLOCK, &mask, NULL);
-               if (kill_link || open_ccp_flag || got_sigchld) {
-                   sigprocmask(SIG_UNBLOCK, &mask, NULL);
-               } else {
-                   waiting = 1;
-                   sigprocmask(SIG_UNBLOCK, &mask, NULL);
-                   wait_input(timeleft(&timo));
-               }
-           }
-           waiting = 0;
-           calltimeout();
+           handle_events();
            get_input();
-           if (kill_link) {
+           if (kill_link)
                lcp_close(0, "User request");
-               kill_link = 0;
-           }
            if (open_ccp_flag) {
                if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) {
                    ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
                    (*ccp_protent.open)(0);
                }
-               open_ccp_flag = 0;
            }
-           if (got_sigchld)
-               reap_kids(0);   /* Don't leave dead kids lying around */
        }
 
        /*
@@ -647,7 +535,6 @@ main(argc, argv)
        if (!persist || (maxfail > 0 && unsuccess >= maxfail))
            break;
 
-       kill_link = 0;
        if (demand)
            demand_discard();
        t = need_holdoff? holdoff: 0;
@@ -657,24 +544,9 @@ main(argc, argv)
            new_phase(PHASE_HOLDOFF);
            TIMEOUT(holdoff_end, NULL, t);
            do {
-               if (sigsetjmp(sigjmp, 1) == 0) {
-                   sigprocmask(SIG_BLOCK, &mask, NULL);
-                   if (kill_link || got_sigchld) {
-                       sigprocmask(SIG_UNBLOCK, &mask, NULL);
-                   } else {
-                       waiting = 1;
-                       sigprocmask(SIG_UNBLOCK, &mask, NULL);
-                       wait_input(timeleft(&timo));
-                   }
-               }
-               waiting = 0;
-               calltimeout();
-               if (kill_link) {
-                   kill_link = 0;
+               handle_events();
+               if (kill_link)
                    new_phase(PHASE_DORMANT); /* allow signal to end holdoff */
-               }
-               if (got_sigchld)
-                   reap_kids(0);
            } while (phase == PHASE_HOLDOFF);
            if (!persist)
                break;
@@ -698,6 +570,50 @@ main(argc, argv)
     return 0;
 }
 
+/*
+ * handle_events - wait for something to happen and respond to it.
+ */
+static void
+handle_events()
+{
+    struct timeval timo;
+    sigset_t mask;
+
+    kill_link = open_ccp_flag = 0;
+    if (sigsetjmp(sigjmp, 1) == 0) {
+       sigprocmask(SIG_BLOCK, &mask, NULL);
+       if (got_sighup || got_sigterm || got_sigusr2 || got_sigchld) {
+           sigprocmask(SIG_UNBLOCK, &mask, NULL);
+       } else {
+           waiting = 1;
+           sigprocmask(SIG_UNBLOCK, &mask, NULL);
+           wait_input(timeleft(&timo));
+       }
+    }
+    waiting = 0;
+    calltimeout();
+    if (got_sighup) {
+       kill_link = 1;
+       got_sighup = 0;
+       if (status != EXIT_HANGUP)
+           status = EXIT_USER_REQUEST;
+    }
+    if (got_sigterm) {
+       kill_link = 1;
+       persist = 0;
+       status = EXIT_USER_REQUEST;
+       got_sigterm = 0;
+    }
+    if (got_sigchld) {
+       reap_kids(0);   /* Don't leave dead kids lying around */
+       got_sigchld = 0;
+    }
+    if (got_sigusr2) {
+       open_ccp_flag = 1;
+       got_sigusr2 = 0;
+    }
+}
+
 /*
  * setup_signals - initialize signal handling.
  */
@@ -1155,18 +1071,19 @@ static struct timeval timenow;          /* Current time */
 /*
  * timeout - Schedule a timeout.
  *
- * Note that this timeout takes the number of seconds, NOT hz (as in
+ * Note that this timeout takes the number of milliseconds, NOT hz (as in
  * the kernel).
  */
 void
-timeout(func, arg, time)
+timeout(func, arg, secs, usecs)
     void (*func) __P((void *));
     void *arg;
-    int time;
+    int secs, usecs;
 {
     struct callout *newp, *p, **pp;
   
-    MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time));
+    MAINDEBUG(("Timeout %p:%p in %d.%03d seconds.", func, arg,
+              time / 1000, time % 1000));
   
     /*
      * Allocate timeout.
@@ -1176,9 +1093,13 @@ timeout(func, arg, time)
     newp->c_arg = arg;
     newp->c_func = func;
     gettimeofday(&timenow, NULL);
-    newp->c_time.tv_sec = timenow.tv_sec + time;
-    newp->c_time.tv_usec = timenow.tv_usec;
-  
+    newp->c_time.tv_sec = timenow.tv_sec + secs;
+    newp->c_time.tv_usec = timenow.tv_usec + usecs;
+    if (newp->c_time.tv_usec >= 1000000) {
+       newp->c_time.tv_sec += newp->c_time.tv_usec / 1000000;
+       newp->c_time.tv_usec %= 1000000;
+    }
+
     /*
      * Find correct place and link it in.
      */
@@ -1295,9 +1216,7 @@ hup(sig)
     int sig;
 {
     info("Hangup (SIGHUP)");
-    kill_link = 1;
-    if (status != EXIT_HANGUP)
-       status = EXIT_USER_REQUEST;
+    got_sighup = 1;
     if (conn_running)
        /* Send the signal to the [dis]connector process(es) also */
        kill_my_pg(sig);
@@ -1318,9 +1237,7 @@ term(sig)
     int sig;
 {
     info("Terminating on signal %d.", sig);
-    persist = 0;               /* don't try to restart */
-    kill_link = 1;
-    status = EXIT_USER_REQUEST;
+    got_sigterm = 1;
     if (conn_running)
        /* Send the signal to the [dis]connector process(es) also */
        kill_my_pg(sig);
@@ -1373,7 +1290,7 @@ static void
 open_ccp(sig)
     int sig;
 {
-    open_ccp_flag = 1;
+    got_sigusr2 = 1;
     if (waiting)
        siglongjmp(sigjmp, 1);
 }
@@ -1624,7 +1541,6 @@ reap_kids(waitfor)
     int pid, status;
     struct subprocess *chp, **prevp;
 
-    got_sigchld = 0;
     if (n_children == 0)
        return 0;
     while ((pid = waitpid(-1, &status, (waitfor? 0: WNOHANG))) != -1