xref: /freebsd/usr.sbin/ppp/main.c (revision c0593e34b75b670f98f730106bb4dd78b30bb1a0)
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  *
2097d92980SPeter Wemm  * $FreeBSD$
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>
36c0593e34SBrian Somers #include <stdlib.h>
3775240ed1SBrian Somers #include <string.h>
3875240ed1SBrian Somers #include <sys/time.h>
3975240ed1SBrian Somers #include <termios.h>
4075240ed1SBrian Somers #include <unistd.h>
411080ea25SBrian Somers #include <sys/stat.h>
4275240ed1SBrian Somers 
4367b072f7SBrian Somers #ifndef NONAT
447884358fSBrian Somers #ifdef __FreeBSD__
451595bacdSBrian Somers #include <alias.h>
467884358fSBrian Somers #else
477884358fSBrian Somers #include "alias.h"
481595bacdSBrian Somers #endif
491595bacdSBrian Somers #endif
505d9e6103SBrian Somers #include "layer.h"
511af29a6eSBrian Somers #include "probe.h"
5275240ed1SBrian Somers #include "mbuf.h"
5375240ed1SBrian Somers #include "log.h"
5475240ed1SBrian Somers #include "defs.h"
555106c671SBrian Somers #include "id.h"
5675240ed1SBrian Somers #include "timer.h"
5775240ed1SBrian Somers #include "fsm.h"
58879ed6faSBrian Somers #include "lqr.h"
59af57ed9fSAtsushi Murai #include "hdlc.h"
60af57ed9fSAtsushi Murai #include "lcp.h"
610053cc58SBrian Somers #include "ccp.h"
6229e275ceSBrian Somers #include "iplist.h"
6329e275ceSBrian Somers #include "throughput.h"
64eaa4df37SBrian Somers #include "slcompress.h"
65af57ed9fSAtsushi Murai #include "ipcp.h"
6684b8a6ebSAtsushi Murai #include "filter.h"
672f786681SBrian Somers #include "descriptor.h"
683b0f8d2eSBrian Somers #include "link.h"
693b0f8d2eSBrian Somers #include "mp.h"
70972a1bcfSBrian Somers #ifndef NORADIUS
71972a1bcfSBrian Somers #include "radius.h"
72972a1bcfSBrian Somers #endif
735828db6dSBrian Somers #include "bundle.h"
741ae349f5Scvs2svn #include "auth.h"
75ed6a16c1SPoul-Henning Kamp #include "systems.h"
76f5ff0f7cSBrian Somers #include "sig.h"
7775240ed1SBrian Somers #include "main.h"
7877ff88adSBrian Somers #include "server.h"
7985b542cfSBrian Somers #include "prompt.h"
80b6dec9f0SBrian Somers #include "chat.h"
81e2ebb036SBrian Somers #include "chap.h"
8292b09558SBrian Somers #include "cbcp.h"
833006ec67SBrian Somers #include "datalink.h"
848fa6ebe4SBrian Somers #include "iface.h"
8553c9f6c0SAtsushi Murai 
8653c9f6c0SAtsushi Murai #ifndef O_NONBLOCK
8753c9f6c0SAtsushi Murai #ifdef O_NDELAY
8853c9f6c0SAtsushi Murai #define	O_NONBLOCK O_NDELAY
8953c9f6c0SAtsushi Murai #endif
9053c9f6c0SAtsushi Murai #endif
91af57ed9fSAtsushi Murai 
920f78c7a7SBrian Somers static void DoLoop(struct bundle *);
9375240ed1SBrian Somers static void TerminalStop(int);
94b6e82f33SBrian Somers static const char *ex_desc(int);
9575240ed1SBrian Somers 
9683d1af55SBrian Somers static struct bundle *SignalBundle;
97b6217683SBrian Somers static struct prompt *SignalPrompt;
98c3899f8dSAtsushi Murai 
99c3899f8dSAtsushi Murai void
100944f7098SBrian Somers Cleanup(int excode)
101af57ed9fSAtsushi Murai {
102a0cbd833SBrian Somers   SignalBundle->CleaningUp = 1;
1039c81b87dSBrian Somers   bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
1046e4959f0SBrian Somers }
105af57ed9fSAtsushi Murai 
1061afedc4bSBrian Somers void
1071afedc4bSBrian Somers AbortProgram(int excode)
1081afedc4bSBrian Somers {
109dd7e2610SBrian Somers   server_Close(SignalBundle);
110dd7e2610SBrian Somers   log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
1119c81b87dSBrian Somers   bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
11268a0f0ccSBrian Somers   bundle_Destroy(SignalBundle);
113dd7e2610SBrian Somers   log_Close();
114af57ed9fSAtsushi Murai   exit(excode);
115af57ed9fSAtsushi Murai }
116af57ed9fSAtsushi Murai 
117af57ed9fSAtsushi Murai static void
118944f7098SBrian Somers CloseConnection(int signo)
119af57ed9fSAtsushi Murai {
120368aee2bSBrian Somers   /* NOTE, these are manual, we've done a setsid() */
121dd7e2610SBrian Somers   sig_signal(SIGINT, SIG_IGN);
122dd7e2610SBrian Somers   log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
123899011c4SBrian Somers   bundle_Down(SignalBundle, CLOSE_STAYDOWN);
124dd7e2610SBrian Somers   sig_signal(SIGINT, CloseConnection);
1256d14e2a8SJordan K. Hubbard }
126af57ed9fSAtsushi Murai 
127af57ed9fSAtsushi Murai static void
128944f7098SBrian Somers CloseSession(int signo)
129af57ed9fSAtsushi Murai {
130dd7e2610SBrian Somers   log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
131af57ed9fSAtsushi Murai   Cleanup(EX_TERM);
132af57ed9fSAtsushi Murai }
133c3899f8dSAtsushi Murai 
13475b8d283SBrian Somers static pid_t BGPid = 0;
13575b8d283SBrian Somers 
13675b8d283SBrian Somers static void
13775b8d283SBrian Somers KillChild(int signo)
13875b8d283SBrian Somers {
139ac37ab22SBrian Somers   signal(signo, SIG_IGN);
140dd7e2610SBrian Somers   log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
14175b8d283SBrian Somers   kill(BGPid, SIGINT);
14275b8d283SBrian Somers }
14375b8d283SBrian Somers 
144c3899f8dSAtsushi Murai static void
145b6e82f33SBrian Somers TerminalCont(int signo)
146c3899f8dSAtsushi Murai {
147f91ad6b0SBrian Somers   signal(SIGCONT, SIG_DFL);
148f91ad6b0SBrian Somers   prompt_Continue(SignalPrompt);
149c3899f8dSAtsushi Murai }
150c3899f8dSAtsushi Murai 
151c3899f8dSAtsushi Murai static void
152944f7098SBrian Somers TerminalStop(int signo)
153c3899f8dSAtsushi Murai {
154f91ad6b0SBrian Somers   prompt_Suspend(SignalPrompt);
155f91ad6b0SBrian Somers   signal(SIGCONT, TerminalCont);
156f91ad6b0SBrian Somers   raise(SIGSTOP);
1574ef16f24SBrian Somers }
1584ef16f24SBrian Somers 
1598ea8442cSBrian Somers static void
1608ea8442cSBrian Somers BringDownServer(int signo)
1618ea8442cSBrian Somers {
162b6217683SBrian Somers   /* Drops all child prompts too ! */
163dd7e2610SBrian Somers   server_Close(SignalBundle);
1648ea8442cSBrian Somers }
1658ea8442cSBrian Somers 
166b6e82f33SBrian Somers static const char *
1676efd9292SBrian Somers ex_desc(int ex)
1686efd9292SBrian Somers {
169d93d3a9cSBrian Somers   static char num[12];		/* Used immediately if returned */
170b6e82f33SBrian Somers   static const char *desc[] = {
171b6e82f33SBrian Somers     "normal", "start", "sock", "modem", "dial", "dead", "done",
172b6e82f33SBrian Somers     "reboot", "errdead", "hangup", "term", "nodial", "nologin"
173b6e82f33SBrian Somers   };
1746efd9292SBrian Somers 
17570ee81ffSBrian Somers   if (ex >= 0 && ex < sizeof desc / sizeof *desc)
1766efd9292SBrian Somers     return desc[ex];
1776efd9292SBrian Somers   snprintf(num, sizeof num, "%d", ex);
1786efd9292SBrian Somers   return num;
1796efd9292SBrian Somers }
180c3899f8dSAtsushi Murai 
18175240ed1SBrian Somers static void
182b6e82f33SBrian Somers Usage(void)
183af57ed9fSAtsushi Murai {
184680026d6SNate Williams   fprintf(stderr,
18567b072f7SBrian Somers 	  "Usage: ppp [-auto | -foreground | -background | -direct | -dedicated | -ddial | -interactive]"
186b6e82f33SBrian Somers #ifndef NOALIAS
18767b072f7SBrian Somers           " [-nat]"
188b6e82f33SBrian Somers #endif
189c0593e34SBrian Somers           " [-quiet] [-unit N] [system ...]\n");
190af57ed9fSAtsushi Murai   exit(EX_START);
191af57ed9fSAtsushi Murai }
192af57ed9fSAtsushi Murai 
193c0593e34SBrian Somers struct switches {
194c0593e34SBrian Somers   unsigned nat : 1;
195c0593e34SBrian Somers   unsigned fg : 1;
196c0593e34SBrian Somers   unsigned quiet : 1;
197c0593e34SBrian Somers   int mode;
198c0593e34SBrian Somers   int unit;
199c0593e34SBrian Somers };
200c0593e34SBrian Somers 
2017cf368ebSBrian Somers static int
202c0593e34SBrian Somers ProcessArgs(int argc, char **argv, struct switches *sw)
203af57ed9fSAtsushi Murai {
2047cf368ebSBrian Somers   int optc, newmode, arg;
205af57ed9fSAtsushi Murai   char *cp;
206af57ed9fSAtsushi Murai 
2077cf368ebSBrian Somers   optc = 0;
208c0593e34SBrian Somers   memset(sw, '\0', sizeof *sw);
209c0593e34SBrian Somers   sw->mode = PHYS_INTERACTIVE;
210c0593e34SBrian Somers   sw->unit = -1;
211c0593e34SBrian Somers 
2127cf368ebSBrian Somers   for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
2137cf368ebSBrian Somers     cp = argv[arg] + 1;
21481358fa3SBrian Somers     newmode = Nam2mode(cp);
21581358fa3SBrian Somers     switch (newmode) {
21681358fa3SBrian Somers       case PHYS_NONE:
217c0593e34SBrian Somers         if (strcmp(cp, "nat") == 0) {
21867b072f7SBrian Somers #ifdef NONAT
219c0593e34SBrian Somers           log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
220615ad4f9SBrian Somers #else
221c0593e34SBrian Somers           sw->nat = 1;
222615ad4f9SBrian Somers #endif
2231ae349f5Scvs2svn           optc--;			/* this option isn't exclusive */
224c0593e34SBrian Somers         } else if (strcmp(cp, "alias") == 0) {
225c0593e34SBrian Somers #ifdef NONAT
226c0593e34SBrian Somers           log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
227c0593e34SBrian Somers           fprintf(stderr, "%s ignored: NAT is compiled out\n", argv[arg]);
228c0593e34SBrian Somers #else
229c0593e34SBrian Somers           log_Printf(LogWARN, "%s is depricated\n", argv[arg]);
230c0593e34SBrian Somers           fprintf(stderr, "%s is depricated\n", argv[arg]);
231c0593e34SBrian Somers           sw->nat = 1;
232c0593e34SBrian Somers #endif
233c0593e34SBrian Somers           optc--;			/* this option isn't exclusive */
234c0593e34SBrian Somers         } else if (strncmp(cp, "unit", 4) == 0) {
235c0593e34SBrian Somers           if (cp[4] == '\0') {
236c0593e34SBrian Somers             if (++arg == argc) {
237c0593e34SBrian Somers               fprintf(stderr, "-unit: Expected unit number\n");
238c0593e34SBrian Somers               Usage();
239c0593e34SBrian Somers             } else
240c0593e34SBrian Somers               sw->unit = atoi(argv[arg]);
241c0593e34SBrian Somers           } else
242c0593e34SBrian Somers             sw->unit = atoi(cp + 4);
24367b072f7SBrian Somers         } else if (strcmp(cp, "quiet") == 0) {
244c0593e34SBrian Somers           sw->quiet = 1;
24567b072f7SBrian Somers           optc--;			/* this option isn't exclusive */
24667b072f7SBrian Somers         } else if (strcmp(cp, "foreground") == 0) {
247c0593e34SBrian Somers           sw->mode = PHYS_BACKGROUND;	/* Kinda like background mode */
248c0593e34SBrian Somers           sw->fg = 1;
249944f7098SBrian Somers         } else
250af57ed9fSAtsushi Murai           Usage();
25181358fa3SBrian Somers         break;
25281358fa3SBrian Somers 
25381358fa3SBrian Somers       case PHYS_ALL:
25481358fa3SBrian Somers         Usage();
25581358fa3SBrian Somers         break;
25681358fa3SBrian Somers 
25781358fa3SBrian Somers       default:
258c0593e34SBrian Somers         sw->mode = newmode;
25981358fa3SBrian Somers     }
260af57ed9fSAtsushi Murai   }
261af57ed9fSAtsushi Murai 
262af57ed9fSAtsushi Murai   if (optc > 1) {
26385602e52SBrian Somers     fprintf(stderr, "You may specify only one mode.\n");
2641ae349f5Scvs2svn     exit(EX_START);
2651ae349f5Scvs2svn   }
2661ae349f5Scvs2svn 
267c0593e34SBrian Somers   if (sw->mode == PHYS_AUTO && arg == argc) {
2687cf368ebSBrian Somers     fprintf(stderr, "A system must be specified in auto mode.\n");
269af57ed9fSAtsushi Murai     exit(EX_START);
270af57ed9fSAtsushi Murai   }
27139f94eddSBrian Somers 
2727cf368ebSBrian Somers   return arg;		/* Don't SetLabel yet ! */
273af57ed9fSAtsushi Murai }
274af57ed9fSAtsushi Murai 
2757cf368ebSBrian Somers static void
2767cf368ebSBrian Somers CheckLabel(const char *label, struct prompt *prompt, int mode)
2777cf368ebSBrian Somers {
2787cf368ebSBrian Somers   const char *err;
2797cf368ebSBrian Somers 
2807cf368ebSBrian Somers   if ((err = system_IsValid(label, prompt, mode)) != NULL) {
2817cf368ebSBrian Somers     fprintf(stderr, "%s: %s\n", label, err);
2827cf368ebSBrian Somers     if (mode == PHYS_DIRECT)
2837cf368ebSBrian Somers       log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
2847cf368ebSBrian Somers                  label, err);
2857cf368ebSBrian Somers     log_Close();
2867cf368ebSBrian Somers     exit(1);
2877cf368ebSBrian Somers   }
2887cf368ebSBrian Somers }
2897cf368ebSBrian Somers 
2907cf368ebSBrian Somers 
2914ef16f24SBrian Somers int
292944f7098SBrian Somers main(int argc, char **argv)
293af57ed9fSAtsushi Murai {
2947cf368ebSBrian Somers   char *name;
295756783fcSBrian Somers   const char *lastlabel;
296c0593e34SBrian Somers   int nfds, label, arg;
2977a6f8720SBrian Somers   struct bundle *bundle;
298b6217683SBrian Somers   struct prompt *prompt;
299c0593e34SBrian Somers   struct switches sw;
300e3b4c400SBrian Somers 
301e3b4c400SBrian Somers   nfds = getdtablesize();
302e3b4c400SBrian Somers   if (nfds >= FD_SETSIZE)
303e3b4c400SBrian Somers     /*
304e3b4c400SBrian Somers      * If we've got loads of file descriptors, make sure they're all
305e3b4c400SBrian Somers      * closed.  If they aren't, we may end up with a seg fault when our
306e3b4c400SBrian Somers      * `fd_set's get too big when select()ing !
307e3b4c400SBrian Somers      */
308e3b4c400SBrian Somers     while (--nfds > 2)
309e3b4c400SBrian Somers       close(nfds);
310af57ed9fSAtsushi Murai 
31175240ed1SBrian Somers   name = strrchr(argv[0], '/');
312dd7e2610SBrian Somers   log_Open(name ? name + 1 : argv[0]);
3132a279fedSBrian Somers 
31467b072f7SBrian Somers #ifndef NONAT
3152a630835SBrian Somers   PacketAliasInit();
3162a630835SBrian Somers #endif
317c0593e34SBrian Somers   label = ProcessArgs(argc, argv, &sw);
31885b542cfSBrian Somers 
31985b542cfSBrian Somers   /*
320a611383fSBrian Somers    * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops
321a611383fSBrian Somers    * output occasionally.... I must find the real reason some time.  To
322a611383fSBrian Somers    * display the dodgy behaviour, comment out this bit, make yourself a large
32385b542cfSBrian Somers    * routing table and then run ppp in interactive mode.  The `show route'
32485b542cfSBrian Somers    * command will drop chunks of data !!!
32585b542cfSBrian Somers    */
326c0593e34SBrian Somers   if (sw.mode == PHYS_INTERACTIVE) {
32785b542cfSBrian Somers     close(STDIN_FILENO);
32885b542cfSBrian Somers     if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
32985b542cfSBrian Somers       fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
33085b542cfSBrian Somers       return 2;
33185b542cfSBrian Somers     }
33285b542cfSBrian Somers   }
33385b542cfSBrian Somers 
334b6217683SBrian Somers   /* Allow output for the moment (except in direct mode) */
335c0593e34SBrian Somers   if (sw.mode == PHYS_DIRECT)
336b6217683SBrian Somers     prompt = NULL;
337ed0e9269SBrian Somers   else
338b6217683SBrian Somers     SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
33912ef29a8SBrian Somers 
3405106c671SBrian Somers   ID0init();
3414562be74SBrian Somers   if (ID0realuid() != 0) {
3424562be74SBrian Somers     char conf[200], *ptr;
3434562be74SBrian Somers 
3444562be74SBrian Somers     snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE);
3454562be74SBrian Somers     do {
3461080ea25SBrian Somers       struct stat sb;
3471080ea25SBrian Somers 
3481080ea25SBrian Somers       if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) {
349a33b2ef7SBrian Somers         log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n",
350a33b2ef7SBrian Somers                    conf);
3514562be74SBrian Somers         return -1;
3524562be74SBrian Somers       }
3534562be74SBrian Somers       ptr = conf + strlen(conf)-2;
3544562be74SBrian Somers       while (ptr > conf && *ptr != '/')
3554562be74SBrian Somers         *ptr-- = '\0';
3564562be74SBrian Somers     } while (ptr >= conf);
3574562be74SBrian Somers   }
3584562be74SBrian Somers 
3597cf368ebSBrian Somers   if (label < argc)
3607cf368ebSBrian Somers     for (arg = label; arg < argc; arg++)
361c0593e34SBrian Somers       CheckLabel(argv[arg], prompt, sw.mode);
3627cf368ebSBrian Somers   else
363c0593e34SBrian Somers     CheckLabel("default", prompt, sw.mode);
36412ef29a8SBrian Somers 
365c0593e34SBrian Somers   if (!sw.quiet)
366c0593e34SBrian Somers     prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode));
367ed0e9269SBrian Somers 
368c0593e34SBrian Somers   if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit,
369c0593e34SBrian Somers                               (const char **)argv)) == NULL)
3704ef16f24SBrian Somers     return EX_START;
371756783fcSBrian Somers 
372756783fcSBrian Somers   /* NOTE:  We may now have changed argv[1] via a ``set proctitle'' */
373756783fcSBrian Somers 
3740f2f3eb3SBrian Somers   if (prompt) {
3750f2f3eb3SBrian Somers     prompt->bundle = bundle;	/* couldn't do it earlier */
376c0593e34SBrian Somers     if (!sw.quiet)
3778fa6ebe4SBrian Somers       prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
3780f2f3eb3SBrian Somers   }
37983d1af55SBrian Somers   SignalBundle = bundle;
380c0593e34SBrian Somers   bundle->NatEnabled = sw.nat;
381c0593e34SBrian Somers   if (sw.nat)
3828fa6ebe4SBrian Somers     bundle->cfg.opt |= OPT_IFACEALIAS;
38312ef29a8SBrian Somers 
38430291ffbSBrian Somers   if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0)
385565e35e5SBrian Somers     prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
3861ae349f5Scvs2svn 
387dd7e2610SBrian Somers   sig_signal(SIGHUP, CloseSession);
388dd7e2610SBrian Somers   sig_signal(SIGTERM, CloseSession);
389dd7e2610SBrian Somers   sig_signal(SIGINT, CloseConnection);
390dd7e2610SBrian Somers   sig_signal(SIGQUIT, CloseSession);
391dd7e2610SBrian Somers   sig_signal(SIGALRM, SIG_IGN);
392e0d3e233SAndrey A. Chernov   signal(SIGPIPE, SIG_IGN);
393565e35e5SBrian Somers 
394c0593e34SBrian Somers   if (sw.mode == PHYS_INTERACTIVE)
395dd7e2610SBrian Somers     sig_signal(SIGTSTP, TerminalStop);
396f91ad6b0SBrian Somers 
397dd7e2610SBrian Somers   sig_signal(SIGUSR2, BringDownServer);
398af57ed9fSAtsushi Murai 
399756783fcSBrian Somers   lastlabel = argc == 2 ? bundle->argv1 : argv[argc - 1];
4007cf368ebSBrian Somers   for (arg = label; arg < argc; arg++) {
4017cf368ebSBrian Somers     /* In case we use LABEL or ``set enddisc label'' */
402756783fcSBrian Somers     bundle_SetLabel(bundle, lastlabel);
403756783fcSBrian Somers     system_Select(bundle, arg == 1 ? bundle->argv1 : argv[arg],
404756783fcSBrian Somers                   CONFFILE, prompt, NULL);
4051ae349f5Scvs2svn   }
4067cf368ebSBrian Somers 
4077cf368ebSBrian Somers   if (label < argc)
4087cf368ebSBrian Somers     /* In case the last label did a ``load'' */
409756783fcSBrian Somers     bundle_SetLabel(bundle, lastlabel);
4107cf368ebSBrian Somers 
411c0593e34SBrian Somers   if (sw.mode == PHYS_AUTO &&
4125828db6dSBrian Somers       bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
4137cf368ebSBrian Somers     prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
4147cf368ebSBrian Somers                   "in auto mode.\n");
4158390b576SBrian Somers     AbortProgram(EX_START);
416af57ed9fSAtsushi Murai   }
4176efd9292SBrian Somers 
418c0593e34SBrian Somers   if (sw.mode != PHYS_INTERACTIVE) {
419c0593e34SBrian Somers     if (sw.mode != PHYS_DIRECT) {
420c0593e34SBrian Somers       if (!sw.fg) {
4215cf4388bSBrian Somers         int bgpipe[2];
4226d14e2a8SJordan K. Hubbard         pid_t bgpid;
423a9c6b5dfSAtsushi Murai 
424c0593e34SBrian Somers         if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) {
425dd7e2610SBrian Somers           log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
4268390b576SBrian Somers 	  AbortProgram(EX_SOCK);
4271ae349f5Scvs2svn         }
4281ae349f5Scvs2svn 
4296d14e2a8SJordan K. Hubbard         bgpid = fork();
4306d14e2a8SJordan K. Hubbard         if (bgpid == -1) {
431dd7e2610SBrian Somers 	  log_Printf(LogERROR, "fork: %s\n", strerror(errno));
4328390b576SBrian Somers 	  AbortProgram(EX_SOCK);
4336d14e2a8SJordan K. Hubbard         }
4345cf4388bSBrian Somers 
4356d14e2a8SJordan K. Hubbard         if (bgpid) {
4366d14e2a8SJordan K. Hubbard 	  char c = EX_NORMAL;
437a9c6b5dfSAtsushi Murai 
438c0593e34SBrian Somers 	  if (sw.mode == PHYS_BACKGROUND) {
4395cf4388bSBrian Somers 	    close(bgpipe[1]);
4406d14e2a8SJordan K. Hubbard 	    BGPid = bgpid;
44175b8d283SBrian Somers             /* If we get a signal, kill the child */
44275b8d283SBrian Somers             signal(SIGHUP, KillChild);
44375b8d283SBrian Somers             signal(SIGTERM, KillChild);
44475b8d283SBrian Somers             signal(SIGINT, KillChild);
44575b8d283SBrian Somers             signal(SIGQUIT, KillChild);
44675b8d283SBrian Somers 
44775b8d283SBrian Somers 	    /* Wait for our child to close its pipe before we exit */
4485cf4388bSBrian Somers 	    if (read(bgpipe[0], &c, 1) != 1) {
449b6217683SBrian Somers 	      prompt_Printf(prompt, "Child exit, no status.\n");
450dd7e2610SBrian Somers 	      log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
4516efd9292SBrian Somers 	    } else if (c == EX_NORMAL) {
452b6217683SBrian Somers 	      prompt_Printf(prompt, "PPP enabled.\n");
453dd7e2610SBrian Somers 	      log_Printf(LogPHASE, "Parent: PPP enabled.\n");
4546efd9292SBrian Somers 	    } else {
455b6217683SBrian Somers 	      prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c));
456dd7e2610SBrian Somers 	      log_Printf(LogPHASE, "Parent: Child failed (%s).\n",
45780e37c72SBrian Somers 		         ex_desc((int) c));
4586efd9292SBrian Somers 	    }
4595cf4388bSBrian Somers 	    close(bgpipe[0]);
4606d14e2a8SJordan K. Hubbard 	  }
4614ef16f24SBrian Somers 	  return c;
462c0593e34SBrian Somers         } else if (sw.mode == PHYS_BACKGROUND) {
4635cf4388bSBrian Somers 	  close(bgpipe[0]);
4645cf4388bSBrian Somers           bundle->notify.fd = bgpipe[1];
465aefd026aSBrian Somers         }
466aefd026aSBrian Somers 
467da66dd13SBrian Somers         bundle_LockTun(bundle);	/* we have a new pid */
46867b072f7SBrian Somers       }
469da66dd13SBrian Somers 
47067b072f7SBrian Somers       /* -auto, -dedicated, -ddial, -foreground & -background */
471b6217683SBrian Somers       prompt_Destroy(prompt, 0);
4722a279fedSBrian Somers       close(STDOUT_FILENO);
4732a279fedSBrian Somers       close(STDERR_FILENO);
4742a279fedSBrian Somers       close(STDIN_FILENO);
475c0593e34SBrian Somers       if (!sw.fg)
476b6217683SBrian Somers         setsid();
477565e35e5SBrian Somers     } else {
47867b072f7SBrian Somers       /* -direct - STDIN_FILENO gets used by physical_Open */
479565e35e5SBrian Somers       prompt_TtyInit(NULL);
480565e35e5SBrian Somers       close(STDOUT_FILENO);
481565e35e5SBrian Somers       close(STDERR_FILENO);
482d656a4c5SBrian Somers     }
483af57ed9fSAtsushi Murai   } else {
48467b072f7SBrian Somers     /* -interactive */
4852a279fedSBrian Somers     close(STDERR_FILENO);
486565e35e5SBrian Somers     prompt_TtyInit(prompt);
487b6217683SBrian Somers     prompt_TtyCommandMode(prompt);
488b6217683SBrian Somers     prompt_Required(prompt);
489af57ed9fSAtsushi Murai   }
49035495becSBrian Somers 
491c0593e34SBrian Somers   log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode));
4920f78c7a7SBrian Somers   DoLoop(bundle);
4931afedc4bSBrian Somers   AbortProgram(EX_NORMAL);
494af57ed9fSAtsushi Murai 
4951afedc4bSBrian Somers   return EX_NORMAL;
496af57ed9fSAtsushi Murai }
497af57ed9fSAtsushi Murai 
498af57ed9fSAtsushi Murai static void
4990f78c7a7SBrian Somers DoLoop(struct bundle *bundle)
500af57ed9fSAtsushi Murai {
501af57ed9fSAtsushi Murai   fd_set rfds, wfds, efds;
5021af29a6eSBrian Somers   int i, nfds, nothing_done;
5031af29a6eSBrian Somers   struct probe probe;
5041af29a6eSBrian Somers 
5051af29a6eSBrian Somers   probe_Init(&probe);
506c3899f8dSAtsushi Murai 
507565e35e5SBrian Somers   do {
508780700e5SAndrey A. Chernov     nfds = 0;
509944f7098SBrian Somers     FD_ZERO(&rfds);
510944f7098SBrian Somers     FD_ZERO(&wfds);
511944f7098SBrian Somers     FD_ZERO(&efds);
51284b8a6ebSAtsushi Murai 
5130f2f3eb3SBrian Somers     /* All our datalinks, the tun device and the MP socket */
5146f384573SBrian Somers     descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds);
5150f2f3eb3SBrian Somers 
5160f2f3eb3SBrian Somers     /* All our prompts and the diagnostic socket */
5170f2f3eb3SBrian Somers     descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds);
51807030d97SBrian Somers 
5193b0f8d2eSBrian Somers     if (bundle_IsDead(bundle))
520f4768038SBrian Somers       /* Don't select - we'll be here forever */
521f4768038SBrian Somers       break;
5220706ff38SBrian Somers 
523486105bcSBrian Somers     /*
524486105bcSBrian Somers      * It's possible that we've had a signal since we last checked.  If
525486105bcSBrian Somers      * we don't check again before calling select(), we may end up stuck
526486105bcSBrian Somers      * after having missed the event.... sig_Handle() tries to be as
527486105bcSBrian Somers      * quick as possible if nothing is likely to have happened.
528486105bcSBrian Somers      * This is only really likely if we block in open(... O_NONBLOCK)
529486105bcSBrian Somers      * which will happen with a misconfigured device.
530486105bcSBrian Somers      */
531486105bcSBrian Somers     if (sig_Handle())
532486105bcSBrian Somers       continue;
533486105bcSBrian Somers 
5343006ec67SBrian Somers     i = select(nfds, &rfds, &wfds, &efds, NULL);
535712ae387SBrian Somers 
53654cd8e13SBrian Somers     if (i < 0 && errno != EINTR) {
537dd7e2610SBrian Somers       log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
53824989c68SBrian Somers       if (log_IsKept(LogTIMER)) {
53924989c68SBrian Somers         struct timeval t;
54024989c68SBrian Somers 
54124989c68SBrian Somers         for (i = 0; i <= nfds; i++) {
54224989c68SBrian Somers           if (FD_ISSET(i, &rfds)) {
54324989c68SBrian Somers             log_Printf(LogTIMER, "Read set contains %d\n", i);
54424989c68SBrian Somers             FD_CLR(i, &rfds);
54524989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
54624989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
54724989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
548af57ed9fSAtsushi Murai               break;
549af57ed9fSAtsushi Murai             }
55024989c68SBrian Somers           }
55124989c68SBrian Somers           if (FD_ISSET(i, &wfds)) {
55224989c68SBrian Somers             log_Printf(LogTIMER, "Write set contains %d\n", i);
55324989c68SBrian Somers             FD_CLR(i, &wfds);
55424989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
55524989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
55624989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
557af57ed9fSAtsushi Murai               break;
558af57ed9fSAtsushi Murai             }
559e0d3e233SAndrey A. Chernov           }
56024989c68SBrian Somers           if (FD_ISSET(i, &efds)) {
56124989c68SBrian Somers             log_Printf(LogTIMER, "Error set contains %d\n", i);
56224989c68SBrian Somers             FD_CLR(i, &efds);
56324989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
56424989c68SBrian Somers             if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
56524989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
56658f264e1SBrian Somers               break;
56758f264e1SBrian Somers             }
56824989c68SBrian Somers           }
56924989c68SBrian Somers         }
57024989c68SBrian Somers       }
57158f264e1SBrian Somers       break;
572de451c68SBrian Somers     }
573de451c68SBrian Somers 
574f0cdd9c0SBrian Somers     log_Printf(LogTIMER, "Select returns %d\n", i);
575f0cdd9c0SBrian Somers 
57654cd8e13SBrian Somers     sig_Handle();
57754cd8e13SBrian Somers 
57854cd8e13SBrian Somers     if (i <= 0)
57954cd8e13SBrian Somers       continue;
58054cd8e13SBrian Somers 
5813006ec67SBrian Somers     for (i = 0; i <= nfds; i++)
5823006ec67SBrian Somers       if (FD_ISSET(i, &efds)) {
583991c2a7bSBrian Somers         log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
584991c2a7bSBrian Somers         /* We deal gracefully with link descriptor exceptions */
585991c2a7bSBrian Somers         if (!bundle_Exception(bundle, i)) {
586991c2a7bSBrian Somers           log_Printf(LogERROR, "Exception cannot be handled !\n");
5871ae349f5Scvs2svn           break;
5881ae349f5Scvs2svn         }
589991c2a7bSBrian Somers       }
590c60f92caSBrian Somers 
591b7c5748eSBrian Somers     if (i <= nfds)
592b7c5748eSBrian Somers       break;
593b7c5748eSBrian Somers 
5941af29a6eSBrian Somers     nothing_done = 1;
5951af29a6eSBrian Somers 
5961af29a6eSBrian Somers     if (descriptor_IsSet(&server.desc, &rfds)) {
597b77776a7SBrian Somers       descriptor_Read(&server.desc, bundle, &rfds);
5981af29a6eSBrian Somers       nothing_done = 0;
5991af29a6eSBrian Somers     }
6001af29a6eSBrian Somers 
6011af29a6eSBrian Somers     if (descriptor_IsSet(&bundle->desc, &rfds)) {
6021af29a6eSBrian Somers       descriptor_Read(&bundle->desc, bundle, &rfds);
6031af29a6eSBrian Somers       nothing_done = 0;
6041af29a6eSBrian Somers     }
60542d4d396SBrian Somers 
6062f786681SBrian Somers     if (descriptor_IsSet(&bundle->desc, &wfds))
6071af29a6eSBrian Somers       if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) {
6081af29a6eSBrian Somers         /*
6091af29a6eSBrian Somers          * This is disasterous.  The OS has told us that something is
6101af29a6eSBrian Somers          * writable, and all our write()s have failed.  Rather than
6111af29a6eSBrian Somers          * going back immediately to do our UpdateSet()s and select(),
6121af29a6eSBrian Somers          * we sleep for a bit to avoid gobbling up all cpu time.
6131af29a6eSBrian Somers          */
6141af29a6eSBrian Somers         struct timeval t;
6152f786681SBrian Somers 
6161af29a6eSBrian Somers         t.tv_sec = 0;
6171af29a6eSBrian Somers         t.tv_usec = 100000;
6181af29a6eSBrian Somers         select(0, NULL, NULL, NULL, &t);
6191af29a6eSBrian Somers       }
620565e35e5SBrian Somers   } while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle));
621565e35e5SBrian Somers 
622dd7e2610SBrian Somers   log_Printf(LogDEBUG, "DoLoop done.\n");
623af57ed9fSAtsushi Murai }
624