]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-linux.c
improve ppp not available message
[ppp.git] / pppd / sys-linux.c
index ba25a93ec843a653635adda286955dc3a603da99..7cae7ce780f58b5bee07710e298ab6fb2ab842b2 100644 (file)
@@ -122,8 +122,6 @@ static u_int32_t default_route_gateway;     /* Gateway for default route added */
 static u_int32_t proxy_arp_addr;       /* Addr for proxy arp entry added */
 static char proxy_arp_dev[16];         /* Device for proxy arp entry */
 
-static char *lock_file;
-
 static struct utsname utsname; /* for the kernel version */
 static int kernel_version;
 #define KVERSION(j,n,p)        ((j)*1000000 + (n)*1000 + (p))
@@ -167,10 +165,6 @@ extern u_char      inpacket_buf[]; /* borrowed from main.c */
 
 extern int hungup;
 
-#ifndef LOCK_PREFIX
-#define LOCK_PREFIX    "/var/lock/LCK.."
-#endif
-
 static void set_ppp_fd (int new_fd)
 {
        SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", new_fd));
@@ -281,8 +275,12 @@ void sys_cleanup(void)
 void
 sys_close(void)
 {
-    close(sock_fd);
-    sock_fd = -1;
+    if (sock_fd >= 0)
+       close(sock_fd);
+    if (slave_fd >= 0)
+       close(slave_fd);
+    if (master_fd >= 0)
+       close(master_fd);
     closelog();
 }
 
@@ -383,10 +381,20 @@ int establish_ppp (int tty_fd)
 
 void disestablish_ppp(int tty_fd)
 {
+    int nout;
+
+    if (!hungup) {
 /*
- * Attempt to restore the previous tty settings
+ * 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.
  */
-    if (!hungup) {
+       do {
+           if (tcflush(tty_fd, TCIOFLUSH) < 0)
+               break;
+           nout = 0;
+       } while (ioctl(tty_fd, TIOCOUTQ, &nout) >= 0 && nout > 0);
 /*
  * Restore the previous line discipline
  */
@@ -862,6 +870,7 @@ void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
     x = get_flags();
     x = pcomp  ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
     x = accomp ? x | SC_COMP_AC   : x & ~SC_COMP_AC;
+    x = sync_serial ? x | SC_SYNC : x & ~SC_SYNC;
     set_flags(x);
 }
 
@@ -1565,48 +1574,33 @@ static int
 ppp_registered(void)
 {
     int local_fd;
-    int init_disc = -1;
-    int initfdflags;
-
-    local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
-    if (local_fd < 0) {
-       error("Failed to open %s: %m(%d)", devnam, errno);
+    int mfd = -1;
+    int ret = 0;
+    char slave[16];
+
+    /*
+     * We used to open the serial device and set it to the ppp line
+     * discipline here, in order to create a ppp unit.  But that is
+     * not a good idea - the user might have specified a device that
+     * they can't open (permission, or maybe it doesn't really exist).
+     * So we grab a pty master/slave pair and use that.
+     */
+    if (!get_pty(&mfd, &local_fd, slave, 0)) {
+       no_ppp_msg = "Couldn't determine if PPP is supported (no free ptys)";
        return 0;
     }
 
-    initfdflags = fcntl(local_fd, F_GETFL);
-    if (initfdflags == -1) {
-       error("Couldn't get device fd flags: %m(%d)", errno);
-       close (local_fd);
-       return 0;
-    }
-
-    initfdflags &= ~O_NONBLOCK;
-    fcntl(local_fd, F_SETFL, initfdflags);
-/*
- * Read the initial line dicipline and try to put the device into the
- * PPP dicipline.
- */
-    if (ioctl(local_fd, TIOCGETD, &init_disc) < 0) {
-       error("ioctl(TIOCGETD): %m(%d)", errno);
-       close (local_fd);
-       return 0;
-    }
-    
+    /*
+     * Try to put the device into the PPP discipline.
+     */
     if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0) {
-       error("ioctl(TIOCSETD): %m(%d)", errno);
-       close (local_fd);
-       return 0;
-    }
-    
-    if (ioctl(local_fd, TIOCSETD, &init_disc) < 0) {
-       error("ioctl(TIOCSETD): %m(%d)", errno);
-       close (local_fd);
-       return 0;
-    }
+       error("ioctl(TIOCSETD(PPP)): %m(%d)", errno);
+    } else
+       ret = 1;
     
-    close (local_fd);
-    return 1;
+    close(local_fd);
+    close(mfd);
+    return ret;
 }
 
 /********************************************************************
@@ -1621,7 +1615,15 @@ int ppp_available(void)
     struct ifreq ifr;
     int    size;
     int    my_version, my_modification, my_patch;
-    extern char *no_ppp_msg;
+
+    no_ppp_msg = 
+       "This system lacks kernel support for PPP.  This could be because\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.
  */    
@@ -1651,18 +1653,11 @@ int ppp_available(void)
     if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
         ok = 0;
 
-    if (!ok)
-       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.6 distribution.\n";
-
 /*
  *  This is the PPP device. Validate the version of the driver at this
  *  point to ensure that this program will work with the driver.
  */
-    else {
+    if (ok) {
        char   abBuffer [1024];
 
        ifr.ifr_data = abBuffer;
@@ -1741,10 +1736,10 @@ void logwtmp (const char *line, const char *name, const char *host)
        memset(&ut, 0, sizeof(ut));
 
     if (ut.ut_id[0] == 0)
-       strlcpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
+       strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
        
-    strlcpy(ut.ut_user, name, sizeof(ut.ut_user));
-    strlcpy(ut.ut_line, line, sizeof(ut.ut_line));
+    strncpy(ut.ut_user, name, sizeof(ut.ut_user));
+    strncpy(ut.ut_line, line, sizeof(ut.ut_line));
 
     time(&ut.ut_time);
 
@@ -1753,7 +1748,7 @@ void logwtmp (const char *line, const char *name, const char *host)
 
     /* Insert the host name if one is supplied */
     if (*host)
-       strlcpy (ut.ut_host, host, sizeof(ut.ut_host));
+       strncpy (ut.ut_host, host, sizeof(ut.ut_host));
 
     /* Insert the IP address of the remote system if IP is enabled */
     if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
@@ -1781,11 +1776,18 @@ void logwtmp (const char *line, const char *name, const char *host)
     }
 }
 
+#if 0
 /********************************************************************
  * Code for locking/unlocking the serial device.
  * This code is derived from chat.c.
  */
 
+#ifndef LOCK_PREFIX
+#define LOCK_PREFIX    "/var/lock/LCK.."
+#endif
+
+static char *lock_file;
+
 /*
  * lock - create a lock file for the named device
  */
@@ -1918,6 +1920,7 @@ void unlock(void)
        lock_file = NULL;
     }
 }
+#endif
 
 /********************************************************************
  *
@@ -2025,10 +2028,10 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
     if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
        if (errno != EEXIST) {
            if (! ok_error (errno))
-               error("ioctl(SIOCAIFADDR): %m(%d)", errno);
+               error("ioctl(SIOCSIFADDR): %m(%d)", errno);
        }
         else {
-           warn("ioctl(SIOCAIFADDR): Address already exists");
+           warn("ioctl(SIOCSIFADDR): Address already exists");
        }
         return (0);
     }
@@ -2296,10 +2299,10 @@ int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
            result = 0;
            if (errno != EEXIST) {
                if (! ok_error (errno))
-                   dbglog("ioctl(SIOCAIFADDR, CRTITF): %m (%d)", errno);
+                   dbglog("ioctl(SIOCSIFADDR, CRTITF): %m (%d)", errno);
            }
            else {
-               warn("ioctl(SIOCAIFADDR, CRTITF): Address already exists");
+               warn("ioctl(SIOCSIFADDR, CRTITF): Address already exists");
            }
        }
        close (skfd);
@@ -2342,7 +2345,7 @@ int cipxfaddr (int unit)
  */
        if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
            if (! ok_error (errno))
-               info("ioctl(SIOCAIFADDR, IPX_DLTITF): %m (%d)", errno);
+               info("ioctl(SIOCSIFADDR, IPX_DLTITF): %m (%d)", errno);
            result = 0;
        }
        close (skfd);
@@ -2351,6 +2354,7 @@ int cipxfaddr (int unit)
     return result;
 }
 
+#if 0
 /*
  * daemon - Detach us from controlling terminal session.
  */
@@ -2374,6 +2378,7 @@ daemon(nochdir, noclose)
     }
     return 0;
 }
+#endif
 
 /*
  * Use the hostname as part of the random number seed.