]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/tty.c
Flush the tty when welcomer or connector fails
[ppp.git] / pppd / tty.c
index a911fde5c0f0b34f274a56763f8d075aa98f67f2..a9832d918d076a0bd8634091d9086269c353de1f 100644 (file)
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: tty.c,v 1.22 2004/11/13 12:07:29 paulus Exp $"
+#define RCSID  "$Id: tty.c,v 1.26 2008/06/23 11:44:06 paulus Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <termios.h>
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
@@ -563,12 +564,16 @@ int connect_tty()
                        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;
-                       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;
@@ -684,11 +689,11 @@ int connect_tty()
                        if (device_script(initializer, ttyfd, ttyfd, 0) < 0) {
                                error("Initializer script failed");
                                status = EXIT_INIT_FAILED;
-                               goto errret;
+                               goto errretf;
                        }
                        if (got_sigterm) {
                                disconnect_tty();
-                               goto errret;
+                               goto errretf;
                        }
                        info("Serial port initialized.");
                }
@@ -697,11 +702,11 @@ int connect_tty()
                        if (device_script(connector, ttyfd, ttyfd, 0) < 0) {
                                error("Connect script failed");
                                status = EXIT_CONNECT_FAILED;
-                               goto errret;
+                               goto errretf;
                        }
                        if (got_sigterm) {
                                disconnect_tty();
-                               goto errret;
+                               goto errretf;
                        }
                        info("Serial connection established.");
                }
@@ -750,19 +755,14 @@ int connect_tty()
 
        return ttyfd;
 
+ errretf:
+       if (real_ttyfd >= 0)
+               tcflush(real_ttyfd, TCIOFLUSH);
  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;
        if (got_sigterm)
                asked_to_quit = 1;
@@ -781,6 +781,7 @@ void disconnect_tty()
        } else {
                info("Serial link disconnected.");
        }
+       stop_charshunt(NULL, 0);
 }
 
 void tty_close_fds()
@@ -944,8 +945,7 @@ start_charshunt(ifd, ofd)
        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;
 }