From 35495bec8dc4bde36f113ce296f7726a99f9db78 Mon Sep 17 00:00:00 2001 From: Brian Somers Date: Mon, 22 Sep 1997 00:46:56 +0000 Subject: [PATCH] Correct the way the uucp lock file and the ttyXX.if lock file get created. We don't create lock files over non-tty connections, but we *do* create lock files in -direct mode. This leaves us capable of adding utmp/wtmp support for successful pap & chap logins (coming soon). --- usr.sbin/ppp/command.c | 28 +++++-------- usr.sbin/ppp/command.h | 14 ++++++- usr.sbin/ppp/main.c | 35 ++++++---------- usr.sbin/ppp/modem.c | 92 ++++++++++++++++++++++++++++++++---------- 4 files changed, 108 insertions(+), 61 deletions(-) diff --git a/usr.sbin/ppp/command.c b/usr.sbin/ppp/command.c index 950e6275f043..483a3bc5b770 100644 --- a/usr.sbin/ppp/command.c +++ b/usr.sbin/ppp/command.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: command.c,v 1.82 1997/09/17 23:17:52 brian Exp $ + * $Id: command.c,v 1.83 1997/09/21 13:07:57 brian Exp $ * */ #include @@ -1192,17 +1192,7 @@ SetNBNS(struct cmdtab const * list, int argc, char **argv) #endif /* MS_EXT */ -#define VAR_AUTHKEY 0 -#define VAR_DIAL 1 -#define VAR_LOGIN 2 -#define VAR_AUTHNAME 3 -#define VAR_DEVICE 4 -#define VAR_ACCMAP 5 -#define VAR_PHONE 6 -#define VAR_HANGUP 7 -#define VAR_ENC 8 - -static int +int SetVariable(struct cmdtab const * list, int argc, char **argv, int param) { u_long map; @@ -1231,11 +1221,15 @@ SetVariable(struct cmdtab const * list, int argc, char **argv, int param) VarLoginScript[sizeof(VarLoginScript) - 1] = '\0'; break; case VAR_DEVICE: - CloseModem(); - strncpy(VarDevice, arg, sizeof(VarDevice) - 1); - VarDevice[sizeof(VarDevice) - 1] = '\0'; - VarBaseDevice = rindex(VarDevice, '/'); - VarBaseDevice = VarBaseDevice ? VarBaseDevice + 1 : ""; + if (modem != -1) + LogPrintf(LogWARN, "Cannot change device to \"%s\" when \"%s\" is open\n", + arg, VarDevice); + else { + strncpy(VarDevice, arg, sizeof(VarDevice) - 1); + VarDevice[sizeof(VarDevice) - 1] = '\0'; + VarBaseDevice = rindex(VarDevice, '/'); + VarBaseDevice = VarBaseDevice ? VarBaseDevice + 1 : ""; + } break; case VAR_ACCMAP: sscanf(arg, "%lx", &map); diff --git a/usr.sbin/ppp/command.h b/usr.sbin/ppp/command.h index 04f918e0998b..bc9f67f9dfed 100644 --- a/usr.sbin/ppp/command.h +++ b/usr.sbin/ppp/command.h @@ -15,7 +15,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: command.h,v 1.4 1997/02/22 16:10:09 peter Exp $ + * $Id: command.h,v 1.5 1997/08/25 00:29:09 brian Exp $ * * TODO: */ @@ -29,3 +29,15 @@ struct cmdtab { char *syntax; void *args; }; + +#define VAR_AUTHKEY 0 +#define VAR_DIAL 1 +#define VAR_LOGIN 2 +#define VAR_AUTHNAME 3 +#define VAR_DEVICE 4 +#define VAR_ACCMAP 5 +#define VAR_PHONE 6 +#define VAR_HANGUP 7 +#define VAR_ENC 8 + +extern int SetVariable(struct cmdtab const *, int, char **, int var_param); diff --git a/usr.sbin/ppp/main.c b/usr.sbin/ppp/main.c index ea257a56e3e0..8eb55fa9edd4 100644 --- a/usr.sbin/ppp/main.c +++ b/usr.sbin/ppp/main.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: main.c,v 1.79 1997/09/18 00:15:25 brian Exp $ + * $Id: main.c,v 1.80 1997/09/21 20:26:46 brian Exp $ * * TODO: * o Add commands for traffic summary, version display, etc. @@ -448,27 +448,6 @@ main(int argc, char **argv) } else if (mode & MODE_BACKGROUND) close(BGFiledes[0]); } - snprintf(pid_filename, sizeof(pid_filename), "%stun%d.pid", - _PATH_VARRUN, tunno); - (void) unlink(pid_filename); - - if ((lockfile = fopen(pid_filename, "w")) != NULL) { - fprintf(lockfile, "%d\n", (int) getpid()); - fclose(lockfile); - } else - LogPrintf(LogALERT, "Warning: Can't create %s: %s\n", - pid_filename, strerror(errno)); - - snprintf(if_filename, sizeof if_filename, "%s%s.if", - _PATH_VARRUN, VarBaseDevice); - (void) unlink(if_filename); - - if ((lockfile = fopen(if_filename, "w")) != NULL) { - fprintf(lockfile, "tun%d\n", tunno); - fclose(lockfile); - } else - LogPrintf(LogALERT, "Warning: Can't create %s: %s\n", - if_filename, strerror(errno)); VarTerm = 0; /* We know it's currently stdout */ close(1); @@ -488,6 +467,18 @@ main(int argc, char **argv) TtyInit(0); TtyCommandMode(1); } + + snprintf(pid_filename, sizeof(pid_filename), "%stun%d.pid", + _PATH_VARRUN, tunno); + (void) unlink(pid_filename); + + if ((lockfile = fopen(pid_filename, "w")) != NULL) { + fprintf(lockfile, "%d\n", (int) getpid()); + fclose(lockfile); + } else + LogPrintf(LogALERT, "Warning: Can't create %s: %s\n", + pid_filename, strerror(errno)); + LogPrintf(LogPHASE, "PPP Started.\n"); diff --git a/usr.sbin/ppp/modem.c b/usr.sbin/ppp/modem.c index 9cd0098c2290..c74da3bd440f 100644 --- a/usr.sbin/ppp/modem.c +++ b/usr.sbin/ppp/modem.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: modem.c,v 1.53 1997/09/18 00:15:25 brian Exp $ + * $Id: modem.c,v 1.54 1997/09/21 20:26:47 brian Exp $ * * TODO: */ @@ -31,6 +31,7 @@ #include #include #include +#include #ifdef __OpenBSD__ #include #else @@ -42,6 +43,7 @@ #include "modem.h" #include "loadalias.h" #include "vars.h" +#include "command.h" #ifndef O_NONBLOCK #ifdef O_NDELAY @@ -428,6 +430,53 @@ OpenConnection(char *host, char *port) return (sock); } +extern int tunno; +static char fn[MAXPATHLEN]; + +int +LockModem() +{ + int res; + FILE *lockfile; + + if (*VarDevice != '/') + return 0; + + if ((res = uu_lock(VarBaseDevice)) != UU_LOCK_OK) { + if (res == UU_LOCK_INUSE) + LogPrintf(LogPHASE, "Modem %s is in use\n", VarDevice); + else + LogPrintf(LogPHASE, "Modem %s is in use: uu_lock: %s\n", + VarDevice, uu_lockerr(res)); + return (-1); + } + + snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, VarBaseDevice); + (void) unlink(fn); + + if ((lockfile = fopen(fn, "w")) != NULL) { + fprintf(lockfile, "tun%d\n", tunno); + fclose(lockfile); + } else + LogPrintf(LogALERT, "Warning: Can't create %s: %s\n", fn, strerror(errno)); + + return 0; +} + +void +UnlockModem() +{ + if (*VarDevice != '/') + return; + + snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, VarBaseDevice); + if (unlink(fn) == -1) + LogPrintf(LogALERT, "Warning: Can't remove %s: %s\n", fn, strerror(errno)); + + if (uu_unlock(VarBaseDevice) == -1) + LogPrintf(LogALERT, "Warning: Can't uu_unlock %s\n", fn); +} + static struct termios modemios; int @@ -436,30 +485,33 @@ OpenModem(int mode) struct termios rstio; int oldflag; char *host, *cp, *port; - int res; if (modem >= 0) LogPrintf(LogDEBUG, "OpenModem: Modem is already open!\n"); /* We're going back into "term" mode */ else if (mode & MODE_DIRECT) { - LogPrintf(LogDEBUG, "OpenModem(direct): Modem is %sa tty\n", - isatty(0) ? "" : "not "); + if (isatty(0)) { + LogPrintf(LogDEBUG, "OpenModem(direct): Modem is a tty\n"); + cp = ttyname(0); + SetVariable(0, 1, &cp, VAR_DEVICE); + if (LockModem() == -1) { + close(0); + return -1; + } + } else { + LogPrintf(LogDEBUG, "OpenModem(direct): Modem is not a tty\n"); + SetVariable(0, 0, 0, VAR_DEVICE); + } return modem = 0; } else { if (strncmp(VarDevice, "/dev/", 5) == 0) { - if ((res = uu_lock(VarBaseDevice)) != UU_LOCK_OK) { - if (res == UU_LOCK_INUSE) - LogPrintf(LogPHASE, "Modem %s is in use\n", VarDevice); - else - LogPrintf(LogPHASE, "Modem %s is in use: uu_lock: %s\n", - VarDevice, uu_lockerr(res)); - return (-1); - } + if (LockModem() == -1) + return (-1); modem = open(VarDevice, O_RDWR | O_NONBLOCK); if (modem < 0) { LogPrintf(LogERROR, "OpenModem failed: %s: %s\n", VarDevice, strerror(errno)); - (void) uu_unlock(VarBaseDevice); + UnlockModem(); return (-1); } LogPrintf(LogDEBUG, "OpenModem: Modem is %s\n", VarDevice); @@ -535,8 +587,8 @@ OpenModem(int mode) if (ioctl(modem, TIOCMGET, &mbits)) { LogPrintf(LogERROR, "OpenModem: Cannot get modem status: %s\n", strerror(errno)); - close(modem); - return (modem = -1); + CloseModem(); + return (-1); } LogPrintf(LogDEBUG, "OpenModem: modem control = %o\n", mbits); @@ -544,8 +596,8 @@ OpenModem(int mode) if (oldflag < 0) { LogPrintf(LogERROR, "OpenModem: Cannot get modem flags: %s\n", strerror(errno)); - close(modem); - return (modem = -1); + CloseModem(); + return (-1); } (void) fcntl(modem, F_SETFL, oldflag & ~O_NONBLOCK); } @@ -653,10 +705,8 @@ HangupModem(int flag) DoChat(ScriptBuffer); tcflush(modem, TCIOFLUSH); UnrawModem(modem); - close(modem); + CloseModem(); } - modem = -1; /* Mark as modem has closed */ - (void) uu_unlock(VarBaseDevice); } else if (modem >= 0) { char ScriptBuffer[200]; @@ -678,9 +728,9 @@ CloseModem() { if (modem >= 0) { close(modem); + UnlockModem(); modem = -1; } - (void) uu_unlock(VarBaseDevice); } /*