xref: /freebsd/usr.sbin/ppp/main.c (revision ac37ab22eb8a99f65b80c39d7ba8bc9ec4894aeb)
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  *
20ac37ab22SBrian Somers  * $Id: main.c,v 1.154 1999/05/08 11:07:05 brian Exp $
21af57ed9fSAtsushi Murai  *
22af57ed9fSAtsushi Murai  *	TODO:
23af57ed9fSAtsushi Murai  */
242764b86aSBrian Somers 
25972a1bcfSBrian Somers #include <sys/param.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
427884358fSBrian Somers #ifdef __FreeBSD__
431595bacdSBrian Somers #include <alias.h>
447884358fSBrian Somers #else
457884358fSBrian Somers #include "alias.h"
461595bacdSBrian Somers #endif
471595bacdSBrian Somers #endif
485d9e6103SBrian Somers #include "layer.h"
491af29a6eSBrian Somers #include "probe.h"
5075240ed1SBrian Somers #include "mbuf.h"
5175240ed1SBrian Somers #include "log.h"
5275240ed1SBrian Somers #include "defs.h"
535106c671SBrian Somers #include "id.h"
5475240ed1SBrian Somers #include "timer.h"
5575240ed1SBrian Somers #include "fsm.h"
56879ed6faSBrian Somers #include "lqr.h"
57af57ed9fSAtsushi Murai #include "hdlc.h"
58af57ed9fSAtsushi Murai #include "lcp.h"
590053cc58SBrian Somers #include "ccp.h"
6029e275ceSBrian Somers #include "iplist.h"
6129e275ceSBrian Somers #include "throughput.h"
62eaa4df37SBrian Somers #include "slcompress.h"
63af57ed9fSAtsushi Murai #include "ipcp.h"
6484b8a6ebSAtsushi Murai #include "filter.h"
652f786681SBrian Somers #include "descriptor.h"
663b0f8d2eSBrian Somers #include "link.h"
673b0f8d2eSBrian Somers #include "mp.h"
68972a1bcfSBrian Somers #ifndef NORADIUS
69972a1bcfSBrian Somers #include "radius.h"
70972a1bcfSBrian Somers #endif
715828db6dSBrian Somers #include "bundle.h"
721ae349f5Scvs2svn #include "auth.h"
73ed6a16c1SPoul-Henning Kamp #include "systems.h"
74f5ff0f7cSBrian Somers #include "sig.h"
7575240ed1SBrian Somers #include "main.h"
7677ff88adSBrian Somers #include "server.h"
7785b542cfSBrian Somers #include "prompt.h"
78b6dec9f0SBrian Somers #include "chat.h"
79e2ebb036SBrian Somers #include "chap.h"
8092b09558SBrian Somers #include "cbcp.h"
813006ec67SBrian Somers #include "datalink.h"
828fa6ebe4SBrian Somers #include "iface.h"
8353c9f6c0SAtsushi Murai 
8453c9f6c0SAtsushi Murai #ifndef O_NONBLOCK
8553c9f6c0SAtsushi Murai #ifdef O_NDELAY
8653c9f6c0SAtsushi Murai #define	O_NONBLOCK O_NDELAY
8753c9f6c0SAtsushi Murai #endif
8853c9f6c0SAtsushi Murai #endif
89af57ed9fSAtsushi Murai 
900f78c7a7SBrian Somers static void DoLoop(struct bundle *);
9175240ed1SBrian Somers static void TerminalStop(int);
92b6e82f33SBrian Somers static const char *ex_desc(int);
9375240ed1SBrian Somers 
9483d1af55SBrian Somers static struct bundle *SignalBundle;
95b6217683SBrian Somers static struct prompt *SignalPrompt;
96c3899f8dSAtsushi Murai 
97c3899f8dSAtsushi Murai void
98944f7098SBrian Somers Cleanup(int excode)
99af57ed9fSAtsushi Murai {
100a0cbd833SBrian Somers   SignalBundle->CleaningUp = 1;
1019c81b87dSBrian Somers   bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
1026e4959f0SBrian Somers }
103af57ed9fSAtsushi Murai 
1041afedc4bSBrian Somers void
1051afedc4bSBrian Somers AbortProgram(int excode)
1061afedc4bSBrian Somers {
107dd7e2610SBrian Somers   server_Close(SignalBundle);
108dd7e2610SBrian Somers   log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
1099c81b87dSBrian Somers   bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
11068a0f0ccSBrian Somers   bundle_Destroy(SignalBundle);
111dd7e2610SBrian Somers   log_Close();
112af57ed9fSAtsushi Murai   exit(excode);
113af57ed9fSAtsushi Murai }
114af57ed9fSAtsushi Murai 
115af57ed9fSAtsushi Murai static void
116944f7098SBrian Somers CloseConnection(int signo)
117af57ed9fSAtsushi Murai {
118368aee2bSBrian Somers   /* NOTE, these are manual, we've done a setsid() */
119dd7e2610SBrian Somers   sig_signal(SIGINT, SIG_IGN);
120dd7e2610SBrian Somers   log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
121899011c4SBrian Somers   bundle_Down(SignalBundle, CLOSE_STAYDOWN);
122dd7e2610SBrian Somers   sig_signal(SIGINT, CloseConnection);
1236d14e2a8SJordan K. Hubbard }
124af57ed9fSAtsushi Murai 
125af57ed9fSAtsushi Murai static void
126944f7098SBrian Somers CloseSession(int signo)
127af57ed9fSAtsushi Murai {
128dd7e2610SBrian Somers   log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
129af57ed9fSAtsushi Murai   Cleanup(EX_TERM);
130af57ed9fSAtsushi Murai }
131c3899f8dSAtsushi Murai 
13275b8d283SBrian Somers static pid_t BGPid = 0;
13375b8d283SBrian Somers 
13475b8d283SBrian Somers static void
13575b8d283SBrian Somers KillChild(int signo)
13675b8d283SBrian Somers {
137ac37ab22SBrian Somers   signal(signo, SIG_IGN);
138dd7e2610SBrian Somers   log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
13975b8d283SBrian Somers   kill(BGPid, SIGINT);
14075b8d283SBrian Somers }
14175b8d283SBrian Somers 
142c3899f8dSAtsushi Murai static void
143b6e82f33SBrian Somers TerminalCont(int signo)
144c3899f8dSAtsushi Murai {
145f91ad6b0SBrian Somers   signal(SIGCONT, SIG_DFL);
146f91ad6b0SBrian Somers   prompt_Continue(SignalPrompt);
147c3899f8dSAtsushi Murai }
148c3899f8dSAtsushi Murai 
149c3899f8dSAtsushi Murai static void
150944f7098SBrian Somers TerminalStop(int signo)
151c3899f8dSAtsushi Murai {
152f91ad6b0SBrian Somers   prompt_Suspend(SignalPrompt);
153f91ad6b0SBrian Somers   signal(SIGCONT, TerminalCont);
154f91ad6b0SBrian Somers   raise(SIGSTOP);
1554ef16f24SBrian Somers }
1564ef16f24SBrian Somers 
1578ea8442cSBrian Somers static void
1588ea8442cSBrian Somers BringDownServer(int signo)
1598ea8442cSBrian Somers {
160b6217683SBrian Somers   /* Drops all child prompts too ! */
161dd7e2610SBrian Somers   server_Close(SignalBundle);
1628ea8442cSBrian Somers }
1638ea8442cSBrian Somers 
164b6e82f33SBrian Somers static const char *
1656efd9292SBrian Somers ex_desc(int ex)
1666efd9292SBrian Somers {
167d93d3a9cSBrian Somers   static char num[12];		/* Used immediately if returned */
168b6e82f33SBrian Somers   static const char *desc[] = {
169b6e82f33SBrian Somers     "normal", "start", "sock", "modem", "dial", "dead", "done",
170b6e82f33SBrian Somers     "reboot", "errdead", "hangup", "term", "nodial", "nologin"
171b6e82f33SBrian Somers   };
1726efd9292SBrian Somers 
17370ee81ffSBrian Somers   if (ex >= 0 && ex < sizeof desc / sizeof *desc)
1746efd9292SBrian Somers     return desc[ex];
1756efd9292SBrian Somers   snprintf(num, sizeof num, "%d", ex);
1766efd9292SBrian Somers   return num;
1776efd9292SBrian Somers }
178c3899f8dSAtsushi Murai 
17975240ed1SBrian Somers static void
180b6e82f33SBrian Somers Usage(void)
181af57ed9fSAtsushi Murai {
182680026d6SNate Williams   fprintf(stderr,
183b6e82f33SBrian Somers 	  "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]"
184b6e82f33SBrian Somers #ifndef NOALIAS
185b6e82f33SBrian Somers           " [ -alias ]"
186b6e82f33SBrian Somers #endif
1877cf368ebSBrian Somers           " [system ...]\n");
188af57ed9fSAtsushi Murai   exit(EX_START);
189af57ed9fSAtsushi Murai }
190af57ed9fSAtsushi Murai 
1917cf368ebSBrian Somers static int
192615ad4f9SBrian Somers ProcessArgs(int argc, char **argv, int *mode, int *alias)
193af57ed9fSAtsushi Murai {
1947cf368ebSBrian Somers   int optc, newmode, arg;
195af57ed9fSAtsushi Murai   char *cp;
196af57ed9fSAtsushi Murai 
1977cf368ebSBrian Somers   optc = 0;
19881358fa3SBrian Somers   *mode = PHYS_INTERACTIVE;
199615ad4f9SBrian Somers   *alias = 0;
2007cf368ebSBrian Somers   for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
2017cf368ebSBrian Somers     cp = argv[arg] + 1;
20281358fa3SBrian Somers     newmode = Nam2mode(cp);
20381358fa3SBrian Somers     switch (newmode) {
20481358fa3SBrian Somers       case PHYS_NONE:
20581358fa3SBrian Somers         if (strcmp(cp, "alias") == 0) {
206615ad4f9SBrian Somers #ifdef NOALIAS
2077cf368ebSBrian Somers           log_Printf(LogWARN, "Cannot load alias library (compiled out)\n");
208615ad4f9SBrian Somers #else
209615ad4f9SBrian Somers           *alias = 1;
210615ad4f9SBrian Somers #endif
2111ae349f5Scvs2svn           optc--;			/* this option isn't exclusive */
212944f7098SBrian Somers         } else
213af57ed9fSAtsushi Murai           Usage();
21481358fa3SBrian Somers         break;
21581358fa3SBrian Somers 
21681358fa3SBrian Somers       case PHYS_ALL:
21781358fa3SBrian Somers         Usage();
21881358fa3SBrian Somers         break;
21981358fa3SBrian Somers 
22081358fa3SBrian Somers       default:
22181358fa3SBrian Somers         *mode = newmode;
22281358fa3SBrian Somers     }
223af57ed9fSAtsushi Murai   }
224af57ed9fSAtsushi Murai 
225af57ed9fSAtsushi Murai   if (optc > 1) {
22685602e52SBrian Somers     fprintf(stderr, "You may specify only one mode.\n");
2271ae349f5Scvs2svn     exit(EX_START);
2281ae349f5Scvs2svn   }
2291ae349f5Scvs2svn 
2307cf368ebSBrian Somers   if (*mode == PHYS_AUTO && arg == argc) {
2317cf368ebSBrian Somers     fprintf(stderr, "A system must be specified in auto mode.\n");
232af57ed9fSAtsushi Murai     exit(EX_START);
233af57ed9fSAtsushi Murai   }
23439f94eddSBrian Somers 
2357cf368ebSBrian Somers   return arg;		/* Don't SetLabel yet ! */
236af57ed9fSAtsushi Murai }
237af57ed9fSAtsushi Murai 
2387cf368ebSBrian Somers static void
2397cf368ebSBrian Somers CheckLabel(const char *label, struct prompt *prompt, int mode)
2407cf368ebSBrian Somers {
2417cf368ebSBrian Somers   const char *err;
2427cf368ebSBrian Somers 
2437cf368ebSBrian Somers   if ((err = system_IsValid(label, prompt, mode)) != NULL) {
2447cf368ebSBrian Somers     fprintf(stderr, "%s: %s\n", label, err);
2457cf368ebSBrian Somers     if (mode == PHYS_DIRECT)
2467cf368ebSBrian Somers       log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
2477cf368ebSBrian Somers                  label, err);
2487cf368ebSBrian Somers     log_Close();
2497cf368ebSBrian Somers     exit(1);
2507cf368ebSBrian Somers   }
2517cf368ebSBrian Somers }
2527cf368ebSBrian Somers 
2537cf368ebSBrian Somers 
2544ef16f24SBrian Somers int
255944f7098SBrian Somers main(int argc, char **argv)
256af57ed9fSAtsushi Murai {
2577cf368ebSBrian Somers   char *name;
258756783fcSBrian Somers   const char *lastlabel;
2597cf368ebSBrian Somers   int nfds, mode, alias, label, arg;
2607a6f8720SBrian Somers   struct bundle *bundle;
261b6217683SBrian Somers   struct prompt *prompt;
262e3b4c400SBrian Somers 
263e3b4c400SBrian Somers   nfds = getdtablesize();
264e3b4c400SBrian Somers   if (nfds >= FD_SETSIZE)
265e3b4c400SBrian Somers     /*
266e3b4c400SBrian Somers      * If we've got loads of file descriptors, make sure they're all
267e3b4c400SBrian Somers      * closed.  If they aren't, we may end up with a seg fault when our
268e3b4c400SBrian Somers      * `fd_set's get too big when select()ing !
269e3b4c400SBrian Somers      */
270e3b4c400SBrian Somers     while (--nfds > 2)
271e3b4c400SBrian Somers       close(nfds);
272af57ed9fSAtsushi Murai 
27375240ed1SBrian Somers   name = strrchr(argv[0], '/');
274dd7e2610SBrian Somers   log_Open(name ? name + 1 : argv[0]);
2752a279fedSBrian Somers 
2762a630835SBrian Somers #ifndef NOALIAS
2772a630835SBrian Somers   PacketAliasInit();
2782a630835SBrian Somers #endif
2797cf368ebSBrian Somers   label = ProcessArgs(argc, argv, &mode, &alias);
28085b542cfSBrian Somers 
28185b542cfSBrian Somers   /*
282a611383fSBrian Somers    * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops
283a611383fSBrian Somers    * output occasionally.... I must find the real reason some time.  To
284a611383fSBrian Somers    * display the dodgy behaviour, comment out this bit, make yourself a large
28585b542cfSBrian Somers    * routing table and then run ppp in interactive mode.  The `show route'
28685b542cfSBrian Somers    * command will drop chunks of data !!!
28785b542cfSBrian Somers    */
28881358fa3SBrian Somers   if (mode == PHYS_INTERACTIVE) {
28985b542cfSBrian Somers     close(STDIN_FILENO);
29085b542cfSBrian Somers     if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
29185b542cfSBrian Somers       fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
29285b542cfSBrian Somers       return 2;
29385b542cfSBrian Somers     }
29485b542cfSBrian Somers   }
29585b542cfSBrian Somers 
296b6217683SBrian Somers   /* Allow output for the moment (except in direct mode) */
2976f384573SBrian Somers   if (mode == PHYS_DIRECT)
298b6217683SBrian Somers     prompt = NULL;
299ed0e9269SBrian Somers   else
300b6217683SBrian Somers     SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
30112ef29a8SBrian Somers 
3025106c671SBrian Somers   ID0init();
3034562be74SBrian Somers   if (ID0realuid() != 0) {
3044562be74SBrian Somers     char conf[200], *ptr;
3054562be74SBrian Somers 
3064562be74SBrian Somers     snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE);
3074562be74SBrian Somers     do {
3084562be74SBrian Somers       if (!access(conf, W_OK)) {
309a33b2ef7SBrian Somers         log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n",
310a33b2ef7SBrian Somers                    conf);
3114562be74SBrian Somers         return -1;
3124562be74SBrian Somers       }
3134562be74SBrian Somers       ptr = conf + strlen(conf)-2;
3144562be74SBrian Somers       while (ptr > conf && *ptr != '/')
3154562be74SBrian Somers         *ptr-- = '\0';
3164562be74SBrian Somers     } while (ptr >= conf);
3174562be74SBrian Somers   }
3184562be74SBrian Somers 
3197cf368ebSBrian Somers   if (label < argc)
3207cf368ebSBrian Somers     for (arg = label; arg < argc; arg++)
3217cf368ebSBrian Somers       CheckLabel(argv[arg], prompt, mode);
3227cf368ebSBrian Somers   else
3237cf368ebSBrian Somers     CheckLabel("default", prompt, mode);
32412ef29a8SBrian Somers 
325ed0e9269SBrian Somers   prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(mode));
326ed0e9269SBrian Somers 
3278e7b8599SBrian Somers   if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) {
328dd7e2610SBrian Somers     log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno));
3294ef16f24SBrian Somers     return EX_START;
330af57ed9fSAtsushi Murai   }
331756783fcSBrian Somers 
332756783fcSBrian Somers   /* NOTE:  We may now have changed argv[1] via a ``set proctitle'' */
333756783fcSBrian Somers 
3340f2f3eb3SBrian Somers   if (prompt) {
3350f2f3eb3SBrian Somers     prompt->bundle = bundle;	/* couldn't do it earlier */
3368fa6ebe4SBrian Somers     prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
3370f2f3eb3SBrian Somers   }
33883d1af55SBrian Somers   SignalBundle = bundle;
339615ad4f9SBrian Somers   bundle->AliasEnabled = alias;
3408fa6ebe4SBrian Somers   if (alias)
3418fa6ebe4SBrian Somers     bundle->cfg.opt |= OPT_IFACEALIAS;
34212ef29a8SBrian Somers 
34330291ffbSBrian Somers   if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0)
344565e35e5SBrian Somers     prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
3451ae349f5Scvs2svn 
346dd7e2610SBrian Somers   sig_signal(SIGHUP, CloseSession);
347dd7e2610SBrian Somers   sig_signal(SIGTERM, CloseSession);
348dd7e2610SBrian Somers   sig_signal(SIGINT, CloseConnection);
349dd7e2610SBrian Somers   sig_signal(SIGQUIT, CloseSession);
350dd7e2610SBrian Somers   sig_signal(SIGALRM, SIG_IGN);
351e0d3e233SAndrey A. Chernov   signal(SIGPIPE, SIG_IGN);
352565e35e5SBrian Somers 
35381358fa3SBrian Somers   if (mode == PHYS_INTERACTIVE)
354dd7e2610SBrian Somers     sig_signal(SIGTSTP, TerminalStop);
355f91ad6b0SBrian Somers 
356dd7e2610SBrian Somers   sig_signal(SIGUSR2, BringDownServer);
357af57ed9fSAtsushi Murai 
358756783fcSBrian Somers   lastlabel = argc == 2 ? bundle->argv1 : argv[argc - 1];
3597cf368ebSBrian Somers   for (arg = label; arg < argc; arg++) {
3607cf368ebSBrian Somers     /* In case we use LABEL or ``set enddisc label'' */
361756783fcSBrian Somers     bundle_SetLabel(bundle, lastlabel);
362756783fcSBrian Somers     system_Select(bundle, arg == 1 ? bundle->argv1 : argv[arg],
363756783fcSBrian Somers                   CONFFILE, prompt, NULL);
3641ae349f5Scvs2svn   }
3657cf368ebSBrian Somers 
3667cf368ebSBrian Somers   if (label < argc)
3677cf368ebSBrian Somers     /* In case the last label did a ``load'' */
368756783fcSBrian Somers     bundle_SetLabel(bundle, lastlabel);
3697cf368ebSBrian Somers 
37081358fa3SBrian Somers   if (mode == PHYS_AUTO &&
3715828db6dSBrian Somers       bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
3727cf368ebSBrian Somers     prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
3737cf368ebSBrian Somers                   "in auto mode.\n");
3748390b576SBrian Somers     AbortProgram(EX_START);
375af57ed9fSAtsushi Murai   }
3766efd9292SBrian Somers 
37781358fa3SBrian Somers   if (mode != PHYS_INTERACTIVE) {
3786f384573SBrian Somers     if (mode != PHYS_DIRECT) {
3795cf4388bSBrian Somers       int bgpipe[2];
3806d14e2a8SJordan K. Hubbard       pid_t bgpid;
381a9c6b5dfSAtsushi Murai 
38281358fa3SBrian Somers       if (mode == PHYS_BACKGROUND && pipe(bgpipe)) {
383dd7e2610SBrian Somers         log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
3848390b576SBrian Somers 	AbortProgram(EX_SOCK);
3851ae349f5Scvs2svn       }
3861ae349f5Scvs2svn 
3876d14e2a8SJordan K. Hubbard       bgpid = fork();
3886d14e2a8SJordan K. Hubbard       if (bgpid == -1) {
389dd7e2610SBrian Somers 	log_Printf(LogERROR, "fork: %s\n", strerror(errno));
3908390b576SBrian Somers 	AbortProgram(EX_SOCK);
3916d14e2a8SJordan K. Hubbard       }
3925cf4388bSBrian Somers 
3936d14e2a8SJordan K. Hubbard       if (bgpid) {
3946d14e2a8SJordan K. Hubbard 	char c = EX_NORMAL;
395a9c6b5dfSAtsushi Murai 
39681358fa3SBrian Somers 	if (mode == PHYS_BACKGROUND) {
3975cf4388bSBrian Somers 	  close(bgpipe[1]);
3986d14e2a8SJordan K. Hubbard 	  BGPid = bgpid;
39975b8d283SBrian Somers           /* If we get a signal, kill the child */
40075b8d283SBrian Somers           signal(SIGHUP, KillChild);
40175b8d283SBrian Somers           signal(SIGTERM, KillChild);
40275b8d283SBrian Somers           signal(SIGINT, KillChild);
40375b8d283SBrian Somers           signal(SIGQUIT, KillChild);
40475b8d283SBrian Somers 
40575b8d283SBrian Somers 	  /* Wait for our child to close its pipe before we exit */
4065cf4388bSBrian Somers 	  if (read(bgpipe[0], &c, 1) != 1) {
407b6217683SBrian Somers 	    prompt_Printf(prompt, "Child exit, no status.\n");
408dd7e2610SBrian Somers 	    log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
4096efd9292SBrian Somers 	  } else if (c == EX_NORMAL) {
410b6217683SBrian Somers 	    prompt_Printf(prompt, "PPP enabled.\n");
411dd7e2610SBrian Somers 	    log_Printf(LogPHASE, "Parent: PPP enabled.\n");
4126efd9292SBrian Somers 	  } else {
413b6217683SBrian Somers 	    prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c));
414dd7e2610SBrian Somers 	    log_Printf(LogPHASE, "Parent: Child failed (%s).\n",
41580e37c72SBrian Somers 		      ex_desc((int) c));
4166efd9292SBrian Somers 	  }
4175cf4388bSBrian Somers 	  close(bgpipe[0]);
4186d14e2a8SJordan K. Hubbard 	}
4194ef16f24SBrian Somers 	return c;
42081358fa3SBrian Somers       } else if (mode == PHYS_BACKGROUND) {
4215cf4388bSBrian Somers 	close(bgpipe[0]);
4225cf4388bSBrian Somers         bundle->notify.fd = bgpipe[1];
423aefd026aSBrian Somers       }
424aefd026aSBrian Somers 
425da66dd13SBrian Somers       bundle_LockTun(bundle);	/* we have a new pid */
426da66dd13SBrian Somers 
427565e35e5SBrian Somers       /* -auto, -dedicated, -ddial & -background */
428b6217683SBrian Somers       prompt_Destroy(prompt, 0);
4292a279fedSBrian Somers       close(STDOUT_FILENO);
4302a279fedSBrian Somers       close(STDERR_FILENO);
4312a279fedSBrian Somers       close(STDIN_FILENO);
432b6217683SBrian Somers       setsid();
433565e35e5SBrian Somers     } else {
434565e35e5SBrian Somers       /* -direct: STDIN_FILENO gets used by modem_Open */
435565e35e5SBrian Somers       prompt_TtyInit(NULL);
436565e35e5SBrian Somers       close(STDOUT_FILENO);
437565e35e5SBrian Somers       close(STDERR_FILENO);
438d656a4c5SBrian Somers     }
439af57ed9fSAtsushi Murai   } else {
440565e35e5SBrian Somers     /* Interactive mode */
4412a279fedSBrian Somers     close(STDERR_FILENO);
442565e35e5SBrian Somers     prompt_TtyInit(prompt);
443b6217683SBrian Somers     prompt_TtyCommandMode(prompt);
444b6217683SBrian Somers     prompt_Required(prompt);
445af57ed9fSAtsushi Murai   }
44635495becSBrian Somers 
447dd7e2610SBrian Somers   log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode));
4480f78c7a7SBrian Somers   DoLoop(bundle);
4491afedc4bSBrian Somers   AbortProgram(EX_NORMAL);
450af57ed9fSAtsushi Murai 
4511afedc4bSBrian Somers   return EX_NORMAL;
452af57ed9fSAtsushi Murai }
453af57ed9fSAtsushi Murai 
454af57ed9fSAtsushi Murai static void
4550f78c7a7SBrian Somers DoLoop(struct bundle *bundle)
456af57ed9fSAtsushi Murai {
457af57ed9fSAtsushi Murai   fd_set rfds, wfds, efds;
4581af29a6eSBrian Somers   int i, nfds, nothing_done;
4591af29a6eSBrian Somers   struct probe probe;
4601af29a6eSBrian Somers 
4611af29a6eSBrian Somers   probe_Init(&probe);
462c3899f8dSAtsushi Murai 
463565e35e5SBrian Somers   do {
464780700e5SAndrey A. Chernov     nfds = 0;
465944f7098SBrian Somers     FD_ZERO(&rfds);
466944f7098SBrian Somers     FD_ZERO(&wfds);
467944f7098SBrian Somers     FD_ZERO(&efds);
46884b8a6ebSAtsushi Murai 
4690f2f3eb3SBrian Somers     /* All our datalinks, the tun device and the MP socket */
4706f384573SBrian Somers     descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds);
4710f2f3eb3SBrian Somers 
4720f2f3eb3SBrian Somers     /* All our prompts and the diagnostic socket */
4730f2f3eb3SBrian Somers     descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds);
47407030d97SBrian Somers 
4753b0f8d2eSBrian Somers     if (bundle_IsDead(bundle))
476f4768038SBrian Somers       /* Don't select - we'll be here forever */
477f4768038SBrian Somers       break;
4780706ff38SBrian Somers 
479486105bcSBrian Somers     /*
480486105bcSBrian Somers      * It's possible that we've had a signal since we last checked.  If
481486105bcSBrian Somers      * we don't check again before calling select(), we may end up stuck
482486105bcSBrian Somers      * after having missed the event.... sig_Handle() tries to be as
483486105bcSBrian Somers      * quick as possible if nothing is likely to have happened.
484486105bcSBrian Somers      * This is only really likely if we block in open(... O_NONBLOCK)
485486105bcSBrian Somers      * which will happen with a misconfigured device.
486486105bcSBrian Somers      */
487486105bcSBrian Somers     if (sig_Handle())
488486105bcSBrian Somers       continue;
489486105bcSBrian Somers 
4903006ec67SBrian Somers     i = select(nfds, &rfds, &wfds, &efds, NULL);
491712ae387SBrian Somers 
49254cd8e13SBrian Somers     if (i < 0 && errno != EINTR) {
493dd7e2610SBrian Somers       log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
49424989c68SBrian Somers       if (log_IsKept(LogTIMER)) {
49524989c68SBrian Somers         struct timeval t;
49624989c68SBrian Somers 
49724989c68SBrian Somers         for (i = 0; i <= nfds; i++) {
49824989c68SBrian Somers           if (FD_ISSET(i, &rfds)) {
49924989c68SBrian Somers             log_Printf(LogTIMER, "Read set contains %d\n", i);
50024989c68SBrian Somers             FD_CLR(i, &rfds);
50124989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
50224989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
50324989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
504af57ed9fSAtsushi Murai               break;
505af57ed9fSAtsushi Murai             }
50624989c68SBrian Somers           }
50724989c68SBrian Somers           if (FD_ISSET(i, &wfds)) {
50824989c68SBrian Somers             log_Printf(LogTIMER, "Write set contains %d\n", i);
50924989c68SBrian Somers             FD_CLR(i, &wfds);
51024989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
51124989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
51224989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
513af57ed9fSAtsushi Murai               break;
514af57ed9fSAtsushi Murai             }
515e0d3e233SAndrey A. Chernov           }
51624989c68SBrian Somers           if (FD_ISSET(i, &efds)) {
51724989c68SBrian Somers             log_Printf(LogTIMER, "Error set contains %d\n", i);
51824989c68SBrian Somers             FD_CLR(i, &efds);
51924989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
52024989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
52124989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
52258f264e1SBrian Somers               break;
52358f264e1SBrian Somers             }
52424989c68SBrian Somers           }
52524989c68SBrian Somers         }
52624989c68SBrian Somers       }
52758f264e1SBrian Somers       break;
528de451c68SBrian Somers     }
529de451c68SBrian Somers 
530f0cdd9c0SBrian Somers     log_Printf(LogTIMER, "Select returns %d\n", i);
531f0cdd9c0SBrian Somers 
53254cd8e13SBrian Somers     sig_Handle();
53354cd8e13SBrian Somers 
53454cd8e13SBrian Somers     if (i <= 0)
53554cd8e13SBrian Somers       continue;
53654cd8e13SBrian Somers 
5373006ec67SBrian Somers     for (i = 0; i <= nfds; i++)
5383006ec67SBrian Somers       if (FD_ISSET(i, &efds)) {
539991c2a7bSBrian Somers         log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
540991c2a7bSBrian Somers         /* We deal gracefully with link descriptor exceptions */
541991c2a7bSBrian Somers         if (!bundle_Exception(bundle, i)) {
542991c2a7bSBrian Somers           log_Printf(LogERROR, "Exception cannot be handled !\n");
5431ae349f5Scvs2svn           break;
5441ae349f5Scvs2svn         }
545991c2a7bSBrian Somers       }
546c60f92caSBrian Somers 
547b7c5748eSBrian Somers     if (i <= nfds)
548b7c5748eSBrian Somers       break;
549b7c5748eSBrian Somers 
5501af29a6eSBrian Somers     nothing_done = 1;
5511af29a6eSBrian Somers 
5521af29a6eSBrian Somers     if (descriptor_IsSet(&server.desc, &rfds)) {
553b77776a7SBrian Somers       descriptor_Read(&server.desc, bundle, &rfds);
5541af29a6eSBrian Somers       nothing_done = 0;
5551af29a6eSBrian Somers     }
5561af29a6eSBrian Somers 
5571af29a6eSBrian Somers     if (descriptor_IsSet(&bundle->desc, &rfds)) {
5581af29a6eSBrian Somers       descriptor_Read(&bundle->desc, bundle, &rfds);
5591af29a6eSBrian Somers       nothing_done = 0;
5601af29a6eSBrian Somers     }
56142d4d396SBrian Somers 
5622f786681SBrian Somers     if (descriptor_IsSet(&bundle->desc, &wfds))
5631af29a6eSBrian Somers       if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) {
5641af29a6eSBrian Somers         /*
5651af29a6eSBrian Somers          * This is disasterous.  The OS has told us that something is
5661af29a6eSBrian Somers          * writable, and all our write()s have failed.  Rather than
5671af29a6eSBrian Somers          * going back immediately to do our UpdateSet()s and select(),
5681af29a6eSBrian Somers          * we sleep for a bit to avoid gobbling up all cpu time.
5691af29a6eSBrian Somers          */
5701af29a6eSBrian Somers         struct timeval t;
5712f786681SBrian Somers 
5721af29a6eSBrian Somers         t.tv_sec = 0;
5731af29a6eSBrian Somers         t.tv_usec = 100000;
5741af29a6eSBrian Somers         select(0, NULL, NULL, NULL, &t);
5751af29a6eSBrian Somers       }
576565e35e5SBrian Somers   } while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle));
577565e35e5SBrian Somers 
578dd7e2610SBrian Somers   log_Printf(LogDEBUG, "DoLoop done.\n");
579af57ed9fSAtsushi Murai }
580