]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-linux.c
fix demand-dialling bug with new driver
[ppp.git] / pppd / sys-linux.c
index 06c5f7fdc457d48402912669f1b8732523675df4..928a7207f806af13225107064536e87e549a1e17 100644 (file)
@@ -166,7 +166,6 @@ static int get_flags (int fd);
 static void set_flags (int fd, int flags);
 static int translate_speed (int bps);
 static int baud_rate_of (int speed);
-static char *path_to_route (void);
 static void close_route_table (void);
 static int open_route_table (void);
 static int read_route_table (struct rtentry *rt);
@@ -255,7 +254,6 @@ static void set_flags (int fd, int flags)
 
 void sys_init(void)
 {
-    int osmaj, osmin, ospatch;
     int flags;
 
     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
@@ -286,11 +284,6 @@ void sys_init(void)
 
     FD_ZERO(&in_fds);
     max_in_fd = 0;
-
-    uname(&utsname);
-    osmaj = osmin = ospatch = 0;
-    sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
-    kernel_version = KVERSION(osmaj, osmin, ospatch);
 }
 
 /********************************************************************
@@ -367,6 +360,8 @@ int establish_ppp (int tty_fd)
  * The current PPP device will be the tty file.
  */
     set_ppp_fd (tty_fd);
+    if (new_style_driver)
+       add_fd(tty_fd);
 /*
  * Ensure that the tty device is in exclusive mode.
  */
@@ -383,6 +378,13 @@ int establish_ppp (int tty_fd)
 /*
  * Set the current tty to the PPP discpline
  */
+
+#ifndef N_SYNC_PPP
+#define N_SYNC_PPP 14
+#endif
+    if (new_style_driver)
+           ppp_disc = sync_serial ? N_SYNC_PPP:N_PPP;
+
     if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) {
        if ( ! ok_error (errno) )
            fatal("ioctl(TIOCSETD): %m(%d)", errno);
@@ -450,6 +452,8 @@ int establish_ppp (int tty_fd)
 
 void disestablish_ppp(int tty_fd)
 {
+    if (new_style_driver)
+       remove_fd(tty_fd);
     if (!hungup) {
 /*
  * Flush the tty output buffer so that the TIOCSETD doesn't hang.
@@ -839,7 +843,7 @@ void remove_fd(int fd)
 
 int read_packet (unsigned char *buf)
 {
-    int len;
+    int len, nr;
 
     len = PPP_MRU + PPP_HDRLEN;
     if (new_style_driver) {
@@ -847,13 +851,18 @@ int read_packet (unsigned char *buf)
        *buf++ = PPP_UI;
        len -= 2;
     }
-    len = read(ppp_dev_fd, buf, len);
-    if (len < 0) {
-       if (errno == EWOULDBLOCK || errno == EIO)
-           return -1;
-       fatal("read: %m(%d)", errno);
+    nr = -1;
+    if (new_style_driver) {
+       nr = read(ppp_dev_fd, buf, len);
+       if (nr < 0 && errno != EWOULDBLOCK && errno != EIO)
+           error("read /dev/ppp: %m");
+    }
+    if (nr < 0 && ppp_fd >= 0) {
+       nr = read(ppp_fd, buf, len);
+       if (nr < 0 && errno != EWOULDBLOCK && errno != EIO)
+           error("read: %m");
     }
-    return new_style_driver? len+2: len;
+    return (new_style_driver && nr > 0)? nr+2: nr;
 }
 
 /********************************************************************
@@ -1075,39 +1084,31 @@ static char *path_to_procfs(const char *tail)
     FILE *fp;
 
     if (proc_path_len == 0) {
+       /* Default the mount location of /proc */
+       strlcpy (proc_path, "/proc", sizeof(proc_path));
+       proc_path_len = 5;
        fp = fopen(MOUNTED, "r");
-       if (fp == NULL) {
-           /* Default the mount location of /proc */
-           strlcpy (proc_path, "/proc", sizeof(proc_path));
-           proc_path_len = 5;
-
-       } else {
+       if (fp != NULL) {
            while ((mntent = getmntent(fp)) != NULL) {
                if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
                    continue;
-               if (strcmp(mntent->mnt_type, "proc") == 0)
+               if (strcmp(mntent->mnt_type, "proc") == 0) {
+                   strlcpy(proc_path, mntent->mnt_dir, sizeof(proc_path));
+                   proc_path_len = strlen(proc_path);
                    break;
+               }
            }
            fclose (fp);
-           if (mntent == 0)
-               proc_path_len = -1;
-           else {
-               strlcpy(proc_path, mntent->mnt_dir, sizeof(proc_path));
-               proc_path_len = strlen(proc_path);
-           }
        }
     }
 
-    if (proc_path_len < 0)
-       return 0;
-
     strlcpy(proc_path + proc_path_len, tail,
            sizeof(proc_path) - proc_path_len);
     return proc_path;
 }
 
 /*
- * path_to_route - determine the path to the proc file system data
+ * /proc/net/route parsing stuff.
  */
 #define ROUTE_MAX_COLS 12
 FILE *route_fd = (FILE *) 0;
@@ -1116,26 +1117,10 @@ static int route_dev_col, route_dest_col, route_gw_col;
 static int route_flags_col, route_mask_col;
 static int route_num_cols;
 
-static char *path_to_route (void);
 static int open_route_table (void);
 static void close_route_table (void);
 static int read_route_table (struct rtentry *rt);
 
-/********************************************************************
- *
- * path_to_route - find the path to the route tables in the proc file system
- */
-
-static char *path_to_route (void)
-{
-    char *path;
-
-    path = path_to_procfs("/net/route");
-    if (path == 0)
-       error("proc file system not mounted");
-    return path;
-}
-
 /********************************************************************
  *
  * close_route_table - close the interface to the route table
@@ -1161,13 +1146,10 @@ static int open_route_table (void)
 
     close_route_table();
 
-    path = path_to_route();
-    if (path == NULL)
-        return 0;
-
+    path = path_to_procfs("/net/route");
     route_fd = fopen (path, "r");
     if (route_fd == NULL) {
-        error("can't open %s: %m (%d)", path, errno);
+        error("can't open routing table %s: %m", path);
         return 0;
     }
 
@@ -1694,6 +1676,7 @@ int ppp_available(void)
     struct ifreq ifr;
     int    size;
     int    my_version, my_modification, my_patch;
+    int osmaj, osmin, ospatch;
 
     no_ppp_msg = 
        "This system lacks kernel support for PPP.  This could be because\n"
@@ -1703,29 +1686,37 @@ int ppp_available(void)
        "ppp.o exists in /lib/modules/`uname -r`/net.\n"
        "See README.linux file in the ppp distribution for more details.\n";
 
-    fd = open("/dev/ppp", O_RDWR);
-    if (fd < 0 && errno == ENOENT) {
-       /* try making it and see if that helps. */
-       if (mknod("/dev/ppp", S_IFCHR | S_IRUSR | S_IWUSR,
-                 makedev(108, 0)) >= 0) {
-           fd = open("/dev/ppp", O_RDWR);
-           if (fd >= 0)
-               info("Created /dev/ppp device node");
-           else
-               unlink("/dev/ppp");     /* didn't work, undo the mknod */
-       } else if (errno == EEXIST) {
-           fd = open("/dev/ppp", O_RDWR);
+    /* get the kernel version now, since we are called before sys_init */
+    uname(&utsname);
+    osmaj = osmin = ospatch = 0;
+    sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
+    kernel_version = KVERSION(osmaj, osmin, ospatch);
+
+    if (kernel_version >= KVERSION(2,3,13)) {
+       fd = open("/dev/ppp", O_RDWR);
+       if (fd < 0 && errno == ENOENT) {
+           /* try making it and see if that helps. */
+           if (mknod("/dev/ppp", S_IFCHR | S_IRUSR | S_IWUSR,
+                     makedev(108, 0)) >= 0) {
+               fd = open("/dev/ppp", O_RDWR);
+               if (fd >= 0)
+                   info("Created /dev/ppp device node");
+               else
+                   unlink("/dev/ppp"); /* didn't work, undo the mknod */
+           } else if (errno == EEXIST) {
+               fd = open("/dev/ppp", O_RDWR);
+           }
        }
-    }
-    if (fd >= 0) {
-       new_style_driver = 1;
+       if (fd >= 0) {
+           new_style_driver = 1;
 
-       /* XXX should get from driver */
-       driver_version = 2;
-       driver_modification = 4;
-       driver_patch = 0;
-       close(fd);
-       return 1;
+           /* XXX should get from driver */
+           driver_version = 2;
+           driver_modification = 4;
+           driver_patch = 0;
+           close(fd);
+           return 1;
+       }
     }
 
 /*
@@ -2237,7 +2228,7 @@ get_pty(master_fdp, slave_fdp, slave_name, uid)
            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)
+           if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0)
                warn("Couldn't open pty slave %s: %m", pty_name);
        }
     }