]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/main.c
Change UID to ORIG_UID because bash defines UID.
[ppp.git] / pppd / main.c
index b11ed66a3a1dbdd1791556a498ce96100ee64ded..7072d0a871fabcf9b615e3df1b179ff41ae2aa1d 100644 (file)
@@ -18,7 +18,7 @@
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: main.c,v 1.45 1998/03/25 01:28:14 paulus Exp $";
+static char rcsid[] = "$Id: main.c,v 1.50 1998/09/13 23:38:49 paulus Exp $";
 #endif
 
 #include <stdio.h>
@@ -85,11 +85,11 @@ int baud_rate;                      /* Actual bits/second for serial device */
 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 phase;                     /* where the link is at */
 int kill_link;
 int open_ccp_flag;
-int redirect_stderr;           /* Connector's stderr should go to file */
 
 char **script_env;             /* Env. variable values for scripts */
 int s_env_nalloc;              /* # words avail at script_env */
@@ -105,6 +105,7 @@ char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n";
 
 /* 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 get_input __P((void));
@@ -165,7 +166,6 @@ main(argc, argv)
 {
     int i, fdflags;
     struct sigaction sa;
-    FILE *pidfile;
     char *p;
     struct passwd *pw;
     struct timeval timo;
@@ -199,7 +199,7 @@ main(argc, argv)
     uid = getuid();
     privileged = uid == 0;
     sprintf(numbuf, "%d", uid);
-    script_setenv("UID", numbuf);
+    script_setenv("ORIG_UID", numbuf);
 
     /*
      * Initialize to the standard option set, then parse, in order,
@@ -256,7 +256,8 @@ main(argc, argv)
      */
     if (!default_device && strcmp(devnam, default_devnam) == 0)
        default_device = 1;
-    redirect_stderr = !nodetach || default_device;
+    if (default_device)
+       nodetach = 1;
 
     /*
      * Initialize system-dependent stuff and magic number package.
@@ -270,10 +271,8 @@ main(argc, argv)
      * Detach ourselves from the terminal, if required,
      * and identify who is running us.
      */
-    if (!default_device && !nodetach && daemon(0, 0) < 0) {
-       perror("Couldn't detach from controlling terminal");
-       exit(1);
-    }
+    if (nodetach == 0)
+       detach();
     pid = getpid();
     p = getlogin();
     if (p == NULL) {
@@ -374,15 +373,7 @@ main(argc, argv)
        (void) sprintf(ifname, "ppp%d", ifunit);
        script_setenv("IFNAME", ifname);
 
-       /* write pid to file */
-       (void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
-       if ((pidfile = fopen(pidfilename, "w")) != NULL) {
-           fprintf(pidfile, "%d\n", pid);
-           (void) fclose(pidfile);
-       } else {
-           syslog(LOG_ERR, "Failed to create pid file %s: %m", pidfilename);
-           pidfilename[0] = 0;
-       }
+       create_pidfile();       /* write pid to file */
 
        /*
         * Configure the interface and mark it up, etc.
@@ -523,16 +514,7 @@ main(argc, argv)
            (void) sprintf(ifname, "ppp%d", ifunit);
            script_setenv("IFNAME", ifname);
 
-           /* write pid to file */
-           (void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
-           if ((pidfile = fopen(pidfilename, "w")) != NULL) {
-               fprintf(pidfile, "%d\n", pid);
-               (void) fclose(pidfile);
-           } else {
-               syslog(LOG_ERR, "Failed to create pid file %s: %m",
-                      pidfilename);
-               pidfilename[0] = 0;
-           }
+           create_pidfile();   /* write pid to file */
        }
 
        /*
@@ -624,6 +606,43 @@ main(argc, argv)
     return 0;
 }
 
+/*
+ * detach - detach us from the controlling terminal.
+ */
+void
+detach()
+{
+    if (detached)
+       return;
+    if (daemon(0, 0) < 0) {
+       perror("Couldn't detach from controlling terminal");
+       die(1);
+    }
+    detached = 1;
+    pid = getpid();
+    /* update pid file if it has been written already */
+    if (pidfilename[0])
+       create_pidfile();
+}
+
+/*
+ * Create a file containing our process ID.
+ */
+static void
+create_pidfile()
+{
+    FILE *pidfile;
+
+    (void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
+    if ((pidfile = fopen(pidfilename, "w")) != NULL) {
+       fprintf(pidfile, "%d\n", pid);
+       (void) fclose(pidfile);
+    } else {
+       syslog(LOG_ERR, "Failed to create pid file %s: %m", pidfilename);
+       pidfilename[0] = 0;
+    }
+}
+
 /*
  * holdoff_end - called via a timeout when the holdoff period ends.
  */
@@ -1018,6 +1037,11 @@ static void
 bad_signal(sig)
     int sig;
 {
+    static int crashed = 0;
+
+    if (crashed)
+       _exit(127);
+    crashed = 1;
     syslog(LOG_ERR, "Fatal signal %d", sig);
     if (conn_running)
        kill_my_pg(SIGTERM);
@@ -1068,7 +1092,7 @@ device_script(program, in, out)
                close(out);
            }
        }
-       if (redirect_stderr) {
+       if (nodetach == 0) {
            close(2);
            errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600);
            if (errfd >= 0 && errfd != 2) {
@@ -1268,10 +1292,9 @@ pr_log __V((void *arg, char *fmt, ...))
     fmt = va_arg(pvar, char *);
 #endif
 
-    vsprintf(buf, fmt, pvar);
+    n = vfmtmsg(buf, sizeof(buf), fmt, pvar);
     va_end(pvar);
 
-    n = strlen(buf);
     if (linep + n + 1 > line + sizeof(line)) {
        syslog(LOG_DEBUG, "%s", line);
        linep = line;