2 * main.c - Point-to-Point Protocol main module
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 static char rcsid[] = "$Id: main.c,v 1.12 1994/05/26 06:36:22 paulus Exp $";
38 * If REQ_SYSOPTIONS is defined to 1, pppd will not run unless
39 * /etc/ppp/options exists.
41 #ifndef REQ_SYSOPTIONS
42 #define REQ_SYSOPTIONS 1
49 #include <sys/ioctl.h>
54 #include <sys/param.h>
55 #include <sys/types.h>
57 #include <sys/socket.h>
71 #include "pathnames.h"
72 #include "patchlevel.h"
84 static char *pidpath = PIDPATH; /* filename in which pid will be stored */
86 static char *pidpath = _PATH_PIDFILE;
90 char ifname[IFNAMSIZ]; /* Interface name */
91 int ifunit; /* Interface unit number */
93 char *progname; /* Name of this program */
94 char hostname[MAXNAMELEN]; /* Our hostname */
95 char our_name[MAXNAMELEN];
96 char remote_name[MAXNAMELEN];
97 static char pidfilename[MAXPATHLEN];
99 static pid_t pid; /* Our pid */
100 static pid_t pgrpid; /* Process Group ID */
101 uid_t uid; /* Our real user-id */
103 char devname[MAXPATHLEN] = "/dev/tty"; /* Device name */
104 int default_device = TRUE; /* use default device (stdin/out) */
106 int fd = -1; /* Device file descriptor */
107 int s; /* Socket file descriptor */
109 int phase; /* where the link is at */
112 static struct sgttyb initsgttyb; /* Initial TTY sgttyb */
114 static struct termios inittermios; /* Initial TTY termios */
117 static int initfdflags = -1; /* Initial file descriptor flags */
119 static int restore_term; /* 1 => we've munged the terminal */
121 u_char outpacket_buf[MTU+DLLHEADERLEN]; /* buffer for outgoing packet */
122 static u_char inpacket_buf[MTU+DLLHEADERLEN]; /* buffer for incoming packet */
124 int hungup; /* terminal has been hung up */
125 static int n_children; /* # child processes still running */
127 /* configured variables */
129 int debug = 0; /* Debug flag */
130 int kdebugflag = 0; /* Kernel debugging flag */
131 char user[MAXNAMELEN]; /* username for PAP */
132 char passwd[MAXSECRETLEN]; /* password for PAP */
133 char *connector = NULL; /* "connect" command */
134 char *disconnector = NULL; /* "disconnect" command */
135 int inspeed = 0; /* Input/Output speed requested */
136 int baud_rate; /* bits/sec currently used */
137 u_long netmask = 0; /* netmask to use on ppp interface */
138 int crtscts = 0; /* use h/w flow control */
139 int nodetach = 0; /* don't fork */
140 int modem = 0; /* use modem control lines */
141 int auth_required = 0; /* require peer to authenticate */
142 int defaultroute = 0; /* assign default route through interface */
143 int proxyarp = 0; /* set entry in arp table */
144 int persist = 0; /* re-initiate on termination */
145 int answer = 0; /* wait for incoming call */
146 int uselogin = 0; /* check PAP info against /etc/passwd */
147 int lockflag = 0; /* lock the serial device */
151 static void hup __ARGS((int));
152 static void intr __ARGS((int));
153 static void term __ARGS((int));
154 static void alrm __ARGS((int));
155 static void io __ARGS((int));
156 static void chld __ARGS((int));
157 static void incdebug __ARGS((int));
158 static void nodebug __ARGS((int));
159 void establish_ppp __ARGS((void));
161 void reap_kids __ARGS((void));
162 void cleanup __ARGS((int, caddr_t));
163 void die __ARGS((int));
164 void novm __ARGS((char *));
166 void log_packet __ARGS((u_char *, int, char *));
167 void format_packet __ARGS((u_char *, int,
168 void (*) (void *, char *, ...), void *));
169 void pr_log __ARGS((void *, char *, ...));
171 extern char *ttyname __ARGS((int));
172 extern char *getlogin __ARGS((void));
175 * PPP Data Link Layer "protocol" table.
176 * One entry per supported protocol.
178 static struct protent {
186 { LCP, lcp_init, lcp_input, lcp_protrej, lcp_printpkt, "LCP" },
187 { IPCP, ipcp_init, ipcp_input, ipcp_protrej, ipcp_printpkt, "IPCP" },
188 { UPAP, upap_init, upap_input, upap_protrej, upap_printpkt, "PAP" },
189 { CHAP, ChapInit, ChapInput, ChapProtocolReject, ChapPrintPkt, "CHAP" },
192 #define N_PROTO (sizeof(prottbl) / sizeof(prottbl[0]))
209 if (gethostname(hostname, MAXNAMELEN) < 0 ) {
210 perror("couldn't get hostname");
213 hostname[MAXNAMELEN-1] = 0;
218 if (!ppp_available()) {
219 fprintf(stderr, "Sorry - PPP is not available on this system\n");
224 * Initialize to the standard option set, then parse, in order,
225 * the system options file, the user's options file, and the command
228 for (i = 0; i < N_PROTO; i++)
229 (*prottbl[i].init)(0);
233 if (!options_from_file(_PATH_SYSOPTIONS, REQ_SYSOPTIONS, 0) ||
234 !options_from_user() ||
235 !parse_args(argc-1, argv+1) ||
238 check_auth_options();
242 * Initialize syslog system and magic number package.
245 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
246 setlogmask(LOG_UPTO(LOG_INFO));
248 openlog("pppd", LOG_PID);
249 #define LOG_UPTO(x) (x)
250 #define setlogmask(x) (x)
253 setlogmask(LOG_UPTO(LOG_DEBUG));
260 if (pw != NULL && pw->pw_name != NULL)
265 syslog(LOG_NOTICE, "pppd %s.%d started by %s, uid %d",
266 VERSION, PATCHLEVEL, p, uid);
270 * Make sure we can set the serial device to be our controlling terminal.
272 if (default_device) {
274 * No device name was specified:
275 * we are in the device's session already.
277 if ((pgrpid = getpgrp(0)) < 0) {
278 syslog(LOG_ERR, "getpgrp(0): %m");
284 * Not default device: make sure we're not a process group leader,
285 * then become session leader of a new session (so we can make
286 * our device its controlling terminal and thus get SIGHUPs).
289 /* fork so we're not a process group leader */
291 exit(0); /* parent is finished */
294 syslog(LOG_ERR, "fork: %m");
297 pid = getpid(); /* otherwise pid is 0 in child */
300 * try to put ourself into our parent's process group,
301 * so we're not a process group leader
303 if (setpgrp(pid, getppid()) < 0)
304 syslog(LOG_WARNING, "setpgrp: %m");
307 /* create new session */
308 if ((pgrpid = setsid()) < 0) {
309 syslog(LOG_ERR, "setsid(): %m");
315 if (lockflag && !default_device)
316 if (lock(devname) < 0)
319 /* Get an internet socket for doing socket ioctl's on. */
320 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
321 syslog(LOG_ERR, "socket : %m");
326 * Compute mask of all interesting signals and install signal handlers
327 * for each. Only one signal handler may be active at a time. Therefore,
328 * all other signals should be masked when any handler is executing.
331 sigaddset(&mask, SIGHUP);
332 sigaddset(&mask, SIGINT);
333 sigaddset(&mask, SIGALRM);
334 sigaddset(&mask, SIGIO);
335 sigaddset(&mask, SIGCHLD);
337 sigaddset(&mask, SIGPOLL);
340 #define SIGNAL(s, handler) { \
341 sa.sa_handler = handler; \
342 if (sigaction(s, &sa, NULL) < 0) { \
343 syslog(LOG_ERR, "sigaction(%d): %m", s); \
350 SIGNAL(SIGHUP, hup); /* Hangup */
351 SIGNAL(SIGINT, intr); /* Interrupt */
352 SIGNAL(SIGTERM, term); /* Terminate */
353 SIGNAL(SIGALRM, alrm); /* Timeout */
354 SIGNAL(SIGIO, io); /* Input available */
355 SIGNAL(SIGCHLD, chld); /* Death of child process */
357 SIGNAL(SIGPOLL, io); /* Input available */
360 signal(SIGUSR1, incdebug); /* Increment debug flag */
361 signal(SIGUSR2, nodebug); /* Reset debug flag */
364 * Block SIGIOs and SIGPOLLs for now
367 sigaddset(&mask, SIGIO);
369 sigaddset(&mask, SIGPOLL);
371 sigprocmask(SIG_BLOCK, &mask, NULL);
374 * Open the serial device and set it up to be the ppp interface.
376 if ((fd = open(devname, O_RDWR /*| O_NDELAY*/)) < 0) {
377 syslog(LOG_ERR, "open(%s): %m", devname);
383 /* set device to be controlling tty */
384 if (!default_device && ioctl(fd, TIOCSCTTY) < 0) {
385 syslog(LOG_ERR, "ioctl(TIOCSCTTY): %m");
388 #endif /* TIOCSCTTY */
390 /* run connection script */
392 MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector));
394 /* set line speed, flow control, etc.; set CLOCAL for now */
397 /* drop dtr to hang up in case modem is off hook */
398 if (!default_device && modem) {
404 if (device_script(connector, fd, fd) < 0) {
405 syslog(LOG_ERR, "could not set up connection");
410 syslog(LOG_INFO, "Connected...");
411 sleep(1); /* give it time to set up its terminal */
414 /* set line speed, flow control, etc.; clear CLOCAL if modem option */
417 /* set up the serial device as a ppp interface */
420 syslog(LOG_INFO, "Using interface ppp%d", ifunit);
421 (void) sprintf(ifname, "ppp%d", ifunit);
423 /* write pid to file */
424 (void) sprintf(pidfilename, "%s/%s.pid", pidpath, ifname);
425 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
426 fprintf(pidfile, "%d\n", pid);
427 (void) fclose(pidfile);
429 syslog(LOG_ERR, "unable to create pid file: %m");
434 * Set process group of device to our process group so we can get
435 * SIGIOs and SIGHUPs.
438 if (default_device) {
439 int id = tcgetpgrp(fd);
441 syslog(LOG_WARNING, "warning: not in tty's process group");
444 if (tcsetpgrp(fd, pgrpid) < 0) {
445 syslog(LOG_ERR, "tcsetpgrp(): %m");
450 /* set process group on tty so we get SIGIO's */
451 if (ioctl(fd, TIOCSPGRP, &pgrpid) < 0) {
452 syslog(LOG_ERR, "ioctl(TIOCSPGRP): %m");
458 * Record initial device flags, then set device to cause SIGIO
459 * signals to be generated.
461 if ((initfdflags = fcntl(fd, F_GETFL)) == -1) {
462 syslog(LOG_ERR, "fcntl(F_GETFL): %m");
466 #ifdef _linux_ /* This is a kludge for Linux. FIXME !!! -- later. */
471 if (ioctl(fd, PPPIOCSINPSIG, &sig) == -1) {
472 syslog(LOG_ERR, "ioctl(PPPIOCSINPSIG): %m");
478 if (fcntl(fd, F_SETFL, FNDELAY | FASYNC) == -1) {
479 syslog(LOG_ERR, "fcntl(F_SETFL, FNDELAY | FASYNC): %m");
484 * Block all signals, start opening the connection, and wait for
485 * incoming signals (reply, timeout, etc.).
487 syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devname);
488 sigprocmask(SIG_BLOCK, &mask, NULL); /* Block signals now */
489 lcp_lowerup(0); /* XXX Well, sort of... */
490 lcp_open(0); /* Start protocol */
491 for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; )
492 sigpause(0); /* Wait for next signal */
495 * Run disconnector script, if requested
498 if (device_script(disconnector, fd, fd) < 0) {
499 syslog(LOG_WARNING, "disconnect script failed");
503 syslog(LOG_INFO, "Disconnected...");
511 * XXX assume speed_t values numerically equal bits per second
512 * (so we can ask for any speed).
514 #define translate_speed(bps) (bps)
515 #define baud_rate_of(speed) (speed)
519 * List of valid speeds.
522 int speed_int, speed_val;
594 * Translate from bits/second to a speed_t.
600 struct speed *speedp;
604 for (speedp = speeds; speedp->speed_int; speedp++)
605 if (bps == speedp->speed_int)
606 return speedp->speed_val;
607 syslog(LOG_WARNING, "speed %d not supported", bps);
612 * Translate from a speed_t to bits/second.
618 struct speed *speedp;
622 for (speedp = speeds; speedp->speed_int; speedp++)
623 if (speed == speedp->speed_val)
624 return speedp->speed_int;
630 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
631 * at the requested speed, etc. If `local' is true, set CLOCAL
632 * regardless of whether the modem option was specified.
634 set_up_tty(fd, local)
641 if (tcgetattr(fd, &tios) < 0) {
642 syslog(LOG_ERR, "tcgetattr: %m");
650 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
652 tios.c_cflag |= CRTSCTS;
654 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
657 tios.c_cflag |= CS8 | CREAD | HUPCL;
659 tios.c_cflag |= CLOCAL;
660 tios.c_iflag = IGNBRK | IGNPAR;
664 tios.c_cc[VTIME] = 0;
667 tios.c_iflag |= IXOFF;
668 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
669 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
672 speed = translate_speed(inspeed);
674 cfsetospeed(&tios, speed);
675 cfsetispeed(&tios, speed);
677 speed = cfgetospeed(&tios);
680 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
681 syslog(LOG_ERR, "tcsetattr: %m");
687 if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
688 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
689 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
690 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
695 struct sgttyb sgttyb;
698 * Put the tty in raw mode.
700 if (ioctl(fd, TIOCGETP, &sgttyb) < 0) {
701 syslog(LOG_ERR, "ioctl(TIOCGETP): %m");
708 sgttyb.sg_flags = RAW | ANYP;
709 speed = translate_speed(inspeed);
711 sgttyb.sg_ispeed = speed;
713 speed = sgttyb.sg_ispeed;
715 if (ioctl(fd, TIOCSETP, &sgttyb) < 0) {
716 syslog(LOG_ERR, "ioctl(TIOCSETP): %m");
721 baud_rate = baud_rate_of(speed);
726 * setdtr - control the DTR line on the serial port.
727 * This is called from die(), so it shouldn't call die().
732 int modembits = TIOCM_DTR;
734 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
739 * quit - Clean up state and exit.
748 * die - like quit, except we can specify an exit status.
755 syslog(LOG_INFO, "Exit.");
760 * cleanup - restore anything which needs to be restored before we exit
769 /* drop dtr to hang up */
773 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
774 syslog(LOG_WARNING, "fcntl(F_SETFL, fdflags): %m");
781 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
782 syslog(LOG_WARNING, "tcsetattr: %m");
784 if (ioctl(fd, TIOCSETP, &initsgttyb) < 0)
785 syslog(LOG_WARNING, "ioctl(TIOCSETP): %m");
793 if (pidfilename[0] != 0 && unlink(pidfilename) < 0)
794 syslog(LOG_WARNING, "unable to unlink pid file: %m");
797 if (lockflag && !default_device)
802 static struct callout *callout = NULL; /* Callout list */
803 static struct timeval schedtime; /* Time last timeout was set */
806 * timeout - Schedule a timeout.
808 * Note that this timeout takes the number of seconds, NOT hz (as in
812 timeout(func, arg, time)
817 struct itimerval itv;
818 struct callout *newp, **oldpp;
820 MAINDEBUG((LOG_DEBUG, "Timeout %x:%x in %d seconds.",
821 (int) func, (int) arg, time));
826 if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) {
827 syslog(LOG_ERR, "Out of memory in timeout()!");
834 * Find correct place to link it in and decrement its time by the
835 * amount of time used by preceding timeouts.
837 for (oldpp = &callout;
838 *oldpp && (*oldpp)->c_time <= time;
839 oldpp = &(*oldpp)->c_next)
840 time -= (*oldpp)->c_time;
842 newp->c_next = *oldpp;
844 (*oldpp)->c_time -= time;
848 * If this is now the first callout then we have to set a new
851 if (callout == newp) {
852 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
853 itv.it_value.tv_usec = 0;
854 itv.it_value.tv_sec = callout->c_time;
855 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in timeout.",
856 itv.it_value.tv_sec));
857 if (setitimer(ITIMER_REAL, &itv, NULL)) {
858 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
861 if (gettimeofday(&schedtime, NULL)) {
862 syslog(LOG_ERR, "gettimeofday: %m");
870 * untimeout - Unschedule a timeout.
877 struct itimerval itv;
878 struct callout **copp, *freep;
881 MAINDEBUG((LOG_DEBUG, "Untimeout %x:%x.", (int) func, (int) arg));
884 * If the first callout is unscheduled then we have to set a new
888 callout->c_func == func &&
889 callout->c_arg == arg)
893 * Find first matching timeout. Add its time to the next timeouts
896 for (copp = &callout; *copp; copp = &(*copp)->c_next)
897 if ((*copp)->c_func == func &&
898 (*copp)->c_arg == arg) {
900 *copp = freep->c_next;
902 (*copp)->c_time += freep->c_time;
903 (void) free((char *) freep);
908 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
909 itv.it_value.tv_usec = 0;
910 itv.it_value.tv_sec = callout ? callout->c_time : 0;
911 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in untimeout.",
912 itv.it_value.tv_sec));
913 if (setitimer(ITIMER_REAL, &itv, NULL)) {
914 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
917 if (gettimeofday(&schedtime, NULL)) {
918 syslog(LOG_ERR, "gettimeofday: %m");
926 * adjtimeout - Decrement the first timeout by the amount of time since
938 * Make sure that the clock hasn't been warped dramatically.
939 * Account for recently expired, but blocked timer by adding
940 * small fudge factor.
942 if (gettimeofday(&tv, NULL)) {
943 syslog(LOG_ERR, "gettimeofday: %m");
946 timediff = tv.tv_sec - schedtime.tv_sec;
948 timediff > callout->c_time + 1)
951 callout->c_time -= timediff; /* OK, Adjust time */
956 * hup - Catch SIGHUP signal.
958 * Indicates that the physical layer has been disconnected.
964 syslog(LOG_INFO, "Hangup (SIGHUP)");
966 hungup = 1; /* they hung up on us! */
967 persist = 0; /* don't try to restart */
968 adjtimeout(); /* Adjust timeouts */
969 lcp_lowerdown(0); /* Reset connection */
970 quit(); /* and die */
975 * term - Catch SIGTERM signal.
977 * Indicates that we should initiate a graceful disconnect and exit.
983 syslog(LOG_INFO, "Terminating link.");
984 persist = 0; /* don't try to restart */
985 adjtimeout(); /* Adjust timeouts */
986 lcp_close(0); /* Close connection */
991 * intr - Catch SIGINT signal (DEL/^C).
993 * Indicates that we should initiate a graceful disconnect and exit.
999 syslog(LOG_INFO, "Interrupt received: terminating link");
1000 persist = 0; /* don't try to restart */
1001 adjtimeout(); /* Adjust timeouts */
1002 lcp_close(0); /* Close connection */
1007 * alrm - Catch SIGALRM signal.
1009 * Indicates a timeout.
1015 struct itimerval itv;
1016 struct callout *freep, *list, *last;
1018 MAINDEBUG((LOG_DEBUG, "Alarm"));
1020 if (callout == NULL)
1023 * Get the first scheduled timeout and any that were scheduled
1024 * for the same time as a list, and remove them all from callout
1027 list = last = callout;
1028 while (last->c_next != NULL && last->c_next->c_time == 0)
1029 last = last->c_next;
1030 callout = last->c_next;
1031 last->c_next = NULL;
1034 * Set a new itimer if there are more timeouts scheduled.
1037 itv.it_interval.tv_sec = itv.it_interval.tv_usec = 0;
1038 itv.it_value.tv_usec = 0;
1039 itv.it_value.tv_sec = callout->c_time;
1040 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in alrm.",
1041 itv.it_value.tv_sec));
1042 if (setitimer(ITIMER_REAL, &itv, NULL)) {
1043 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
1046 if (gettimeofday(&schedtime, NULL)) {
1047 syslog(LOG_ERR, "gettimeofday: %m");
1053 * Now call all the timeout routines scheduled for this time.
1056 (*list->c_func)(list->c_arg);
1058 list = list->c_next;
1059 (void) free((char *) freep);
1066 * chld - Catch SIGCHLD signal.
1067 * Calls reap_kids to get status for any dead kids.
1078 * io - Catch SIGIO signal.
1080 * Indicates that incoming data is available.
1090 struct timeval notime;
1093 MAINDEBUG((LOG_DEBUG, "IO signal received"));
1094 adjtimeout(); /* Adjust timeouts */
1096 /* Yup, this is for real */
1097 for (;;) { /* Read all available packets */
1098 p = inpacket_buf; /* point to beginning of packet buffer */
1100 len = read_packet(inpacket_buf);
1105 MAINDEBUG((LOG_DEBUG, "End of file on fd!"));
1110 if (debug /*&& (debugflags & DBG_INPACKET)*/)
1111 log_packet(p, len, "rcvd ");
1113 if (len < DLLHEADERLEN) {
1114 MAINDEBUG((LOG_INFO, "io(): Received short packet."));
1118 p += 2; /* Skip address and control */
1119 GETSHORT(protocol, p);
1120 len -= DLLHEADERLEN;
1123 * Toss all non-LCP packets unless LCP is OPEN.
1125 if (protocol != LCP && lcp_fsm[0].state != OPENED) {
1126 MAINDEBUG((LOG_INFO,
1127 "io(): Received non-LCP packet when LCP not open."));
1132 * Upcall the proper protocol input routine.
1134 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1135 if (prottbl[i].protocol == protocol) {
1136 (*prottbl[i].input)(0, p, len);
1140 if (i == sizeof (prottbl) / sizeof (struct protent)) {
1141 syslog(LOG_WARNING, "input: Unknown protocol (%x) received!",
1143 lcp_sprotrej(0, p - DLLHEADERLEN, len + DLLHEADERLEN);
1149 * demuxprotrej - Demultiplex a Protocol-Reject.
1152 demuxprotrej(unit, protocol)
1159 * Upcall the proper Protocol-Reject routine.
1161 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1162 if (prottbl[i].protocol == protocol) {
1163 (*prottbl[i].protrej)(unit);
1168 "demuxprotrej: Unrecognized Protocol-Reject for protocol %d!",
1174 * incdebug - Catch SIGUSR1 signal.
1176 * Increment debug flag.
1183 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
1184 setlogmask(LOG_UPTO(LOG_DEBUG));
1190 * nodebug - Catch SIGUSR2 signal.
1192 * Turn off debugging.
1199 setlogmask(LOG_UPTO(LOG_WARNING));
1205 * device_script - run a program to connect or disconnect the
1209 device_script(program, in, out)
1218 sigaddset(&mask, SIGINT);
1219 sigaddset(&mask, SIGHUP);
1220 sigprocmask(SIG_BLOCK, &mask, &mask);
1225 syslog(LOG_ERR, "fork: %m");
1230 setreuid(getuid(), getuid());
1231 setregid(getgid(), getgid());
1232 sigprocmask(SIG_SETMASK, &mask, NULL);
1235 execl("/bin/sh", "sh", "-c", program, (char *)0);
1236 syslog(LOG_ERR, "could not exec /bin/sh: %m");
1241 while (waitpid(pid, &status, 0) < 0) {
1244 syslog(LOG_ERR, "waiting for (dis)connection process: %m");
1247 sigprocmask(SIG_SETMASK, &mask, NULL);
1249 return (status == 0 ? 0 : -1);
1254 * run-program - execute a program with given arguments,
1255 * but don't wait for it.
1256 * If the program can't be executed, logs an error unless
1257 * must_exist is 0 and the program file doesn't exist.
1260 run_program(prog, args, must_exist)
1269 syslog(LOG_ERR, "can't fork to run %s: %m", prog);
1274 if (must_exist || errno != ENOENT)
1275 syslog(LOG_WARNING, "can't execute %s: %m", prog);
1278 MAINDEBUG((LOG_DEBUG, "Script %s started; pid = %d", prog, pid));
1285 * reap_kids - get status from any dead child processes,
1286 * and log a message for abnormal terminations.
1293 if (n_children == 0)
1295 if ((pid = waitpid(-1, &status, WNOHANG)) == -1) {
1296 if (errno != ECHILD)
1297 syslog(LOG_ERR, "waitpid: %m");
1302 if (WIFSIGNALED(status)) {
1303 syslog(LOG_WARNING, "child process %d terminated with signal %d",
1304 pid, WTERMSIG(status));
1311 * log_packet - format a packet and log it.
1314 char line[256]; /* line to be logged accumulated here */
1318 log_packet(p, len, prefix)
1323 strcpy(line, prefix);
1324 linep = line + strlen(line);
1325 format_packet(p, len, pr_log, NULL);
1327 syslog(LOG_DEBUG, "%s", line);
1331 * format_packet - make a readable representation of a packet,
1332 * calling `printer(arg, format, ...)' to output it.
1335 format_packet(p, len, printer, arg)
1338 void (*printer) __ARGS((void *, char *, ...));
1345 if (len >= DLLHEADERLEN && p[0] == ALLSTATIONS && p[1] == UI) {
1348 len -= DLLHEADERLEN;
1349 for (i = 0; i < N_PROTO; ++i)
1350 if (proto == prottbl[i].protocol)
1353 printer(arg, "[%s", prottbl[i].name);
1354 n = (*prottbl[i].printpkt)(p, len, printer, arg);
1359 printer(arg, "[proto=0x%x]", proto);
1363 for (; len > 0; --len) {
1365 printer(arg, " %.2x", x);
1373 pr_log(void *arg, char *fmt, ...)
1379 va_start(pvar, fmt);
1380 vsprintf(buf, fmt, pvar);
1384 if (linep + n + 1 > line + sizeof(line)) {
1385 syslog(LOG_DEBUG, "%s", line);
1392 #else /* __STDC__ */
1393 #include <varargs.h>
1396 pr_log(arg, fmt, va_alist)
1406 vsprintf(buf, fmt, pvar);
1410 if (linep + n + 1 > line + sizeof(line)) {
1411 syslog(LOG_DEBUG, "%s", line);
1420 * print_string - print a readable representation of a string using
1424 print_string(p, len, printer, arg)
1427 void (*printer) __ARGS((void *, char *, ...));
1433 for (; len > 0; --len) {
1435 if (' ' <= c && c <= '~')
1436 printer(arg, "%c", c);
1438 printer(arg, "\\%.3o", c);
1444 * novm - log an error message saying we ran out of memory, and die.
1450 syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg);