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.8 1994/05/01 11:44:43 paulus Exp $";
36 * If REQ_SYSOPTIONS is defined to 1, pppd will not run unless
37 * /etc/ppp/options exists.
39 #ifndef REQ_SYSOPTIONS
40 #define REQ_SYSOPTIONS 0
51 #include <sys/ioctl.h>
56 #include <sys/param.h>
57 #include <sys/types.h>
59 #include <sys/socket.h>
65 #include <net/if_ppp.h>
82 #include "pathnames.h"
83 #include "patchlevel.h"
95 static char *pidpath = PIDPATH; /* filename in which pid will be stored */
97 static char *pidpath = _PATH_PIDFILE;
101 char ifname[IFNAMSIZ]; /* Interface name */
102 int ifunit; /* Interface unit number */
104 char *progname; /* Name of this program */
105 char hostname[MAXNAMELEN]; /* Our hostname */
106 char our_name[MAXNAMELEN];
107 char remote_name[MAXNAMELEN];
109 static pid_t pid; /* Our pid */
110 static pid_t pgrpid; /* Process Group ID */
111 static char pidfilename[MAXPATHLEN];
113 char devname[MAXPATHLEN] = "/dev/tty"; /* Device name */
114 int default_device = TRUE; /* use default device (stdin/out) */
116 int fd; /* Device file descriptor */
117 int s; /* Socket file descriptor */
119 int phase; /* where the link is at */
122 static struct sgttyb initsgttyb; /* Initial TTY sgttyb */
124 static struct termios inittermios; /* Initial TTY termios */
127 static int initfdflags = -1; /* Initial file descriptor flags */
129 static int restore_term; /* 1 => we've munged the terminal */
131 u_char outpacket_buf[MTU+DLLHEADERLEN]; /* buffer for outgoing packet */
132 static u_char inpacket_buf[MTU+DLLHEADERLEN]; /* buffer for incoming packet */
134 int hungup; /* terminal has been hung up */
135 static int n_children; /* # child processes still running */
137 /* configured variables */
139 int debug = 0; /* Debug flag */
140 int kdebugflag = 0; /* Kernel debugging flag */
141 char user[MAXNAMELEN]; /* username for PAP */
142 char passwd[MAXSECRETLEN]; /* password for PAP */
143 char *connector = NULL; /* "connect" command */
144 char *disconnector = NULL; /* "disconnect" command */
145 int inspeed = 0; /* Input/Output speed requested */
146 int baud_rate; /* bits/sec currently used */
147 u_long netmask = 0; /* netmask to use on ppp interface */
148 int crtscts = 0; /* use h/w flow control */
149 int nodetach = 0; /* don't fork */
150 int modem = 0; /* use modem control lines */
151 int auth_required = 0; /* require peer to authenticate */
152 int defaultroute = 0; /* assign default route through interface */
153 int proxyarp = 0; /* set entry in arp table */
154 int persist = 0; /* re-initiate on termination */
155 int answer = 0; /* wait for incoming call */
156 int uselogin = 0; /* check PAP info against /etc/passwd */
157 int lockflag = 0; /* lock the serial device */
161 static void hup __ARGS((int, int, struct sigcontext *, char *));
162 static void intr __ARGS((int, int, struct sigcontext *, char *));
163 static void term __ARGS((int, int, struct sigcontext *, char *));
164 static void alrm __ARGS((int, int, struct sigcontext *, char *));
165 static void io __ARGS((int, int, struct sigcontext *, char *));
166 static void incdebug __ARGS((int));
167 static void nodebug __ARGS((int));
168 void establish_ppp __ARGS((void));
170 void reap_kids __ARGS((void));
171 void cleanup __ARGS((int, caddr_t));
172 void die __ARGS((int));
173 void novm __ARGS((char *));
175 void log_packet __ARGS((u_char *, int, char *));
176 void format_packet __ARGS((u_char *, int,
177 void (*) (void *, char *, ...), void *));
178 void pr_log __ARGS((void *, char *, ...));
181 extern char *ttyname __ARGS((int));
183 extern char *getlogin __ARGS((void));
186 * PPP Data Link Layer "protocol" table.
187 * One entry per supported protocol.
189 static struct protent {
197 { LCP, lcp_init, lcp_input, lcp_protrej, lcp_printpkt, "LCP" },
198 { IPCP, ipcp_init, ipcp_input, ipcp_protrej, ipcp_printpkt, "IPCP" },
199 { UPAP, upap_init, upap_input, upap_protrej, upap_printpkt, "PAP" },
200 { CHAP, ChapInit, ChapInput, ChapProtocolReject, ChapPrintPkt, "CHAP" },
203 #define N_PROTO (sizeof(prottbl) / sizeof(prottbl[0]))
216 p = ttyname(fileno(stdin));
221 if (gethostname(hostname, MAXNAMELEN) < 0 ) {
222 perror("couldn't get hostname");
225 hostname[MAXNAMELEN-1] = 0;
229 if (!ppp_available()) {
230 fprintf(stderr, "Sorry - PPP is not available on this system\n");
235 * Initialize to the standard option set, then parse, in order,
236 * the system options file, the user's options file, and the command
239 for (i = 0; i < N_PROTO; i++)
240 (*prottbl[i].init)(0);
244 if (!options_from_file(_PATH_SYSOPTIONS, REQ_SYSOPTIONS) ||
245 !options_from_user() ||
246 !parse_args(argc-1, argv+1))
248 check_auth_options();
251 if (lockflag && !default_device)
252 if (lock(devname) < 0)
256 * Initialize syslog system and magic number package.
258 #if (BSD >= 43 || defined(sun)) && !defined(ultrix)
259 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
260 setlogmask(LOG_UPTO(LOG_INFO));
262 openlog("pppd", LOG_PID);
263 #define LOG_UPTO(x) (x)
264 #define setlogmask(x) (x)
267 setlogmask(LOG_UPTO(LOG_DEBUG));
274 syslog(LOG_NOTICE, "pppd %s.%d started by %s, uid %d",
275 VERSION, PATCHLEVEL, p, getuid());
279 * Make sure we can set the serial device to be our controlling terminal.
281 if (default_device) {
283 * No device name was specified:
284 * we are in the device's session already.
286 if ((pgrpid = getpgrp(0)) < 0) {
287 syslog(LOG_ERR, "getpgrp(0): %m");
293 * Not default device: make sure we're not a process group leader,
294 * then become session leader of a new session (so we can make
295 * our device its controlling terminal and thus get SIGHUPs).
298 /* fork so we're not a process group leader */
300 exit(0); /* parent is finished */
303 syslog(LOG_ERR, "fork: %m");
306 pid = getpid(); /* otherwise pid is 0 in child */
309 * try to put ourself into our parent's process group,
310 * so we're not a process group leader
312 if (setpgrp(pid, getppid()) < 0)
313 syslog(LOG_WARNING, "setpgrp: %m");
316 /* create new session */
317 if ((pgrpid = setsid()) < 0) {
318 syslog(LOG_ERR, "setsid(): %m");
324 /* Get an internet socket for doing socket ioctl's on. */
325 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
326 syslog(LOG_ERR, "socket : %m");
331 * Compute mask of all interesting signals and install signal handlers
332 * for each. Only one signal handler may be active at a time. Therefore,
333 * all other signals should be masked when any handler is executing.
336 sigaddset(&mask, SIGHUP);
337 sigaddset(&mask, SIGINT);
338 sigaddset(&mask, SIGALRM);
339 sigaddset(&mask, SIGIO);
341 sigaddset(&mask, SIGPOLL);
344 #define SIGNAL(s, handler) { \
345 sv.sv_handler = handler; \
346 if (sigvec(s, &sv, NULL) < 0) { \
347 syslog(LOG_ERR, "sigvec(%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 */
360 SIGNAL(SIGPOLL, io); /* Input available */
363 signal(SIGUSR1, incdebug); /* Increment debug flag */
364 signal(SIGUSR2, nodebug); /* Reset debug flag */
367 * Block SIGIOs and SIGPOLLs for now
370 sigaddset(&mask, SIGIO);
372 sigaddset(&mask, SIGPOLL);
374 sigprocmask(SIG_BLOCK, &mask, NULL);
377 * Open the serial device and set it up to be the ppp interface.
379 if ((fd = open(devname, O_RDWR /*| O_NDELAY*/)) < 0) {
380 syslog(LOG_ERR, "open(%s): %m", devname);
386 /* set device to be controlling tty */
387 if (!default_device && ioctl(fd, TIOCSCTTY) < 0) {
388 syslog(LOG_ERR, "ioctl(TIOCSCTTY): %m");
391 #endif /* TIOCSCTTY */
393 /* run connection script */
395 MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector));
397 /* set line speed, flow control, etc.; set CLOCAL for now */
400 /* drop dtr to hang up in case modem is off hook */
401 if (!default_device && modem) {
407 if (device_script(connector, fd, fd) < 0) {
408 syslog(LOG_ERR, "could not set up connection");
413 syslog(LOG_INFO, "Connected...");
414 sleep(1); /* give it time to set up its terminal */
417 /* set line speed, flow control, etc.; clear CLOCAL if modem option */
420 /* set up the serial device as a ppp interface */
423 syslog(LOG_INFO, "Using interface ppp%d", ifunit);
424 (void) sprintf(ifname, "ppp%d", ifunit);
426 /* write pid to file */
427 (void) sprintf(pidfilename, "%s/%s.pid", pidpath, ifname);
428 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
429 fprintf(pidfile, "%d\n", pid);
430 (void) fclose(pidfile);
432 syslog(LOG_ERR, "unable to create pid file: %m");
437 * Set process group of device to our process group so we can get
438 * SIGIOs and SIGHUPs.
441 if (default_device) {
442 int id = tcgetpgrp(fd);
444 syslog(LOG_WARNING, "warning: not in tty's process group");
447 if (tcsetpgrp(fd, pgrpid) < 0) {
448 syslog(LOG_ERR, "tcsetpgrp(): %m");
453 /* set process group on tty so we get SIGIO's */
454 if (ioctl(fd, TIOCSPGRP, &pgrpid) < 0) {
455 syslog(LOG_ERR, "ioctl(TIOCSPGRP): %m");
461 * Record initial device flags, then set device to cause SIGIO
462 * signals to be generated.
464 if ((initfdflags = fcntl(fd, F_GETFL)) == -1) {
465 syslog(LOG_ERR, "fcntl(F_GETFL): %m");
468 if (fcntl(fd, F_SETFL, FNDELAY | FASYNC) == -1) {
469 syslog(LOG_ERR, "fcntl(F_SETFL, FNDELAY | FASYNC): %m");
474 * Block all signals, start opening the connection, and wait for
475 * incoming signals (reply, timeout, etc.).
477 syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devname);
478 sigprocmask(SIG_BLOCK, &mask, NULL); /* Block signals now */
479 lcp_lowerup(0); /* XXX Well, sort of... */
480 lcp_open(0); /* Start protocol */
481 for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; ) {
482 sigpause(0); /* Wait for next signal */
483 reap_kids(); /* Don't leave dead kids lying around */
487 * Run disconnector script, if requested
490 if (device_script(disconnector, fd, fd) < 0) {
491 syslog(LOG_WARNING, "disconnect script failed");
495 syslog(LOG_INFO, "Disconnected...");
503 * XXX assume speed_t values numerically equal bits per second
504 * (so we can ask for any speed).
506 #define translate_speed(bps) (bps)
507 #define baud_rate_of(speed) (speed)
511 * List of valid speeds.
514 int speed_int, speed_val;
586 * Translate from bits/second to a speed_t.
592 struct speed *speedp;
596 for (speedp = speeds; speedp->speed_int; speedp++)
597 if (bps == speedp->speed_int)
598 return speedp->speed_val;
599 syslog(LOG_WARNING, "speed %d not supported", bps);
604 * Translate from a speed_t to bits/second.
610 struct speed *speedp;
614 for (speedp = speeds; speedp->speed_int; speedp++)
615 if (speed == speedp->speed_val)
616 return speedp->speed_int;
622 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
623 * at the requested speed, etc. If `local' is true, set CLOCAL
624 * regardless of whether the modem option was specified.
626 set_up_tty(fd, local)
633 if (tcgetattr(fd, &tios) < 0) {
634 syslog(LOG_ERR, "tcgetattr: %m");
642 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
644 tios.c_cflag |= CRTSCTS;
646 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
649 tios.c_cflag |= CS8 | CREAD | HUPCL;
651 tios.c_cflag |= CLOCAL;
652 tios.c_iflag = IGNBRK | IGNPAR;
656 tios.c_cc[VTIME] = 0;
657 speed = translate_speed(inspeed);
659 cfsetospeed(&tios, speed);
660 cfsetispeed(&tios, speed);
662 speed = cfgetospeed(&tios);
665 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
666 syslog(LOG_ERR, "tcsetattr: %m");
672 struct sgttyb sgttyb;
675 * Put the tty in raw mode.
677 if (ioctl(fd, TIOCGETP, &sgttyb) < 0) {
678 syslog(LOG_ERR, "ioctl(TIOCGETP): %m");
685 sgttyb.sg_flags = RAW | ANYP;
686 speed = translate_speed(inspeed);
688 sgttyb.sg_ispeed = speed;
690 speed = sgttyb.sg_ispeed;
692 if (ioctl(fd, TIOCSETP, &sgttyb) < 0) {
693 syslog(LOG_ERR, "ioctl(TIOCSETP): %m");
698 baud_rate = baud_rate_of(speed);
703 * setdtr - control the DTR line on the serial port.
704 * This is called from die(), so it shouldn't call die().
709 int modembits = TIOCM_DTR;
711 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
716 * quit - Clean up state and exit.
725 * die - like quit, except we can specify an exit status.
732 syslog(LOG_INFO, "Exit.");
737 * cleanup - restore anything which needs to be restored before we exit
746 /* drop dtr to hang up */
750 if (fcntl(fd, F_SETFL, initfdflags) < 0)
751 syslog(LOG_WARNING, "fcntl(F_SETFL, fdflags): %m");
757 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
758 syslog(LOG_WARNING, "tcsetattr: %m");
760 if (ioctl(fd, TIOCSETP, &initsgttyb) < 0)
761 syslog(LOG_WARNING, "ioctl(TIOCSETP): %m");
769 if (pidfilename[0] != 0 && unlink(pidfilename) < 0)
770 syslog(LOG_WARNING, "unable to unlink pid file: %m");
773 if (lockflag && !default_device)
778 static struct callout *callout = NULL; /* Callout list */
779 static struct timeval schedtime; /* Time last timeout was set */
782 * timeout - Schedule a timeout.
784 * Note that this timeout takes the number of seconds, NOT hz (as in
788 timeout(func, arg, time)
793 struct itimerval itv;
794 struct callout *newp, **oldpp;
796 MAINDEBUG((LOG_DEBUG, "Timeout %x:%x in %d seconds.",
797 (int) func, (int) arg, time));
802 if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) {
803 syslog(LOG_ERR, "Out of memory in timeout()!");
810 * Find correct place to link it in and decrement its time by the
811 * amount of time used by preceding timeouts.
813 for (oldpp = &callout;
814 *oldpp && (*oldpp)->c_time <= time;
815 oldpp = &(*oldpp)->c_next)
816 time -= (*oldpp)->c_time;
818 newp->c_next = *oldpp;
820 (*oldpp)->c_time -= time;
824 * If this is now the first callout then we have to set a new
827 if (callout == newp) {
828 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
829 itv.it_value.tv_usec = 0;
830 itv.it_value.tv_sec = callout->c_time;
831 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in timeout.",
832 itv.it_value.tv_sec));
833 if (setitimer(ITIMER_REAL, &itv, NULL)) {
834 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
837 if (gettimeofday(&schedtime, NULL)) {
838 syslog(LOG_ERR, "gettimeofday: %m");
846 * untimeout - Unschedule a timeout.
853 struct itimerval itv;
854 struct callout **copp, *freep;
857 MAINDEBUG((LOG_DEBUG, "Untimeout %x:%x.", (int) func, (int) arg));
860 * If the first callout is unscheduled then we have to set a new
864 callout->c_func == func &&
865 callout->c_arg == arg)
869 * Find first matching timeout. Add its time to the next timeouts
872 for (copp = &callout; *copp; copp = &(*copp)->c_next)
873 if ((*copp)->c_func == func &&
874 (*copp)->c_arg == arg) {
876 *copp = freep->c_next;
878 (*copp)->c_time += freep->c_time;
879 (void) free((char *) freep);
884 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
885 itv.it_value.tv_usec = 0;
886 itv.it_value.tv_sec = callout ? callout->c_time : 0;
887 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in untimeout.",
888 itv.it_value.tv_sec));
889 if (setitimer(ITIMER_REAL, &itv, NULL)) {
890 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
893 if (gettimeofday(&schedtime, NULL)) {
894 syslog(LOG_ERR, "gettimeofday: %m");
902 * adjtimeout - Decrement the first timeout by the amount of time since
914 * Make sure that the clock hasn't been warped dramatically.
915 * Account for recently expired, but blocked timer by adding
916 * small fudge factor.
918 if (gettimeofday(&tv, NULL)) {
919 syslog(LOG_ERR, "gettimeofday: %m");
922 timediff = tv.tv_sec - schedtime.tv_sec;
924 timediff > callout->c_time + 1)
927 callout->c_time -= timediff; /* OK, Adjust time */
932 * hup - Catch SIGHUP signal.
934 * Indicates that the physical layer has been disconnected.
938 hup(sig, code, scp, addr)
940 struct sigcontext *scp;
943 syslog(LOG_INFO, "Hangup (SIGHUP)");
945 hungup = 1; /* they hung up on us! */
946 persist = 0; /* don't try to restart */
947 adjtimeout(); /* Adjust timeouts */
948 lcp_lowerdown(0); /* Reset connection */
949 quit(); /* and die */
954 * term - Catch SIGTERM signal.
956 * Indicates that we should initiate a graceful disconnect and exit.
960 term(sig, code, scp, addr)
962 struct sigcontext *scp;
965 syslog(LOG_INFO, "Terminating link.");
966 persist = 0; /* don't try to restart */
967 adjtimeout(); /* Adjust timeouts */
968 lcp_close(0); /* Close connection */
973 * intr - Catch SIGINT signal (DEL/^C).
975 * Indicates that we should initiate a graceful disconnect and exit.
979 intr(sig, code, scp, addr)
981 struct sigcontext *scp;
984 syslog(LOG_INFO, "Interrupt received: terminating link");
985 persist = 0; /* don't try to restart */
986 adjtimeout(); /* Adjust timeouts */
987 lcp_close(0); /* Close connection */
992 * alrm - Catch SIGALRM signal.
994 * Indicates a timeout.
998 alrm(sig, code, scp, addr)
1000 struct sigcontext *scp;
1003 struct itimerval itv;
1004 struct callout *freep, *list, *last;
1006 MAINDEBUG((LOG_DEBUG, "Alarm"));
1008 if (callout == NULL)
1011 * Get the first scheduled timeout and any that were scheduled
1012 * for the same time as a list, and remove them all from callout
1015 list = last = callout;
1016 while (last->c_next != NULL && last->c_next->c_time == 0)
1017 last = last->c_next;
1018 callout = last->c_next;
1019 last->c_next = NULL;
1022 * Set a new itimer if there are more timeouts scheduled.
1025 itv.it_interval.tv_sec = itv.it_interval.tv_usec = 0;
1026 itv.it_value.tv_usec = 0;
1027 itv.it_value.tv_sec = callout->c_time;
1028 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in alrm.",
1029 itv.it_value.tv_sec));
1030 if (setitimer(ITIMER_REAL, &itv, NULL)) {
1031 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
1034 if (gettimeofday(&schedtime, NULL)) {
1035 syslog(LOG_ERR, "gettimeofday: %m");
1041 * Now call all the timeout routines scheduled for this time.
1044 (*list->c_func)(list->c_arg);
1046 list = list->c_next;
1047 (void) free((char *) freep);
1054 * io - Catch SIGIO signal.
1056 * Indicates that incoming data is available.
1060 io(sig, code, scp, addr)
1062 struct sigcontext *scp;
1069 struct timeval notime;
1072 MAINDEBUG((LOG_DEBUG, "IO signal received"));
1073 adjtimeout(); /* Adjust timeouts */
1075 /* we do this to see if the SIGIO handler is being invoked for input */
1076 /* ready, or for the socket buffer hitting the low-water mark. */
1083 if ((ready = select(32, &fdset, (fd_set *) NULL, (fd_set *) NULL,
1085 syslog(LOG_ERR, "Error in io() select: %m");
1090 MAINDEBUG((LOG_DEBUG, "IO non-input ready SIGIO occured."));
1094 /* Yup, this is for real */
1095 for (;;) { /* Read all available packets */
1096 p = inpacket_buf; /* point to beginning of packet buffer */
1098 len = read_packet(inpacket_buf);
1103 syslog(LOG_WARNING, "End of file on fd!");
1107 if (debug /*&& (debugflags & DBG_INPACKET)*/)
1108 log_packet(p, len, "rcvd ");
1110 if (len < DLLHEADERLEN) {
1111 MAINDEBUG((LOG_INFO, "io(): Received short packet."));
1115 p += 2; /* Skip address and control */
1116 GETSHORT(protocol, p);
1117 len -= DLLHEADERLEN;
1120 * Toss all non-LCP packets unless LCP is OPEN.
1122 if (protocol != LCP && lcp_fsm[0].state != OPENED) {
1123 MAINDEBUG((LOG_INFO,
1124 "io(): Received non-LCP packet when LCP not open."));
1129 * Upcall the proper protocol input routine.
1131 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1132 if (prottbl[i].protocol == protocol) {
1133 (*prottbl[i].input)(0, p, len);
1137 if (i == sizeof (prottbl) / sizeof (struct protent)) {
1138 syslog(LOG_WARNING, "input: Unknown protocol (%x) received!",
1140 lcp_sprotrej(0, p - DLLHEADERLEN, len + DLLHEADERLEN);
1146 * demuxprotrej - Demultiplex a Protocol-Reject.
1149 demuxprotrej(unit, protocol)
1156 * Upcall the proper Protocol-Reject routine.
1158 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1159 if (prottbl[i].protocol == protocol) {
1160 (*prottbl[i].protrej)(unit);
1165 "demuxprotrej: Unrecognized Protocol-Reject for protocol %d!",
1171 * incdebug - Catch SIGUSR1 signal.
1173 * Increment debug flag.
1180 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
1181 setlogmask(LOG_UPTO(LOG_DEBUG));
1187 * nodebug - Catch SIGUSR2 signal.
1189 * Turn off debugging.
1196 setlogmask(LOG_UPTO(LOG_WARNING));
1202 * device_script - run a program to connect or disconnect the
1206 device_script(program, in, out)
1215 sigaddset(&mask, SIGINT);
1216 sigaddset(&mask, SIGHUP);
1217 sigprocmask(SIG_BLOCK, &mask, &mask);
1222 syslog(LOG_ERR, "fork: %m");
1227 setreuid(getuid(), getuid());
1228 setregid(getgid(), getgid());
1229 sigprocmask(SIG_SETMASK, &mask, NULL);
1232 execl("/bin/sh", "sh", "-c", program, (char *)0);
1233 syslog(LOG_ERR, "could not exec /bin/sh: %m");
1238 while (waitpid(pid, &status, 0) < 0) {
1241 syslog(LOG_ERR, "waiting for (dis)connection process: %m");
1244 sigprocmask(SIG_SETMASK, &mask, NULL);
1246 return (status == 0 ? 0 : -1);
1251 * run-program - execute a program with given arguments,
1252 * but don't wait for it.
1253 * If the program can't be executed, logs an error unless
1254 * must_exist is 0 and the program file doesn't exist.
1257 run_program(prog, args, must_exist)
1266 syslog(LOG_ERR, "can't fork to run %s: %m", prog);
1271 if (must_exist || errno != ENOENT)
1272 syslog(LOG_WARNING, "can't execute %s: %m", prog);
1275 MAINDEBUG((LOG_DEBUG, "Script %s started; pid = %d", prog, pid));
1282 * reap_kids - get status from any dead child processes,
1283 * and log a message for abnormal terminations.
1290 if (n_children == 0)
1292 if ((pid = waitpid(-1, &status, WNOHANG)) == -1) {
1293 if (errno != ECHILD)
1294 syslog(LOG_ERR, "waitpid: %m");
1299 if (WIFSIGNALED(status)) {
1300 syslog(LOG_WARNING, "child process %d terminated with signal %d",
1301 pid, WTERMSIG(status));
1308 * log_packet - format a packet and log it.
1311 char line[256]; /* line to be logged accumulated here */
1315 log_packet(p, len, prefix)
1320 strcpy(line, prefix);
1321 linep = line + strlen(line);
1322 format_packet(p, len, pr_log, NULL);
1324 syslog(LOG_DEBUG, "%s", line);
1328 * format_packet - make a readable representation of a packet,
1329 * calling `printer(arg, format, ...)' to output it.
1332 format_packet(p, len, printer, arg)
1335 void (*printer) __ARGS((void *, char *, ...));
1342 if (len >= DLLHEADERLEN && p[0] == ALLSTATIONS && p[1] == UI) {
1345 len -= DLLHEADERLEN;
1346 for (i = 0; i < N_PROTO; ++i)
1347 if (proto == prottbl[i].protocol)
1350 printer(arg, "[%s", prottbl[i].name);
1351 n = (*prottbl[i].printpkt)(p, len, printer, arg);
1356 printer(arg, "[proto=0x%x]", proto);
1360 for (; len > 0; --len) {
1362 printer(arg, " %.2x", x);
1370 pr_log(void *arg, char *fmt, ...)
1376 va_start(pvar, fmt);
1377 vsprintf(buf, fmt, pvar);
1381 if (linep + n + 1 > line + sizeof(line)) {
1382 syslog(LOG_DEBUG, "%s", line);
1389 #else /* __STDC__ */
1390 #include <varargs.h>
1393 pr_log(arg, fmt, va_alist)
1403 vsprintf(buf, fmt, pvar);
1407 if (linep + n + 1 > line + sizeof(line)) {
1408 syslog(LOG_DEBUG, "%s", line);
1417 * print_string - print a readable representation of a string using
1421 print_string(p, len, printer, arg)
1424 void (*printer) __ARGS((void *, char *, ...));
1430 for (; len > 0; --len) {
1432 if (' ' <= c && c <= '~')
1433 printer(arg, "%c", c);
1435 printer(arg, "\\%.3o", c);
1441 * novm - log an error message saying we ran out of memory, and die.
1447 syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg);