X-Git-Url: https://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=pppd%2Fsys-linux.c;h=a1d63452e8347dd9031dd95a1e7e8edffa25a3f8;hp=a5989d3da489b1d79a1e890d17d9187a6ca6661e;hb=2c354e20b0479b7f76309e3d1464e48d8ae2fed7;hpb=713f439d750c5774b1ae86b9b830a35819d00d2e diff --git a/pppd/sys-linux.c b/pppd/sys-linux.c index a5989d3..a1d6345 100644 --- a/pppd/sys-linux.c +++ b/pppd/sys-linux.c @@ -102,6 +102,7 @@ static int ppp_fd = -1; /* fd which is set to PPP discipline */ static int sock_fd = -1; /* socket for doing interface ioctls */ static int slave_fd = -1; static int master_fd = -1; +static int ppp_dev_fd = -1; /* fd for /dev/ppp (new style driver) */ static fd_set in_fds; /* set of fds that wait_input waits for */ static int max_in_fd; /* highest fd set in in_fds */ @@ -114,6 +115,8 @@ static int driver_is_old = 0; static int restore_term = 0; /* 1 => we've munged the terminal */ static struct termios inittermios; /* Initial TTY termios */ +static int new_style_driver = 0; + static char loop_name[20]; static unsigned char inbuf[512]; /* buffer for chars read from loopback */ @@ -122,8 +125,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)) @@ -135,8 +136,8 @@ static int kernel_version; IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP) /* Prototypes for procedures local to this file. */ -static int get_flags (void); -static void set_flags (int flags); +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); @@ -167,18 +168,19 @@ extern u_char inpacket_buf[]; /* borrowed from main.c */ extern int hungup; -#ifndef LOCK_PREFIX -#define LOCK_PREFIX "/var/lock/LCK.." -#endif - +/* new_fd is the fd of a tty */ static void set_ppp_fd (int new_fd) { SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", new_fd)); ppp_fd = new_fd; + if (!new_style_driver) + ppp_dev_fd = new_fd; } static int still_ppp(void) { + if (new_style_driver) + return 1; if (!hungup || ppp_fd == slave_fd) return 1; if (slave_fd >= 0) { @@ -193,11 +195,11 @@ static int still_ppp(void) * Functions to read and set the flags value in the device driver */ -static int get_flags (void) +static int get_flags (int fd) { int flags; - if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) { + if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) { if ( ok_error (errno) ) flags = 0; else @@ -210,13 +212,13 @@ static int get_flags (void) /********************************************************************/ -static void set_flags (int flags) +static void set_flags (int fd, int flags) { SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags)); - if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0) { + if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0) { if (! ok_error (errno) ) - fatal("ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno); + fatal("ioctl(PPPIOCSFLAGS, %x): %m", flags, errno); } } @@ -228,12 +230,23 @@ static void set_flags (int flags) void sys_init(void) { int osmaj, osmin, ospatch; + int flags; openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP); setlogmask(LOG_UPTO(LOG_INFO)); if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); - + + if (new_style_driver) { + ppp_dev_fd = open("/dev/ppp", O_RDWR); + if (ppp_dev_fd < 0) + fatal("Couldn't open /dev/ppp: %m"); + flags = fcntl(ppp_dev_fd, F_GETFL); + if (flags == -1 + || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1) + warn("Couldn't set /dev/ppp to nonblock: %m"); + } + /* Get an internet socket for doing socket ioctls. */ sock_fd = socket(AF_INET, SOCK_DGRAM, 0); if (sock_fd < 0) { @@ -281,6 +294,8 @@ void sys_cleanup(void) void sys_close(void) { + if (new_style_driver) + close(ppp_dev_fd); if (sock_fd >= 0) close(sock_fd); if (slave_fd >= 0) @@ -297,7 +312,7 @@ sys_close(void) static int set_kdebugflag (int requested_level) { - if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0) { + if (ioctl(ppp_dev_fd, PPPIOCSDEBUG, &requested_level) < 0) { if ( ! ok_error (errno) ) error("ioctl(PPPIOCSDEBUG): %m"); return (0); @@ -315,6 +330,7 @@ static int set_kdebugflag (int requested_level) int establish_ppp (int tty_fd) { int x; + /* * The current PPP device will be the tty file. */ @@ -329,54 +345,68 @@ int establish_ppp (int tty_fd) /* * Demand mode - prime the old ppp device to relinquish the unit. */ - if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) + if (!new_style_driver && demand + && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) fatal("ioctl(transfer ppp unit): %m(%d)", errno); /* * Set the current tty to the PPP discpline */ - if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0) { + if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) { if ( ! ok_error (errno) ) fatal("ioctl(TIOCSETD): %m(%d)", errno); } /* * Find out which interface we were given. */ - if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0) { - if ( ! ok_error (errno)) - fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno); - } -/* - * Check that we got the same unit again. - */ - if (demand) { - if (x != ifunit) + if (new_style_driver) { + if (!demand) { + /* allocate ourselves a ppp unit */ + ifunit = -1; + if (ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit) < 0) + fatal("Couldn't create new ppp unit: %m"); + set_kdebugflag(kdebugflag); + } else { + set_flags(ppp_dev_fd, get_flags(ppp_dev_fd) & ~SC_LOOP_TRAFFIC); + } + if (ioctl(tty_fd, PPPIOCATTACH, &ifunit) < 0) { + if (errno == EIO) + return -1; + fatal("Couldn't attach tty to PPP unit %d: %m", ifunit); + } + } else { + if (ioctl(tty_fd, PPPIOCGUNIT, &x) < 0) { + if ( ! ok_error (errno)) + fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno); + } + /* Check that we got the same unit again. */ + if (demand && x != ifunit) fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x); + ifunit = x; } - ifunit = x; /* * Enable debug in the driver if requested. */ if (!demand) set_kdebugflag (kdebugflag); - set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 | - SC_RCV_EVNP | SC_RCV_ODDP)); + set_flags(tty_fd, get_flags(tty_fd) & ~(SC_RCV_B7_0 | SC_RCV_B7_1 | + SC_RCV_EVNP | SC_RCV_ODDP)); SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver", driver_version, driver_modification, driver_patch)); + /* * Fetch the initial file flags and reset blocking mode on the file. */ - initfdflags = fcntl(ppp_fd, F_GETFL); - + initfdflags = fcntl(tty_fd, F_GETFL); if (initfdflags == -1 || - fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) { + fcntl(tty_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) { if ( ! ok_error (errno)) warn("Couldn't set device to non-blocking mode: %m"); } - return ppp_fd; + return ppp_dev_fd; } /******************************************************************** @@ -387,10 +417,12 @@ int establish_ppp (int tty_fd) void disestablish_ppp(int tty_fd) { + if (!hungup) { /* - * Attempt to restore the previous tty settings + * Flush the tty output buffer so that the TIOCSETD doesn't hang. */ - if (!hungup) { + if (tcflush(tty_fd, TCIOFLUSH) < 0) + warn("tcflush failed: %m"); /* * Restore the previous line discipline */ @@ -428,16 +460,10 @@ void clean_check(void) s = NULL; switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) { case SC_RCV_B7_0: - case SC_RCV_B7_0 | SC_RCV_EVNP: - case SC_RCV_B7_0 | SC_RCV_ODDP: - case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP: s = "all had bit 7 set to 1"; break; case SC_RCV_B7_1: - case SC_RCV_B7_1 | SC_RCV_EVNP: - case SC_RCV_B7_1 | SC_RCV_ODDP: - case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP: s = "all had bit 7 set to 0"; break; @@ -701,8 +727,14 @@ void output (int unit, unsigned char *p, int len) { if (debug) dbglog("sent %P", p, len); - - if (write(ppp_fd, p, len) < 0) { + + if (len < PPP_HDRLEN) + return; + if (new_style_driver) { + p += 2; + len -= 2; + } + if (write(ppp_dev_fd, p, len) < 0) { if (errno == EWOULDBLOCK || errno == ENOBUFS || errno == ENXIO || errno == EIO || errno == EINTR) warn("write: warning: %m (%d)", errno); @@ -747,42 +779,6 @@ void remove_fd(int fd) FD_CLR(fd, &in_fds); } -#if 0 -/******************************************************************** - * - * wait_loop_output - wait until there is data available on the - * loopback, for the length of time specified by *timo (indefinite - * if timo is NULL). - */ -void wait_loop_output(timo) - struct timeval *timo; -{ - fd_set ready; - int n; - - FD_ZERO(&ready); - FD_SET(master_fd, &ready); - n = select(master_fd + 1, &ready, NULL, &ready, timo); - if (n < 0 && errno != EINTR) - fatal("select: %m(%d)", errno); -} - -/******************************************************************** - * - * wait_time - wait for a given length of time or until a - * signal is received. - */ - -void wait_time(timo) - struct timeval *timo; -{ - int n; - - n = select(0, NULL, NULL, NULL, timo); - if (n < 0 && errno != EINTR) - fatal("select: %m(%d)", errno); -} -#endif /******************************************************************** * @@ -792,14 +788,20 @@ void wait_time(timo) int read_packet (unsigned char *buf) { int len; - - len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN); + + len = PPP_MRU + PPP_HDRLEN; + if (new_style_driver) { + *buf++ = PPP_ALLSTATIONS; + *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); } - return len; + return new_style_driver? len+2: len; } /******************************************************************** @@ -812,20 +814,25 @@ int get_loop_output(void) { int rv = 0; - int n = read(master_fd, inbuf, sizeof(inbuf)); + int n; + + if (new_style_driver) { + while ((n = read_packet(inpacket_buf)) > 0) + if (loop_frame(inpacket_buf, n)) + rv = 1; + return rv; + } - while (n > 0) { + while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0) if (loop_chars(inbuf, n)) rv = 1; - n = read(master_fd, inbuf, sizeof(inbuf)); - } if (n == 0) fatal("eof on loopback"); if (errno != EWOULDBLOCK) fatal("read from loopback: %m(%d)", errno); - + return rv; } @@ -863,11 +870,11 @@ void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp) return; } - x = get_flags(); + x = get_flags(ppp_fd); 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); + set_flags(ppp_fd, x); } /******************************************************************** @@ -894,8 +901,6 @@ void ppp_set_xaccm (int unit, ext_accm accm) void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp) { - u_int x; - SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru)); /* * If we were called because the link has gone down then there is nothing @@ -910,16 +915,14 @@ void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp) if ( ! ok_error (errno)) error("ioctl(PPPIOCSMRU): %m(%d)", errno); } + if (new_style_driver && ioctl(ppp_dev_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) + error("Couldn't set MRU in generic PPP layer: %m"); SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap)); if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) { if (!ok_error(errno)) error("ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno); } - - x = get_flags(); - x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC; - set_flags (x); } /******************************************************************** @@ -937,7 +940,7 @@ int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit) data.length = opt_len; data.transmit = for_transmit; - if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0) + if (ioctl(ppp_dev_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0) return 1; return (errno == ENOBUFS)? 0: -1; @@ -951,10 +954,10 @@ int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit) void ccp_flags_set (int unit, int isopen, int isup) { if (still_ppp()) { - int x = get_flags(); + int x = get_flags(ppp_dev_fd); x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN; x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP; - set_flags (x); + set_flags (ppp_dev_fd, x); } } @@ -967,7 +970,7 @@ get_idle_time(u, ip) int u; struct ppp_idle *ip; { - return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0; + return ioctl(ppp_dev_fd, PPPIOCGIDLE, ip) >= 0; } /******************************************************************** @@ -1003,7 +1006,7 @@ get_ppp_stats(u, stats) int ccp_fatal_error (int unit) { - int x = get_flags(); + int x = get_flags(ppp_dev_fd); return x & SC_DC_FERROR; } @@ -1028,7 +1031,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; @@ -1037,6 +1040,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; } @@ -1051,6 +1055,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; } @@ -1061,11 +1066,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); } @@ -1160,6 +1164,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 = @@ -1570,48 +1575,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); - return 0; - } - - initfdflags = fcntl(local_fd, F_GETFL); - if (initfdflags == -1) { - error("Couldn't get device fd flags: %m(%d)", errno); - close (local_fd); + 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 &= ~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; } /******************************************************************** @@ -1622,11 +1612,31 @@ ppp_registered(void) int ppp_available(void) { - int s, ok; + int s, ok, fd; 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"; + + fd = open("/dev/ppp", O_RDWR); + 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; + } + /* * Open a socket for doing the ioctl operations. */ @@ -1656,18 +1666,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.7 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; @@ -1746,10 +1749,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); @@ -1758,7 +1761,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) @@ -1786,143 +1789,6 @@ void logwtmp (const char *line, const char *name, const char *host) } } -/******************************************************************** - * Code for locking/unlocking the serial device. - * This code is derived from chat.c. - */ - -/* - * lock - create a lock file for the named device - */ - -int lock (char *dev) -{ -#ifdef LOCKLIB - int result; - lock_file = strdup(dev); - if (lock_file == NULL) - novm("lock file name"); - result = mklock (dev, (void *) 0); - - if (result > 0) { - notice("Device %s is locked by pid %d", dev, result); - free (lock_file); - lock_file = NULL; - result = -1; - } - else { - if (result < 0) { - error("Can't create lock file %s", lock_file); - free (lock_file); - lock_file = NULL; - result = -1; - } - } - return (result); -#else - char hdb_lock_buffer[12]; - int fd, n; - int pid = getpid(); - char *p; - size_t l; - - p = strrchr(dev, '/'); - if (p != NULL) - dev = ++p; - - l = strlen(LOCK_PREFIX) + strlen(dev) + 1; - lock_file = malloc(l); - if (lock_file == NULL) - novm("lock file name"); - - slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev); -/* - * Attempt to create the lock file at this point. - */ - while (1) { - fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644); - if (fd >= 0) { - pid = getpid(); -#ifndef PID_BINARY - slprintf(hdb_lock_buffer, sizeof(hdb_lock_buffer), "%010d\n", pid); - write (fd, hdb_lock_buffer, 11); -#else - write(fd, &pid, sizeof (pid)); -#endif - close(fd); - return 0; - } -/* - * If the file exists then check to see if the pid is stale - */ - if (errno == EEXIST) { - fd = open(lock_file, O_RDONLY, 0); - if (fd < 0) { - if (errno == ENOENT) /* This is just a timing problem. */ - continue; - break; - } - - /* Read the lock file to find out who has the device locked */ - n = read (fd, hdb_lock_buffer, 11); - close (fd); - if (n < 0) { - error("Can't read pid from lock file %s", lock_file); - break; - } - - /* See the process still exists. */ - if (n > 0) { -#ifndef PID_BINARY - hdb_lock_buffer[n] = '\0'; - sscanf (hdb_lock_buffer, " %d", &pid); -#else - pid = ((int *) hdb_lock_buffer)[0]; -#endif - if (pid == 0 || pid == getpid() - || (kill(pid, 0) == -1 && errno == ESRCH)) - n = 0; - } - - /* If the process does not exist then try to remove the lock */ - if (n == 0 && unlink (lock_file) == 0) { - notice("Removed stale lock on %s (pid %d)", - dev, pid); - continue; - } - - notice("Device %s is locked by pid %d", dev, pid); - break; - } - - error("Can't create lock file %s: %m(%d)", lock_file, errno); - break; - } - - free(lock_file); - lock_file = NULL; - return -1; -#endif -} - - -/******************************************************************** - * - * unlock - remove our lockfile - */ - -void unlock(void) -{ - if (lock_file) { -#ifdef LOCKLIB - (void) rmlock (lock_file, (void *) 0); -#else - unlink(lock_file); -#endif - free(lock_file); - lock_file = NULL; - } -} /******************************************************************** * @@ -1931,10 +1797,10 @@ void unlock(void) int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid) { - u_int x = get_flags(); + u_int x = get_flags(ppp_dev_fd); if (vjcomp) { - if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) { + if (ioctl (ppp_dev_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) { if (! ok_error (errno)) error("ioctl(PPPIOCSMAXCID): %m(%d)", errno); vjcomp = 0; @@ -1943,7 +1809,7 @@ int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid) x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP; x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID; - set_flags (x); + set_flags (ppp_dev_fd, x); return 1; } @@ -2126,7 +1992,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) @@ -2135,31 +2001,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; @@ -2184,6 +2075,17 @@ open_ppp_loopback(void) { int flags; + if (new_style_driver) { + /* allocate ourselves a ppp unit */ + ifunit = -1; + if (ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit) < 0) + fatal("Couldn't create PPP unit: %m"); + set_flags(ppp_dev_fd, SC_LOOP_TRAFFIC); + set_kdebugflag(kdebugflag); + ppp_fd = -1; + return ppp_dev_fd; + } + if (!get_pty(&master_fd, &slave_fd, loop_name, 0)) fatal("No free pty for loopback"); SYSDEBUG(("using %s for loopback", loop_name)); @@ -2231,6 +2133,10 @@ open_ppp_loopback(void) void restore_loop(void) { + if (new_style_driver) { + set_flags(ppp_dev_fd, get_flags(ppp_dev_fd) & SC_LOOP_TRAFFIC); + return; + } if (ppp_fd != slave_fd) { (void) ioctl(ppp_fd, TIOCSETD, &tty_disc); set_ppp_fd(slave_fd); @@ -2252,11 +2158,11 @@ sifnpmode(u, proto, mode) npi.protocol = proto; npi.mode = mode; - if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) { + if (ioctl(ppp_dev_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) { if (! ok_error (errno)) { error("ioctl(PPPIOCSNPMODE, %d, %d): %m (%d)", proto, mode, errno); - error("ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd); + error("ppp_dev_fd=%d slave_fd=%d\n", ppp_dev_fd, slave_fd); } return 0; } @@ -2356,30 +2262,6 @@ int cipxfaddr (int unit) return result; } -/* - * daemon - Detach us from controlling terminal session. - */ -int -daemon(nochdir, noclose) - int nochdir, noclose; -{ - int pid; - - if ((pid = fork()) < 0) - return -1; - if (pid != 0) - exit(0); /* parent dies */ - setsid(); - if (!nochdir) - chdir("/"); - if (!noclose) { - fclose(stdin); /* don't need stdin, stdout, stderr */ - fclose(stdout); - fclose(stderr); - } - return 0; -} - /* * Use the hostname as part of the random number seed. */ @@ -2404,21 +2286,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) {