]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/main.c
put linux ioctls in sys-linux.c
[ppp.git] / pppd / main.c
index ad3edebf3f1b580403110e75af77a9c21bf1ca6f..1e1397e5505353f79bfba97228807e57c8fd3a1b 100644 (file)
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: main.c,v 1.7 1994/04/18 04:06:26 paulus Exp $";
+static char rcsid[] = "$Id: main.c,v 1.13 1994/05/27 01:02:14 paulus Exp $";
 #endif
 
 #define SETSID
 
 #include <stdio.h>
+#include <string.h>
 #include <signal.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <syslog.h>
 #include <netdb.h>
 #include <utmp.h>
+#include <pwd.h>
 #include <sys/wait.h>
 
 /*
@@ -37,11 +39,7 @@ static char rcsid[] = "$Id: main.c,v 1.7 1994/04/18 04:06:26 paulus Exp $";
  * /etc/ppp/options exists.
  */
 #ifndef        REQ_SYSOPTIONS
-#define REQ_SYSOPTIONS 0
-#endif
-
-#ifdef STREAMS
-#undef SGTTY
+#define REQ_SYSOPTIONS 1
 #endif
 
 #ifdef SGTTY
@@ -58,18 +56,9 @@ static char rcsid[] = "$Id: main.c,v 1.7 1994/04/18 04:06:26 paulus Exp $";
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/time.h>
-
-#include "callout.h"
-
 #include <net/if.h>
-#include <net/if_ppp.h>
-
-#include <string.h>
-
-#ifndef BSD
-#define BSD 43
-#endif /*BSD*/
 
+#include "callout.h"
 #include "ppp.h"
 #include "magic.h"
 #include "fsm.h"
@@ -105,15 +94,16 @@ char *progname;                    /* Name of this program */
 char hostname[MAXNAMELEN];     /* Our hostname */
 char our_name[MAXNAMELEN];
 char remote_name[MAXNAMELEN];
+static char pidfilename[MAXPATHLEN];
 
 static pid_t   pid;            /* Our pid */
 static pid_t   pgrpid;         /* Process Group ID */
-static char pidfilename[MAXPATHLEN];
+uid_t uid;                     /* Our real user-id */
 
 char devname[MAXPATHLEN] = "/dev/tty"; /* Device name */
 int default_device = TRUE;     /* use default device (stdin/out) */
 
-int fd;                                /* Device file descriptor */
+int fd = -1;                   /* Device file descriptor */
 int s;                         /* Socket file descriptor */
 
 int phase;                     /* where the link is at */
@@ -158,11 +148,12 @@ int lockflag = 0;         /* lock the serial device */
 
 
 /* prototypes */
-static void hup __ARGS((int, int, struct sigcontext *, char *));
-static void intr __ARGS((int, int, struct sigcontext *, char *));
-static void term __ARGS((int, int, struct sigcontext *, char *));
-static void alrm __ARGS((int, int, struct sigcontext *, char *));
-static void io __ARGS((int, int, struct sigcontext *, char *));
+static void hup __ARGS((int));
+static void intr __ARGS((int));
+static void term __ARGS((int));
+static void alrm __ARGS((int));
+static void io __ARGS((int));
+static void chld __ARGS((int));
 static void incdebug __ARGS((int));
 static void nodebug __ARGS((int));
 void establish_ppp __ARGS((void));
@@ -177,9 +168,7 @@ void format_packet __ARGS((u_char *, int,
                           void (*) (void *, char *, ...), void *));
 void pr_log __ARGS((void *, char *, ...));
 
-#ifdef STREAMS
 extern char    *ttyname __ARGS((int));
-#endif
 extern char    *getlogin __ARGS((void));
 
 /*
@@ -207,16 +196,15 @@ main(argc, argv)
     char *argv[];
 {
     int mask, i;
-    struct sigvec sv;
+    struct sigaction sa;
     struct cmd *cmdp;
     FILE *pidfile;
     char *p;
+    struct passwd *pw;
 
-#ifdef STREAMS
-    p = ttyname(fileno(stdin));
+    p = ttyname(0);
     if (p)
        strcpy(devname, p);
-#endif
   
     if (gethostname(hostname, MAXNAMELEN) < 0 ) {
        perror("couldn't get hostname");
@@ -225,6 +213,7 @@ main(argc, argv)
     hostname[MAXNAMELEN-1] = 0;
 
     pid = getpid();
+    uid = getuid();
 
     if (!ppp_available()) {
        fprintf(stderr, "Sorry - PPP is not available on this system\n");
@@ -241,21 +230,18 @@ main(argc, argv)
   
     progname = *argv;
 
-    if (!options_from_file(_PATH_SYSOPTIONS, REQ_SYSOPTIONS) ||
+    if (!options_from_file(_PATH_SYSOPTIONS, REQ_SYSOPTIONS, 0) ||
        !options_from_user() ||
-       !parse_args(argc-1, argv+1))
+       !parse_args(argc-1, argv+1) ||
+       !options_for_tty())
        die(1);
     check_auth_options();
     setipdefault();
 
-    if (lockflag && !default_device)
-       if (lock(devname) < 0)
-           die(1);
-
     /*
      * Initialize syslog system and magic number package.
      */
-#if (BSD >= 43 || defined(sun)) && !defined(ultrix)
+#if !defined(ultrix)
     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
     setlogmask(LOG_UPTO(LOG_INFO));
 #else
@@ -269,10 +255,15 @@ main(argc, argv)
     magic_init();
 
     p = getlogin();
-    if (p == NULL)
-       p = "(unknown)";
+    if (p == NULL) {
+       pw = getpwuid(uid);
+       if (pw != NULL && pw->pw_name != NULL)
+           p = pw->pw_name;
+       else
+           p = "(unknown)";
+    }
     syslog(LOG_NOTICE, "pppd %s.%d started by %s, uid %d",
-          VERSION, PATCHLEVEL, p, getuid());
+          VERSION, PATCHLEVEL, p, uid);
 
 #ifdef SETSID
     /*
@@ -321,6 +312,10 @@ main(argc, argv)
     }
 #endif
 
+    if (lockflag && !default_device)
+       if (lock(devname) < 0)
+           die(1);
+
     /* Get an internet socket for doing socket ioctl's on. */
     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        syslog(LOG_ERR, "socket : %m");
@@ -337,25 +332,27 @@ main(argc, argv)
     sigaddset(&mask, SIGINT);
     sigaddset(&mask, SIGALRM);
     sigaddset(&mask, SIGIO);
+    sigaddset(&mask, SIGCHLD);
 #ifdef STREAMS
     sigaddset(&mask, SIGPOLL);
 #endif
 
 #define SIGNAL(s, handler)     { \
-       sv.sv_handler = handler; \
-       if (sigvec(s, &sv, NULL) < 0) { \
-           syslog(LOG_ERR, "sigvec(%d): %m", s); \
+       sa.sa_handler = handler; \
+       if (sigaction(s, &sa, NULL) < 0) { \
+           syslog(LOG_ERR, "sigaction(%d): %m", s); \
            die(1); \
        } \
     }
 
-    sv.sv_mask = mask;
-    sv.sv_flags = 0;
+    sa.sa_mask = mask;
+    sa.sa_flags = 0;
     SIGNAL(SIGHUP, hup);               /* Hangup */
     SIGNAL(SIGINT, intr);              /* Interrupt */
     SIGNAL(SIGTERM, term);             /* Terminate */
     SIGNAL(SIGALRM, alrm);             /* Timeout */
     SIGNAL(SIGIO, io);                 /* Input available */
+    SIGNAL(SIGCHLD, chld);             /* Death of child process */
 #ifdef STREAMS
     SIGNAL(SIGPOLL, io);               /* Input available */
 #endif
@@ -390,13 +387,13 @@ main(argc, argv)
     }
 #endif /* TIOCSCTTY */
 
-    /* set line speed, flow control, etc. */
-    set_up_tty(fd);
-
     /* run connection script */
     if (connector) {
        MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector));
 
+       /* set line speed, flow control, etc.; set CLOCAL for now */
+       set_up_tty(fd, 1);
+
        /* drop dtr to hang up in case modem is off hook */
        if (!default_device && modem) {
            setdtr(fd, FALSE);
@@ -414,6 +411,9 @@ main(argc, argv)
        sleep(1);               /* give it time to set up its terminal */
     }
   
+    /* set line speed, flow control, etc.; clear CLOCAL if modem option */
+    set_up_tty(fd, 0);
+
     /* set up the serial device as a ppp interface */
     establish_ppp();
 
@@ -462,6 +462,12 @@ main(argc, argv)
        syslog(LOG_ERR, "fcntl(F_GETFL): %m");
        die(1);
     }
+
+#ifdef _linux_ /* This is a kludge for Linux. FIXME !!! -- later. */
+#undef FASYNC
+#define FASYNC 0
+#endif
+
     if (fcntl(fd, F_SETFL, FNDELAY | FASYNC) == -1) {
        syslog(LOG_ERR, "fcntl(F_SETFL, FNDELAY | FASYNC): %m");
        die(1);
@@ -475,10 +481,8 @@ main(argc, argv)
     sigprocmask(SIG_BLOCK, &mask, NULL); /* Block signals now */
     lcp_lowerup(0);            /* XXX Well, sort of... */
     lcp_open(0);               /* Start protocol */
-    for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; ) {
+    for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; )
        sigpause(0);            /* Wait for next signal */
-       reap_kids();            /* Don't leave dead kids lying around */
-    }
 
     /*
      * Run disconnector script, if requested
@@ -617,13 +621,14 @@ baud_rate_of(speed)
 
 /*
  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
- * at the requested speed, etc.
+ * at the requested speed, etc.  If `local' is true, set CLOCAL
+ * regardless of whether the modem option was specified.
  */
-set_up_tty(fd)
-    int fd;
+set_up_tty(fd, local)
+    int fd, local;
 {
 #ifndef SGTTY
-    int speed;
+    int speed, x;
     struct termios tios;
 
     if (tcgetattr(fd, &tios) < 0) {
@@ -636,20 +641,27 @@ set_up_tty(fd)
 
 #ifdef CRTSCTS
     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
-    if (crtscts)
+    if (crtscts == 1)
        tios.c_cflag |= CRTSCTS;
 #else
     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
 #endif /* CRTSCTS */
 
     tios.c_cflag |= CS8 | CREAD | HUPCL;
-    if (!modem)
+    if (local || !modem)
        tios.c_cflag |= CLOCAL;
     tios.c_iflag = IGNBRK | IGNPAR;
     tios.c_oflag = 0;
     tios.c_lflag = 0;
     tios.c_cc[VMIN] = 1;
     tios.c_cc[VTIME] = 0;
+
+    if (crtscts == 2) {
+       tios.c_iflag |= IXOFF;
+       tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
+       tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
+    }
+
     speed = translate_speed(inspeed);
     if (speed) {
        cfsetospeed(&tios, speed);
@@ -663,6 +675,14 @@ set_up_tty(fd)
        die(1);
     }
 
+#ifdef ultrix
+    x = 0;
+    if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
+       syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
+    if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
+       syslog(LOG_WARNING, "TIOC(N)CAR: %m");
+#endif
+
 #else  /* SGTTY */
     int speed;
     struct sgttyb sgttyb;
@@ -743,8 +763,9 @@ cleanup(status, arg)
        if (modem)
            setdtr(fd, FALSE);
 
-       if (fcntl(fd, F_SETFL, initfdflags) < 0)
+       if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
            syslog(LOG_WARNING, "fcntl(F_SETFL, fdflags): %m");
+       initfdflags = -1;
 
        disestablish_ppp();
 
@@ -759,7 +780,7 @@ cleanup(status, arg)
        }
 
        close(fd);
-       fd = 0;
+       fd = -1;
     }
 
     if (pidfilename[0] != 0 && unlink(pidfilename) < 0) 
@@ -929,12 +950,9 @@ adjtimeout()
  *
  * Indicates that the physical layer has been disconnected.
  */
-/*ARGSUSED*/
 static void
-hup(sig, code, scp, addr)
-    int sig, code;
-    struct sigcontext *scp;
-    char *addr;
+hup(sig)
+    int sig;
 {
     syslog(LOG_INFO, "Hangup (SIGHUP)");
 
@@ -951,12 +969,9 @@ hup(sig, code, scp, addr)
  *
  * Indicates that we should initiate a graceful disconnect and exit.
  */
-/*ARGSUSED*/
 static void
-term(sig, code, scp, addr)
-    int sig, code;
-    struct sigcontext *scp;
-    char *addr;
+term(sig)
+    int sig;
 {
     syslog(LOG_INFO, "Terminating link.");
     persist = 0;               /* don't try to restart */
@@ -970,12 +985,9 @@ term(sig, code, scp, addr)
  *
  * Indicates that we should initiate a graceful disconnect and exit.
  */
-/*ARGSUSED*/
 static void
-intr(sig, code, scp, addr)
-    int sig, code;
-    struct sigcontext *scp;
-    char *addr;
+intr(sig)
+    int sig;
 {
     syslog(LOG_INFO, "Interrupt received: terminating link");
     persist = 0;               /* don't try to restart */
@@ -989,18 +1001,17 @@ intr(sig, code, scp, addr)
  *
  * Indicates a timeout.
  */
-/*ARGSUSED*/
 static void
-alrm(sig, code, scp, addr)
-    int sig, code;
-    struct sigcontext *scp;
-    char *addr;
+alrm(sig)
+    int sig;
 {
     struct itimerval itv;
     struct callout *freep, *list, *last;
 
     MAINDEBUG((LOG_DEBUG, "Alarm"));
 
+    if (callout == NULL)
+       return;
     /*
      * Get the first scheduled timeout and any that were scheduled
      * for the same time as a list, and remove them all from callout
@@ -1044,17 +1055,26 @@ alrm(sig, code, scp, addr)
 }
 
 
+/*
+ * chld - Catch SIGCHLD signal.
+ * Calls reap_kids to get status for any dead kids.
+ */
+static void
+chld(sig)
+    int sig;
+{
+    reap_kids();
+}
+
+
 /*
  * io - Catch SIGIO signal.
  *
  * Indicates that incoming data is available.
  */
-/*ARGSUSED*/
 static void
-io(sig, code, scp, addr)
-    int sig, code;
-    struct sigcontext *scp;
-    char *addr;
+io(sig)
+    int sig;
 {
     int len, i;
     u_char *p;
@@ -1066,25 +1086,6 @@ io(sig, code, scp, addr)
     MAINDEBUG((LOG_DEBUG, "IO signal received"));
     adjtimeout();              /* Adjust timeouts */
 
-    /* we do this to see if the SIGIO handler is being invoked for input */
-    /* ready, or for the socket buffer hitting the low-water mark. */
-
-    notime.tv_sec = 0;
-    notime.tv_usec = 0;
-    FD_ZERO(&fdset);
-    FD_SET(fd, &fdset);
-  
-    if ((ready = select(32, &fdset, (fd_set *) NULL, (fd_set *) NULL,
-                     &notime)) == -1) {
-       syslog(LOG_ERR, "Error in io() select: %m");
-       die(1);
-    }
-    
-    if (ready == 0) {
-       MAINDEBUG((LOG_DEBUG, "IO non-input ready SIGIO occured."));
-       return;
-    }
-
     /* Yup, this is for real */
     for (;;) {                 /* Read all available packets */
        p = inpacket_buf;       /* point to beginning of packet buffer */
@@ -1094,8 +1095,9 @@ io(sig, code, scp, addr)
            return;
 
        if (len == 0) {
-           syslog(LOG_WARNING, "End of file on fd!");
-           die(1);
+           MAINDEBUG((LOG_DEBUG, "End of file on fd!"));
+           lcp_lowerdown(0);
+           return;
        }
 
        if (debug /*&& (debugflags & DBG_INPACKET)*/)