main.c revision 1661f734d42e48458fc62f5c6413c308481b4e5a
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * main.c - Point-to-Point Protocol main module
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modification, are permitted provided that the following conditions
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * are met:
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *    the documentation and/or other materials provided with the
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *    distribution.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 3. The name "Carnegie Mellon University" must not be used to
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    endorse or promote products derived from this software without
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    prior written permission. For permission or any legal
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    details, please contact
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *      Office of Technology Transfer
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *      Carnegie Mellon University
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *      5000 Forbes Avenue
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *      Pittsburgh, PA  15213-3890
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *      (412) 268-4387, fax: (412) 268-7395
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *      tech-transfer@andrew.cmu.edu
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 4. Redistributions of any form whatsoever must retain the following
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    acknowledgment:
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    "This product includes software developed by Computing Services
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 1999-2004 Paul Mackerras. All rights reserved.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modification, are permitted provided that the following conditions
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * are met:
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2. The name(s) of the authors of this software must not be used to
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    endorse or promote products derived from this software without
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    prior written permission.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 3. Redistributions of any form whatsoever must retain the following
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    acknowledgment:
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    "This product includes software developed by Paul Mackerras
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     <paulus@samba.org>".
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define RCSID	"$Id: main.c,v 1.148 2004/11/13 12:05:48 paulus Exp $"
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h>
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <ctype.h>
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdlib.h>
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string.h>
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <signal.h>
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <errno.h>
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fcntl.h>
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <syslog.h>
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <netdb.h>
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utmp.h>
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <pwd.h>
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <setjmp.h>
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/param.h>
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/types.h>
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/wait.h>
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/time.h>
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/resource.h>
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/stat.h>
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/socket.h>
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <netinet/in.h>
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <arpa/inet.h>
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cutils/properties.h>
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pppd.h"
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "magic.h"
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "fsm.h"
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "lcp.h"
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipcp.h"
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef INET6
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipv6cp.h"
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "upap.h"
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chap-new.h"
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "eap.h"
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ccp.h"
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ecp.h"
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pathnames.h"
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef USE_TDB
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "tdb.h"
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef CBCP_SUPPORT
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "cbcp.h"
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef IPX_CHANGE
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipxcp.h"
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* IPX_CHANGE */
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef AT_CHANGE
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "atcp.h"
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const char rcsid[] = RCSID;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* interface vars */
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char ifname[32];		/* Interface name */
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ifunit;			/* Interface unit number */
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct channel *the_channel;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char *progname;			/* Name of this program */
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char hostname[MAXNAMELEN];	/* Our hostname */
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static char pidfilename[MAXPATHLEN];	/* name of pid file */
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static char linkpidfile[MAXPATHLEN];	/* name of linkname pid file */
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char ppp_devnam[MAXPATHLEN];	/* name of PPP tty (maybe ttypx) */
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uid_t uid;			/* Our real user-id */
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct notifier *pidchange = NULL;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct notifier *phasechange = NULL;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct notifier *exitnotify = NULL;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct notifier *sigreceived = NULL;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct notifier *fork_notifier = NULL;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int hungup;			/* terminal has been hung up */
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int privileged;			/* we're running as real uid root */
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int need_holdoff;		/* need holdoff period before restarting */
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int detached;			/* have detached from terminal */
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)volatile int status;		/* exit status for pppd */
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int unsuccess;			/* # unsuccessful connection attempts */
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int do_callback;		/* != 0 if we should do callback next */
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int doing_callback;		/* != 0 if we are doing callback */
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ppp_session_number;		/* Session number, for channels with such a
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   concept (eg PPPoE) */
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int childwait_done;		/* have timed out waiting for children */
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef USE_TDB
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TDB_CONTEXT *pppdb;		/* database for storing status etc. */
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char db_key[32];
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int (*holdoff_hook) __P((void)) = NULL;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int (*new_phase_hook) __P((int)) = NULL;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void (*snoop_recv_hook) __P((unsigned char *p, int len)) = NULL;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void (*snoop_send_hook) __P((unsigned char *p, int len)) = NULL;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int conn_running;	/* we have a [dis]connector running */
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int fd_loop;		/* fd for getting demand-dial packets */
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int fd_devnull;			/* fd for /dev/null */
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int devfd = -1;			/* fd of underlying device */
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int fd_ppp = -1;		/* fd for talking PPP */
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int phase;			/* where the link is at */
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int kill_link;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int asked_to_quit;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int open_ccp_flag;
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int listen_time;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int got_sigusr2;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int got_sigterm;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int got_sighup;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static sigset_t signals_handled;
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int waiting;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static sigjmp_buf sigjmp;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char **script_env;		/* Env. variable values for scripts */
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int s_env_nalloc;		/* # words avail at script_env */
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)u_char outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for outgoing packet */
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int n_children;		/* # child processes still running */
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int got_sigchld;		/* set if we have received a SIGCHLD */
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int privopen;			/* don't lock, open device as root */
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n";
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GIDSET_TYPE groups[NGROUPS_MAX];/* groups the user is in */
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ngroups;			/* How many groups valid in groups */
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct timeval start_time;	/* Time when link was started. */
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct pppd_stats old_link_stats;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct pppd_stats link_stats;
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned link_connect_time;
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int link_stats_valid;
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int error_count;
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool bundle_eof;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool bundle_terminating;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int sent_since_received = 0;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int sent_total = 0;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int received_total = 0;
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * We maintain a list of child process pids and
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * functions to call when they exit.
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct subprocess {
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pid_t	pid;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    char	*prog;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void	(*done) __P((void *));
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void	*arg;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    struct subprocess *next;
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct subprocess *children;
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Prototypes for procedures local to this file. */
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void setup_signals __P((void));
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void create_pidfile __P((int pid));
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void create_linkpidfile __P((int pid));
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void cleanup __P((void));
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void get_input __P((void));
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void calltimeout __P((void));
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct timeval *timeleft __P((struct timeval *));
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void kill_my_pg __P((int));
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void hup __P((int));
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void term __P((int));
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void chld __P((int));
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void toggle_debug __P((int));
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void open_ccp __P((int));
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void bad_signal __P((int));
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void holdoff_end __P((void *));
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int reap_kids __P((void));
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void childwait_end __P((void *));
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef USE_TDB
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void update_db_entry __P((void));
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void add_db_key __P((const char *));
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void delete_db_key __P((const char *));
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void cleanup_db __P((void));
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void handle_events __P((void));
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void print_link_stats __P((void));
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern	char	*ttyname __P((int));
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern	char	*getlogin __P((void));
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int main __P((int, char *[]));
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef ultrix
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef	O_NONBLOCK
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define	O_NONBLOCK	O_NDELAY
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef ULTRIX
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define setlogmask(x)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * PPP Data Link Layer "protocol" table.
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * One entry per supported protocol.
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The last entry must be NULL.
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct protent *protocols[] = {
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &lcp_protent,
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &pap_protent,
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &chap_protent,
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef CBCP_SUPPORT
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &cbcp_protent,
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &ipcp_protent,
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef INET6
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &ipv6cp_protent,
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &ccp_protent,
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &ecp_protent,
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef IPX_CHANGE
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &ipxcp_protent,
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef AT_CHANGE
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &atcp_protent,
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    &eap_protent,
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NULL
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * If PPP_DRV_NAME is not defined, use the default "ppp" as the device name.
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(PPP_DRV_NAME)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPP_DRV_NAME	"ppp"
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* !defined(PPP_DRV_NAME) */
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)main(argc, argv)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int argc;
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    char *argv[];
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int i, t;
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    char *p;
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    struct passwd *pw;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    struct protent *protp;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    char numbuf[16];
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    link_stats_valid = 0;
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_phase(PHASE_INITIALIZE);
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    script_env = NULL;
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /* Initialize syslog facilities */
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reopen_log();
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (gethostname(hostname, MAXNAMELEN) < 0 ) {
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	option_error("Couldn't get hostname: %m");
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	exit(1);
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    hostname[MAXNAMELEN-1] = 0;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /* make sure we don't create world or group writable files. */
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    umask(umask(0777) | 022);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    uid = getuid();
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    privileged = uid == 0;
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    slprintf(numbuf, sizeof(numbuf), "%d", uid);
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    script_setenv("ORIG_UID", numbuf, 0);
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ngroups = getgroups(NGROUPS_MAX, groups);
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /*
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Initialize magic number generator now so that protocols may
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * use magic numbers in initialization.
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    magic_init();
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /*
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Initialize each protocol.
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (i = 0; (protp = protocols[i]) != NULL; ++i)
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (*protp->init)(0);
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /*
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Initialize the default channel.
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    tty_init();
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    progname = *argv;
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef ANDROID_CHANGES
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        extern void pppox_init();
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        pppox_init();
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        privileged = 1;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        char *envargs = getenv("envargs");
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (envargs) {
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            int i;
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            /* Decode the arguments in-place and count the number of them.
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             * They were hex encoded using [A-P] instead of [0-9A-F]. */
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            for (argc = 0, i = 0; envargs[i] && envargs[i + 1]; i += 2) {
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                char c = ((envargs[i] - 'A') << 4) + (envargs[i + 1] - 'A');
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                if (c == 0) {
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    ++argc;
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                }
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                envargs[i / 2 + 1] = c;
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            }
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            if (argc == 0 || (argv = malloc(sizeof(char *) * argc)) == NULL) {
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                fatal("Failed to parse envargs!");
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            }
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            for (envargs[0] = 0, i = 0; i < argc; ++envargs) {
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                if (envargs[0] == 0) {
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    argv[i++] = &envargs[1];
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                }
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            }
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        }
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /*
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * Parse, in order, the system options file, the user's options file,
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * and the command line arguments.
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef ANDROID_CHANGES
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /* Android: only take options from commandline */
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!parse_args(argc-1, argv+1))
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	exit(EXIT_OPTION_ERROR);
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1)
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	|| !options_from_user()
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	|| !parse_args(argc-1, argv+1))
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	exit(EXIT_OPTION_ERROR);
4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    devnam_fixed = 1;		/* can no longer change device name */
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /*
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * Work out the device name, if it hasn't already been specified,
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * and parse the tty's options file.
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (the_channel->process_extra_options)
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	(*the_channel->process_extra_options)();
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (debug)
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	setlogmask(LOG_UPTO(LOG_DEBUG));
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef ANDROID_CHANGES
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /*
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * Check that we are running as root.
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (geteuid() != 0) {
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	option_error("must be root to run %s, since it is not setuid-root",
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		     argv[0]);
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	exit(EXIT_NOT_ROOT);
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!ppp_available()) {
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	option_error("%s", no_ppp_msg);
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	exit(EXIT_NO_KERNEL_SUPPORT);
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /*
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * Check that the options given are valid and consistent.
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    check_options();
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!sys_check_options())
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	exit(EXIT_OPTION_ERROR);
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    auth_check_options();
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef HAVE_MULTILINK
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    mp_check_options();
4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (i = 0; (protp = protocols[i]) != NULL; ++i)
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (protp->check_options != NULL)
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    (*protp->check_options)();
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (the_channel->check_options)
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	(*the_channel->check_options)();
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (dump_options || dryrun) {
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	init_pr_log(NULL, LOG_INFO);
4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	print_options(pr_log, NULL);
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	end_pr_log();
4622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (dryrun)
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	die(0);
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /* Make sure fds 0, 1, 2 are open to somewhere. */
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    fd_devnull = open(_PATH_DEVNULL, O_RDWR);
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (fd_devnull < 0)
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	fatal("Couldn't open %s: %m", _PATH_DEVNULL);
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    while (fd_devnull <= 2) {
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	i = dup(fd_devnull);
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (i < 0)
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    fatal("Critical shortage of file descriptors: dup failed: %m");
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	fd_devnull = i;
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /*
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * Initialize system-dependent stuff.
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    sys_init();
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef USE_TDB
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    pppdb = tdb_open(_PATH_PPPDB, 0, 0, O_RDWR|O_CREAT, 0644);
4842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (pppdb != NULL) {
4852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	slprintf(db_key, sizeof(db_key), "pppd%d", getpid());
4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	update_db_entry();
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	warn("Warning: couldn't open ppp database %s", _PATH_PPPDB);
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (multilink) {
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    warn("Warning: disabling multilink");
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    multilink = 0;
4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	}
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /*
4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * Detach ourselves from the terminal, if required,
4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * and identify who is running us.
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!nodetach && !updetach)
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	detach();
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    p = getlogin();
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (p == NULL) {
5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	pw = getpwuid(uid);
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (pw != NULL && pw->pw_name != NULL)
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    p = pw->pw_name;
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	else
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    p = "(unknown)";
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
5102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    syslog(LOG_NOTICE, "pppd %s started by %s, uid %d", VERSION, p, uid);
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    script_setenv("PPPLOGNAME", p, 0);
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (devnam[0])
5142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	script_setenv("DEVICE", devnam, 1);
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    slprintf(numbuf, sizeof(numbuf), "%d", getpid());
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    script_setenv("PPPD_PID", numbuf, 1);
5172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    setup_signals();
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    create_linkpidfile(getpid());
5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    waiting = 0;
5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /*
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * If we're doing dial-on-demand, set up the interface now.
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (demand) {
5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	/*
5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * Open the loopback channel and set it up to be the ppp interface.
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 */
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	fd_loop = open_ppp_loopback();
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	set_ifunit(1);
5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	/*
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * Configure the interface and mark it up, etc.
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 */
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	demand_conf();
5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
5382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    do_callback = 0;
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (;;) {
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	bundle_eof = 0;
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	bundle_terminating = 0;
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	listen_time = 0;
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	need_holdoff = 1;
5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	devfd = -1;
5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	status = EXIT_OK;
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	++unsuccess;
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	doing_callback = do_callback;
5502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	do_callback = 0;
5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (demand && !doing_callback) {
5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    /*
5542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	     * Don't do anything until we see some activity.
5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	     */
5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    new_phase(PHASE_DORMANT);
5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    demand_unblock();
5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    add_fd(fd_loop);
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    for (;;) {
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		handle_events();
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		if (asked_to_quit)
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		    break;
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		if (get_loop_output())
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		    break;
5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    }
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    remove_fd(fd_loop);
5672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    if (asked_to_quit)
5682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		break;
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    /*
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	     * Now we want to bring up the link.
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	     */
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    demand_block();
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    info("Starting link");
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	gettimeofday(&start_time, NULL);
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	script_unsetenv("CONNECT_TIME");
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	script_unsetenv("BYTES_SENT");
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	script_unsetenv("BYTES_RCVD");
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	lcp_open(0);		/* Start protocol */
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	while (phase != PHASE_DEAD) {
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    handle_events();
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    get_input();
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    if (kill_link)
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		lcp_close(0, "User request");
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    if (asked_to_quit) {
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		bundle_terminating = 1;
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (phase == PHASE_MASTER)
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    mp_bundle_terminated();
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    }
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    if (open_ccp_flag) {
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) {
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    (*ccp_protent.open)(0);
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    }
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (!persist || asked_to_quit || (maxfail > 0 && unsuccess >= maxfail))
6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    break;
6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (demand)
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    demand_discard();
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	t = need_holdoff? holdoff: 0;
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (holdoff_hook)
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    t = (*holdoff_hook)();
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (t > 0) {
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    new_phase(PHASE_HOLDOFF);
6112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    TIMEOUT(holdoff_end, NULL, t);
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    do {
6132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		handle_events();
6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		if (kill_link)
6152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		    new_phase(PHASE_DORMANT); /* allow signal to end holdoff */
6162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    } while (phase == PHASE_HOLDOFF);
6172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    if (!persist)
6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		break;
6192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	}
6202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
6212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /* Wait for scripts to finish */
6232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    reap_kids();
6242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (n_children > 0) {
6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (child_wait > 0)
6262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    TIMEOUT(childwait_end, NULL, child_wait);
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (debug) {
6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    struct subprocess *chp;
6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    dbglog("Waiting for %d child processes...", n_children);
6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    for (chp = children; chp != NULL; chp = chp->next)
6312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		dbglog("  script %s, pid %d", chp->prog, chp->pid);
6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	}
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	while (n_children > 0 && !childwait_done) {
6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    handle_events();
6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    if (kill_link && !childwait_done)
6362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		childwait_end(NULL);
6372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	}
6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    die(status);
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return 0;
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/*
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * handle_events - wait for something to happen and respond to it.
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
6472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void
6482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)handle_events()
6492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
6502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    struct timeval timo;
6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    kill_link = open_ccp_flag = 0;
6532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (sigsetjmp(sigjmp, 1) == 0) {
6542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	sigprocmask(SIG_BLOCK, &signals_handled, NULL);
6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (got_sighup || got_sigterm || got_sigusr2 || got_sigchld) {
6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    sigprocmask(SIG_UNBLOCK, &signals_handled, NULL);
6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	} else {
6582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    waiting = 1;
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    sigprocmask(SIG_UNBLOCK, &signals_handled, NULL);
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    wait_input(timeleft(&timo));
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	}
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    waiting = 0;
6642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    calltimeout();
6652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (got_sighup) {
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	info("Hangup (SIGHUP)");
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	kill_link = 1;
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	got_sighup = 0;
6692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (status != EXIT_HANGUP)
6702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    status = EXIT_USER_REQUEST;
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (got_sigterm) {
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	info("Terminating on signal %d", got_sigterm);
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	kill_link = 1;
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	asked_to_quit = 1;
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	persist = 0;
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	status = EXIT_USER_REQUEST;
6782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	got_sigterm = 0;
6792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
6802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (got_sigchld) {
6812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	got_sigchld = 0;
6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	reap_kids();	/* Don't leave dead kids lying around */
6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (got_sigusr2) {
6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	open_ccp_flag = 1;
6862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	got_sigusr2 = 0;
6872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/*
6912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * setup_signals - initialize signal handling.
6922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
6932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void
6942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)setup_signals()
6952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    struct sigaction sa;
6972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /*
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * Compute mask of all interesting signals and install signal handlers
7002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * for each.  Only one signal handler may be active at a time.  Therefore,
7012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * all other signals should be masked when any handler is executing.
7022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
7032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    sigemptyset(&signals_handled);
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    sigaddset(&signals_handled, SIGHUP);
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    sigaddset(&signals_handled, SIGINT);
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    sigaddset(&signals_handled, SIGTERM);
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    sigaddset(&signals_handled, SIGCHLD);
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    sigaddset(&signals_handled, SIGUSR2);
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SIGNAL(s, handler)	do { \
7112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	sa.sa_handler = handler; \
7122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (sigaction(s, &sa, NULL) < 0) \
7132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    fatal("Couldn't establish signal handler (%d): %m", s); \
7142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } while (0)
7152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    sa.sa_mask = signals_handled;
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    sa.sa_flags = 0;
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SIGNAL(SIGHUP, hup);		/* Hangup */
7192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SIGNAL(SIGINT, term);		/* Interrupt */
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SIGNAL(SIGTERM, term);		/* Terminate */
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SIGNAL(SIGCHLD, chld);
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SIGNAL(SIGUSR1, toggle_debug);	/* Toggle debug flag */
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGUSR2, open_ccp);		/* Reopen CCP */
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /*
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Install a handler for other signals which would otherwise
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * cause pppd to exit without cleaning up.
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
7302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SIGNAL(SIGABRT, bad_signal);
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGALRM, bad_signal);
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGFPE, bad_signal);
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGILL, bad_signal);
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGPIPE, bad_signal);
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGQUIT, bad_signal);
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGSEGV, bad_signal);
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef SIGBUS
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGBUS, bad_signal);
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef SIGEMT
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGEMT, bad_signal);
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef SIGPOLL
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGPOLL, bad_signal);
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef SIGPROF
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGPROF, bad_signal);
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef SIGSYS
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGSYS, bad_signal);
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef SIGTRAP
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGTRAP, bad_signal);
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef SIGVTALRM
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGVTALRM, bad_signal);
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef SIGXCPU
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGXCPU, bad_signal);
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef SIGXFSZ
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SIGNAL(SIGXFSZ, bad_signal);
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /*
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Apparently we can get a SIGPIPE when we call syslog, if
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * syslogd has died and been restarted.  Ignoring it seems
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * be sufficient.
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    signal(SIGPIPE, SIG_IGN);
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * set_ifunit - do things we need to do once we know which ppp
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * unit we are using.
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)set_ifunit(iskey)
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int iskey;
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    info("Using interface %s%d", PPP_DRV_NAME, ifunit);
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    slprintf(ifname, sizeof(ifname), "%s%d", PPP_DRV_NAME, ifunit);
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    script_setenv("IFNAME", ifname, iskey);
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (iskey) {
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	create_pidfile(getpid());	/* write pid to file */
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	create_linkpidfile(getpid());
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
791 * detach - detach us from the controlling terminal.
792 */
793void
794detach()
795{
796    int pid;
797    char numbuf[16];
798    int pipefd[2];
799
800    if (detached)
801	return;
802    if (pipe(pipefd) == -1)
803	pipefd[0] = pipefd[1] = -1;
804    if ((pid = fork()) < 0) {
805	error("Couldn't detach (fork failed: %m)");
806	die(1);			/* or just return? */
807    }
808    if (pid != 0) {
809	/* parent */
810	notify(pidchange, pid);
811	/* update pid files if they have been written already */
812	if (pidfilename[0])
813	    create_pidfile(pid);
814	if (linkpidfile[0])
815	    create_linkpidfile(pid);
816	exit(0);		/* parent dies */
817    }
818    setsid();
819    chdir("/");
820    dup2(fd_devnull, 0);
821    dup2(fd_devnull, 1);
822    dup2(fd_devnull, 2);
823    detached = 1;
824    if (log_default)
825	log_to_fd = -1;
826    slprintf(numbuf, sizeof(numbuf), "%d", getpid());
827    script_setenv("PPPD_PID", numbuf, 1);
828
829    /* wait for parent to finish updating pid & lock files and die */
830    close(pipefd[1]);
831    complete_read(pipefd[0], numbuf, 1);
832    close(pipefd[0]);
833}
834
835/*
836 * reopen_log - (re)open our connection to syslog.
837 */
838void
839reopen_log()
840{
841#ifndef ANDROID_CHANGES
842    openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
843    setlogmask(LOG_UPTO(LOG_INFO));
844#endif
845}
846
847/*
848 * Create a file containing our process ID.
849 */
850static void
851create_pidfile(pid)
852    int pid;
853{
854#ifndef ANDROID_CHANGES
855    FILE *pidfile;
856
857    slprintf(pidfilename, sizeof(pidfilename), "%s%s.pid",
858	     _PATH_VARRUN, ifname);
859    if ((pidfile = fopen(pidfilename, "w")) != NULL) {
860	fprintf(pidfile, "%d\n", pid);
861	(void) fclose(pidfile);
862    } else {
863	error("Failed to create pid file %s: %m", pidfilename);
864	pidfilename[0] = 0;
865    }
866#endif
867}
868
869void
870create_linkpidfile(pid)
871    int pid;
872{
873#ifndef ANDROID_CHANGES
874    FILE *pidfile;
875
876    if (linkname[0] == 0)
877	return;
878    script_setenv("LINKNAME", linkname, 1);
879    slprintf(linkpidfile, sizeof(linkpidfile), "%sppp-%s.pid",
880	     _PATH_VARRUN, linkname);
881    if ((pidfile = fopen(linkpidfile, "w")) != NULL) {
882	fprintf(pidfile, "%d\n", pid);
883	if (ifname[0])
884	    fprintf(pidfile, "%s\n", ifname);
885	(void) fclose(pidfile);
886    } else {
887	error("Failed to create pid file %s: %m", linkpidfile);
888	linkpidfile[0] = 0;
889    }
890#endif
891}
892
893/*
894 * remove_pidfile - remove our pid files
895 */
896void remove_pidfiles()
897{
898#ifndef ANDROID_CHANGES
899    if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT)
900	warn("unable to delete pid file %s: %m", pidfilename);
901    pidfilename[0] = 0;
902    if (linkpidfile[0] != 0 && unlink(linkpidfile) < 0 && errno != ENOENT)
903	warn("unable to delete pid file %s: %m", linkpidfile);
904    linkpidfile[0] = 0;
905#endif
906}
907
908/*
909 * holdoff_end - called via a timeout when the holdoff period ends.
910 */
911static void
912holdoff_end(arg)
913    void *arg;
914{
915    new_phase(PHASE_DORMANT);
916}
917
918/* List of protocol names, to make our messages a little more informative. */
919struct protocol_list {
920    u_short	proto;
921    const char	*name;
922} protocol_list[] = {
923    { 0x21,	"IP" },
924    { 0x23,	"OSI Network Layer" },
925    { 0x25,	"Xerox NS IDP" },
926    { 0x27,	"DECnet Phase IV" },
927    { 0x29,	"Appletalk" },
928    { 0x2b,	"Novell IPX" },
929    { 0x2d,	"VJ compressed TCP/IP" },
930    { 0x2f,	"VJ uncompressed TCP/IP" },
931    { 0x31,	"Bridging PDU" },
932    { 0x33,	"Stream Protocol ST-II" },
933    { 0x35,	"Banyan Vines" },
934    { 0x39,	"AppleTalk EDDP" },
935    { 0x3b,	"AppleTalk SmartBuffered" },
936    { 0x3d,	"Multi-Link" },
937    { 0x3f,	"NETBIOS Framing" },
938    { 0x41,	"Cisco Systems" },
939    { 0x43,	"Ascom Timeplex" },
940    { 0x45,	"Fujitsu Link Backup and Load Balancing (LBLB)" },
941    { 0x47,	"DCA Remote Lan" },
942    { 0x49,	"Serial Data Transport Protocol (PPP-SDTP)" },
943    { 0x4b,	"SNA over 802.2" },
944    { 0x4d,	"SNA" },
945    { 0x4f,	"IP6 Header Compression" },
946    { 0x6f,	"Stampede Bridging" },
947    { 0xfb,	"single-link compression" },
948    { 0xfd,	"1st choice compression" },
949    { 0x0201,	"802.1d Hello Packets" },
950    { 0x0203,	"IBM Source Routing BPDU" },
951    { 0x0205,	"DEC LANBridge100 Spanning Tree" },
952    { 0x0231,	"Luxcom" },
953    { 0x0233,	"Sigma Network Systems" },
954    { 0x8021,	"Internet Protocol Control Protocol" },
955    { 0x8023,	"OSI Network Layer Control Protocol" },
956    { 0x8025,	"Xerox NS IDP Control Protocol" },
957    { 0x8027,	"DECnet Phase IV Control Protocol" },
958    { 0x8029,	"Appletalk Control Protocol" },
959    { 0x802b,	"Novell IPX Control Protocol" },
960    { 0x8031,	"Bridging NCP" },
961    { 0x8033,	"Stream Protocol Control Protocol" },
962    { 0x8035,	"Banyan Vines Control Protocol" },
963    { 0x803d,	"Multi-Link Control Protocol" },
964    { 0x803f,	"NETBIOS Framing Control Protocol" },
965    { 0x8041,	"Cisco Systems Control Protocol" },
966    { 0x8043,	"Ascom Timeplex" },
967    { 0x8045,	"Fujitsu LBLB Control Protocol" },
968    { 0x8047,	"DCA Remote Lan Network Control Protocol (RLNCP)" },
969    { 0x8049,	"Serial Data Control Protocol (PPP-SDCP)" },
970    { 0x804b,	"SNA over 802.2 Control Protocol" },
971    { 0x804d,	"SNA Control Protocol" },
972    { 0x804f,	"IP6 Header Compression Control Protocol" },
973    { 0x006f,	"Stampede Bridging Control Protocol" },
974    { 0x80fb,	"Single Link Compression Control Protocol" },
975    { 0x80fd,	"Compression Control Protocol" },
976    { 0xc021,	"Link Control Protocol" },
977    { 0xc023,	"Password Authentication Protocol" },
978    { 0xc025,	"Link Quality Report" },
979    { 0xc027,	"Shiva Password Authentication Protocol" },
980    { 0xc029,	"CallBack Control Protocol (CBCP)" },
981    { 0xc081,	"Container Control Protocol" },
982    { 0xc223,	"Challenge Handshake Authentication Protocol" },
983    { 0xc281,	"Proprietary Authentication Protocol" },
984    { 0,	NULL },
985};
986
987/*
988 * protocol_name - find a name for a PPP protocol.
989 */
990const char *
991protocol_name(proto)
992    int proto;
993{
994    struct protocol_list *lp;
995
996    for (lp = protocol_list; lp->proto != 0; ++lp)
997	if (proto == lp->proto)
998	    return lp->name;
999    return NULL;
1000}
1001
1002/*
1003 * get_input - called when incoming data is available.
1004 */
1005static void
1006get_input()
1007{
1008    int len, i;
1009    u_char *p;
1010    u_short protocol;
1011    struct protent *protp;
1012
1013    p = inpacket_buf;	/* point to beginning of packet buffer */
1014
1015    len = read_packet(inpacket_buf);
1016    if (len < 0)
1017	return;
1018
1019    if (len == 0) {
1020	if (bundle_eof && multilink_master) {
1021	    notice("Last channel has disconnected");
1022	    mp_bundle_terminated();
1023	    return;
1024	}
1025	notice("Modem hangup");
1026	hungup = 1;
1027	status = EXIT_HANGUP;
1028	lcp_lowerdown(0);	/* serial link is no longer available */
1029	link_terminated(0);
1030	return;
1031    }
1032
1033    if (len < PPP_HDRLEN) {
1034	dbglog("received short packet:%.*B", len, p);
1035	return;
1036    }
1037
1038    dump_packet("rcvd", p, len);
1039    if (snoop_recv_hook) snoop_recv_hook(p, len);
1040
1041    p += 2;				/* Skip address and control */
1042    GETSHORT(protocol, p);
1043    len -= PPP_HDRLEN;
1044
1045    /*
1046     * Toss all non-LCP packets unless LCP is OPEN.
1047     */
1048    if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) {
1049	dbglog("Discarded non-LCP packet when LCP not open");
1050	return;
1051    }
1052
1053    /*
1054     * Until we get past the authentication phase, toss all packets
1055     * except LCP, LQR and authentication packets.
1056     */
1057    if (phase <= PHASE_AUTHENTICATE
1058	&& !(protocol == PPP_LCP || protocol == PPP_LQR
1059	     || protocol == PPP_PAP || protocol == PPP_CHAP ||
1060		protocol == PPP_EAP)) {
1061	dbglog("discarding proto 0x%x in phase %d",
1062		   protocol, phase);
1063	return;
1064    }
1065
1066    /*
1067     * Upcall the proper protocol input routine.
1068     */
1069    for (i = 0; (protp = protocols[i]) != NULL; ++i) {
1070	if (protp->protocol == protocol && protp->enabled_flag) {
1071	    (*protp->input)(0, p, len);
1072	    return;
1073	}
1074        if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag
1075	    && protp->datainput != NULL) {
1076	    (*protp->datainput)(0, p, len);
1077	    return;
1078	}
1079    }
1080
1081    if (debug) {
1082	const char *pname = protocol_name(protocol);
1083	if (pname != NULL)
1084	    warn("Unsupported protocol '%s' (0x%x) received", pname, protocol);
1085	else
1086	    warn("Unsupported protocol 0x%x received", protocol);
1087    }
1088    lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN);
1089}
1090
1091/*
1092 * ppp_send_config - configure the transmit-side characteristics of
1093 * the ppp interface.  Returns -1, indicating an error, if the channel
1094 * send_config procedure called error() (or incremented error_count
1095 * itself), otherwise 0.
1096 */
1097int
1098ppp_send_config(unit, mtu, accm, pcomp, accomp)
1099    int unit, mtu;
1100    u_int32_t accm;
1101    int pcomp, accomp;
1102{
1103	int errs;
1104
1105	if (the_channel->send_config == NULL)
1106		return 0;
1107	errs = error_count;
1108	(*the_channel->send_config)(mtu, accm, pcomp, accomp);
1109	return (error_count != errs)? -1: 0;
1110}
1111
1112/*
1113 * ppp_recv_config - configure the receive-side characteristics of
1114 * the ppp interface.  Returns -1, indicating an error, if the channel
1115 * recv_config procedure called error() (or incremented error_count
1116 * itself), otherwise 0.
1117 */
1118int
1119ppp_recv_config(unit, mru, accm, pcomp, accomp)
1120    int unit, mru;
1121    u_int32_t accm;
1122    int pcomp, accomp;
1123{
1124	int errs;
1125
1126	if (the_channel->recv_config == NULL)
1127		return 0;
1128	errs = error_count;
1129	(*the_channel->recv_config)(mru, accm, pcomp, accomp);
1130	return (error_count != errs)? -1: 0;
1131}
1132
1133/*
1134 * new_phase - signal the start of a new phase of pppd's operation.
1135 */
1136void
1137new_phase(p)
1138    int p;
1139{
1140    phase = p;
1141    if (new_phase_hook)
1142	(*new_phase_hook)(p);
1143    notify(phasechange, p);
1144}
1145
1146/*
1147 * die - clean up state and exit with the specified status.
1148 */
1149void
1150die(status)
1151    int status;
1152{
1153    if (!doing_multilink || multilink_master)
1154	print_link_stats();
1155    cleanup();
1156    notify(exitnotify, status);
1157    syslog(LOG_INFO, "Exit.");
1158    exit(status);
1159}
1160
1161/*
1162 * cleanup - restore anything which needs to be restored before we exit
1163 */
1164/* ARGSUSED */
1165static void
1166cleanup()
1167{
1168    sys_cleanup();
1169
1170    if (fd_ppp >= 0)
1171	the_channel->disestablish_ppp(devfd);
1172    if (the_channel->cleanup)
1173	(*the_channel->cleanup)();
1174    remove_pidfiles();
1175
1176#ifdef USE_TDB
1177    if (pppdb != NULL)
1178	cleanup_db();
1179#endif
1180
1181}
1182
1183void
1184print_link_stats()
1185{
1186    /*
1187     * Print connect time and statistics.
1188     */
1189    if (link_stats_valid) {
1190       int t = (link_connect_time + 5) / 6;    /* 1/10ths of minutes */
1191       info("Connect time %d.%d minutes.", t/10, t%10);
1192       info("Sent %u bytes, received %u bytes.",
1193	    link_stats.bytes_out, link_stats.bytes_in);
1194       link_stats_valid = 0;
1195    }
1196}
1197
1198/*
1199 * reset_link_stats - "reset" stats when link goes up.
1200 */
1201void
1202reset_link_stats(u)
1203    int u;
1204{
1205    if (!get_ppp_stats(u, &old_link_stats))
1206	return;
1207    gettimeofday(&start_time, NULL);
1208}
1209
1210/*
1211 * update_link_stats - get stats at link termination.
1212 */
1213void
1214update_link_stats(u)
1215    int u;
1216{
1217    struct timeval now;
1218    char numbuf[32];
1219
1220    if (!get_ppp_stats(u, &link_stats)
1221	|| gettimeofday(&now, NULL) < 0)
1222	return;
1223    link_connect_time = now.tv_sec - start_time.tv_sec;
1224    link_stats_valid = 1;
1225
1226    link_stats.bytes_in  -= old_link_stats.bytes_in;
1227    link_stats.bytes_out -= old_link_stats.bytes_out;
1228    link_stats.pkts_in   -= old_link_stats.pkts_in;
1229    link_stats.pkts_out  -= old_link_stats.pkts_out;
1230
1231    slprintf(numbuf, sizeof(numbuf), "%u", link_connect_time);
1232    script_setenv("CONNECT_TIME", numbuf, 0);
1233    slprintf(numbuf, sizeof(numbuf), "%u", link_stats.bytes_out);
1234    script_setenv("BYTES_SENT", numbuf, 0);
1235    slprintf(numbuf, sizeof(numbuf), "%u", link_stats.bytes_in);
1236    script_setenv("BYTES_RCVD", numbuf, 0);
1237}
1238
1239
1240struct	callout {
1241    struct timeval	c_time;		/* time at which to call routine */
1242    void		*c_arg;		/* argument to routine */
1243    void		(*c_func) __P((void *)); /* routine */
1244    struct		callout *c_next;
1245};
1246
1247static struct callout *callout = NULL;	/* Callout list */
1248static struct timeval timenow;		/* Current time */
1249
1250/*
1251 * timeout - Schedule a timeout.
1252 */
1253void
1254timeout(func, arg, secs, usecs)
1255    void (*func) __P((void *));
1256    void *arg;
1257    int secs, usecs;
1258{
1259    struct callout *newp, *p, **pp;
1260
1261    /*
1262     * Allocate timeout.
1263     */
1264    if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL)
1265	fatal("Out of memory in timeout()!");
1266    newp->c_arg = arg;
1267    newp->c_func = func;
1268    gettimeofday(&timenow, NULL);
1269    newp->c_time.tv_sec = timenow.tv_sec + secs;
1270    newp->c_time.tv_usec = timenow.tv_usec + usecs;
1271    if (newp->c_time.tv_usec >= 1000000) {
1272	newp->c_time.tv_sec += newp->c_time.tv_usec / 1000000;
1273	newp->c_time.tv_usec %= 1000000;
1274    }
1275
1276    /*
1277     * Find correct place and link it in.
1278     */
1279    for (pp = &callout; (p = *pp); pp = &p->c_next)
1280	if (newp->c_time.tv_sec < p->c_time.tv_sec
1281	    || (newp->c_time.tv_sec == p->c_time.tv_sec
1282		&& newp->c_time.tv_usec < p->c_time.tv_usec))
1283	    break;
1284    newp->c_next = p;
1285    *pp = newp;
1286}
1287
1288
1289/*
1290 * untimeout - Unschedule a timeout.
1291 */
1292void
1293untimeout(func, arg)
1294    void (*func) __P((void *));
1295    void *arg;
1296{
1297    struct callout **copp, *freep;
1298
1299    /*
1300     * Find first matching timeout and remove it from the list.
1301     */
1302    for (copp = &callout; (freep = *copp); copp = &freep->c_next)
1303	if (freep->c_func == func && freep->c_arg == arg) {
1304	    *copp = freep->c_next;
1305	    free((char *) freep);
1306	    break;
1307	}
1308}
1309
1310
1311/*
1312 * calltimeout - Call any timeout routines which are now due.
1313 */
1314static void
1315calltimeout()
1316{
1317    struct callout *p;
1318
1319    while (callout != NULL) {
1320	p = callout;
1321
1322	if (gettimeofday(&timenow, NULL) < 0)
1323	    fatal("Failed to get time of day: %m");
1324	if (!(p->c_time.tv_sec < timenow.tv_sec
1325	      || (p->c_time.tv_sec == timenow.tv_sec
1326		  && p->c_time.tv_usec <= timenow.tv_usec)))
1327	    break;		/* no, it's not time yet */
1328
1329	callout = p->c_next;
1330	(*p->c_func)(p->c_arg);
1331
1332	free((char *) p);
1333    }
1334}
1335
1336
1337/*
1338 * timeleft - return the length of time until the next timeout is due.
1339 */
1340static struct timeval *
1341timeleft(tvp)
1342    struct timeval *tvp;
1343{
1344    if (callout == NULL)
1345	return NULL;
1346
1347    gettimeofday(&timenow, NULL);
1348    tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
1349    tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec;
1350    if (tvp->tv_usec < 0) {
1351	tvp->tv_usec += 1000000;
1352	tvp->tv_sec -= 1;
1353    }
1354    if (tvp->tv_sec < 0)
1355	tvp->tv_sec = tvp->tv_usec = 0;
1356
1357    return tvp;
1358}
1359
1360
1361/*
1362 * kill_my_pg - send a signal to our process group, and ignore it ourselves.
1363 * We assume that sig is currently blocked.
1364 */
1365static void
1366kill_my_pg(sig)
1367    int sig;
1368{
1369    struct sigaction act, oldact;
1370
1371    sigemptyset(&act.sa_mask);		/* unnecessary in fact */
1372    act.sa_handler = SIG_IGN;
1373    act.sa_flags = 0;
1374    kill(0, sig);
1375    /*
1376     * The kill() above made the signal pending for us, as well as
1377     * the rest of our process group, but we don't want it delivered
1378     * to us.  It is blocked at the moment.  Setting it to be ignored
1379     * will cause the pending signal to be discarded.  If we did the
1380     * kill() after setting the signal to be ignored, it is unspecified
1381     * (by POSIX) whether the signal is immediately discarded or left
1382     * pending, and in fact Linux would leave it pending, and so it
1383     * would be delivered after the current signal handler exits,
1384     * leading to an infinite loop.
1385     */
1386    sigaction(sig, &act, &oldact);
1387    sigaction(sig, &oldact, NULL);
1388}
1389
1390
1391/*
1392 * hup - Catch SIGHUP signal.
1393 *
1394 * Indicates that the physical layer has been disconnected.
1395 * We don't rely on this indication; if the user has sent this
1396 * signal, we just take the link down.
1397 */
1398static void
1399hup(sig)
1400    int sig;
1401{
1402    /* can't log a message here, it can deadlock */
1403    got_sighup = 1;
1404    if (conn_running)
1405	/* Send the signal to the [dis]connector process(es) also */
1406	kill_my_pg(sig);
1407    notify(sigreceived, sig);
1408    if (waiting)
1409	siglongjmp(sigjmp, 1);
1410}
1411
1412
1413/*
1414 * term - Catch SIGTERM signal and SIGINT signal (^C/del).
1415 *
1416 * Indicates that we should initiate a graceful disconnect and exit.
1417 */
1418/*ARGSUSED*/
1419static void
1420term(sig)
1421    int sig;
1422{
1423    /* can't log a message here, it can deadlock */
1424    got_sigterm = sig;
1425    if (conn_running)
1426	/* Send the signal to the [dis]connector process(es) also */
1427	kill_my_pg(sig);
1428    notify(sigreceived, sig);
1429    if (waiting)
1430	siglongjmp(sigjmp, 1);
1431}
1432
1433
1434/*
1435 * chld - Catch SIGCHLD signal.
1436 * Sets a flag so we will call reap_kids in the mainline.
1437 */
1438static void
1439chld(sig)
1440    int sig;
1441{
1442    got_sigchld = 1;
1443    if (waiting)
1444	siglongjmp(sigjmp, 1);
1445}
1446
1447
1448/*
1449 * toggle_debug - Catch SIGUSR1 signal.
1450 *
1451 * Toggle debug flag.
1452 */
1453/*ARGSUSED*/
1454static void
1455toggle_debug(sig)
1456    int sig;
1457{
1458    debug = !debug;
1459    if (debug) {
1460	setlogmask(LOG_UPTO(LOG_DEBUG));
1461    } else {
1462	setlogmask(LOG_UPTO(LOG_WARNING));
1463    }
1464}
1465
1466
1467/*
1468 * open_ccp - Catch SIGUSR2 signal.
1469 *
1470 * Try to (re)negotiate compression.
1471 */
1472/*ARGSUSED*/
1473static void
1474open_ccp(sig)
1475    int sig;
1476{
1477    got_sigusr2 = 1;
1478    if (waiting)
1479	siglongjmp(sigjmp, 1);
1480}
1481
1482
1483/*
1484 * bad_signal - We've caught a fatal signal.  Clean up state and exit.
1485 */
1486static void
1487bad_signal(sig)
1488    int sig;
1489{
1490    static int crashed = 0;
1491
1492    if (crashed)
1493	_exit(127);
1494    crashed = 1;
1495    error("Fatal signal %d", sig);
1496    if (conn_running)
1497	kill_my_pg(SIGTERM);
1498    notify(sigreceived, sig);
1499    die(127);
1500}
1501
1502/*
1503 * safe_fork - Create a child process.  The child closes all the
1504 * file descriptors that we don't want to leak to a script.
1505 * The parent waits for the child to do this before returning.
1506 * This also arranges for the specified fds to be dup'd to
1507 * fds 0, 1, 2 in the child.
1508 */
1509pid_t
1510safe_fork(int infd, int outfd, int errfd)
1511{
1512	pid_t pid;
1513	int fd, pipefd[2];
1514	char buf[1];
1515
1516	/* make sure fds 0, 1, 2 are occupied (probably not necessary) */
1517	while ((fd = dup(fd_devnull)) >= 0) {
1518		if (fd > 2) {
1519			close(fd);
1520			break;
1521		}
1522	}
1523
1524	if (pipe(pipefd) == -1)
1525		pipefd[0] = pipefd[1] = -1;
1526	pid = fork();
1527	if (pid < 0) {
1528		error("fork failed: %m");
1529		return -1;
1530	}
1531	if (pid > 0) {
1532		/* parent */
1533		close(pipefd[1]);
1534		/* this read() blocks until the close(pipefd[1]) below */
1535		complete_read(pipefd[0], buf, 1);
1536		close(pipefd[0]);
1537		return pid;
1538	}
1539
1540	/* Executing in the child */
1541	sys_close();
1542#ifdef USE_TDB
1543	tdb_close(pppdb);
1544#endif
1545
1546	/* make sure infd, outfd and errfd won't get tromped on below */
1547	if (infd == 1 || infd == 2)
1548		infd = dup(infd);
1549	if (outfd == 0 || outfd == 2)
1550		outfd = dup(outfd);
1551	if (errfd == 0 || errfd == 1)
1552		errfd = dup(errfd);
1553
1554	/* dup the in, out, err fds to 0, 1, 2 */
1555	if (infd != 0)
1556		dup2(infd, 0);
1557	if (outfd != 1)
1558		dup2(outfd, 1);
1559	if (errfd != 2)
1560		dup2(errfd, 2);
1561
1562#ifndef ANDROID_CHANGES
1563	closelog();
1564#endif
1565	if (log_to_fd > 2)
1566		close(log_to_fd);
1567	if (the_channel->close)
1568		(*the_channel->close)();
1569	else
1570		close(devfd);	/* some plugins don't have a close function */
1571	close(fd_ppp);
1572	close(fd_devnull);
1573	if (infd != 0)
1574		close(infd);
1575	if (outfd != 1)
1576		close(outfd);
1577	if (errfd != 2)
1578		close(errfd);
1579
1580	notify(fork_notifier, 0);
1581	close(pipefd[0]);
1582	/* this close unblocks the read() call above in the parent */
1583	close(pipefd[1]);
1584
1585	return 0;
1586}
1587
1588/*
1589 * device_script - run a program to talk to the specified fds
1590 * (e.g. to run the connector or disconnector script).
1591 * stderr gets connected to the log fd or to the _PATH_CONNERRS file.
1592 */
1593int
1594device_script(program, in, out, dont_wait)
1595    char *program;
1596    int in, out;
1597    int dont_wait;
1598{
1599    int pid;
1600    int status = -1;
1601    int errfd;
1602
1603    if (log_to_fd >= 0)
1604	errfd = log_to_fd;
1605    else
1606	errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600);
1607
1608    ++conn_running;
1609    pid = safe_fork(in, out, errfd);
1610
1611    if (pid != 0 && log_to_fd < 0)
1612	close(errfd);
1613
1614    if (pid < 0) {
1615	--conn_running;
1616	error("Failed to create child process: %m");
1617	return -1;
1618    }
1619
1620    if (pid != 0) {
1621	if (dont_wait) {
1622	    record_child(pid, program, NULL, NULL);
1623	    status = 0;
1624	} else {
1625	    while (waitpid(pid, &status, 0) < 0) {
1626		if (errno == EINTR)
1627		    continue;
1628		fatal("error waiting for (dis)connection process: %m");
1629	    }
1630	    --conn_running;
1631	}
1632	return (status == 0 ? 0 : -1);
1633    }
1634
1635    /* here we are executing in the child */
1636
1637    setgid(getgid());
1638    setuid(uid);
1639    if (getuid() != uid) {
1640	fprintf(stderr, "pppd: setuid failed\n");
1641	exit(1);
1642    }
1643    execl("/system/bin/sh", "sh", "-c", program, NULL);
1644    perror("pppd: could not exec /bin/sh");
1645    exit(99);
1646    /* NOTREACHED */
1647}
1648
1649
1650/*
1651 * run-program - execute a program with given arguments,
1652 * but don't wait for it.
1653 * If the program can't be executed, logs an error unless
1654 * must_exist is 0 and the program file doesn't exist.
1655 * Returns -1 if it couldn't fork, 0 if the file doesn't exist
1656 * or isn't an executable plain file, or the process ID of the child.
1657 * If done != NULL, (*done)(arg) will be called later (within
1658 * reap_kids) iff the return value is > 0.
1659 */
1660pid_t
1661run_program(prog, args, must_exist, done, arg)
1662    char *prog;
1663    char **args;
1664    int must_exist;
1665    void (*done) __P((void *));
1666    void *arg;
1667{
1668    int pid;
1669    struct stat sbuf;
1670
1671#ifdef ANDROID_CHANGES
1672    /* Originally linkname is used to create named pid files, which is
1673    * meaningless to android. Here we use it as a suffix of program names,
1674    * so different users can run their own program by specifying it. For
1675    * example, "/etc/ppp/ip-up-vpn" will be executed when IPCP is up and
1676    * linkname is "vpn". Note that "/" is not allowed for security reasons. */
1677    char file[MAXPATHLEN];
1678
1679    if (linkname[0] && !strchr(linkname, '/')) {
1680        snprintf(file, MAXPATHLEN, "%s-%s", prog, linkname);
1681        file[MAXPATHLEN - 1] = '\0';
1682        prog = file;
1683    }
1684#endif
1685
1686    /*
1687     * First check if the file exists and is executable.
1688     * We don't use access() because that would use the
1689     * real user-id, which might not be root, and the script
1690     * might be accessible only to root.
1691     */
1692    errno = EINVAL;
1693    if (stat(prog, &sbuf) < 0 || !S_ISREG(sbuf.st_mode)
1694	|| (sbuf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) {
1695	if (must_exist || errno != ENOENT)
1696	    warn("Can't execute %s: %m", prog);
1697	return 0;
1698    }
1699
1700    pid = safe_fork(fd_devnull, fd_devnull, fd_devnull);
1701    if (pid == -1) {
1702	error("Failed to create child process for %s: %m", prog);
1703	return -1;
1704    }
1705    if (pid != 0) {
1706	if (debug)
1707	    dbglog("Script %s started (pid %d)", prog, pid);
1708	record_child(pid, prog, done, arg);
1709	return pid;
1710    }
1711
1712    /* Leave the current location */
1713    (void) setsid();	/* No controlling tty. */
1714    (void) umask (S_IRWXG|S_IRWXO);
1715    (void) chdir ("/");	/* no current directory. */
1716    setuid(0);		/* set real UID = root */
1717    setgid(getegid());
1718
1719#ifdef BSD
1720    /* Force the priority back to zero if pppd is running higher. */
1721    if (setpriority (PRIO_PROCESS, 0, 0) < 0)
1722	warn("can't reset priority to 0: %m");
1723#endif
1724
1725    /* run the program */
1726    execve(prog, args, script_env);
1727    if (must_exist || errno != ENOENT) {
1728#ifndef ANDROID_CHANGES
1729	/* have to reopen the log, there's nowhere else
1730	   for the message to go. */
1731	reopen_log();
1732	syslog(LOG_ERR, "Can't execute %s: %m", prog);
1733	closelog();
1734#else
1735	error("Can't execute %s: %m", prog);
1736#endif
1737    }
1738    _exit(-1);
1739}
1740
1741
1742/*
1743 * record_child - add a child process to the list for reap_kids
1744 * to use.
1745 */
1746void
1747record_child(pid, prog, done, arg)
1748    int pid;
1749    char *prog;
1750    void (*done) __P((void *));
1751    void *arg;
1752{
1753    struct subprocess *chp;
1754
1755    ++n_children;
1756
1757    chp = (struct subprocess *) malloc(sizeof(struct subprocess));
1758    if (chp == NULL) {
1759	warn("losing track of %s process", prog);
1760    } else {
1761	chp->pid = pid;
1762	chp->prog = prog;
1763	chp->done = done;
1764	chp->arg = arg;
1765	chp->next = children;
1766	children = chp;
1767    }
1768}
1769
1770/*
1771 * childwait_end - we got fed up waiting for the child processes to
1772 * exit, send them all a SIGTERM.
1773 */
1774static void
1775childwait_end(arg)
1776    void *arg;
1777{
1778    struct subprocess *chp;
1779
1780    for (chp = children; chp != NULL; chp = chp->next) {
1781	if (debug)
1782	    dbglog("sending SIGTERM to process %d", chp->pid);
1783	kill(chp->pid, SIGTERM);
1784    }
1785    childwait_done = 1;
1786}
1787
1788/*
1789 * reap_kids - get status from any dead child processes,
1790 * and log a message for abnormal terminations.
1791 */
1792static int
1793reap_kids()
1794{
1795    int pid, status;
1796    struct subprocess *chp, **prevp;
1797
1798    if (n_children == 0)
1799	return 0;
1800    while ((pid = waitpid(-1, &status, WNOHANG)) != -1 && pid != 0) {
1801	for (prevp = &children; (chp = *prevp) != NULL; prevp = &chp->next) {
1802	    if (chp->pid == pid) {
1803		--n_children;
1804		*prevp = chp->next;
1805		break;
1806	    }
1807	}
1808	if (WIFSIGNALED(status)) {
1809	    warn("Child process %s (pid %d) terminated with signal %d",
1810		 (chp? chp->prog: "??"), pid, WTERMSIG(status));
1811	} else if (debug)
1812	    dbglog("Script %s finished (pid %d), status = 0x%x",
1813		   (chp? chp->prog: "??"), pid,
1814		   WIFEXITED(status) ? WEXITSTATUS(status) : status);
1815	if (chp && chp->done)
1816	    (*chp->done)(chp->arg);
1817	if (chp)
1818	    free(chp);
1819    }
1820    if (pid == -1) {
1821	if (errno == ECHILD)
1822	    return -1;
1823	if (errno != EINTR)
1824	    error("Error waiting for child process: %m");
1825    }
1826    return 0;
1827}
1828
1829/*
1830 * add_notifier - add a new function to be called when something happens.
1831 */
1832void
1833add_notifier(notif, func, arg)
1834    struct notifier **notif;
1835    notify_func func;
1836    void *arg;
1837{
1838    struct notifier *np;
1839
1840    np = malloc(sizeof(struct notifier));
1841    if (np == 0)
1842	novm("notifier struct");
1843    np->next = *notif;
1844    np->func = func;
1845    np->arg = arg;
1846    *notif = np;
1847}
1848
1849/*
1850 * remove_notifier - remove a function from the list of things to
1851 * be called when something happens.
1852 */
1853void
1854remove_notifier(notif, func, arg)
1855    struct notifier **notif;
1856    notify_func func;
1857    void *arg;
1858{
1859    struct notifier *np;
1860
1861    for (; (np = *notif) != 0; notif = &np->next) {
1862	if (np->func == func && np->arg == arg) {
1863	    *notif = np->next;
1864	    free(np);
1865	    break;
1866	}
1867    }
1868}
1869
1870/*
1871 * notify - call a set of functions registered with add_notifier.
1872 */
1873void
1874notify(notif, val)
1875    struct notifier *notif;
1876    int val;
1877{
1878    struct notifier *np;
1879
1880    while ((np = notif) != 0) {
1881	notif = np->next;
1882	(*np->func)(np->arg, val);
1883    }
1884}
1885
1886/*
1887 * novm - log an error message saying we ran out of memory, and die.
1888 */
1889void
1890novm(msg)
1891    char *msg;
1892{
1893    fatal("Virtual memory exhausted allocating %s\n", msg);
1894}
1895
1896/*
1897 * script_setenv - set an environment variable value to be used
1898 * for scripts that we run (e.g. ip-up, auth-up, etc.)
1899 */
1900void
1901script_setenv(var, value, iskey)
1902    char *var, *value;
1903    int iskey;
1904{
1905    size_t varl = strlen(var);
1906    size_t vl = varl + strlen(value) + 2;
1907    int i;
1908    char *p, *newstring;
1909
1910    newstring = (char *) malloc(vl+1);
1911    if (newstring == 0)
1912	return;
1913    *newstring++ = iskey;
1914    slprintf(newstring, vl, "%s=%s", var, value);
1915
1916    /* check if this variable is already set */
1917    if (script_env != 0) {
1918	for (i = 0; (p = script_env[i]) != 0; ++i) {
1919	    if (strncmp(p, var, varl) == 0 && p[varl] == '=') {
1920#ifdef USE_TDB
1921		if (p[-1] && pppdb != NULL)
1922		    delete_db_key(p);
1923#endif
1924		free(p-1);
1925		script_env[i] = newstring;
1926#ifdef USE_TDB
1927		if (iskey && pppdb != NULL)
1928		    add_db_key(newstring);
1929		update_db_entry();
1930#endif
1931		return;
1932	    }
1933	}
1934    } else {
1935	/* no space allocated for script env. ptrs. yet */
1936	i = 0;
1937	script_env = (char **) malloc(16 * sizeof(char *));
1938	if (script_env == 0)
1939	    return;
1940	s_env_nalloc = 16;
1941    }
1942
1943    /* reallocate script_env with more space if needed */
1944    if (i + 1 >= s_env_nalloc) {
1945	int new_n = i + 17;
1946	char **newenv = (char **) realloc((void *)script_env,
1947					  new_n * sizeof(char *));
1948	if (newenv == 0)
1949	    return;
1950	script_env = newenv;
1951	s_env_nalloc = new_n;
1952    }
1953
1954    script_env[i] = newstring;
1955    script_env[i+1] = 0;
1956
1957#ifdef USE_TDB
1958    if (pppdb != NULL) {
1959	if (iskey)
1960	    add_db_key(newstring);
1961	update_db_entry();
1962    }
1963#endif
1964}
1965
1966/*
1967 * script_unsetenv - remove a variable from the environment
1968 * for scripts.
1969 */
1970void
1971script_unsetenv(var)
1972    char *var;
1973{
1974    int vl = strlen(var);
1975    int i;
1976    char *p;
1977
1978    if (script_env == 0)
1979	return;
1980    for (i = 0; (p = script_env[i]) != 0; ++i) {
1981	if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
1982#ifdef USE_TDB
1983	    if (p[-1] && pppdb != NULL)
1984		delete_db_key(p);
1985#endif
1986	    free(p-1);
1987	    while ((script_env[i] = script_env[i+1]) != 0)
1988		++i;
1989	    break;
1990	}
1991    }
1992#ifdef USE_TDB
1993    if (pppdb != NULL)
1994	update_db_entry();
1995#endif
1996}
1997
1998/*
1999 * Any arbitrary string used as a key for locking the database.
2000 * It doesn't matter what it is as long as all pppds use the same string.
2001 */
2002#define PPPD_LOCK_KEY	"pppd lock"
2003
2004/*
2005 * lock_db - get an exclusive lock on the TDB database.
2006 * Used to ensure atomicity of various lookup/modify operations.
2007 */
2008void lock_db()
2009{
2010#ifdef USE_TDB
2011	TDB_DATA key;
2012
2013	key.dptr = PPPD_LOCK_KEY;
2014	key.dsize = strlen(key.dptr);
2015	tdb_chainlock(pppdb, key);
2016#endif
2017}
2018
2019/*
2020 * unlock_db - remove the exclusive lock obtained by lock_db.
2021 */
2022void unlock_db()
2023{
2024#ifdef USE_TDB
2025	TDB_DATA key;
2026
2027	key.dptr = PPPD_LOCK_KEY;
2028	key.dsize = strlen(key.dptr);
2029	tdb_chainunlock(pppdb, key);
2030#endif
2031}
2032
2033#ifdef USE_TDB
2034/*
2035 * update_db_entry - update our entry in the database.
2036 */
2037static void
2038update_db_entry()
2039{
2040    TDB_DATA key, dbuf;
2041    int vlen, i;
2042    char *p, *q, *vbuf;
2043
2044    if (script_env == NULL)
2045	return;
2046    vlen = 0;
2047    for (i = 0; (p = script_env[i]) != 0; ++i)
2048	vlen += strlen(p) + 1;
2049    vbuf = malloc(vlen + 1);
2050    if (vbuf == 0)
2051	novm("database entry");
2052    q = vbuf;
2053    for (i = 0; (p = script_env[i]) != 0; ++i)
2054	q += slprintf(q, vbuf + vlen - q, "%s;", p);
2055
2056    key.dptr = db_key;
2057    key.dsize = strlen(db_key);
2058    dbuf.dptr = vbuf;
2059    dbuf.dsize = vlen;
2060    if (tdb_store(pppdb, key, dbuf, TDB_REPLACE))
2061	error("tdb_store failed: %s", tdb_error(pppdb));
2062
2063    if (vbuf)
2064        free(vbuf);
2065
2066}
2067
2068/*
2069 * add_db_key - add a key that we can use to look up our database entry.
2070 */
2071static void
2072add_db_key(str)
2073    const char *str;
2074{
2075    TDB_DATA key, dbuf;
2076
2077    key.dptr = (char *) str;
2078    key.dsize = strlen(str);
2079    dbuf.dptr = db_key;
2080    dbuf.dsize = strlen(db_key);
2081    if (tdb_store(pppdb, key, dbuf, TDB_REPLACE))
2082	error("tdb_store key failed: %s", tdb_error(pppdb));
2083}
2084
2085/*
2086 * delete_db_key - delete a key for looking up our database entry.
2087 */
2088static void
2089delete_db_key(str)
2090    const char *str;
2091{
2092    TDB_DATA key;
2093
2094    key.dptr = (char *) str;
2095    key.dsize = strlen(str);
2096    tdb_delete(pppdb, key);
2097}
2098
2099/*
2100 * cleanup_db - delete all the entries we put in the database.
2101 */
2102static void
2103cleanup_db()
2104{
2105    TDB_DATA key;
2106    int i;
2107    char *p;
2108
2109    key.dptr = db_key;
2110    key.dsize = strlen(db_key);
2111    tdb_delete(pppdb, key);
2112    for (i = 0; (p = script_env[i]) != 0; ++i)
2113	if (p[-1])
2114	    delete_db_key(p);
2115}
2116#endif /* USE_TDB */
2117