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 * 207884358fSBrian Somers * $Id: main.c,v 1.152 1999/03/30 00:44:57 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 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" 67972a1bcfSBrian Somers #ifndef NORADIUS 68972a1bcfSBrian Somers #include "radius.h" 69972a1bcfSBrian Somers #endif 705828db6dSBrian Somers #include "bundle.h" 711ae349f5Scvs2svn #include "auth.h" 72ed6a16c1SPoul-Henning Kamp #include "systems.h" 73f5ff0f7cSBrian Somers #include "sig.h" 7475240ed1SBrian Somers #include "main.h" 7577ff88adSBrian Somers #include "server.h" 7685b542cfSBrian Somers #include "prompt.h" 77b6dec9f0SBrian Somers #include "chat.h" 78e2ebb036SBrian Somers #include "chap.h" 7992b09558SBrian Somers #include "cbcp.h" 803006ec67SBrian Somers #include "datalink.h" 818fa6ebe4SBrian Somers #include "iface.h" 8253c9f6c0SAtsushi Murai 8353c9f6c0SAtsushi Murai #ifndef O_NONBLOCK 8453c9f6c0SAtsushi Murai #ifdef O_NDELAY 8553c9f6c0SAtsushi Murai #define O_NONBLOCK O_NDELAY 8653c9f6c0SAtsushi Murai #endif 8753c9f6c0SAtsushi Murai #endif 88af57ed9fSAtsushi Murai 890f78c7a7SBrian Somers static void DoLoop(struct bundle *); 9075240ed1SBrian Somers static void TerminalStop(int); 91b6e82f33SBrian Somers static const char *ex_desc(int); 9275240ed1SBrian Somers 9383d1af55SBrian Somers static struct bundle *SignalBundle; 94b6217683SBrian Somers static struct prompt *SignalPrompt; 95c3899f8dSAtsushi Murai 96c3899f8dSAtsushi Murai void 97944f7098SBrian Somers Cleanup(int excode) 98af57ed9fSAtsushi Murai { 99a0cbd833SBrian Somers SignalBundle->CleaningUp = 1; 1009c81b87dSBrian Somers bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 1016e4959f0SBrian Somers } 102af57ed9fSAtsushi Murai 1031afedc4bSBrian Somers void 1041afedc4bSBrian Somers AbortProgram(int excode) 1051afedc4bSBrian Somers { 106dd7e2610SBrian Somers server_Close(SignalBundle); 107dd7e2610SBrian Somers log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode)); 1089c81b87dSBrian Somers bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 10968a0f0ccSBrian Somers bundle_Destroy(SignalBundle); 110dd7e2610SBrian Somers log_Close(); 111af57ed9fSAtsushi Murai exit(excode); 112af57ed9fSAtsushi Murai } 113af57ed9fSAtsushi Murai 114af57ed9fSAtsushi Murai static void 115944f7098SBrian Somers CloseConnection(int signo) 116af57ed9fSAtsushi Murai { 117368aee2bSBrian Somers /* NOTE, these are manual, we've done a setsid() */ 118dd7e2610SBrian Somers sig_signal(SIGINT, SIG_IGN); 119dd7e2610SBrian Somers log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo); 120899011c4SBrian Somers bundle_Down(SignalBundle, CLOSE_STAYDOWN); 121dd7e2610SBrian Somers sig_signal(SIGINT, CloseConnection); 1226d14e2a8SJordan K. Hubbard } 123af57ed9fSAtsushi Murai 124af57ed9fSAtsushi Murai static void 125944f7098SBrian Somers CloseSession(int signo) 126af57ed9fSAtsushi Murai { 127dd7e2610SBrian Somers log_Printf(LogPHASE, "Signal %d, terminate.\n", signo); 128af57ed9fSAtsushi Murai Cleanup(EX_TERM); 129af57ed9fSAtsushi Murai } 130c3899f8dSAtsushi Murai 13175b8d283SBrian Somers static pid_t BGPid = 0; 13275b8d283SBrian Somers 13375b8d283SBrian Somers static void 13475b8d283SBrian Somers KillChild(int signo) 13575b8d283SBrian Somers { 136dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Signal %d\n", signo); 13775b8d283SBrian Somers kill(BGPid, SIGINT); 13875b8d283SBrian Somers } 13975b8d283SBrian Somers 140c3899f8dSAtsushi Murai static void 141b6e82f33SBrian Somers TerminalCont(int signo) 142c3899f8dSAtsushi Murai { 143f91ad6b0SBrian Somers signal(SIGCONT, SIG_DFL); 144f91ad6b0SBrian Somers prompt_Continue(SignalPrompt); 145c3899f8dSAtsushi Murai } 146c3899f8dSAtsushi Murai 147c3899f8dSAtsushi Murai static void 148944f7098SBrian Somers TerminalStop(int signo) 149c3899f8dSAtsushi Murai { 150f91ad6b0SBrian Somers prompt_Suspend(SignalPrompt); 151f91ad6b0SBrian Somers signal(SIGCONT, TerminalCont); 152f91ad6b0SBrian Somers raise(SIGSTOP); 1534ef16f24SBrian Somers } 1544ef16f24SBrian Somers 1558ea8442cSBrian Somers static void 1568ea8442cSBrian Somers BringDownServer(int signo) 1578ea8442cSBrian Somers { 158b6217683SBrian Somers /* Drops all child prompts too ! */ 159dd7e2610SBrian Somers server_Close(SignalBundle); 1608ea8442cSBrian Somers } 1618ea8442cSBrian Somers 162b6e82f33SBrian Somers static const char * 1636efd9292SBrian Somers ex_desc(int ex) 1646efd9292SBrian Somers { 165d93d3a9cSBrian Somers static char num[12]; /* Used immediately if returned */ 166b6e82f33SBrian Somers static const char *desc[] = { 167b6e82f33SBrian Somers "normal", "start", "sock", "modem", "dial", "dead", "done", 168b6e82f33SBrian Somers "reboot", "errdead", "hangup", "term", "nodial", "nologin" 169b6e82f33SBrian Somers }; 1706efd9292SBrian Somers 17170ee81ffSBrian Somers if (ex >= 0 && ex < sizeof desc / sizeof *desc) 1726efd9292SBrian Somers return desc[ex]; 1736efd9292SBrian Somers snprintf(num, sizeof num, "%d", ex); 1746efd9292SBrian Somers return num; 1756efd9292SBrian Somers } 176c3899f8dSAtsushi Murai 17775240ed1SBrian Somers static void 178b6e82f33SBrian Somers Usage(void) 179af57ed9fSAtsushi Murai { 180680026d6SNate Williams fprintf(stderr, 181b6e82f33SBrian Somers "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]" 182b6e82f33SBrian Somers #ifndef NOALIAS 183b6e82f33SBrian Somers " [ -alias ]" 184b6e82f33SBrian Somers #endif 1857cf368ebSBrian Somers " [system ...]\n"); 186af57ed9fSAtsushi Murai exit(EX_START); 187af57ed9fSAtsushi Murai } 188af57ed9fSAtsushi Murai 1897cf368ebSBrian Somers static int 190615ad4f9SBrian Somers ProcessArgs(int argc, char **argv, int *mode, int *alias) 191af57ed9fSAtsushi Murai { 1927cf368ebSBrian Somers int optc, newmode, arg; 193af57ed9fSAtsushi Murai char *cp; 194af57ed9fSAtsushi Murai 1957cf368ebSBrian Somers optc = 0; 19681358fa3SBrian Somers *mode = PHYS_INTERACTIVE; 197615ad4f9SBrian Somers *alias = 0; 1987cf368ebSBrian Somers for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) { 1997cf368ebSBrian Somers cp = argv[arg] + 1; 20081358fa3SBrian Somers newmode = Nam2mode(cp); 20181358fa3SBrian Somers switch (newmode) { 20281358fa3SBrian Somers case PHYS_NONE: 20381358fa3SBrian Somers if (strcmp(cp, "alias") == 0) { 204615ad4f9SBrian Somers #ifdef NOALIAS 2057cf368ebSBrian Somers log_Printf(LogWARN, "Cannot load alias library (compiled out)\n"); 206615ad4f9SBrian Somers #else 207615ad4f9SBrian Somers *alias = 1; 208615ad4f9SBrian Somers #endif 2091ae349f5Scvs2svn optc--; /* this option isn't exclusive */ 210944f7098SBrian Somers } else 211af57ed9fSAtsushi Murai Usage(); 21281358fa3SBrian Somers break; 21381358fa3SBrian Somers 21481358fa3SBrian Somers case PHYS_ALL: 21581358fa3SBrian Somers Usage(); 21681358fa3SBrian Somers break; 21781358fa3SBrian Somers 21881358fa3SBrian Somers default: 21981358fa3SBrian Somers *mode = newmode; 22081358fa3SBrian Somers } 221af57ed9fSAtsushi Murai } 222af57ed9fSAtsushi Murai 223af57ed9fSAtsushi Murai if (optc > 1) { 22485602e52SBrian Somers fprintf(stderr, "You may specify only one mode.\n"); 2251ae349f5Scvs2svn exit(EX_START); 2261ae349f5Scvs2svn } 2271ae349f5Scvs2svn 2287cf368ebSBrian Somers if (*mode == PHYS_AUTO && arg == argc) { 2297cf368ebSBrian Somers fprintf(stderr, "A system must be specified in auto mode.\n"); 230af57ed9fSAtsushi Murai exit(EX_START); 231af57ed9fSAtsushi Murai } 23239f94eddSBrian Somers 2337cf368ebSBrian Somers return arg; /* Don't SetLabel yet ! */ 234af57ed9fSAtsushi Murai } 235af57ed9fSAtsushi Murai 2367cf368ebSBrian Somers static void 2377cf368ebSBrian Somers CheckLabel(const char *label, struct prompt *prompt, int mode) 2387cf368ebSBrian Somers { 2397cf368ebSBrian Somers const char *err; 2407cf368ebSBrian Somers 2417cf368ebSBrian Somers if ((err = system_IsValid(label, prompt, mode)) != NULL) { 2427cf368ebSBrian Somers fprintf(stderr, "%s: %s\n", label, err); 2437cf368ebSBrian Somers if (mode == PHYS_DIRECT) 2447cf368ebSBrian Somers log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n", 2457cf368ebSBrian Somers label, err); 2467cf368ebSBrian Somers log_Close(); 2477cf368ebSBrian Somers exit(1); 2487cf368ebSBrian Somers } 2497cf368ebSBrian Somers } 2507cf368ebSBrian Somers 2517cf368ebSBrian Somers 2524ef16f24SBrian Somers int 253944f7098SBrian Somers main(int argc, char **argv) 254af57ed9fSAtsushi Murai { 2557cf368ebSBrian Somers char *name; 256756783fcSBrian Somers const char *lastlabel; 2577cf368ebSBrian Somers int nfds, mode, alias, label, arg; 2587a6f8720SBrian Somers struct bundle *bundle; 259b6217683SBrian Somers struct prompt *prompt; 260e3b4c400SBrian Somers 261e3b4c400SBrian Somers nfds = getdtablesize(); 262e3b4c400SBrian Somers if (nfds >= FD_SETSIZE) 263e3b4c400SBrian Somers /* 264e3b4c400SBrian Somers * If we've got loads of file descriptors, make sure they're all 265e3b4c400SBrian Somers * closed. If they aren't, we may end up with a seg fault when our 266e3b4c400SBrian Somers * `fd_set's get too big when select()ing ! 267e3b4c400SBrian Somers */ 268e3b4c400SBrian Somers while (--nfds > 2) 269e3b4c400SBrian Somers close(nfds); 270af57ed9fSAtsushi Murai 27175240ed1SBrian Somers name = strrchr(argv[0], '/'); 272dd7e2610SBrian Somers log_Open(name ? name + 1 : argv[0]); 2732a279fedSBrian Somers 2742a630835SBrian Somers #ifndef NOALIAS 2752a630835SBrian Somers PacketAliasInit(); 2762a630835SBrian Somers #endif 2777cf368ebSBrian Somers label = ProcessArgs(argc, argv, &mode, &alias); 27885b542cfSBrian Somers 27985b542cfSBrian Somers /* 280a611383fSBrian Somers * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops 281a611383fSBrian Somers * output occasionally.... I must find the real reason some time. To 282a611383fSBrian Somers * display the dodgy behaviour, comment out this bit, make yourself a large 28385b542cfSBrian Somers * routing table and then run ppp in interactive mode. The `show route' 28485b542cfSBrian Somers * command will drop chunks of data !!! 28585b542cfSBrian Somers */ 28681358fa3SBrian Somers if (mode == PHYS_INTERACTIVE) { 28785b542cfSBrian Somers close(STDIN_FILENO); 28885b542cfSBrian Somers if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) { 28985b542cfSBrian Somers fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY); 29085b542cfSBrian Somers return 2; 29185b542cfSBrian Somers } 29285b542cfSBrian Somers } 29385b542cfSBrian Somers 294b6217683SBrian Somers /* Allow output for the moment (except in direct mode) */ 2956f384573SBrian Somers if (mode == PHYS_DIRECT) 296b6217683SBrian Somers prompt = NULL; 297ed0e9269SBrian Somers else 298b6217683SBrian Somers SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD); 29912ef29a8SBrian Somers 3005106c671SBrian Somers ID0init(); 3014562be74SBrian Somers if (ID0realuid() != 0) { 3024562be74SBrian Somers char conf[200], *ptr; 3034562be74SBrian Somers 3044562be74SBrian Somers snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE); 3054562be74SBrian Somers do { 3064562be74SBrian Somers if (!access(conf, W_OK)) { 307a33b2ef7SBrian Somers log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n", 308a33b2ef7SBrian Somers conf); 3094562be74SBrian Somers return -1; 3104562be74SBrian Somers } 3114562be74SBrian Somers ptr = conf + strlen(conf)-2; 3124562be74SBrian Somers while (ptr > conf && *ptr != '/') 3134562be74SBrian Somers *ptr-- = '\0'; 3144562be74SBrian Somers } while (ptr >= conf); 3154562be74SBrian Somers } 3164562be74SBrian Somers 3177cf368ebSBrian Somers if (label < argc) 3187cf368ebSBrian Somers for (arg = label; arg < argc; arg++) 3197cf368ebSBrian Somers CheckLabel(argv[arg], prompt, mode); 3207cf368ebSBrian Somers else 3217cf368ebSBrian Somers CheckLabel("default", prompt, mode); 32212ef29a8SBrian Somers 323ed0e9269SBrian Somers prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(mode)); 324ed0e9269SBrian Somers 3258e7b8599SBrian Somers if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) { 326dd7e2610SBrian Somers log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno)); 3274ef16f24SBrian Somers return EX_START; 328af57ed9fSAtsushi Murai } 329756783fcSBrian Somers 330756783fcSBrian Somers /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */ 331756783fcSBrian Somers 3320f2f3eb3SBrian Somers if (prompt) { 3330f2f3eb3SBrian Somers prompt->bundle = bundle; /* couldn't do it earlier */ 3348fa6ebe4SBrian Somers prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name); 3350f2f3eb3SBrian Somers } 33683d1af55SBrian Somers SignalBundle = bundle; 337615ad4f9SBrian Somers bundle->AliasEnabled = alias; 3388fa6ebe4SBrian Somers if (alias) 3398fa6ebe4SBrian Somers bundle->cfg.opt |= OPT_IFACEALIAS; 34012ef29a8SBrian Somers 34130291ffbSBrian Somers if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0) 342565e35e5SBrian Somers prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); 3431ae349f5Scvs2svn 344dd7e2610SBrian Somers sig_signal(SIGHUP, CloseSession); 345dd7e2610SBrian Somers sig_signal(SIGTERM, CloseSession); 346dd7e2610SBrian Somers sig_signal(SIGINT, CloseConnection); 347dd7e2610SBrian Somers sig_signal(SIGQUIT, CloseSession); 348dd7e2610SBrian Somers sig_signal(SIGALRM, SIG_IGN); 349e0d3e233SAndrey A. Chernov signal(SIGPIPE, SIG_IGN); 350565e35e5SBrian Somers 35181358fa3SBrian Somers if (mode == PHYS_INTERACTIVE) 352dd7e2610SBrian Somers sig_signal(SIGTSTP, TerminalStop); 353f91ad6b0SBrian Somers 354dd7e2610SBrian Somers sig_signal(SIGUSR2, BringDownServer); 355af57ed9fSAtsushi Murai 356756783fcSBrian Somers lastlabel = argc == 2 ? bundle->argv1 : argv[argc - 1]; 3577cf368ebSBrian Somers for (arg = label; arg < argc; arg++) { 3587cf368ebSBrian Somers /* In case we use LABEL or ``set enddisc label'' */ 359756783fcSBrian Somers bundle_SetLabel(bundle, lastlabel); 360756783fcSBrian Somers system_Select(bundle, arg == 1 ? bundle->argv1 : argv[arg], 361756783fcSBrian Somers CONFFILE, prompt, NULL); 3621ae349f5Scvs2svn } 3637cf368ebSBrian Somers 3647cf368ebSBrian Somers if (label < argc) 3657cf368ebSBrian Somers /* In case the last label did a ``load'' */ 366756783fcSBrian Somers bundle_SetLabel(bundle, lastlabel); 3677cf368ebSBrian Somers 36881358fa3SBrian Somers if (mode == PHYS_AUTO && 3695828db6dSBrian Somers bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) { 3707cf368ebSBrian Somers prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address " 3717cf368ebSBrian Somers "in auto mode.\n"); 3728390b576SBrian Somers AbortProgram(EX_START); 373af57ed9fSAtsushi Murai } 3746efd9292SBrian Somers 37581358fa3SBrian Somers if (mode != PHYS_INTERACTIVE) { 3766f384573SBrian Somers if (mode != PHYS_DIRECT) { 3775cf4388bSBrian Somers int bgpipe[2]; 3786d14e2a8SJordan K. Hubbard pid_t bgpid; 379a9c6b5dfSAtsushi Murai 38081358fa3SBrian Somers if (mode == PHYS_BACKGROUND && pipe(bgpipe)) { 381dd7e2610SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 3828390b576SBrian Somers AbortProgram(EX_SOCK); 3831ae349f5Scvs2svn } 3841ae349f5Scvs2svn 3856d14e2a8SJordan K. Hubbard bgpid = fork(); 3866d14e2a8SJordan K. Hubbard if (bgpid == -1) { 387dd7e2610SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 3888390b576SBrian Somers AbortProgram(EX_SOCK); 3896d14e2a8SJordan K. Hubbard } 3905cf4388bSBrian Somers 3916d14e2a8SJordan K. Hubbard if (bgpid) { 3926d14e2a8SJordan K. Hubbard char c = EX_NORMAL; 393a9c6b5dfSAtsushi Murai 39481358fa3SBrian Somers if (mode == PHYS_BACKGROUND) { 3955cf4388bSBrian Somers close(bgpipe[1]); 3966d14e2a8SJordan K. Hubbard BGPid = bgpid; 39775b8d283SBrian Somers /* If we get a signal, kill the child */ 39875b8d283SBrian Somers signal(SIGHUP, KillChild); 39975b8d283SBrian Somers signal(SIGTERM, KillChild); 40075b8d283SBrian Somers signal(SIGINT, KillChild); 40175b8d283SBrian Somers signal(SIGQUIT, KillChild); 40275b8d283SBrian Somers 40375b8d283SBrian Somers /* Wait for our child to close its pipe before we exit */ 4045cf4388bSBrian Somers if (read(bgpipe[0], &c, 1) != 1) { 405b6217683SBrian Somers prompt_Printf(prompt, "Child exit, no status.\n"); 406dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Child exit, no status.\n"); 4076efd9292SBrian Somers } else if (c == EX_NORMAL) { 408b6217683SBrian Somers prompt_Printf(prompt, "PPP enabled.\n"); 409dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: PPP enabled.\n"); 4106efd9292SBrian Somers } else { 411b6217683SBrian Somers prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c)); 412dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Child failed (%s).\n", 41380e37c72SBrian Somers ex_desc((int) c)); 4146efd9292SBrian Somers } 4155cf4388bSBrian Somers close(bgpipe[0]); 4166d14e2a8SJordan K. Hubbard } 4174ef16f24SBrian Somers return c; 41881358fa3SBrian Somers } else if (mode == PHYS_BACKGROUND) { 4195cf4388bSBrian Somers close(bgpipe[0]); 4205cf4388bSBrian Somers bundle->notify.fd = bgpipe[1]; 421aefd026aSBrian Somers } 422aefd026aSBrian Somers 423da66dd13SBrian Somers bundle_LockTun(bundle); /* we have a new pid */ 424da66dd13SBrian Somers 425565e35e5SBrian Somers /* -auto, -dedicated, -ddial & -background */ 426b6217683SBrian Somers prompt_Destroy(prompt, 0); 4272a279fedSBrian Somers close(STDOUT_FILENO); 4282a279fedSBrian Somers close(STDERR_FILENO); 4292a279fedSBrian Somers close(STDIN_FILENO); 430b6217683SBrian Somers setsid(); 431565e35e5SBrian Somers } else { 432565e35e5SBrian Somers /* -direct: STDIN_FILENO gets used by modem_Open */ 433565e35e5SBrian Somers prompt_TtyInit(NULL); 434565e35e5SBrian Somers close(STDOUT_FILENO); 435565e35e5SBrian Somers close(STDERR_FILENO); 436d656a4c5SBrian Somers } 437af57ed9fSAtsushi Murai } else { 438565e35e5SBrian Somers /* Interactive mode */ 4392a279fedSBrian Somers close(STDERR_FILENO); 440565e35e5SBrian Somers prompt_TtyInit(prompt); 441b6217683SBrian Somers prompt_TtyCommandMode(prompt); 442b6217683SBrian Somers prompt_Required(prompt); 443af57ed9fSAtsushi Murai } 44435495becSBrian Somers 445dd7e2610SBrian Somers log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode)); 4460f78c7a7SBrian Somers DoLoop(bundle); 4471afedc4bSBrian Somers AbortProgram(EX_NORMAL); 448af57ed9fSAtsushi Murai 4491afedc4bSBrian Somers return EX_NORMAL; 450af57ed9fSAtsushi Murai } 451af57ed9fSAtsushi Murai 452af57ed9fSAtsushi Murai static void 4530f78c7a7SBrian Somers DoLoop(struct bundle *bundle) 454af57ed9fSAtsushi Murai { 455af57ed9fSAtsushi Murai fd_set rfds, wfds, efds; 4561af29a6eSBrian Somers int i, nfds, nothing_done; 4571af29a6eSBrian Somers struct probe probe; 4581af29a6eSBrian Somers 4591af29a6eSBrian Somers probe_Init(&probe); 460c3899f8dSAtsushi Murai 461565e35e5SBrian Somers do { 462780700e5SAndrey A. Chernov nfds = 0; 463944f7098SBrian Somers FD_ZERO(&rfds); 464944f7098SBrian Somers FD_ZERO(&wfds); 465944f7098SBrian Somers FD_ZERO(&efds); 46684b8a6ebSAtsushi Murai 4670f2f3eb3SBrian Somers /* All our datalinks, the tun device and the MP socket */ 4686f384573SBrian Somers descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds); 4690f2f3eb3SBrian Somers 4700f2f3eb3SBrian Somers /* All our prompts and the diagnostic socket */ 4710f2f3eb3SBrian Somers descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds); 47207030d97SBrian Somers 4733b0f8d2eSBrian Somers if (bundle_IsDead(bundle)) 474f4768038SBrian Somers /* Don't select - we'll be here forever */ 475f4768038SBrian Somers break; 4760706ff38SBrian Somers 477486105bcSBrian Somers /* 478486105bcSBrian Somers * It's possible that we've had a signal since we last checked. If 479486105bcSBrian Somers * we don't check again before calling select(), we may end up stuck 480486105bcSBrian Somers * after having missed the event.... sig_Handle() tries to be as 481486105bcSBrian Somers * quick as possible if nothing is likely to have happened. 482486105bcSBrian Somers * This is only really likely if we block in open(... O_NONBLOCK) 483486105bcSBrian Somers * which will happen with a misconfigured device. 484486105bcSBrian Somers */ 485486105bcSBrian Somers if (sig_Handle()) 486486105bcSBrian Somers continue; 487486105bcSBrian Somers 4883006ec67SBrian Somers i = select(nfds, &rfds, &wfds, &efds, NULL); 489712ae387SBrian Somers 49054cd8e13SBrian Somers if (i < 0 && errno != EINTR) { 491dd7e2610SBrian Somers log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno)); 49224989c68SBrian Somers if (log_IsKept(LogTIMER)) { 49324989c68SBrian Somers struct timeval t; 49424989c68SBrian Somers 49524989c68SBrian Somers for (i = 0; i <= nfds; i++) { 49624989c68SBrian Somers if (FD_ISSET(i, &rfds)) { 49724989c68SBrian Somers log_Printf(LogTIMER, "Read set contains %d\n", i); 49824989c68SBrian Somers FD_CLR(i, &rfds); 49924989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 50024989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 50124989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 502af57ed9fSAtsushi Murai break; 503af57ed9fSAtsushi Murai } 50424989c68SBrian Somers } 50524989c68SBrian Somers if (FD_ISSET(i, &wfds)) { 50624989c68SBrian Somers log_Printf(LogTIMER, "Write set contains %d\n", i); 50724989c68SBrian Somers FD_CLR(i, &wfds); 50824989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 50924989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 51024989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 511af57ed9fSAtsushi Murai break; 512af57ed9fSAtsushi Murai } 513e0d3e233SAndrey A. Chernov } 51424989c68SBrian Somers if (FD_ISSET(i, &efds)) { 51524989c68SBrian Somers log_Printf(LogTIMER, "Error set contains %d\n", i); 51624989c68SBrian Somers FD_CLR(i, &efds); 51724989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 51824989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 51924989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 52058f264e1SBrian Somers break; 52158f264e1SBrian Somers } 52224989c68SBrian Somers } 52324989c68SBrian Somers } 52424989c68SBrian Somers } 52558f264e1SBrian Somers break; 526de451c68SBrian Somers } 527de451c68SBrian Somers 528f0cdd9c0SBrian Somers log_Printf(LogTIMER, "Select returns %d\n", i); 529f0cdd9c0SBrian Somers 53054cd8e13SBrian Somers sig_Handle(); 53154cd8e13SBrian Somers 53254cd8e13SBrian Somers if (i <= 0) 53354cd8e13SBrian Somers continue; 53454cd8e13SBrian Somers 5353006ec67SBrian Somers for (i = 0; i <= nfds; i++) 5363006ec67SBrian Somers if (FD_ISSET(i, &efds)) { 537991c2a7bSBrian Somers log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i); 538991c2a7bSBrian Somers /* We deal gracefully with link descriptor exceptions */ 539991c2a7bSBrian Somers if (!bundle_Exception(bundle, i)) { 540991c2a7bSBrian Somers log_Printf(LogERROR, "Exception cannot be handled !\n"); 5411ae349f5Scvs2svn break; 5421ae349f5Scvs2svn } 543991c2a7bSBrian Somers } 544c60f92caSBrian Somers 545b7c5748eSBrian Somers if (i <= nfds) 546b7c5748eSBrian Somers break; 547b7c5748eSBrian Somers 5481af29a6eSBrian Somers nothing_done = 1; 5491af29a6eSBrian Somers 5501af29a6eSBrian Somers if (descriptor_IsSet(&server.desc, &rfds)) { 551b77776a7SBrian Somers descriptor_Read(&server.desc, bundle, &rfds); 5521af29a6eSBrian Somers nothing_done = 0; 5531af29a6eSBrian Somers } 5541af29a6eSBrian Somers 5551af29a6eSBrian Somers if (descriptor_IsSet(&bundle->desc, &rfds)) { 5561af29a6eSBrian Somers descriptor_Read(&bundle->desc, bundle, &rfds); 5571af29a6eSBrian Somers nothing_done = 0; 5581af29a6eSBrian Somers } 55942d4d396SBrian Somers 5602f786681SBrian Somers if (descriptor_IsSet(&bundle->desc, &wfds)) 5611af29a6eSBrian Somers if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) { 5621af29a6eSBrian Somers /* 5631af29a6eSBrian Somers * This is disasterous. The OS has told us that something is 5641af29a6eSBrian Somers * writable, and all our write()s have failed. Rather than 5651af29a6eSBrian Somers * going back immediately to do our UpdateSet()s and select(), 5661af29a6eSBrian Somers * we sleep for a bit to avoid gobbling up all cpu time. 5671af29a6eSBrian Somers */ 5681af29a6eSBrian Somers struct timeval t; 5692f786681SBrian Somers 5701af29a6eSBrian Somers t.tv_sec = 0; 5711af29a6eSBrian Somers t.tv_usec = 100000; 5721af29a6eSBrian Somers select(0, NULL, NULL, NULL, &t); 5731af29a6eSBrian Somers } 57454cd8e13SBrian Somers 575565e35e5SBrian Somers } while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle)); 576565e35e5SBrian Somers 577dd7e2610SBrian Somers log_Printf(LogDEBUG, "DoLoop done.\n"); 578af57ed9fSAtsushi Murai } 579