2 * tty.c - code for handling serial ports in pppd.
4 * Copyright (C) 2000 Paul Mackerras.
7 * Portions Copyright (c) 1989 Carnegie Mellon University.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that the above copyright notice and this paragraph are
12 * duplicated in all such forms and that any documentation,
13 * advertising materials, and other materials related to such
14 * distribution and use acknowledge that the software was developed
15 * by Carnegie Mellon University. The name of the
16 * University may not be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 #define RCSID "$Id: tty.c,v 1.6 2001/03/12 22:59:01 paulus Exp $"
38 #include <sys/param.h>
39 #include <sys/types.h>
42 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
52 void tty_process_extra_options __P((void));
53 void tty_check_options __P((void));
54 int connect_tty __P((void));
55 void disconnect_tty __P((void));
56 void tty_close_fds __P((void));
57 void cleanup_tty __P((void));
58 void tty_do_send_config __P((int, u_int32_t, int, int));
60 static int setdevname __P((char *, char **, int));
61 static int setspeed __P((char *, char **, int));
62 static int setxonxoff __P((char **));
63 static int setescape __P((char **));
64 static void printescape __P((option_t *, void (*)(void *, char *,...),void *));
65 static void finish_tty __P((void));
66 static int start_charshunt __P((int, int));
67 static void stop_charshunt __P((void *, int));
68 static void charshunt_done __P((void *));
69 static void charshunt __P((int, int, char *));
70 static int record_write __P((FILE *, int code, u_char *buf, int nb,
72 static int open_socket __P((char *));
73 static void maybe_relock __P((void *, int));
75 static int pty_master; /* fd for master side of pty */
76 static int pty_slave; /* fd for slave side of pty */
77 static int real_ttyfd; /* fd for actual serial port (not pty) */
78 static int ttyfd; /* Serial port file descriptor */
79 static char speed_str[16]; /* Serial port speed as string */
81 mode_t tty_mode = (mode_t)-1; /* Original access permissions to tty */
82 int baud_rate; /* Actual bits/second for serial device */
83 char *callback_script; /* script for doing callback */
84 int charshunt_pid; /* Process ID for charshunt */
85 int locked; /* lock() has succeeded */
86 struct stat devstat; /* result of stat() on devnam */
88 /* option variables */
89 int crtscts = 0; /* Use hardware flow control */
90 bool modem = 1; /* Use modem control lines */
91 int inspeed = 0; /* Input/Output speed requested */
92 bool lockflag = 0; /* Create lock file to lock the serial dev */
93 char *initializer = NULL; /* Script to initialize physical link */
94 char *connect_script = NULL; /* Script to establish physical link */
95 char *disconnect_script = NULL; /* Script to disestablish physical link */
96 char *welcomer = NULL; /* Script to run after phys link estab. */
97 char *ptycommand = NULL; /* Command to run on other side of pty */
98 bool notty = 0; /* Stdin/out is not a tty */
99 char *record_file = NULL; /* File to record chars sent/received */
100 int max_data_rate; /* max bytes/sec through charshunt */
101 bool sync_serial = 0; /* Device is synchronous serial device */
102 char *pty_socket = NULL; /* Socket to connect to pty */
103 int using_pty = 0; /* we're allocating a pty as the device */
106 extern int kill_link;
109 extern int privopen; /* don't lock, open device as root */
111 u_int32_t xmit_accm[8]; /* extended transmit ACCM */
113 /* option descriptors */
114 option_t tty_options[] = {
115 /* device name must be first, or change connect_tty() below! */
116 { "device name", o_wild, (void *) &setdevname,
117 "Serial port device name",
118 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
121 { "tty speed", o_wild, (void *) &setspeed,
122 "Baud rate for serial port",
123 OPT_PRIO | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC, speed_str },
125 { "lock", o_bool, &lockflag,
126 "Lock serial device with UUCP-style lock file", OPT_PRIO | 1 },
127 { "nolock", o_bool, &lockflag,
128 "Don't lock serial device", OPT_PRIOSUB | OPT_PRIV },
130 { "init", o_string, &initializer,
131 "A program to initialize the device", OPT_PRIO | OPT_PRIVFIX },
133 { "connect", o_string, &connect_script,
134 "A program to set up a connection", OPT_PRIO | OPT_PRIVFIX },
136 { "disconnect", o_string, &disconnect_script,
137 "Program to disconnect serial device", OPT_PRIO | OPT_PRIVFIX },
139 { "welcome", o_string, &welcomer,
140 "Script to welcome client", OPT_PRIO | OPT_PRIVFIX },
142 { "pty", o_string, &ptycommand,
143 "Script to run on pseudo-tty master side",
144 OPT_PRIO | OPT_PRIVFIX | OPT_DEVNAM },
146 { "notty", o_bool, ¬ty,
147 "Input/output is not a tty", OPT_DEVNAM | 1 },
149 { "socket", o_string, &pty_socket,
150 "Send and receive over socket, arg is host:port",
151 OPT_PRIO | OPT_DEVNAM },
153 { "record", o_string, &record_file,
154 "Record characters sent/received to file", OPT_PRIO },
156 { "crtscts", o_int, &crtscts,
157 "Set hardware (RTS/CTS) flow control",
158 OPT_PRIO | OPT_NOARG | OPT_VAL(1) },
159 { "cdtrcts", o_int, &crtscts,
160 "Set alternate hardware (DTR/CTS) flow control",
161 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(2) },
162 { "nocrtscts", o_int, &crtscts,
163 "Disable hardware flow control",
164 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
165 { "-crtscts", o_int, &crtscts,
166 "Disable hardware flow control",
167 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
168 { "nocdtrcts", o_int, &crtscts,
169 "Disable hardware flow control",
170 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
171 { "xonxoff", o_special_noarg, (void *)setxonxoff,
172 "Set software (XON/XOFF) flow control", OPT_PRIOSUB },
174 { "modem", o_bool, &modem,
175 "Use modem control lines", OPT_PRIO | 1 },
176 { "local", o_bool, &modem,
177 "Don't use modem control lines", OPT_PRIOSUB | 0 },
179 { "sync", o_bool, &sync_serial,
180 "Use synchronous HDLC serial encoding", 1 },
182 { "datarate", o_int, &max_data_rate,
183 "Maximum data rate in bytes/sec (with pty, notty or record option)",
186 { "escape", o_special, (void *)setescape,
187 "List of character codes to escape on transmission",
188 OPT_A2PRINTER, (void *)printescape },
194 struct channel tty_channel = {
196 &tty_process_extra_options,
201 &tty_disestablish_ppp,
209 * setspeed - Set the serial port baud rate.
210 * If doit is 0, the call is to check whether this option is
211 * potentially a speed value.
214 setspeed(arg, argv, doit)
222 spd = strtol(arg, &ptr, 0);
223 if (ptr == arg || *ptr != 0 || spd == 0)
227 slprintf(speed_str, sizeof(speed_str), "%d", spd);
234 * setdevname - Set the device name.
235 * If doit is 0, the call is to check whether this option is
236 * potentially a device name.
239 setdevname(cp, argv, doit)
245 char dev[MAXPATHLEN];
250 if (strncmp("/dev/", cp, 5) != 0) {
251 strlcpy(dev, "/dev/", sizeof(dev));
252 strlcat(dev, cp, sizeof(dev));
257 * Check if there is a character device by this name.
259 if (stat(cp, &statbuf) < 0) {
261 return errno != ENOENT;
262 option_error("Couldn't stat %s: %m", cp);
265 if (!S_ISCHR(statbuf.st_mode)) {
267 option_error("%s is not a character device", cp);
272 strlcpy(devnam, cp, sizeof(devnam));
284 lcp_wantoptions[0].asyncmap |= 0x000A0000; /* escape ^S and ^Q */
285 lcp_wantoptions[0].neg_asyncmap = 1;
292 * setescape - add chars to the set we escape on transmission.
304 n = strtol(p, &endp, 16);
306 option_error("escape parameter contains invalid hex number '%s'",
311 if (n < 0 || n == 0x5E || n > 0xFF) {
312 option_error("can't escape character 0x%x", n);
315 xmit_accm[n >> 5] |= 1 << (n & 0x1F);
316 while (*p == ',' || *p == ' ')
319 lcp_allowoptions[0].asyncmap = xmit_accm[0];
324 printescape(opt, printer, arg)
326 void (*printer) __P((void *, char *, ...));
332 for (n = 0; n < 256; ++n) {
334 n += 2; /* skip 7d, 7e */
335 if (xmit_accm[n >> 5] & (1 << (n & 0x1f))) {
340 printer(arg, "%x", n);
344 printer(arg, "oops # nothing escaped");
348 * tty_init - do various tty-related initializations.
352 add_notifier(&pidchange, maybe_relock, 0);
353 the_channel = &tty_channel;
354 xmit_accm[3] = 0x60000000;
358 * tty_process_extra_options - work out which tty device we are using
359 * and read its options file.
361 void tty_process_extra_options()
363 using_pty = notty || ptycommand != NULL || pty_socket != NULL;
366 if (default_device) {
368 if (!isatty(0) || (p = ttyname(0)) == NULL) {
369 option_error("no device specified and stdin is not a tty");
370 exit(EXIT_OPTION_ERROR);
372 strlcpy(devnam, p, sizeof(devnam));
373 if (stat(devnam, &devstat) < 0)
374 fatal("Couldn't stat default device %s: %m", devnam);
379 * Parse the tty options file.
380 * The per-tty options file should not change
381 * ptycommand, pty_socket, notty or devnam.
382 * options_for_tty doesn't override options set on the command line,
383 * except for some privileged options.
385 if (!options_for_tty())
386 exit(EXIT_OPTION_ERROR);
390 * tty_check_options - do consistency checks on the options we were given.
398 if (demand && connect_script == 0) {
399 option_error("connect script is required for demand-dialling\n");
400 exit(EXIT_OPTION_ERROR);
402 /* default holdoff to 0 if no connect script has been given */
403 if (connect_script == 0 && !holdoff_specified)
407 if (!default_device) {
408 option_error("%s option precludes specifying device name",
409 notty? "notty": "pty");
410 exit(EXIT_OPTION_ERROR);
412 if (ptycommand != NULL && notty) {
413 option_error("pty option is incompatible with notty option");
414 exit(EXIT_OPTION_ERROR);
416 if (pty_socket != NULL && (ptycommand != NULL || notty)) {
417 option_error("socket option is incompatible with pty and notty");
418 exit(EXIT_OPTION_ERROR);
420 default_device = notty;
423 if (notty && log_to_fd <= 1)
427 * If the user has specified a device which is the same as
428 * the one on stdin, pretend they didn't specify any.
429 * If the device is already open read/write on stdin,
430 * we assume we don't need to lock it, and we can open it
433 if (fstat(0, &statbuf) >= 0 && S_ISCHR(statbuf.st_mode)
434 && statbuf.st_rdev == devstat.st_rdev) {
436 fdflags = fcntl(0, F_GETFL);
437 if (fdflags != -1 && (fdflags & O_ACCMODE) == O_RDWR)
445 * Don't send log messages to the serial port, it tends to
446 * confuse the peer. :-)
448 if (log_to_fd >= 0 && fstat(log_to_fd, &statbuf) >= 0
449 && S_ISCHR(statbuf.st_mode) && statbuf.st_rdev == devstat.st_rdev)
454 * connect_tty - get the serial port ready to start doing PPP.
455 * That is, open the serial port, set its speed and mode, and run
456 * the connector and/or welcomer.
466 * Get a pty master/slave pair if the pty, notty, socket,
467 * or record options were specified.
469 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
473 if (using_pty || record_file != NULL) {
474 if (!get_pty(&pty_master, &pty_slave, ppp_devnam, uid)) {
475 error("Couldn't allocate pseudo-tty");
476 status = EXIT_FATAL_ERROR;
479 set_up_tty(pty_slave, 1);
483 * Lock the device if we've been asked to.
485 status = EXIT_LOCK_FAILED;
486 if (lockflag && !privopen) {
487 if (lock(devnam) < 0)
493 * Open the serial device and set it up to be the ppp interface.
494 * First we open it in non-blocking mode so we can set the
495 * various termios flags appropriately. If we aren't dialling
496 * out and we want to use the modem lines, we reopen it later
497 * in order to wait for the carrier detect signal from the modem.
501 connector = doing_callback? callback_script: connect_script;
502 if (devnam[0] != 0) {
504 /* If the user specified the device name, become the
505 user before opening it. */
508 prio = privopen? OPRIO_ROOT: tty_options[0].priority;
509 if (prio < OPRIO_ROOT)
511 ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0);
513 if (prio < OPRIO_ROOT)
519 error("Failed to open %s: %m", devnam);
520 status = EXIT_OPEN_FAILED;
522 if (!persist || err != EINTR)
526 if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1
527 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
528 warn("Couldn't reset non-blocking mode on device: %m");
531 * Do the equivalent of `mesg n' to stop broadcast messages.
533 if (fstat(ttyfd, &statbuf) < 0
534 || fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) {
535 warn("Couldn't restrict write permissions to %s: %m", devnam);
537 tty_mode = statbuf.st_mode;
540 * Set line speed, flow control, etc.
541 * If we have a non-null connection or initializer script,
542 * on most systems we set CLOCAL for now so that we can talk
543 * to the modem before carrier comes up. But this has the
544 * side effect that we might miss it if CD drops before we
545 * get to clear CLOCAL below. On systems where we can talk
546 * successfully to the modem with CLOCAL clear and CD down,
547 * we could clear CLOCAL at this point.
549 set_up_tty(ttyfd, ((connector != NULL && connector[0] != 0)
550 || initializer != NULL));
554 * If the pty, socket, notty and/or record option was specified,
555 * start up the character shunt now.
557 status = EXIT_PTYCMD_FAILED;
558 if (ptycommand != NULL) {
559 if (record_file != NULL) {
560 int ipipe[2], opipe[2], ok;
562 if (pipe(ipipe) < 0 || pipe(opipe) < 0)
563 fatal("Couldn't create pipes for record option: %m");
564 ok = device_script(ptycommand, opipe[0], ipipe[1], 1) == 0
565 && start_charshunt(ipipe[0], opipe[1]);
573 if (device_script(ptycommand, pty_master, pty_master, 1) < 0)
579 } else if (pty_socket != NULL) {
580 int fd = open_socket(pty_socket);
583 if (!start_charshunt(fd, fd))
586 if (!start_charshunt(0, 1))
588 } else if (record_file != NULL) {
589 if (!start_charshunt(ttyfd, ttyfd))
593 /* run connection script */
594 if ((connector && connector[0]) || initializer) {
595 if (real_ttyfd != -1) {
596 /* XXX do this if doing_callback == CALLBACK_DIALIN? */
597 if (!default_device && modem) {
598 setdtr(real_ttyfd, 0); /* in case modem is off hook */
600 setdtr(real_ttyfd, 1);
604 if (initializer && initializer[0]) {
605 if (device_script(initializer, ttyfd, ttyfd, 0) < 0) {
606 error("Initializer script failed");
607 status = EXIT_INIT_FAILED;
614 info("Serial port initialized.");
617 if (connector && connector[0]) {
618 if (device_script(connector, ttyfd, ttyfd, 0) < 0) {
619 error("Connect script failed");
620 status = EXIT_CONNECT_FAILED;
627 info("Serial connection established.");
630 /* set line speed, flow control, etc.;
631 clear CLOCAL if modem option */
632 if (real_ttyfd != -1)
633 set_up_tty(real_ttyfd, 0);
635 if (doing_callback == CALLBACK_DIALIN)
639 /* reopen tty if necessary to wait for carrier */
640 if (connector == NULL && modem && devnam[0] != 0) {
643 if ((i = open(devnam, O_RDWR)) >= 0)
645 if (errno != EINTR) {
646 error("Failed to reopen %s: %m", devnam);
647 status = EXIT_OPEN_FAILED;
649 if (!persist || errno != EINTR || hungup || kill_link)
655 slprintf(numbuf, sizeof(numbuf), "%d", baud_rate);
656 script_setenv("SPEED", numbuf, 0);
658 /* run welcome script, if any */
659 if (welcomer && welcomer[0]) {
660 if (device_script(welcomer, ttyfd, ttyfd, 0) < 0)
661 warn("Welcome script failed");
665 * If we are initiating this connection, wait for a short
666 * time for something from the peer. This can avoid bouncing
667 * our packets off his tty before he has it set up.
669 if (connector != NULL || ptycommand != NULL)
670 listen_time = connect_delay;
676 void disconnect_tty()
678 if (disconnect_script == NULL || hungup)
681 set_up_tty(real_ttyfd, 1);
682 if (device_script(disconnect_script, ttyfd, ttyfd, 0) < 0) {
683 warn("disconnect script failed");
685 info("Serial link disconnected.");
695 if (real_ttyfd >= 0) {
699 /* N.B. ttyfd will == either pty_slave or real_ttyfd */
714 * tty_do_send_config - set transmit-side PPP configuration.
715 * We set the extended transmit ACCM here as well.
718 tty_do_send_config(mtu, accm, pcomp, accomp)
723 tty_set_xaccm(xmit_accm);
724 tty_send_config(mtu, accm, pcomp, accomp);
728 * finish_tty - restore the terminal device to its original settings
733 /* drop dtr to hang up */
734 if (!default_device && modem) {
735 setdtr(real_ttyfd, 0);
737 * This sleep is in case the serial port has CLOCAL set by default,
738 * and consequently will reassert DTR when we close the device.
743 restore_tty(real_ttyfd);
745 if (tty_mode != (mode_t) -1) {
746 if (fchmod(real_ttyfd, tty_mode) != 0) {
747 /* XXX if devnam is a symlink, this will change the link */
748 chmod(devnam, tty_mode);
757 * maybe_relock - our PID has changed, maybe update the lock file.
760 maybe_relock(arg, pid)
769 * open_socket - establish a stream socket connection to the nominated
776 char *sep, *endp = NULL;
779 struct hostent *hent;
780 struct sockaddr_in sad;
782 /* parse host:port and resolve host to an IP address */
783 sep = strchr(dest, ':');
785 port = strtol(sep+1, &endp, 10);
786 if (port < 0 || endp == sep+1 || sep == dest) {
787 error("Can't parse host:port for socket destination");
791 host = inet_addr(dest);
792 if (host == (u_int32_t) -1) {
793 hent = gethostbyname(dest);
795 error("%s: unknown host in socket option", dest);
799 host = *(u_int32_t *)(hent->h_addr_list[0]);
803 /* get a socket and connect it to the other end */
804 sock = socket(PF_INET, SOCK_STREAM, 0);
806 error("Can't create socket: %m");
809 memset(&sad, 0, sizeof(sad));
810 sad.sin_family = AF_INET;
811 sad.sin_port = htons(port);
812 sad.sin_addr.s_addr = host;
813 if (connect(sock, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
814 error("Can't connect to %s: %m", dest);
824 * start_charshunt - create a child process to run the character shunt.
827 start_charshunt(ifd, ofd)
834 error("Can't fork process for character shunt: %m");
842 fatal("setuid failed");
846 charshunt(ifd, ofd, record_file);
849 charshunt_pid = cpid;
850 add_notifier(&sigreceived, stop_charshunt, 0);
854 record_child(cpid, "pppd (charshunt)", charshunt_done, NULL);
866 stop_charshunt(arg, sig)
871 kill(charshunt_pid, (sig == SIGINT? sig: SIGTERM));
875 * charshunt - the character shunt, which passes characters between
876 * the pty master side and the serial port (or stdin/stdout).
877 * This runs as the user (not as root).
878 * (We assume ofd >= ifd which is true the way this gets called. :-).
881 charshunt(ifd, ofd, record_file)
886 fd_set ready, writey;
887 u_char *ibufp, *obufp;
890 int pty_readable, stdin_readable;
891 struct timeval lasttime;
892 FILE *recordf = NULL;
893 int ilevel, olevel, max_level;
894 struct timeval levelt, tout, *top;
895 extern u_char inpacket_buf[];
898 * Reset signal handlers.
900 signal(SIGHUP, SIG_IGN); /* Hangup */
901 signal(SIGINT, SIG_DFL); /* Interrupt */
902 signal(SIGTERM, SIG_DFL); /* Terminate */
903 signal(SIGCHLD, SIG_DFL);
904 signal(SIGUSR1, SIG_DFL);
905 signal(SIGUSR2, SIG_DFL);
906 signal(SIGABRT, SIG_DFL);
907 signal(SIGALRM, SIG_DFL);
908 signal(SIGFPE, SIG_DFL);
909 signal(SIGILL, SIG_DFL);
910 signal(SIGPIPE, SIG_DFL);
911 signal(SIGQUIT, SIG_DFL);
912 signal(SIGSEGV, SIG_DFL);
914 signal(SIGBUS, SIG_DFL);
917 signal(SIGEMT, SIG_DFL);
920 signal(SIGPOLL, SIG_DFL);
923 signal(SIGPROF, SIG_DFL);
926 signal(SIGSYS, SIG_DFL);
929 signal(SIGTRAP, SIG_DFL);
932 signal(SIGVTALRM, SIG_DFL);
935 signal(SIGXCPU, SIG_DFL);
938 signal(SIGXFSZ, SIG_DFL);
942 * Open the record file if required.
944 if (record_file != NULL) {
945 recordf = fopen(record_file, "a");
947 error("Couldn't create record file %s: %m", record_file);
950 /* set all the fds to non-blocking mode */
951 flags = fcntl(pty_master, F_GETFL);
953 || fcntl(pty_master, F_SETFL, flags | O_NONBLOCK) == -1)
954 warn("couldn't set pty master to nonblock: %m");
955 flags = fcntl(ifd, F_GETFL);
957 || fcntl(ifd, F_SETFL, flags | O_NONBLOCK) == -1)
958 warn("couldn't set %s to nonblock: %m", (ifd==0? "stdin": "tty"));
960 flags = fcntl(ofd, F_GETFL);
962 || fcntl(ofd, F_SETFL, flags | O_NONBLOCK) == -1)
963 warn("couldn't set stdout to nonblock: %m");
967 ibufp = obufp = NULL;
968 pty_readable = stdin_readable = 1;
971 gettimeofday(&levelt, NULL);
973 max_level = max_data_rate / 10;
977 max_level = PPP_MRU + PPP_HDRLEN + 1;
979 nfds = (ofd > pty_master? ofd: pty_master) + 1;
980 if (recordf != NULL) {
981 gettimeofday(&lasttime, NULL);
982 putc(7, recordf); /* put start marker */
983 putc(lasttime.tv_sec >> 24, recordf);
984 putc(lasttime.tv_sec >> 16, recordf);
985 putc(lasttime.tv_sec >> 8, recordf);
986 putc(lasttime.tv_sec, recordf);
987 lasttime.tv_usec = 0;
990 while (nibuf != 0 || nobuf != 0 || pty_readable || stdin_readable) {
993 tout.tv_usec = 10000;
997 if (ilevel >= max_level)
1000 FD_SET(pty_master, &writey);
1001 } else if (stdin_readable)
1002 FD_SET(ifd, &ready);
1004 if (olevel >= max_level)
1007 FD_SET(ofd, &writey);
1008 } else if (pty_readable)
1009 FD_SET(pty_master, &ready);
1010 if (select(nfds, &ready, &writey, NULL, top) < 0) {
1015 if (max_data_rate) {
1020 gettimeofday(&now, NULL);
1021 dt = (now.tv_sec - levelt.tv_sec
1022 + (now.tv_usec - levelt.tv_usec) / 1e6);
1023 nbt = (int)(dt * max_data_rate);
1024 ilevel = (nbt < 0 || nbt > ilevel)? 0: ilevel - nbt;
1025 olevel = (nbt < 0 || nbt > olevel)? 0: olevel - nbt;
1028 ilevel = olevel = 0;
1029 if (FD_ISSET(ifd, &ready)) {
1030 ibufp = inpacket_buf;
1031 nibuf = read(ifd, ibufp, PPP_MRU + PPP_HDRLEN);
1032 if (nibuf < 0 && errno == EIO)
1035 if (!(errno == EINTR || errno == EAGAIN)) {
1036 error("Error reading standard input: %m");
1040 } else if (nibuf == 0) {
1041 /* end of file from stdin */
1043 /* do a 0-length write, hopefully this will generate
1044 an EOF (hangup) on the slave side. */
1045 write(pty_master, inpacket_buf, 0);
1047 if (!record_write(recordf, 4, NULL, 0, &lasttime))
1050 FD_SET(pty_master, &writey);
1052 if (!record_write(recordf, 2, ibufp, nibuf, &lasttime))
1056 if (FD_ISSET(pty_master, &ready)) {
1057 obufp = outpacket_buf;
1058 nobuf = read(pty_master, obufp, PPP_MRU + PPP_HDRLEN);
1059 if (nobuf < 0 && errno == EIO)
1062 if (!(errno == EINTR || errno == EAGAIN)) {
1063 error("Error reading pseudo-tty master: %m");
1067 } else if (nobuf == 0) {
1068 /* end of file from the pty - slave side has closed */
1070 stdin_readable = 0; /* pty is not writable now */
1074 if (!record_write(recordf, 3, NULL, 0, &lasttime))
1077 FD_SET(ofd, &writey);
1079 if (!record_write(recordf, 1, obufp, nobuf, &lasttime))
1083 if (FD_ISSET(ofd, &writey)) {
1085 if (olevel + n > max_level)
1086 n = max_level - olevel;
1087 n = write(ofd, obufp, n);
1092 } else if (errno != EAGAIN && errno != EINTR) {
1093 error("Error writing standard output: %m");
1102 if (FD_ISSET(pty_master, &writey)) {
1104 if (ilevel + n > max_level)
1105 n = max_level - ilevel;
1106 n = write(pty_master, ibufp, n);
1111 } else if (errno != EAGAIN && errno != EINTR) {
1112 error("Error writing pseudo-tty master: %m");
1126 record_write(f, code, buf, nb, tp)
1136 gettimeofday(&now, NULL);
1137 now.tv_usec /= 100000; /* actually 1/10 s, not usec now */
1138 diff = (now.tv_sec - tp->tv_sec) * 10 + (now.tv_usec - tp->tv_usec);
1142 putc(diff >> 24, f);
1143 putc(diff >> 16, f);
1156 fwrite(buf, nb, 1, f);
1160 error("Error writing record file: %m");