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.11 1994/05/24 11:25:33 paulus Exp $";
37 * If REQ_SYSOPTIONS is defined to 1, pppd will not run unless
38 * /etc/ppp/options exists.
40 #ifndef REQ_SYSOPTIONS
41 #define REQ_SYSOPTIONS 1
48 #include <sys/ioctl.h>
53 #include <sys/param.h>
54 #include <sys/types.h>
56 #include <sys/socket.h>
62 #include <net/if_ppp.h>
75 #include "pathnames.h"
76 #include "patchlevel.h"
88 static char *pidpath = PIDPATH; /* filename in which pid will be stored */
90 static char *pidpath = _PATH_PIDFILE;
94 char ifname[IFNAMSIZ]; /* Interface name */
95 int ifunit; /* Interface unit number */
97 char *progname; /* Name of this program */
98 char hostname[MAXNAMELEN]; /* Our hostname */
99 char our_name[MAXNAMELEN];
100 char remote_name[MAXNAMELEN];
101 static char pidfilename[MAXPATHLEN];
103 static pid_t pid; /* Our pid */
104 static pid_t pgrpid; /* Process Group ID */
105 uid_t uid; /* Our real user-id */
107 char devname[MAXPATHLEN] = "/dev/tty"; /* Device name */
108 int default_device = TRUE; /* use default device (stdin/out) */
110 int fd = -1; /* Device file descriptor */
111 int s; /* Socket file descriptor */
113 int phase; /* where the link is at */
116 static struct sgttyb initsgttyb; /* Initial TTY sgttyb */
118 static struct termios inittermios; /* Initial TTY termios */
121 static int initfdflags = -1; /* Initial file descriptor flags */
123 static int restore_term; /* 1 => we've munged the terminal */
125 u_char outpacket_buf[MTU+DLLHEADERLEN]; /* buffer for outgoing packet */
126 static u_char inpacket_buf[MTU+DLLHEADERLEN]; /* buffer for incoming packet */
128 int hungup; /* terminal has been hung up */
129 static int n_children; /* # child processes still running */
131 /* configured variables */
133 int debug = 0; /* Debug flag */
134 int kdebugflag = 0; /* Kernel debugging flag */
135 char user[MAXNAMELEN]; /* username for PAP */
136 char passwd[MAXSECRETLEN]; /* password for PAP */
137 char *connector = NULL; /* "connect" command */
138 char *disconnector = NULL; /* "disconnect" command */
139 int inspeed = 0; /* Input/Output speed requested */
140 int baud_rate; /* bits/sec currently used */
141 u_long netmask = 0; /* netmask to use on ppp interface */
142 int crtscts = 0; /* use h/w flow control */
143 int nodetach = 0; /* don't fork */
144 int modem = 0; /* use modem control lines */
145 int auth_required = 0; /* require peer to authenticate */
146 int defaultroute = 0; /* assign default route through interface */
147 int proxyarp = 0; /* set entry in arp table */
148 int persist = 0; /* re-initiate on termination */
149 int answer = 0; /* wait for incoming call */
150 int uselogin = 0; /* check PAP info against /etc/passwd */
151 int lockflag = 0; /* lock the serial device */
155 static void hup __ARGS((int));
156 static void intr __ARGS((int));
157 static void term __ARGS((int));
158 static void alrm __ARGS((int));
159 static void io __ARGS((int));
160 static void chld __ARGS((int));
161 static void incdebug __ARGS((int));
162 static void nodebug __ARGS((int));
163 void establish_ppp __ARGS((void));
165 void reap_kids __ARGS((void));
166 void cleanup __ARGS((int, caddr_t));
167 void die __ARGS((int));
168 void novm __ARGS((char *));
170 void log_packet __ARGS((u_char *, int, char *));
171 void format_packet __ARGS((u_char *, int,
172 void (*) (void *, char *, ...), void *));
173 void pr_log __ARGS((void *, char *, ...));
175 extern char *ttyname __ARGS((int));
176 extern char *getlogin __ARGS((void));
179 * PPP Data Link Layer "protocol" table.
180 * One entry per supported protocol.
182 static struct protent {
190 { LCP, lcp_init, lcp_input, lcp_protrej, lcp_printpkt, "LCP" },
191 { IPCP, ipcp_init, ipcp_input, ipcp_protrej, ipcp_printpkt, "IPCP" },
192 { UPAP, upap_init, upap_input, upap_protrej, upap_printpkt, "PAP" },
193 { CHAP, ChapInit, ChapInput, ChapProtocolReject, ChapPrintPkt, "CHAP" },
196 #define N_PROTO (sizeof(prottbl) / sizeof(prottbl[0]))
213 if (gethostname(hostname, MAXNAMELEN) < 0 ) {
214 perror("couldn't get hostname");
217 hostname[MAXNAMELEN-1] = 0;
222 if (!ppp_available()) {
223 fprintf(stderr, "Sorry - PPP is not available on this system\n");
228 * Initialize to the standard option set, then parse, in order,
229 * the system options file, the user's options file, and the command
232 for (i = 0; i < N_PROTO; i++)
233 (*prottbl[i].init)(0);
237 if (!options_from_file(_PATH_SYSOPTIONS, REQ_SYSOPTIONS, 0) ||
238 !options_from_user() ||
239 !parse_args(argc-1, argv+1) ||
242 check_auth_options();
246 * Initialize syslog system and magic number package.
249 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
250 setlogmask(LOG_UPTO(LOG_INFO));
252 openlog("pppd", LOG_PID);
253 #define LOG_UPTO(x) (x)
254 #define setlogmask(x) (x)
257 setlogmask(LOG_UPTO(LOG_DEBUG));
264 if (pw != NULL && pw->pw_name != NULL)
269 syslog(LOG_NOTICE, "pppd %s.%d started by %s, uid %d",
270 VERSION, PATCHLEVEL, p, uid);
274 * Make sure we can set the serial device to be our controlling terminal.
276 if (default_device) {
278 * No device name was specified:
279 * we are in the device's session already.
281 if ((pgrpid = getpgrp(0)) < 0) {
282 syslog(LOG_ERR, "getpgrp(0): %m");
288 * Not default device: make sure we're not a process group leader,
289 * then become session leader of a new session (so we can make
290 * our device its controlling terminal and thus get SIGHUPs).
293 /* fork so we're not a process group leader */
295 exit(0); /* parent is finished */
298 syslog(LOG_ERR, "fork: %m");
301 pid = getpid(); /* otherwise pid is 0 in child */
304 * try to put ourself into our parent's process group,
305 * so we're not a process group leader
307 if (setpgrp(pid, getppid()) < 0)
308 syslog(LOG_WARNING, "setpgrp: %m");
311 /* create new session */
312 if ((pgrpid = setsid()) < 0) {
313 syslog(LOG_ERR, "setsid(): %m");
319 if (lockflag && !default_device)
320 if (lock(devname) < 0)
323 /* Get an internet socket for doing socket ioctl's on. */
324 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
325 syslog(LOG_ERR, "socket : %m");
330 * Compute mask of all interesting signals and install signal handlers
331 * for each. Only one signal handler may be active at a time. Therefore,
332 * all other signals should be masked when any handler is executing.
335 sigaddset(&mask, SIGHUP);
336 sigaddset(&mask, SIGINT);
337 sigaddset(&mask, SIGALRM);
338 sigaddset(&mask, SIGIO);
339 sigaddset(&mask, SIGCHLD);
341 sigaddset(&mask, SIGPOLL);
344 #define SIGNAL(s, handler) { \
345 sa.sa_handler = handler; \
346 if (sigaction(s, &sa, NULL) < 0) { \
347 syslog(LOG_ERR, "sigaction(%d): %m", s); \
354 SIGNAL(SIGHUP, hup); /* Hangup */
355 SIGNAL(SIGINT, intr); /* Interrupt */
356 SIGNAL(SIGTERM, term); /* Terminate */
357 SIGNAL(SIGALRM, alrm); /* Timeout */
358 SIGNAL(SIGIO, io); /* Input available */
359 SIGNAL(SIGCHLD, chld); /* Death of child process */
361 SIGNAL(SIGPOLL, io); /* Input available */
364 signal(SIGUSR1, incdebug); /* Increment debug flag */
365 signal(SIGUSR2, nodebug); /* Reset debug flag */
368 * Block SIGIOs and SIGPOLLs for now
371 sigaddset(&mask, SIGIO);
373 sigaddset(&mask, SIGPOLL);
375 sigprocmask(SIG_BLOCK, &mask, NULL);
378 * Open the serial device and set it up to be the ppp interface.
380 if ((fd = open(devname, O_RDWR /*| O_NDELAY*/)) < 0) {
381 syslog(LOG_ERR, "open(%s): %m", devname);
387 /* set device to be controlling tty */
388 if (!default_device && ioctl(fd, TIOCSCTTY) < 0) {
389 syslog(LOG_ERR, "ioctl(TIOCSCTTY): %m");
392 #endif /* TIOCSCTTY */
394 /* run connection script */
396 MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector));
398 /* set line speed, flow control, etc.; set CLOCAL for now */
401 /* drop dtr to hang up in case modem is off hook */
402 if (!default_device && modem) {
408 if (device_script(connector, fd, fd) < 0) {
409 syslog(LOG_ERR, "could not set up connection");
414 syslog(LOG_INFO, "Connected...");
415 sleep(1); /* give it time to set up its terminal */
418 /* set line speed, flow control, etc.; clear CLOCAL if modem option */
421 /* set up the serial device as a ppp interface */
424 syslog(LOG_INFO, "Using interface ppp%d", ifunit);
425 (void) sprintf(ifname, "ppp%d", ifunit);
427 /* write pid to file */
428 (void) sprintf(pidfilename, "%s/%s.pid", pidpath, ifname);
429 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
430 fprintf(pidfile, "%d\n", pid);
431 (void) fclose(pidfile);
433 syslog(LOG_ERR, "unable to create pid file: %m");
438 * Set process group of device to our process group so we can get
439 * SIGIOs and SIGHUPs.
442 if (default_device) {
443 int id = tcgetpgrp(fd);
445 syslog(LOG_WARNING, "warning: not in tty's process group");
448 if (tcsetpgrp(fd, pgrpid) < 0) {
449 syslog(LOG_ERR, "tcsetpgrp(): %m");
454 /* set process group on tty so we get SIGIO's */
455 if (ioctl(fd, TIOCSPGRP, &pgrpid) < 0) {
456 syslog(LOG_ERR, "ioctl(TIOCSPGRP): %m");
462 * Record initial device flags, then set device to cause SIGIO
463 * signals to be generated.
465 if ((initfdflags = fcntl(fd, F_GETFL)) == -1) {
466 syslog(LOG_ERR, "fcntl(F_GETFL): %m");
470 #ifdef _linux_ /* This is a kludge for Linux. FIXME !!! -- later. */
475 if (ioctl(fd, PPPIOCSINPSIG, &sig) == -1) {
476 syslog(LOG_ERR, "ioctl(PPPIOCSINPSIG): %m");
482 if (fcntl(fd, F_SETFL, FNDELAY | FASYNC) == -1) {
483 syslog(LOG_ERR, "fcntl(F_SETFL, FNDELAY | FASYNC): %m");
488 * Block all signals, start opening the connection, and wait for
489 * incoming signals (reply, timeout, etc.).
491 syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devname);
492 sigprocmask(SIG_BLOCK, &mask, NULL); /* Block signals now */
493 lcp_lowerup(0); /* XXX Well, sort of... */
494 lcp_open(0); /* Start protocol */
495 for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; )
496 sigpause(0); /* Wait for next signal */
499 * Run disconnector script, if requested
502 if (device_script(disconnector, fd, fd) < 0) {
503 syslog(LOG_WARNING, "disconnect script failed");
507 syslog(LOG_INFO, "Disconnected...");
515 * XXX assume speed_t values numerically equal bits per second
516 * (so we can ask for any speed).
518 #define translate_speed(bps) (bps)
519 #define baud_rate_of(speed) (speed)
523 * List of valid speeds.
526 int speed_int, speed_val;
598 * Translate from bits/second to a speed_t.
604 struct speed *speedp;
608 for (speedp = speeds; speedp->speed_int; speedp++)
609 if (bps == speedp->speed_int)
610 return speedp->speed_val;
611 syslog(LOG_WARNING, "speed %d not supported", bps);
616 * Translate from a speed_t to bits/second.
622 struct speed *speedp;
626 for (speedp = speeds; speedp->speed_int; speedp++)
627 if (speed == speedp->speed_val)
628 return speedp->speed_int;
634 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
635 * at the requested speed, etc. If `local' is true, set CLOCAL
636 * regardless of whether the modem option was specified.
638 set_up_tty(fd, local)
645 if (tcgetattr(fd, &tios) < 0) {
646 syslog(LOG_ERR, "tcgetattr: %m");
654 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
656 tios.c_cflag |= CRTSCTS;
658 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
661 tios.c_cflag |= CS8 | CREAD | HUPCL;
663 tios.c_cflag |= CLOCAL;
664 tios.c_iflag = IGNBRK | IGNPAR;
668 tios.c_cc[VTIME] = 0;
671 tios.c_iflag |= IXOFF;
672 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
673 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
676 speed = translate_speed(inspeed);
678 cfsetospeed(&tios, speed);
679 cfsetispeed(&tios, speed);
681 speed = cfgetospeed(&tios);
684 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
685 syslog(LOG_ERR, "tcsetattr: %m");
691 if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
692 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
693 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
694 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
699 struct sgttyb sgttyb;
702 * Put the tty in raw mode.
704 if (ioctl(fd, TIOCGETP, &sgttyb) < 0) {
705 syslog(LOG_ERR, "ioctl(TIOCGETP): %m");
712 sgttyb.sg_flags = RAW | ANYP;
713 speed = translate_speed(inspeed);
715 sgttyb.sg_ispeed = speed;
717 speed = sgttyb.sg_ispeed;
719 if (ioctl(fd, TIOCSETP, &sgttyb) < 0) {
720 syslog(LOG_ERR, "ioctl(TIOCSETP): %m");
725 baud_rate = baud_rate_of(speed);
730 * setdtr - control the DTR line on the serial port.
731 * This is called from die(), so it shouldn't call die().
736 int modembits = TIOCM_DTR;
738 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
743 * quit - Clean up state and exit.
752 * die - like quit, except we can specify an exit status.
759 syslog(LOG_INFO, "Exit.");
764 * cleanup - restore anything which needs to be restored before we exit
773 /* drop dtr to hang up */
777 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
778 syslog(LOG_WARNING, "fcntl(F_SETFL, fdflags): %m");
785 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
786 syslog(LOG_WARNING, "tcsetattr: %m");
788 if (ioctl(fd, TIOCSETP, &initsgttyb) < 0)
789 syslog(LOG_WARNING, "ioctl(TIOCSETP): %m");
797 if (pidfilename[0] != 0 && unlink(pidfilename) < 0)
798 syslog(LOG_WARNING, "unable to unlink pid file: %m");
801 if (lockflag && !default_device)
806 static struct callout *callout = NULL; /* Callout list */
807 static struct timeval schedtime; /* Time last timeout was set */
810 * timeout - Schedule a timeout.
812 * Note that this timeout takes the number of seconds, NOT hz (as in
816 timeout(func, arg, time)
821 struct itimerval itv;
822 struct callout *newp, **oldpp;
824 MAINDEBUG((LOG_DEBUG, "Timeout %x:%x in %d seconds.",
825 (int) func, (int) arg, time));
830 if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) {
831 syslog(LOG_ERR, "Out of memory in timeout()!");
838 * Find correct place to link it in and decrement its time by the
839 * amount of time used by preceding timeouts.
841 for (oldpp = &callout;
842 *oldpp && (*oldpp)->c_time <= time;
843 oldpp = &(*oldpp)->c_next)
844 time -= (*oldpp)->c_time;
846 newp->c_next = *oldpp;
848 (*oldpp)->c_time -= time;
852 * If this is now the first callout then we have to set a new
855 if (callout == newp) {
856 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
857 itv.it_value.tv_usec = 0;
858 itv.it_value.tv_sec = callout->c_time;
859 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in timeout.",
860 itv.it_value.tv_sec));
861 if (setitimer(ITIMER_REAL, &itv, NULL)) {
862 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
865 if (gettimeofday(&schedtime, NULL)) {
866 syslog(LOG_ERR, "gettimeofday: %m");
874 * untimeout - Unschedule a timeout.
881 struct itimerval itv;
882 struct callout **copp, *freep;
885 MAINDEBUG((LOG_DEBUG, "Untimeout %x:%x.", (int) func, (int) arg));
888 * If the first callout is unscheduled then we have to set a new
892 callout->c_func == func &&
893 callout->c_arg == arg)
897 * Find first matching timeout. Add its time to the next timeouts
900 for (copp = &callout; *copp; copp = &(*copp)->c_next)
901 if ((*copp)->c_func == func &&
902 (*copp)->c_arg == arg) {
904 *copp = freep->c_next;
906 (*copp)->c_time += freep->c_time;
907 (void) free((char *) freep);
912 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
913 itv.it_value.tv_usec = 0;
914 itv.it_value.tv_sec = callout ? callout->c_time : 0;
915 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in untimeout.",
916 itv.it_value.tv_sec));
917 if (setitimer(ITIMER_REAL, &itv, NULL)) {
918 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
921 if (gettimeofday(&schedtime, NULL)) {
922 syslog(LOG_ERR, "gettimeofday: %m");
930 * adjtimeout - Decrement the first timeout by the amount of time since
942 * Make sure that the clock hasn't been warped dramatically.
943 * Account for recently expired, but blocked timer by adding
944 * small fudge factor.
946 if (gettimeofday(&tv, NULL)) {
947 syslog(LOG_ERR, "gettimeofday: %m");
950 timediff = tv.tv_sec - schedtime.tv_sec;
952 timediff > callout->c_time + 1)
955 callout->c_time -= timediff; /* OK, Adjust time */
960 * hup - Catch SIGHUP signal.
962 * Indicates that the physical layer has been disconnected.
968 syslog(LOG_INFO, "Hangup (SIGHUP)");
970 hungup = 1; /* they hung up on us! */
971 persist = 0; /* don't try to restart */
972 adjtimeout(); /* Adjust timeouts */
973 lcp_lowerdown(0); /* Reset connection */
974 quit(); /* and die */
979 * term - Catch SIGTERM signal.
981 * Indicates that we should initiate a graceful disconnect and exit.
987 syslog(LOG_INFO, "Terminating link.");
988 persist = 0; /* don't try to restart */
989 adjtimeout(); /* Adjust timeouts */
990 lcp_close(0); /* Close connection */
995 * intr - Catch SIGINT signal (DEL/^C).
997 * Indicates that we should initiate a graceful disconnect and exit.
1003 syslog(LOG_INFO, "Interrupt received: terminating link");
1004 persist = 0; /* don't try to restart */
1005 adjtimeout(); /* Adjust timeouts */
1006 lcp_close(0); /* Close connection */
1011 * alrm - Catch SIGALRM signal.
1013 * Indicates a timeout.
1019 struct itimerval itv;
1020 struct callout *freep, *list, *last;
1022 MAINDEBUG((LOG_DEBUG, "Alarm"));
1024 if (callout == NULL)
1027 * Get the first scheduled timeout and any that were scheduled
1028 * for the same time as a list, and remove them all from callout
1031 list = last = callout;
1032 while (last->c_next != NULL && last->c_next->c_time == 0)
1033 last = last->c_next;
1034 callout = last->c_next;
1035 last->c_next = NULL;
1038 * Set a new itimer if there are more timeouts scheduled.
1041 itv.it_interval.tv_sec = itv.it_interval.tv_usec = 0;
1042 itv.it_value.tv_usec = 0;
1043 itv.it_value.tv_sec = callout->c_time;
1044 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in alrm.",
1045 itv.it_value.tv_sec));
1046 if (setitimer(ITIMER_REAL, &itv, NULL)) {
1047 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
1050 if (gettimeofday(&schedtime, NULL)) {
1051 syslog(LOG_ERR, "gettimeofday: %m");
1057 * Now call all the timeout routines scheduled for this time.
1060 (*list->c_func)(list->c_arg);
1062 list = list->c_next;
1063 (void) free((char *) freep);
1070 * chld - Catch SIGCHLD signal.
1071 * Calls reap_kids to get status for any dead kids.
1082 * io - Catch SIGIO signal.
1084 * Indicates that incoming data is available.
1094 struct timeval notime;
1097 MAINDEBUG((LOG_DEBUG, "IO signal received"));
1098 adjtimeout(); /* Adjust timeouts */
1100 /* Yup, this is for real */
1101 for (;;) { /* Read all available packets */
1102 p = inpacket_buf; /* point to beginning of packet buffer */
1104 len = read_packet(inpacket_buf);
1109 MAINDEBUG((LOG_DEBUG, "End of file on fd!"));
1114 if (debug /*&& (debugflags & DBG_INPACKET)*/)
1115 log_packet(p, len, "rcvd ");
1117 if (len < DLLHEADERLEN) {
1118 MAINDEBUG((LOG_INFO, "io(): Received short packet."));
1122 p += 2; /* Skip address and control */
1123 GETSHORT(protocol, p);
1124 len -= DLLHEADERLEN;
1127 * Toss all non-LCP packets unless LCP is OPEN.
1129 if (protocol != LCP && lcp_fsm[0].state != OPENED) {
1130 MAINDEBUG((LOG_INFO,
1131 "io(): Received non-LCP packet when LCP not open."));
1136 * Upcall the proper protocol input routine.
1138 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1139 if (prottbl[i].protocol == protocol) {
1140 (*prottbl[i].input)(0, p, len);
1144 if (i == sizeof (prottbl) / sizeof (struct protent)) {
1145 syslog(LOG_WARNING, "input: Unknown protocol (%x) received!",
1147 lcp_sprotrej(0, p - DLLHEADERLEN, len + DLLHEADERLEN);
1153 * demuxprotrej - Demultiplex a Protocol-Reject.
1156 demuxprotrej(unit, protocol)
1163 * Upcall the proper Protocol-Reject routine.
1165 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1166 if (prottbl[i].protocol == protocol) {
1167 (*prottbl[i].protrej)(unit);
1172 "demuxprotrej: Unrecognized Protocol-Reject for protocol %d!",
1178 * incdebug - Catch SIGUSR1 signal.
1180 * Increment debug flag.
1187 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
1188 setlogmask(LOG_UPTO(LOG_DEBUG));
1194 * nodebug - Catch SIGUSR2 signal.
1196 * Turn off debugging.
1203 setlogmask(LOG_UPTO(LOG_WARNING));
1209 * device_script - run a program to connect or disconnect the
1213 device_script(program, in, out)
1222 sigaddset(&mask, SIGINT);
1223 sigaddset(&mask, SIGHUP);
1224 sigprocmask(SIG_BLOCK, &mask, &mask);
1229 syslog(LOG_ERR, "fork: %m");
1234 setreuid(getuid(), getuid());
1235 setregid(getgid(), getgid());
1236 sigprocmask(SIG_SETMASK, &mask, NULL);
1239 execl("/bin/sh", "sh", "-c", program, (char *)0);
1240 syslog(LOG_ERR, "could not exec /bin/sh: %m");
1245 while (waitpid(pid, &status, 0) < 0) {
1248 syslog(LOG_ERR, "waiting for (dis)connection process: %m");
1251 sigprocmask(SIG_SETMASK, &mask, NULL);
1253 return (status == 0 ? 0 : -1);
1258 * run-program - execute a program with given arguments,
1259 * but don't wait for it.
1260 * If the program can't be executed, logs an error unless
1261 * must_exist is 0 and the program file doesn't exist.
1264 run_program(prog, args, must_exist)
1273 syslog(LOG_ERR, "can't fork to run %s: %m", prog);
1278 if (must_exist || errno != ENOENT)
1279 syslog(LOG_WARNING, "can't execute %s: %m", prog);
1282 MAINDEBUG((LOG_DEBUG, "Script %s started; pid = %d", prog, pid));
1289 * reap_kids - get status from any dead child processes,
1290 * and log a message for abnormal terminations.
1297 if (n_children == 0)
1299 if ((pid = waitpid(-1, &status, WNOHANG)) == -1) {
1300 if (errno != ECHILD)
1301 syslog(LOG_ERR, "waitpid: %m");
1306 if (WIFSIGNALED(status)) {
1307 syslog(LOG_WARNING, "child process %d terminated with signal %d",
1308 pid, WTERMSIG(status));
1315 * log_packet - format a packet and log it.
1318 char line[256]; /* line to be logged accumulated here */
1322 log_packet(p, len, prefix)
1327 strcpy(line, prefix);
1328 linep = line + strlen(line);
1329 format_packet(p, len, pr_log, NULL);
1331 syslog(LOG_DEBUG, "%s", line);
1335 * format_packet - make a readable representation of a packet,
1336 * calling `printer(arg, format, ...)' to output it.
1339 format_packet(p, len, printer, arg)
1342 void (*printer) __ARGS((void *, char *, ...));
1349 if (len >= DLLHEADERLEN && p[0] == ALLSTATIONS && p[1] == UI) {
1352 len -= DLLHEADERLEN;
1353 for (i = 0; i < N_PROTO; ++i)
1354 if (proto == prottbl[i].protocol)
1357 printer(arg, "[%s", prottbl[i].name);
1358 n = (*prottbl[i].printpkt)(p, len, printer, arg);
1363 printer(arg, "[proto=0x%x]", proto);
1367 for (; len > 0; --len) {
1369 printer(arg, " %.2x", x);
1377 pr_log(void *arg, char *fmt, ...)
1383 va_start(pvar, fmt);
1384 vsprintf(buf, fmt, pvar);
1388 if (linep + n + 1 > line + sizeof(line)) {
1389 syslog(LOG_DEBUG, "%s", line);
1396 #else /* __STDC__ */
1397 #include <varargs.h>
1400 pr_log(arg, fmt, va_alist)
1410 vsprintf(buf, fmt, pvar);
1414 if (linep + n + 1 > line + sizeof(line)) {
1415 syslog(LOG_DEBUG, "%s", line);
1424 * print_string - print a readable representation of a string using
1428 print_string(p, len, printer, arg)
1431 void (*printer) __ARGS((void *, char *, ...));
1437 for (; len > 0; --len) {
1439 if (' ' <= c && c <= '~')
1440 printer(arg, "%c", c);
1442 printer(arg, "\\%.3o", c);
1448 * novm - log an error message saying we ran out of memory, and die.
1454 syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg);