diff options
author | jkh <jkh@FreeBSD.org> | 1996-12-22 17:29:33 +0000 |
---|---|---|
committer | jkh <jkh@FreeBSD.org> | 1996-12-22 17:29:33 +0000 |
commit | 52623b565c8fc4ba063611c16810780149f5a572 (patch) | |
tree | 72cd3867b0a34f1c1c5b4b27757dd905d1a8e4e0 /usr.sbin/ppp/main.c | |
parent | 6ca14a89c608ae04e77c840dbd24a9587eb1e5c7 (diff) | |
download | FreeBSD-src-52623b565c8fc4ba063611c16810780149f5a572.zip FreeBSD-src-52623b565c8fc4ba063611c16810780149f5a572.tar.gz |
For /usr/sbin/ppp, you must choose between running ppp in the background or
connecting to a host immediately in the foreground.
I would like to be able to run ppp from a script so that my script can be
sure that it is connected to the 'net before it continues running:
# Dial up the internet.
ppp -background myprovider || exit 1
do-some-net-command
# Hang up the modem.
kill -HUP `cat /var/run/ppp.tun0.pid`
Another problem is that the current ppp calls its process id file
`/var/run/PPP.server', which may conflict if you have more than one IP
tunnel interface available.
Closes PR#1469
Submitted by: Gord Matzigkeit <gord@enci.ucalgary.ca>
Diffstat (limited to 'usr.sbin/ppp/main.c')
-rw-r--r-- | usr.sbin/ppp/main.c | 112 |
1 files changed, 80 insertions, 32 deletions
diff --git a/usr.sbin/ppp/main.c b/usr.sbin/ppp/main.c index e692a40..75d78a5 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.24 1996/12/12 14:39:45 jkh Exp $ + * $Id: main.c,v 1.25 1996/12/19 00:41:42 nate Exp $ * * TODO: * o Add commands for traffic summary, version display, etc. @@ -74,6 +74,7 @@ static struct termios oldtio; /* Original tty mode */ static struct termios comtio; /* Command level tty mode */ int TermMode; static int server; +static pid_t BGPid = 0; struct sockaddr_in ifsin; char pid_filename[128]; @@ -159,6 +160,8 @@ int excode; sleep(1); if (mode & MODE_AUTO) { DeleteIfRoutes(1); + } + if (mode & (MODE_AUTO | MODE_BACKGROUND)) { unlink(pid_filename); } OsInterfaceDown(1); @@ -180,20 +183,31 @@ int signo; LogClose(); abort(); } - LogPrintf(LOG_PHASE_BIT, "Signal %d, hangup.\n", signo); - Cleanup(EX_HANGUP); + if (BGPid) { + kill (BGPid, SIGHUP); + exit (EX_HANGUP); + } + else { + LogPrintf(LOG_PHASE_BIT, "Signal %d, hangup.\n", signo); + Cleanup(EX_HANGUP); + } } static void CloseSession(signo) int signo; { - LogPrintf(LOG_PHASE_BIT, "Signal %d, terminate.\n", signo); - LcpClose(); - Cleanup(EX_TERM); + if (BGPid) { + kill (BGPid, SIGINT); + exit (EX_TERM); + } + else { + LogPrintf(LOG_PHASE_BIT, "Signal %d, terminate.\n", signo); + LcpClose(); + Cleanup(EX_TERM); + } } - static void TerminalCont() { @@ -217,7 +231,7 @@ void Usage() { fprintf(stderr, - "Usage: ppp [-auto | -direct | -dedicated | -ddial ] [ -alias ] [system]\n"); + "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ] [ -alias ] [system]\n"); exit(EX_START); } @@ -232,6 +246,8 @@ ProcessArgs(int argc, char **argv) cp = *argv + 1; if (strcmp(cp, "auto") == 0) mode |= MODE_AUTO; + else if (strcmp(cp, "background") == 0) + mode |= MODE_BACKGROUND; else if (strcmp(cp, "direct") == 0) mode |= MODE_DIRECT; else if (strcmp(cp, "dedicated") == 0) @@ -308,7 +324,7 @@ char **argv; exit(EX_START); } - if (mode & (MODE_AUTO|MODE_DIRECT|MODE_DEDICATED)) + if (mode & (MODE_AUTO|MODE_DIRECT|MODE_DEDICATED|MODE_BACKGROUND)) mode &= ~MODE_INTER; if (mode & MODE_INTER) { printf("Interactive mode\n"); @@ -320,6 +336,12 @@ char **argv; "Destination system must be specified in auto or ddial mode.\n"); exit(EX_START); } + } else if (mode & MODE_BACKGROUND) { + printf("Background mode\n"); + if (dstsystem == NULL) { + fprintf(stderr, "Destination system must be specified in background mode.\n"); + exit(EX_START); + } } tcgetattr(0, &oldtio); /* Save original tty mode */ @@ -370,36 +392,61 @@ char **argv; #endif if (!(mode & MODE_INTER)) { - int port = SERVER_PORT + tunno; - /* - * Create server socket and listen at there. - */ - server = socket(PF_INET, SOCK_STREAM, 0); - if (server < 0) { - perror("socket"); - Cleanup(EX_SOCK); + int port = SERVER_PORT + tunno; + if (mode & MODE_BACKGROUND) { + if (pipe (BGFiledes)) { + perror("pipe"); + Cleanup(EX_SOCK); + } + server = -1; } - ifsin.sin_family = AF_INET; - ifsin.sin_addr.s_addr = INADDR_ANY; - ifsin.sin_port = htons(port); - if (bind(server, (struct sockaddr *) &ifsin, sizeof(ifsin)) < 0) { - perror("bind"); - if (errno == EADDRINUSE) - fprintf(stderr, "Wait for a while, then try again.\n"); - Cleanup(EX_SOCK); + else { + /* + * Create server socket and listen at there. + */ + server = socket(PF_INET, SOCK_STREAM, 0); + if (server < 0) { + perror("socket"); + Cleanup(EX_SOCK); + } + ifsin.sin_family = AF_INET; + ifsin.sin_addr.s_addr = INADDR_ANY; + ifsin.sin_port = htons(port); + if (bind(server, (struct sockaddr *) &ifsin, sizeof(ifsin)) < 0) { + perror("bind"); + if (errno == EADDRINUSE) + fprintf(stderr, "Wait for a while, then try again.\n"); + Cleanup(EX_SOCK); + } + listen(server, 5); } - listen(server, 5); DupLog(); if (!(mode & MODE_DIRECT)) { int fd; char pid[32]; + pid_t bgpid; - if (fork()) - exit(0); + bgpid = fork (); + if (bgpid == -1) { + perror ("fork"); + Cleanup (EX_SOCK); + } + if (bgpid) { + char c = EX_NORMAL; + + if (mode & MODE_BACKGROUND) { + /* Wait for our child to close its pipe before we exit. */ + BGPid = bgpid; + read (BGFiledes[0], &c, 1); + if (c == EX_NORMAL) + LogPrintf (LOG_CHAT, "PPP enabled.\n"); + } + exit(c); + } - snprintf(pid_filename, sizeof (pid_filename), "%s/PPP.%s", - _PATH_VARRUN, dstsystem); + snprintf(pid_filename, sizeof (pid_filename), "%s/ppp.tun%d.pid", + _PATH_VARRUN, tunno); unlink(pid_filename); sprintf(pid, "%d\n", (int)getpid()); @@ -409,7 +456,8 @@ char **argv; close(fd); } } - LogPrintf(LOG_PHASE_BIT, "Listening at %d.\n", port); + if (server > 0) + LogPrintf(LOG_PHASE_BIT, "Listening at %d.\n", port); #ifdef DOTTYINIT if (mode & (MODE_DIRECT|MODE_DEDICATED)) { /* } */ #else @@ -444,7 +492,7 @@ char **argv; } /* - * Turn into packet mode, where we speek PPP. + * Turn into packet mode, where we speak PPP. */ void PacketMode() |