xref: /freebsd/usr.sbin/ppp/main.c (revision 7cf368eb42edc6d7738f431289a1e3e2203421e6)
1af57ed9fSAtsushi Murai /*
2af57ed9fSAtsushi Murai  *			User Process PPP
3af57ed9fSAtsushi Murai  *
4af57ed9fSAtsushi Murai  *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5af57ed9fSAtsushi Murai  *
6af57ed9fSAtsushi Murai  *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7af57ed9fSAtsushi Murai  *
8af57ed9fSAtsushi Murai  * Redistribution and use in source and binary forms are permitted
9af57ed9fSAtsushi Murai  * provided that the above copyright notice and this paragraph are
10af57ed9fSAtsushi Murai  * duplicated in all such forms and that any documentation,
11af57ed9fSAtsushi Murai  * advertising materials, and other materials related to such
12af57ed9fSAtsushi Murai  * distribution and use acknowledge that the software was developed
13af57ed9fSAtsushi Murai  * by the Internet Initiative Japan, Inc.  The name of the
14af57ed9fSAtsushi Murai  * IIJ may not be used to endorse or promote products derived
15af57ed9fSAtsushi Murai  * from this software without specific prior written permission.
16af57ed9fSAtsushi Murai  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17af57ed9fSAtsushi Murai  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18af57ed9fSAtsushi Murai  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19af57ed9fSAtsushi Murai  *
207cf368ebSBrian Somers  * $Id: main.c,v 1.144 1998/10/22 02:32:49 brian Exp $
21af57ed9fSAtsushi Murai  *
22af57ed9fSAtsushi Murai  *	TODO:
23af57ed9fSAtsushi Murai  */
242764b86aSBrian Somers 
251384bd27SBrian Somers #include <sys/types.h>
2675240ed1SBrian Somers #include <netinet/in.h>
27a9f484e5SJordan K. Hubbard #include <netinet/in_systm.h>
28a9f484e5SJordan K. Hubbard #include <netinet/ip.h>
29565e35e5SBrian Somers #include <sys/un.h>
3075240ed1SBrian Somers 
3175240ed1SBrian Somers #include <errno.h>
3275240ed1SBrian Somers #include <fcntl.h>
3375240ed1SBrian Somers #include <paths.h>
3475240ed1SBrian Somers #include <signal.h>
3575240ed1SBrian Somers #include <stdio.h>
3675240ed1SBrian Somers #include <string.h>
3775240ed1SBrian Somers #include <sys/time.h>
3875240ed1SBrian Somers #include <termios.h>
3975240ed1SBrian Somers #include <unistd.h>
4075240ed1SBrian Somers 
411595bacdSBrian Somers #ifndef NOALIAS
421595bacdSBrian Somers #ifdef __OpenBSD__
431595bacdSBrian Somers #include "alias.h"
441595bacdSBrian Somers #else
451595bacdSBrian Somers #include <alias.h>
461595bacdSBrian Somers #endif
471595bacdSBrian Somers #endif
481af29a6eSBrian Somers #include "probe.h"
4975240ed1SBrian Somers #include "mbuf.h"
5075240ed1SBrian Somers #include "log.h"
5175240ed1SBrian Somers #include "defs.h"
525106c671SBrian Somers #include "id.h"
5375240ed1SBrian Somers #include "timer.h"
5475240ed1SBrian Somers #include "fsm.h"
55879ed6faSBrian Somers #include "lqr.h"
56af57ed9fSAtsushi Murai #include "hdlc.h"
57af57ed9fSAtsushi Murai #include "lcp.h"
580053cc58SBrian Somers #include "ccp.h"
5929e275ceSBrian Somers #include "iplist.h"
6029e275ceSBrian Somers #include "throughput.h"
61eaa4df37SBrian Somers #include "slcompress.h"
62af57ed9fSAtsushi Murai #include "ipcp.h"
6384b8a6ebSAtsushi Murai #include "filter.h"
642f786681SBrian Somers #include "descriptor.h"
653b0f8d2eSBrian Somers #include "link.h"
663b0f8d2eSBrian Somers #include "mp.h"
675828db6dSBrian Somers #include "bundle.h"
681ae349f5Scvs2svn #include "auth.h"
69ed6a16c1SPoul-Henning Kamp #include "systems.h"
70f5ff0f7cSBrian Somers #include "sig.h"
7175240ed1SBrian Somers #include "main.h"
7277ff88adSBrian Somers #include "server.h"
7385b542cfSBrian Somers #include "prompt.h"
74b6dec9f0SBrian Somers #include "chat.h"
75e2ebb036SBrian Somers #include "chap.h"
7692b09558SBrian Somers #include "cbcp.h"
773006ec67SBrian Somers #include "datalink.h"
788fa6ebe4SBrian Somers #include "iface.h"
7953c9f6c0SAtsushi Murai 
8053c9f6c0SAtsushi Murai #ifndef O_NONBLOCK
8153c9f6c0SAtsushi Murai #ifdef O_NDELAY
8253c9f6c0SAtsushi Murai #define	O_NONBLOCK O_NDELAY
8353c9f6c0SAtsushi Murai #endif
8453c9f6c0SAtsushi Murai #endif
85af57ed9fSAtsushi Murai 
860f78c7a7SBrian Somers static void DoLoop(struct bundle *);
8775240ed1SBrian Somers static void TerminalStop(int);
88b6e82f33SBrian Somers static const char *ex_desc(int);
8975240ed1SBrian Somers 
9083d1af55SBrian Somers static struct bundle *SignalBundle;
91b6217683SBrian Somers static struct prompt *SignalPrompt;
92c3899f8dSAtsushi Murai 
93c3899f8dSAtsushi Murai void
94944f7098SBrian Somers Cleanup(int excode)
95af57ed9fSAtsushi Murai {
96a0cbd833SBrian Somers   SignalBundle->CleaningUp = 1;
979c81b87dSBrian Somers   bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
986e4959f0SBrian Somers }
99af57ed9fSAtsushi Murai 
1001afedc4bSBrian Somers void
1011afedc4bSBrian Somers AbortProgram(int excode)
1021afedc4bSBrian Somers {
103dd7e2610SBrian Somers   server_Close(SignalBundle);
104dd7e2610SBrian Somers   log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
1059c81b87dSBrian Somers   bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
10668a0f0ccSBrian Somers   bundle_Destroy(SignalBundle);
107dd7e2610SBrian Somers   log_Close();
108af57ed9fSAtsushi Murai   exit(excode);
109af57ed9fSAtsushi Murai }
110af57ed9fSAtsushi Murai 
111af57ed9fSAtsushi Murai static void
112944f7098SBrian Somers CloseConnection(int signo)
113af57ed9fSAtsushi Murai {
114368aee2bSBrian Somers   /* NOTE, these are manual, we've done a setsid() */
115dd7e2610SBrian Somers   sig_signal(SIGINT, SIG_IGN);
116dd7e2610SBrian Somers   log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
117899011c4SBrian Somers   bundle_Down(SignalBundle, CLOSE_STAYDOWN);
118dd7e2610SBrian Somers   sig_signal(SIGINT, CloseConnection);
1196d14e2a8SJordan K. Hubbard }
120af57ed9fSAtsushi Murai 
121af57ed9fSAtsushi Murai static void
122944f7098SBrian Somers CloseSession(int signo)
123af57ed9fSAtsushi Murai {
124dd7e2610SBrian Somers   log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
125af57ed9fSAtsushi Murai   Cleanup(EX_TERM);
126af57ed9fSAtsushi Murai }
127c3899f8dSAtsushi Murai 
12875b8d283SBrian Somers static pid_t BGPid = 0;
12975b8d283SBrian Somers 
13075b8d283SBrian Somers static void
13175b8d283SBrian Somers KillChild(int signo)
13275b8d283SBrian Somers {
133dd7e2610SBrian Somers   log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
13475b8d283SBrian Somers   kill(BGPid, SIGINT);
13575b8d283SBrian Somers }
13675b8d283SBrian Somers 
137c3899f8dSAtsushi Murai static void
138b6e82f33SBrian Somers TerminalCont(int signo)
139c3899f8dSAtsushi Murai {
140f91ad6b0SBrian Somers   signal(SIGCONT, SIG_DFL);
141f91ad6b0SBrian Somers   prompt_Continue(SignalPrompt);
142c3899f8dSAtsushi Murai }
143c3899f8dSAtsushi Murai 
144c3899f8dSAtsushi Murai static void
145944f7098SBrian Somers TerminalStop(int signo)
146c3899f8dSAtsushi Murai {
147f91ad6b0SBrian Somers   prompt_Suspend(SignalPrompt);
148f91ad6b0SBrian Somers   signal(SIGCONT, TerminalCont);
149f91ad6b0SBrian Somers   raise(SIGSTOP);
1504ef16f24SBrian Somers }
1514ef16f24SBrian Somers 
1528ea8442cSBrian Somers static void
1538ea8442cSBrian Somers BringDownServer(int signo)
1548ea8442cSBrian Somers {
155b6217683SBrian Somers   /* Drops all child prompts too ! */
156dd7e2610SBrian Somers   server_Close(SignalBundle);
1578ea8442cSBrian Somers }
1588ea8442cSBrian Somers 
159b6e82f33SBrian Somers static const char *
1606efd9292SBrian Somers ex_desc(int ex)
1616efd9292SBrian Somers {
162d93d3a9cSBrian Somers   static char num[12];		/* Used immediately if returned */
163b6e82f33SBrian Somers   static const char *desc[] = {
164b6e82f33SBrian Somers     "normal", "start", "sock", "modem", "dial", "dead", "done",
165b6e82f33SBrian Somers     "reboot", "errdead", "hangup", "term", "nodial", "nologin"
166b6e82f33SBrian Somers   };
1676efd9292SBrian Somers 
16870ee81ffSBrian Somers   if (ex >= 0 && ex < sizeof desc / sizeof *desc)
1696efd9292SBrian Somers     return desc[ex];
1706efd9292SBrian Somers   snprintf(num, sizeof num, "%d", ex);
1716efd9292SBrian Somers   return num;
1726efd9292SBrian Somers }
173c3899f8dSAtsushi Murai 
17475240ed1SBrian Somers static void
175b6e82f33SBrian Somers Usage(void)
176af57ed9fSAtsushi Murai {
177680026d6SNate Williams   fprintf(stderr,
178b6e82f33SBrian Somers 	  "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]"
179b6e82f33SBrian Somers #ifndef NOALIAS
180b6e82f33SBrian Somers           " [ -alias ]"
181b6e82f33SBrian Somers #endif
1827cf368ebSBrian Somers           " [system ...]\n");
183af57ed9fSAtsushi Murai   exit(EX_START);
184af57ed9fSAtsushi Murai }
185af57ed9fSAtsushi Murai 
1867cf368ebSBrian Somers static int
187615ad4f9SBrian Somers ProcessArgs(int argc, char **argv, int *mode, int *alias)
188af57ed9fSAtsushi Murai {
1897cf368ebSBrian Somers   int optc, newmode, arg;
190af57ed9fSAtsushi Murai   char *cp;
191af57ed9fSAtsushi Murai 
1927cf368ebSBrian Somers   optc = 0;
19381358fa3SBrian Somers   *mode = PHYS_INTERACTIVE;
194615ad4f9SBrian Somers   *alias = 0;
1957cf368ebSBrian Somers   for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
1967cf368ebSBrian Somers     cp = argv[arg] + 1;
19781358fa3SBrian Somers     newmode = Nam2mode(cp);
19881358fa3SBrian Somers     switch (newmode) {
19981358fa3SBrian Somers       case PHYS_NONE:
20081358fa3SBrian Somers         if (strcmp(cp, "alias") == 0) {
201615ad4f9SBrian Somers #ifdef NOALIAS
2027cf368ebSBrian Somers           log_Printf(LogWARN, "Cannot load alias library (compiled out)\n");
203615ad4f9SBrian Somers #else
204615ad4f9SBrian Somers           *alias = 1;
205615ad4f9SBrian Somers #endif
2061ae349f5Scvs2svn           optc--;			/* this option isn't exclusive */
207944f7098SBrian Somers         } else
208af57ed9fSAtsushi Murai           Usage();
20981358fa3SBrian Somers         break;
21081358fa3SBrian Somers 
21181358fa3SBrian Somers       case PHYS_ALL:
21281358fa3SBrian Somers         Usage();
21381358fa3SBrian Somers         break;
21481358fa3SBrian Somers 
21581358fa3SBrian Somers       default:
21681358fa3SBrian Somers         *mode = newmode;
21781358fa3SBrian Somers     }
218af57ed9fSAtsushi Murai   }
219af57ed9fSAtsushi Murai 
220af57ed9fSAtsushi Murai   if (optc > 1) {
22185602e52SBrian Somers     fprintf(stderr, "You may specify only one mode.\n");
2221ae349f5Scvs2svn     exit(EX_START);
2231ae349f5Scvs2svn   }
2241ae349f5Scvs2svn 
2257cf368ebSBrian Somers   if (*mode == PHYS_AUTO && arg == argc) {
2267cf368ebSBrian Somers     fprintf(stderr, "A system must be specified in auto mode.\n");
227af57ed9fSAtsushi Murai     exit(EX_START);
228af57ed9fSAtsushi Murai   }
22939f94eddSBrian Somers 
2307cf368ebSBrian Somers   return arg;		/* Don't SetLabel yet ! */
231af57ed9fSAtsushi Murai }
232af57ed9fSAtsushi Murai 
2337cf368ebSBrian Somers static void
2347cf368ebSBrian Somers CheckLabel(const char *label, struct prompt *prompt, int mode)
2357cf368ebSBrian Somers {
2367cf368ebSBrian Somers   const char *err;
2377cf368ebSBrian Somers 
2387cf368ebSBrian Somers   if ((err = system_IsValid(label, prompt, mode)) != NULL) {
2397cf368ebSBrian Somers     fprintf(stderr, "You may not use ppp in this mode with this label\n");
2407cf368ebSBrian Somers     fprintf(stderr, "%s: %s\n", label, err);
2417cf368ebSBrian Somers     if (mode == PHYS_DIRECT)
2427cf368ebSBrian Somers       log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
2437cf368ebSBrian Somers                  label, err);
2447cf368ebSBrian Somers     log_Close();
2457cf368ebSBrian Somers     exit(1);
2467cf368ebSBrian Somers   }
2477cf368ebSBrian Somers }
2487cf368ebSBrian Somers 
2497cf368ebSBrian Somers 
2504ef16f24SBrian Somers int
251944f7098SBrian Somers main(int argc, char **argv)
252af57ed9fSAtsushi Murai {
2537cf368ebSBrian Somers   char *name;
2547cf368ebSBrian Somers   int nfds, mode, alias, label, arg;
2557a6f8720SBrian Somers   struct bundle *bundle;
256b6217683SBrian Somers   struct prompt *prompt;
257e3b4c400SBrian Somers 
258e3b4c400SBrian Somers   nfds = getdtablesize();
259e3b4c400SBrian Somers   if (nfds >= FD_SETSIZE)
260e3b4c400SBrian Somers     /*
261e3b4c400SBrian Somers      * If we've got loads of file descriptors, make sure they're all
262e3b4c400SBrian Somers      * closed.  If they aren't, we may end up with a seg fault when our
263e3b4c400SBrian Somers      * `fd_set's get too big when select()ing !
264e3b4c400SBrian Somers      */
265e3b4c400SBrian Somers     while (--nfds > 2)
266e3b4c400SBrian Somers       close(nfds);
267af57ed9fSAtsushi Murai 
26875240ed1SBrian Somers   name = strrchr(argv[0], '/');
269dd7e2610SBrian Somers   log_Open(name ? name + 1 : argv[0]);
2702a279fedSBrian Somers 
2712a630835SBrian Somers #ifndef NOALIAS
2722a630835SBrian Somers   PacketAliasInit();
2732a630835SBrian Somers #endif
2747cf368ebSBrian Somers   label = ProcessArgs(argc, argv, &mode, &alias);
27585b542cfSBrian Somers 
27685b542cfSBrian Somers #ifdef __FreeBSD__
27785b542cfSBrian Somers   /*
27885b542cfSBrian Somers    * A FreeBSD hack to dodge a bug in the tty driver that drops output
27985b542cfSBrian Somers    * occasionally.... I must find the real reason some time.  To display
28085b542cfSBrian Somers    * the dodgy behaviour, comment out this bit, make yourself a large
28185b542cfSBrian Somers    * routing table and then run ppp in interactive mode.  The `show route'
28285b542cfSBrian Somers    * command will drop chunks of data !!!
28385b542cfSBrian Somers    */
28481358fa3SBrian Somers   if (mode == PHYS_INTERACTIVE) {
28585b542cfSBrian Somers     close(STDIN_FILENO);
28685b542cfSBrian Somers     if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
28785b542cfSBrian Somers       fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
28885b542cfSBrian Somers       return 2;
28985b542cfSBrian Somers     }
29085b542cfSBrian Somers   }
29185b542cfSBrian Somers #endif
29285b542cfSBrian Somers 
293b6217683SBrian Somers   /* Allow output for the moment (except in direct mode) */
2946f384573SBrian Somers   if (mode == PHYS_DIRECT)
295b6217683SBrian Somers     prompt = NULL;
29685602e52SBrian Somers   else {
297b6217683SBrian Somers     SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
29881358fa3SBrian Somers     prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(mode));
29985602e52SBrian Somers   }
30012ef29a8SBrian Somers 
3015106c671SBrian Somers   ID0init();
3024562be74SBrian Somers   if (ID0realuid() != 0) {
3034562be74SBrian Somers     char conf[200], *ptr;
3044562be74SBrian Somers 
3054562be74SBrian Somers     snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE);
3064562be74SBrian Somers     do {
3074562be74SBrian Somers       if (!access(conf, W_OK)) {
308a33b2ef7SBrian Somers         log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n",
309a33b2ef7SBrian Somers                    conf);
3104562be74SBrian Somers         return -1;
3114562be74SBrian Somers       }
3124562be74SBrian Somers       ptr = conf + strlen(conf)-2;
3134562be74SBrian Somers       while (ptr > conf && *ptr != '/')
3144562be74SBrian Somers         *ptr-- = '\0';
3154562be74SBrian Somers     } while (ptr >= conf);
3164562be74SBrian Somers   }
3174562be74SBrian Somers 
3187cf368ebSBrian Somers   if (label < argc)
3197cf368ebSBrian Somers     for (arg = label; arg < argc; arg++)
3207cf368ebSBrian Somers       CheckLabel(argv[arg], prompt, mode);
3217cf368ebSBrian Somers   else
3227cf368ebSBrian Somers     CheckLabel("default", prompt, mode);
32312ef29a8SBrian Somers 
3248e7b8599SBrian Somers   if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) {
325dd7e2610SBrian Somers     log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno));
3264ef16f24SBrian Somers     return EX_START;
327af57ed9fSAtsushi Murai   }
3280f2f3eb3SBrian Somers   if (prompt) {
3290f2f3eb3SBrian Somers     prompt->bundle = bundle;	/* couldn't do it earlier */
3308fa6ebe4SBrian Somers     prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
3310f2f3eb3SBrian Somers   }
33283d1af55SBrian Somers   SignalBundle = bundle;
333615ad4f9SBrian Somers   bundle->AliasEnabled = alias;
3348fa6ebe4SBrian Somers   if (alias)
3358fa6ebe4SBrian Somers     bundle->cfg.opt |= OPT_IFACEALIAS;
33612ef29a8SBrian Somers 
33730291ffbSBrian Somers   if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0)
338565e35e5SBrian Somers     prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
3391ae349f5Scvs2svn 
340dd7e2610SBrian Somers   sig_signal(SIGHUP, CloseSession);
341dd7e2610SBrian Somers   sig_signal(SIGTERM, CloseSession);
342dd7e2610SBrian Somers   sig_signal(SIGINT, CloseConnection);
343dd7e2610SBrian Somers   sig_signal(SIGQUIT, CloseSession);
344dd7e2610SBrian Somers   sig_signal(SIGALRM, SIG_IGN);
345e0d3e233SAndrey A. Chernov   signal(SIGPIPE, SIG_IGN);
346565e35e5SBrian Somers 
34781358fa3SBrian Somers   if (mode == PHYS_INTERACTIVE)
348dd7e2610SBrian Somers     sig_signal(SIGTSTP, TerminalStop);
349f91ad6b0SBrian Somers 
350dd7e2610SBrian Somers   sig_signal(SIGUSR2, BringDownServer);
351af57ed9fSAtsushi Murai 
3527cf368ebSBrian Somers   for (arg = label; arg < argc; arg++) {
3537cf368ebSBrian Somers     /* In case we use LABEL or ``set enddisc label'' */
3547cf368ebSBrian Somers     bundle_SetLabel(bundle, argv[argc - 1]);
3557cf368ebSBrian Somers     system_Select(bundle, argv[arg], CONFFILE, prompt, NULL);
3561ae349f5Scvs2svn   }
3577cf368ebSBrian Somers 
3587cf368ebSBrian Somers   if (label < argc)
3597cf368ebSBrian Somers     /* In case the last label did a ``load'' */
3607cf368ebSBrian Somers     bundle_SetLabel(bundle, argv[argc - 1]);
3617cf368ebSBrian Somers 
36281358fa3SBrian Somers   if (mode == PHYS_AUTO &&
3635828db6dSBrian Somers       bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
3647cf368ebSBrian Somers     prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
3657cf368ebSBrian Somers                   "in auto mode.\n");
3668390b576SBrian Somers     AbortProgram(EX_START);
367af57ed9fSAtsushi Murai   }
3686efd9292SBrian Somers 
36981358fa3SBrian Somers   if (mode != PHYS_INTERACTIVE) {
3706f384573SBrian Somers     if (mode != PHYS_DIRECT) {
3715cf4388bSBrian Somers       int bgpipe[2];
3726d14e2a8SJordan K. Hubbard       pid_t bgpid;
373a9c6b5dfSAtsushi Murai 
37481358fa3SBrian Somers       if (mode == PHYS_BACKGROUND && pipe(bgpipe)) {
375dd7e2610SBrian Somers         log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
3768390b576SBrian Somers 	AbortProgram(EX_SOCK);
3771ae349f5Scvs2svn       }
3781ae349f5Scvs2svn 
3796d14e2a8SJordan K. Hubbard       bgpid = fork();
3806d14e2a8SJordan K. Hubbard       if (bgpid == -1) {
381dd7e2610SBrian Somers 	log_Printf(LogERROR, "fork: %s\n", strerror(errno));
3828390b576SBrian Somers 	AbortProgram(EX_SOCK);
3836d14e2a8SJordan K. Hubbard       }
3845cf4388bSBrian Somers 
3856d14e2a8SJordan K. Hubbard       if (bgpid) {
3866d14e2a8SJordan K. Hubbard 	char c = EX_NORMAL;
387a9c6b5dfSAtsushi Murai 
38881358fa3SBrian Somers 	if (mode == PHYS_BACKGROUND) {
3895cf4388bSBrian Somers 	  close(bgpipe[1]);
3906d14e2a8SJordan K. Hubbard 	  BGPid = bgpid;
39175b8d283SBrian Somers           /* If we get a signal, kill the child */
39275b8d283SBrian Somers           signal(SIGHUP, KillChild);
39375b8d283SBrian Somers           signal(SIGTERM, KillChild);
39475b8d283SBrian Somers           signal(SIGINT, KillChild);
39575b8d283SBrian Somers           signal(SIGQUIT, KillChild);
39675b8d283SBrian Somers 
39775b8d283SBrian Somers 	  /* Wait for our child to close its pipe before we exit */
3985cf4388bSBrian Somers 	  if (read(bgpipe[0], &c, 1) != 1) {
399b6217683SBrian Somers 	    prompt_Printf(prompt, "Child exit, no status.\n");
400dd7e2610SBrian Somers 	    log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
4016efd9292SBrian Somers 	  } else if (c == EX_NORMAL) {
402b6217683SBrian Somers 	    prompt_Printf(prompt, "PPP enabled.\n");
403dd7e2610SBrian Somers 	    log_Printf(LogPHASE, "Parent: PPP enabled.\n");
4046efd9292SBrian Somers 	  } else {
405b6217683SBrian Somers 	    prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c));
406dd7e2610SBrian Somers 	    log_Printf(LogPHASE, "Parent: Child failed (%s).\n",
40780e37c72SBrian Somers 		      ex_desc((int) c));
4086efd9292SBrian Somers 	  }
4095cf4388bSBrian Somers 	  close(bgpipe[0]);
4106d14e2a8SJordan K. Hubbard 	}
4114ef16f24SBrian Somers 	return c;
41281358fa3SBrian Somers       } else if (mode == PHYS_BACKGROUND) {
4135cf4388bSBrian Somers 	close(bgpipe[0]);
4145cf4388bSBrian Somers         bundle->notify.fd = bgpipe[1];
415aefd026aSBrian Somers       }
416aefd026aSBrian Somers 
417da66dd13SBrian Somers       bundle_LockTun(bundle);	/* we have a new pid */
418da66dd13SBrian Somers 
419565e35e5SBrian Somers       /* -auto, -dedicated, -ddial & -background */
420b6217683SBrian Somers       prompt_Destroy(prompt, 0);
4212a279fedSBrian Somers       close(STDOUT_FILENO);
4222a279fedSBrian Somers       close(STDERR_FILENO);
4232a279fedSBrian Somers       close(STDIN_FILENO);
424b6217683SBrian Somers       setsid();
425565e35e5SBrian Somers     } else {
426565e35e5SBrian Somers       /* -direct: STDIN_FILENO gets used by modem_Open */
427565e35e5SBrian Somers       prompt_TtyInit(NULL);
428565e35e5SBrian Somers       close(STDOUT_FILENO);
429565e35e5SBrian Somers       close(STDERR_FILENO);
430d656a4c5SBrian Somers     }
431af57ed9fSAtsushi Murai   } else {
432565e35e5SBrian Somers     /* Interactive mode */
4332a279fedSBrian Somers     close(STDERR_FILENO);
434565e35e5SBrian Somers     prompt_TtyInit(prompt);
435b6217683SBrian Somers     prompt_TtyCommandMode(prompt);
436b6217683SBrian Somers     prompt_Required(prompt);
437af57ed9fSAtsushi Murai   }
43835495becSBrian Somers 
439dd7e2610SBrian Somers   log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode));
4400f78c7a7SBrian Somers   DoLoop(bundle);
4411afedc4bSBrian Somers   AbortProgram(EX_NORMAL);
442af57ed9fSAtsushi Murai 
4431afedc4bSBrian Somers   return EX_NORMAL;
444af57ed9fSAtsushi Murai }
445af57ed9fSAtsushi Murai 
446af57ed9fSAtsushi Murai static void
4470f78c7a7SBrian Somers DoLoop(struct bundle *bundle)
448af57ed9fSAtsushi Murai {
449af57ed9fSAtsushi Murai   fd_set rfds, wfds, efds;
4501af29a6eSBrian Somers   int i, nfds, nothing_done;
4511af29a6eSBrian Somers   struct probe probe;
4521af29a6eSBrian Somers 
4531af29a6eSBrian Somers   probe_Init(&probe);
454c3899f8dSAtsushi Murai 
455565e35e5SBrian Somers   do {
456780700e5SAndrey A. Chernov     nfds = 0;
457944f7098SBrian Somers     FD_ZERO(&rfds);
458944f7098SBrian Somers     FD_ZERO(&wfds);
459944f7098SBrian Somers     FD_ZERO(&efds);
46084b8a6ebSAtsushi Murai 
4610f2f3eb3SBrian Somers     /* All our datalinks, the tun device and the MP socket */
4626f384573SBrian Somers     descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds);
4630f2f3eb3SBrian Somers 
4640f2f3eb3SBrian Somers     /* All our prompts and the diagnostic socket */
4650f2f3eb3SBrian Somers     descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds);
46607030d97SBrian Somers 
4673b0f8d2eSBrian Somers     if (bundle_IsDead(bundle))
468f4768038SBrian Somers       /* Don't select - we'll be here forever */
469f4768038SBrian Somers       break;
4700706ff38SBrian Somers 
4713006ec67SBrian Somers     i = select(nfds, &rfds, &wfds, &efds, NULL);
472712ae387SBrian Somers 
47354cd8e13SBrian Somers     if (i < 0 && errno != EINTR) {
474dd7e2610SBrian Somers       log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
47524989c68SBrian Somers       if (log_IsKept(LogTIMER)) {
47624989c68SBrian Somers         struct timeval t;
47724989c68SBrian Somers 
47824989c68SBrian Somers         for (i = 0; i <= nfds; i++) {
47924989c68SBrian Somers           if (FD_ISSET(i, &rfds)) {
48024989c68SBrian Somers             log_Printf(LogTIMER, "Read set contains %d\n", i);
48124989c68SBrian Somers             FD_CLR(i, &rfds);
48224989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
48324989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
48424989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
485af57ed9fSAtsushi Murai               break;
486af57ed9fSAtsushi Murai             }
48724989c68SBrian Somers           }
48824989c68SBrian Somers           if (FD_ISSET(i, &wfds)) {
48924989c68SBrian Somers             log_Printf(LogTIMER, "Write set contains %d\n", i);
49024989c68SBrian Somers             FD_CLR(i, &wfds);
49124989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
49224989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
49324989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
494af57ed9fSAtsushi Murai               break;
495af57ed9fSAtsushi Murai             }
496e0d3e233SAndrey A. Chernov           }
49724989c68SBrian Somers           if (FD_ISSET(i, &efds)) {
49824989c68SBrian Somers             log_Printf(LogTIMER, "Error set contains %d\n", i);
49924989c68SBrian Somers             FD_CLR(i, &efds);
50024989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
50124989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
50224989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
50358f264e1SBrian Somers               break;
50458f264e1SBrian Somers             }
50524989c68SBrian Somers           }
50624989c68SBrian Somers         }
50724989c68SBrian Somers       }
50858f264e1SBrian Somers       break;
509de451c68SBrian Somers     }
510de451c68SBrian Somers 
51154cd8e13SBrian Somers     sig_Handle();
51254cd8e13SBrian Somers 
51354cd8e13SBrian Somers     if (i <= 0)
51454cd8e13SBrian Somers       continue;
51554cd8e13SBrian Somers 
5163006ec67SBrian Somers     for (i = 0; i <= nfds; i++)
5173006ec67SBrian Somers       if (FD_ISSET(i, &efds)) {
518a33b2ef7SBrian Somers         log_Printf(LogERROR, "Exception detected on descriptor %d\n", i);
5191ae349f5Scvs2svn         break;
5201ae349f5Scvs2svn       }
521c60f92caSBrian Somers 
522b7c5748eSBrian Somers     if (i <= nfds)
523b7c5748eSBrian Somers       break;
524b7c5748eSBrian Somers 
5251af29a6eSBrian Somers     nothing_done = 1;
5261af29a6eSBrian Somers 
5271af29a6eSBrian Somers     if (descriptor_IsSet(&server.desc, &rfds)) {
528b77776a7SBrian Somers       descriptor_Read(&server.desc, bundle, &rfds);
5291af29a6eSBrian Somers       nothing_done = 0;
5301af29a6eSBrian Somers     }
5311af29a6eSBrian Somers 
5321af29a6eSBrian Somers     if (descriptor_IsSet(&bundle->desc, &rfds)) {
5331af29a6eSBrian Somers       descriptor_Read(&bundle->desc, bundle, &rfds);
5341af29a6eSBrian Somers       nothing_done = 0;
5351af29a6eSBrian Somers     }
53642d4d396SBrian Somers 
5372f786681SBrian Somers     if (descriptor_IsSet(&bundle->desc, &wfds))
5381af29a6eSBrian Somers       if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) {
5391af29a6eSBrian Somers         /*
5401af29a6eSBrian Somers          * This is disasterous.  The OS has told us that something is
5411af29a6eSBrian Somers          * writable, and all our write()s have failed.  Rather than
5421af29a6eSBrian Somers          * going back immediately to do our UpdateSet()s and select(),
5431af29a6eSBrian Somers          * we sleep for a bit to avoid gobbling up all cpu time.
5441af29a6eSBrian Somers          */
5451af29a6eSBrian Somers         struct timeval t;
5462f786681SBrian Somers 
5471af29a6eSBrian Somers         t.tv_sec = 0;
5481af29a6eSBrian Somers         t.tv_usec = 100000;
5491af29a6eSBrian Somers         select(0, NULL, NULL, NULL, &t);
5501af29a6eSBrian Somers       }
55154cd8e13SBrian Somers 
552565e35e5SBrian Somers   } while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle));
553565e35e5SBrian Somers 
554dd7e2610SBrian Somers   log_Printf(LogDEBUG, "DoLoop done.\n");
555af57ed9fSAtsushi Murai }
556