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.13 1994/05/27 01:02:14 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 (fcntl(fd, F_SETFL, FNDELAY | FASYNC) == -1) {
472 syslog(LOG_ERR, "fcntl(F_SETFL, FNDELAY | FASYNC): %m");
477 * Block all signals, start opening the connection, and wait for
478 * incoming signals (reply, timeout, etc.).
480 syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devname);
481 sigprocmask(SIG_BLOCK, &mask, NULL); /* Block signals now */
482 lcp_lowerup(0); /* XXX Well, sort of... */
483 lcp_open(0); /* Start protocol */
484 for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; )
485 sigpause(0); /* Wait for next signal */
488 * Run disconnector script, if requested
491 if (device_script(disconnector, fd, fd) < 0) {
492 syslog(LOG_WARNING, "disconnect script failed");
496 syslog(LOG_INFO, "Disconnected...");
504 * XXX assume speed_t values numerically equal bits per second
505 * (so we can ask for any speed).
507 #define translate_speed(bps) (bps)
508 #define baud_rate_of(speed) (speed)
512 * List of valid speeds.
515 int speed_int, speed_val;
587 * Translate from bits/second to a speed_t.
593 struct speed *speedp;
597 for (speedp = speeds; speedp->speed_int; speedp++)
598 if (bps == speedp->speed_int)
599 return speedp->speed_val;
600 syslog(LOG_WARNING, "speed %d not supported", bps);
605 * Translate from a speed_t to bits/second.
611 struct speed *speedp;
615 for (speedp = speeds; speedp->speed_int; speedp++)
616 if (speed == speedp->speed_val)
617 return speedp->speed_int;
623 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
624 * at the requested speed, etc. If `local' is true, set CLOCAL
625 * regardless of whether the modem option was specified.
627 set_up_tty(fd, local)
634 if (tcgetattr(fd, &tios) < 0) {
635 syslog(LOG_ERR, "tcgetattr: %m");
643 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
645 tios.c_cflag |= CRTSCTS;
647 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
650 tios.c_cflag |= CS8 | CREAD | HUPCL;
652 tios.c_cflag |= CLOCAL;
653 tios.c_iflag = IGNBRK | IGNPAR;
657 tios.c_cc[VTIME] = 0;
660 tios.c_iflag |= IXOFF;
661 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
662 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
665 speed = translate_speed(inspeed);
667 cfsetospeed(&tios, speed);
668 cfsetispeed(&tios, speed);
670 speed = cfgetospeed(&tios);
673 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
674 syslog(LOG_ERR, "tcsetattr: %m");
680 if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
681 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
682 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
683 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
688 struct sgttyb sgttyb;
691 * Put the tty in raw mode.
693 if (ioctl(fd, TIOCGETP, &sgttyb) < 0) {
694 syslog(LOG_ERR, "ioctl(TIOCGETP): %m");
701 sgttyb.sg_flags = RAW | ANYP;
702 speed = translate_speed(inspeed);
704 sgttyb.sg_ispeed = speed;
706 speed = sgttyb.sg_ispeed;
708 if (ioctl(fd, TIOCSETP, &sgttyb) < 0) {
709 syslog(LOG_ERR, "ioctl(TIOCSETP): %m");
714 baud_rate = baud_rate_of(speed);
719 * setdtr - control the DTR line on the serial port.
720 * This is called from die(), so it shouldn't call die().
725 int modembits = TIOCM_DTR;
727 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
732 * quit - Clean up state and exit.
741 * die - like quit, except we can specify an exit status.
748 syslog(LOG_INFO, "Exit.");
753 * cleanup - restore anything which needs to be restored before we exit
762 /* drop dtr to hang up */
766 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
767 syslog(LOG_WARNING, "fcntl(F_SETFL, fdflags): %m");
774 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
775 syslog(LOG_WARNING, "tcsetattr: %m");
777 if (ioctl(fd, TIOCSETP, &initsgttyb) < 0)
778 syslog(LOG_WARNING, "ioctl(TIOCSETP): %m");
786 if (pidfilename[0] != 0 && unlink(pidfilename) < 0)
787 syslog(LOG_WARNING, "unable to unlink pid file: %m");
790 if (lockflag && !default_device)
795 static struct callout *callout = NULL; /* Callout list */
796 static struct timeval schedtime; /* Time last timeout was set */
799 * timeout - Schedule a timeout.
801 * Note that this timeout takes the number of seconds, NOT hz (as in
805 timeout(func, arg, time)
810 struct itimerval itv;
811 struct callout *newp, **oldpp;
813 MAINDEBUG((LOG_DEBUG, "Timeout %x:%x in %d seconds.",
814 (int) func, (int) arg, time));
819 if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) {
820 syslog(LOG_ERR, "Out of memory in timeout()!");
827 * Find correct place to link it in and decrement its time by the
828 * amount of time used by preceding timeouts.
830 for (oldpp = &callout;
831 *oldpp && (*oldpp)->c_time <= time;
832 oldpp = &(*oldpp)->c_next)
833 time -= (*oldpp)->c_time;
835 newp->c_next = *oldpp;
837 (*oldpp)->c_time -= time;
841 * If this is now the first callout then we have to set a new
844 if (callout == newp) {
845 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
846 itv.it_value.tv_usec = 0;
847 itv.it_value.tv_sec = callout->c_time;
848 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in timeout.",
849 itv.it_value.tv_sec));
850 if (setitimer(ITIMER_REAL, &itv, NULL)) {
851 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
854 if (gettimeofday(&schedtime, NULL)) {
855 syslog(LOG_ERR, "gettimeofday: %m");
863 * untimeout - Unschedule a timeout.
870 struct itimerval itv;
871 struct callout **copp, *freep;
874 MAINDEBUG((LOG_DEBUG, "Untimeout %x:%x.", (int) func, (int) arg));
877 * If the first callout is unscheduled then we have to set a new
881 callout->c_func == func &&
882 callout->c_arg == arg)
886 * Find first matching timeout. Add its time to the next timeouts
889 for (copp = &callout; *copp; copp = &(*copp)->c_next)
890 if ((*copp)->c_func == func &&
891 (*copp)->c_arg == arg) {
893 *copp = freep->c_next;
895 (*copp)->c_time += freep->c_time;
896 (void) free((char *) freep);
901 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
902 itv.it_value.tv_usec = 0;
903 itv.it_value.tv_sec = callout ? callout->c_time : 0;
904 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in untimeout.",
905 itv.it_value.tv_sec));
906 if (setitimer(ITIMER_REAL, &itv, NULL)) {
907 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
910 if (gettimeofday(&schedtime, NULL)) {
911 syslog(LOG_ERR, "gettimeofday: %m");
919 * adjtimeout - Decrement the first timeout by the amount of time since
931 * Make sure that the clock hasn't been warped dramatically.
932 * Account for recently expired, but blocked timer by adding
933 * small fudge factor.
935 if (gettimeofday(&tv, NULL)) {
936 syslog(LOG_ERR, "gettimeofday: %m");
939 timediff = tv.tv_sec - schedtime.tv_sec;
941 timediff > callout->c_time + 1)
944 callout->c_time -= timediff; /* OK, Adjust time */
949 * hup - Catch SIGHUP signal.
951 * Indicates that the physical layer has been disconnected.
957 syslog(LOG_INFO, "Hangup (SIGHUP)");
959 hungup = 1; /* they hung up on us! */
960 persist = 0; /* don't try to restart */
961 adjtimeout(); /* Adjust timeouts */
962 lcp_lowerdown(0); /* Reset connection */
963 quit(); /* and die */
968 * term - Catch SIGTERM signal.
970 * Indicates that we should initiate a graceful disconnect and exit.
976 syslog(LOG_INFO, "Terminating link.");
977 persist = 0; /* don't try to restart */
978 adjtimeout(); /* Adjust timeouts */
979 lcp_close(0); /* Close connection */
984 * intr - Catch SIGINT signal (DEL/^C).
986 * Indicates that we should initiate a graceful disconnect and exit.
992 syslog(LOG_INFO, "Interrupt received: terminating link");
993 persist = 0; /* don't try to restart */
994 adjtimeout(); /* Adjust timeouts */
995 lcp_close(0); /* Close connection */
1000 * alrm - Catch SIGALRM signal.
1002 * Indicates a timeout.
1008 struct itimerval itv;
1009 struct callout *freep, *list, *last;
1011 MAINDEBUG((LOG_DEBUG, "Alarm"));
1013 if (callout == NULL)
1016 * Get the first scheduled timeout and any that were scheduled
1017 * for the same time as a list, and remove them all from callout
1020 list = last = callout;
1021 while (last->c_next != NULL && last->c_next->c_time == 0)
1022 last = last->c_next;
1023 callout = last->c_next;
1024 last->c_next = NULL;
1027 * Set a new itimer if there are more timeouts scheduled.
1030 itv.it_interval.tv_sec = itv.it_interval.tv_usec = 0;
1031 itv.it_value.tv_usec = 0;
1032 itv.it_value.tv_sec = callout->c_time;
1033 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in alrm.",
1034 itv.it_value.tv_sec));
1035 if (setitimer(ITIMER_REAL, &itv, NULL)) {
1036 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
1039 if (gettimeofday(&schedtime, NULL)) {
1040 syslog(LOG_ERR, "gettimeofday: %m");
1046 * Now call all the timeout routines scheduled for this time.
1049 (*list->c_func)(list->c_arg);
1051 list = list->c_next;
1052 (void) free((char *) freep);
1059 * chld - Catch SIGCHLD signal.
1060 * Calls reap_kids to get status for any dead kids.
1071 * io - Catch SIGIO signal.
1073 * Indicates that incoming data is available.
1083 struct timeval notime;
1086 MAINDEBUG((LOG_DEBUG, "IO signal received"));
1087 adjtimeout(); /* Adjust timeouts */
1089 /* Yup, this is for real */
1090 for (;;) { /* Read all available packets */
1091 p = inpacket_buf; /* point to beginning of packet buffer */
1093 len = read_packet(inpacket_buf);
1098 MAINDEBUG((LOG_DEBUG, "End of file on fd!"));
1103 if (debug /*&& (debugflags & DBG_INPACKET)*/)
1104 log_packet(p, len, "rcvd ");
1106 if (len < DLLHEADERLEN) {
1107 MAINDEBUG((LOG_INFO, "io(): Received short packet."));
1111 p += 2; /* Skip address and control */
1112 GETSHORT(protocol, p);
1113 len -= DLLHEADERLEN;
1116 * Toss all non-LCP packets unless LCP is OPEN.
1118 if (protocol != LCP && lcp_fsm[0].state != OPENED) {
1119 MAINDEBUG((LOG_INFO,
1120 "io(): Received non-LCP packet when LCP not open."));
1125 * Upcall the proper protocol input routine.
1127 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1128 if (prottbl[i].protocol == protocol) {
1129 (*prottbl[i].input)(0, p, len);
1133 if (i == sizeof (prottbl) / sizeof (struct protent)) {
1134 syslog(LOG_WARNING, "input: Unknown protocol (%x) received!",
1136 lcp_sprotrej(0, p - DLLHEADERLEN, len + DLLHEADERLEN);
1142 * demuxprotrej - Demultiplex a Protocol-Reject.
1145 demuxprotrej(unit, protocol)
1152 * Upcall the proper Protocol-Reject routine.
1154 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1155 if (prottbl[i].protocol == protocol) {
1156 (*prottbl[i].protrej)(unit);
1161 "demuxprotrej: Unrecognized Protocol-Reject for protocol %d!",
1167 * incdebug - Catch SIGUSR1 signal.
1169 * Increment debug flag.
1176 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
1177 setlogmask(LOG_UPTO(LOG_DEBUG));
1183 * nodebug - Catch SIGUSR2 signal.
1185 * Turn off debugging.
1192 setlogmask(LOG_UPTO(LOG_WARNING));
1198 * device_script - run a program to connect or disconnect the
1202 device_script(program, in, out)
1211 sigaddset(&mask, SIGINT);
1212 sigaddset(&mask, SIGHUP);
1213 sigprocmask(SIG_BLOCK, &mask, &mask);
1218 syslog(LOG_ERR, "fork: %m");
1223 setreuid(getuid(), getuid());
1224 setregid(getgid(), getgid());
1225 sigprocmask(SIG_SETMASK, &mask, NULL);
1228 execl("/bin/sh", "sh", "-c", program, (char *)0);
1229 syslog(LOG_ERR, "could not exec /bin/sh: %m");
1234 while (waitpid(pid, &status, 0) < 0) {
1237 syslog(LOG_ERR, "waiting for (dis)connection process: %m");
1240 sigprocmask(SIG_SETMASK, &mask, NULL);
1242 return (status == 0 ? 0 : -1);
1247 * run-program - execute a program with given arguments,
1248 * but don't wait for it.
1249 * If the program can't be executed, logs an error unless
1250 * must_exist is 0 and the program file doesn't exist.
1253 run_program(prog, args, must_exist)
1262 syslog(LOG_ERR, "can't fork to run %s: %m", prog);
1267 if (must_exist || errno != ENOENT)
1268 syslog(LOG_WARNING, "can't execute %s: %m", prog);
1271 MAINDEBUG((LOG_DEBUG, "Script %s started; pid = %d", prog, pid));
1278 * reap_kids - get status from any dead child processes,
1279 * and log a message for abnormal terminations.
1286 if (n_children == 0)
1288 if ((pid = waitpid(-1, &status, WNOHANG)) == -1) {
1289 if (errno != ECHILD)
1290 syslog(LOG_ERR, "waitpid: %m");
1295 if (WIFSIGNALED(status)) {
1296 syslog(LOG_WARNING, "child process %d terminated with signal %d",
1297 pid, WTERMSIG(status));
1304 * log_packet - format a packet and log it.
1307 char line[256]; /* line to be logged accumulated here */
1311 log_packet(p, len, prefix)
1316 strcpy(line, prefix);
1317 linep = line + strlen(line);
1318 format_packet(p, len, pr_log, NULL);
1320 syslog(LOG_DEBUG, "%s", line);
1324 * format_packet - make a readable representation of a packet,
1325 * calling `printer(arg, format, ...)' to output it.
1328 format_packet(p, len, printer, arg)
1331 void (*printer) __ARGS((void *, char *, ...));
1338 if (len >= DLLHEADERLEN && p[0] == ALLSTATIONS && p[1] == UI) {
1341 len -= DLLHEADERLEN;
1342 for (i = 0; i < N_PROTO; ++i)
1343 if (proto == prottbl[i].protocol)
1346 printer(arg, "[%s", prottbl[i].name);
1347 n = (*prottbl[i].printpkt)(p, len, printer, arg);
1352 printer(arg, "[proto=0x%x]", proto);
1356 for (; len > 0; --len) {
1358 printer(arg, " %.2x", x);
1366 pr_log(void *arg, char *fmt, ...)
1372 va_start(pvar, fmt);
1373 vsprintf(buf, fmt, pvar);
1377 if (linep + n + 1 > line + sizeof(line)) {
1378 syslog(LOG_DEBUG, "%s", line);
1385 #else /* __STDC__ */
1386 #include <varargs.h>
1389 pr_log(arg, fmt, va_alist)
1399 vsprintf(buf, fmt, pvar);
1403 if (linep + n + 1 > line + sizeof(line)) {
1404 syslog(LOG_DEBUG, "%s", line);
1413 * print_string - print a readable representation of a string using
1417 print_string(p, len, printer, arg)
1420 void (*printer) __ARGS((void *, char *, ...));
1426 for (; len > 0; --len) {
1428 if (' ' <= c && c <= '~')
1429 printer(arg, "%c", c);
1431 printer(arg, "\\%.3o", c);
1437 * novm - log an error message saying we ran out of memory, and die.
1443 syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg);