]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-linux.c
fix strtok loop
[ppp.git] / pppd / sys-linux.c
index 70ba2fa4d34b1f679a044bebc2dc769bcb90f0ee..4cc8987cc57b84cce67e167de7b4779379da14e5 100644 (file)
@@ -381,20 +381,12 @@ int establish_ppp (int tty_fd)
 
 void disestablish_ppp(int tty_fd)
 {
-    int nout;
-
     if (!hungup) {
 /*
  * Flush the tty output buffer so that the TIOCSETD doesn't hang.
- * We may have to do this several times because the tcflush only
- * affects the serial driver, and may trigger the ppp driver to
- * supply more data to the serial driver.
  */
-       do {
-           if (tcflush(tty_fd, TCIOFLUSH) < 0)
-               break;
-           nout = 0;
-       } while (ioctl(tty_fd, TIOCOUTQ, &nout) >= 0 && nout > 0);
+       if (tcflush(tty_fd, TCIOFLUSH) < 0)
+           warn("tcflush failed: %m");
 /*
  * Restore the previous line discipline
  */
@@ -1032,7 +1024,7 @@ static int read_route_table (struct rtentry *rt);
  * path_to_procfs - find the path to the proc file system mount point
  */
 
-static int path_to_procfs (void)
+static int path_to_procfs (const char *tail)
 {
     struct mntent *mntent;
     FILE *fp;
@@ -1041,6 +1033,7 @@ static int path_to_procfs (void)
     if (fp == NULL) {
        /* Default the mount location of /proc */
        strlcpy (route_buffer, "/proc", sizeof (route_buffer));
+       strlcat (route_buffer, tail, sizeof(route_buffer));
        return 1;
     }
 
@@ -1055,6 +1048,7 @@ static int path_to_procfs (void)
        return 0;
 
     strlcpy(route_buffer, mntent->mnt_dir, sizeof (route_buffer));
+    strlcat (route_buffer, tail, sizeof(route_buffer));
     return 1;
 }
 
@@ -1065,11 +1059,10 @@ static int path_to_procfs (void)
 
 static char *path_to_route (void)
 {
-    if (!path_to_procfs()) {
+    if (!path_to_procfs("/net/route")) {
        error("proc file system not mounted");
        return 0;
     }
-    strlcat (route_buffer, "/net/route", sizeof(route_buffer));
     return (route_buffer);
 }
 
@@ -1164,6 +1157,7 @@ static int read_route_table(struct rtentry *rt)
        cols[col] = strtok(p, route_delims);
        if (cols[col] == NULL)
            return 0;           /* didn't get enough columns */
+       p = NULL;
     }
 
     ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
@@ -1618,9 +1612,11 @@ int ppp_available(void)
 
     no_ppp_msg = 
        "This system lacks kernel support for PPP.  This could be because\n"
-       "the PPP kernel module is not loaded, or because the kernel is\n"
-       "not configured for PPP.  See the README.linux file in the\n"
-       "ppp-2.3.7 distribution.\n";
+       "the PPP kernel module could not be loaded, or because PPP was not\n"
+       "included in the kernel configuration.  If PPP was included as a\n"
+       "module, try `/sbin/modprobe -v ppp'.  If that fails, check that\n"
+       "ppp.o exists in /lib/modules/`uname -r`/net.\n"
+       "See README.linux file in the ppp distribution for more details.\n";
 
 /*
  * Open a socket for doing the ioctl operations.
@@ -2122,7 +2118,7 @@ int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
 
 /*
  * get_pty - get a pty master/slave pair and chown the slave side
- * to the uid given.  Assumes slave_name points to >= 12 bytes of space.
+ * to the uid given.  Assumes slave_name points to >= 16 bytes of space.
  */
 int
 get_pty(master_fdp, slave_fdp, slave_name, uid)
@@ -2131,31 +2127,56 @@ get_pty(master_fdp, slave_fdp, slave_name, uid)
     char *slave_name;
     int uid;
 {
-    int i, mfd, sfd;
-    char pty_name[12];
+    int i, mfd, sfd = -1;
+    char pty_name[16];
     struct termios tios;
 
-    sfd = -1;
-    for (i = 0; i < 64; ++i) {
-       slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
-                'p' + i / 16, i % 16);
-       mfd = open(pty_name, O_RDWR, 0);
-       if (mfd >= 0) {
-           pty_name[5] = 't';
-           sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
-           if (sfd >= 0)
-               break;
-           close(mfd);
+#ifdef TIOCGPTN
+    /*
+     * Try the unix98 way first.
+     */
+    mfd = open("/dev/ptmx", O_RDWR);
+    if (mfd >= 0) {
+       int ptn;
+       if (ioctl(mfd, TIOCGPTN, &ptn) >= 0) {
+           slprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn);
+           chmod(pty_name, S_IRUSR | S_IWUSR);
+#ifdef TIOCSPTLCK
+           ptn = 0;
+           if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0)
+               warn("Couldn't unlock pty slave %s: %m", pty_name);
+#endif
+           if ((sfd = open(pty_name, O_RDWR)) < 0)
+               warn("Couldn't open pty slave %s: %m", pty_name);
        }
     }
+#endif /* TIOCGPTN */
+
+    if (sfd < 0) {
+       /* the old way - scan through the pty name space */
+       for (i = 0; i < 64; ++i) {
+           slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
+                    'p' + i / 16, i % 16);
+           mfd = open(pty_name, O_RDWR, 0);
+           if (mfd >= 0) {
+               pty_name[5] = 't';
+               sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
+               if (sfd >= 0) {
+                   fchown(sfd, uid, -1);
+                   fchmod(sfd, S_IRUSR | S_IWUSR);
+                   break;
+               }
+               close(mfd);
+           }
+       }
+    }
+
     if (sfd < 0)
        return 0;
 
-    strlcpy(slave_name, pty_name, 12);
+    strlcpy(slave_name, pty_name, 16);
     *master_fdp = mfd;
     *slave_fdp = sfd;
-    fchown(sfd, uid, -1);
-    fchmod(sfd, S_IRUSR | S_IWUSR);
     if (tcgetattr(sfd, &tios) == 0) {
        tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
        tios.c_cflag |= CS8 | CREAD;
@@ -2402,21 +2423,16 @@ int
 sys_check_options(void)
 {
 #ifdef IPX_CHANGE
-    struct stat stat_buf;
 /*
  * Disable the IPX protocol if the support is not present in the kernel.
- * If we disable it then ensure that IP support is enabled.
  */
-    while (ipxcp_protent.enabled_flag) {
-        if (path_to_procfs()) {
-           strlcat (route_buffer, "/net/ipx_interface", sizeof(route_buffer));
-           if (lstat (route_buffer, &stat_buf) >= 0)
-               break;
+    if (ipxcp_protent.enabled_flag) {
+       struct stat stat_buf;
+        if (!path_to_procfs("/net/ipx_interface")
+           || lstat (route_buffer, &stat_buf) < 0) {
+           error("IPX support is not present in the kernel\n");
+           ipxcp_protent.enabled_flag = 0;
        }
-       error("IPX support is not present in the kernel\n");
-       ipxcp_protent.enabled_flag = 0;
-       ipcp_protent.enabled_flag  = 1;
-       break;
     }
 #endif
     if (demand && driver_is_old) {