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> 306b457978SBrian Somers #include <sys/socket.h> 316b457978SBrian Somers #include <net/route.h> 3275240ed1SBrian Somers 3375240ed1SBrian Somers #include <errno.h> 3475240ed1SBrian Somers #include <fcntl.h> 3575240ed1SBrian Somers #include <paths.h> 3675240ed1SBrian Somers #include <signal.h> 3775240ed1SBrian Somers #include <stdio.h> 38c0593e34SBrian Somers #include <stdlib.h> 3975240ed1SBrian Somers #include <string.h> 4075240ed1SBrian Somers #include <sys/time.h> 4175240ed1SBrian Somers #include <termios.h> 4275240ed1SBrian Somers #include <unistd.h> 431080ea25SBrian Somers #include <sys/stat.h> 4475240ed1SBrian Somers 4567b072f7SBrian Somers #ifndef NONAT 4610e629b9SBrian Somers #ifdef LOCALNAT 477884358fSBrian Somers #include "alias.h" 4810e629b9SBrian Somers #else 4910e629b9SBrian Somers #include <alias.h> 501595bacdSBrian Somers #endif 511595bacdSBrian Somers #endif 5210e629b9SBrian Somers 535d9e6103SBrian Somers #include "layer.h" 541af29a6eSBrian Somers #include "probe.h" 5575240ed1SBrian Somers #include "mbuf.h" 5675240ed1SBrian Somers #include "log.h" 5775240ed1SBrian Somers #include "defs.h" 585106c671SBrian Somers #include "id.h" 5975240ed1SBrian Somers #include "timer.h" 6075240ed1SBrian Somers #include "fsm.h" 61879ed6faSBrian Somers #include "lqr.h" 62af57ed9fSAtsushi Murai #include "hdlc.h" 63af57ed9fSAtsushi Murai #include "lcp.h" 640053cc58SBrian Somers #include "ccp.h" 6529e275ceSBrian Somers #include "iplist.h" 6629e275ceSBrian Somers #include "throughput.h" 67eaa4df37SBrian Somers #include "slcompress.h" 68af57ed9fSAtsushi Murai #include "ipcp.h" 6984b8a6ebSAtsushi Murai #include "filter.h" 702f786681SBrian Somers #include "descriptor.h" 713b0f8d2eSBrian Somers #include "link.h" 723b0f8d2eSBrian Somers #include "mp.h" 73972a1bcfSBrian Somers #ifndef NORADIUS 74972a1bcfSBrian Somers #include "radius.h" 75972a1bcfSBrian Somers #endif 765828db6dSBrian Somers #include "bundle.h" 771ae349f5Scvs2svn #include "auth.h" 78ed6a16c1SPoul-Henning Kamp #include "systems.h" 79f5ff0f7cSBrian Somers #include "sig.h" 8075240ed1SBrian Somers #include "main.h" 8177ff88adSBrian Somers #include "server.h" 8285b542cfSBrian Somers #include "prompt.h" 83b6dec9f0SBrian Somers #include "chat.h" 84e2ebb036SBrian Somers #include "chap.h" 8592b09558SBrian Somers #include "cbcp.h" 863006ec67SBrian Somers #include "datalink.h" 878fa6ebe4SBrian Somers #include "iface.h" 8853c9f6c0SAtsushi Murai 8953c9f6c0SAtsushi Murai #ifndef O_NONBLOCK 9053c9f6c0SAtsushi Murai #ifdef O_NDELAY 9153c9f6c0SAtsushi Murai #define O_NONBLOCK O_NDELAY 9253c9f6c0SAtsushi Murai #endif 9353c9f6c0SAtsushi Murai #endif 94af57ed9fSAtsushi Murai 950f78c7a7SBrian Somers static void DoLoop(struct bundle *); 9675240ed1SBrian Somers static void TerminalStop(int); 97b6e82f33SBrian Somers static const char *ex_desc(int); 9875240ed1SBrian Somers 9983d1af55SBrian Somers static struct bundle *SignalBundle; 100b6217683SBrian Somers static struct prompt *SignalPrompt; 101c3899f8dSAtsushi Murai 102c3899f8dSAtsushi Murai void 103944f7098SBrian Somers Cleanup(int excode) 104af57ed9fSAtsushi Murai { 105a0cbd833SBrian Somers SignalBundle->CleaningUp = 1; 1069c81b87dSBrian Somers bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 1076e4959f0SBrian Somers } 108af57ed9fSAtsushi Murai 1091afedc4bSBrian Somers void 1101afedc4bSBrian Somers AbortProgram(int excode) 1111afedc4bSBrian Somers { 112dd7e2610SBrian Somers server_Close(SignalBundle); 113dd7e2610SBrian Somers log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode)); 1149c81b87dSBrian Somers bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 11568a0f0ccSBrian Somers bundle_Destroy(SignalBundle); 116dd7e2610SBrian Somers log_Close(); 117af57ed9fSAtsushi Murai exit(excode); 118af57ed9fSAtsushi Murai } 119af57ed9fSAtsushi Murai 120af57ed9fSAtsushi Murai static void 121944f7098SBrian Somers CloseConnection(int signo) 122af57ed9fSAtsushi Murai { 123368aee2bSBrian Somers /* NOTE, these are manual, we've done a setsid() */ 124dd7e2610SBrian Somers sig_signal(SIGINT, SIG_IGN); 125dd7e2610SBrian Somers log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo); 126899011c4SBrian Somers bundle_Down(SignalBundle, CLOSE_STAYDOWN); 127dd7e2610SBrian Somers sig_signal(SIGINT, CloseConnection); 1286d14e2a8SJordan K. Hubbard } 129af57ed9fSAtsushi Murai 130af57ed9fSAtsushi Murai static void 131944f7098SBrian Somers CloseSession(int signo) 132af57ed9fSAtsushi Murai { 133dd7e2610SBrian Somers log_Printf(LogPHASE, "Signal %d, terminate.\n", signo); 134af57ed9fSAtsushi Murai Cleanup(EX_TERM); 135af57ed9fSAtsushi Murai } 136c3899f8dSAtsushi Murai 13775b8d283SBrian Somers static pid_t BGPid = 0; 13875b8d283SBrian Somers 13975b8d283SBrian Somers static void 14075b8d283SBrian Somers KillChild(int signo) 14175b8d283SBrian Somers { 142ac37ab22SBrian Somers signal(signo, SIG_IGN); 143dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Signal %d\n", signo); 14475b8d283SBrian Somers kill(BGPid, SIGINT); 14575b8d283SBrian Somers } 14675b8d283SBrian Somers 147c3899f8dSAtsushi Murai static void 148b6e82f33SBrian Somers TerminalCont(int signo) 149c3899f8dSAtsushi Murai { 150f91ad6b0SBrian Somers signal(SIGCONT, SIG_DFL); 151f91ad6b0SBrian Somers prompt_Continue(SignalPrompt); 152c3899f8dSAtsushi Murai } 153c3899f8dSAtsushi Murai 154c3899f8dSAtsushi Murai static void 155944f7098SBrian Somers TerminalStop(int signo) 156c3899f8dSAtsushi Murai { 157f91ad6b0SBrian Somers prompt_Suspend(SignalPrompt); 158f91ad6b0SBrian Somers signal(SIGCONT, TerminalCont); 159f91ad6b0SBrian Somers raise(SIGSTOP); 1604ef16f24SBrian Somers } 1614ef16f24SBrian Somers 1628ea8442cSBrian Somers static void 1638ea8442cSBrian Somers BringDownServer(int signo) 1648ea8442cSBrian Somers { 165b6217683SBrian Somers /* Drops all child prompts too ! */ 166dd7e2610SBrian Somers server_Close(SignalBundle); 1678ea8442cSBrian Somers } 1688ea8442cSBrian Somers 169b6e82f33SBrian Somers static const char * 1706efd9292SBrian Somers ex_desc(int ex) 1716efd9292SBrian Somers { 172d93d3a9cSBrian Somers static char num[12]; /* Used immediately if returned */ 173182c898aSBrian Somers static const char * const desc[] = { 174b6e82f33SBrian Somers "normal", "start", "sock", "modem", "dial", "dead", "done", 175b6e82f33SBrian Somers "reboot", "errdead", "hangup", "term", "nodial", "nologin" 176b6e82f33SBrian Somers }; 1776efd9292SBrian Somers 17870ee81ffSBrian Somers if (ex >= 0 && ex < sizeof desc / sizeof *desc) 1796efd9292SBrian Somers return desc[ex]; 1806efd9292SBrian Somers snprintf(num, sizeof num, "%d", ex); 1816efd9292SBrian Somers return num; 1826efd9292SBrian Somers } 183c3899f8dSAtsushi Murai 18475240ed1SBrian Somers static void 185b6e82f33SBrian Somers Usage(void) 186af57ed9fSAtsushi Murai { 187680026d6SNate Williams fprintf(stderr, 18867b072f7SBrian Somers "Usage: ppp [-auto | -foreground | -background | -direct | -dedicated | -ddial | -interactive]" 189b6e82f33SBrian Somers #ifndef NOALIAS 19067b072f7SBrian Somers " [-nat]" 191b6e82f33SBrian Somers #endif 192c0593e34SBrian Somers " [-quiet] [-unit N] [system ...]\n"); 193af57ed9fSAtsushi Murai exit(EX_START); 194af57ed9fSAtsushi Murai } 195af57ed9fSAtsushi Murai 196c0593e34SBrian Somers struct switches { 197c0593e34SBrian Somers unsigned nat : 1; 198c0593e34SBrian Somers unsigned fg : 1; 199c0593e34SBrian Somers unsigned quiet : 1; 200c0593e34SBrian Somers int mode; 201c0593e34SBrian Somers int unit; 202c0593e34SBrian Somers }; 203c0593e34SBrian Somers 2047cf368ebSBrian Somers static int 205c0593e34SBrian Somers ProcessArgs(int argc, char **argv, struct switches *sw) 206af57ed9fSAtsushi Murai { 2077cf368ebSBrian Somers int optc, newmode, arg; 208af57ed9fSAtsushi Murai char *cp; 209af57ed9fSAtsushi Murai 2107cf368ebSBrian Somers optc = 0; 211c0593e34SBrian Somers memset(sw, '\0', sizeof *sw); 212c0593e34SBrian Somers sw->mode = PHYS_INTERACTIVE; 213c0593e34SBrian Somers sw->unit = -1; 214c0593e34SBrian Somers 2157cf368ebSBrian Somers for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) { 2167cf368ebSBrian Somers cp = argv[arg] + 1; 21781358fa3SBrian Somers newmode = Nam2mode(cp); 21881358fa3SBrian Somers switch (newmode) { 21981358fa3SBrian Somers case PHYS_NONE: 220c0593e34SBrian Somers if (strcmp(cp, "nat") == 0) { 22167b072f7SBrian Somers #ifdef NONAT 222c0593e34SBrian Somers log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]); 223615ad4f9SBrian Somers #else 224c0593e34SBrian Somers sw->nat = 1; 225615ad4f9SBrian Somers #endif 2261ae349f5Scvs2svn optc--; /* this option isn't exclusive */ 227c0593e34SBrian Somers } else if (strcmp(cp, "alias") == 0) { 228c0593e34SBrian Somers #ifdef NONAT 229c0593e34SBrian Somers log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]); 230c0593e34SBrian Somers fprintf(stderr, "%s ignored: NAT is compiled out\n", argv[arg]); 231c0593e34SBrian Somers #else 232886530abSBrian Somers log_Printf(LogWARN, "%s is deprecated\n", argv[arg]); 233886530abSBrian Somers fprintf(stderr, "%s is deprecated\n", argv[arg]); 234c0593e34SBrian Somers sw->nat = 1; 235c0593e34SBrian Somers #endif 236c0593e34SBrian Somers optc--; /* this option isn't exclusive */ 237c0593e34SBrian Somers } else if (strncmp(cp, "unit", 4) == 0) { 2389d06928dSBrian Somers optc--; /* this option isn't exclusive */ 239c0593e34SBrian Somers if (cp[4] == '\0') { 2409d06928dSBrian Somers optc--; /* nor is the argument */ 241c0593e34SBrian Somers if (++arg == argc) { 242c0593e34SBrian Somers fprintf(stderr, "-unit: Expected unit number\n"); 243c0593e34SBrian Somers Usage(); 244c0593e34SBrian Somers } else 245c0593e34SBrian Somers sw->unit = atoi(argv[arg]); 246c0593e34SBrian Somers } else 247c0593e34SBrian Somers sw->unit = atoi(cp + 4); 24867b072f7SBrian Somers } else if (strcmp(cp, "quiet") == 0) { 249c0593e34SBrian Somers sw->quiet = 1; 25067b072f7SBrian Somers optc--; /* this option isn't exclusive */ 251944f7098SBrian Somers } else 252af57ed9fSAtsushi Murai Usage(); 25381358fa3SBrian Somers break; 25481358fa3SBrian Somers 25581358fa3SBrian Somers case PHYS_ALL: 25681358fa3SBrian Somers Usage(); 25781358fa3SBrian Somers break; 25881358fa3SBrian Somers 25981358fa3SBrian Somers default: 260c0593e34SBrian Somers sw->mode = newmode; 261f6a4e748SBrian Somers if (newmode == PHYS_FOREGROUND) 262f6a4e748SBrian Somers sw->fg = 1; 26381358fa3SBrian Somers } 264af57ed9fSAtsushi Murai } 265af57ed9fSAtsushi Murai 266af57ed9fSAtsushi Murai if (optc > 1) { 26785602e52SBrian Somers fprintf(stderr, "You may specify only one mode.\n"); 2681ae349f5Scvs2svn exit(EX_START); 2691ae349f5Scvs2svn } 2701ae349f5Scvs2svn 271c0593e34SBrian Somers if (sw->mode == PHYS_AUTO && arg == argc) { 2727cf368ebSBrian Somers fprintf(stderr, "A system must be specified in auto mode.\n"); 273af57ed9fSAtsushi Murai exit(EX_START); 274af57ed9fSAtsushi Murai } 27539f94eddSBrian Somers 2767cf368ebSBrian Somers return arg; /* Don't SetLabel yet ! */ 277af57ed9fSAtsushi Murai } 278af57ed9fSAtsushi Murai 2797cf368ebSBrian Somers static void 2807cf368ebSBrian Somers CheckLabel(const char *label, struct prompt *prompt, int mode) 2817cf368ebSBrian Somers { 2827cf368ebSBrian Somers const char *err; 2837cf368ebSBrian Somers 2847cf368ebSBrian Somers if ((err = system_IsValid(label, prompt, mode)) != NULL) { 2857cf368ebSBrian Somers fprintf(stderr, "%s: %s\n", label, err); 2867cf368ebSBrian Somers if (mode == PHYS_DIRECT) 2877cf368ebSBrian Somers log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n", 2887cf368ebSBrian Somers label, err); 2897cf368ebSBrian Somers log_Close(); 2907cf368ebSBrian Somers exit(1); 2917cf368ebSBrian Somers } 2927cf368ebSBrian Somers } 2937cf368ebSBrian Somers 2947cf368ebSBrian Somers 2954ef16f24SBrian Somers int 296944f7098SBrian Somers main(int argc, char **argv) 297af57ed9fSAtsushi Murai { 2987cf368ebSBrian Somers char *name; 299756783fcSBrian Somers const char *lastlabel; 300c0593e34SBrian Somers int nfds, label, arg; 3017a6f8720SBrian Somers struct bundle *bundle; 302b6217683SBrian Somers struct prompt *prompt; 303c0593e34SBrian Somers struct switches sw; 304e3b4c400SBrian Somers 305e3b4c400SBrian Somers nfds = getdtablesize(); 306e3b4c400SBrian Somers if (nfds >= FD_SETSIZE) 307e3b4c400SBrian Somers /* 308e3b4c400SBrian Somers * If we've got loads of file descriptors, make sure they're all 309e3b4c400SBrian Somers * closed. If they aren't, we may end up with a seg fault when our 310e3b4c400SBrian Somers * `fd_set's get too big when select()ing ! 311e3b4c400SBrian Somers */ 312e3b4c400SBrian Somers while (--nfds > 2) 313e3b4c400SBrian Somers close(nfds); 314af57ed9fSAtsushi Murai 31575240ed1SBrian Somers name = strrchr(argv[0], '/'); 316dd7e2610SBrian Somers log_Open(name ? name + 1 : argv[0]); 3172a279fedSBrian Somers 31867b072f7SBrian Somers #ifndef NONAT 3192a630835SBrian Somers PacketAliasInit(); 3202a630835SBrian Somers #endif 321c0593e34SBrian Somers label = ProcessArgs(argc, argv, &sw); 32285b542cfSBrian Somers 32385b542cfSBrian Somers /* 324a611383fSBrian Somers * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops 325a611383fSBrian Somers * output occasionally.... I must find the real reason some time. To 326a611383fSBrian Somers * display the dodgy behaviour, comment out this bit, make yourself a large 32785b542cfSBrian Somers * routing table and then run ppp in interactive mode. The `show route' 32885b542cfSBrian Somers * command will drop chunks of data !!! 32985b542cfSBrian Somers */ 330c0593e34SBrian Somers if (sw.mode == PHYS_INTERACTIVE) { 33185b542cfSBrian Somers close(STDIN_FILENO); 33285b542cfSBrian Somers if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) { 33385b542cfSBrian Somers fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY); 33485b542cfSBrian Somers return 2; 33585b542cfSBrian Somers } 33685b542cfSBrian Somers } 33785b542cfSBrian Somers 338b6217683SBrian Somers /* Allow output for the moment (except in direct mode) */ 339c0593e34SBrian Somers if (sw.mode == PHYS_DIRECT) 340b6217683SBrian Somers prompt = NULL; 341ed0e9269SBrian Somers else 342b6217683SBrian Somers SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD); 34312ef29a8SBrian Somers 3445106c671SBrian Somers ID0init(); 3454562be74SBrian Somers if (ID0realuid() != 0) { 3464562be74SBrian Somers char conf[200], *ptr; 3474562be74SBrian Somers 3484562be74SBrian Somers snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE); 3494562be74SBrian Somers do { 3501080ea25SBrian Somers struct stat sb; 3511080ea25SBrian Somers 3521080ea25SBrian Somers if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) { 353a33b2ef7SBrian Somers log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n", 354a33b2ef7SBrian Somers conf); 3554562be74SBrian Somers return -1; 3564562be74SBrian Somers } 3574562be74SBrian Somers ptr = conf + strlen(conf)-2; 3584562be74SBrian Somers while (ptr > conf && *ptr != '/') 3594562be74SBrian Somers *ptr-- = '\0'; 3604562be74SBrian Somers } while (ptr >= conf); 3614562be74SBrian Somers } 3624562be74SBrian Somers 3637cf368ebSBrian Somers if (label < argc) 3647cf368ebSBrian Somers for (arg = label; arg < argc; arg++) 365c0593e34SBrian Somers CheckLabel(argv[arg], prompt, sw.mode); 3667cf368ebSBrian Somers else 367c0593e34SBrian Somers CheckLabel("default", prompt, sw.mode); 36812ef29a8SBrian Somers 369c0593e34SBrian Somers if (!sw.quiet) 370c0593e34SBrian Somers prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode)); 371ed0e9269SBrian Somers 372cf0a3940SBrian Somers if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit)) == NULL) 3734ef16f24SBrian Somers return EX_START; 374756783fcSBrian Somers 375756783fcSBrian Somers /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */ 376756783fcSBrian Somers 3770f2f3eb3SBrian Somers if (prompt) { 3780f2f3eb3SBrian Somers prompt->bundle = bundle; /* couldn't do it earlier */ 379c0593e34SBrian Somers if (!sw.quiet) 3808fa6ebe4SBrian Somers prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name); 3810f2f3eb3SBrian Somers } 38283d1af55SBrian Somers SignalBundle = bundle; 383c0593e34SBrian Somers bundle->NatEnabled = sw.nat; 384c0593e34SBrian Somers if (sw.nat) 3858fa6ebe4SBrian Somers bundle->cfg.opt |= OPT_IFACEALIAS; 38612ef29a8SBrian Somers 38730291ffbSBrian Somers if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0) 388565e35e5SBrian Somers prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); 3891ae349f5Scvs2svn 390dd7e2610SBrian Somers sig_signal(SIGHUP, CloseSession); 391dd7e2610SBrian Somers sig_signal(SIGTERM, CloseSession); 392dd7e2610SBrian Somers sig_signal(SIGINT, CloseConnection); 393dd7e2610SBrian Somers sig_signal(SIGQUIT, CloseSession); 394dd7e2610SBrian Somers sig_signal(SIGALRM, SIG_IGN); 395e0d3e233SAndrey A. Chernov signal(SIGPIPE, SIG_IGN); 396565e35e5SBrian Somers 397c0593e34SBrian Somers if (sw.mode == PHYS_INTERACTIVE) 398dd7e2610SBrian Somers sig_signal(SIGTSTP, TerminalStop); 399f91ad6b0SBrian Somers 400dd7e2610SBrian Somers sig_signal(SIGUSR2, BringDownServer); 401af57ed9fSAtsushi Murai 402cf0a3940SBrian Somers lastlabel = argv[argc - 1]; 4037cf368ebSBrian Somers for (arg = label; arg < argc; arg++) { 4047cf368ebSBrian Somers /* In case we use LABEL or ``set enddisc label'' */ 405756783fcSBrian Somers bundle_SetLabel(bundle, lastlabel); 406cf0a3940SBrian Somers system_Select(bundle, argv[arg], CONFFILE, prompt, NULL); 4071ae349f5Scvs2svn } 4087cf368ebSBrian Somers 4097cf368ebSBrian Somers if (label < argc) 4107cf368ebSBrian Somers /* In case the last label did a ``load'' */ 411756783fcSBrian Somers bundle_SetLabel(bundle, lastlabel); 4127cf368ebSBrian Somers 413c0593e34SBrian Somers if (sw.mode == PHYS_AUTO && 4145828db6dSBrian Somers bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) { 4157cf368ebSBrian Somers prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address " 4167cf368ebSBrian Somers "in auto mode.\n"); 4178390b576SBrian Somers AbortProgram(EX_START); 418af57ed9fSAtsushi Murai } 4196efd9292SBrian Somers 420c0593e34SBrian Somers if (sw.mode != PHYS_INTERACTIVE) { 421c0593e34SBrian Somers if (sw.mode != PHYS_DIRECT) { 422c0593e34SBrian Somers if (!sw.fg) { 4235cf4388bSBrian Somers int bgpipe[2]; 4246d14e2a8SJordan K. Hubbard pid_t bgpid; 425a9c6b5dfSAtsushi Murai 426c0593e34SBrian Somers if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) { 427dd7e2610SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 4288390b576SBrian Somers AbortProgram(EX_SOCK); 4291ae349f5Scvs2svn } 4301ae349f5Scvs2svn 4316d14e2a8SJordan K. Hubbard bgpid = fork(); 4326d14e2a8SJordan K. Hubbard if (bgpid == -1) { 433dd7e2610SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 4348390b576SBrian Somers AbortProgram(EX_SOCK); 4356d14e2a8SJordan K. Hubbard } 4365cf4388bSBrian Somers 4376d14e2a8SJordan K. Hubbard if (bgpid) { 4386d14e2a8SJordan K. Hubbard char c = EX_NORMAL; 439a9c6b5dfSAtsushi Murai 440c0593e34SBrian Somers if (sw.mode == PHYS_BACKGROUND) { 4415cf4388bSBrian Somers close(bgpipe[1]); 4426d14e2a8SJordan K. Hubbard BGPid = bgpid; 44375b8d283SBrian Somers /* If we get a signal, kill the child */ 44475b8d283SBrian Somers signal(SIGHUP, KillChild); 44575b8d283SBrian Somers signal(SIGTERM, KillChild); 44675b8d283SBrian Somers signal(SIGINT, KillChild); 44775b8d283SBrian Somers signal(SIGQUIT, KillChild); 44875b8d283SBrian Somers 44975b8d283SBrian Somers /* Wait for our child to close its pipe before we exit */ 4505cf4388bSBrian Somers if (read(bgpipe[0], &c, 1) != 1) { 451b6217683SBrian Somers prompt_Printf(prompt, "Child exit, no status.\n"); 452dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Child exit, no status.\n"); 4536efd9292SBrian Somers } else if (c == EX_NORMAL) { 454b6217683SBrian Somers prompt_Printf(prompt, "PPP enabled.\n"); 455dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: PPP enabled.\n"); 4566efd9292SBrian Somers } else { 457b6217683SBrian Somers prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c)); 458dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Child failed (%s).\n", 45980e37c72SBrian Somers ex_desc((int) c)); 4606efd9292SBrian Somers } 4615cf4388bSBrian Somers close(bgpipe[0]); 4626d14e2a8SJordan K. Hubbard } 4634ef16f24SBrian Somers return c; 464c0593e34SBrian Somers } else if (sw.mode == PHYS_BACKGROUND) { 4655cf4388bSBrian Somers close(bgpipe[0]); 4665cf4388bSBrian Somers bundle->notify.fd = bgpipe[1]; 467aefd026aSBrian Somers } 468aefd026aSBrian Somers 46906b47306SBrian Somers bundle_ChangedPID(bundle); 470da66dd13SBrian Somers bundle_LockTun(bundle); /* we have a new pid */ 47167b072f7SBrian Somers } 472da66dd13SBrian Somers 47367b072f7SBrian Somers /* -auto, -dedicated, -ddial, -foreground & -background */ 474b6217683SBrian Somers prompt_Destroy(prompt, 0); 4752a279fedSBrian Somers close(STDOUT_FILENO); 4762a279fedSBrian Somers close(STDERR_FILENO); 4772a279fedSBrian Somers close(STDIN_FILENO); 478c0593e34SBrian Somers if (!sw.fg) 479b6217683SBrian Somers setsid(); 480565e35e5SBrian Somers } else { 48167b072f7SBrian Somers /* -direct - STDIN_FILENO gets used by physical_Open */ 482565e35e5SBrian Somers prompt_TtyInit(NULL); 483565e35e5SBrian Somers close(STDOUT_FILENO); 484565e35e5SBrian Somers close(STDERR_FILENO); 485d656a4c5SBrian Somers } 486af57ed9fSAtsushi Murai } else { 48767b072f7SBrian Somers /* -interactive */ 4882a279fedSBrian Somers close(STDERR_FILENO); 489565e35e5SBrian Somers prompt_TtyInit(prompt); 490b6217683SBrian Somers prompt_TtyCommandMode(prompt); 491b6217683SBrian Somers prompt_Required(prompt); 492af57ed9fSAtsushi Murai } 49335495becSBrian Somers 494c0593e34SBrian Somers log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode)); 4950f78c7a7SBrian Somers DoLoop(bundle); 4961afedc4bSBrian Somers AbortProgram(EX_NORMAL); 497af57ed9fSAtsushi Murai 4981afedc4bSBrian Somers return EX_NORMAL; 499af57ed9fSAtsushi Murai } 500af57ed9fSAtsushi Murai 501af57ed9fSAtsushi Murai static void 5020f78c7a7SBrian Somers DoLoop(struct bundle *bundle) 503af57ed9fSAtsushi Murai { 504af57ed9fSAtsushi Murai fd_set rfds, wfds, efds; 5051af29a6eSBrian Somers int i, nfds, nothing_done; 5061af29a6eSBrian Somers struct probe probe; 5071af29a6eSBrian Somers 5081af29a6eSBrian Somers probe_Init(&probe); 509c3899f8dSAtsushi Murai 510da8b7034SBrian Somers for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) { 511780700e5SAndrey A. Chernov nfds = 0; 512944f7098SBrian Somers FD_ZERO(&rfds); 513944f7098SBrian Somers FD_ZERO(&wfds); 514944f7098SBrian Somers FD_ZERO(&efds); 51584b8a6ebSAtsushi Murai 5160f2f3eb3SBrian Somers /* All our datalinks, the tun device and the MP socket */ 5176f384573SBrian Somers descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds); 5180f2f3eb3SBrian Somers 5190f2f3eb3SBrian Somers /* All our prompts and the diagnostic socket */ 5200f2f3eb3SBrian Somers descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds); 52107030d97SBrian Somers 5220cd8e902SBrian Somers bundle_CleanDatalinks(bundle); 5233b0f8d2eSBrian Somers if (bundle_IsDead(bundle)) 524f4768038SBrian Somers /* Don't select - we'll be here forever */ 525f4768038SBrian Somers break; 5260706ff38SBrian Somers 527486105bcSBrian Somers /* 528486105bcSBrian Somers * It's possible that we've had a signal since we last checked. If 529486105bcSBrian Somers * we don't check again before calling select(), we may end up stuck 530486105bcSBrian Somers * after having missed the event.... sig_Handle() tries to be as 531486105bcSBrian Somers * quick as possible if nothing is likely to have happened. 532486105bcSBrian Somers * This is only really likely if we block in open(... O_NONBLOCK) 533486105bcSBrian Somers * which will happen with a misconfigured device. 534486105bcSBrian Somers */ 535486105bcSBrian Somers if (sig_Handle()) 536486105bcSBrian Somers continue; 537486105bcSBrian Somers 5383006ec67SBrian Somers i = select(nfds, &rfds, &wfds, &efds, NULL); 539712ae387SBrian Somers 54054cd8e13SBrian Somers if (i < 0 && errno != EINTR) { 541dd7e2610SBrian Somers log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno)); 54224989c68SBrian Somers if (log_IsKept(LogTIMER)) { 54324989c68SBrian Somers struct timeval t; 54424989c68SBrian Somers 54524989c68SBrian Somers for (i = 0; i <= nfds; i++) { 54624989c68SBrian Somers if (FD_ISSET(i, &rfds)) { 54724989c68SBrian Somers log_Printf(LogTIMER, "Read set contains %d\n", i); 54824989c68SBrian Somers FD_CLR(i, &rfds); 54924989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 55024989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 55124989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 552af57ed9fSAtsushi Murai break; 553af57ed9fSAtsushi Murai } 55424989c68SBrian Somers } 55524989c68SBrian Somers if (FD_ISSET(i, &wfds)) { 55624989c68SBrian Somers log_Printf(LogTIMER, "Write set contains %d\n", i); 55724989c68SBrian Somers FD_CLR(i, &wfds); 55824989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 55924989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 56024989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 561af57ed9fSAtsushi Murai break; 562af57ed9fSAtsushi Murai } 563e0d3e233SAndrey A. Chernov } 56424989c68SBrian Somers if (FD_ISSET(i, &efds)) { 56524989c68SBrian Somers log_Printf(LogTIMER, "Error set contains %d\n", i); 56624989c68SBrian Somers FD_CLR(i, &efds); 56724989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 56824989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 56924989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 57058f264e1SBrian Somers break; 57158f264e1SBrian Somers } 57224989c68SBrian Somers } 57324989c68SBrian Somers } 57424989c68SBrian Somers } 57558f264e1SBrian Somers break; 576de451c68SBrian Somers } 577de451c68SBrian Somers 578f0cdd9c0SBrian Somers log_Printf(LogTIMER, "Select returns %d\n", i); 579f0cdd9c0SBrian Somers 58054cd8e13SBrian Somers sig_Handle(); 58154cd8e13SBrian Somers 58254cd8e13SBrian Somers if (i <= 0) 58354cd8e13SBrian Somers continue; 58454cd8e13SBrian Somers 5853006ec67SBrian Somers for (i = 0; i <= nfds; i++) 5863006ec67SBrian Somers if (FD_ISSET(i, &efds)) { 5878e7bd08eSBrian Somers log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i); 5888e7bd08eSBrian Somers /* We deal gracefully with link descriptor exceptions */ 589991c2a7bSBrian Somers if (!bundle_Exception(bundle, i)) { 590991c2a7bSBrian Somers log_Printf(LogERROR, "Exception cannot be handled !\n"); 5911ae349f5Scvs2svn break; 5921ae349f5Scvs2svn } 593991c2a7bSBrian Somers } 594c60f92caSBrian Somers 595b7c5748eSBrian Somers if (i <= nfds) 596b7c5748eSBrian Somers break; 597b7c5748eSBrian Somers 5981af29a6eSBrian Somers nothing_done = 1; 5991af29a6eSBrian Somers 6001af29a6eSBrian Somers if (descriptor_IsSet(&server.desc, &rfds)) { 601b77776a7SBrian Somers descriptor_Read(&server.desc, bundle, &rfds); 6021af29a6eSBrian Somers nothing_done = 0; 6031af29a6eSBrian Somers } 6041af29a6eSBrian Somers 6051af29a6eSBrian Somers if (descriptor_IsSet(&bundle->desc, &rfds)) { 6061af29a6eSBrian Somers descriptor_Read(&bundle->desc, bundle, &rfds); 6071af29a6eSBrian Somers nothing_done = 0; 6081af29a6eSBrian Somers } 60942d4d396SBrian Somers 6102f786681SBrian Somers if (descriptor_IsSet(&bundle->desc, &wfds)) 6111af29a6eSBrian Somers if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) { 6121af29a6eSBrian Somers /* 6131af29a6eSBrian Somers * This is disasterous. The OS has told us that something is 6141af29a6eSBrian Somers * writable, and all our write()s have failed. Rather than 6151af29a6eSBrian Somers * going back immediately to do our UpdateSet()s and select(), 6161af29a6eSBrian Somers * we sleep for a bit to avoid gobbling up all cpu time. 6171af29a6eSBrian Somers */ 6181af29a6eSBrian Somers struct timeval t; 6192f786681SBrian Somers 6201af29a6eSBrian Somers t.tv_sec = 0; 6211af29a6eSBrian Somers t.tv_usec = 100000; 6221af29a6eSBrian Somers select(0, NULL, NULL, NULL, &t); 6231af29a6eSBrian Somers } 624da8b7034SBrian Somers } 625565e35e5SBrian Somers 626dd7e2610SBrian Somers log_Printf(LogDEBUG, "DoLoop done.\n"); 627af57ed9fSAtsushi Murai } 628