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.6 1994/04/11 07:15:13 paulus Exp $";
35 * If REQ_SYSOPTIONS is defined to 1, pppd will not run unless
36 * /etc/ppp/options exists.
38 #ifndef REQ_SYSOPTIONS
39 #define REQ_SYSOPTIONS 0
50 #include <sys/ioctl.h>
55 #include <sys/param.h>
56 #include <sys/types.h>
58 #include <sys/socket.h>
64 #include <net/if_ppp.h>
81 #include "pathnames.h"
82 #include "patchlevel.h"
94 static char *pidpath = PIDPATH; /* filename in which pid will be stored */
96 static char *pidpath = _PATH_PIDFILE;
100 char ifname[IFNAMSIZ]; /* Interface name */
101 int ifunit; /* Interface unit number */
103 char *progname; /* Name of this program */
104 char hostname[MAXNAMELEN]; /* Our hostname */
105 char our_name[MAXNAMELEN];
106 char remote_name[MAXNAMELEN];
108 static pid_t pid; /* Our pid */
109 static pid_t pgrpid; /* Process Group ID */
110 static char pidfilename[MAXPATHLEN];
112 char devname[MAXPATHLEN] = "/dev/tty"; /* Device name */
113 int default_device = TRUE; /* use default device (stdin/out) */
115 int fd; /* Device file descriptor */
116 int s; /* Socket file descriptor */
119 static struct sgttyb initsgttyb; /* Initial TTY sgttyb */
121 static struct termios inittermios; /* Initial TTY termios */
124 static int initfdflags = -1; /* Initial file descriptor flags */
126 static int restore_term; /* 1 => we've munged the terminal */
128 u_char outpacket_buf[MTU+DLLHEADERLEN]; /* buffer for outgoing packet */
129 static u_char inpacket_buf[MTU+DLLHEADERLEN]; /* buffer for incoming packet */
131 int hungup; /* terminal has been hung up */
133 /* configured variables */
135 int debug = 0; /* Debug flag */
136 char user[MAXNAMELEN]; /* username for PAP */
137 char passwd[MAXSECRETLEN]; /* password for PAP */
138 char *connector = NULL; /* "connect" 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 */
154 static void hup __ARGS((int, int, struct sigcontext *, char *));
155 static void intr __ARGS((int, int, struct sigcontext *, char *));
156 static void term __ARGS((int, int, struct sigcontext *, char *));
157 static void alrm __ARGS((int, int, struct sigcontext *, char *));
158 static void io __ARGS((int, int, struct sigcontext *, char *));
159 static void incdebug __ARGS((int));
160 static void nodebug __ARGS((int));
161 void establish_ppp __ARGS((void));
163 void cleanup __ARGS((int, caddr_t));
164 void die __ARGS((int));
165 void novm __ARGS((char *));
167 void log_packet __ARGS((u_char *, int, char *));
168 void format_packet __ARGS((u_char *, int,
169 void (*) (void *, char *, ...), void *));
170 void pr_log __ARGS((void *, char *, ...));
173 extern char *ttyname __ARGS((int));
175 extern char *getlogin __ARGS((void));
178 * PPP Data Link Layer "protocol" table.
179 * One entry per supported protocol.
181 static struct protent {
189 { LCP, lcp_init, lcp_input, lcp_protrej, lcp_printpkt, "LCP" },
190 { IPCP, ipcp_init, ipcp_input, ipcp_protrej, ipcp_printpkt, "IPCP" },
191 { UPAP, upap_init, upap_input, upap_protrej, upap_printpkt, "PAP" },
192 { CHAP, ChapInit, ChapInput, ChapProtocolReject, ChapPrintPkt, "CHAP" },
195 #define N_PROTO (sizeof(prottbl) / sizeof(prottbl[0]))
208 * Initialize syslog system and magic number package.
210 #if BSD >= 43 || defined(sun)
211 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
212 setlogmask(LOG_UPTO(LOG_INFO));
214 openlog("pppd", LOG_PID);
215 #define LOG_UPTO(x) (x)
216 #define setlogmask(x) (x)
220 p = ttyname(fileno(stdin));
227 if (gethostname(hostname, MAXNAMELEN) < 0 ) {
228 syslog(LOG_ERR, "couldn't get hostname: %m");
231 hostname[MAXNAMELEN-1] = 0;
235 if (!ppp_available()) {
236 fprintf(stderr, "Sorry - PPP is not available on this system\n");
241 * Initialize to the standard option set, then parse, in order,
242 * the system options file, the user's options file, and the command
245 for (i = 0; i < N_PROTO; i++)
246 (*prottbl[i].init)(0);
250 if (!options_from_file(_PATH_SYSOPTIONS, REQ_SYSOPTIONS) ||
251 !options_from_user() ||
252 !parse_args(argc-1, argv+1))
254 check_auth_options();
260 syslog(LOG_NOTICE, "pppd %s.%d started by %s, uid %d",
261 VERSION, PATCHLEVEL, p, getuid());
265 * Make sure we can set the serial device to be our controlling terminal.
267 if (default_device) {
269 * No device name was specified:
270 * we are in the device's session already.
272 if ((pgrpid = getpgrp(0)) < 0) {
273 syslog(LOG_ERR, "getpgrp(0): %m");
279 * Not default device: make sure we're not a process group leader,
280 * then become session leader of a new session (so we can make
281 * our device its controlling terminal and thus get SIGHUPs).
284 /* fork so we're not a process group leader */
286 exit(0); /* parent is finished */
289 syslog(LOG_ERR, "fork: %m");
292 pid = getpid(); /* otherwise pid is 0 in child */
295 * try to put ourself into our parent's process group,
296 * so we're not a process group leader
298 if (setpgrp(pid, getppid()) < 0)
299 syslog(LOG_WARNING, "setpgrp: %m");
302 /* create new session */
303 if ((pgrpid = setsid()) < 0) {
304 syslog(LOG_ERR, "setsid(): %m");
310 /* Get an internet socket for doing socket ioctl's on. */
311 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
312 syslog(LOG_ERR, "socket : %m");
317 * Compute mask of all interesting signals and install signal handlers
318 * for each. Only one signal handler may be active at a time. Therefore,
319 * all other signals should be masked when any handler is executing.
322 sigaddset(&mask, SIGHUP);
323 sigaddset(&mask, SIGINT);
324 sigaddset(&mask, SIGALRM);
325 sigaddset(&mask, SIGIO);
327 sigaddset(&mask, SIGPOLL);
330 #define SIGNAL(s, handler) { \
331 sv.sv_handler = handler; \
332 if (sigvec(s, &sv, NULL) < 0) { \
333 syslog(LOG_ERR, "sigvec(%d): %m", s); \
340 SIGNAL(SIGHUP, hup); /* Hangup */
341 SIGNAL(SIGINT, intr); /* Interrupt */
342 SIGNAL(SIGTERM, term); /* Terminate */
343 SIGNAL(SIGALRM, alrm); /* Timeout */
344 SIGNAL(SIGIO, io); /* Input available */
346 SIGNAL(SIGPOLL, io); /* Input available */
349 signal(SIGUSR1, incdebug); /* Increment debug flag */
350 signal(SIGUSR2, nodebug); /* Reset debug flag */
353 * Block SIGIOs and SIGPOLLs for now
356 sigaddset(&mask, SIGIO);
358 sigaddset(&mask, SIGPOLL);
360 sigprocmask(SIG_BLOCK, &mask, NULL);
363 * Open the serial device and set it up to be the ppp interface.
365 if ((fd = open(devname, O_RDWR /*| O_NDELAY*/)) < 0) {
366 syslog(LOG_ERR, "open(%s): %m", devname);
371 /* set device to be controlling tty */
372 if (!default_device && ioctl(fd, TIOCSCTTY) < 0) {
373 syslog(LOG_ERR, "ioctl(TIOCSCTTY): %m");
377 /* set line speed, flow control, etc. */
380 /* run connection script */
382 syslog(LOG_INFO, "Connecting with <%s>", connector);
384 /* drop dtr to hang up in case modem is off hook */
385 if (!default_device && modem) {
391 if (set_up_connection(connector, fd, fd) < 0) {
392 syslog(LOG_ERR, "could not set up connection");
397 syslog(LOG_INFO, "Connected...");
398 sleep(1); /* give it time to set up its terminal */
401 /* set up the serial device as a ppp interface */
404 syslog(LOG_INFO, "Using interface ppp%d", ifunit);
405 (void) sprintf(ifname, "ppp%d", ifunit);
407 /* write pid to file */
408 (void) sprintf(pidfilename, "%s/%s.pid", pidpath, ifname);
409 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
410 fprintf(pidfile, "%d\n", pid);
411 (void) fclose(pidfile);
413 syslog(LOG_ERR, "unable to create pid file: %m");
418 * Set process group of device to our process group so we can get
419 * SIGIOs and SIGHUPs.
422 if (default_device) {
423 int id = tcgetpgrp(fd);
425 syslog(LOG_WARNING, "warning: not in tty's process group");
428 if (tcsetpgrp(fd, pgrpid) < 0) {
429 syslog(LOG_ERR, "tcsetpgrp(): %m");
434 /* set process group on tty so we get SIGIO's */
435 if (ioctl(fd, TIOCSPGRP, &pgrpid) < 0) {
436 syslog(LOG_ERR, "ioctl(TIOCSPGRP): %m");
442 * Record initial device flags, then set device to cause SIGIO
443 * signals to be generated.
445 if ((initfdflags = fcntl(fd, F_GETFL)) == -1) {
446 syslog(LOG_ERR, "fcntl(F_GETFL): %m");
449 if (fcntl(fd, F_SETFL, FNDELAY | FASYNC) == -1) {
450 syslog(LOG_ERR, "fcntl(F_SETFL, FNDELAY | FASYNC): %m");
455 * Block all signals, start opening the connection, and wait for
456 * incoming signals (reply, timeout, etc.).
458 syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devname);
459 sigprocmask(SIG_BLOCK, &mask, NULL); /* Block signals now */
460 lcp_lowerup(0); /* XXX Well, sort of... */
461 lcp_open(0); /* Start protocol */
463 sigpause(0); /* Wait for next signal */
464 reap_kids(); /* Don't leave dead kids lying around */
470 * XXX assume speed_t values numerically equal bits per second
471 * (so we can ask for any speed).
473 #define translate_speed(bps) (bps)
474 #define baud_rate_of(speed) (speed)
478 * List of valid speeds.
481 int speed_int, speed_val;
553 * Translate from bits/second to a speed_t.
559 struct speed *speedp;
563 for (speedp = speeds; speedp->speed_int; speedp++)
564 if (bps == speedp->speed_int)
565 return speedp->speed_val;
566 syslog(LOG_WARNING, "speed %d not supported", bps);
571 * Translate from a speed_t to bits/second.
577 struct speed *speedp;
581 for (speedp = speeds; speedp->speed_int; speedp++)
582 if (speed == speedp->speed_val)
583 return speedp->speed_int;
589 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
590 * at the requested speed, etc.
599 if (tcgetattr(fd, &tios) < 0) {
600 syslog(LOG_ERR, "tcgetattr: %m");
607 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
608 tios.c_cflag |= CS8 | CREAD | HUPCL;
610 tios.c_cflag |= CRTSCTS;
612 tios.c_cflag |= CLOCAL;
613 tios.c_iflag = IGNBRK | IGNPAR;
617 tios.c_cc[VTIME] = 0;
618 speed = translate_speed(inspeed);
620 cfsetospeed(&tios, speed);
621 cfsetispeed(&tios, speed);
623 speed = cfgetospeed(&tios);
626 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
627 syslog(LOG_ERR, "tcsetattr: %m");
633 struct sgttyb sgttyb;
636 * Put the tty in raw mode.
638 if (ioctl(fd, TIOCGETP, &sgttyb) < 0) {
639 syslog(LOG_ERR, "ioctl(TIOCGETP): %m");
646 sgttyb.sg_flags = RAW | ANYP;
647 speed = translate_speed(inspeed);
649 sgttyb.sg_ispeed = speed;
651 speed = sgttyb.sg_ispeed;
653 if (ioctl(fd, TIOCSETP, &sgttyb) < 0) {
654 syslog(LOG_ERR, "ioctl(TIOCSETP): %m");
659 baud_rate = baud_rate_of(speed);
665 * quit - Clean up state and exit.
674 * die - like quit, except we can specify an exit status.
681 syslog(LOG_INFO, "Exit.");
686 * cleanup - restore anything which needs to be restored before we exit
695 /* drop dtr to hang up */
699 if (fcntl(fd, F_SETFL, initfdflags) < 0)
700 syslog(LOG_ERR, "fcntl(F_SETFL, fdflags): %m");
706 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
707 syslog(LOG_ERR, "tcsetattr: %m");
709 if (ioctl(fd, TIOCSETP, &initsgttyb) < 0)
710 syslog(LOG_ERR, "ioctl(TIOCSETP): %m");
718 if (pidfilename[0] != 0 && unlink(pidfilename) < 0)
719 syslog(LOG_WARNING, "unable to unlink pid file: %m");
724 static struct callout *callout = NULL; /* Callout list */
725 static struct timeval schedtime; /* Time last timeout was set */
728 * timeout - Schedule a timeout.
730 * Note that this timeout takes the number of seconds, NOT hz (as in
734 timeout(func, arg, time)
739 struct itimerval itv;
740 struct callout *newp, **oldpp;
742 MAINDEBUG((LOG_DEBUG, "Timeout %x:%x in %d seconds.",
743 (int) func, (int) arg, time));
748 if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) {
749 syslog(LOG_ERR, "Out of memory in timeout()!");
756 * Find correct place to link it in and decrement its time by the
757 * amount of time used by preceding timeouts.
759 for (oldpp = &callout;
760 *oldpp && (*oldpp)->c_time <= time;
761 oldpp = &(*oldpp)->c_next)
762 time -= (*oldpp)->c_time;
764 newp->c_next = *oldpp;
766 (*oldpp)->c_time -= time;
770 * If this is now the first callout then we have to set a new
773 if (callout == newp) {
774 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
775 itv.it_value.tv_usec = 0;
776 itv.it_value.tv_sec = callout->c_time;
777 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds.",
778 itv.it_value.tv_sec));
779 if (setitimer(ITIMER_REAL, &itv, NULL)) {
780 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
783 if (gettimeofday(&schedtime, NULL)) {
784 syslog(LOG_ERR, "gettimeofday: %m");
792 * untimeout - Unschedule a timeout.
799 struct itimerval itv;
800 struct callout **copp, *freep;
803 MAINDEBUG((LOG_DEBUG, "Untimeout %x:%x.", (int) func, (int) arg));
806 * If the first callout is unscheduled then we have to set a new
810 callout->c_func == func &&
811 callout->c_arg == arg)
815 * Find first matching timeout. Add its time to the next timeouts
818 for (copp = &callout; *copp; copp = &(*copp)->c_next)
819 if ((*copp)->c_func == func &&
820 (*copp)->c_arg == arg) {
822 *copp = freep->c_next;
824 (*copp)->c_time += freep->c_time;
825 (void) free((char *) freep);
830 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
831 itv.it_value.tv_usec = 0;
832 itv.it_value.tv_sec = callout ? callout->c_time : 0;
833 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds.",
834 itv.it_value.tv_sec));
835 if (setitimer(ITIMER_REAL, &itv, NULL)) {
836 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
839 if (gettimeofday(&schedtime, NULL)) {
840 syslog(LOG_ERR, "gettimeofday: %m");
848 * adjtimeout - Decrement the first timeout by the amount of time since
860 * Make sure that the clock hasn't been warped dramatically.
861 * Account for recently expired, but blocked timer by adding
862 * small fudge factor.
864 if (gettimeofday(&tv, NULL)) {
865 syslog(LOG_ERR, "gettimeofday: %m");
868 timediff = tv.tv_sec - schedtime.tv_sec;
870 timediff > callout->c_time + 1)
873 callout->c_time -= timediff; /* OK, Adjust time */
878 * hup - Catch SIGHUP signal.
880 * Indicates that the physical layer has been disconnected.
884 hup(sig, code, scp, addr)
886 struct sigcontext *scp;
889 syslog(LOG_INFO, "Hangup (SIGHUP)");
891 hungup = 1; /* they hung up on us! */
892 persist = 0; /* don't try to restart */
893 adjtimeout(); /* Adjust timeouts */
894 lcp_lowerdown(0); /* Reset connection */
895 quit(); /* and die */
900 * term - Catch SIGTERM signal.
902 * Indicates that we should initiate a graceful disconnect and exit.
906 term(sig, code, scp, addr)
908 struct sigcontext *scp;
911 syslog(LOG_INFO, "Terminating link.");
912 persist = 0; /* don't try to restart */
913 adjtimeout(); /* Adjust timeouts */
914 lcp_close(0); /* Close connection */
919 * intr - Catch SIGINT signal (DEL/^C).
921 * Indicates that we should initiate a graceful disconnect and exit.
925 intr(sig, code, scp, addr)
927 struct sigcontext *scp;
930 syslog(LOG_INFO, "Interrupt received: terminating link");
931 persist = 0; /* don't try to restart */
932 adjtimeout(); /* Adjust timeouts */
933 lcp_close(0); /* Close connection */
938 * alrm - Catch SIGALRM signal.
940 * Indicates a timeout.
944 alrm(sig, code, scp, addr)
946 struct sigcontext *scp;
949 struct itimerval itv;
950 struct callout *freep;
952 MAINDEBUG((LOG_DEBUG, "Alarm"));
955 * Call and free first scheduled timeout and any that were scheduled
959 freep = callout; /* Remove entry before calling */
960 callout = freep->c_next;
961 (*freep->c_func)(freep->c_arg);
962 (void) free((char *) freep);
963 if (callout && callout->c_time)
968 * Set a new itimer if there are more timeouts scheduled.
971 itv.it_interval.tv_sec = itv.it_interval.tv_usec = 0;
972 itv.it_value.tv_usec = 0;
973 itv.it_value.tv_sec = callout->c_time;
974 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds.",
975 itv.it_value.tv_sec));
976 if (setitimer(ITIMER_REAL, &itv, NULL)) {
977 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
980 if (gettimeofday(&schedtime, NULL)) {
981 syslog(LOG_ERR, "gettimeofday: %m");
989 * io - Catch SIGIO signal.
991 * Indicates that incoming data is available.
995 io(sig, code, scp, addr)
997 struct sigcontext *scp;
1004 struct timeval notime;
1007 MAINDEBUG((LOG_DEBUG, "IO signal received"));
1008 adjtimeout(); /* Adjust timeouts */
1010 /* we do this to see if the SIGIO handler is being invoked for input */
1011 /* ready, or for the socket buffer hitting the low-water mark. */
1018 if ((ready = select(32, &fdset, (fd_set *) NULL, (fd_set *) NULL,
1020 syslog(LOG_ERR, "Error in io() select: %m");
1025 MAINDEBUG((LOG_DEBUG, "IO non-input ready SIGIO occured."));
1029 /* Yup, this is for real */
1030 for (;;) { /* Read all available packets */
1031 p = inpacket_buf; /* point to beginning of packet buffer */
1033 len = read_packet(inpacket_buf);
1038 syslog(LOG_ERR, "End of file on fd!");
1042 if (debug /*&& (debugflags & DBG_INPACKET)*/)
1043 log_packet(p, len, "rcvd ");
1045 if (len < DLLHEADERLEN) {
1046 MAINDEBUG((LOG_INFO, "io(): Received short packet."));
1050 p += 2; /* Skip address and control */
1051 GETSHORT(protocol, p);
1052 len -= DLLHEADERLEN;
1055 * Toss all non-LCP packets unless LCP is OPEN.
1057 if (protocol != LCP && lcp_fsm[0].state != OPENED) {
1058 MAINDEBUG((LOG_INFO,
1059 "io(): Received non-LCP packet when LCP not open."));
1064 * Upcall the proper protocol input routine.
1066 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1067 if (prottbl[i].protocol == protocol) {
1068 (*prottbl[i].input)(0, p, len);
1072 if (i == sizeof (prottbl) / sizeof (struct protent)) {
1073 syslog(LOG_WARNING, "input: Unknown protocol (%x) received!",
1075 lcp_sprotrej(0, p - DLLHEADERLEN, len + DLLHEADERLEN);
1081 * demuxprotrej - Demultiplex a Protocol-Reject.
1084 demuxprotrej(unit, protocol)
1091 * Upcall the proper Protocol-Reject routine.
1093 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1094 if (prottbl[i].protocol == protocol) {
1095 (*prottbl[i].protrej)(unit);
1100 "demuxprotrej: Unrecognized Protocol-Reject for protocol %d!",
1106 * incdebug - Catch SIGUSR1 signal.
1108 * Increment debug flag.
1115 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
1116 setlogmask(LOG_UPTO(LOG_DEBUG));
1122 * nodebug - Catch SIGUSR2 signal.
1124 * Turn off debugging.
1131 setlogmask(LOG_UPTO(LOG_WARNING));
1137 * set_up_connection - run a program to initialize the serial connector
1140 set_up_connection(program, in, out)
1149 sigaddset(&mask, SIGINT);
1150 sigaddset(&mask, SIGHUP);
1151 sigprocmask(SIG_BLOCK, &mask, &mask);
1156 syslog(LOG_ERR, "fork: %m");
1161 setreuid(getuid(), getuid());
1162 setregid(getgid(), getgid());
1163 sigprocmask(SIG_SETMASK, &mask, NULL);
1166 execl("/bin/sh", "sh", "-c", program, (char *)0);
1167 syslog(LOG_ERR, "could not exec /bin/sh: %m");
1172 while (waitpid(pid, &status, 0) != pid) {
1175 syslog(LOG_ERR, "waiting for connection process: %m");
1178 sigprocmask(SIG_SETMASK, &mask, NULL);
1180 return (status == 0 ? 0 : -1);
1185 * Return user specified netmask. A value of zero means no netmask has
1197 * setdtr - control the DTR line on the serial port.
1198 * This is called from die(), so it shouldn't call die().
1203 int modembits = TIOCM_DTR;
1205 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
1209 * log_packet - format a packet and log it.
1212 char line[256]; /* line to be logged accumulated here */
1216 log_packet(p, len, prefix)
1221 strcpy(line, prefix);
1222 linep = line + strlen(line);
1223 format_packet(p, len, pr_log, NULL);
1225 syslog(LOG_DEBUG, "%s", line);
1229 * format_packet - make a readable representation of a packet,
1230 * calling `printer(arg, format, ...)' to output it.
1233 format_packet(p, len, printer, arg)
1236 void (*printer) __ARGS((void *, char *, ...));
1243 if (len >= DLLHEADERLEN && p[0] == ALLSTATIONS && p[1] == UI) {
1246 len -= DLLHEADERLEN;
1247 for (i = 0; i < N_PROTO; ++i)
1248 if (proto == prottbl[i].protocol)
1251 printer(arg, "[%s", prottbl[i].name);
1252 n = (*prottbl[i].printpkt)(p, len, printer, arg);
1257 printer(arg, "[proto=0x%x]", proto);
1261 for (; len > 0; --len) {
1263 printer(arg, " %.2x", x);
1271 pr_log(void *arg, char *fmt, ...)
1277 va_start(pvar, fmt);
1278 vsprintf(buf, fmt, pvar);
1282 if (linep + n + 1 > line + sizeof(line)) {
1283 syslog(LOG_DEBUG, "%s", line);
1290 #else /* __STDC__ */
1291 #include <varargs.h>
1294 pr_log(arg, fmt, va_alist)
1304 vsprintf(buf, fmt, pvar);
1308 if (linep + n + 1 > line + sizeof(line)) {
1309 syslog(LOG_DEBUG, "%s", line);
1318 * print_string - print a readable representation of a string using
1322 print_string(p, len, printer, arg)
1325 void (*printer) __ARGS((void *, char *, ...));
1331 for (; len > 0; --len) {
1333 if (' ' <= c && c <= '~')
1334 printer(arg, "%c", c);
1336 printer(arg, "\\%.3o", c);
1342 * novm - log an error message saying we ran out of memory, and die.
1348 syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg);