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.
20 #define RCSID "$Id: main.c,v 1.88 1999/12/23 01:28:27 paulus Exp $"
35 #include <sys/param.h>
36 #include <sys/types.h>
39 #include <sys/resource.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
55 #include "pathnames.h"
56 #include "patchlevel.h"
64 #endif /* IPX_CHANGE */
69 static const char rcsid[] = RCSID;
72 char ifname[32]; /* Interface name */
73 int ifunit; /* Interface unit number */
75 char *progname; /* Name of this program */
76 char hostname[MAXNAMELEN]; /* Our hostname */
77 static char pidfilename[MAXPATHLEN]; /* name of pid file */
78 static char linkpidfile[MAXPATHLEN]; /* name of linkname pid file */
79 static char ppp_devnam[MAXPATHLEN]; /* name of PPP tty (maybe ttypx) */
80 static uid_t uid; /* Our real user-id */
81 static int conn_running; /* we have a [dis]connector running */
83 int ttyfd; /* Serial port file descriptor */
84 mode_t tty_mode = -1; /* Original access permissions to tty */
85 int baud_rate; /* Actual bits/second for serial device */
86 int hungup; /* terminal has been hung up */
87 int privileged; /* we're running as real uid root */
88 int need_holdoff; /* need holdoff period before restarting */
89 int detached; /* have detached from terminal */
90 struct stat devstat; /* result of stat() on devnam */
91 int prepass = 0; /* doing prepass to find device name */
92 int devnam_fixed; /* set while in options.ttyxx file */
93 volatile int status; /* exit status for pppd */
94 int unsuccess; /* # unsuccessful connection attempts */
95 int do_callback; /* != 0 if we should do callback next */
96 int doing_callback; /* != 0 if we are doing callback */
98 int (*holdoff_hook) __P((void)) = NULL;
99 int (*new_phase_hook) __P((int)) = NULL;
101 static int fd_ppp = -1; /* fd for talking PPP */
102 static int fd_loop; /* fd for getting demand-dial packets */
103 static int pty_master; /* fd for master side of pty */
104 static int pty_slave; /* fd for slave side of pty */
105 static int real_ttyfd; /* fd for actual serial port (not pty) */
107 int phase; /* where the link is at */
112 static sigjmp_buf sigjmp;
114 char **script_env; /* Env. variable values for scripts */
115 int s_env_nalloc; /* # words avail at script_env */
117 u_char outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for outgoing packet */
118 u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */
120 static int n_children; /* # child processes still running */
121 static int got_sigchld; /* set if we have received a SIGCHLD */
123 static int locked; /* lock() has succeeded */
124 static int privopen; /* don't lock, open device as root */
126 char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n";
128 GIDSET_TYPE groups[NGROUPS_MAX];/* groups the user is in */
129 int ngroups; /* How many groups valid in groups */
131 static struct timeval start_time; /* Time when link was started. */
133 struct pppd_stats link_stats;
134 int link_connect_time;
135 int link_stats_valid;
137 static int charshunt_pid; /* Process ID for charshunt */
140 * We maintain a list of child process pids and
141 * functions to call when they exit.
146 void (*done) __P((void *));
148 struct subprocess *next;
151 static struct subprocess *children;
153 /* Prototypes for procedures local to this file. */
155 static void create_pidfile __P((void));
156 static void create_linkpidfile __P((void));
157 static void cleanup __P((void));
158 static void close_tty __P((void));
159 static void get_input __P((void));
160 static void calltimeout __P((void));
161 static struct timeval *timeleft __P((struct timeval *));
162 static void kill_my_pg __P((int));
163 static void hup __P((int));
164 static void term __P((int));
165 static void chld __P((int));
166 static void toggle_debug __P((int));
167 static void open_ccp __P((int));
168 static void bad_signal __P((int));
169 static void holdoff_end __P((void *));
170 static int device_script __P((char *, int, int, int));
171 static int reap_kids __P((int waitfor));
172 static void record_child __P((int, char *, void (*) (void *), void *));
173 static int start_charshunt __P((int, int));
174 static void charshunt_done __P((void *));
175 static void charshunt __P((int, int, char *));
176 static int record_write __P((FILE *, int code, u_char *buf, int nb,
179 extern char *ttyname __P((int));
180 extern char *getlogin __P((void));
181 int main __P((int, char *[]));
185 #define O_NONBLOCK O_NDELAY
189 #define setlogmask(x)
193 * PPP Data Link Layer "protocol" table.
194 * One entry per supported protocol.
195 * The last entry must be NULL.
197 struct protent *protocols[] = {
229 struct protent *protp;
233 new_phase(PHASE_INITIALIZE);
236 * Ensure that fds 0, 1, 2 are open, to /dev/null if nowhere else.
237 * This way we can close 0, 1, 2 in detach() without clobbering
238 * a fd that we are using.
240 if ((i = open("/dev/null", O_RDWR)) >= 0) {
241 while (0 <= i && i <= 2)
249 /* Initialize syslog facilities */
252 if (gethostname(hostname, MAXNAMELEN) < 0 ) {
253 option_error("Couldn't get hostname: %m");
256 hostname[MAXNAMELEN-1] = 0;
258 /* make sure we don't create world or group writable files. */
259 umask(umask(0777) | 022);
262 privileged = uid == 0;
263 slprintf(numbuf, sizeof(numbuf), "%d", uid);
264 script_setenv("ORIG_UID", numbuf);
266 ngroups = getgroups(NGROUPS_MAX, groups);
269 * Initialize magic number generator now so that protocols may
270 * use magic numbers in initialization.
275 * Initialize to the standard option set, then parse, in order,
276 * the system options file, the user's options file,
277 * the tty's options file, and the command line arguments.
279 for (i = 0; (protp = protocols[i]) != NULL; ++i)
285 if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1)
286 || !options_from_user())
287 exit(EXIT_OPTION_ERROR);
289 /* scan command line and options files to find device name */
291 parse_args(argc-1, argv+1);
295 * Work out the device name, if it hasn't already been specified.
297 using_pty = notty || ptycommand != NULL;
298 if (!using_pty && default_device) {
300 if (!isatty(0) || (p = ttyname(0)) == NULL) {
301 option_error("no device specified and stdin is not a tty");
302 exit(EXIT_OPTION_ERROR);
304 strlcpy(devnam, p, sizeof(devnam));
305 if (stat(devnam, &devstat) < 0)
306 fatal("Couldn't stat default device %s: %m", devnam);
310 * Parse the tty options file and the command line.
311 * The per-tty options file should not change
312 * ptycommand, notty or devnam.
316 if (!options_for_tty())
317 exit(EXIT_OPTION_ERROR);
321 if (!parse_args(argc-1, argv+1))
322 exit(EXIT_OPTION_ERROR);
325 * Check that we are running as root.
327 if (geteuid() != 0) {
328 option_error("must be root to run %s, since it is not setuid-root",
333 if (!ppp_available()) {
334 option_error(no_ppp_msg);
335 exit(EXIT_NO_KERNEL_SUPPORT);
339 * Check that the options given are valid and consistent.
341 if (!sys_check_options())
342 exit(EXIT_OPTION_ERROR);
343 auth_check_options();
344 for (i = 0; (protp = protocols[i]) != NULL; ++i)
345 if (protp->check_options != NULL)
346 (*protp->check_options)();
347 if (demand && connect_script == 0) {
348 option_error("connect script is required for demand-dialling\n");
349 exit(EXIT_OPTION_ERROR);
351 /* default holdoff to 0 if no connect script has been given */
352 if (connect_script == 0 && !holdoff_specified)
356 if (!default_device) {
357 option_error("%s option precludes specifying device name",
358 notty? "notty": "pty");
359 exit(EXIT_OPTION_ERROR);
361 if (ptycommand != NULL && notty) {
362 option_error("pty option is incompatible with notty option");
363 exit(EXIT_OPTION_ERROR);
365 default_device = notty;
368 if (notty && log_to_fd <= 1)
372 * If the user has specified a device which is the same as
373 * the one on stdin, pretend they didn't specify any.
374 * If the device is already open read/write on stdin,
375 * we assume we don't need to lock it, and we can open it as root.
377 if (fstat(0, &statbuf) >= 0 && S_ISCHR(statbuf.st_mode)
378 && statbuf.st_rdev == devstat.st_rdev) {
380 fdflags = fcntl(0, F_GETFL);
381 if (fdflags != -1 && (fdflags & O_ACCMODE) == O_RDWR)
389 * Don't send log messages to the serial port, it tends to
390 * confuse the peer. :-)
392 if (log_to_fd >= 0 && fstat(log_to_fd, &statbuf) >= 0
393 && S_ISCHR(statbuf.st_mode) && statbuf.st_rdev == devstat.st_rdev)
396 script_setenv("DEVICE", devnam);
399 * Initialize system-dependent stuff.
403 setlogmask(LOG_UPTO(LOG_DEBUG));
406 * Detach ourselves from the terminal, if required,
407 * and identify who is running us.
409 if (!nodetach && !updetach)
414 if (pw != NULL && pw->pw_name != NULL)
419 syslog(LOG_NOTICE, "pppd %s.%d%s started by %s, uid %d",
420 VERSION, PATCHLEVEL, IMPLEMENTATION, p, uid);
421 script_setenv("PPPLOGNAME", p);
424 * Compute mask of all interesting signals and install signal handlers
425 * for each. Only one signal handler may be active at a time. Therefore,
426 * all other signals should be masked when any handler is executing.
429 sigaddset(&mask, SIGHUP);
430 sigaddset(&mask, SIGINT);
431 sigaddset(&mask, SIGTERM);
432 sigaddset(&mask, SIGCHLD);
433 sigaddset(&mask, SIGUSR2);
435 #define SIGNAL(s, handler) do { \
436 sa.sa_handler = handler; \
437 if (sigaction(s, &sa, NULL) < 0) \
438 fatal("Couldn't establish signal handler (%d): %m", s); \
443 SIGNAL(SIGHUP, hup); /* Hangup */
444 SIGNAL(SIGINT, term); /* Interrupt */
445 SIGNAL(SIGTERM, term); /* Terminate */
446 SIGNAL(SIGCHLD, chld);
448 SIGNAL(SIGUSR1, toggle_debug); /* Toggle debug flag */
449 SIGNAL(SIGUSR2, open_ccp); /* Reopen CCP */
452 * Install a handler for other signals which would otherwise
453 * cause pppd to exit without cleaning up.
455 SIGNAL(SIGABRT, bad_signal);
456 SIGNAL(SIGALRM, bad_signal);
457 SIGNAL(SIGFPE, bad_signal);
458 SIGNAL(SIGILL, bad_signal);
459 SIGNAL(SIGPIPE, bad_signal);
460 SIGNAL(SIGQUIT, bad_signal);
461 SIGNAL(SIGSEGV, bad_signal);
463 SIGNAL(SIGBUS, bad_signal);
466 SIGNAL(SIGEMT, bad_signal);
469 SIGNAL(SIGPOLL, bad_signal);
472 SIGNAL(SIGPROF, bad_signal);
475 SIGNAL(SIGSYS, bad_signal);
478 SIGNAL(SIGTRAP, bad_signal);
481 SIGNAL(SIGVTALRM, bad_signal);
484 SIGNAL(SIGXCPU, bad_signal);
487 SIGNAL(SIGXFSZ, bad_signal);
491 * Apparently we can get a SIGPIPE when we call syslog, if
492 * syslogd has died and been restarted. Ignoring it seems
495 signal(SIGPIPE, SIG_IGN);
499 create_linkpidfile();
502 * If we're doing dial-on-demand, set up the interface now.
506 * Open the loopback channel and set it up to be the ppp interface.
508 fd_loop = open_ppp_loopback();
510 syslog(LOG_INFO, "Using interface ppp%d", ifunit);
511 slprintf(ifname, sizeof(ifname), "ppp%d", ifunit);
512 script_setenv("IFNAME", ifname);
514 create_pidfile(); /* write pid to file */
517 * Configure the interface and mark it up, etc.
530 doing_callback = do_callback;
533 if (demand && !doing_callback) {
535 * Don't do anything until we see some activity.
538 new_phase(PHASE_DORMANT);
542 if (sigsetjmp(sigjmp, 1) == 0) {
543 sigprocmask(SIG_BLOCK, &mask, NULL);
544 if (kill_link || got_sigchld) {
545 sigprocmask(SIG_UNBLOCK, &mask, NULL);
548 sigprocmask(SIG_UNBLOCK, &mask, NULL);
549 wait_input(timeleft(&timo));
559 if (get_loop_output())
565 if (kill_link && !persist)
569 * Now we want to bring up the link.
572 info("Starting link");
575 new_phase(PHASE_SERIALCONN);
578 * Get a pty master/slave pair if the pty, notty, or record
579 * options were specified.
581 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
584 if (ptycommand != NULL || notty || record_file != NULL) {
585 if (!get_pty(&pty_master, &pty_slave, ppp_devnam, uid)) {
586 error("Couldn't allocate pseudo-tty");
587 status = EXIT_FATAL_ERROR;
590 set_up_tty(pty_slave, 1);
594 * Lock the device if we've been asked to.
596 status = EXIT_LOCK_FAILED;
597 if (lockflag && !privopen) {
598 if (lock(devnam) < 0)
604 * Open the serial device and set it up to be the ppp interface.
605 * First we open it in non-blocking mode so we can set the
606 * various termios flags appropriately. If we aren't dialling
607 * out and we want to use the modem lines, we reopen it later
608 * in order to wait for the carrier detect signal from the modem.
612 connector = doing_callback? callback_script: connect_script;
613 if (devnam[0] != 0) {
615 /* If the user specified the device name, become the
616 user before opening it. */
618 if (!devnam_info.priv && !privopen)
620 ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0);
622 if (!devnam_info.priv && !privopen)
628 error("Failed to open %s: %m", devnam);
629 status = EXIT_OPEN_FAILED;
631 if (!persist || err != EINTR)
634 if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1
635 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
636 warn("Couldn't reset non-blocking mode on device: %m");
639 * Do the equivalent of `mesg n' to stop broadcast messages.
641 if (fstat(ttyfd, &statbuf) < 0
642 || fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) {
643 warn("Couldn't restrict write permissions to %s: %m", devnam);
645 tty_mode = statbuf.st_mode;
648 * Set line speed, flow control, etc.
649 * If we have a non-null connection or initializer script,
650 * on most systems we set CLOCAL for now so that we can talk
651 * to the modem before carrier comes up. But this has the
652 * side effect that we might miss it if CD drops before we
653 * get to clear CLOCAL below. On systems where we can talk
654 * successfully to the modem with CLOCAL clear and CD down,
655 * we could clear CLOCAL at this point.
657 set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0)
658 || initializer != NULL));
663 * If the notty and/or record option was specified,
664 * start up the character shunt now.
666 status = EXIT_PTYCMD_FAILED;
667 if (ptycommand != NULL) {
668 if (record_file != NULL) {
669 int ipipe[2], opipe[2], ok;
671 if (pipe(ipipe) < 0 || pipe(opipe) < 0)
672 fatal("Couldn't create pipes for record option: %m");
673 ok = device_script(ptycommand, opipe[0], ipipe[1], 1) == 0
674 && start_charshunt(ipipe[0], opipe[1]);
682 if (device_script(ptycommand, pty_master, pty_master, 1) < 0)
689 if (!start_charshunt(0, 1))
691 } else if (record_file != NULL) {
692 if (!start_charshunt(ttyfd, ttyfd))
696 /* run connection script */
697 if ((connector && connector[0]) || initializer) {
698 if (real_ttyfd != -1) {
699 /* XXX do this if doing_callback == CALLBACK_DIALIN? */
700 if (!default_device && modem) {
701 setdtr(real_ttyfd, 0); /* in case modem is off hook */
703 setdtr(real_ttyfd, 1);
707 if (initializer && initializer[0]) {
708 if (device_script(initializer, ttyfd, ttyfd, 0) < 0) {
709 error("Initializer script failed");
710 status = EXIT_INIT_FAILED;
716 info("Serial port initialized.");
719 if (connector && connector[0]) {
720 if (device_script(connector, ttyfd, ttyfd, 0) < 0) {
721 error("Connect script failed");
722 status = EXIT_CONNECT_FAILED;
728 info("Serial connection established.");
731 /* set line speed, flow control, etc.;
732 clear CLOCAL if modem option */
733 if (real_ttyfd != -1)
734 set_up_tty(real_ttyfd, 0);
736 if (doing_callback == CALLBACK_DIALIN)
740 /* reopen tty if necessary to wait for carrier */
741 if (connector == NULL && modem && devnam[0] != 0) {
743 if ((i = open(devnam, O_RDWR)) >= 0)
745 if (errno != EINTR) {
746 error("Failed to reopen %s: %m", devnam);
747 status = EXIT_OPEN_FAILED;
749 if (!persist || errno != EINTR || hungup || kill_link)
755 slprintf(numbuf, sizeof(numbuf), "%d", baud_rate);
756 script_setenv("SPEED", numbuf);
758 /* run welcome script, if any */
759 if (welcomer && welcomer[0]) {
760 if (device_script(welcomer, ttyfd, ttyfd, 0) < 0)
761 warn("Welcome script failed");
764 /* set up the serial device as a ppp interface */
765 fd_ppp = establish_ppp(ttyfd);
767 status = EXIT_FATAL_ERROR;
773 info("Using interface ppp%d", ifunit);
774 slprintf(ifname, sizeof(ifname), "ppp%d", ifunit);
775 script_setenv("IFNAME", ifname);
777 create_pidfile(); /* write pid to file */
781 * Start opening the connection and wait for
782 * incoming events (reply, timeout, etc.).
784 notice("Connect: %s <--> %s", ifname, ppp_devnam);
785 gettimeofday(&start_time, NULL);
786 link_stats_valid = 0;
787 script_unsetenv("CONNECT_TIME");
788 script_unsetenv("BYTES_SENT");
789 script_unsetenv("BYTES_RCVD");
793 * If we are initiating this connection, wait for a short
794 * time for something from the peer. This can avoid bouncing
795 * our packets off his tty before he has it set up.
798 if (connect_delay != 0 && (connector != NULL || ptycommand != NULL)) {
800 t.tv_sec = connect_delay / 1000;
801 t.tv_usec = connect_delay % 1000;
805 lcp_open(0); /* Start protocol */
807 status = EXIT_NEGOTIATION_FAILED;
808 new_phase(PHASE_ESTABLISH);
809 while (phase != PHASE_DEAD) {
810 if (sigsetjmp(sigjmp, 1) == 0) {
811 sigprocmask(SIG_BLOCK, &mask, NULL);
812 if (kill_link || open_ccp_flag || got_sigchld) {
813 sigprocmask(SIG_UNBLOCK, &mask, NULL);
816 sigprocmask(SIG_UNBLOCK, &mask, NULL);
817 wait_input(timeleft(&timo));
824 lcp_close(0, "User request");
828 if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) {
829 ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
830 (*ccp_protent.open)(0);
835 reap_kids(0); /* Don't leave dead kids lying around */
839 * Print connect time and statistics.
841 if (link_stats_valid) {
842 int t = (link_connect_time + 5) / 6; /* 1/10ths of minutes */
843 info("Connect time %d.%d minutes.", t/10, t%10);
844 info("Sent %d bytes, received %d bytes.",
845 link_stats.bytes_out, link_stats.bytes_in);
849 * Delete pid file before disestablishing ppp. Otherwise it
850 * can happen that another pppd gets the same unit and then
851 * we delete its pid file.
854 if (pidfilename[0] != 0
855 && unlink(pidfilename) < 0 && errno != ENOENT)
856 warn("unable to delete pid file %s: %m", pidfilename);
861 * If we may want to bring the link up again, transfer
862 * the ppp unit back to the loopback. Set the
863 * real serial device back to its normal mode of operation.
869 disestablish_ppp(ttyfd);
875 * Run disconnector script, if requested.
876 * XXX we may not be able to do this if the line has hung up!
879 if (disconnect_script && !hungup) {
880 new_phase(PHASE_DISCONNECT);
882 set_up_tty(real_ttyfd, 1);
883 if (device_script(disconnect_script, ttyfd, ttyfd, 0) < 0) {
884 warn("disconnect script failed");
886 info("Serial link disconnected.");
903 if (pidfilename[0] != 0
904 && unlink(pidfilename) < 0 && errno != ENOENT)
905 warn("unable to delete pid file %s: %m", pidfilename);
909 if (!persist || (maxfail > 0 && unsuccess >= maxfail))
915 t = need_holdoff? holdoff: 0;
917 t = (*holdoff_hook)();
919 new_phase(PHASE_HOLDOFF);
920 TIMEOUT(holdoff_end, NULL, t);
922 if (sigsetjmp(sigjmp, 1) == 0) {
923 sigprocmask(SIG_BLOCK, &mask, NULL);
924 if (kill_link || got_sigchld) {
925 sigprocmask(SIG_UNBLOCK, &mask, NULL);
928 sigprocmask(SIG_UNBLOCK, &mask, NULL);
929 wait_input(timeleft(&timo));
936 new_phase(PHASE_DORMANT); /* allow signal to end holdoff */
940 } while (phase == PHASE_HOLDOFF);
946 /* Wait for scripts to finish */
947 /* XXX should have a timeout here */
948 while (n_children > 0) {
950 struct subprocess *chp;
951 dbglog("Waiting for %d child processes...", n_children);
952 for (chp = children; chp != NULL; chp = chp->next)
953 dbglog(" script %s, pid %d", chp->prog, chp->pid);
955 if (reap_kids(1) < 0)
964 * detach - detach us from the controlling terminal.
973 if ((pid = fork()) < 0) {
974 error("Couldn't detach (fork failed: %m)");
975 die(1); /* or just return? */
981 exit(0); /* parent dies */
990 /* update pid files if they have been written already */
994 create_linkpidfile();
998 * reopen_log - (re)open our connection to syslog.
1004 openlog("pppd", LOG_PID);
1006 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
1007 setlogmask(LOG_UPTO(LOG_INFO));
1012 * Create a file containing our process ID.
1020 slprintf(pidfilename, sizeof(pidfilename), "%s%s.pid",
1021 _PATH_VARRUN, ifname);
1022 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
1023 fprintf(pidfile, "%d\n", getpid());
1024 (void) fclose(pidfile);
1026 error("Failed to create pid file %s: %m", pidfilename);
1029 slprintf(numbuf, sizeof(numbuf), "%d", getpid());
1030 script_setenv("PPPD_PID", numbuf);
1032 create_linkpidfile();
1036 create_linkpidfile()
1040 if (linkname[0] == 0)
1042 slprintf(linkpidfile, sizeof(linkpidfile), "%sppp-%s.pid",
1043 _PATH_VARRUN, linkname);
1044 if ((pidfile = fopen(linkpidfile, "w")) != NULL) {
1045 fprintf(pidfile, "%d\n", getpid());
1047 fprintf(pidfile, "%s\n", ifname);
1048 (void) fclose(pidfile);
1050 error("Failed to create pid file %s: %m", linkpidfile);
1053 script_setenv("LINKNAME", linkname);
1057 * holdoff_end - called via a timeout when the holdoff period ends.
1063 new_phase(PHASE_DORMANT);
1066 /* List of protocol names, to make our messages a little more informative. */
1067 struct protocol_list {
1070 } protocol_list[] = {
1072 { 0x23, "OSI Network Layer" },
1073 { 0x25, "Xerox NS IDP" },
1074 { 0x27, "DECnet Phase IV" },
1075 { 0x29, "Appletalk" },
1076 { 0x2b, "Novell IPX" },
1077 { 0x2d, "VJ compressed TCP/IP" },
1078 { 0x2f, "VJ uncompressed TCP/IP" },
1079 { 0x31, "Bridging PDU" },
1080 { 0x33, "Stream Protocol ST-II" },
1081 { 0x35, "Banyan Vines" },
1082 { 0x39, "AppleTalk EDDP" },
1083 { 0x3b, "AppleTalk SmartBuffered" },
1084 { 0x3d, "Multi-Link" },
1085 { 0x3f, "NETBIOS Framing" },
1086 { 0x41, "Cisco Systems" },
1087 { 0x43, "Ascom Timeplex" },
1088 { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" },
1089 { 0x47, "DCA Remote Lan" },
1090 { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" },
1091 { 0x4b, "SNA over 802.2" },
1093 { 0x4f, "IP6 Header Compression" },
1094 { 0x6f, "Stampede Bridging" },
1095 { 0xfb, "single-link compression" },
1096 { 0xfd, "1st choice compression" },
1097 { 0x0201, "802.1d Hello Packets" },
1098 { 0x0203, "IBM Source Routing BPDU" },
1099 { 0x0205, "DEC LANBridge100 Spanning Tree" },
1100 { 0x0231, "Luxcom" },
1101 { 0x0233, "Sigma Network Systems" },
1102 { 0x8021, "Internet Protocol Control Protocol" },
1103 { 0x8023, "OSI Network Layer Control Protocol" },
1104 { 0x8025, "Xerox NS IDP Control Protocol" },
1105 { 0x8027, "DECnet Phase IV Control Protocol" },
1106 { 0x8029, "Appletalk Control Protocol" },
1107 { 0x802b, "Novell IPX Control Protocol" },
1108 { 0x8031, "Bridging NCP" },
1109 { 0x8033, "Stream Protocol Control Protocol" },
1110 { 0x8035, "Banyan Vines Control Protocol" },
1111 { 0x803d, "Multi-Link Control Protocol" },
1112 { 0x803f, "NETBIOS Framing Control Protocol" },
1113 { 0x8041, "Cisco Systems Control Protocol" },
1114 { 0x8043, "Ascom Timeplex" },
1115 { 0x8045, "Fujitsu LBLB Control Protocol" },
1116 { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" },
1117 { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" },
1118 { 0x804b, "SNA over 802.2 Control Protocol" },
1119 { 0x804d, "SNA Control Protocol" },
1120 { 0x804f, "IP6 Header Compression Control Protocol" },
1121 { 0x006f, "Stampede Bridging Control Protocol" },
1122 { 0x80fb, "Single Link Compression Control Protocol" },
1123 { 0x80fd, "Compression Control Protocol" },
1124 { 0xc021, "Link Control Protocol" },
1125 { 0xc023, "Password Authentication Protocol" },
1126 { 0xc025, "Link Quality Report" },
1127 { 0xc027, "Shiva Password Authentication Protocol" },
1128 { 0xc029, "CallBack Control Protocol (CBCP)" },
1129 { 0xc081, "Container Control Protocol" },
1130 { 0xc223, "Challenge Handshake Authentication Protocol" },
1131 { 0xc281, "Proprietary Authentication Protocol" },
1136 * protocol_name - find a name for a PPP protocol.
1139 protocol_name(proto)
1142 struct protocol_list *lp;
1144 for (lp = protocol_list; lp->proto != 0; ++lp)
1145 if (proto == lp->proto)
1151 * get_input - called when incoming data is available.
1159 struct protent *protp;
1161 p = inpacket_buf; /* point to beginning of packet buffer */
1163 len = read_packet(inpacket_buf);
1168 notice("Modem hangup");
1170 status = EXIT_HANGUP;
1171 lcp_lowerdown(0); /* serial link is no longer available */
1176 if (debug /*&& (debugflags & DBG_INPACKET)*/)
1177 dbglog("rcvd %P", p, len);
1179 if (len < PPP_HDRLEN) {
1180 MAINDEBUG(("io(): Received short packet."));
1184 p += 2; /* Skip address and control */
1185 GETSHORT(protocol, p);
1189 * Toss all non-LCP packets unless LCP is OPEN.
1191 if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) {
1192 MAINDEBUG(("get_input: Received non-LCP packet when LCP not open."));
1197 * Until we get past the authentication phase, toss all packets
1198 * except LCP, LQR and authentication packets.
1200 if (phase <= PHASE_AUTHENTICATE
1201 && !(protocol == PPP_LCP || protocol == PPP_LQR
1202 || protocol == PPP_PAP || protocol == PPP_CHAP)) {
1203 MAINDEBUG(("get_input: discarding proto 0x%x in phase %d",
1209 * Upcall the proper protocol input routine.
1211 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
1212 if (protp->protocol == protocol && protp->enabled_flag) {
1213 (*protp->input)(0, p, len);
1216 if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag
1217 && protp->datainput != NULL) {
1218 (*protp->datainput)(0, p, len);
1224 const char *pname = protocol_name(protocol);
1226 warn("Unsupported protocol '%s' (0x%x) received", pname, protocol);
1228 warn("Unsupported protocol 0x%x received", protocol);
1230 lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN);
1234 * new_phase - signal the start of a new phase of pppd's operation.
1242 (*new_phase_hook)(p);
1246 * die - clean up state and exit with the specified status.
1253 syslog(LOG_INFO, "Exit.");
1258 * cleanup - restore anything which needs to be restored before we exit
1267 disestablish_ppp(ttyfd);
1268 if (real_ttyfd >= 0)
1271 if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT)
1272 warn("unable to delete pid file %s: %m", pidfilename);
1274 if (linkpidfile[0] != 0 && unlink(linkpidfile) < 0 && errno != ENOENT)
1275 warn("unable to delete pid file %s: %m", linkpidfile);
1283 * close_tty - restore the terminal device and close it.
1288 /* drop dtr to hang up */
1289 if (!default_device && modem) {
1290 setdtr(real_ttyfd, 0);
1292 * This sleep is in case the serial port has CLOCAL set by default,
1293 * and consequently will reassert DTR when we close the device.
1298 restore_tty(real_ttyfd);
1300 if (tty_mode != (mode_t) -1) {
1301 if (fchmod(real_ttyfd, tty_mode) != 0) {
1302 /* XXX if devnam is a symlink, this will change the link */
1303 chmod(devnam, tty_mode);
1312 * update_link_stats - get stats at link termination.
1315 update_link_stats(u)
1321 if (!get_ppp_stats(u, &link_stats)
1322 || gettimeofday(&now, NULL) < 0)
1324 link_connect_time = now.tv_sec - start_time.tv_sec;
1325 link_stats_valid = 1;
1327 slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time);
1328 script_setenv("CONNECT_TIME", numbuf);
1329 slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out);
1330 script_setenv("BYTES_SENT", numbuf);
1331 slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in);
1332 script_setenv("BYTES_RCVD", numbuf);
1337 struct timeval c_time; /* time at which to call routine */
1338 void *c_arg; /* argument to routine */
1339 void (*c_func) __P((void *)); /* routine */
1340 struct callout *c_next;
1343 static struct callout *callout = NULL; /* Callout list */
1344 static struct timeval timenow; /* Current time */
1347 * timeout - Schedule a timeout.
1349 * Note that this timeout takes the number of seconds, NOT hz (as in
1353 timeout(func, arg, time)
1354 void (*func) __P((void *));
1358 struct callout *newp, *p, **pp;
1360 MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time));
1365 if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL)
1366 fatal("Out of memory in timeout()!");
1368 newp->c_func = func;
1369 gettimeofday(&timenow, NULL);
1370 newp->c_time.tv_sec = timenow.tv_sec + time;
1371 newp->c_time.tv_usec = timenow.tv_usec;
1374 * Find correct place and link it in.
1376 for (pp = &callout; (p = *pp); pp = &p->c_next)
1377 if (newp->c_time.tv_sec < p->c_time.tv_sec
1378 || (newp->c_time.tv_sec == p->c_time.tv_sec
1379 && newp->c_time.tv_usec < p->c_time.tv_usec))
1387 * untimeout - Unschedule a timeout.
1390 untimeout(func, arg)
1391 void (*func) __P((void *));
1394 struct callout **copp, *freep;
1396 MAINDEBUG(("Untimeout %p:%p.", func, arg));
1399 * Find first matching timeout and remove it from the list.
1401 for (copp = &callout; (freep = *copp); copp = &freep->c_next)
1402 if (freep->c_func == func && freep->c_arg == arg) {
1403 *copp = freep->c_next;
1404 free((char *) freep);
1411 * calltimeout - Call any timeout routines which are now due.
1418 while (callout != NULL) {
1421 if (gettimeofday(&timenow, NULL) < 0)
1422 fatal("Failed to get time of day: %m");
1423 if (!(p->c_time.tv_sec < timenow.tv_sec
1424 || (p->c_time.tv_sec == timenow.tv_sec
1425 && p->c_time.tv_usec <= timenow.tv_usec)))
1426 break; /* no, it's not time yet */
1428 callout = p->c_next;
1429 (*p->c_func)(p->c_arg);
1437 * timeleft - return the length of time until the next timeout is due.
1439 static struct timeval *
1441 struct timeval *tvp;
1443 if (callout == NULL)
1446 gettimeofday(&timenow, NULL);
1447 tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
1448 tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec;
1449 if (tvp->tv_usec < 0) {
1450 tvp->tv_usec += 1000000;
1453 if (tvp->tv_sec < 0)
1454 tvp->tv_sec = tvp->tv_usec = 0;
1461 * kill_my_pg - send a signal to our process group, and ignore it ourselves.
1467 struct sigaction act, oldact;
1469 act.sa_handler = SIG_IGN;
1472 sigaction(sig, &act, &oldact);
1473 sigaction(sig, &oldact, NULL);
1478 * hup - Catch SIGHUP signal.
1480 * Indicates that the physical layer has been disconnected.
1481 * We don't rely on this indication; if the user has sent this
1482 * signal, we just take the link down.
1488 info("Hangup (SIGHUP)");
1490 if (status != EXIT_HANGUP)
1491 status = EXIT_USER_REQUEST;
1493 /* Send the signal to the [dis]connector process(es) also */
1496 kill(charshunt_pid, sig);
1498 siglongjmp(sigjmp, 1);
1503 * term - Catch SIGTERM signal and SIGINT signal (^C/del).
1505 * Indicates that we should initiate a graceful disconnect and exit.
1512 info("Terminating on signal %d.", sig);
1513 persist = 0; /* don't try to restart */
1515 status = EXIT_USER_REQUEST;
1517 /* Send the signal to the [dis]connector process(es) also */
1520 kill(charshunt_pid, sig);
1522 siglongjmp(sigjmp, 1);
1527 * chld - Catch SIGCHLD signal.
1528 * Sets a flag so we will call reap_kids in the mainline.
1536 siglongjmp(sigjmp, 1);
1541 * toggle_debug - Catch SIGUSR1 signal.
1543 * Toggle debug flag.
1552 setlogmask(LOG_UPTO(LOG_DEBUG));
1554 setlogmask(LOG_UPTO(LOG_WARNING));
1560 * open_ccp - Catch SIGUSR2 signal.
1562 * Try to (re)negotiate compression.
1571 siglongjmp(sigjmp, 1);
1576 * bad_signal - We've caught a fatal signal. Clean up state and exit.
1582 static int crashed = 0;
1587 error("Fatal signal %d", sig);
1589 kill_my_pg(SIGTERM);
1591 kill(charshunt_pid, SIGTERM);
1597 * device_script - run a program to talk to the serial device
1598 * (e.g. to run the connector or disconnector script).
1601 device_script(program, in, out, dont_wait)
1615 error("Failed to create child process: %m");
1624 int newin = dup(in);
1628 } else if (out == 2) {
1631 if (log_to_fd >= 0) {
1636 errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600);
1637 if (errfd >= 0 && errfd != 2) {
1657 if (getuid() != uid) {
1658 error("setuid failed");
1662 execl("/bin/sh", "sh", "-c", program, (char *)0);
1663 error("could not exec /bin/sh: %m");
1669 record_child(pid, program, NULL, NULL);
1672 while (waitpid(pid, &status, 0) < 0) {
1675 fatal("error waiting for (dis)connection process: %m");
1680 return (status == 0 ? 0 : -1);
1685 * run-program - execute a program with given arguments,
1686 * but don't wait for it.
1687 * If the program can't be executed, logs an error unless
1688 * must_exist is 0 and the program file doesn't exist.
1689 * Returns -1 if it couldn't fork, 0 if the file doesn't exist
1690 * or isn't an executable plain file, or the process ID of the child.
1691 * If done != NULL, (*done)(arg) will be called later (within
1692 * reap_kids) iff the return value is > 0.
1695 run_program(prog, args, must_exist, done, arg)
1699 void (*done) __P((void *));
1706 * First check if the file exists and is executable.
1707 * We don't use access() because that would use the
1708 * real user-id, which might not be root, and the script
1709 * might be accessible only to root.
1712 if (stat(prog, &sbuf) < 0 || !S_ISREG(sbuf.st_mode)
1713 || (sbuf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) {
1714 if (must_exist || errno != ENOENT)
1715 warn("Can't execute %s: %m", prog);
1721 error("Failed to create child process for %s: %m", prog);
1727 /* Leave the current location */
1728 (void) setsid(); /* No controlling tty. */
1729 (void) umask (S_IRWXG|S_IRWXO);
1730 (void) chdir ("/"); /* no current directory. */
1731 setuid(0); /* set real UID = root */
1734 /* Ensure that nothing of our device environment is inherited. */
1740 close (ttyfd); /* tty interface to the ppp device */
1741 if (real_ttyfd >= 0)
1744 /* Don't pass handles to the PPP device, even by accident. */
1745 new_fd = open (_PATH_DEVNULL, O_RDWR);
1748 dup2 (new_fd, 0); /* stdin <- /dev/null */
1751 dup2 (0, 1); /* stdout -> /dev/null */
1752 dup2 (0, 2); /* stderr -> /dev/null */
1756 /* Force the priority back to zero if pppd is running higher. */
1757 if (setpriority (PRIO_PROCESS, 0, 0) < 0)
1758 warn("can't reset priority to 0: %m");
1761 /* SysV recommends a second fork at this point. */
1763 /* run the program */
1764 execve(prog, args, script_env);
1765 if (must_exist || errno != ENOENT) {
1766 /* have to reopen the log, there's nowhere else
1767 for the message to go. */
1769 syslog(LOG_ERR, "Can't execute %s: %m", prog);
1776 dbglog("Script %s started (pid %d)", prog, pid);
1777 record_child(pid, prog, done, arg);
1784 * record_child - add a child process to the list for reap_kids
1788 record_child(pid, prog, done, arg)
1791 void (*done) __P((void *));
1794 struct subprocess *chp;
1798 chp = (struct subprocess *) malloc(sizeof(struct subprocess));
1800 warn("losing track of %s process", prog);
1806 chp->next = children;
1813 * reap_kids - get status from any dead child processes,
1814 * and log a message for abnormal terminations.
1821 struct subprocess *chp, **prevp;
1824 if (n_children == 0)
1826 while ((pid = waitpid(-1, &status, (waitfor? 0: WNOHANG))) != -1
1829 for (prevp = &children; (chp = *prevp) != NULL; prevp = &chp->next) {
1830 if (chp->pid == pid) {
1835 if (WIFSIGNALED(status)) {
1836 warn("Child process %s (pid %d) terminated with signal %d",
1837 (chp? chp->prog: "??"), pid, WTERMSIG(status));
1839 dbglog("Script %s finished (pid %d), status = 0x%x",
1840 (chp? chp->prog: "??"), pid, status);
1841 if (chp && chp->done)
1842 (*chp->done)(chp->arg);
1847 if (errno == ECHILD)
1850 error("Error waiting for child process: %m");
1857 * novm - log an error message saying we ran out of memory, and die.
1863 fatal("Virtual memory exhausted allocating %s\n", msg);
1867 * script_setenv - set an environment variable value to be used
1868 * for scripts that we run (e.g. ip-up, auth-up, etc.)
1871 script_setenv(var, value)
1874 size_t vl = strlen(var) + strlen(value) + 2;
1876 char *p, *newstring;
1878 newstring = (char *) malloc(vl);
1881 slprintf(newstring, vl, "%s=%s", var, value);
1883 /* check if this variable is already set */
1884 if (script_env != 0) {
1885 for (i = 0; (p = script_env[i]) != 0; ++i) {
1886 if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
1888 script_env[i] = newstring;
1893 /* no space allocated for script env. ptrs. yet */
1895 script_env = (char **) malloc(16 * sizeof(char *));
1896 if (script_env == 0)
1901 /* reallocate script_env with more space if needed */
1902 if (i + 1 >= s_env_nalloc) {
1904 char **newenv = (char **) realloc((void *)script_env,
1905 new_n * sizeof(char *));
1908 script_env = newenv;
1909 s_env_nalloc = new_n;
1912 script_env[i] = newstring;
1913 script_env[i+1] = 0;
1917 * script_unsetenv - remove a variable from the environment
1921 script_unsetenv(var)
1924 int vl = strlen(var);
1928 if (script_env == 0)
1930 for (i = 0; (p = script_env[i]) != 0; ++i) {
1931 if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
1933 while ((script_env[i] = script_env[i+1]) != 0)
1941 * start_charshunt - create a child process to run the character shunt.
1944 start_charshunt(ifd, ofd)
1951 error("Can't fork process for character shunt: %m");
1958 if (getuid() != uid)
1959 fatal("setuid failed");
1963 charshunt(ifd, ofd, record_file);
1966 charshunt_pid = cpid;
1970 record_child(cpid, "pppd (charshunt)", charshunt_done, NULL);
1982 * charshunt - the character shunt, which passes characters between
1983 * the pty master side and the serial port (or stdin/stdout).
1984 * This runs as the user (not as root).
1985 * (We assume ofd >= ifd which is true the way this gets called. :-).
1988 charshunt(ifd, ofd, record_file)
1993 fd_set ready, writey;
1994 u_char *ibufp, *obufp;
1997 int pty_readable, stdin_readable;
1998 struct timeval lasttime;
1999 FILE *recordf = NULL;
2002 * Reset signal handlers.
2004 signal(SIGHUP, SIG_IGN); /* Hangup */
2005 signal(SIGINT, SIG_DFL); /* Interrupt */
2006 signal(SIGTERM, SIG_DFL); /* Terminate */
2007 signal(SIGCHLD, SIG_DFL);
2008 signal(SIGUSR1, SIG_DFL);
2009 signal(SIGUSR2, SIG_DFL);
2010 signal(SIGABRT, SIG_DFL);
2011 signal(SIGALRM, SIG_DFL);
2012 signal(SIGFPE, SIG_DFL);
2013 signal(SIGILL, SIG_DFL);
2014 signal(SIGPIPE, SIG_DFL);
2015 signal(SIGQUIT, SIG_DFL);
2016 signal(SIGSEGV, SIG_DFL);
2018 signal(SIGBUS, SIG_DFL);
2021 signal(SIGEMT, SIG_DFL);
2024 signal(SIGPOLL, SIG_DFL);
2027 signal(SIGPROF, SIG_DFL);
2030 signal(SIGSYS, SIG_DFL);
2033 signal(SIGTRAP, SIG_DFL);
2036 signal(SIGVTALRM, SIG_DFL);
2039 signal(SIGXCPU, SIG_DFL);
2042 signal(SIGXFSZ, SIG_DFL);
2046 * Open the record file if required.
2048 if (record_file != NULL) {
2049 recordf = fopen(record_file, "a");
2050 if (recordf == NULL)
2051 error("Couldn't create record file %s: %m", record_file);
2054 /* set all the fds to non-blocking mode */
2055 flags = fcntl(pty_master, F_GETFL);
2057 || fcntl(pty_master, F_SETFL, flags | O_NONBLOCK) == -1)
2058 warn("couldn't set pty master to nonblock: %m");
2059 flags = fcntl(ifd, F_GETFL);
2061 || fcntl(ifd, F_SETFL, flags | O_NONBLOCK) == -1)
2062 warn("couldn't set %s to nonblock: %m", (ifd==0? "stdin": "tty"));
2064 flags = fcntl(ofd, F_GETFL);
2066 || fcntl(ofd, F_SETFL, flags | O_NONBLOCK) == -1)
2067 warn("couldn't set stdout to nonblock: %m");
2071 ibufp = obufp = NULL;
2072 pty_readable = stdin_readable = 1;
2073 nfds = (ofd > pty_master? ofd: pty_master) + 1;
2074 if (recordf != NULL) {
2075 gettimeofday(&lasttime, NULL);
2076 putc(7, recordf); /* put start marker */
2077 putc(lasttime.tv_sec >> 24, recordf);
2078 putc(lasttime.tv_sec >> 16, recordf);
2079 putc(lasttime.tv_sec >> 8, recordf);
2080 putc(lasttime.tv_sec, recordf);
2081 lasttime.tv_usec = 0;
2084 while (nibuf != 0 || nobuf != 0 || pty_readable || stdin_readable) {
2088 FD_SET(pty_master, &writey);
2089 else if (stdin_readable)
2090 FD_SET(ifd, &ready);
2092 FD_SET(ofd, &writey);
2093 else if (pty_readable)
2094 FD_SET(pty_master, &ready);
2095 if (select(nfds, &ready, &writey, NULL, NULL) < 0) {
2100 if (FD_ISSET(ifd, &ready)) {
2101 ibufp = inpacket_buf;
2102 nibuf = read(ifd, ibufp, sizeof(inpacket_buf));
2103 if (nibuf < 0 && errno == EIO)
2106 if (!(errno == EINTR || errno == EAGAIN)) {
2107 error("Error reading standard input: %m");
2111 } else if (nibuf == 0) {
2112 /* end of file from stdin */
2114 /* do a 0-length write, hopefully this will generate
2115 an EOF (hangup) on the slave side. */
2116 write(pty_master, inpacket_buf, 0);
2118 if (!record_write(recordf, 4, NULL, 0, &lasttime))
2121 FD_SET(pty_master, &writey);
2123 if (!record_write(recordf, 2, ibufp, nibuf, &lasttime))
2127 if (FD_ISSET(pty_master, &ready)) {
2128 obufp = outpacket_buf;
2129 nobuf = read(pty_master, obufp, sizeof(outpacket_buf));
2130 if (nobuf < 0 && errno == EIO)
2133 if (!(errno == EINTR || errno == EAGAIN)) {
2134 error("Error reading pseudo-tty master: %m");
2138 } else if (nobuf == 0) {
2139 /* end of file from the pty - slave side has closed */
2141 stdin_readable = 0; /* pty is not writable now */
2145 if (!record_write(recordf, 3, NULL, 0, &lasttime))
2148 FD_SET(ofd, &writey);
2150 if (!record_write(recordf, 1, obufp, nobuf, &lasttime))
2154 if (FD_ISSET(ofd, &writey)) {
2155 n = write(ofd, obufp, nobuf);
2158 error("Error writing standard output: %m");
2168 if (FD_ISSET(pty_master, &writey)) {
2169 n = write(pty_master, ibufp, nibuf);
2172 error("Error writing pseudo-tty master: %m");
2187 record_write(f, code, buf, nb, tp)
2197 gettimeofday(&now, NULL);
2198 now.tv_usec /= 100000; /* actually 1/10 s, not usec now */
2199 diff = (now.tv_sec - tp->tv_sec) * 10 + (now.tv_usec - tp->tv_usec);
2203 putc(diff >> 24, f);
2204 putc(diff >> 16, f);
2217 fwrite(buf, nb, 1, f);
2221 error("Error writing record file: %m");