]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-osf.c
make sure fds 0, 1, 2 are always open
[ppp.git] / pppd / sys-osf.c
index f11a9961a418056e3dba54c136f87f2e1d07e936..6d882a95c3af914e9bf105ced7c78d7df019de7a 100644 (file)
@@ -26,7 +26,7 @@
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: sys-osf.c,v 1.19 1999/03/16 02:57:06 paulus Exp $";
+static char rcsid[] = "$Id: sys-osf.c,v 1.27 1999/05/12 06:16:16 paulus Exp $";
 #endif
 
 #include <stdio.h>
@@ -200,7 +200,7 @@ sys_check_options()
     return 1;
 }
 
-
+#if 0
 /*
  * daemon - Detach us from controlling terminal session.
  */
@@ -224,19 +224,7 @@ daemon(nochdir, noclose)
     }
     return 0;
 }
-
-/*
- * note_debug_level - note a change in the debug level.
- */
-void
-note_debug_level()
-{
-    if (debug) {
-       setlogmask(LOG_UPTO(LOG_DEBUG));
-    } else {
-       setlogmask(LOG_UPTO(LOG_WARNING));
-    }
-}
+#endif
 
 /*
  * ppp_available - check whether the system has any ppp interfaces
@@ -344,7 +332,7 @@ streamify(int fd)
 /*
  * establish_ppp - Turn the serial port into a ppp interface.
  */
-void
+int
 establish_ppp(fd)
     int fd;
 {
@@ -387,15 +375,25 @@ establish_ppp(fd)
             if (i != fd && i != sockfd)
                 close(i);
         closed_stdio = 1;
+       /* make sure 0, 1, 2 are open to /dev/null */
+       while ((i = open("/dev/null", O_RDWR)) >= 0) {
+           if (i > 2) {
+               close(i);
+               break;
+           }
+       }
     }
 
     /*
      * Set device for non-blocking reads.
+     * XXX why do we need to do this?  don't we use pppfd not fd?
      */
     if ((initfdflags = fcntl(fd, F_GETFL)) == -1
         || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
         warn("Couldn't set device to non-blocking mode: %m");
     }
+
+    return pppfd;
 }
 
 /*
@@ -704,12 +702,13 @@ int fd, on;
 
 /*
  * open_loopback - open the device we use for getting packets
- * in demand mode.  Under Solaris 2, we use our existing fd
+ * in demand mode.  Under Digital Unix, we use our existing fd
  * to the ppp driver.
  */
-void
+int
 open_ppp_loopback()
 {
+    return pppfd;
 }
 
 /*
@@ -763,7 +762,8 @@ wait_input(timo)
 /*
  * add_fd - add an fd to the set that wait_input waits for.
  */
-void add_fd(int fd)
+void add_fd(fd)
+    int fd;
 {
     int n;
 
@@ -781,7 +781,8 @@ void add_fd(int fd)
 /*
  * remove_fd - remove an fd from the set that wait_input waits for.
  */
-void remove_fd(int fd)
+void remove_fd(fd)
+    int fd;
 {
     int n;
 
@@ -1018,6 +1019,25 @@ get_idle_time(u, ip)
     return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
 }
 
+/*
+ * get_ppp_stats - return statistics for the link.
+ */
+int
+get_ppp_stats(u, stats)
+    int u;
+    struct pppd_stats *stats;
+{
+    struct ppp_stats s;
+
+    if (strioctl(pppfd, PPPIO_GETSTAT, &s, 0, sizeof(s)) < 0) {
+       error("Couldn't get link statistics: %m");
+       return 0;
+    }
+    stats->bytes_in = s.p.ppp_ibytes;
+    stats->bytes_out = s.p.ppp_obytes;
+    return 1;
+}
+
 
 /*
  * ccp_fatal_error - returns 1 if decompression was disabled as a
@@ -1077,7 +1097,7 @@ sifup(u)
 {
     struct ifreq ifr;
 
-    strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
+    strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
        error("Couldn't mark interface up (get): %m");
        return 0;
@@ -1100,7 +1120,8 @@ sifdown(u)
 {
     struct ifreq ifr;
 
-    strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
+    bzero(&ifr, sizeof(ifr));
+    strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
        error("Couldn't mark interface down (get): %m");
        return 0;
@@ -1163,7 +1184,7 @@ sifaddr(u, o, h, m)
     /* flush old address, if any
      */
     bzero(&ifr, sizeof (ifr));
-    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
+    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
     if ((ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0)
@@ -1173,7 +1194,7 @@ sifaddr(u, o, h, m)
     }
 
     bzero(&addreq, sizeof (addreq));
-    strlcpy(addreq.ifra_name, sizeof (addreq.ifra_name), ifname);
+    strlcpy(addreq.ifra_name, ifname, sizeof (addreq.ifra_name));
     SET_SA_FAMILY(addreq.ifra_addr, AF_INET);
     SET_SA_FAMILY(addreq.ifra_broadaddr, AF_INET);
     ((struct sockaddr_in *)&addreq.ifra_addr)->sin_addr.s_addr = o;
@@ -1192,7 +1213,8 @@ sifaddr(u, o, h, m)
         ret = 0;
     }
 
-    ifr.ifr_metric = link_mtu;
+    ifr.ifr_data = (caddr_t)&link_mtu;
+
     if (ioctl(sockfd, SIOCSIPMTU, &ifr) < 0) {
        error("Couldn't set IP MTU: %m");
         ret = 0;
@@ -1218,7 +1240,7 @@ cifaddr(u, o, h)
     ifaddrs[0] = 0;
     ifaddrs[1] = 0;
     bzero(&ifr, sizeof (ifr));
-    strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
+    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
     if (ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0) {
@@ -1368,7 +1390,7 @@ get_ether_addr(ipaddr, hwaddr)
              * Check that the interface is up, and not point-to-point
              * or loopback.
              */
-            strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
+            strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
                 continue;
             if ((ifreq.ifr_flags &
@@ -1397,7 +1419,7 @@ get_ether_addr(ipaddr, hwaddr)
        return 0;
     info("found interface %s for proxy arp", ifr->ifr_name);
 
-    strlcpy(ifdevreq.ifr_name, sizeof(ifdevreq.ifr_name), ifr->ifr_name);
+    strlcpy(ifdevreq.ifr_name, ifr->ifr_name, sizeof(ifdevreq.ifr_name));
 
     if (ioctl(sockfd, (int)SIOCRPHYSADDR, &ifdevreq) < 0) {
         perror("ioctl(SIOCRPHYSADDR)");
@@ -1422,9 +1444,9 @@ logwtmp(line, name, host)
     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
        return;
     if (!fstat(fd, &buf)) {
-       strlcpy(ut.ut_line, sizeof(ut.ut_line), line);
-       strlcpy(ut.ut_name, sizeof(ut.ut_name), name);
-       strlcpy(ut.ut_host, sizeof(ut.ut_host), host);
+       strncpy(ut.ut_line, line, sizeof(ut.ut_line));
+       strncpy(ut.ut_name, name, sizeof(ut.ut_name));
+       strncpy(ut.ut_host, host, sizeof(ut.ut_host));
        (void)time(&ut.ut_time);
        if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
            (void)ftruncate(fd, buf.st_size);
@@ -1481,7 +1503,7 @@ GetMask(addr)
            /*
             * Check that the interface is up, and not point-to-point or loopback.
             */
-           strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
+           strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
            if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
                continue;
            if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
@@ -1596,6 +1618,57 @@ get_host_seed()
     return gethostid();
 }
 
+/*
+ * 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.
+ */
+int
+get_pty(master_fdp, slave_fdp, slave_name, uid)
+    int *master_fdp;
+    int *slave_fdp;
+    char *slave_name;
+    int uid;
+{
+    int i, mfd, sfd;
+    char pty_name[12];
+    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);
+       }
+    }
+    if (sfd < 0)
+       return 0;
+
+    strlcpy(slave_name, pty_name, 12);
+    *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;
+       tios.c_iflag  = IGNPAR | CLOCAL;
+       tios.c_oflag  = 0;
+       tios.c_lflag  = 0;
+       if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
+           warn("couldn't set attributes on pty: %m");
+    } else
+       warn("couldn't get attributes on pty: %m");
+
+    return 1;
+}
+
+#if 0
 /*
  * Code for locking/unlocking the serial device.
  * This code is derived from chat.c.
@@ -1699,6 +1772,7 @@ unlock()
        lock_file = NULL;
     }
 }
+#endif
 
 int
 set_filters(pass, active)