]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/tty.c
Clear hungup in start_link() instead of connect_tty()
[ppp.git] / pppd / tty.c
index 30d476559f6c32a3604f5495d90a4a54ac9ab005..d571b111d7e34755d9304104c075dcbcc16f4e44 100644 (file)
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: tty.c,v 1.19 2004/11/06 05:42:29 paulus Exp $"
+#define RCSID  "$Id: tty.c,v 1.27 2008/07/01 12:27:56 paulus Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <termios.h>
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
@@ -152,6 +153,8 @@ int using_pty = 0;          /* we're allocating a pty as the device */
 
 extern uid_t uid;
 extern int kill_link;
 
 extern uid_t uid;
 extern int kill_link;
+extern int asked_to_quit;
+extern int got_sigterm;
 
 /* XXX */
 extern int privopen;           /* don't lock, open device as root */
 
 /* XXX */
 extern int privopen;           /* don't lock, open device as root */
@@ -443,7 +446,12 @@ tty_check_options()
        struct stat statbuf;
        int fdflags;
 
        struct stat statbuf;
        int fdflags;
 
-       if (demand && connect_script == 0) {
+       if (demand && notty) {
+               option_error("demand-dialling is incompatible with notty");
+               exit(EXIT_OPTION_ERROR);
+       }
+       if (demand && connect_script == 0 && ptycommand == NULL
+           && pty_socket == NULL) {
                option_error("connect script is required for demand-dialling\n");
                exit(EXIT_OPTION_ERROR);
        }
                option_error("connect script is required for demand-dialling\n");
                exit(EXIT_OPTION_ERROR);
        }
@@ -546,8 +554,7 @@ int connect_tty()
         * out and we want to use the modem lines, we reopen it later
         * in order to wait for the carrier detect signal from the modem.
         */
         * out and we want to use the modem lines, we reopen it later
         * in order to wait for the carrier detect signal from the modem.
         */
-       hungup = 0;
-       kill_link = 0;
+       got_sigterm = 0;
        connector = doing_callback? callback_script: connect_script;
        if (devnam[0] != 0) {
                for (;;) {
        connector = doing_callback? callback_script: connect_script;
        if (devnam[0] != 0) {
                for (;;) {
@@ -556,12 +563,16 @@ int connect_tty()
                        int err, prio;
 
                        prio = privopen? OPRIO_ROOT: tty_options[0].priority;
                        int err, prio;
 
                        prio = privopen? OPRIO_ROOT: tty_options[0].priority;
-                       if (prio < OPRIO_ROOT)
-                               seteuid(uid);
+                       if (prio < OPRIO_ROOT && seteuid(uid) == -1) {
+                               error("Unable to drop privileges before opening %s: %m\n",
+                                     devnam);
+                               status = EXIT_OPEN_FAILED;
+                               goto errret;
+                       }
                        real_ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0);
                        err = errno;
                        real_ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0);
                        err = errno;
-                       if (prio < OPRIO_ROOT)
-                               seteuid(0);
+                       if (prio < OPRIO_ROOT && seteuid(0) == -1)
+                               fatal("Unable to regain privileges");
                        if (real_ttyfd >= 0)
                                break;
                        errno = err;
                        if (real_ttyfd >= 0)
                                break;
                        errno = err;
@@ -644,6 +655,12 @@ int connect_tty()
        } else if (notty) {
                if (!start_charshunt(0, 1))
                        goto errret;
        } else if (notty) {
                if (!start_charshunt(0, 1))
                        goto errret;
+               dup2(fd_devnull, 0);
+               dup2(fd_devnull, 1);
+               if (log_to_fd == 1)
+                       log_to_fd = -1;
+               if (log_to_fd != 2)
+                       dup2(fd_devnull, 2);
        } else if (record_file != NULL) {
                int fd = dup(ttyfd);
                if (!start_charshunt(fd, fd))
        } else if (record_file != NULL) {
                int fd = dup(ttyfd);
                if (!start_charshunt(fd, fd))
@@ -671,11 +688,11 @@ int connect_tty()
                        if (device_script(initializer, ttyfd, ttyfd, 0) < 0) {
                                error("Initializer script failed");
                                status = EXIT_INIT_FAILED;
                        if (device_script(initializer, ttyfd, ttyfd, 0) < 0) {
                                error("Initializer script failed");
                                status = EXIT_INIT_FAILED;
-                               goto errret;
+                               goto errretf;
                        }
                        }
-                       if (kill_link) {
+                       if (got_sigterm) {
                                disconnect_tty();
                                disconnect_tty();
-                               goto errret;
+                               goto errretf;
                        }
                        info("Serial port initialized.");
                }
                        }
                        info("Serial port initialized.");
                }
@@ -684,11 +701,11 @@ int connect_tty()
                        if (device_script(connector, ttyfd, ttyfd, 0) < 0) {
                                error("Connect script failed");
                                status = EXIT_CONNECT_FAILED;
                        if (device_script(connector, ttyfd, ttyfd, 0) < 0) {
                                error("Connect script failed");
                                status = EXIT_CONNECT_FAILED;
-                               goto errret;
+                               goto errretf;
                        }
                        }
-                       if (kill_link) {
+                       if (got_sigterm) {
                                disconnect_tty();
                                disconnect_tty();
-                               goto errret;
+                               goto errretf;
                        }
                        info("Serial connection established.");
                }
                        }
                        info("Serial connection established.");
                }
@@ -712,7 +729,7 @@ int connect_tty()
                                error("Failed to reopen %s: %m", devnam);
                                status = EXIT_OPEN_FAILED;
                        }
                                error("Failed to reopen %s: %m", devnam);
                                status = EXIT_OPEN_FAILED;
                        }
-                       if (!persist || errno != EINTR || hungup || kill_link)
+                       if (!persist || errno != EINTR || hungup || got_sigterm)
                                goto errret;
                }
                close(i);
                                goto errret;
                }
                close(i);
@@ -732,25 +749,22 @@ int connect_tty()
         * time for something from the peer.  This can avoid bouncing
         * our packets off his tty before he has it set up.
         */
         * time for something from the peer.  This can avoid bouncing
         * our packets off his tty before he has it set up.
         */
-       if (connector != NULL || ptycommand != NULL)
+       if (connector != NULL || ptycommand != NULL || pty_socket != NULL)
                listen_time = connect_delay;
 
        return ttyfd;
 
                listen_time = connect_delay;
 
        return ttyfd;
 
+ errretf:
+       if (real_ttyfd >= 0)
+               tcflush(real_ttyfd, TCIOFLUSH);
  errret:
        if (pty_master >= 0) {
                close(pty_master);
                pty_master = -1;
        }
  errret:
        if (pty_master >= 0) {
                close(pty_master);
                pty_master = -1;
        }
-       if (pty_slave >= 0) {
-               close(pty_slave);
-               pty_slave = -1;
-       }
-       if (real_ttyfd >= 0) {
-               close(real_ttyfd);
-               real_ttyfd = -1;
-       }
        ttyfd = -1;
        ttyfd = -1;
+       if (got_sigterm)
+               asked_to_quit = 1;
        return -1;
 }
 
        return -1;
 }
 
@@ -766,6 +780,7 @@ void disconnect_tty()
        } else {
                info("Serial link disconnected.");
        }
        } else {
                info("Serial link disconnected.");
        }
+       stop_charshunt(NULL, 0);
 }
 
 void tty_close_fds()
 }
 
 void tty_close_fds()
@@ -929,8 +944,7 @@ start_charshunt(ifd, ofd)
        exit(0);
     }
     charshunt_pid = cpid;
        exit(0);
     }
     charshunt_pid = cpid;
-    add_notifier(&sigreceived, stop_charshunt, 0);
-    record_child(cpid, "pppd (charshunt)", charshunt_done, NULL);
+    record_child(cpid, "pppd (charshunt)", charshunt_done, NULL, 1);
     return 1;
 }
 
     return 1;
 }