17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * main.c - Point-to-Point Protocol main module 37c478bd9Sstevel@tonic-gate * 4*f53eecf5SJames Carlson * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 57c478bd9Sstevel@tonic-gate * Use is subject to license terms. 67c478bd9Sstevel@tonic-gate * 77c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software and its 87c478bd9Sstevel@tonic-gate * documentation is hereby granted, provided that the above copyright 97c478bd9Sstevel@tonic-gate * notice appears in all copies. 107c478bd9Sstevel@tonic-gate * 117c478bd9Sstevel@tonic-gate * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF 127c478bd9Sstevel@tonic-gate * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 137c478bd9Sstevel@tonic-gate * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 147c478bd9Sstevel@tonic-gate * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR 157c478bd9Sstevel@tonic-gate * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR 167c478bd9Sstevel@tonic-gate * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES 177c478bd9Sstevel@tonic-gate * 187c478bd9Sstevel@tonic-gate * Copyright (c) 1989 Carnegie Mellon University. 197c478bd9Sstevel@tonic-gate * All rights reserved. 207c478bd9Sstevel@tonic-gate * 217c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted 227c478bd9Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are 237c478bd9Sstevel@tonic-gate * duplicated in all such forms and that any documentation, 247c478bd9Sstevel@tonic-gate * advertising materials, and other materials related to such 257c478bd9Sstevel@tonic-gate * distribution and use acknowledge that the software was developed 267c478bd9Sstevel@tonic-gate * by Carnegie Mellon University. The name of the 277c478bd9Sstevel@tonic-gate * University may not be used to endorse or promote products derived 287c478bd9Sstevel@tonic-gate * from this software without specific prior written permission. 297c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 307c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 317c478bd9Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #define RCSID "$Id: main.c,v 1.97 2000/04/24 02:54:16 masputra Exp $" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <string.h> 397c478bd9Sstevel@tonic-gate #include <unistd.h> 407c478bd9Sstevel@tonic-gate #include <signal.h> 417c478bd9Sstevel@tonic-gate #include <errno.h> 427c478bd9Sstevel@tonic-gate #include <fcntl.h> 437c478bd9Sstevel@tonic-gate #include <syslog.h> 447c478bd9Sstevel@tonic-gate #include <netdb.h> 457c478bd9Sstevel@tonic-gate #include <pwd.h> 467c478bd9Sstevel@tonic-gate #include <setjmp.h> 477c478bd9Sstevel@tonic-gate #include <sys/param.h> 487c478bd9Sstevel@tonic-gate #include <sys/types.h> 497c478bd9Sstevel@tonic-gate #include <sys/wait.h> 507c478bd9Sstevel@tonic-gate #include <sys/time.h> 517c478bd9Sstevel@tonic-gate #include <sys/resource.h> 527c478bd9Sstevel@tonic-gate #include <sys/stat.h> 537c478bd9Sstevel@tonic-gate #include <sys/socket.h> 547c478bd9Sstevel@tonic-gate #include <netinet/in.h> 557c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include "pppd.h" 587c478bd9Sstevel@tonic-gate #include "magic.h" 597c478bd9Sstevel@tonic-gate #include "fsm.h" 607c478bd9Sstevel@tonic-gate #include "lcp.h" 617c478bd9Sstevel@tonic-gate #include "ipcp.h" 627c478bd9Sstevel@tonic-gate #ifdef INET6 637c478bd9Sstevel@tonic-gate #include "ipv6cp.h" 647c478bd9Sstevel@tonic-gate #endif 657c478bd9Sstevel@tonic-gate #include "upap.h" 667c478bd9Sstevel@tonic-gate #include "chap.h" 677c478bd9Sstevel@tonic-gate #include "ccp.h" 687c478bd9Sstevel@tonic-gate #include "pathnames.h" 697c478bd9Sstevel@tonic-gate #include "patchlevel.h" 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 727c478bd9Sstevel@tonic-gate #include "tdb.h" 737c478bd9Sstevel@tonic-gate #endif 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate #ifdef CBCP_SUPPORT 767c478bd9Sstevel@tonic-gate #include "cbcp.h" 777c478bd9Sstevel@tonic-gate #endif 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #ifdef IPX_CHANGE 807c478bd9Sstevel@tonic-gate #include "ipxcp.h" 817c478bd9Sstevel@tonic-gate #endif /* IPX_CHANGE */ 827c478bd9Sstevel@tonic-gate #ifdef AT_CHANGE 837c478bd9Sstevel@tonic-gate #include "atcp.h" 847c478bd9Sstevel@tonic-gate #endif 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate #if !defined(lint) && !defined(_lint) 877c478bd9Sstevel@tonic-gate static const char rcsid[] = RCSID; 887c478bd9Sstevel@tonic-gate #endif 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* interface vars */ 917c478bd9Sstevel@tonic-gate char ifname[32]; /* Interface name */ 927c478bd9Sstevel@tonic-gate int ifunit = -1; /* Interface unit number */ 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate char *progname; /* Name of this program */ 957c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN+1]; /* Our hostname */ 967c478bd9Sstevel@tonic-gate static char pidfilename[MAXPATHLEN]; /* name of pid file */ 977c478bd9Sstevel@tonic-gate static char linkpidfile[MAXPATHLEN]; /* name of linkname pid file */ 987c478bd9Sstevel@tonic-gate char ppp_devnam[MAXPATHLEN]; /* name of PPP tty (maybe ttypx) */ 997c478bd9Sstevel@tonic-gate static uid_t uid; /* Our real user-id */ 1007c478bd9Sstevel@tonic-gate static int conn_running; /* we have a [dis]connector running */ 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate int ttyfd; /* Serial port file descriptor */ 1037c478bd9Sstevel@tonic-gate mode_t tty_mode = (mode_t)-1; /* Original access permissions to tty */ 1047c478bd9Sstevel@tonic-gate int baud_rate; /* Actual bits/second for serial device */ 1057c478bd9Sstevel@tonic-gate bool hungup; /* terminal has been hung up */ 1067c478bd9Sstevel@tonic-gate bool privileged; /* we're running as real uid root */ 1077c478bd9Sstevel@tonic-gate bool need_holdoff; /* need holdoff period before restarting */ 1087c478bd9Sstevel@tonic-gate bool detached; /* have detached from terminal */ 1097c478bd9Sstevel@tonic-gate struct stat devstat; /* result of stat() on devnam */ 1107c478bd9Sstevel@tonic-gate bool prepass = 0; /* doing prepass to find device name */ 1117c478bd9Sstevel@tonic-gate int devnam_fixed; /* set while in options.ttyxx file */ 1127c478bd9Sstevel@tonic-gate volatile int status; /* exit status for pppd */ 1137c478bd9Sstevel@tonic-gate int unsuccess; /* # unsuccessful connection attempts */ 1147c478bd9Sstevel@tonic-gate int do_callback; /* != 0 if we should do callback next */ 1157c478bd9Sstevel@tonic-gate int doing_callback; /* != 0 if we are doing callback */ 1167c478bd9Sstevel@tonic-gate char *callback_script; /* script for doing callback */ 1177c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 1187c478bd9Sstevel@tonic-gate TDB_CONTEXT *pppdb; /* database for storing status etc. */ 1197c478bd9Sstevel@tonic-gate char db_key[32]; 1207c478bd9Sstevel@tonic-gate #endif 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* 1237c478bd9Sstevel@tonic-gate * For plug-in usage: 1247c478bd9Sstevel@tonic-gate * 1257c478bd9Sstevel@tonic-gate * holdoff_hook - Can be used to change the demand-dial hold-off 1267c478bd9Sstevel@tonic-gate * time dynamically. This is normally set by the 1277c478bd9Sstevel@tonic-gate * "holdoff" option, and is 30 seconds by default. 1287c478bd9Sstevel@tonic-gate * 1297c478bd9Sstevel@tonic-gate * new_phase_hook - This is called for each change in the PPP 1307c478bd9Sstevel@tonic-gate * phase (per RFC 1661). This can be used to log 1317c478bd9Sstevel@tonic-gate * progress. 1327c478bd9Sstevel@tonic-gate * 1337c478bd9Sstevel@tonic-gate * check_options_hook - This is called before doing sys_init() 1347c478bd9Sstevel@tonic-gate * and allows the plugin to verify the selected options. 1357c478bd9Sstevel@tonic-gate * 1367c478bd9Sstevel@tonic-gate * updown_script_hook - This is called with the proposed 1377c478bd9Sstevel@tonic-gate * command-line arguments for any of the 1387c478bd9Sstevel@tonic-gate * /etc/ppp/{ip,ipv6,ipx,auth}-{up,down} scripts before 1397c478bd9Sstevel@tonic-gate * fork/exec. It can be used to add or change arguments. 1407c478bd9Sstevel@tonic-gate * 1417c478bd9Sstevel@tonic-gate * device_pipe_hook - If this is set, then an extra fd (3) is 1427c478bd9Sstevel@tonic-gate * passed to the connect/disconnect script. This extra 1437c478bd9Sstevel@tonic-gate * fd is the write side of a pipe, and the read side is 1447c478bd9Sstevel@tonic-gate * passed to this routine. This can be used to pass 1457c478bd9Sstevel@tonic-gate * arbitrary data from the script back to pppd. 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate int (*holdoff_hook) __P((void)) = NULL; 1487c478bd9Sstevel@tonic-gate int (*new_phase_hook) __P((int new, int old)) = NULL; 1497c478bd9Sstevel@tonic-gate int (*check_options_hook) __P((uid_t uid)) = NULL; 1507c478bd9Sstevel@tonic-gate int (*updown_script_hook) __P((const char ***argsp)) = NULL; 1517c478bd9Sstevel@tonic-gate void (*device_pipe_hook) __P((int pipefd)) = NULL; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate static int fd_ppp = -1; /* fd for talking PPP */ 1547c478bd9Sstevel@tonic-gate static int fd_loop; /* fd for getting demand-dial packets */ 1557c478bd9Sstevel@tonic-gate static int pty_master; /* fd for master side of pty */ 1567c478bd9Sstevel@tonic-gate int pty_slave = -1; /* fd for slave side of pty */ 1577c478bd9Sstevel@tonic-gate static int real_ttyfd; /* fd for actual serial port (not pty) */ 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate int phase; /* where the link is at */ 1607c478bd9Sstevel@tonic-gate int kill_link; 1617c478bd9Sstevel@tonic-gate int open_ccp_flag; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate static int waiting; /* for input from peer or timer expiration */ 1647c478bd9Sstevel@tonic-gate static sigjmp_buf sigjmp; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate char **script_env; /* Env. variable values for scripts */ 1677c478bd9Sstevel@tonic-gate int s_env_nalloc; /* # words avail at script_env */ 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate u_char outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for outgoing packet */ 1707c478bd9Sstevel@tonic-gate u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */ 1717c478bd9Sstevel@tonic-gate u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */ 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate static int n_children; /* # child processes still running */ 1747c478bd9Sstevel@tonic-gate static bool got_sigchld; /* set if we have received a SIGCHLD */ 1757c478bd9Sstevel@tonic-gate static sigset_t main_sigmask; /* signals blocked while dispatching */ 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate static bool locked; /* lock() has succeeded */ 1787c478bd9Sstevel@tonic-gate static bool privopen; /* don't lock, open device as root */ 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n"; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate GIDSET_TYPE groups[NGROUPS_MAX];/* groups the user is in */ 1837c478bd9Sstevel@tonic-gate int ngroups; /* How many groups valid in groups */ 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate static struct timeval start_time; /* Time when link was started. */ 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate struct pppd_stats link_stats; 1887c478bd9Sstevel@tonic-gate int link_connect_time; 1897c478bd9Sstevel@tonic-gate bool link_stats_valid; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate static pid_t charshunt_pid; /* Process ID for charshunt */ 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate extern option_t general_options[]; 1947c478bd9Sstevel@tonic-gate extern option_t auth_options[]; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * We maintain a list of child process pids and 1987c478bd9Sstevel@tonic-gate * functions to call when they exit. 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate struct subprocess { 2017c478bd9Sstevel@tonic-gate pid_t pid; 2027c478bd9Sstevel@tonic-gate char *prog; 2037c478bd9Sstevel@tonic-gate void (*done) __P((void *, int)); 2047c478bd9Sstevel@tonic-gate void *arg; 2057c478bd9Sstevel@tonic-gate struct subprocess *next; 2067c478bd9Sstevel@tonic-gate }; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate static struct subprocess *children; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate /* Prototypes for procedures local to this file. */ 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate static void setup_signals __P((void)); 2137c478bd9Sstevel@tonic-gate static void create_pidfile __P((void)); 2147c478bd9Sstevel@tonic-gate static void create_linkpidfile __P((void)); 2157c478bd9Sstevel@tonic-gate static void cleanup __P((void)); 2167c478bd9Sstevel@tonic-gate static void close_tty __P((void)); 2177c478bd9Sstevel@tonic-gate static void get_input __P((void)); 2187c478bd9Sstevel@tonic-gate static void calltimeout __P((void)); 2197c478bd9Sstevel@tonic-gate static struct timeval *timeleft __P((struct timeval *)); 2207c478bd9Sstevel@tonic-gate static void kill_my_pg __P((int)); 2217c478bd9Sstevel@tonic-gate static void hup __P((int)); 2227c478bd9Sstevel@tonic-gate static void term __P((int)); 2237c478bd9Sstevel@tonic-gate static void chld __P((int)); 2247c478bd9Sstevel@tonic-gate static void toggle_debug __P((int)); 2257c478bd9Sstevel@tonic-gate static void open_ccp __P((int)); 2267c478bd9Sstevel@tonic-gate static void bad_signal __P((int)); 2277c478bd9Sstevel@tonic-gate static void holdoff_end __P((void *)); 2287c478bd9Sstevel@tonic-gate static int device_script __P((char *, int, int, int, char *)); 2297c478bd9Sstevel@tonic-gate static int reap_kids __P((int waitfor)); 2307c478bd9Sstevel@tonic-gate static void record_child __P((pid_t, char *, void (*) (void *, int), void *)); 2317c478bd9Sstevel@tonic-gate static int open_socket __P((char *)); 2327c478bd9Sstevel@tonic-gate static int start_charshunt __P((int, int)); 2337c478bd9Sstevel@tonic-gate static void charshunt_done __P((void *, int)); 2347c478bd9Sstevel@tonic-gate static void charshunt __P((int, int, char *)); 2357c478bd9Sstevel@tonic-gate static int record_write __P((FILE *, int code, u_char *buf, int nb, 2367c478bd9Sstevel@tonic-gate struct timeval *)); 2377c478bd9Sstevel@tonic-gate static void final_reap __P((void)); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 2407c478bd9Sstevel@tonic-gate static void update_db_entry __P((void)); 2417c478bd9Sstevel@tonic-gate static void add_db_key __P((const char *)); 2427c478bd9Sstevel@tonic-gate static void delete_db_key __P((const char *)); 2437c478bd9Sstevel@tonic-gate static void cleanup_db __P((void)); 2447c478bd9Sstevel@tonic-gate #endif 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate int main __P((int, char *[])); 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate #ifdef ultrix 2497c478bd9Sstevel@tonic-gate #undef O_NONBLOCK 2507c478bd9Sstevel@tonic-gate #define O_NONBLOCK O_NDELAY 2517c478bd9Sstevel@tonic-gate #endif 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate #ifdef ULTRIX 2547c478bd9Sstevel@tonic-gate #define setlogmask(x) 0 2557c478bd9Sstevel@tonic-gate #endif 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate /* Backward compatibility for Linux */ 2587c478bd9Sstevel@tonic-gate #ifndef RECMARK_TIMESTART 2597c478bd9Sstevel@tonic-gate #define RECMARK_STARTSEND 1 2607c478bd9Sstevel@tonic-gate #define RECMARK_STARTRECV 2 2617c478bd9Sstevel@tonic-gate #define RECMARK_ENDSEND 3 2627c478bd9Sstevel@tonic-gate #define RECMARK_ENDRECV 4 2637c478bd9Sstevel@tonic-gate #define RECMARK_TIMEDELTA32 5 2647c478bd9Sstevel@tonic-gate #define RECMARK_TIMEDELTA8 6 2657c478bd9Sstevel@tonic-gate #define RECMARK_TIMESTART 7 2667c478bd9Sstevel@tonic-gate #endif 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /* 2697c478bd9Sstevel@tonic-gate * PPP Data Link Layer "protocol" table. 2707c478bd9Sstevel@tonic-gate * One entry per supported protocol. 2717c478bd9Sstevel@tonic-gate * The last entry must be NULL. 2727c478bd9Sstevel@tonic-gate */ 2737c478bd9Sstevel@tonic-gate struct protent *protocols[] = { 2747c478bd9Sstevel@tonic-gate &lcp_protent, 2757c478bd9Sstevel@tonic-gate &pap_protent, 2767c478bd9Sstevel@tonic-gate &chap_protent, 2777c478bd9Sstevel@tonic-gate #ifdef CBCP_SUPPORT 2787c478bd9Sstevel@tonic-gate &cbcp_protent, 2797c478bd9Sstevel@tonic-gate #endif 2807c478bd9Sstevel@tonic-gate &ipcp_protent, 2817c478bd9Sstevel@tonic-gate #ifdef INET6 2827c478bd9Sstevel@tonic-gate &ipv6cp_protent, 2837c478bd9Sstevel@tonic-gate #endif 2847c478bd9Sstevel@tonic-gate &ccp_protent, 2857c478bd9Sstevel@tonic-gate #ifdef IPX_CHANGE 2867c478bd9Sstevel@tonic-gate &ipxcp_protent, 2877c478bd9Sstevel@tonic-gate #endif 2887c478bd9Sstevel@tonic-gate #ifdef AT_CHANGE 2897c478bd9Sstevel@tonic-gate &atcp_protent, 2907c478bd9Sstevel@tonic-gate #endif 2917c478bd9Sstevel@tonic-gate NULL 2927c478bd9Sstevel@tonic-gate }; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate int 2957c478bd9Sstevel@tonic-gate main(argc, argv) 2967c478bd9Sstevel@tonic-gate int argc; 2977c478bd9Sstevel@tonic-gate char *argv[]; 2987c478bd9Sstevel@tonic-gate { 2997c478bd9Sstevel@tonic-gate int i, fdflags, t; 3007c478bd9Sstevel@tonic-gate char *p, *connector; 3017c478bd9Sstevel@tonic-gate struct passwd *pw; 3027c478bd9Sstevel@tonic-gate struct timeval timo; 3037c478bd9Sstevel@tonic-gate struct protent *protp; 3047c478bd9Sstevel@tonic-gate struct stat statbuf; 3057c478bd9Sstevel@tonic-gate char numbuf[16]; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate ifname[0] = '\0'; 3087c478bd9Sstevel@tonic-gate new_phase(PHASE_INITIALIZE); 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate /* 3117c478bd9Sstevel@tonic-gate * Ensure that fds 0, 1, 2 are open, to /dev/null if nowhere else. 3127c478bd9Sstevel@tonic-gate * This way we can close 0, 1, 2 in detach() without clobbering 3137c478bd9Sstevel@tonic-gate * a fd that we are using. 3147c478bd9Sstevel@tonic-gate */ 3157c478bd9Sstevel@tonic-gate if ((i = open(_PATH_DEVNULL, O_RDWR)) >= 0) { 3167c478bd9Sstevel@tonic-gate while (0 <= i && i <= 2) 3177c478bd9Sstevel@tonic-gate i = dup(i); 3187c478bd9Sstevel@tonic-gate if (i >= 0) 3197c478bd9Sstevel@tonic-gate (void) close(i); 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate script_env = NULL; 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate /* Initialize syslog facilities */ 3257c478bd9Sstevel@tonic-gate reopen_log(); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate if (gethostname(hostname, MAXHOSTNAMELEN+1) < 0 ) { 3287c478bd9Sstevel@tonic-gate option_error("Couldn't get hostname: %m"); 3297c478bd9Sstevel@tonic-gate exit(1); 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate hostname[MAXHOSTNAMELEN] = '\0'; 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* make sure we don't create world or group writable files. */ 3347c478bd9Sstevel@tonic-gate (void) umask(umask(0777) | 022); 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate uid = getuid(); 3377c478bd9Sstevel@tonic-gate privileged = (uid == 0); 3387c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", uid); 3397c478bd9Sstevel@tonic-gate script_setenv("ORIG_UID", numbuf, 0); 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate ngroups = getgroups(NGROUPS_MAX, groups); 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate /* 3447c478bd9Sstevel@tonic-gate * Initialize magic number generator now so that protocols may 3457c478bd9Sstevel@tonic-gate * use magic numbers in initialization. 3467c478bd9Sstevel@tonic-gate */ 3477c478bd9Sstevel@tonic-gate magic_init(); 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate progname = *argv; 3507c478bd9Sstevel@tonic-gate prepass = 0; 3517c478bd9Sstevel@tonic-gate /* 3527c478bd9Sstevel@tonic-gate * Initialize to the standard option set, then parse, in order, the 3537c478bd9Sstevel@tonic-gate * system options file, the user's options file, the tty's options file, 3547c478bd9Sstevel@tonic-gate * and the command line arguments. At last, install the options declared 3557c478bd9Sstevel@tonic-gate * by each protocol into the extra_option list. 3567c478bd9Sstevel@tonic-gate */ 3577c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) { 3587c478bd9Sstevel@tonic-gate (*protp->init)(0); 3597c478bd9Sstevel@tonic-gate if (protp->options != NULL) { 3607c478bd9Sstevel@tonic-gate add_options(protp->options); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate /* 3657c478bd9Sstevel@tonic-gate * Install "generic" options into the extra_options list. 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate add_options(auth_options); 3687c478bd9Sstevel@tonic-gate add_options(general_options); 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate /* Install any system-specific options (or remove unusable ones) */ 3717c478bd9Sstevel@tonic-gate sys_options(); 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1) 3747c478bd9Sstevel@tonic-gate || !options_from_user()) 3757c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate /* scan command line and options files to find device name */ 3787c478bd9Sstevel@tonic-gate prepass = 1; 3797c478bd9Sstevel@tonic-gate (void) parse_args(argc-1, argv+1); 3807c478bd9Sstevel@tonic-gate prepass = 0; 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate /* 3837c478bd9Sstevel@tonic-gate * Work out the device name, if it hasn't already been specified. 3847c478bd9Sstevel@tonic-gate */ 3857c478bd9Sstevel@tonic-gate using_pty = notty || ptycommand != NULL || pty_socket != NULL; 3867c478bd9Sstevel@tonic-gate if (!using_pty && default_device && !direct_tty) { 3877c478bd9Sstevel@tonic-gate char *p; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate if (!isatty(0) || (p = ttyname(0)) == NULL) { 3907c478bd9Sstevel@tonic-gate option_error("no device specified and stdin is not a tty"); 3917c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate (void) strlcpy(devnam, p, sizeof(devnam)); 3947c478bd9Sstevel@tonic-gate if (stat(devnam, &devstat) < 0) 3957c478bd9Sstevel@tonic-gate fatal("Couldn't stat default device %s: %m", devnam); 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate /* 3997c478bd9Sstevel@tonic-gate * Parse the tty options file and the command line. 4007c478bd9Sstevel@tonic-gate * The per-tty options file should not change 4017c478bd9Sstevel@tonic-gate * ptycommand, pty_socket, notty or devnam. 4027c478bd9Sstevel@tonic-gate */ 4037c478bd9Sstevel@tonic-gate devnam_fixed = 1; 4047c478bd9Sstevel@tonic-gate if (!using_pty && !direct_tty) { 4057c478bd9Sstevel@tonic-gate if (!options_for_tty()) 4067c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate devnam_fixed = 0; 4107c478bd9Sstevel@tonic-gate if (!parse_args(argc-1, argv+1)) 4117c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate /* 4147c478bd9Sstevel@tonic-gate * Check that we are running as root. 4157c478bd9Sstevel@tonic-gate */ 4167c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 4177c478bd9Sstevel@tonic-gate option_error("must be root to run %s, since it is not setuid-root", 4187c478bd9Sstevel@tonic-gate argv[0]); 4197c478bd9Sstevel@tonic-gate exit(EXIT_NOT_ROOT); 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate if (!ppp_available()) { 4237c478bd9Sstevel@tonic-gate option_error(no_ppp_msg); 4247c478bd9Sstevel@tonic-gate exit(EXIT_NO_KERNEL_SUPPORT); 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate /* 4287c478bd9Sstevel@tonic-gate * Check that the options given are valid and consistent. 4297c478bd9Sstevel@tonic-gate */ 4307c478bd9Sstevel@tonic-gate if (!sys_check_options()) 4317c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4327c478bd9Sstevel@tonic-gate auth_check_options(); 4337c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 4347c478bd9Sstevel@tonic-gate mp_check_options(); 4357c478bd9Sstevel@tonic-gate #endif 4367c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) 4377c478bd9Sstevel@tonic-gate if (protp->enabled_flag && protp->check_options != NULL) 4387c478bd9Sstevel@tonic-gate (*protp->check_options)(); 4397c478bd9Sstevel@tonic-gate if (demand && (connect_script == NULL)) { 4407c478bd9Sstevel@tonic-gate option_error("connect script is required for demand-dialling\n"); 4417c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate if (updetach && (nodetach || demand)) { 4447c478bd9Sstevel@tonic-gate option_error("updetach cannot be used with %s", 4457c478bd9Sstevel@tonic-gate nodetach ? "nodetach" : "demand"); 4467c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate /* default holdoff to 0 if no connect script has been given */ 4497c478bd9Sstevel@tonic-gate if ((connect_script == NULL) && !holdoff_specified) 4507c478bd9Sstevel@tonic-gate holdoff = 0; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate if (using_pty || direct_tty) { 4537c478bd9Sstevel@tonic-gate if (!default_device) { 4547c478bd9Sstevel@tonic-gate option_error("%s option precludes specifying device name", 4557c478bd9Sstevel@tonic-gate notty? "notty": "pty"); 4567c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate if (ptycommand != NULL && (notty || direct_tty)) { 4597c478bd9Sstevel@tonic-gate option_error("pty option is incompatible with notty option"); 4607c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate if (pty_socket != NULL && (ptycommand != NULL || notty || 4637c478bd9Sstevel@tonic-gate direct_tty)) { 4647c478bd9Sstevel@tonic-gate option_error("socket option is incompatible with pty and notty"); 4657c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate default_device = notty || direct_tty; 4687c478bd9Sstevel@tonic-gate lockflag = 0; 4697c478bd9Sstevel@tonic-gate modem = 0; 4707c478bd9Sstevel@tonic-gate if (default_device && log_to_fd <= 1) 4717c478bd9Sstevel@tonic-gate log_to_fd = -1; 4727c478bd9Sstevel@tonic-gate } else { 4737c478bd9Sstevel@tonic-gate /* 4747c478bd9Sstevel@tonic-gate * If the user has specified a device which is the same as 4757c478bd9Sstevel@tonic-gate * the one on stdin, pretend they didn't specify any. 4767c478bd9Sstevel@tonic-gate * If the device is already open read/write on stdin, 4777c478bd9Sstevel@tonic-gate * we assume we don't need to lock it, and we can open it as root. 4787c478bd9Sstevel@tonic-gate */ 4797c478bd9Sstevel@tonic-gate if (fstat(0, &statbuf) >= 0 && S_ISCHR(statbuf.st_mode) 4807c478bd9Sstevel@tonic-gate && statbuf.st_rdev == devstat.st_rdev) { 4817c478bd9Sstevel@tonic-gate default_device = 1; 4827c478bd9Sstevel@tonic-gate fdflags = fcntl(0, F_GETFL); 4837c478bd9Sstevel@tonic-gate if (fdflags != -1 && (fdflags & O_ACCMODE) == O_RDWR) 4847c478bd9Sstevel@tonic-gate privopen = 1; 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate if (default_device) 4887c478bd9Sstevel@tonic-gate nodetach = 1; 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate /* 4917c478bd9Sstevel@tonic-gate * Don't send log messages to the serial port, it tends to 4927c478bd9Sstevel@tonic-gate * confuse the peer. :-) 4937c478bd9Sstevel@tonic-gate */ 4947c478bd9Sstevel@tonic-gate if (log_to_fd >= 0 && fstat(log_to_fd, &statbuf) >= 0 4957c478bd9Sstevel@tonic-gate && S_ISCHR(statbuf.st_mode) && statbuf.st_rdev == devstat.st_rdev) 4967c478bd9Sstevel@tonic-gate log_to_fd = -1; 4977c478bd9Sstevel@tonic-gate early_log = 0; 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate if (debug) 5007c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_DEBUG)); 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate /* 5037c478bd9Sstevel@tonic-gate * Initialize system-dependent stuff. 5047c478bd9Sstevel@tonic-gate */ 5057c478bd9Sstevel@tonic-gate if (check_options_hook != NULL && 5067c478bd9Sstevel@tonic-gate (*check_options_hook)(uid) == -1) { 5077c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate sys_init(!devnam_info.priv && !privopen); 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 5127c478bd9Sstevel@tonic-gate pppdb = tdb_open(_PATH_PPPDB, 0, 0, O_RDWR|O_CREAT, 0644); 5137c478bd9Sstevel@tonic-gate if (pppdb != NULL) { 5147c478bd9Sstevel@tonic-gate (void) slprintf(db_key, sizeof(db_key), "pppd%d", getpid()); 5157c478bd9Sstevel@tonic-gate update_db_entry(); 5167c478bd9Sstevel@tonic-gate } else { 5177c478bd9Sstevel@tonic-gate warn("Warning: couldn't open ppp database %s", _PATH_PPPDB); 5187c478bd9Sstevel@tonic-gate if (multilink) { 5197c478bd9Sstevel@tonic-gate warn("Warning: disabling multilink"); 5207c478bd9Sstevel@tonic-gate multilink = 0; 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate #endif 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate /* 5267c478bd9Sstevel@tonic-gate * Detach ourselves from the terminal, if required, and identify 5277c478bd9Sstevel@tonic-gate * who is running us. Printing to stderr stops here unless 5287c478bd9Sstevel@tonic-gate * nodetach or updetach is set. 5297c478bd9Sstevel@tonic-gate */ 5307c478bd9Sstevel@tonic-gate if (!nodetach && !updetach) 5317c478bd9Sstevel@tonic-gate detach(); 5327c478bd9Sstevel@tonic-gate p = getlogin(); 5337c478bd9Sstevel@tonic-gate if (p == NULL) { 5347c478bd9Sstevel@tonic-gate pw = getpwuid(uid); 5357c478bd9Sstevel@tonic-gate if (pw != NULL && pw->pw_name != NULL) 5367c478bd9Sstevel@tonic-gate p = pw->pw_name; 5377c478bd9Sstevel@tonic-gate else 5387c478bd9Sstevel@tonic-gate p = "(unknown)"; 5397c478bd9Sstevel@tonic-gate } 5407c478bd9Sstevel@tonic-gate syslog(LOG_NOTICE, "pppd %s.%d%s started by %s, uid %d", 5417c478bd9Sstevel@tonic-gate VERSION, PATCHLEVEL, IMPLEMENTATION, p, uid); 5427c478bd9Sstevel@tonic-gate script_setenv("PPPLOGNAME", p, 0); 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate if (devnam[0] != '\0') 5457c478bd9Sstevel@tonic-gate script_setenv("DEVICE", devnam, 1); 5467c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", getpid()); 5477c478bd9Sstevel@tonic-gate script_setenv("PPPD_PID", numbuf, 1); 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate setup_signals(); 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate waiting = 0; 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate create_linkpidfile(); 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate /* 5567c478bd9Sstevel@tonic-gate * If we're doing dial-on-demand, set up the interface now. 5577c478bd9Sstevel@tonic-gate */ 5587c478bd9Sstevel@tonic-gate if (demand) { 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * Open the loopback channel and set it up to be the ppp interface. 5617c478bd9Sstevel@tonic-gate */ 5627c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 5637c478bd9Sstevel@tonic-gate (void) tdb_writelock(pppdb); 5647c478bd9Sstevel@tonic-gate #endif 5657c478bd9Sstevel@tonic-gate set_ifunit(1); 5667c478bd9Sstevel@tonic-gate fd_loop = open_ppp_loopback(); 5677c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 5687c478bd9Sstevel@tonic-gate (void) tdb_writeunlock(pppdb); 5697c478bd9Sstevel@tonic-gate #endif 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate /* 5727c478bd9Sstevel@tonic-gate * Configure the interface and mark it up, etc. 5737c478bd9Sstevel@tonic-gate */ 5747c478bd9Sstevel@tonic-gate demand_conf(); 5757c478bd9Sstevel@tonic-gate } 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate new_phase(PHASE_INITIALIZED); 5787c478bd9Sstevel@tonic-gate do_callback = 0; 5797c478bd9Sstevel@tonic-gate for (;;) { 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate need_holdoff = 1; 5827c478bd9Sstevel@tonic-gate ttyfd = -1; 5837c478bd9Sstevel@tonic-gate real_ttyfd = -1; 5847c478bd9Sstevel@tonic-gate status = EXIT_OK; 5857c478bd9Sstevel@tonic-gate ++unsuccess; 5867c478bd9Sstevel@tonic-gate doing_callback = do_callback; 5877c478bd9Sstevel@tonic-gate do_callback = 0; 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate if (demand && !doing_callback) { 5907c478bd9Sstevel@tonic-gate /* 5917c478bd9Sstevel@tonic-gate * Don't do anything until we see some activity. 5927c478bd9Sstevel@tonic-gate */ 5937c478bd9Sstevel@tonic-gate kill_link = 0; 5947c478bd9Sstevel@tonic-gate new_phase(PHASE_DORMANT); 5957c478bd9Sstevel@tonic-gate demand_unblock(); 5967c478bd9Sstevel@tonic-gate add_fd(fd_loop); 5977c478bd9Sstevel@tonic-gate for (;;) { 5987c478bd9Sstevel@tonic-gate if (sigsetjmp(sigjmp, 1) == 0) { 5997c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL); 6007c478bd9Sstevel@tonic-gate if (kill_link || got_sigchld) { 6017c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 6027c478bd9Sstevel@tonic-gate } else { 6037c478bd9Sstevel@tonic-gate waiting = 1; 6047c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 6057c478bd9Sstevel@tonic-gate wait_input(timeleft(&timo)); 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate waiting = 0; 6097c478bd9Sstevel@tonic-gate calltimeout(); 6107c478bd9Sstevel@tonic-gate if (kill_link) { 6117c478bd9Sstevel@tonic-gate if (!persist) 6127c478bd9Sstevel@tonic-gate break; 6137c478bd9Sstevel@tonic-gate kill_link = 0; 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate if (get_loop_output()) 6167c478bd9Sstevel@tonic-gate break; 6177c478bd9Sstevel@tonic-gate if (got_sigchld) 6187c478bd9Sstevel@tonic-gate (void) reap_kids(0); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate remove_fd(fd_loop); 6217c478bd9Sstevel@tonic-gate if (kill_link && !persist) 6227c478bd9Sstevel@tonic-gate break; 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate /* 6257c478bd9Sstevel@tonic-gate * Now we want to bring up the link. 6267c478bd9Sstevel@tonic-gate */ 6277c478bd9Sstevel@tonic-gate demand_block(); 6287c478bd9Sstevel@tonic-gate info("Starting link"); 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate new_phase(doing_callback ? PHASE_CALLINGBACK : PHASE_SERIALCONN); 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate /* 6347c478bd9Sstevel@tonic-gate * Get a pty master/slave pair if the pty, notty, socket, 6357c478bd9Sstevel@tonic-gate * or record options were specified. 6367c478bd9Sstevel@tonic-gate */ 6377c478bd9Sstevel@tonic-gate (void) strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); 6387c478bd9Sstevel@tonic-gate pty_master = -1; 6397c478bd9Sstevel@tonic-gate pty_slave = -1; 6407c478bd9Sstevel@tonic-gate if (using_pty || record_file != NULL) { 6417c478bd9Sstevel@tonic-gate if (!get_pty(&pty_master, &pty_slave, ppp_devnam, uid)) { 6427c478bd9Sstevel@tonic-gate error("Couldn't allocate pseudo-tty"); 6437c478bd9Sstevel@tonic-gate status = EXIT_FATAL_ERROR; 6447c478bd9Sstevel@tonic-gate goto fail; 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate set_up_tty(pty_slave, 1); 6477c478bd9Sstevel@tonic-gate } 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate /* 6507c478bd9Sstevel@tonic-gate * Lock the device if we've been asked to. 6517c478bd9Sstevel@tonic-gate */ 6527c478bd9Sstevel@tonic-gate status = EXIT_LOCK_FAILED; 6537c478bd9Sstevel@tonic-gate if (lockflag && !privopen && !direct_tty) { 6547c478bd9Sstevel@tonic-gate if (lock(devnam) < 0) 6557c478bd9Sstevel@tonic-gate goto fail; 6567c478bd9Sstevel@tonic-gate locked = 1; 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate /* 6607c478bd9Sstevel@tonic-gate * Open the serial device and set it up to be the ppp interface. 6617c478bd9Sstevel@tonic-gate * First we open it in non-blocking mode so we can set the 6627c478bd9Sstevel@tonic-gate * various termios flags appropriately. If we aren't dialling 6637c478bd9Sstevel@tonic-gate * out and we want to use the modem lines, we reopen it later 6647c478bd9Sstevel@tonic-gate * in order to wait for the carrier detect signal from the modem. 6657c478bd9Sstevel@tonic-gate */ 6667c478bd9Sstevel@tonic-gate hungup = 0; 6677c478bd9Sstevel@tonic-gate kill_link = 0; 6687c478bd9Sstevel@tonic-gate connector = doing_callback? callback_script: connect_script; 6697c478bd9Sstevel@tonic-gate if (direct_tty) { 6707c478bd9Sstevel@tonic-gate ttyfd = 0; 6717c478bd9Sstevel@tonic-gate } else if (devnam[0] != '\0') { 6727c478bd9Sstevel@tonic-gate for (;;) { 6737c478bd9Sstevel@tonic-gate /* If the user specified the device name, become the 6747c478bd9Sstevel@tonic-gate user before opening it. */ 6757c478bd9Sstevel@tonic-gate int err; 6767c478bd9Sstevel@tonic-gate if (!devnam_info.priv && !privopen) 6777c478bd9Sstevel@tonic-gate (void) seteuid(uid); 6787c478bd9Sstevel@tonic-gate if ((ttyfd = sys_extra_fd()) < 0) 6797c478bd9Sstevel@tonic-gate ttyfd = open(devnam, O_NONBLOCK | O_RDWR); 6807c478bd9Sstevel@tonic-gate err = errno; 6817c478bd9Sstevel@tonic-gate if (!devnam_info.priv && !privopen) 6827c478bd9Sstevel@tonic-gate (void) seteuid(0); 6837c478bd9Sstevel@tonic-gate if (ttyfd >= 0) 6847c478bd9Sstevel@tonic-gate break; 6857c478bd9Sstevel@tonic-gate errno = err; 6867c478bd9Sstevel@tonic-gate if (err != EINTR) { 6877c478bd9Sstevel@tonic-gate error("Failed to open %s: %m", devnam); 6887c478bd9Sstevel@tonic-gate status = EXIT_OPEN_FAILED; 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate if (!persist || err != EINTR) 6917c478bd9Sstevel@tonic-gate goto fail; 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 6947c478bd9Sstevel@tonic-gate || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) 6957c478bd9Sstevel@tonic-gate warn("Couldn't reset non-blocking mode on device: %m"); 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate /* 6987c478bd9Sstevel@tonic-gate * Do the equivalent of `mesg n' to stop broadcast messages. 6997c478bd9Sstevel@tonic-gate */ 7007c478bd9Sstevel@tonic-gate if (fstat(ttyfd, &statbuf) < 0 7017c478bd9Sstevel@tonic-gate || fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) { 7027c478bd9Sstevel@tonic-gate warn("Couldn't restrict write permissions to %s: %m", devnam); 7037c478bd9Sstevel@tonic-gate } else 7047c478bd9Sstevel@tonic-gate tty_mode = statbuf.st_mode; 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate /* 7077c478bd9Sstevel@tonic-gate * Set line speed, flow control, etc. 7087c478bd9Sstevel@tonic-gate * If we have a non-null connection or initializer script, 7097c478bd9Sstevel@tonic-gate * on most systems we set CLOCAL for now so that we can talk 7107c478bd9Sstevel@tonic-gate * to the modem before carrier comes up. But this has the 7117c478bd9Sstevel@tonic-gate * side effect that we might miss it if CD drops before we 7127c478bd9Sstevel@tonic-gate * get to clear CLOCAL below. On systems where we can talk 7137c478bd9Sstevel@tonic-gate * successfully to the modem with CLOCAL clear and CD down, 7147c478bd9Sstevel@tonic-gate * we could clear CLOCAL at this point. 7157c478bd9Sstevel@tonic-gate */ 7167c478bd9Sstevel@tonic-gate set_up_tty(ttyfd, ((connector != NULL && connector[0] != '\0') 7177c478bd9Sstevel@tonic-gate || initializer != NULL)); 7187c478bd9Sstevel@tonic-gate real_ttyfd = ttyfd; 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate /* 7227c478bd9Sstevel@tonic-gate * If the pty, socket, notty and/or record option was specified, 7237c478bd9Sstevel@tonic-gate * start up the character shunt now. 7247c478bd9Sstevel@tonic-gate */ 7257c478bd9Sstevel@tonic-gate status = EXIT_PTYCMD_FAILED; 7267c478bd9Sstevel@tonic-gate if (ptycommand != NULL) { 7277c478bd9Sstevel@tonic-gate if (record_file != NULL) { 7287c478bd9Sstevel@tonic-gate int ipipe[2], opipe[2], ok; 7297c478bd9Sstevel@tonic-gate 7307c478bd9Sstevel@tonic-gate if (pipe(ipipe) < 0 || pipe(opipe) < 0) 7317c478bd9Sstevel@tonic-gate fatal("Couldn't create pipes for record option: %m"); 7327c478bd9Sstevel@tonic-gate dbglog("starting charshunt for pty option"); 7337c478bd9Sstevel@tonic-gate ok = device_script(ptycommand, opipe[0], ipipe[1], 1, 7347c478bd9Sstevel@tonic-gate "record") == 0 && start_charshunt(ipipe[0], opipe[1]); 7357c478bd9Sstevel@tonic-gate (void) close(ipipe[0]); 7367c478bd9Sstevel@tonic-gate (void) close(ipipe[1]); 7377c478bd9Sstevel@tonic-gate (void) close(opipe[0]); 7387c478bd9Sstevel@tonic-gate (void) close(opipe[1]); 7397c478bd9Sstevel@tonic-gate if (!ok) 7407c478bd9Sstevel@tonic-gate goto fail; 7417c478bd9Sstevel@tonic-gate } else { 7427c478bd9Sstevel@tonic-gate if (device_script(ptycommand, pty_master, pty_master, 1, 7437c478bd9Sstevel@tonic-gate "pty") < 0) 7447c478bd9Sstevel@tonic-gate goto fail; 7457c478bd9Sstevel@tonic-gate ttyfd = pty_slave; 7467c478bd9Sstevel@tonic-gate (void) close(pty_master); 7477c478bd9Sstevel@tonic-gate pty_master = -1; 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate } else if (pty_socket != NULL) { 7507c478bd9Sstevel@tonic-gate int fd = open_socket(pty_socket); 7517c478bd9Sstevel@tonic-gate if (fd < 0) 7527c478bd9Sstevel@tonic-gate goto fail; 7537c478bd9Sstevel@tonic-gate dbglog("starting charshunt for socket option"); 7547c478bd9Sstevel@tonic-gate if (!start_charshunt(fd, fd)) 7557c478bd9Sstevel@tonic-gate goto fail; 7567c478bd9Sstevel@tonic-gate } else if (notty) { 7577c478bd9Sstevel@tonic-gate dbglog("starting charshunt for notty option"); 7587c478bd9Sstevel@tonic-gate if (!start_charshunt(0, 1)) 7597c478bd9Sstevel@tonic-gate goto fail; 7607c478bd9Sstevel@tonic-gate } else if (record_file != NULL) { 7617c478bd9Sstevel@tonic-gate dbglog("starting charshunt for record option"); 7627c478bd9Sstevel@tonic-gate if (!start_charshunt(ttyfd, ttyfd)) 7637c478bd9Sstevel@tonic-gate goto fail; 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate /* run connection script */ 7677c478bd9Sstevel@tonic-gate if (((connector != NULL) && (connector[0] != '\0')) || initializer) { 7687c478bd9Sstevel@tonic-gate if (real_ttyfd != -1) { 7697c478bd9Sstevel@tonic-gate /* XXX do this if doing_callback == CALLBACK_DIALIN? */ 7707c478bd9Sstevel@tonic-gate if (!default_device && modem && !direct_tty) { 7717c478bd9Sstevel@tonic-gate setdtr(real_ttyfd, 0); /* in case modem is off hook */ 7727c478bd9Sstevel@tonic-gate (void) sleep(1); 7737c478bd9Sstevel@tonic-gate setdtr(real_ttyfd, 1); 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate } 7767c478bd9Sstevel@tonic-gate 7777c478bd9Sstevel@tonic-gate if ((initializer != NULL) && (initializer[0] != '\0')) { 7787c478bd9Sstevel@tonic-gate if (device_script(initializer, ttyfd, ttyfd, 0, "init") < 0) { 7797c478bd9Sstevel@tonic-gate error("Initializer script failed"); 7807c478bd9Sstevel@tonic-gate status = EXIT_INIT_FAILED; 7817c478bd9Sstevel@tonic-gate goto fail; 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate if (kill_link) 7847c478bd9Sstevel@tonic-gate goto disconnect; 7857c478bd9Sstevel@tonic-gate 7867c478bd9Sstevel@tonic-gate info("Serial port initialized."); 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate if ((connector != NULL) && (connector[0] != '\0')) { 7907c478bd9Sstevel@tonic-gate if (device_script(connector, ttyfd, ttyfd, 0, "connect") < 0) { 7917c478bd9Sstevel@tonic-gate error("Connect script failed"); 7927c478bd9Sstevel@tonic-gate status = EXIT_CONNECT_FAILED; 7937c478bd9Sstevel@tonic-gate goto fail; 7947c478bd9Sstevel@tonic-gate } 7957c478bd9Sstevel@tonic-gate if (kill_link) 7967c478bd9Sstevel@tonic-gate goto disconnect; 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate info("Serial connection established."); 7997c478bd9Sstevel@tonic-gate } 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate /* 8027c478bd9Sstevel@tonic-gate * Clear CLOCAL if modem option -- we now have carrier 8037c478bd9Sstevel@tonic-gate * established, and we should respect loss of carrier. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate if (real_ttyfd != -1) 8067c478bd9Sstevel@tonic-gate set_up_tty(real_ttyfd, 0); 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate if (doing_callback == CALLBACK_DIALIN) 8097c478bd9Sstevel@tonic-gate connector = NULL; 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate /* reopen tty if necessary to wait for carrier */ 8137c478bd9Sstevel@tonic-gate if (connector == NULL && modem && devnam[0] != '\0' && !direct_tty) { 8147c478bd9Sstevel@tonic-gate for (;;) { 8157c478bd9Sstevel@tonic-gate if ((i = open(devnam, O_RDWR)) >= 0) 8167c478bd9Sstevel@tonic-gate break; 8177c478bd9Sstevel@tonic-gate if (errno != EINTR) { 8187c478bd9Sstevel@tonic-gate error("Failed to reopen %s: %m", devnam); 8197c478bd9Sstevel@tonic-gate status = EXIT_OPEN_FAILED; 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate if (!persist || errno != EINTR || hungup || kill_link) 8227c478bd9Sstevel@tonic-gate goto fail; 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate (void) close(i); 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", baud_rate); 8287c478bd9Sstevel@tonic-gate script_setenv("SPEED", numbuf, 0); 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate /* run welcome script, if any */ 8317c478bd9Sstevel@tonic-gate if ((welcomer != NULL) && (welcomer[0] != '\0')) { 8327c478bd9Sstevel@tonic-gate if (device_script(welcomer, ttyfd, ttyfd, 0, "welcome") < 0) 8337c478bd9Sstevel@tonic-gate warn("Welcome script failed"); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate /* set up the serial device as a ppp interface */ 8377c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 8387c478bd9Sstevel@tonic-gate (void) tdb_writelock(pppdb); 8397c478bd9Sstevel@tonic-gate #endif 8407c478bd9Sstevel@tonic-gate fd_ppp = establish_ppp(ttyfd); 8417c478bd9Sstevel@tonic-gate if (fd_ppp < 0) { 8427c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 8437c478bd9Sstevel@tonic-gate (void) tdb_writeunlock(pppdb); 8447c478bd9Sstevel@tonic-gate #endif 8457c478bd9Sstevel@tonic-gate status = EXIT_FATAL_ERROR; 8467c478bd9Sstevel@tonic-gate goto disconnect; 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate if (!demand && ifunit >= 0) 8507c478bd9Sstevel@tonic-gate set_ifunit(1); 8517c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 8527c478bd9Sstevel@tonic-gate (void) tdb_writeunlock(pppdb); 8537c478bd9Sstevel@tonic-gate #endif 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate /* 8567c478bd9Sstevel@tonic-gate * Start opening the connection and wait for 8577c478bd9Sstevel@tonic-gate * incoming events (reply, timeout, etc.). 8587c478bd9Sstevel@tonic-gate */ 8597c478bd9Sstevel@tonic-gate notice("Connect: %s <--> %s", ifname, ppp_devnam); 8607c478bd9Sstevel@tonic-gate (void) gettimeofday(&start_time, NULL); 8617c478bd9Sstevel@tonic-gate link_stats_valid = 0; 8627c478bd9Sstevel@tonic-gate script_unsetenv("CONNECT_TIME"); 8637c478bd9Sstevel@tonic-gate script_unsetenv("BYTES_SENT"); 8647c478bd9Sstevel@tonic-gate script_unsetenv("BYTES_RCVD"); 8657c478bd9Sstevel@tonic-gate lcp_lowerup(0); 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate /* Mostly for accounting purposes */ 8687c478bd9Sstevel@tonic-gate new_phase(PHASE_CONNECTED); 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* 8717c478bd9Sstevel@tonic-gate * If we are initiating this connection, wait for a short 8727c478bd9Sstevel@tonic-gate * time for something from the peer. This can avoid bouncing 8737c478bd9Sstevel@tonic-gate * our packets off his tty before he has it set up. 8747c478bd9Sstevel@tonic-gate */ 8757c478bd9Sstevel@tonic-gate add_fd(fd_ppp); 8767c478bd9Sstevel@tonic-gate if (connect_delay != 0 && (connector != NULL || ptycommand != NULL)) { 8777c478bd9Sstevel@tonic-gate struct timeval t; 8787c478bd9Sstevel@tonic-gate t.tv_sec = connect_delay / 1000; 8797c478bd9Sstevel@tonic-gate t.tv_usec = connect_delay % 1000; 8807c478bd9Sstevel@tonic-gate wait_input(&t); 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate 8837c478bd9Sstevel@tonic-gate lcp_open(0); /* Start protocol */ 8847c478bd9Sstevel@tonic-gate open_ccp_flag = 0; 8857c478bd9Sstevel@tonic-gate status = EXIT_NEGOTIATION_FAILED; 8867c478bd9Sstevel@tonic-gate new_phase(PHASE_ESTABLISH); 8877c478bd9Sstevel@tonic-gate while (phase != PHASE_DEAD) { 8887c478bd9Sstevel@tonic-gate if (sigsetjmp(sigjmp, 1) == 0) { 8897c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL); 8907c478bd9Sstevel@tonic-gate if (kill_link || open_ccp_flag || got_sigchld) { 8917c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 8927c478bd9Sstevel@tonic-gate } else { 8937c478bd9Sstevel@tonic-gate waiting = 1; 8947c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 8957c478bd9Sstevel@tonic-gate wait_input(timeleft(&timo)); 8967c478bd9Sstevel@tonic-gate } 8977c478bd9Sstevel@tonic-gate } 8987c478bd9Sstevel@tonic-gate waiting = 0; 8997c478bd9Sstevel@tonic-gate calltimeout(); 9007c478bd9Sstevel@tonic-gate get_input(); 9017c478bd9Sstevel@tonic-gate if (kill_link) { 9027c478bd9Sstevel@tonic-gate lcp_close(0, "User request"); 9037c478bd9Sstevel@tonic-gate kill_link = 0; 9047c478bd9Sstevel@tonic-gate } 9057c478bd9Sstevel@tonic-gate if (open_ccp_flag) { 9067c478bd9Sstevel@tonic-gate if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { 9077c478bd9Sstevel@tonic-gate /* Uncloak ourselves. */ 9087c478bd9Sstevel@tonic-gate ccp_fsm[0].flags &= ~OPT_SILENT; 9097c478bd9Sstevel@tonic-gate (*ccp_protent.open)(0); 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate open_ccp_flag = 0; 9127c478bd9Sstevel@tonic-gate } 9137c478bd9Sstevel@tonic-gate if (got_sigchld) 9147c478bd9Sstevel@tonic-gate (void) reap_kids(0); /* Don't leave dead kids lying around */ 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * Print connect time and statistics. 9197c478bd9Sstevel@tonic-gate */ 9207c478bd9Sstevel@tonic-gate if (link_stats_valid) { 9217c478bd9Sstevel@tonic-gate int t = (link_connect_time + 5) / 6; /* 1/10ths of minutes */ 9227c478bd9Sstevel@tonic-gate info("Connect time %d.%d minutes.", t/10, t%10); 9237c478bd9Sstevel@tonic-gate info("Sent %" PPP_COUNTER_F " bytes (%" PPP_COUNTER_F 9247c478bd9Sstevel@tonic-gate " packets), received %" PPP_COUNTER_F " bytes (%" PPP_COUNTER_F 9257c478bd9Sstevel@tonic-gate " packets).", 9267c478bd9Sstevel@tonic-gate link_stats.bytes_out, link_stats.pkts_out, 9277c478bd9Sstevel@tonic-gate link_stats.bytes_in, link_stats.pkts_in); 9287c478bd9Sstevel@tonic-gate } 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate /* 9317c478bd9Sstevel@tonic-gate * Delete pid file before disestablishing ppp. Otherwise it 9327c478bd9Sstevel@tonic-gate * can happen that another pppd gets the same unit and then 9337c478bd9Sstevel@tonic-gate * we delete its pid file. 9347c478bd9Sstevel@tonic-gate */ 9357c478bd9Sstevel@tonic-gate if (!demand) { 9367c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0' 9377c478bd9Sstevel@tonic-gate && unlink(pidfilename) < 0 && errno != ENOENT) 9387c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", pidfilename); 9397c478bd9Sstevel@tonic-gate pidfilename[0] = '\0'; 9407c478bd9Sstevel@tonic-gate } 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate /* 9437c478bd9Sstevel@tonic-gate * If we may want to bring the link up again, transfer 9447c478bd9Sstevel@tonic-gate * the ppp unit back to the loopback. Set the 9457c478bd9Sstevel@tonic-gate * real serial device back to its normal mode of operation. 9467c478bd9Sstevel@tonic-gate */ 9477c478bd9Sstevel@tonic-gate remove_fd(fd_ppp); 9487c478bd9Sstevel@tonic-gate clean_check(); 9497c478bd9Sstevel@tonic-gate if (demand) 9507c478bd9Sstevel@tonic-gate restore_loop(); 9517c478bd9Sstevel@tonic-gate disestablish_ppp(ttyfd); 9527c478bd9Sstevel@tonic-gate fd_ppp = -1; 9537c478bd9Sstevel@tonic-gate if (!hungup) 9547c478bd9Sstevel@tonic-gate lcp_lowerdown(0); 9557c478bd9Sstevel@tonic-gate if (!demand) 9567c478bd9Sstevel@tonic-gate script_unsetenv("IFNAME"); 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate /* 9597c478bd9Sstevel@tonic-gate * Run disconnector script, if requested. 9607c478bd9Sstevel@tonic-gate * XXX we may not be able to do this if the line has hung up! 9617c478bd9Sstevel@tonic-gate */ 9627c478bd9Sstevel@tonic-gate disconnect: 9637c478bd9Sstevel@tonic-gate if ((disconnect_script != NULL) && (disconnect_script[0] != '\0') && 9647c478bd9Sstevel@tonic-gate !hungup) { 9657c478bd9Sstevel@tonic-gate new_phase(PHASE_DISCONNECT); 9667c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0) 9677c478bd9Sstevel@tonic-gate set_up_tty(real_ttyfd, 1); 9687c478bd9Sstevel@tonic-gate if (device_script(disconnect_script, ttyfd, ttyfd, 0, 9697c478bd9Sstevel@tonic-gate "disconnect") < 0) { 9707c478bd9Sstevel@tonic-gate warn("disconnect script failed"); 9717c478bd9Sstevel@tonic-gate } else { 9727c478bd9Sstevel@tonic-gate info("Serial link disconnected."); 9737c478bd9Sstevel@tonic-gate } 9747c478bd9Sstevel@tonic-gate } 9757c478bd9Sstevel@tonic-gate 9767c478bd9Sstevel@tonic-gate fail: 9777c478bd9Sstevel@tonic-gate if (pty_master >= 0) 9787c478bd9Sstevel@tonic-gate (void) close(pty_master); 9797c478bd9Sstevel@tonic-gate if (pty_slave >= 0) { 9807c478bd9Sstevel@tonic-gate (void) close(pty_slave); 9817c478bd9Sstevel@tonic-gate pty_slave = -1; 9827c478bd9Sstevel@tonic-gate } 9837c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0) 9847c478bd9Sstevel@tonic-gate close_tty(); 9857c478bd9Sstevel@tonic-gate if (locked) { 9867c478bd9Sstevel@tonic-gate locked = 0; 9877c478bd9Sstevel@tonic-gate unlock(); 9887c478bd9Sstevel@tonic-gate } 9897c478bd9Sstevel@tonic-gate 9907c478bd9Sstevel@tonic-gate if (!demand) { 9917c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0' 9927c478bd9Sstevel@tonic-gate && unlink(pidfilename) < 0 && errno != ENOENT) 9937c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", pidfilename); 9947c478bd9Sstevel@tonic-gate pidfilename[0] = '\0'; 9957c478bd9Sstevel@tonic-gate } 9967c478bd9Sstevel@tonic-gate 9977c478bd9Sstevel@tonic-gate if (!persist || (maxfail > 0 && unsuccess >= maxfail)) 9987c478bd9Sstevel@tonic-gate break; 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate kill_link = 0; 10017c478bd9Sstevel@tonic-gate if (demand) 10027c478bd9Sstevel@tonic-gate demand_discard(); 10037c478bd9Sstevel@tonic-gate t = need_holdoff? holdoff: 0; 10047c478bd9Sstevel@tonic-gate if (holdoff_hook != NULL) 10057c478bd9Sstevel@tonic-gate t = (*holdoff_hook)(); 10067c478bd9Sstevel@tonic-gate if (t > 0) { 10077c478bd9Sstevel@tonic-gate new_phase(PHASE_HOLDOFF); 10087c478bd9Sstevel@tonic-gate TIMEOUT(holdoff_end, NULL, t); 10097c478bd9Sstevel@tonic-gate do { 10107c478bd9Sstevel@tonic-gate if (sigsetjmp(sigjmp, 1) == 0) { 10117c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL); 10127c478bd9Sstevel@tonic-gate if (kill_link || got_sigchld) { 10137c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 10147c478bd9Sstevel@tonic-gate } else { 10157c478bd9Sstevel@tonic-gate waiting = 1; 10167c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 10177c478bd9Sstevel@tonic-gate wait_input(timeleft(&timo)); 10187c478bd9Sstevel@tonic-gate } 10197c478bd9Sstevel@tonic-gate } 10207c478bd9Sstevel@tonic-gate waiting = 0; 10217c478bd9Sstevel@tonic-gate calltimeout(); 10227c478bd9Sstevel@tonic-gate if (kill_link) { 10237c478bd9Sstevel@tonic-gate kill_link = 0; 10247c478bd9Sstevel@tonic-gate new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ 10257c478bd9Sstevel@tonic-gate } 10267c478bd9Sstevel@tonic-gate if (got_sigchld) 10277c478bd9Sstevel@tonic-gate (void) reap_kids(0); 10287c478bd9Sstevel@tonic-gate } while (phase == PHASE_HOLDOFF); 10297c478bd9Sstevel@tonic-gate if (!persist) 10307c478bd9Sstevel@tonic-gate break; 10317c478bd9Sstevel@tonic-gate } 10327c478bd9Sstevel@tonic-gate } 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate /* Wait for scripts to finish */ 10357c478bd9Sstevel@tonic-gate final_reap(); 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate die(status); 10387c478bd9Sstevel@tonic-gate return (0); 10397c478bd9Sstevel@tonic-gate } 10407c478bd9Sstevel@tonic-gate 10417c478bd9Sstevel@tonic-gate /* 10427c478bd9Sstevel@tonic-gate * setup_signals - initialize signal handling. 10437c478bd9Sstevel@tonic-gate */ 10447c478bd9Sstevel@tonic-gate static void 10457c478bd9Sstevel@tonic-gate setup_signals() 10467c478bd9Sstevel@tonic-gate { 10477c478bd9Sstevel@tonic-gate struct sigaction sa; 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate /* 10507c478bd9Sstevel@tonic-gate * Compute mask of all interesting signals and install signal handlers 10517c478bd9Sstevel@tonic-gate * for each. Only one signal handler may be active at a time. Therefore, 10527c478bd9Sstevel@tonic-gate * all other signals should be masked when any handler is executing. 10537c478bd9Sstevel@tonic-gate */ 10547c478bd9Sstevel@tonic-gate (void) sigemptyset(&main_sigmask); 10557c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGHUP); 10567c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGINT); 10577c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGTERM); 10587c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGCHLD); 10597c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGUSR2); 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate #define SIGNAL(s, handler) if (1) { \ 10627c478bd9Sstevel@tonic-gate sa.sa_handler = handler; \ 10637c478bd9Sstevel@tonic-gate if (sigaction(s, &sa, NULL) < 0) \ 10647c478bd9Sstevel@tonic-gate fatal("Couldn't establish signal handler (%d): %m", s); \ 10657c478bd9Sstevel@tonic-gate } else ((void)0) 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate sa.sa_mask = main_sigmask; 10687c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 10697c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGHUP, hup); /* Hangup */ 10707c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGINT, term); /* Interrupt */ 10717c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGTERM, term); /* Terminate */ 10727c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGCHLD, chld); 10737c478bd9Sstevel@tonic-gate 10747c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGUSR1, toggle_debug); /* Toggle debug flag */ 10757c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGUSR2, open_ccp); /* Reopen CCP */ 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate /* 10787c478bd9Sstevel@tonic-gate * Install a handler for other signals which would otherwise 10797c478bd9Sstevel@tonic-gate * cause pppd to exit without cleaning up. 10807c478bd9Sstevel@tonic-gate */ 10817c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGALRM, bad_signal); 1082*f53eecf5SJames Carlson /*CONSTANTCONDITION*/ SIGNAL(SIGQUIT, bad_signal); 1083*f53eecf5SJames Carlson 1084*f53eecf5SJames Carlson /* Do not hook any of these signals on Solaris; allow core dump instead */ 1085*f53eecf5SJames Carlson #ifndef SOL2 1086*f53eecf5SJames Carlson /*CONSTANTCONDITION*/ SIGNAL(SIGABRT, bad_signal); 10877c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGFPE, bad_signal); 10887c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGILL, bad_signal); 10897c478bd9Sstevel@tonic-gate #ifndef DEBUG 10907c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGSEGV, bad_signal); 10917c478bd9Sstevel@tonic-gate #endif 10927c478bd9Sstevel@tonic-gate #ifdef SIGBUS 10937c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGBUS, bad_signal); 10947c478bd9Sstevel@tonic-gate #endif 10957c478bd9Sstevel@tonic-gate #ifdef SIGEMT 10967c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGEMT, bad_signal); 10977c478bd9Sstevel@tonic-gate #endif 10987c478bd9Sstevel@tonic-gate #ifdef SIGPOLL 10997c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGPOLL, bad_signal); 11007c478bd9Sstevel@tonic-gate #endif 11017c478bd9Sstevel@tonic-gate #ifdef SIGPROF 11027c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGPROF, bad_signal); 11037c478bd9Sstevel@tonic-gate #endif 11047c478bd9Sstevel@tonic-gate #ifdef SIGSYS 11057c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGSYS, bad_signal); 11067c478bd9Sstevel@tonic-gate #endif 11077c478bd9Sstevel@tonic-gate #ifdef SIGTRAP 11087c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGTRAP, bad_signal); 11097c478bd9Sstevel@tonic-gate #endif 11107c478bd9Sstevel@tonic-gate #ifdef SIGVTALRM 11117c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGVTALRM, bad_signal); 11127c478bd9Sstevel@tonic-gate #endif 11137c478bd9Sstevel@tonic-gate #ifdef SIGXCPU 11147c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGXCPU, bad_signal); 11157c478bd9Sstevel@tonic-gate #endif 11167c478bd9Sstevel@tonic-gate #ifdef SIGXFSZ 11177c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGXFSZ, bad_signal); 1118*f53eecf5SJames Carlson #endif 11197c478bd9Sstevel@tonic-gate #endif 11207c478bd9Sstevel@tonic-gate 11217c478bd9Sstevel@tonic-gate /* 11227c478bd9Sstevel@tonic-gate * Apparently we can get a SIGPIPE when we call syslog, if 11237c478bd9Sstevel@tonic-gate * syslogd has died and been restarted. Ignoring it seems 11247c478bd9Sstevel@tonic-gate * be sufficient. 11257c478bd9Sstevel@tonic-gate */ 11267c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_IGN); 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate /* 11307c478bd9Sstevel@tonic-gate * set_ifunit - do things we need to do once we know which ppp 11317c478bd9Sstevel@tonic-gate * unit we are using. 11327c478bd9Sstevel@tonic-gate */ 11337c478bd9Sstevel@tonic-gate void 11347c478bd9Sstevel@tonic-gate set_ifunit(iskey) 11357c478bd9Sstevel@tonic-gate int iskey; 11367c478bd9Sstevel@tonic-gate { 11377c478bd9Sstevel@tonic-gate sys_ifname(); 11387c478bd9Sstevel@tonic-gate info("Using interface %s", ifname); 11397c478bd9Sstevel@tonic-gate script_setenv("IFNAME", ifname, iskey); 11407c478bd9Sstevel@tonic-gate if (iskey) { 11417c478bd9Sstevel@tonic-gate create_pidfile(); /* write pid to file */ 11427c478bd9Sstevel@tonic-gate create_linkpidfile(); 11437c478bd9Sstevel@tonic-gate } 11447c478bd9Sstevel@tonic-gate } 11457c478bd9Sstevel@tonic-gate 11467c478bd9Sstevel@tonic-gate /* 11477c478bd9Sstevel@tonic-gate * detach - detach us from the controlling terminal. 11487c478bd9Sstevel@tonic-gate */ 11497c478bd9Sstevel@tonic-gate void 11507c478bd9Sstevel@tonic-gate detach() 11517c478bd9Sstevel@tonic-gate { 11527c478bd9Sstevel@tonic-gate pid_t pid; 11537c478bd9Sstevel@tonic-gate char numbuf[16]; 11547c478bd9Sstevel@tonic-gate 11557c478bd9Sstevel@tonic-gate if (detached) 11567c478bd9Sstevel@tonic-gate return; 11577c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)-1) { 11587c478bd9Sstevel@tonic-gate error("Couldn't detach (fork failed: %m)"); 11597c478bd9Sstevel@tonic-gate die(1); /* or just return? */ 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate if (pid != (pid_t)0) { 11627c478bd9Sstevel@tonic-gate /* parent */ 11637c478bd9Sstevel@tonic-gate if (locked) 11647c478bd9Sstevel@tonic-gate (void) relock(pid); 11657c478bd9Sstevel@tonic-gate exit(0); /* parent dies */ 11667c478bd9Sstevel@tonic-gate } 11677c478bd9Sstevel@tonic-gate (void) setsid(); 11687c478bd9Sstevel@tonic-gate /* 11697c478bd9Sstevel@tonic-gate * Fork again to relinquish session leadership. This is needed 11707c478bd9Sstevel@tonic-gate * to prevent the daemon from acquiring controlling terminal. 11717c478bd9Sstevel@tonic-gate */ 11727c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)-1) { 11737c478bd9Sstevel@tonic-gate error("Couldn't detach (second fork failed: %m)"); 11747c478bd9Sstevel@tonic-gate die(1); /* or just return? */ 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate if (pid != (pid_t)0) { 11777c478bd9Sstevel@tonic-gate /* parent */ 11787c478bd9Sstevel@tonic-gate if (locked) 11797c478bd9Sstevel@tonic-gate (void) relock(pid); 11807c478bd9Sstevel@tonic-gate exit(0); /* parent dies */ 11817c478bd9Sstevel@tonic-gate } 11827c478bd9Sstevel@tonic-gate (void) chdir("/"); 11837c478bd9Sstevel@tonic-gate (void) close(0); 11847c478bd9Sstevel@tonic-gate (void) close(1); 11857c478bd9Sstevel@tonic-gate (void) close(2); 11867c478bd9Sstevel@tonic-gate detached = 1; 11877c478bd9Sstevel@tonic-gate if (!log_to_file && !log_to_specific_fd) 11887c478bd9Sstevel@tonic-gate log_to_fd = -1; 11897c478bd9Sstevel@tonic-gate /* update pid files if they have been written already */ 11907c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0') 11917c478bd9Sstevel@tonic-gate create_pidfile(); 11927c478bd9Sstevel@tonic-gate if (linkpidfile[0] != '\0') 11937c478bd9Sstevel@tonic-gate create_linkpidfile(); 11947c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", getpid()); 11957c478bd9Sstevel@tonic-gate script_setenv("PPPD_PID", numbuf, 1); 11967c478bd9Sstevel@tonic-gate } 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate /* 11997c478bd9Sstevel@tonic-gate * reopen_log - (re)open our connection to syslog. 12007c478bd9Sstevel@tonic-gate */ 12017c478bd9Sstevel@tonic-gate void 12027c478bd9Sstevel@tonic-gate reopen_log() 12037c478bd9Sstevel@tonic-gate { 12047c478bd9Sstevel@tonic-gate #ifdef ULTRIX 12057c478bd9Sstevel@tonic-gate openlog("pppd", LOG_PID); 12067c478bd9Sstevel@tonic-gate #else 12077c478bd9Sstevel@tonic-gate openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP); 12087c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_INFO)); 12097c478bd9Sstevel@tonic-gate #endif 12107c478bd9Sstevel@tonic-gate } 12117c478bd9Sstevel@tonic-gate 12127c478bd9Sstevel@tonic-gate /* 12137c478bd9Sstevel@tonic-gate * Create a file containing our process ID. 12147c478bd9Sstevel@tonic-gate */ 12157c478bd9Sstevel@tonic-gate static void 12167c478bd9Sstevel@tonic-gate create_pidfile() 12177c478bd9Sstevel@tonic-gate { 12187c478bd9Sstevel@tonic-gate FILE *pidfile; 12197c478bd9Sstevel@tonic-gate 12207c478bd9Sstevel@tonic-gate (void) slprintf(pidfilename, sizeof(pidfilename), "%s%s.pid", 12217c478bd9Sstevel@tonic-gate _PATH_VARRUN, ifname); 12227c478bd9Sstevel@tonic-gate if ((pidfile = fopen(pidfilename, "w")) != NULL) { 12237c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%u\n", (unsigned)getpid()); 12247c478bd9Sstevel@tonic-gate (void) fclose(pidfile); 12257c478bd9Sstevel@tonic-gate } else { 12267c478bd9Sstevel@tonic-gate error("Failed to create pid file %s: %m", pidfilename); 12277c478bd9Sstevel@tonic-gate pidfilename[0] = '\0'; 12287c478bd9Sstevel@tonic-gate } 12297c478bd9Sstevel@tonic-gate } 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate static void 12327c478bd9Sstevel@tonic-gate create_linkpidfile() 12337c478bd9Sstevel@tonic-gate { 12347c478bd9Sstevel@tonic-gate FILE *pidfile; 12357c478bd9Sstevel@tonic-gate 12367c478bd9Sstevel@tonic-gate if (linkname[0] == '\0') 12377c478bd9Sstevel@tonic-gate return; 12387c478bd9Sstevel@tonic-gate script_setenv("LINKNAME", linkname, 1); 12397c478bd9Sstevel@tonic-gate (void) slprintf(linkpidfile, sizeof(linkpidfile), "%sppp-%s.pid", 12407c478bd9Sstevel@tonic-gate _PATH_VARRUN, linkname); 12417c478bd9Sstevel@tonic-gate if ((pidfile = fopen(linkpidfile, "w")) != NULL) { 12427c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%u\n", (unsigned)getpid()); 12437c478bd9Sstevel@tonic-gate if (ifname[0] != '\0') 12447c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%s\n", ifname); 12457c478bd9Sstevel@tonic-gate (void) fclose(pidfile); 12467c478bd9Sstevel@tonic-gate } else { 12477c478bd9Sstevel@tonic-gate error("Failed to create pid file %s: %m", linkpidfile); 12487c478bd9Sstevel@tonic-gate linkpidfile[0] = '\0'; 12497c478bd9Sstevel@tonic-gate } 12507c478bd9Sstevel@tonic-gate } 12517c478bd9Sstevel@tonic-gate 12527c478bd9Sstevel@tonic-gate /* 12537c478bd9Sstevel@tonic-gate * holdoff_end - called via a timeout when the holdoff period ends. 12547c478bd9Sstevel@tonic-gate */ 12557c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 12567c478bd9Sstevel@tonic-gate static void 12577c478bd9Sstevel@tonic-gate holdoff_end(arg) 12587c478bd9Sstevel@tonic-gate void *arg; 12597c478bd9Sstevel@tonic-gate { 12607c478bd9Sstevel@tonic-gate new_phase(PHASE_DORMANT); 12617c478bd9Sstevel@tonic-gate } 12627c478bd9Sstevel@tonic-gate 12637c478bd9Sstevel@tonic-gate /* List of protocol names, to make our messages a little more informative. */ 12647c478bd9Sstevel@tonic-gate struct protocol_list { 12657c478bd9Sstevel@tonic-gate u_short proto; 12667c478bd9Sstevel@tonic-gate const char *name; 12677c478bd9Sstevel@tonic-gate } protocol_list[] = { 12687c478bd9Sstevel@tonic-gate { 0x21, "IP" }, 12697c478bd9Sstevel@tonic-gate { 0x23, "OSI Network Layer" }, 12707c478bd9Sstevel@tonic-gate { 0x25, "Xerox NS IDP" }, 12717c478bd9Sstevel@tonic-gate { 0x27, "DECnet Phase IV" }, 12727c478bd9Sstevel@tonic-gate { 0x29, "Appletalk" }, 12737c478bd9Sstevel@tonic-gate { 0x2b, "Novell IPX" }, 12747c478bd9Sstevel@tonic-gate { 0x2d, "VJ compressed TCP/IP" }, 12757c478bd9Sstevel@tonic-gate { 0x2f, "VJ uncompressed TCP/IP" }, 12767c478bd9Sstevel@tonic-gate { 0x31, "Bridging PDU" }, 12777c478bd9Sstevel@tonic-gate { 0x33, "Stream Protocol ST-II" }, 12787c478bd9Sstevel@tonic-gate { 0x35, "Banyan Vines" }, 12797c478bd9Sstevel@tonic-gate { 0x37, "Old VJ compressed TCP/IP" }, 12807c478bd9Sstevel@tonic-gate { 0x39, "AppleTalk EDDP" }, 12817c478bd9Sstevel@tonic-gate { 0x3b, "AppleTalk SmartBuffered" }, 12827c478bd9Sstevel@tonic-gate { 0x3d, "Multilink" }, 12837c478bd9Sstevel@tonic-gate { 0x3f, "NetBIOS Frame" }, 12847c478bd9Sstevel@tonic-gate { 0x41, "Cisco LAN Extension" }, 12857c478bd9Sstevel@tonic-gate { 0x43, "Ascom Timeplex" }, 12867c478bd9Sstevel@tonic-gate { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, 12877c478bd9Sstevel@tonic-gate { 0x47, "DCA Remote Lan" }, 12887c478bd9Sstevel@tonic-gate { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, 12897c478bd9Sstevel@tonic-gate { 0x4b, "SNA over 802.2" }, 12907c478bd9Sstevel@tonic-gate { 0x4d, "SNA" }, 12917c478bd9Sstevel@tonic-gate { 0x4f, "IP6 Header Compression" }, 12927c478bd9Sstevel@tonic-gate { 0x51, "KNX Bridging" }, 12937c478bd9Sstevel@tonic-gate { 0x53, "Encrypted" }, 12947c478bd9Sstevel@tonic-gate { 0x55, "per-link encrypted" }, 12957c478bd9Sstevel@tonic-gate { 0x57, "IPv6" }, 12967c478bd9Sstevel@tonic-gate { 0x59, "PPP Muxing" }, 12977c478bd9Sstevel@tonic-gate { 0x6f, "Stampede Bridging" }, 12987c478bd9Sstevel@tonic-gate { 0x73, "MP+" }, 12997c478bd9Sstevel@tonic-gate { 0xc1, "STMF" }, 13007c478bd9Sstevel@tonic-gate { 0xfb, "per-link compressed" }, 13017c478bd9Sstevel@tonic-gate { 0xfd, "compressed datagram" }, 13027c478bd9Sstevel@tonic-gate { 0x0201, "802.1d Hello Packets" }, 13037c478bd9Sstevel@tonic-gate { 0x0203, "IBM Source Routing BPDU" }, 13047c478bd9Sstevel@tonic-gate { 0x0205, "DEC LANBridge100 Spanning Tree" }, 13057c478bd9Sstevel@tonic-gate { 0x0207, "Cisco Discovery Protocol" }, 13067c478bd9Sstevel@tonic-gate { 0x0231, "Luxcom" }, 13077c478bd9Sstevel@tonic-gate { 0x0233, "Sigma Network Systems" }, 13087c478bd9Sstevel@tonic-gate { 0x0235, "Apple Client Server Protocol" }, 13097c478bd9Sstevel@tonic-gate { 0x0281, "MPLS Unicast" }, 13107c478bd9Sstevel@tonic-gate { 0x0283, "MPLS Multicast" }, 13117c478bd9Sstevel@tonic-gate { 0x0285, "IEEE p1284.4" }, 13127c478bd9Sstevel@tonic-gate { 0x0287, "ETSI TETRA TNP1" }, 13137c478bd9Sstevel@tonic-gate { 0x4021, "Stacker LZS" }, 13147c478bd9Sstevel@tonic-gate { 0x8021, "Internet Protocol Control Protocol" }, 13157c478bd9Sstevel@tonic-gate { 0x8023, "OSI Network Layer Control Protocol" }, 13167c478bd9Sstevel@tonic-gate { 0x8025, "Xerox NS IDP Control Protocol" }, 13177c478bd9Sstevel@tonic-gate { 0x8027, "DECnet Phase IV Control Protocol" }, 13187c478bd9Sstevel@tonic-gate { 0x8029, "Appletalk Control Protocol" }, 13197c478bd9Sstevel@tonic-gate { 0x802b, "Novell IPX Control Protocol" }, 13207c478bd9Sstevel@tonic-gate { 0x8031, "Bridging Control Protocol" }, 13217c478bd9Sstevel@tonic-gate { 0x8033, "Stream Protocol Control Protocol" }, 13227c478bd9Sstevel@tonic-gate { 0x8035, "Banyan Vines Control Protocol" }, 13237c478bd9Sstevel@tonic-gate { 0x803f, "NetBIOS Frames Control Protocol" }, 13247c478bd9Sstevel@tonic-gate { 0x8041, "Cisco LAN Extension Control Protocol" }, 13257c478bd9Sstevel@tonic-gate { 0x8043, "Ascom Timeplex Control Protocol" }, 13267c478bd9Sstevel@tonic-gate { 0x8045, "Fujitsu LBLB Control Protocol" }, 13277c478bd9Sstevel@tonic-gate { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, 13287c478bd9Sstevel@tonic-gate { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, 13297c478bd9Sstevel@tonic-gate { 0x804b, "SNA over 802.2 Control Protocol" }, 13307c478bd9Sstevel@tonic-gate { 0x804d, "SNA Control Protocol" }, 13317c478bd9Sstevel@tonic-gate { 0x8051, "KNX Bridging Control Protocol" }, 13327c478bd9Sstevel@tonic-gate { 0x8053, "Encryption Control Protocol" }, 13337c478bd9Sstevel@tonic-gate { 0x8055, "Per-link Encryption Control Protocol" }, 13347c478bd9Sstevel@tonic-gate { 0x8057, "IPv6 Control Protocol" }, 13357c478bd9Sstevel@tonic-gate { 0x806f, "Stampede Bridging Control Protocol" }, 13367c478bd9Sstevel@tonic-gate { 0x80c1, "STMF Control Protocol" }, 13377c478bd9Sstevel@tonic-gate { 0x80fb, "Per-link Compression Control Protocol" }, 13387c478bd9Sstevel@tonic-gate { 0x80fd, "Compression Control Protocol" }, 13397c478bd9Sstevel@tonic-gate { 0x8207, "Cisco Discovery Control Protocol" }, 13407c478bd9Sstevel@tonic-gate { 0x8235, "Apple Client Server Control Protocol" }, 13417c478bd9Sstevel@tonic-gate { 0x8281, "MPLS Control Protocol" }, 13427c478bd9Sstevel@tonic-gate { 0x8287, "ETSI TETRA TNP1 Control Protocol" }, 13437c478bd9Sstevel@tonic-gate { 0xc021, "Link Control Protocol" }, 13447c478bd9Sstevel@tonic-gate { 0xc023, "Password Authentication Protocol" }, 13457c478bd9Sstevel@tonic-gate { 0xc025, "Link Quality Report" }, 13467c478bd9Sstevel@tonic-gate { 0xc027, "Shiva Password Authentication Protocol" }, 13477c478bd9Sstevel@tonic-gate { 0xc029, "CallBack Control Protocol (CBCP)" }, 13487c478bd9Sstevel@tonic-gate { 0xc02b, "Bandwidth Allocation Control Protocol" }, 13497c478bd9Sstevel@tonic-gate { 0xc02d, "BAP" }, 13507c478bd9Sstevel@tonic-gate { 0xc081, "Container Control Protocol" }, 13517c478bd9Sstevel@tonic-gate { 0xc223, "Challenge Handshake Authentication Protocol" }, 13527c478bd9Sstevel@tonic-gate { 0xc227, "Extensible Authentication Protocol" }, 13537c478bd9Sstevel@tonic-gate { 0xc281, "Funk Proprietary Authentication Protocol" }, 13547c478bd9Sstevel@tonic-gate { 0, NULL }, 13557c478bd9Sstevel@tonic-gate }; 13567c478bd9Sstevel@tonic-gate 13577c478bd9Sstevel@tonic-gate /* 13587c478bd9Sstevel@tonic-gate * protocol_name - find a name for a PPP protocol. 13597c478bd9Sstevel@tonic-gate */ 13607c478bd9Sstevel@tonic-gate const char * 13617c478bd9Sstevel@tonic-gate protocol_name(proto) 13627c478bd9Sstevel@tonic-gate int proto; 13637c478bd9Sstevel@tonic-gate { 13647c478bd9Sstevel@tonic-gate struct protocol_list *lp; 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate for (lp = protocol_list; lp->proto != 0; ++lp) 13677c478bd9Sstevel@tonic-gate if (proto == lp->proto) 13687c478bd9Sstevel@tonic-gate return (lp->name); 13697c478bd9Sstevel@tonic-gate return (NULL); 13707c478bd9Sstevel@tonic-gate } 13717c478bd9Sstevel@tonic-gate 13727c478bd9Sstevel@tonic-gate static const char *phase_names[] = { PHASE__NAMES }; 13737c478bd9Sstevel@tonic-gate 13747c478bd9Sstevel@tonic-gate const char * 13757c478bd9Sstevel@tonic-gate phase_name(pval) 13767c478bd9Sstevel@tonic-gate int pval; 13777c478bd9Sstevel@tonic-gate { 13787c478bd9Sstevel@tonic-gate static char buf[32]; 13797c478bd9Sstevel@tonic-gate 13807c478bd9Sstevel@tonic-gate if (pval < 0 || pval >= Dim(phase_names)) { 13817c478bd9Sstevel@tonic-gate (void) slprintf(buf, sizeof (buf), "unknown %d", pval); 13827c478bd9Sstevel@tonic-gate return ((const char *)buf); 13837c478bd9Sstevel@tonic-gate } 13847c478bd9Sstevel@tonic-gate return (phase_names[pval]); 13857c478bd9Sstevel@tonic-gate } 13867c478bd9Sstevel@tonic-gate 13877c478bd9Sstevel@tonic-gate /* 13887c478bd9Sstevel@tonic-gate * get_input - called when incoming data is available. 13897c478bd9Sstevel@tonic-gate */ 13907c478bd9Sstevel@tonic-gate static void 13917c478bd9Sstevel@tonic-gate get_input() 13927c478bd9Sstevel@tonic-gate { 13937c478bd9Sstevel@tonic-gate int len, i; 13947c478bd9Sstevel@tonic-gate u_char *p; 13957c478bd9Sstevel@tonic-gate u_short protocol; 13967c478bd9Sstevel@tonic-gate struct protent *protp; 13977c478bd9Sstevel@tonic-gate const char *pname; 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate p = inpacket_buf; /* point to beginning of packet buffer */ 14007c478bd9Sstevel@tonic-gate 14017c478bd9Sstevel@tonic-gate len = read_packet(inpacket_buf); 14027c478bd9Sstevel@tonic-gate if (len < 0) 14037c478bd9Sstevel@tonic-gate return; 14047c478bd9Sstevel@tonic-gate 14057c478bd9Sstevel@tonic-gate if (len == 0) { 14067c478bd9Sstevel@tonic-gate notice("Modem hangup"); 14077c478bd9Sstevel@tonic-gate hungup = 1; 14087c478bd9Sstevel@tonic-gate status = EXIT_HANGUP; 14097c478bd9Sstevel@tonic-gate lcp_lowerdown(0); /* serial link is no longer available */ 14107c478bd9Sstevel@tonic-gate link_terminated(0); 14117c478bd9Sstevel@tonic-gate return; 14127c478bd9Sstevel@tonic-gate } 14137c478bd9Sstevel@tonic-gate 14147c478bd9Sstevel@tonic-gate if (debug /*&& (debugflags & DBG_INPACKET)*/) 14157c478bd9Sstevel@tonic-gate dbglog("rcvd %P", p, len); 14167c478bd9Sstevel@tonic-gate 14177c478bd9Sstevel@tonic-gate if (len < PPP_HDRLEN) { 14187c478bd9Sstevel@tonic-gate dbglog("Discarded short packet (%d < %d)", len, PPP_HDRLEN); 14197c478bd9Sstevel@tonic-gate return; 14207c478bd9Sstevel@tonic-gate } 14217c478bd9Sstevel@tonic-gate 14227c478bd9Sstevel@tonic-gate p += 2; /* Skip address and control */ 14237c478bd9Sstevel@tonic-gate GETSHORT(protocol, p); 14247c478bd9Sstevel@tonic-gate len -= PPP_HDRLEN; 14257c478bd9Sstevel@tonic-gate 14267c478bd9Sstevel@tonic-gate pname = debug ? NULL : protocol_name(protocol); 14277c478bd9Sstevel@tonic-gate 14287c478bd9Sstevel@tonic-gate /* 14297c478bd9Sstevel@tonic-gate * Toss all non-LCP packets unless LCP is in Opened state and 14307c478bd9Sstevel@tonic-gate * discard non-authentication protocols if we're not yet 14317c478bd9Sstevel@tonic-gate * authenticated. 14327c478bd9Sstevel@tonic-gate */ 14337c478bd9Sstevel@tonic-gate if ((protocol != PPP_LCP && 14347c478bd9Sstevel@tonic-gate (phase < PHASE_AUTHENTICATE || phase > PHASE_RUNNING)) || 14357c478bd9Sstevel@tonic-gate (phase <= PHASE_AUTHENTICATE && 14367c478bd9Sstevel@tonic-gate !(protocol == PPP_LCP || protocol == PPP_LQR || 14377c478bd9Sstevel@tonic-gate protocol == PPP_PAP || protocol == PPP_CHAP))) { 14387c478bd9Sstevel@tonic-gate if (pname == NULL) 14397c478bd9Sstevel@tonic-gate dbglog("Discarded proto 0x%x in %s phase", 14407c478bd9Sstevel@tonic-gate protocol, phase_name(phase)); 14417c478bd9Sstevel@tonic-gate else 14427c478bd9Sstevel@tonic-gate dbglog("Discarded %s (0x%x) in %s phase", 14437c478bd9Sstevel@tonic-gate pname, protocol, phase_name(phase)); 14447c478bd9Sstevel@tonic-gate return; 14457c478bd9Sstevel@tonic-gate } 14467c478bd9Sstevel@tonic-gate 14477c478bd9Sstevel@tonic-gate /* 14487c478bd9Sstevel@tonic-gate * Upcall the proper protocol input routine. 14497c478bd9Sstevel@tonic-gate */ 14507c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) { 14517c478bd9Sstevel@tonic-gate if (protp->protocol == protocol && protp->enabled_flag) { 14527c478bd9Sstevel@tonic-gate (*protp->input)(0, p, len); 14537c478bd9Sstevel@tonic-gate return; 14547c478bd9Sstevel@tonic-gate } 14557c478bd9Sstevel@tonic-gate if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag 14567c478bd9Sstevel@tonic-gate && protp->datainput != NULL) { 14577c478bd9Sstevel@tonic-gate (*protp->datainput)(0, p, len); 14587c478bd9Sstevel@tonic-gate return; 14597c478bd9Sstevel@tonic-gate } 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate 14627c478bd9Sstevel@tonic-gate if (debug) { 14637c478bd9Sstevel@tonic-gate if (pname != NULL) 14647c478bd9Sstevel@tonic-gate warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); 14657c478bd9Sstevel@tonic-gate else 14667c478bd9Sstevel@tonic-gate warn("Unsupported protocol 0x%x received", protocol); 14677c478bd9Sstevel@tonic-gate } 14687c478bd9Sstevel@tonic-gate lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); 14697c478bd9Sstevel@tonic-gate } 14707c478bd9Sstevel@tonic-gate 14717c478bd9Sstevel@tonic-gate /* 14727c478bd9Sstevel@tonic-gate * new_phase - signal the start of a new phase of pppd's operation. 14737c478bd9Sstevel@tonic-gate */ 14747c478bd9Sstevel@tonic-gate void 14757c478bd9Sstevel@tonic-gate new_phase(p) 14767c478bd9Sstevel@tonic-gate int p; 14777c478bd9Sstevel@tonic-gate { 14787c478bd9Sstevel@tonic-gate if (new_phase_hook != NULL) 14797c478bd9Sstevel@tonic-gate (*new_phase_hook)(p, phase); 14807c478bd9Sstevel@tonic-gate phase = p; 14817c478bd9Sstevel@tonic-gate } 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate /* 14847c478bd9Sstevel@tonic-gate * die - clean up state and exit with the specified status. 14857c478bd9Sstevel@tonic-gate */ 14867c478bd9Sstevel@tonic-gate void 14877c478bd9Sstevel@tonic-gate die(status) 14887c478bd9Sstevel@tonic-gate int status; 14897c478bd9Sstevel@tonic-gate { 14907c478bd9Sstevel@tonic-gate cleanup(); 14917c478bd9Sstevel@tonic-gate if (phase != PHASE_EXIT) { 14927c478bd9Sstevel@tonic-gate syslog(LOG_INFO, "Exit."); 14937c478bd9Sstevel@tonic-gate new_phase(PHASE_EXIT); 14947c478bd9Sstevel@tonic-gate } 14957c478bd9Sstevel@tonic-gate exit(status); 14967c478bd9Sstevel@tonic-gate } 14977c478bd9Sstevel@tonic-gate 14987c478bd9Sstevel@tonic-gate /* 14997c478bd9Sstevel@tonic-gate * cleanup - restore anything which needs to be restored before we exit 15007c478bd9Sstevel@tonic-gate */ 15017c478bd9Sstevel@tonic-gate static void 15027c478bd9Sstevel@tonic-gate cleanup() 15037c478bd9Sstevel@tonic-gate { 15047c478bd9Sstevel@tonic-gate sys_cleanup(); /* XXX: Need to check if this is okay after close_tty */ 15057c478bd9Sstevel@tonic-gate 15067c478bd9Sstevel@tonic-gate if (fd_ppp >= 0) { 15077c478bd9Sstevel@tonic-gate fd_ppp = -1; 15087c478bd9Sstevel@tonic-gate disestablish_ppp(ttyfd); 15097c478bd9Sstevel@tonic-gate } 15107c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0) 15117c478bd9Sstevel@tonic-gate close_tty(); 15127c478bd9Sstevel@tonic-gate 15137c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0' && unlink(pidfilename) < 0 && errno != ENOENT) 15147c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", pidfilename); 15157c478bd9Sstevel@tonic-gate pidfilename[0] = '\0'; 15167c478bd9Sstevel@tonic-gate if (linkpidfile[0] != '\0' && unlink(linkpidfile) < 0 && errno != ENOENT) 15177c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", linkpidfile); 15187c478bd9Sstevel@tonic-gate linkpidfile[0] = '\0'; 15197c478bd9Sstevel@tonic-gate 15207c478bd9Sstevel@tonic-gate if (locked) { 15217c478bd9Sstevel@tonic-gate locked = 0; 15227c478bd9Sstevel@tonic-gate unlock(); 15237c478bd9Sstevel@tonic-gate } 15247c478bd9Sstevel@tonic-gate 15257c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 15267c478bd9Sstevel@tonic-gate if (pppdb != NULL) { 15277c478bd9Sstevel@tonic-gate cleanup_db(); 15287c478bd9Sstevel@tonic-gate pppdb = NULL; 15297c478bd9Sstevel@tonic-gate } 15307c478bd9Sstevel@tonic-gate #endif 15317c478bd9Sstevel@tonic-gate } 15327c478bd9Sstevel@tonic-gate 15337c478bd9Sstevel@tonic-gate /* 15347c478bd9Sstevel@tonic-gate * close_tty - restore the terminal device and close it. 15357c478bd9Sstevel@tonic-gate */ 15367c478bd9Sstevel@tonic-gate static void 15377c478bd9Sstevel@tonic-gate close_tty() 15387c478bd9Sstevel@tonic-gate { 15397c478bd9Sstevel@tonic-gate int fd = real_ttyfd; 15407c478bd9Sstevel@tonic-gate 15417c478bd9Sstevel@tonic-gate real_ttyfd = -1; 15427c478bd9Sstevel@tonic-gate 15437c478bd9Sstevel@tonic-gate /* drop dtr to hang up */ 15447c478bd9Sstevel@tonic-gate if (!default_device && modem) { 15457c478bd9Sstevel@tonic-gate setdtr(fd, 0); 15467c478bd9Sstevel@tonic-gate /* 15477c478bd9Sstevel@tonic-gate * This sleep is in case the serial port has CLOCAL set by default, 15487c478bd9Sstevel@tonic-gate * and consequently will reassert DTR when we close the device. 15497c478bd9Sstevel@tonic-gate */ 15507c478bd9Sstevel@tonic-gate (void) sleep(1); 15517c478bd9Sstevel@tonic-gate } 15527c478bd9Sstevel@tonic-gate 15537c478bd9Sstevel@tonic-gate restore_tty(fd); 15547c478bd9Sstevel@tonic-gate 15557c478bd9Sstevel@tonic-gate if (tty_mode != (mode_t) -1) { 15567c478bd9Sstevel@tonic-gate if (fchmod(fd, tty_mode) != 0) { 15577c478bd9Sstevel@tonic-gate /* XXX if devnam is a symlink, this will change the link */ 15587c478bd9Sstevel@tonic-gate if (chmod(devnam, tty_mode) != 0) { 15597c478bd9Sstevel@tonic-gate error("Unable to chmod file %s: %m", devnam); 15607c478bd9Sstevel@tonic-gate } 15617c478bd9Sstevel@tonic-gate } 15627c478bd9Sstevel@tonic-gate } 15637c478bd9Sstevel@tonic-gate 15647c478bd9Sstevel@tonic-gate (void) close(fd); 15657c478bd9Sstevel@tonic-gate } 15667c478bd9Sstevel@tonic-gate 15677c478bd9Sstevel@tonic-gate /* 15687c478bd9Sstevel@tonic-gate * update_link_stats - get stats at link termination. 15697c478bd9Sstevel@tonic-gate */ 15707c478bd9Sstevel@tonic-gate void 15717c478bd9Sstevel@tonic-gate update_link_stats(u) 15727c478bd9Sstevel@tonic-gate int u; 15737c478bd9Sstevel@tonic-gate { 15747c478bd9Sstevel@tonic-gate struct timeval now; 15757c478bd9Sstevel@tonic-gate char numbuf[32]; 15767c478bd9Sstevel@tonic-gate 15777c478bd9Sstevel@tonic-gate if (gettimeofday(&now, NULL) >= 0) { 15787c478bd9Sstevel@tonic-gate link_connect_time = now.tv_sec - start_time.tv_sec; 15797c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); 15807c478bd9Sstevel@tonic-gate script_setenv("CONNECT_TIME", numbuf, 0); 15817c478bd9Sstevel@tonic-gate } else { 15827c478bd9Sstevel@tonic-gate link_connect_time = 0; 15837c478bd9Sstevel@tonic-gate } 15847c478bd9Sstevel@tonic-gate 15857c478bd9Sstevel@tonic-gate if (get_ppp_stats(u, &link_stats)) { 15867c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F, 15877c478bd9Sstevel@tonic-gate link_stats.bytes_out); 15887c478bd9Sstevel@tonic-gate script_setenv("BYTES_SENT", numbuf, 0); 15897c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F, 15907c478bd9Sstevel@tonic-gate link_stats.bytes_in); 15917c478bd9Sstevel@tonic-gate script_setenv("BYTES_RCVD", numbuf, 0); 15927c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F, 15937c478bd9Sstevel@tonic-gate link_stats.pkts_in); 15947c478bd9Sstevel@tonic-gate script_setenv("PKTS_RCVD", numbuf, 0); 15957c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F, 15967c478bd9Sstevel@tonic-gate link_stats.pkts_out); 15977c478bd9Sstevel@tonic-gate script_setenv("PKTS_SENT", numbuf, 0); 15987c478bd9Sstevel@tonic-gate link_stats_valid = 1; 15997c478bd9Sstevel@tonic-gate } 16007c478bd9Sstevel@tonic-gate } 16017c478bd9Sstevel@tonic-gate 16027c478bd9Sstevel@tonic-gate 16037c478bd9Sstevel@tonic-gate struct callout { 16047c478bd9Sstevel@tonic-gate struct timeval c_time; /* time at which to call routine */ 16057c478bd9Sstevel@tonic-gate void *c_arg; /* argument to routine */ 16067c478bd9Sstevel@tonic-gate void (*c_func) __P((void *)); /* routine */ 16077c478bd9Sstevel@tonic-gate struct callout *c_next; 16087c478bd9Sstevel@tonic-gate }; 16097c478bd9Sstevel@tonic-gate 16107c478bd9Sstevel@tonic-gate static struct callout *callout = NULL; /* Callout list */ 16117c478bd9Sstevel@tonic-gate static struct timeval timenow; /* Current time */ 16127c478bd9Sstevel@tonic-gate 16137c478bd9Sstevel@tonic-gate /* 16147c478bd9Sstevel@tonic-gate * timeout - Schedule a timeout. 16157c478bd9Sstevel@tonic-gate * 16167c478bd9Sstevel@tonic-gate * Note that this timeout takes the number of seconds, NOT hz (as in 16177c478bd9Sstevel@tonic-gate * the kernel). 16187c478bd9Sstevel@tonic-gate */ 16197c478bd9Sstevel@tonic-gate void 16207c478bd9Sstevel@tonic-gate timeout(func, arg, time) 16217c478bd9Sstevel@tonic-gate void (*func) __P((void *)); 16227c478bd9Sstevel@tonic-gate void *arg; 16237c478bd9Sstevel@tonic-gate int time; 16247c478bd9Sstevel@tonic-gate { 16257c478bd9Sstevel@tonic-gate struct callout *newp, *p, **pp; 16267c478bd9Sstevel@tonic-gate 16277c478bd9Sstevel@tonic-gate MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); 16287c478bd9Sstevel@tonic-gate 16297c478bd9Sstevel@tonic-gate /* 16307c478bd9Sstevel@tonic-gate * Allocate timeout. 16317c478bd9Sstevel@tonic-gate */ 16327c478bd9Sstevel@tonic-gate if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) 16337c478bd9Sstevel@tonic-gate novm("callout structure for timeout."); 16347c478bd9Sstevel@tonic-gate newp->c_arg = arg; 16357c478bd9Sstevel@tonic-gate newp->c_func = func; 16367c478bd9Sstevel@tonic-gate (void) gettimeofday(&timenow, NULL); 16377c478bd9Sstevel@tonic-gate newp->c_time.tv_sec = timenow.tv_sec + time; 16387c478bd9Sstevel@tonic-gate newp->c_time.tv_usec = timenow.tv_usec; 16397c478bd9Sstevel@tonic-gate 16407c478bd9Sstevel@tonic-gate /* 16417c478bd9Sstevel@tonic-gate * Find correct place and link it in. 16427c478bd9Sstevel@tonic-gate */ 16437c478bd9Sstevel@tonic-gate for (pp = &callout; (p = *pp) != NULL; pp = &p->c_next) 16447c478bd9Sstevel@tonic-gate if (newp->c_time.tv_sec < p->c_time.tv_sec 16457c478bd9Sstevel@tonic-gate || (newp->c_time.tv_sec == p->c_time.tv_sec 16467c478bd9Sstevel@tonic-gate && newp->c_time.tv_usec < p->c_time.tv_usec)) 16477c478bd9Sstevel@tonic-gate break; 16487c478bd9Sstevel@tonic-gate newp->c_next = p; 16497c478bd9Sstevel@tonic-gate *pp = newp; 16507c478bd9Sstevel@tonic-gate } 16517c478bd9Sstevel@tonic-gate 16527c478bd9Sstevel@tonic-gate 16537c478bd9Sstevel@tonic-gate /* 16547c478bd9Sstevel@tonic-gate * untimeout - Unschedule a timeout. 16557c478bd9Sstevel@tonic-gate */ 16567c478bd9Sstevel@tonic-gate void 16577c478bd9Sstevel@tonic-gate untimeout(func, arg) 16587c478bd9Sstevel@tonic-gate void (*func) __P((void *)); 16597c478bd9Sstevel@tonic-gate void *arg; 16607c478bd9Sstevel@tonic-gate { 16617c478bd9Sstevel@tonic-gate struct callout **copp, *freep; 16627c478bd9Sstevel@tonic-gate 16637c478bd9Sstevel@tonic-gate MAINDEBUG(("Untimeout %p:%p.", func, arg)); 16647c478bd9Sstevel@tonic-gate 16657c478bd9Sstevel@tonic-gate /* 16667c478bd9Sstevel@tonic-gate * Find first matching timeout and remove it from the list. 16677c478bd9Sstevel@tonic-gate */ 16687c478bd9Sstevel@tonic-gate for (copp = &callout; (freep = *copp) != NULL; copp = &freep->c_next) 16697c478bd9Sstevel@tonic-gate if (freep->c_func == func && freep->c_arg == arg) { 16707c478bd9Sstevel@tonic-gate *copp = freep->c_next; 16717c478bd9Sstevel@tonic-gate free((char *) freep); 16727c478bd9Sstevel@tonic-gate break; 16737c478bd9Sstevel@tonic-gate } 16747c478bd9Sstevel@tonic-gate } 16757c478bd9Sstevel@tonic-gate 16767c478bd9Sstevel@tonic-gate 16777c478bd9Sstevel@tonic-gate /* 16787c478bd9Sstevel@tonic-gate * calltimeout - Call any timeout routines which are now due. 16797c478bd9Sstevel@tonic-gate */ 16807c478bd9Sstevel@tonic-gate static void 16817c478bd9Sstevel@tonic-gate calltimeout() 16827c478bd9Sstevel@tonic-gate { 16837c478bd9Sstevel@tonic-gate struct callout *p; 16847c478bd9Sstevel@tonic-gate 16857c478bd9Sstevel@tonic-gate while (callout != NULL) { 16867c478bd9Sstevel@tonic-gate p = callout; 16877c478bd9Sstevel@tonic-gate 16887c478bd9Sstevel@tonic-gate if (gettimeofday(&timenow, NULL) < 0) 16897c478bd9Sstevel@tonic-gate fatal("Failed to get time of day: %m"); 16907c478bd9Sstevel@tonic-gate if (!(p->c_time.tv_sec < timenow.tv_sec 16917c478bd9Sstevel@tonic-gate || (p->c_time.tv_sec == timenow.tv_sec 16927c478bd9Sstevel@tonic-gate && p->c_time.tv_usec <= timenow.tv_usec))) 16937c478bd9Sstevel@tonic-gate break; /* no, it's not time yet */ 16947c478bd9Sstevel@tonic-gate 16957c478bd9Sstevel@tonic-gate callout = p->c_next; 16967c478bd9Sstevel@tonic-gate (*p->c_func)(p->c_arg); 16977c478bd9Sstevel@tonic-gate 16987c478bd9Sstevel@tonic-gate free((char *) p); 16997c478bd9Sstevel@tonic-gate } 17007c478bd9Sstevel@tonic-gate } 17017c478bd9Sstevel@tonic-gate 17027c478bd9Sstevel@tonic-gate 17037c478bd9Sstevel@tonic-gate /* 17047c478bd9Sstevel@tonic-gate * timeleft - return the length of time until the next timeout is due. 17057c478bd9Sstevel@tonic-gate */ 17067c478bd9Sstevel@tonic-gate static struct timeval * 17077c478bd9Sstevel@tonic-gate timeleft(tvp) 17087c478bd9Sstevel@tonic-gate struct timeval *tvp; 17097c478bd9Sstevel@tonic-gate { 17107c478bd9Sstevel@tonic-gate if (callout == NULL) 17117c478bd9Sstevel@tonic-gate return (NULL); 17127c478bd9Sstevel@tonic-gate 17137c478bd9Sstevel@tonic-gate (void) gettimeofday(&timenow, NULL); 17147c478bd9Sstevel@tonic-gate tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; 17157c478bd9Sstevel@tonic-gate tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; 17167c478bd9Sstevel@tonic-gate if (tvp->tv_usec < 0) { 17177c478bd9Sstevel@tonic-gate tvp->tv_usec += 1000000; 17187c478bd9Sstevel@tonic-gate tvp->tv_sec -= 1; 17197c478bd9Sstevel@tonic-gate } 17207c478bd9Sstevel@tonic-gate if (tvp->tv_sec < 0) 17217c478bd9Sstevel@tonic-gate tvp->tv_sec = tvp->tv_usec = 0; 17227c478bd9Sstevel@tonic-gate 17237c478bd9Sstevel@tonic-gate return (tvp); 17247c478bd9Sstevel@tonic-gate } 17257c478bd9Sstevel@tonic-gate 17267c478bd9Sstevel@tonic-gate 17277c478bd9Sstevel@tonic-gate /* 17287c478bd9Sstevel@tonic-gate * kill_my_pg - send a signal to our process group, and ignore it ourselves. 17297c478bd9Sstevel@tonic-gate */ 17307c478bd9Sstevel@tonic-gate static void 17317c478bd9Sstevel@tonic-gate kill_my_pg(sig) 17327c478bd9Sstevel@tonic-gate int sig; 17337c478bd9Sstevel@tonic-gate { 17347c478bd9Sstevel@tonic-gate struct sigaction act, oldact; 17357c478bd9Sstevel@tonic-gate sigset_t mask; 17367c478bd9Sstevel@tonic-gate 17377c478bd9Sstevel@tonic-gate BZERO(&act, sizeof (act)); 17387c478bd9Sstevel@tonic-gate act.sa_handler = SIG_IGN; 17397c478bd9Sstevel@tonic-gate (void) sigemptyset(&mask); 17407c478bd9Sstevel@tonic-gate (void) sigaddset(&mask, sig); 17417c478bd9Sstevel@tonic-gate /* 17427c478bd9Sstevel@tonic-gate * Ignore signal 'sig' temporarily, before finally re-activating the 17437c478bd9Sstevel@tonic-gate * original handler. We need to do it in the following sequence, since 17447c478bd9Sstevel@tonic-gate * otherwise the signal handler for 'sig' will be called forever. 17457c478bd9Sstevel@tonic-gate */ 17467c478bd9Sstevel@tonic-gate if (sigaction(sig, &act, &oldact) < 0) { 17477c478bd9Sstevel@tonic-gate fatal("kill_my_pg: couldn't establish signal handler (%d): %m", sig); 17487c478bd9Sstevel@tonic-gate } 17497c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &mask, NULL); 17507c478bd9Sstevel@tonic-gate /* 17517c478bd9Sstevel@tonic-gate * Send signal 'sig' to all processes whose process group ID is equal 17527c478bd9Sstevel@tonic-gate * to the process group ID of the sender. 17537c478bd9Sstevel@tonic-gate */ 17547c478bd9Sstevel@tonic-gate (void) kill(0, sig); 17557c478bd9Sstevel@tonic-gate if (sigaction(sig, &oldact, NULL) < 0) { 17567c478bd9Sstevel@tonic-gate fatal("kill_my_pg: couldn't establish signal handler (%d): %m", sig); 17577c478bd9Sstevel@tonic-gate } 17587c478bd9Sstevel@tonic-gate } 17597c478bd9Sstevel@tonic-gate 17607c478bd9Sstevel@tonic-gate 17617c478bd9Sstevel@tonic-gate /* 17627c478bd9Sstevel@tonic-gate * hup - Catch SIGHUP signal. 17637c478bd9Sstevel@tonic-gate * 17647c478bd9Sstevel@tonic-gate * Indicates that the physical layer has been disconnected. 17657c478bd9Sstevel@tonic-gate * We don't rely on this indication; if the user has sent this 17667c478bd9Sstevel@tonic-gate * signal, we just take the link down. 17677c478bd9Sstevel@tonic-gate */ 17687c478bd9Sstevel@tonic-gate static void 17697c478bd9Sstevel@tonic-gate hup(sig) 17707c478bd9Sstevel@tonic-gate int sig; 17717c478bd9Sstevel@tonic-gate { 17727c478bd9Sstevel@tonic-gate info("Hangup (SIGHUP)"); 17737c478bd9Sstevel@tonic-gate kill_link = 1; 17747c478bd9Sstevel@tonic-gate if (status != EXIT_HANGUP) 17757c478bd9Sstevel@tonic-gate status = EXIT_USER_REQUEST; 17767c478bd9Sstevel@tonic-gate if (conn_running > 0) 17777c478bd9Sstevel@tonic-gate /* Send the signal to the [dis]connector process(es) also */ 17787c478bd9Sstevel@tonic-gate kill_my_pg(sig); 17797c478bd9Sstevel@tonic-gate if (charshunt_pid) 17807c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, sig); 17817c478bd9Sstevel@tonic-gate if (waiting) 17827c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1); 17837c478bd9Sstevel@tonic-gate } 17847c478bd9Sstevel@tonic-gate 17857c478bd9Sstevel@tonic-gate 17867c478bd9Sstevel@tonic-gate /* 17877c478bd9Sstevel@tonic-gate * term - Catch SIGTERM signal and SIGINT signal (^C/del). 17887c478bd9Sstevel@tonic-gate * 17897c478bd9Sstevel@tonic-gate * Indicates that we should initiate a graceful disconnect and exit. 17907c478bd9Sstevel@tonic-gate */ 17917c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 17927c478bd9Sstevel@tonic-gate static void 17937c478bd9Sstevel@tonic-gate term(sig) 17947c478bd9Sstevel@tonic-gate int sig; 17957c478bd9Sstevel@tonic-gate { 17967c478bd9Sstevel@tonic-gate info("Terminating on signal %d.", sig); 17977c478bd9Sstevel@tonic-gate persist = 0; /* don't try to restart */ 17987c478bd9Sstevel@tonic-gate kill_link = 1; 17997c478bd9Sstevel@tonic-gate status = EXIT_USER_REQUEST; 18007c478bd9Sstevel@tonic-gate if (conn_running > 0) 18017c478bd9Sstevel@tonic-gate /* Send the signal to the [dis]connector process(es) also */ 18027c478bd9Sstevel@tonic-gate kill_my_pg(sig); 18037c478bd9Sstevel@tonic-gate if (charshunt_pid) 18047c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, sig); 18057c478bd9Sstevel@tonic-gate if (waiting) 18067c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1); 18077c478bd9Sstevel@tonic-gate } 18087c478bd9Sstevel@tonic-gate 18097c478bd9Sstevel@tonic-gate 18107c478bd9Sstevel@tonic-gate /* 18117c478bd9Sstevel@tonic-gate * chld - Catch SIGCHLD signal. 18127c478bd9Sstevel@tonic-gate * Sets a flag so we will call reap_kids in the mainline. 18137c478bd9Sstevel@tonic-gate */ 18147c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18157c478bd9Sstevel@tonic-gate static void 18167c478bd9Sstevel@tonic-gate chld(sig) 18177c478bd9Sstevel@tonic-gate int sig; 18187c478bd9Sstevel@tonic-gate { 18197c478bd9Sstevel@tonic-gate got_sigchld = 1; 18207c478bd9Sstevel@tonic-gate if (waiting) 18217c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1); 18227c478bd9Sstevel@tonic-gate } 18237c478bd9Sstevel@tonic-gate 18247c478bd9Sstevel@tonic-gate /* 18257c478bd9Sstevel@tonic-gate * toggle_debug - Catch SIGUSR1 signal. 18267c478bd9Sstevel@tonic-gate * 18277c478bd9Sstevel@tonic-gate * Toggle debug flag. 18287c478bd9Sstevel@tonic-gate */ 18297c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18307c478bd9Sstevel@tonic-gate static void 18317c478bd9Sstevel@tonic-gate toggle_debug(sig) 18327c478bd9Sstevel@tonic-gate int sig; 18337c478bd9Sstevel@tonic-gate { 18347c478bd9Sstevel@tonic-gate if (debug) { 18357c478bd9Sstevel@tonic-gate print_ncpstate(0, NULL); 18367c478bd9Sstevel@tonic-gate dbglog("debug logging disabled"); 18377c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_WARNING)); 18387c478bd9Sstevel@tonic-gate debug = 0; 18397c478bd9Sstevel@tonic-gate } else { 18407c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_DEBUG)); 18417c478bd9Sstevel@tonic-gate dbglog("debug logging enabled"); 18427c478bd9Sstevel@tonic-gate print_ncpstate(0, NULL); 18437c478bd9Sstevel@tonic-gate debug = 1; 18447c478bd9Sstevel@tonic-gate } 18457c478bd9Sstevel@tonic-gate } 18467c478bd9Sstevel@tonic-gate 18477c478bd9Sstevel@tonic-gate 18487c478bd9Sstevel@tonic-gate /* 18497c478bd9Sstevel@tonic-gate * open_ccp - Catch SIGUSR2 signal. 18507c478bd9Sstevel@tonic-gate * 18517c478bd9Sstevel@tonic-gate * Try to (re)negotiate compression. 18527c478bd9Sstevel@tonic-gate */ 18537c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18547c478bd9Sstevel@tonic-gate static void 18557c478bd9Sstevel@tonic-gate open_ccp(sig) 18567c478bd9Sstevel@tonic-gate int sig; 18577c478bd9Sstevel@tonic-gate { 18587c478bd9Sstevel@tonic-gate open_ccp_flag = 1; 18597c478bd9Sstevel@tonic-gate if (waiting) 18607c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1); 18617c478bd9Sstevel@tonic-gate } 18627c478bd9Sstevel@tonic-gate 18637c478bd9Sstevel@tonic-gate 18647c478bd9Sstevel@tonic-gate /* 18657c478bd9Sstevel@tonic-gate * bad_signal - We've caught a fatal signal. Clean up state and exit. 18667c478bd9Sstevel@tonic-gate */ 18677c478bd9Sstevel@tonic-gate static void 18687c478bd9Sstevel@tonic-gate bad_signal(sig) 18697c478bd9Sstevel@tonic-gate int sig; 18707c478bd9Sstevel@tonic-gate { 18717c478bd9Sstevel@tonic-gate static int crashed = 0; 18727c478bd9Sstevel@tonic-gate 18737c478bd9Sstevel@tonic-gate if (crashed) 18747c478bd9Sstevel@tonic-gate _exit(127); 18757c478bd9Sstevel@tonic-gate crashed = 1; 18767c478bd9Sstevel@tonic-gate error("Fatal signal %d", sig); 18777c478bd9Sstevel@tonic-gate if (conn_running > 0) 18787c478bd9Sstevel@tonic-gate kill_my_pg(SIGTERM); 18797c478bd9Sstevel@tonic-gate if (charshunt_pid) 18807c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, SIGTERM); 18817c478bd9Sstevel@tonic-gate die(127); 18827c478bd9Sstevel@tonic-gate } 18837c478bd9Sstevel@tonic-gate 18847c478bd9Sstevel@tonic-gate 18857c478bd9Sstevel@tonic-gate /* 18867c478bd9Sstevel@tonic-gate * device_script - run a program to talk to the serial device 18877c478bd9Sstevel@tonic-gate * (e.g. to run the connector or disconnector script). 18887c478bd9Sstevel@tonic-gate */ 18897c478bd9Sstevel@tonic-gate static int 18907c478bd9Sstevel@tonic-gate device_script(program, in, out, dont_wait, optname) 18917c478bd9Sstevel@tonic-gate char *program; 18927c478bd9Sstevel@tonic-gate int in, out; 18937c478bd9Sstevel@tonic-gate int dont_wait; 18947c478bd9Sstevel@tonic-gate char *optname; 18957c478bd9Sstevel@tonic-gate { 18967c478bd9Sstevel@tonic-gate pid_t pid; 18977c478bd9Sstevel@tonic-gate int status = -1; 18987c478bd9Sstevel@tonic-gate int errfd; 18997c478bd9Sstevel@tonic-gate int envpipe[2]; 19007c478bd9Sstevel@tonic-gate 19017c478bd9Sstevel@tonic-gate envpipe[0] = envpipe[1] = -1; 19027c478bd9Sstevel@tonic-gate if (!dont_wait && device_pipe_hook != NULL && pipe(envpipe) == -1) { 19037c478bd9Sstevel@tonic-gate error("Cannot create pipe for child: %m"); 19047c478bd9Sstevel@tonic-gate return (-1); 19057c478bd9Sstevel@tonic-gate } 19067c478bd9Sstevel@tonic-gate 19077c478bd9Sstevel@tonic-gate ++conn_running; 19087c478bd9Sstevel@tonic-gate pid = fork(); 19097c478bd9Sstevel@tonic-gate 19107c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) { 19117c478bd9Sstevel@tonic-gate --conn_running; 19127c478bd9Sstevel@tonic-gate error("Failed to create child process: %m"); 19137c478bd9Sstevel@tonic-gate return (-1); 19147c478bd9Sstevel@tonic-gate } 19157c478bd9Sstevel@tonic-gate 19167c478bd9Sstevel@tonic-gate if (pid == (pid_t)0) { 19177c478bd9Sstevel@tonic-gate sys_close(); 19187c478bd9Sstevel@tonic-gate closelog(); 19197c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0) { 19207c478bd9Sstevel@tonic-gate if (envpipe[1] <= 2) 19217c478bd9Sstevel@tonic-gate envpipe[1] = dup(envpipe[1]); 19227c478bd9Sstevel@tonic-gate (void) close(envpipe[0]); 19237c478bd9Sstevel@tonic-gate } 19247c478bd9Sstevel@tonic-gate if (in == 2) { 19257c478bd9Sstevel@tonic-gate /* aargh!!! */ 19267c478bd9Sstevel@tonic-gate int newin = dup(in); 19277c478bd9Sstevel@tonic-gate if (in == out) 19287c478bd9Sstevel@tonic-gate out = newin; 19297c478bd9Sstevel@tonic-gate in = newin; 19307c478bd9Sstevel@tonic-gate } else if (out == 2) { 19317c478bd9Sstevel@tonic-gate out = dup(out); 19327c478bd9Sstevel@tonic-gate } 19337c478bd9Sstevel@tonic-gate if (log_to_fd >= 0) { 19347c478bd9Sstevel@tonic-gate if (log_to_fd != 2) { 19357c478bd9Sstevel@tonic-gate if (dup2(log_to_fd, 2) < 0) 19367c478bd9Sstevel@tonic-gate error("dup2(log_to_fd, STDERR) failed: %m"); 19377c478bd9Sstevel@tonic-gate } 19387c478bd9Sstevel@tonic-gate } else { 19397c478bd9Sstevel@tonic-gate (void) close(2); 19407c478bd9Sstevel@tonic-gate errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600); 19417c478bd9Sstevel@tonic-gate if (errfd >= 0 && errfd != 2) { 19427c478bd9Sstevel@tonic-gate if (dup2(errfd, 2) < 0) 19437c478bd9Sstevel@tonic-gate error("dup2(errfd, STDERR) failed: %m"); 19447c478bd9Sstevel@tonic-gate (void) close(errfd); 19457c478bd9Sstevel@tonic-gate } 19467c478bd9Sstevel@tonic-gate } 19477c478bd9Sstevel@tonic-gate if (in != 0) { 19487c478bd9Sstevel@tonic-gate if (out == 0) 19497c478bd9Sstevel@tonic-gate out = dup(out); 19507c478bd9Sstevel@tonic-gate if (dup2(in, 0) < 0) 19517c478bd9Sstevel@tonic-gate error("dup2(in, STDIN) failed: %m"); 19527c478bd9Sstevel@tonic-gate } 19537c478bd9Sstevel@tonic-gate if (out != 1) { 19547c478bd9Sstevel@tonic-gate if (dup2(out, 1) < 0) 19557c478bd9Sstevel@tonic-gate error("dup2(out, STDOUT) failed: %m"); 19567c478bd9Sstevel@tonic-gate } 19577c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0 && dup2(envpipe[1], 3) < 0) 19587c478bd9Sstevel@tonic-gate error("dup2(pipe, pipeout) failed: %m"); 19597c478bd9Sstevel@tonic-gate if (real_ttyfd > 2) 19607c478bd9Sstevel@tonic-gate (void) close(real_ttyfd); 19617c478bd9Sstevel@tonic-gate if (pty_master > 2) 19627c478bd9Sstevel@tonic-gate (void) close(pty_master); 19637c478bd9Sstevel@tonic-gate if (pty_slave > 2) { 19647c478bd9Sstevel@tonic-gate (void) close(pty_slave); 19657c478bd9Sstevel@tonic-gate pty_slave = -1; 19667c478bd9Sstevel@tonic-gate } 19677c478bd9Sstevel@tonic-gate (void) setuid(uid); 19687c478bd9Sstevel@tonic-gate if (getuid() != uid) { 19697c478bd9Sstevel@tonic-gate error("setuid failed"); 19707c478bd9Sstevel@tonic-gate exit(1); 19717c478bd9Sstevel@tonic-gate } 19727c478bd9Sstevel@tonic-gate (void) setgid(getgid()); 19737c478bd9Sstevel@tonic-gate if (script_env != NULL) { 19747c478bd9Sstevel@tonic-gate while (*script_env != NULL) { 19757c478bd9Sstevel@tonic-gate if (putenv(*script_env) == -1) 19767c478bd9Sstevel@tonic-gate warn("unable to set %s for %s: %m", *script_env, program); 19777c478bd9Sstevel@tonic-gate script_env++; 19787c478bd9Sstevel@tonic-gate } 19797c478bd9Sstevel@tonic-gate } 19807c478bd9Sstevel@tonic-gate (void) execl("/bin/sh", "sh", "-c", program, (char *)0); 19817c478bd9Sstevel@tonic-gate error("could not exec /bin/sh: %m"); 19827c478bd9Sstevel@tonic-gate exit(99); 19837c478bd9Sstevel@tonic-gate /* NOTREACHED */ 19847c478bd9Sstevel@tonic-gate } 19857c478bd9Sstevel@tonic-gate 19867c478bd9Sstevel@tonic-gate if (debug) 19877c478bd9Sstevel@tonic-gate dbglog("%s option: '%s' started (pid %d)", optname, program, pid); 19887c478bd9Sstevel@tonic-gate if (dont_wait) { 19897c478bd9Sstevel@tonic-gate record_child(pid, program, NULL, NULL); 19907c478bd9Sstevel@tonic-gate status = 0; 19917c478bd9Sstevel@tonic-gate } else { 19927c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0) { 19937c478bd9Sstevel@tonic-gate (void) close(envpipe[1]); 19947c478bd9Sstevel@tonic-gate (*device_pipe_hook)(envpipe[0]); 19957c478bd9Sstevel@tonic-gate } 19967c478bd9Sstevel@tonic-gate while (waitpid(pid, &status, 0) < 0) { 19977c478bd9Sstevel@tonic-gate if (errno == EINTR) 19987c478bd9Sstevel@tonic-gate continue; 19997c478bd9Sstevel@tonic-gate fatal("error waiting for (dis)connection process: %m"); 20007c478bd9Sstevel@tonic-gate } 20017c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0) 20027c478bd9Sstevel@tonic-gate (void) close(envpipe[0]); 20037c478bd9Sstevel@tonic-gate --conn_running; 20047c478bd9Sstevel@tonic-gate } 20057c478bd9Sstevel@tonic-gate 20067c478bd9Sstevel@tonic-gate return (status == 0 ? 0 : -1); 20077c478bd9Sstevel@tonic-gate } 20087c478bd9Sstevel@tonic-gate 20097c478bd9Sstevel@tonic-gate 20107c478bd9Sstevel@tonic-gate /* 20117c478bd9Sstevel@tonic-gate * run-program - execute a program with given arguments, 20127c478bd9Sstevel@tonic-gate * but don't wait for it. 20137c478bd9Sstevel@tonic-gate * If the program can't be executed, logs an error unless 20147c478bd9Sstevel@tonic-gate * must_exist is 0 and the program file doesn't exist. 20157c478bd9Sstevel@tonic-gate * Returns -1 if it couldn't fork, 0 if the file doesn't exist 20167c478bd9Sstevel@tonic-gate * or isn't an executable plain file, or the process ID of the child. 20177c478bd9Sstevel@tonic-gate * If done != NULL, (*done)(arg, int) will be called later (within 20187c478bd9Sstevel@tonic-gate * reap_kids) if this routine returns value > 0. 20197c478bd9Sstevel@tonic-gate */ 20207c478bd9Sstevel@tonic-gate pid_t 20217c478bd9Sstevel@tonic-gate run_program(prog, args, must_exist, done, arg) 20227c478bd9Sstevel@tonic-gate char *prog; 20237c478bd9Sstevel@tonic-gate char **args; 20247c478bd9Sstevel@tonic-gate int must_exist; 20257c478bd9Sstevel@tonic-gate void (*done) __P((void *arg, int status)); 20267c478bd9Sstevel@tonic-gate void *arg; 20277c478bd9Sstevel@tonic-gate { 20287c478bd9Sstevel@tonic-gate pid_t pid; 20297c478bd9Sstevel@tonic-gate struct stat sbuf; 20307c478bd9Sstevel@tonic-gate int retv; 20317c478bd9Sstevel@tonic-gate 20327c478bd9Sstevel@tonic-gate /* 20337c478bd9Sstevel@tonic-gate * First check if the file exists and is executable. 20347c478bd9Sstevel@tonic-gate * We don't use access() because that would use the 20357c478bd9Sstevel@tonic-gate * real user-id, which might not be root, and the script 20367c478bd9Sstevel@tonic-gate * might be accessible only to root. 20377c478bd9Sstevel@tonic-gate */ 20387c478bd9Sstevel@tonic-gate errno = EINVAL; 20397c478bd9Sstevel@tonic-gate if (stat(prog, &sbuf) < 0 || !S_ISREG(sbuf.st_mode) 20407c478bd9Sstevel@tonic-gate || (sbuf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) { 20417c478bd9Sstevel@tonic-gate if (must_exist || errno != ENOENT) 20427c478bd9Sstevel@tonic-gate warn("Can't execute %s: %m", prog); 20437c478bd9Sstevel@tonic-gate return (0); 20447c478bd9Sstevel@tonic-gate } 20457c478bd9Sstevel@tonic-gate 20467c478bd9Sstevel@tonic-gate if (updown_script_hook != NULL) { 20477c478bd9Sstevel@tonic-gate retv = (*updown_script_hook)((const char ***)&args); 20487c478bd9Sstevel@tonic-gate if (retv == -1) { 20497c478bd9Sstevel@tonic-gate return (-1); 20507c478bd9Sstevel@tonic-gate } 20517c478bd9Sstevel@tonic-gate } 20527c478bd9Sstevel@tonic-gate 20537c478bd9Sstevel@tonic-gate pid = fork(); 20547c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) { 20557c478bd9Sstevel@tonic-gate error("Failed to create child process for %s: %m", prog); 20567c478bd9Sstevel@tonic-gate return (-1); 20577c478bd9Sstevel@tonic-gate } 20587c478bd9Sstevel@tonic-gate if (pid == (pid_t)0) { 20597c478bd9Sstevel@tonic-gate int new_fd; 20607c478bd9Sstevel@tonic-gate 20617c478bd9Sstevel@tonic-gate /* Leave the current location */ 20627c478bd9Sstevel@tonic-gate (void) setsid(); /* No controlling tty. */ 20637c478bd9Sstevel@tonic-gate (void) umask (S_IRWXG|S_IRWXO); 20647c478bd9Sstevel@tonic-gate (void) chdir ("/"); /* no current directory. */ 20657c478bd9Sstevel@tonic-gate (void) setuid(0); /* set real UID = root */ 20667c478bd9Sstevel@tonic-gate (void) setgid(getegid()); 20677c478bd9Sstevel@tonic-gate 20687c478bd9Sstevel@tonic-gate /* Ensure that nothing of our device environment is inherited. */ 20697c478bd9Sstevel@tonic-gate sys_close(); 20707c478bd9Sstevel@tonic-gate closelog(); 20717c478bd9Sstevel@tonic-gate (void) close(0); 20727c478bd9Sstevel@tonic-gate (void) close(1); 20737c478bd9Sstevel@tonic-gate (void) close(2); 20747c478bd9Sstevel@tonic-gate (void) close(ttyfd); /* tty interface to the ppp device */ 20757c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0) 20767c478bd9Sstevel@tonic-gate (void) close(real_ttyfd); 20777c478bd9Sstevel@tonic-gate 20787c478bd9Sstevel@tonic-gate /* Don't pass handles to the PPP device, even by accident. */ 20797c478bd9Sstevel@tonic-gate new_fd = open (_PATH_DEVNULL, O_RDWR); 20807c478bd9Sstevel@tonic-gate if (new_fd >= 0) { 20817c478bd9Sstevel@tonic-gate if (new_fd != 0) { 20827c478bd9Sstevel@tonic-gate if (dup2(new_fd, 0) < 0) /* stdin <- /dev/null */ 20837c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDIN) failed: %m"); 20847c478bd9Sstevel@tonic-gate (void) close(new_fd); 20857c478bd9Sstevel@tonic-gate } 20867c478bd9Sstevel@tonic-gate if (dup2(0, 1) < 0) /* stdout -> /dev/null */ 20877c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDOUT) failed: %m"); 20887c478bd9Sstevel@tonic-gate if (dup2(0, 2) < 0) /* stderr -> /dev/null */ 20897c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDERR) failed: %m"); 20907c478bd9Sstevel@tonic-gate } 20917c478bd9Sstevel@tonic-gate 20927c478bd9Sstevel@tonic-gate #ifdef BSD 20937c478bd9Sstevel@tonic-gate /* Force the priority back to zero if pppd is running higher. */ 20947c478bd9Sstevel@tonic-gate if (setpriority (PRIO_PROCESS, 0, 0) < 0) 20957c478bd9Sstevel@tonic-gate warn("can't reset priority to 0: %m"); 20967c478bd9Sstevel@tonic-gate #endif 20977c478bd9Sstevel@tonic-gate 20987c478bd9Sstevel@tonic-gate /* SysV recommends a second fork at this point. */ 20997c478bd9Sstevel@tonic-gate 21007c478bd9Sstevel@tonic-gate /* run the program */ 21017c478bd9Sstevel@tonic-gate (void) execve(prog, args, script_env); 21027c478bd9Sstevel@tonic-gate if (must_exist || errno != ENOENT) { 21037c478bd9Sstevel@tonic-gate /* have to reopen the log, there's nowhere else 21047c478bd9Sstevel@tonic-gate for the message to go. */ 21057c478bd9Sstevel@tonic-gate reopen_log(); 21067c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Can't execute %s: %m", prog); 21077c478bd9Sstevel@tonic-gate closelog(); 21087c478bd9Sstevel@tonic-gate } 21097c478bd9Sstevel@tonic-gate _exit(-1); 21107c478bd9Sstevel@tonic-gate } 21117c478bd9Sstevel@tonic-gate 21127c478bd9Sstevel@tonic-gate if (debug) 21137c478bd9Sstevel@tonic-gate dbglog("Script %s started (pid %d)", prog, pid); 21147c478bd9Sstevel@tonic-gate record_child(pid, prog, done, arg); 21157c478bd9Sstevel@tonic-gate 21167c478bd9Sstevel@tonic-gate return (pid); 21177c478bd9Sstevel@tonic-gate } 21187c478bd9Sstevel@tonic-gate 21197c478bd9Sstevel@tonic-gate 21207c478bd9Sstevel@tonic-gate /* 21217c478bd9Sstevel@tonic-gate * record_child - add a child process to the list for reap_kids 21227c478bd9Sstevel@tonic-gate * to use. 21237c478bd9Sstevel@tonic-gate */ 21247c478bd9Sstevel@tonic-gate static void 21257c478bd9Sstevel@tonic-gate record_child(pid, prog, done, arg) 21267c478bd9Sstevel@tonic-gate pid_t pid; 21277c478bd9Sstevel@tonic-gate char *prog; 21287c478bd9Sstevel@tonic-gate void (*done) __P((void *, int)); 21297c478bd9Sstevel@tonic-gate void *arg; 21307c478bd9Sstevel@tonic-gate { 21317c478bd9Sstevel@tonic-gate struct subprocess *chp; 21327c478bd9Sstevel@tonic-gate 21337c478bd9Sstevel@tonic-gate ++n_children; 21347c478bd9Sstevel@tonic-gate 21357c478bd9Sstevel@tonic-gate chp = (struct subprocess *) malloc(sizeof(struct subprocess)); 21367c478bd9Sstevel@tonic-gate if (chp == NULL) { 21377c478bd9Sstevel@tonic-gate warn("losing track of %s process", prog); 21387c478bd9Sstevel@tonic-gate } else { 21397c478bd9Sstevel@tonic-gate chp->pid = pid; 21407c478bd9Sstevel@tonic-gate chp->prog = prog; 21417c478bd9Sstevel@tonic-gate chp->done = done; 21427c478bd9Sstevel@tonic-gate chp->arg = arg; 21437c478bd9Sstevel@tonic-gate chp->next = children; 21447c478bd9Sstevel@tonic-gate children = chp; 21457c478bd9Sstevel@tonic-gate } 21467c478bd9Sstevel@tonic-gate } 21477c478bd9Sstevel@tonic-gate 21487c478bd9Sstevel@tonic-gate 21497c478bd9Sstevel@tonic-gate /* 21507c478bd9Sstevel@tonic-gate * reap_kids - get status from any dead child processes, 21517c478bd9Sstevel@tonic-gate * and log a message for abnormal terminations. 21527c478bd9Sstevel@tonic-gate */ 21537c478bd9Sstevel@tonic-gate static int 21547c478bd9Sstevel@tonic-gate reap_kids(waitfor) 21557c478bd9Sstevel@tonic-gate int waitfor; 21567c478bd9Sstevel@tonic-gate { 21577c478bd9Sstevel@tonic-gate pid_t pid; 21587c478bd9Sstevel@tonic-gate int status, i; 21597c478bd9Sstevel@tonic-gate struct subprocess *chp, **prevp; 21607c478bd9Sstevel@tonic-gate 21617c478bd9Sstevel@tonic-gate got_sigchld = 0; 21627c478bd9Sstevel@tonic-gate if (n_children == 0) 21637c478bd9Sstevel@tonic-gate return (0); 21647c478bd9Sstevel@tonic-gate 21657c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ 21667c478bd9Sstevel@tonic-gate while (1) { 21677c478bd9Sstevel@tonic-gate pid = waitpid(-1, &status, (waitfor ? 0 : WNOHANG)); 21687c478bd9Sstevel@tonic-gate if (pid == 0) { 21697c478bd9Sstevel@tonic-gate break; /* return 0 */ 21707c478bd9Sstevel@tonic-gate } else if (pid == -1) { 21717c478bd9Sstevel@tonic-gate if (errno == EINTR) 21727c478bd9Sstevel@tonic-gate continue; 21737c478bd9Sstevel@tonic-gate if (errno != ECHILD) 21747c478bd9Sstevel@tonic-gate error("Error waiting for child process: %m"); 21757c478bd9Sstevel@tonic-gate return (-1); 21767c478bd9Sstevel@tonic-gate } else { 21777c478bd9Sstevel@tonic-gate for (prevp = &children; (chp = *prevp) != NULL; 21787c478bd9Sstevel@tonic-gate prevp = &chp->next) { 21797c478bd9Sstevel@tonic-gate if (chp->pid == pid) { 21807c478bd9Sstevel@tonic-gate --n_children; 21817c478bd9Sstevel@tonic-gate *prevp = chp->next; 21827c478bd9Sstevel@tonic-gate break; 21837c478bd9Sstevel@tonic-gate } 21847c478bd9Sstevel@tonic-gate } 21857c478bd9Sstevel@tonic-gate if (WIFSIGNALED(status) || WIFSTOPPED(status)) { 21867c478bd9Sstevel@tonic-gate i = WIFSIGNALED(status) ? WTERMSIG(status) : WSTOPSIG(status); 21877c478bd9Sstevel@tonic-gate warn("Child process %s (pid %d) %s with signal %d (%s)", 21887c478bd9Sstevel@tonic-gate (chp != NULL ? chp->prog : "??"), pid, 21897c478bd9Sstevel@tonic-gate (WIFSIGNALED(status) ? "terminated" : "stopped"), 21907c478bd9Sstevel@tonic-gate i, signal_name(i)); 21917c478bd9Sstevel@tonic-gate } else if (debug) { 21927c478bd9Sstevel@tonic-gate dbglog("Child process %s finished (pid %d), status = %d", 21937c478bd9Sstevel@tonic-gate (chp != NULL ? chp->prog: "??"), pid, 21947c478bd9Sstevel@tonic-gate WEXITSTATUS(status)); 21957c478bd9Sstevel@tonic-gate } 21967c478bd9Sstevel@tonic-gate if ((chp != NULL) && (chp->done != NULL)) 21977c478bd9Sstevel@tonic-gate (*chp->done)(chp->arg, status); 21987c478bd9Sstevel@tonic-gate if (chp != NULL) 21997c478bd9Sstevel@tonic-gate free(chp); 22007c478bd9Sstevel@tonic-gate } 22017c478bd9Sstevel@tonic-gate } 22027c478bd9Sstevel@tonic-gate return (0); 22037c478bd9Sstevel@tonic-gate } 22047c478bd9Sstevel@tonic-gate 22057c478bd9Sstevel@tonic-gate /* 22067c478bd9Sstevel@tonic-gate * infanticide - timeout while waiting for child process. 22077c478bd9Sstevel@tonic-gate */ 22087c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 22097c478bd9Sstevel@tonic-gate static void 22107c478bd9Sstevel@tonic-gate infanticide(sig) 22117c478bd9Sstevel@tonic-gate int sig; 22127c478bd9Sstevel@tonic-gate { 22137c478bd9Sstevel@tonic-gate struct subprocess *chp; 22147c478bd9Sstevel@tonic-gate static int runcount = 0; 22157c478bd9Sstevel@tonic-gate 22167c478bd9Sstevel@tonic-gate if (runcount < 2) { 22177c478bd9Sstevel@tonic-gate for (chp = children; chp != NULL; chp = chp->next) 22187c478bd9Sstevel@tonic-gate (void) kill(chp->pid, runcount == 0 ? SIGTERM : SIGKILL); 22197c478bd9Sstevel@tonic-gate } else { 22207c478bd9Sstevel@tonic-gate kill_my_pg(SIGTERM); 22217c478bd9Sstevel@tonic-gate /* Quit and hope for the best. */ 22227c478bd9Sstevel@tonic-gate n_children = 0; 22237c478bd9Sstevel@tonic-gate } 22247c478bd9Sstevel@tonic-gate runcount++; 22257c478bd9Sstevel@tonic-gate } 22267c478bd9Sstevel@tonic-gate 22277c478bd9Sstevel@tonic-gate /* 22287c478bd9Sstevel@tonic-gate * Perform final wait before exiting. 22297c478bd9Sstevel@tonic-gate */ 22307c478bd9Sstevel@tonic-gate static void 22317c478bd9Sstevel@tonic-gate final_reap() 22327c478bd9Sstevel@tonic-gate { 22337c478bd9Sstevel@tonic-gate struct sigaction sa; 22347c478bd9Sstevel@tonic-gate struct subprocess *chp; 22357c478bd9Sstevel@tonic-gate 22367c478bd9Sstevel@tonic-gate if (n_children > 0 && debug) { 22377c478bd9Sstevel@tonic-gate dbglog("Waiting for %d child processes...", n_children); 22387c478bd9Sstevel@tonic-gate for (chp = children; chp != NULL; chp = chp->next) 22397c478bd9Sstevel@tonic-gate dbglog(" pid %d: %s", chp->pid, chp->prog); 22407c478bd9Sstevel@tonic-gate } 22417c478bd9Sstevel@tonic-gate BZERO(&sa, sizeof (sa)); 22427c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGALRM, infanticide); 22437c478bd9Sstevel@tonic-gate while (n_children > 0) { 22447c478bd9Sstevel@tonic-gate (void) alarm(7); 22457c478bd9Sstevel@tonic-gate if (reap_kids(1) < 0) 22467c478bd9Sstevel@tonic-gate break; 22477c478bd9Sstevel@tonic-gate } 22487c478bd9Sstevel@tonic-gate (void) alarm(0); 22497c478bd9Sstevel@tonic-gate } 22507c478bd9Sstevel@tonic-gate 22517c478bd9Sstevel@tonic-gate /* 22527c478bd9Sstevel@tonic-gate * novm - log an error message saying we ran out of memory, and die. 22537c478bd9Sstevel@tonic-gate */ 22547c478bd9Sstevel@tonic-gate void 22557c478bd9Sstevel@tonic-gate novm(msg) 22567c478bd9Sstevel@tonic-gate char *msg; 22577c478bd9Sstevel@tonic-gate { 22587c478bd9Sstevel@tonic-gate fatal("Virtual memory exhausted allocating %s\n", msg); 22597c478bd9Sstevel@tonic-gate } 22607c478bd9Sstevel@tonic-gate 22617c478bd9Sstevel@tonic-gate /* 22627c478bd9Sstevel@tonic-gate * script_setenv - set an environment variable value to be used 22637c478bd9Sstevel@tonic-gate * for scripts that we run (e.g. ip-up, auth-up, etc.) 22647c478bd9Sstevel@tonic-gate */ 22657c478bd9Sstevel@tonic-gate void 22667c478bd9Sstevel@tonic-gate script_setenv(var, value, iskey) 22677c478bd9Sstevel@tonic-gate const char *var; 22687c478bd9Sstevel@tonic-gate const char *value; 22697c478bd9Sstevel@tonic-gate int iskey; 22707c478bd9Sstevel@tonic-gate { 22717c478bd9Sstevel@tonic-gate size_t varl = strlen(var); 22727c478bd9Sstevel@tonic-gate size_t vl = varl + strlen(value) + 2; 22737c478bd9Sstevel@tonic-gate int i; 22747c478bd9Sstevel@tonic-gate char *p, *newstring; 22757c478bd9Sstevel@tonic-gate 22767c478bd9Sstevel@tonic-gate /* 22777c478bd9Sstevel@tonic-gate * XXX: Can we assert that a tdb write lock is held here ? It appears that 22787c478bd9Sstevel@tonic-gate * Linux's use of tdb is not safe. 22797c478bd9Sstevel@tonic-gate */ 22807c478bd9Sstevel@tonic-gate newstring = (char *) malloc(vl+1); 22817c478bd9Sstevel@tonic-gate if (newstring == NULL) { 22827c478bd9Sstevel@tonic-gate novm("script environment string"); 22837c478bd9Sstevel@tonic-gate return; 22847c478bd9Sstevel@tonic-gate } 22857c478bd9Sstevel@tonic-gate *newstring++ = iskey; 22867c478bd9Sstevel@tonic-gate (void) slprintf(newstring, vl, "%s=%s", var, value); 22877c478bd9Sstevel@tonic-gate 22887c478bd9Sstevel@tonic-gate /* check if this variable is already set */ 22897c478bd9Sstevel@tonic-gate if (script_env != NULL) { 22907c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) { 22917c478bd9Sstevel@tonic-gate if (strncmp(p, var, varl) == 0 && p[varl] == '=') { 22927c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 22937c478bd9Sstevel@tonic-gate if (p[-1] != '\0' && pppdb != NULL) 22947c478bd9Sstevel@tonic-gate delete_db_key(p); 22957c478bd9Sstevel@tonic-gate #endif 22967c478bd9Sstevel@tonic-gate free(p-1); 22977c478bd9Sstevel@tonic-gate script_env[i] = newstring; 22987c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 22997c478bd9Sstevel@tonic-gate if (iskey && pppdb != NULL) 23007c478bd9Sstevel@tonic-gate add_db_key(newstring); 23017c478bd9Sstevel@tonic-gate update_db_entry(); 23027c478bd9Sstevel@tonic-gate #endif 23037c478bd9Sstevel@tonic-gate return; 23047c478bd9Sstevel@tonic-gate } 23057c478bd9Sstevel@tonic-gate } 23067c478bd9Sstevel@tonic-gate } else { 23077c478bd9Sstevel@tonic-gate /* no space allocated for script env. ptrs. yet */ 23087c478bd9Sstevel@tonic-gate i = 0; 23097c478bd9Sstevel@tonic-gate script_env = (char **) malloc(16 * sizeof(char *)); 23107c478bd9Sstevel@tonic-gate if (script_env == NULL) { 23117c478bd9Sstevel@tonic-gate novm("script environment variable."); 23127c478bd9Sstevel@tonic-gate return; 23137c478bd9Sstevel@tonic-gate } 23147c478bd9Sstevel@tonic-gate s_env_nalloc = 16; 23157c478bd9Sstevel@tonic-gate } 23167c478bd9Sstevel@tonic-gate 23177c478bd9Sstevel@tonic-gate /* reallocate script_env with more space if needed */ 23187c478bd9Sstevel@tonic-gate if (i + 1 >= s_env_nalloc) { 23197c478bd9Sstevel@tonic-gate int new_n = i + 17; 23207c478bd9Sstevel@tonic-gate char **newenv = (char **) realloc((void *)script_env, 23217c478bd9Sstevel@tonic-gate new_n * sizeof(char *)); 23227c478bd9Sstevel@tonic-gate if (newenv == NULL) { 23237c478bd9Sstevel@tonic-gate novm("expanded script environment variable."); 23247c478bd9Sstevel@tonic-gate return; 23257c478bd9Sstevel@tonic-gate } 23267c478bd9Sstevel@tonic-gate script_env = newenv; 23277c478bd9Sstevel@tonic-gate s_env_nalloc = new_n; 23287c478bd9Sstevel@tonic-gate } 23297c478bd9Sstevel@tonic-gate 23307c478bd9Sstevel@tonic-gate script_env[i] = newstring; 23317c478bd9Sstevel@tonic-gate script_env[i+1] = NULL; 23327c478bd9Sstevel@tonic-gate 23337c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 23347c478bd9Sstevel@tonic-gate if (pppdb != NULL) { 23357c478bd9Sstevel@tonic-gate if (iskey) 23367c478bd9Sstevel@tonic-gate add_db_key(newstring); 23377c478bd9Sstevel@tonic-gate update_db_entry(); 23387c478bd9Sstevel@tonic-gate } 23397c478bd9Sstevel@tonic-gate #endif 23407c478bd9Sstevel@tonic-gate } 23417c478bd9Sstevel@tonic-gate 23427c478bd9Sstevel@tonic-gate /* 23437c478bd9Sstevel@tonic-gate * script_unsetenv - remove a variable from the environment 23447c478bd9Sstevel@tonic-gate * for scripts. 23457c478bd9Sstevel@tonic-gate */ 23467c478bd9Sstevel@tonic-gate void 23477c478bd9Sstevel@tonic-gate script_unsetenv(var) 23487c478bd9Sstevel@tonic-gate const char *var; 23497c478bd9Sstevel@tonic-gate { 23507c478bd9Sstevel@tonic-gate int vl = strlen(var); 23517c478bd9Sstevel@tonic-gate int i; 23527c478bd9Sstevel@tonic-gate char *p; 23537c478bd9Sstevel@tonic-gate 23547c478bd9Sstevel@tonic-gate /* 23557c478bd9Sstevel@tonic-gate * XXX: Can we assert that a tdb write lock is held here ? It appears that 23567c478bd9Sstevel@tonic-gate * Linux's use of tdb is not safe. 23577c478bd9Sstevel@tonic-gate */ 23587c478bd9Sstevel@tonic-gate if (script_env == NULL) 23597c478bd9Sstevel@tonic-gate return; 23607c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) { 23617c478bd9Sstevel@tonic-gate if (strncmp(p, var, vl) == 0 && p[vl] == '=') { 23627c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 23637c478bd9Sstevel@tonic-gate if (p[-1] != '\0' && pppdb != NULL) 23647c478bd9Sstevel@tonic-gate delete_db_key(p); 23657c478bd9Sstevel@tonic-gate #endif 23667c478bd9Sstevel@tonic-gate free(p-1); 23677c478bd9Sstevel@tonic-gate while ((script_env[i] = script_env[i+1]) != NULL) 23687c478bd9Sstevel@tonic-gate ++i; 23697c478bd9Sstevel@tonic-gate break; 23707c478bd9Sstevel@tonic-gate } 23717c478bd9Sstevel@tonic-gate } 23727c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 23737c478bd9Sstevel@tonic-gate if ((pppdb != NULL) && (p != NULL)) 23747c478bd9Sstevel@tonic-gate update_db_entry(); 23757c478bd9Sstevel@tonic-gate #endif 23767c478bd9Sstevel@tonic-gate } 23777c478bd9Sstevel@tonic-gate 23787c478bd9Sstevel@tonic-gate /* 23797c478bd9Sstevel@tonic-gate * script_getenv - find a variable in the script environment. 23807c478bd9Sstevel@tonic-gate */ 23817c478bd9Sstevel@tonic-gate const char * 23827c478bd9Sstevel@tonic-gate script_getenv(var) 23837c478bd9Sstevel@tonic-gate const char *var; 23847c478bd9Sstevel@tonic-gate { 23857c478bd9Sstevel@tonic-gate int vl = strlen(var); 23867c478bd9Sstevel@tonic-gate int i; 23877c478bd9Sstevel@tonic-gate char *p; 23887c478bd9Sstevel@tonic-gate 23897c478bd9Sstevel@tonic-gate if (script_env == NULL) 23907c478bd9Sstevel@tonic-gate return (NULL); 23917c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) { 23927c478bd9Sstevel@tonic-gate if (strncmp(p, var, vl) == 0 && p[vl] == '=') 23937c478bd9Sstevel@tonic-gate return ((const char *)p+vl+1); 23947c478bd9Sstevel@tonic-gate } 23957c478bd9Sstevel@tonic-gate return (NULL); 23967c478bd9Sstevel@tonic-gate } 23977c478bd9Sstevel@tonic-gate 23987c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 23997c478bd9Sstevel@tonic-gate /* 24007c478bd9Sstevel@tonic-gate * update_db_entry - update our entry in the database. 24017c478bd9Sstevel@tonic-gate */ 24027c478bd9Sstevel@tonic-gate static void 24037c478bd9Sstevel@tonic-gate update_db_entry() 24047c478bd9Sstevel@tonic-gate { 24057c478bd9Sstevel@tonic-gate TDB_DATA key, dbuf; 24067c478bd9Sstevel@tonic-gate int vlen, i; 24077c478bd9Sstevel@tonic-gate char *p, *q, *vbuf; 24087c478bd9Sstevel@tonic-gate 24097c478bd9Sstevel@tonic-gate if (script_env == NULL) 24107c478bd9Sstevel@tonic-gate return; 24117c478bd9Sstevel@tonic-gate /* 24127c478bd9Sstevel@tonic-gate * vlen needs to be initialized as 1, or otherwise, the last string 24137c478bd9Sstevel@tonic-gate * is truncated by slprintf. 24147c478bd9Sstevel@tonic-gate */ 24157c478bd9Sstevel@tonic-gate vlen = 1; 24167c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) 24177c478bd9Sstevel@tonic-gate vlen += strlen(p) + 1; 24187c478bd9Sstevel@tonic-gate vbuf = malloc(vlen); 24197c478bd9Sstevel@tonic-gate if (vbuf == NULL) 24207c478bd9Sstevel@tonic-gate novm("database entry"); 24217c478bd9Sstevel@tonic-gate q = vbuf; 24227c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) 24237c478bd9Sstevel@tonic-gate q += slprintf(q, vbuf + vlen - q, "%s;", p); 24247c478bd9Sstevel@tonic-gate 24257c478bd9Sstevel@tonic-gate key.dptr = db_key; 24267c478bd9Sstevel@tonic-gate key.dsize = strlen(db_key); 24277c478bd9Sstevel@tonic-gate dbuf.dptr = vbuf; 24287c478bd9Sstevel@tonic-gate dbuf.dsize = vlen; 24297c478bd9Sstevel@tonic-gate if (tdb_store(pppdb, key, dbuf, TDB_REPLACE)) 24307c478bd9Sstevel@tonic-gate error("tdb_store failed: %s", tdb_error(pppdb)); 24317c478bd9Sstevel@tonic-gate } 24327c478bd9Sstevel@tonic-gate 24337c478bd9Sstevel@tonic-gate /* 24347c478bd9Sstevel@tonic-gate * add_db_key - add a key that we can use to look up our database entry. 24357c478bd9Sstevel@tonic-gate */ 24367c478bd9Sstevel@tonic-gate static void 24377c478bd9Sstevel@tonic-gate add_db_key(str) 24387c478bd9Sstevel@tonic-gate const char *str; 24397c478bd9Sstevel@tonic-gate { 24407c478bd9Sstevel@tonic-gate TDB_DATA key, dbuf; 24417c478bd9Sstevel@tonic-gate 24427c478bd9Sstevel@tonic-gate key.dptr = (char *) str; 24437c478bd9Sstevel@tonic-gate key.dsize = strlen(str); 24447c478bd9Sstevel@tonic-gate dbuf.dptr = db_key; 24457c478bd9Sstevel@tonic-gate dbuf.dsize = strlen(db_key); 24467c478bd9Sstevel@tonic-gate if (tdb_store(pppdb, key, dbuf, TDB_REPLACE)) 24477c478bd9Sstevel@tonic-gate error("tdb_store key failed: %s", tdb_error(pppdb)); 24487c478bd9Sstevel@tonic-gate } 24497c478bd9Sstevel@tonic-gate 24507c478bd9Sstevel@tonic-gate /* 24517c478bd9Sstevel@tonic-gate * delete_db_key - delete a key for looking up our database entry. 24527c478bd9Sstevel@tonic-gate */ 24537c478bd9Sstevel@tonic-gate static void 24547c478bd9Sstevel@tonic-gate delete_db_key(str) 24557c478bd9Sstevel@tonic-gate const char *str; 24567c478bd9Sstevel@tonic-gate { 24577c478bd9Sstevel@tonic-gate TDB_DATA key; 24587c478bd9Sstevel@tonic-gate 24597c478bd9Sstevel@tonic-gate key.dptr = (char *) str; 24607c478bd9Sstevel@tonic-gate key.dsize = strlen(str); 24617c478bd9Sstevel@tonic-gate (void) tdb_delete(pppdb, key); 24627c478bd9Sstevel@tonic-gate } 24637c478bd9Sstevel@tonic-gate 24647c478bd9Sstevel@tonic-gate /* 24657c478bd9Sstevel@tonic-gate * cleanup_db - delete all the entries we put in the database. 24667c478bd9Sstevel@tonic-gate */ 24677c478bd9Sstevel@tonic-gate static void 24687c478bd9Sstevel@tonic-gate cleanup_db() 24697c478bd9Sstevel@tonic-gate { 24707c478bd9Sstevel@tonic-gate TDB_DATA key; 24717c478bd9Sstevel@tonic-gate int i; 24727c478bd9Sstevel@tonic-gate char *p; 24737c478bd9Sstevel@tonic-gate 24747c478bd9Sstevel@tonic-gate key.dptr = db_key; 24757c478bd9Sstevel@tonic-gate key.dsize = strlen(db_key); 24767c478bd9Sstevel@tonic-gate (void) tdb_delete(pppdb, key); 24777c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) 24787c478bd9Sstevel@tonic-gate if (p[-1] != '\0') 24797c478bd9Sstevel@tonic-gate delete_db_key(p); 24807c478bd9Sstevel@tonic-gate } 24817c478bd9Sstevel@tonic-gate #endif /* HAVE_MULTILINK */ 24827c478bd9Sstevel@tonic-gate 24837c478bd9Sstevel@tonic-gate /* 24847c478bd9Sstevel@tonic-gate * open_socket - establish a stream socket connection to the nominated 24857c478bd9Sstevel@tonic-gate * host and port. 24867c478bd9Sstevel@tonic-gate * XXX: Need IPv6 support for those systems that support it (use getaddrinfo), 24877c478bd9Sstevel@tonic-gate * but requires portability changes. 24887c478bd9Sstevel@tonic-gate */ 24897c478bd9Sstevel@tonic-gate static int 24907c478bd9Sstevel@tonic-gate open_socket(dest) 24917c478bd9Sstevel@tonic-gate char *dest; 24927c478bd9Sstevel@tonic-gate { 24937c478bd9Sstevel@tonic-gate char *sep, *endp = NULL; 24947c478bd9Sstevel@tonic-gate int sock; 24957c478bd9Sstevel@tonic-gate int port = -1; 24967c478bd9Sstevel@tonic-gate u_int32_t host; 24977c478bd9Sstevel@tonic-gate struct hostent *hent = NULL; 24987c478bd9Sstevel@tonic-gate struct sockaddr_in sad; 24997c478bd9Sstevel@tonic-gate struct servent *se; 25007c478bd9Sstevel@tonic-gate 25017c478bd9Sstevel@tonic-gate /* parse host:port and resolve host to an IP address */ 25027c478bd9Sstevel@tonic-gate sep = strchr(dest, ':'); 25037c478bd9Sstevel@tonic-gate if (sep != NULL) { 25047c478bd9Sstevel@tonic-gate se = getservbyname((const char *)sep+1, "tcp"); 25057c478bd9Sstevel@tonic-gate if (se != NULL) { 25067c478bd9Sstevel@tonic-gate port = ntohs(se->s_port); 25077c478bd9Sstevel@tonic-gate } else { 25087c478bd9Sstevel@tonic-gate port = strtol(sep+1, &endp, 10); 25097c478bd9Sstevel@tonic-gate if (endp == sep+1 || *endp != '\0') { 25107c478bd9Sstevel@tonic-gate error("Can't parse host:port for socket destination"); 25117c478bd9Sstevel@tonic-gate return (-1); 25127c478bd9Sstevel@tonic-gate } 25137c478bd9Sstevel@tonic-gate } 25147c478bd9Sstevel@tonic-gate } 25157c478bd9Sstevel@tonic-gate if (port < 0 || port > 65535 || sep == dest) { 25167c478bd9Sstevel@tonic-gate error("Can't parse host:port for socket destination"); 25177c478bd9Sstevel@tonic-gate return (-1); 25187c478bd9Sstevel@tonic-gate } 25197c478bd9Sstevel@tonic-gate *sep = '\0'; 25207c478bd9Sstevel@tonic-gate host = inet_addr(dest); 25217c478bd9Sstevel@tonic-gate if (host == (u_int32_t) -1) { 25227c478bd9Sstevel@tonic-gate hent = gethostbyname(dest); 25237c478bd9Sstevel@tonic-gate if (hent == NULL) { 25247c478bd9Sstevel@tonic-gate error("%s: unknown host in socket option", dest); 25257c478bd9Sstevel@tonic-gate *sep = ':'; 25267c478bd9Sstevel@tonic-gate return (-1); 25277c478bd9Sstevel@tonic-gate } 25287c478bd9Sstevel@tonic-gate BCOPY(hent->h_addr_list[0], &host, sizeof(host)); 25297c478bd9Sstevel@tonic-gate hent->h_addr_list++; 25307c478bd9Sstevel@tonic-gate } 25317c478bd9Sstevel@tonic-gate *sep = ':'; 25327c478bd9Sstevel@tonic-gate 25337c478bd9Sstevel@tonic-gate for (;;) { 25347c478bd9Sstevel@tonic-gate /* get a socket and connect it to the other end */ 25357c478bd9Sstevel@tonic-gate sock = socket(PF_INET, SOCK_STREAM, 0); 25367c478bd9Sstevel@tonic-gate if (sock < 0) { 25377c478bd9Sstevel@tonic-gate error("Can't create socket: %m"); 25387c478bd9Sstevel@tonic-gate return (-1); 25397c478bd9Sstevel@tonic-gate } 25407c478bd9Sstevel@tonic-gate BZERO(&sad, sizeof(sad)); 25417c478bd9Sstevel@tonic-gate sad.sin_family = AF_INET; 25427c478bd9Sstevel@tonic-gate sad.sin_port = htons(port); 25437c478bd9Sstevel@tonic-gate sad.sin_addr.s_addr = host; 25447c478bd9Sstevel@tonic-gate if (connect(sock, (struct sockaddr *)&sad, sizeof(sad)) >= 0) { 25457c478bd9Sstevel@tonic-gate break; /* return sock file descriptor */ 25467c478bd9Sstevel@tonic-gate } 25477c478bd9Sstevel@tonic-gate if ((hent != NULL) && (hent->h_addr_list != NULL)) { 25487c478bd9Sstevel@tonic-gate BCOPY(hent->h_addr_list[0], &host, sizeof(host)); 25497c478bd9Sstevel@tonic-gate hent->h_addr_list++; 25507c478bd9Sstevel@tonic-gate (void) close(sock); 25517c478bd9Sstevel@tonic-gate continue; 25527c478bd9Sstevel@tonic-gate } 25537c478bd9Sstevel@tonic-gate error("Can't connect to %s: %m", dest); 25547c478bd9Sstevel@tonic-gate (void) close(sock); 25557c478bd9Sstevel@tonic-gate return (-1); 25567c478bd9Sstevel@tonic-gate } 25577c478bd9Sstevel@tonic-gate return (sock); 25587c478bd9Sstevel@tonic-gate } 25597c478bd9Sstevel@tonic-gate 25607c478bd9Sstevel@tonic-gate /* 25617c478bd9Sstevel@tonic-gate * print_ncpstate - prints out current NCP state. 25627c478bd9Sstevel@tonic-gate * 25637c478bd9Sstevel@tonic-gate * We're normally called from SIGUSR1 here, but this is safe because 25647c478bd9Sstevel@tonic-gate * these signals are blocked unless we're idle waiting for events. 25657c478bd9Sstevel@tonic-gate * There's no need to otherwise lock the data structures referenced. 25667c478bd9Sstevel@tonic-gate */ 25677c478bd9Sstevel@tonic-gate void 25687c478bd9Sstevel@tonic-gate print_ncpstate(unit, strptr) 25697c478bd9Sstevel@tonic-gate int unit; 25707c478bd9Sstevel@tonic-gate FILE *strptr; 25717c478bd9Sstevel@tonic-gate { 25727c478bd9Sstevel@tonic-gate struct protent *protp; 25737c478bd9Sstevel@tonic-gate int i; 25747c478bd9Sstevel@tonic-gate 25757c478bd9Sstevel@tonic-gate (void) flprintf(strptr, "In %s phase\n", phase_name(phase)); 25767c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) { 25777c478bd9Sstevel@tonic-gate if (protp->print_stat != NULL) 25787c478bd9Sstevel@tonic-gate (*protp->print_stat)(unit, strptr); 25797c478bd9Sstevel@tonic-gate } 25807c478bd9Sstevel@tonic-gate sys_print_state(strptr); 25817c478bd9Sstevel@tonic-gate } 25827c478bd9Sstevel@tonic-gate 25837c478bd9Sstevel@tonic-gate /* 25847c478bd9Sstevel@tonic-gate * start_charshunt - create a child process to run the character shunt. 25857c478bd9Sstevel@tonic-gate */ 25867c478bd9Sstevel@tonic-gate static int 25877c478bd9Sstevel@tonic-gate start_charshunt(ifd, ofd) 25887c478bd9Sstevel@tonic-gate int ifd, ofd; 25897c478bd9Sstevel@tonic-gate { 25907c478bd9Sstevel@tonic-gate pid_t cpid; 25917c478bd9Sstevel@tonic-gate 25927c478bd9Sstevel@tonic-gate cpid = fork(); 25937c478bd9Sstevel@tonic-gate if (cpid == (pid_t)-1) { 25947c478bd9Sstevel@tonic-gate error("Can't fork process for character shunt: %m"); 25957c478bd9Sstevel@tonic-gate return (0); 25967c478bd9Sstevel@tonic-gate } 25977c478bd9Sstevel@tonic-gate if (cpid == (pid_t)0) { 25987c478bd9Sstevel@tonic-gate /* child */ 25997c478bd9Sstevel@tonic-gate (void) close(pty_slave); 26007c478bd9Sstevel@tonic-gate pty_slave = -1; 26017c478bd9Sstevel@tonic-gate (void) setgid(getgid()); 26027c478bd9Sstevel@tonic-gate (void) setuid(uid); 26037c478bd9Sstevel@tonic-gate if (getuid() != uid) 26047c478bd9Sstevel@tonic-gate fatal("setuid failed"); 26057c478bd9Sstevel@tonic-gate if (!nodetach) 26067c478bd9Sstevel@tonic-gate log_to_fd = -1; 26077c478bd9Sstevel@tonic-gate charshunt(ifd, ofd, record_file); 26087c478bd9Sstevel@tonic-gate exit(0); 26097c478bd9Sstevel@tonic-gate } 26107c478bd9Sstevel@tonic-gate charshunt_pid = cpid; 26117c478bd9Sstevel@tonic-gate (void) close(pty_master); 26127c478bd9Sstevel@tonic-gate pty_master = -1; 26137c478bd9Sstevel@tonic-gate ttyfd = pty_slave; 26147c478bd9Sstevel@tonic-gate record_child(cpid, "pppd (charshunt)", charshunt_done, NULL); 26157c478bd9Sstevel@tonic-gate return (1); 26167c478bd9Sstevel@tonic-gate } 26177c478bd9Sstevel@tonic-gate 26187c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 26197c478bd9Sstevel@tonic-gate static void 26207c478bd9Sstevel@tonic-gate charshunt_done(arg, status) 26217c478bd9Sstevel@tonic-gate void *arg; 26227c478bd9Sstevel@tonic-gate int status; 26237c478bd9Sstevel@tonic-gate { 26247c478bd9Sstevel@tonic-gate charshunt_pid = (pid_t)0; 26257c478bd9Sstevel@tonic-gate } 26267c478bd9Sstevel@tonic-gate 26277c478bd9Sstevel@tonic-gate static void 26287c478bd9Sstevel@tonic-gate reportme(int signo) 26297c478bd9Sstevel@tonic-gate { 26307c478bd9Sstevel@tonic-gate dbglog("charshunt taking signal %d", signo); 26317c478bd9Sstevel@tonic-gate exit(1); 26327c478bd9Sstevel@tonic-gate } 26337c478bd9Sstevel@tonic-gate 26347c478bd9Sstevel@tonic-gate /* 26357c478bd9Sstevel@tonic-gate * charshunt - the character shunt, which passes characters between 26367c478bd9Sstevel@tonic-gate * the pty master side and the serial port (or stdin/stdout). 26377c478bd9Sstevel@tonic-gate * This runs as the user (not as root). 26387c478bd9Sstevel@tonic-gate * (We assume ofd >= ifd which is true the way this gets called. :-). 26397c478bd9Sstevel@tonic-gate */ 26407c478bd9Sstevel@tonic-gate static void 26417c478bd9Sstevel@tonic-gate charshunt(ifd, ofd, record_file) 26427c478bd9Sstevel@tonic-gate int ifd, ofd; 26437c478bd9Sstevel@tonic-gate char *record_file; 26447c478bd9Sstevel@tonic-gate { 26457c478bd9Sstevel@tonic-gate int n, nfds; 26467c478bd9Sstevel@tonic-gate fd_set ready, writey; 26477c478bd9Sstevel@tonic-gate u_char *ibufp, *obufp; 26487c478bd9Sstevel@tonic-gate int nibuf, nobuf; 26497c478bd9Sstevel@tonic-gate int flags; 26507c478bd9Sstevel@tonic-gate struct timeval lasttime; 26517c478bd9Sstevel@tonic-gate FILE *recordf = NULL; 26527c478bd9Sstevel@tonic-gate int ilevel, olevel, max_level; 26537c478bd9Sstevel@tonic-gate struct timeval levelt, tout, *top; 26547c478bd9Sstevel@tonic-gate 26557c478bd9Sstevel@tonic-gate /* 26567c478bd9Sstevel@tonic-gate * Reset signal handlers. 26577c478bd9Sstevel@tonic-gate */ 26587c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); /* Hangup */ 26597c478bd9Sstevel@tonic-gate (void) signal(SIGINT, reportme); /* Interrupt */ 26607c478bd9Sstevel@tonic-gate (void) signal(SIGTERM, reportme); /* Terminate */ 26617c478bd9Sstevel@tonic-gate (void) signal(SIGCHLD, reportme); 26627c478bd9Sstevel@tonic-gate (void) signal(SIGUSR1, reportme); 26637c478bd9Sstevel@tonic-gate (void) signal(SIGUSR2, reportme); 26647c478bd9Sstevel@tonic-gate (void) signal(SIGABRT, reportme); 26657c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, reportme); 26667c478bd9Sstevel@tonic-gate (void) signal(SIGFPE, reportme); 26677c478bd9Sstevel@tonic-gate (void) signal(SIGILL, reportme); 26687c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, reportme); 26697c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, reportme); 26707c478bd9Sstevel@tonic-gate #ifndef DEBUG 26717c478bd9Sstevel@tonic-gate (void) signal(SIGSEGV, reportme); 26727c478bd9Sstevel@tonic-gate #endif 26737c478bd9Sstevel@tonic-gate #ifdef SIGBUS 26747c478bd9Sstevel@tonic-gate (void) signal(SIGBUS, reportme); 26757c478bd9Sstevel@tonic-gate #endif 26767c478bd9Sstevel@tonic-gate #ifdef SIGEMT 26777c478bd9Sstevel@tonic-gate (void) signal(SIGEMT, reportme); 26787c478bd9Sstevel@tonic-gate #endif 26797c478bd9Sstevel@tonic-gate #ifdef SIGPOLL 26807c478bd9Sstevel@tonic-gate (void) signal(SIGPOLL, reportme); 26817c478bd9Sstevel@tonic-gate #endif 26827c478bd9Sstevel@tonic-gate #ifdef SIGPROF 26837c478bd9Sstevel@tonic-gate (void) signal(SIGPROF, reportme); 26847c478bd9Sstevel@tonic-gate #endif 26857c478bd9Sstevel@tonic-gate #ifdef SIGSYS 26867c478bd9Sstevel@tonic-gate (void) signal(SIGSYS, reportme); 26877c478bd9Sstevel@tonic-gate #endif 26887c478bd9Sstevel@tonic-gate #ifdef SIGTRAP 26897c478bd9Sstevel@tonic-gate (void) signal(SIGTRAP, reportme); 26907c478bd9Sstevel@tonic-gate #endif 26917c478bd9Sstevel@tonic-gate #ifdef SIGVTALRM 26927c478bd9Sstevel@tonic-gate (void) signal(SIGVTALRM, reportme); 26937c478bd9Sstevel@tonic-gate #endif 26947c478bd9Sstevel@tonic-gate #ifdef SIGXCPU 26957c478bd9Sstevel@tonic-gate (void) signal(SIGXCPU, reportme); 26967c478bd9Sstevel@tonic-gate #endif 26977c478bd9Sstevel@tonic-gate #ifdef SIGXFSZ 26987c478bd9Sstevel@tonic-gate (void) signal(SIGXFSZ, reportme); 26997c478bd9Sstevel@tonic-gate #endif 27007c478bd9Sstevel@tonic-gate 27017c478bd9Sstevel@tonic-gate /* 27027c478bd9Sstevel@tonic-gate * Open the record file if required. 27037c478bd9Sstevel@tonic-gate */ 27047c478bd9Sstevel@tonic-gate if (record_file != NULL) { 27057c478bd9Sstevel@tonic-gate recordf = fopen(record_file, "a"); 27067c478bd9Sstevel@tonic-gate if (recordf == NULL) 27077c478bd9Sstevel@tonic-gate error("Couldn't create record file %s: %m", record_file); 27087c478bd9Sstevel@tonic-gate } 27097c478bd9Sstevel@tonic-gate 27107c478bd9Sstevel@tonic-gate /* set all the fds to non-blocking mode */ 27117c478bd9Sstevel@tonic-gate flags = fcntl(pty_master, F_GETFL); 27127c478bd9Sstevel@tonic-gate if (flags == -1 27137c478bd9Sstevel@tonic-gate || fcntl(pty_master, F_SETFL, flags | O_NONBLOCK) == -1) 27147c478bd9Sstevel@tonic-gate warn("couldn't set pty master to nonblock: %m"); 27157c478bd9Sstevel@tonic-gate flags = fcntl(ifd, F_GETFL); 27167c478bd9Sstevel@tonic-gate if (flags == -1 27177c478bd9Sstevel@tonic-gate || fcntl(ifd, F_SETFL, flags | O_NONBLOCK) == -1) 27187c478bd9Sstevel@tonic-gate warn("couldn't set %s to nonblock: %m", (ifd==0? "stdin": "tty")); 27197c478bd9Sstevel@tonic-gate if (ofd != ifd) { 27207c478bd9Sstevel@tonic-gate flags = fcntl(ofd, F_GETFL); 27217c478bd9Sstevel@tonic-gate if (flags == -1 27227c478bd9Sstevel@tonic-gate || fcntl(ofd, F_SETFL, flags | O_NONBLOCK) == -1) 27237c478bd9Sstevel@tonic-gate warn("couldn't set stdout to nonblock: %m"); 27247c478bd9Sstevel@tonic-gate } 27257c478bd9Sstevel@tonic-gate 27267c478bd9Sstevel@tonic-gate nibuf = nobuf = 0; 27277c478bd9Sstevel@tonic-gate ibufp = obufp = NULL; 27287c478bd9Sstevel@tonic-gate 27297c478bd9Sstevel@tonic-gate ilevel = olevel = 0; 27307c478bd9Sstevel@tonic-gate (void) gettimeofday(&levelt, NULL); 27317c478bd9Sstevel@tonic-gate if (max_data_rate) { 27327c478bd9Sstevel@tonic-gate max_level = max_data_rate / 10; 27337c478bd9Sstevel@tonic-gate if (max_level < MAXLEVELMINSIZE) 27347c478bd9Sstevel@tonic-gate max_level = MAXLEVELMINSIZE; 27357c478bd9Sstevel@tonic-gate } else 27367c478bd9Sstevel@tonic-gate max_level = sizeof(inpacket_buf) + 1; 27377c478bd9Sstevel@tonic-gate 27387c478bd9Sstevel@tonic-gate nfds = (ofd > pty_master? ofd: pty_master) + 1; 27397c478bd9Sstevel@tonic-gate if (recordf != NULL) { 27407c478bd9Sstevel@tonic-gate (void) gettimeofday(&lasttime, NULL); 27417c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMESTART, recordf); /* put start marker */ 27427c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 24, recordf); 27437c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 16, recordf); 27447c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 8, recordf); 27457c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec, recordf); 27467c478bd9Sstevel@tonic-gate lasttime.tv_usec = 0; 27477c478bd9Sstevel@tonic-gate } 27487c478bd9Sstevel@tonic-gate 27497c478bd9Sstevel@tonic-gate while (nibuf != 0 || nobuf != 0 || ofd >= 0 || pty_master >= 0) { 27507c478bd9Sstevel@tonic-gate top = 0; 27517c478bd9Sstevel@tonic-gate tout.tv_sec = 0; 27527c478bd9Sstevel@tonic-gate tout.tv_usec = 10000; 27537c478bd9Sstevel@tonic-gate FD_ZERO(&ready); 27547c478bd9Sstevel@tonic-gate FD_ZERO(&writey); 27557c478bd9Sstevel@tonic-gate if (nibuf != 0) { 27567c478bd9Sstevel@tonic-gate if (ilevel >= max_level) 27577c478bd9Sstevel@tonic-gate top = &tout; 27587c478bd9Sstevel@tonic-gate else if (pty_master >= 0) 27597c478bd9Sstevel@tonic-gate FD_SET(pty_master, &writey); 27607c478bd9Sstevel@tonic-gate } else if (ifd >= 0) 27617c478bd9Sstevel@tonic-gate FD_SET(ifd, &ready); 27627c478bd9Sstevel@tonic-gate if (nobuf != 0) { 27637c478bd9Sstevel@tonic-gate if (olevel >= max_level) 27647c478bd9Sstevel@tonic-gate top = &tout; 27657c478bd9Sstevel@tonic-gate else if (ofd >= 0) 27667c478bd9Sstevel@tonic-gate FD_SET(ofd, &writey); 27677c478bd9Sstevel@tonic-gate } else { 27687c478bd9Sstevel@tonic-gate /* Don't read from pty if it's gone or it has closed. */ 27697c478bd9Sstevel@tonic-gate if (pty_master >= 0 && ofd >= 0) 27707c478bd9Sstevel@tonic-gate FD_SET(pty_master, &ready); 27717c478bd9Sstevel@tonic-gate } 27727c478bd9Sstevel@tonic-gate if (select(nfds, &ready, &writey, NULL, top) < 0) { 27737c478bd9Sstevel@tonic-gate if (errno != EINTR) 27747c478bd9Sstevel@tonic-gate fatal("select"); 27757c478bd9Sstevel@tonic-gate continue; 27767c478bd9Sstevel@tonic-gate } 27777c478bd9Sstevel@tonic-gate if (max_data_rate) { 27787c478bd9Sstevel@tonic-gate double dt; 27797c478bd9Sstevel@tonic-gate int nbt; 27807c478bd9Sstevel@tonic-gate struct timeval now; 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL); 27837c478bd9Sstevel@tonic-gate dt = (now.tv_sec - levelt.tv_sec 27847c478bd9Sstevel@tonic-gate + (now.tv_usec - levelt.tv_usec) / 1e6); 27857c478bd9Sstevel@tonic-gate nbt = (int)(dt * max_data_rate); 27867c478bd9Sstevel@tonic-gate ilevel = (nbt < 0 || nbt > ilevel)? 0: ilevel - nbt; 27877c478bd9Sstevel@tonic-gate olevel = (nbt < 0 || nbt > olevel)? 0: olevel - nbt; 27887c478bd9Sstevel@tonic-gate levelt = now; 27897c478bd9Sstevel@tonic-gate } else 27907c478bd9Sstevel@tonic-gate ilevel = olevel = 0; 27917c478bd9Sstevel@tonic-gate if (FD_ISSET(ifd, &ready)) { 27927c478bd9Sstevel@tonic-gate ibufp = inpacket_buf; 27937c478bd9Sstevel@tonic-gate nibuf = read(ifd, ibufp, sizeof(inpacket_buf)); 27947c478bd9Sstevel@tonic-gate if (nibuf < 0 && errno == EIO) 27957c478bd9Sstevel@tonic-gate nibuf = 0; 27967c478bd9Sstevel@tonic-gate if (nibuf < 0 || pty_master == -1) { 27977c478bd9Sstevel@tonic-gate if (errno != EINTR && errno != EAGAIN) { 27987c478bd9Sstevel@tonic-gate error("Error reading standard input: %m"); 27997c478bd9Sstevel@tonic-gate break; 28007c478bd9Sstevel@tonic-gate } 28017c478bd9Sstevel@tonic-gate nibuf = 0; 28027c478bd9Sstevel@tonic-gate } else if (nibuf == 0) { 28037c478bd9Sstevel@tonic-gate /* end of file from stdin */ 28047c478bd9Sstevel@tonic-gate (void) close(pty_master); 28057c478bd9Sstevel@tonic-gate pty_master = -1; 28067c478bd9Sstevel@tonic-gate (void) close(ifd); 28077c478bd9Sstevel@tonic-gate ifd = -1; 28087c478bd9Sstevel@tonic-gate if (recordf) 28097c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_ENDRECV, NULL, 0, 28107c478bd9Sstevel@tonic-gate &lasttime)) 28117c478bd9Sstevel@tonic-gate recordf = NULL; 28127c478bd9Sstevel@tonic-gate } else { 28137c478bd9Sstevel@tonic-gate FD_SET(pty_master, &writey); 28147c478bd9Sstevel@tonic-gate if (recordf) 28157c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_STARTRECV, ibufp, nibuf, 28167c478bd9Sstevel@tonic-gate &lasttime)) 28177c478bd9Sstevel@tonic-gate recordf = NULL; 28187c478bd9Sstevel@tonic-gate } 28197c478bd9Sstevel@tonic-gate } 28207c478bd9Sstevel@tonic-gate if (ofd >= 0 && pty_master >= 0 && FD_ISSET(pty_master, &ready)) { 28217c478bd9Sstevel@tonic-gate obufp = outpacket_buf; 28227c478bd9Sstevel@tonic-gate nobuf = read(pty_master, obufp, sizeof(outpacket_buf)); 28237c478bd9Sstevel@tonic-gate if (nobuf < 0 && errno == EIO) 28247c478bd9Sstevel@tonic-gate nobuf = 0; 28257c478bd9Sstevel@tonic-gate if (nobuf < 0 || ofd == -1) { 28267c478bd9Sstevel@tonic-gate if (!(errno == EINTR || errno == EAGAIN)) { 28277c478bd9Sstevel@tonic-gate error("Error reading pseudo-tty master: %m"); 28287c478bd9Sstevel@tonic-gate break; 28297c478bd9Sstevel@tonic-gate } 28307c478bd9Sstevel@tonic-gate nobuf = 0; 28317c478bd9Sstevel@tonic-gate } else if (nobuf == 0) { 28327c478bd9Sstevel@tonic-gate /* end of file from the pty - slave side has closed */ 28337c478bd9Sstevel@tonic-gate nibuf = 0; 28347c478bd9Sstevel@tonic-gate (void) close(ofd); 28357c478bd9Sstevel@tonic-gate ofd = -1; 28367c478bd9Sstevel@tonic-gate if (recordf) 28377c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_ENDSEND, NULL, 0, 28387c478bd9Sstevel@tonic-gate &lasttime)) 28397c478bd9Sstevel@tonic-gate recordf = NULL; 28407c478bd9Sstevel@tonic-gate } else { 28417c478bd9Sstevel@tonic-gate FD_SET(ofd, &writey); 28427c478bd9Sstevel@tonic-gate if (recordf) 28437c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_STARTSEND, obufp, nobuf, 28447c478bd9Sstevel@tonic-gate &lasttime)) 28457c478bd9Sstevel@tonic-gate recordf = NULL; 28467c478bd9Sstevel@tonic-gate } 28477c478bd9Sstevel@tonic-gate } 28487c478bd9Sstevel@tonic-gate if (ofd == -1) 28497c478bd9Sstevel@tonic-gate nobuf = 0; 28507c478bd9Sstevel@tonic-gate else if (FD_ISSET(ofd, &writey)) { 28517c478bd9Sstevel@tonic-gate n = nobuf; 28527c478bd9Sstevel@tonic-gate if (olevel + n > max_level) 28537c478bd9Sstevel@tonic-gate n = max_level - olevel; 28547c478bd9Sstevel@tonic-gate n = write(ofd, obufp, n); 28557c478bd9Sstevel@tonic-gate if (n < 0) { 28567c478bd9Sstevel@tonic-gate if (errno == EIO) { 28577c478bd9Sstevel@tonic-gate (void) close(ofd); 28587c478bd9Sstevel@tonic-gate ofd = -1; 28597c478bd9Sstevel@tonic-gate nobuf = 0; 28607c478bd9Sstevel@tonic-gate } else if (errno != EAGAIN && errno != EINTR) { 28617c478bd9Sstevel@tonic-gate error("Error writing standard output: %m"); 28627c478bd9Sstevel@tonic-gate break; 28637c478bd9Sstevel@tonic-gate } 28647c478bd9Sstevel@tonic-gate } else { 28657c478bd9Sstevel@tonic-gate obufp += n; 28667c478bd9Sstevel@tonic-gate nobuf -= n; 28677c478bd9Sstevel@tonic-gate olevel += n; 28687c478bd9Sstevel@tonic-gate } 28697c478bd9Sstevel@tonic-gate } 28707c478bd9Sstevel@tonic-gate if (pty_master == -1) 28717c478bd9Sstevel@tonic-gate nibuf = 0; 28727c478bd9Sstevel@tonic-gate else if (FD_ISSET(pty_master, &writey)) { 28737c478bd9Sstevel@tonic-gate n = nibuf; 28747c478bd9Sstevel@tonic-gate if (ilevel + n > max_level) 28757c478bd9Sstevel@tonic-gate n = max_level - ilevel; 28767c478bd9Sstevel@tonic-gate n = write(pty_master, ibufp, n); 28777c478bd9Sstevel@tonic-gate if (n < 0) { 28787c478bd9Sstevel@tonic-gate if (errno == EAGAIN || errno == EINTR) 28797c478bd9Sstevel@tonic-gate continue; 28807c478bd9Sstevel@tonic-gate if (errno != EIO) { 28817c478bd9Sstevel@tonic-gate error("Error writing pseudo-tty master: %m"); 28827c478bd9Sstevel@tonic-gate break; 28837c478bd9Sstevel@tonic-gate } 28847c478bd9Sstevel@tonic-gate (void) close(pty_master); 28857c478bd9Sstevel@tonic-gate pty_master = -1; 28867c478bd9Sstevel@tonic-gate nibuf = 0; 28877c478bd9Sstevel@tonic-gate } else { 28887c478bd9Sstevel@tonic-gate ibufp += n; 28897c478bd9Sstevel@tonic-gate nibuf -= n; 28907c478bd9Sstevel@tonic-gate ilevel += n; 28917c478bd9Sstevel@tonic-gate } 28927c478bd9Sstevel@tonic-gate } 28937c478bd9Sstevel@tonic-gate } 28947c478bd9Sstevel@tonic-gate exit(0); 28957c478bd9Sstevel@tonic-gate } 28967c478bd9Sstevel@tonic-gate 28977c478bd9Sstevel@tonic-gate static int 28987c478bd9Sstevel@tonic-gate record_write(f, code, buf, nb, tp) 28997c478bd9Sstevel@tonic-gate FILE *f; 29007c478bd9Sstevel@tonic-gate int code; 29017c478bd9Sstevel@tonic-gate u_char *buf; 29027c478bd9Sstevel@tonic-gate int nb; 29037c478bd9Sstevel@tonic-gate struct timeval *tp; 29047c478bd9Sstevel@tonic-gate { 29057c478bd9Sstevel@tonic-gate struct timeval now; 29067c478bd9Sstevel@tonic-gate int diff; 29077c478bd9Sstevel@tonic-gate 29087c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL); 29097c478bd9Sstevel@tonic-gate now.tv_usec /= 100000; /* actually 1/10 s, not usec now */ 29107c478bd9Sstevel@tonic-gate diff = (now.tv_sec - tp->tv_sec) * 10 + (now.tv_usec - tp->tv_usec); 29117c478bd9Sstevel@tonic-gate if (diff > 0) { 29127c478bd9Sstevel@tonic-gate if (diff > 255) { 29137c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMEDELTA32, f); 29147c478bd9Sstevel@tonic-gate (void) putc(diff >> 24, f); 29157c478bd9Sstevel@tonic-gate (void) putc(diff >> 16, f); 29167c478bd9Sstevel@tonic-gate (void) putc(diff >> 8, f); 29177c478bd9Sstevel@tonic-gate (void) putc(diff, f); 29187c478bd9Sstevel@tonic-gate } else { 29197c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMEDELTA8, f); 29207c478bd9Sstevel@tonic-gate (void) putc(diff, f); 29217c478bd9Sstevel@tonic-gate } 29227c478bd9Sstevel@tonic-gate *tp = now; 29237c478bd9Sstevel@tonic-gate } 29247c478bd9Sstevel@tonic-gate (void) putc(code, f); 29257c478bd9Sstevel@tonic-gate if (buf != NULL) { 29267c478bd9Sstevel@tonic-gate (void) putc(nb >> 8, f); 29277c478bd9Sstevel@tonic-gate (void) putc(nb, f); 29287c478bd9Sstevel@tonic-gate (void) fwrite(buf, nb, 1, f); 29297c478bd9Sstevel@tonic-gate } 29307c478bd9Sstevel@tonic-gate (void) fflush(f); 29317c478bd9Sstevel@tonic-gate if (ferror(f)) { 29327c478bd9Sstevel@tonic-gate error("Error writing record file: %m"); 29337c478bd9Sstevel@tonic-gate return (0); 29347c478bd9Sstevel@tonic-gate } 29357c478bd9Sstevel@tonic-gate return (1); 29367c478bd9Sstevel@tonic-gate } 2937