2 * tty.c - code for handling serial ports in pppd.
4 * Copyright (C) 2000-2004 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. The name(s) of the authors of this software must not be used to
14 * endorse or promote products derived from this software without
15 * prior written permission.
17 * 3. Redistributions of any form whatsoever must retain the following
19 * "This product includes software developed by Paul Mackerras
20 * <paulus@samba.org>".
22 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 * Portions derived from main.c, which is:
32 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
46 * 3. The name "Carnegie Mellon University" must not be used to
47 * endorse or promote products derived from this software without
48 * prior written permission. For permission or any legal
49 * details, please contact
50 * Office of Technology Transfer
51 * Carnegie Mellon University
53 * Pittsburgh, PA 15213-3890
54 * (412) 268-4387, fax: (412) 268-7395
55 * tech-transfer@andrew.cmu.edu
57 * 4. Redistributions of any form whatsoever must retain the following
59 * "This product includes software developed by Computing Services
60 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
62 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
63 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
65 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
67 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
68 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 #define RCSID "$Id: tty.c,v 1.23 2004/12/31 11:49:22 paulus Exp $"
86 #include <sys/param.h>
87 #include <sys/types.h>
90 #include <sys/resource.h>
92 #include <sys/socket.h>
93 #include <netinet/in.h>
94 #include <arpa/inet.h>
100 void tty_process_extra_options
__P((void));
101 void tty_check_options
__P((void));
102 int connect_tty
__P((void));
103 void disconnect_tty
__P((void));
104 void tty_close_fds
__P((void));
105 void cleanup_tty
__P((void));
106 void tty_do_send_config
__P((int, u_int32_t
, int, int));
108 static int setdevname
__P((char *, char **, int));
109 static int setspeed
__P((char *, char **, int));
110 static int setxonxoff
__P((char **));
111 static int setescape
__P((char **));
112 static void printescape
__P((option_t
*, void (*)(void *, char *,...),void *));
113 static void finish_tty
__P((void));
114 static int start_charshunt
__P((int, int));
115 static void stop_charshunt
__P((void *, int));
116 static void charshunt_done
__P((void *));
117 static void charshunt
__P((int, int, char *));
118 static int record_write
__P((FILE *, int code
, u_char
*buf
, int nb
,
120 static int open_socket
__P((char *));
121 static void maybe_relock
__P((void *, int));
123 static int pty_master
; /* fd for master side of pty */
124 static int pty_slave
; /* fd for slave side of pty */
125 static int real_ttyfd
; /* fd for actual serial port (not pty) */
126 static int ttyfd
; /* Serial port file descriptor */
127 static char speed_str
[16]; /* Serial port speed as string */
129 mode_t tty_mode
= (mode_t
)-1; /* Original access permissions to tty */
130 int baud_rate
; /* Actual bits/second for serial device */
131 char *callback_script
; /* script for doing callback */
132 int charshunt_pid
; /* Process ID for charshunt */
133 int locked
; /* lock() has succeeded */
134 struct stat devstat
; /* result of stat() on devnam */
136 /* option variables */
137 int crtscts
= 0; /* Use hardware flow control */
138 bool modem
= 1; /* Use modem control lines */
139 int inspeed
= 0; /* Input/Output speed requested */
140 bool lockflag
= 0; /* Create lock file to lock the serial dev */
141 char *initializer
= NULL
; /* Script to initialize physical link */
142 char *connect_script
= NULL
; /* Script to establish physical link */
143 char *disconnect_script
= NULL
; /* Script to disestablish physical link */
144 char *welcomer
= NULL
; /* Script to run after phys link estab. */
145 char *ptycommand
= NULL
; /* Command to run on other side of pty */
146 bool notty
= 0; /* Stdin/out is not a tty */
147 char *record_file
= NULL
; /* File to record chars sent/received */
148 int max_data_rate
; /* max bytes/sec through charshunt */
149 bool sync_serial
= 0; /* Device is synchronous serial device */
150 char *pty_socket
= NULL
; /* Socket to connect to pty */
151 int using_pty
= 0; /* we're allocating a pty as the device */
154 extern int kill_link
;
155 extern int asked_to_quit
;
156 extern int got_sigterm
;
159 extern int privopen
; /* don't lock, open device as root */
161 u_int32_t xmit_accm
[8]; /* extended transmit ACCM */
163 /* option descriptors */
164 option_t tty_options
[] = {
165 /* device name must be first, or change connect_tty() below! */
166 { "device name", o_wild
, (void *) &setdevname
,
167 "Serial port device name",
168 OPT_DEVNAM
| OPT_PRIVFIX
| OPT_NOARG
| OPT_A2STRVAL
| OPT_STATIC
,
171 { "tty speed", o_wild
, (void *) &setspeed
,
172 "Baud rate for serial port",
173 OPT_PRIO
| OPT_NOARG
| OPT_A2STRVAL
| OPT_STATIC
, speed_str
},
175 { "lock", o_bool
, &lockflag
,
176 "Lock serial device with UUCP-style lock file", OPT_PRIO
| 1 },
177 { "nolock", o_bool
, &lockflag
,
178 "Don't lock serial device", OPT_PRIOSUB
| OPT_PRIV
},
180 { "init", o_string
, &initializer
,
181 "A program to initialize the device", OPT_PRIO
| OPT_PRIVFIX
},
183 { "connect", o_string
, &connect_script
,
184 "A program to set up a connection", OPT_PRIO
| OPT_PRIVFIX
},
186 { "disconnect", o_string
, &disconnect_script
,
187 "Program to disconnect serial device", OPT_PRIO
| OPT_PRIVFIX
},
189 { "welcome", o_string
, &welcomer
,
190 "Script to welcome client", OPT_PRIO
| OPT_PRIVFIX
},
192 { "pty", o_string
, &ptycommand
,
193 "Script to run on pseudo-tty master side",
194 OPT_PRIO
| OPT_PRIVFIX
| OPT_DEVNAM
},
196 { "notty", o_bool
, ¬ty
,
197 "Input/output is not a tty", OPT_DEVNAM
| 1 },
199 { "socket", o_string
, &pty_socket
,
200 "Send and receive over socket, arg is host:port",
201 OPT_PRIO
| OPT_DEVNAM
},
203 { "record", o_string
, &record_file
,
204 "Record characters sent/received to file", OPT_PRIO
},
206 { "crtscts", o_int
, &crtscts
,
207 "Set hardware (RTS/CTS) flow control",
208 OPT_PRIO
| OPT_NOARG
| OPT_VAL(1) },
209 { "cdtrcts", o_int
, &crtscts
,
210 "Set alternate hardware (DTR/CTS) flow control",
211 OPT_PRIOSUB
| OPT_NOARG
| OPT_VAL(2) },
212 { "nocrtscts", o_int
, &crtscts
,
213 "Disable hardware flow control",
214 OPT_PRIOSUB
| OPT_NOARG
| OPT_VAL(-1) },
215 { "-crtscts", o_int
, &crtscts
,
216 "Disable hardware flow control",
217 OPT_PRIOSUB
| OPT_ALIAS
| OPT_NOARG
| OPT_VAL(-1) },
218 { "nocdtrcts", o_int
, &crtscts
,
219 "Disable hardware flow control",
220 OPT_PRIOSUB
| OPT_ALIAS
| OPT_NOARG
| OPT_VAL(-1) },
221 { "xonxoff", o_special_noarg
, (void *)setxonxoff
,
222 "Set software (XON/XOFF) flow control", OPT_PRIOSUB
},
224 { "modem", o_bool
, &modem
,
225 "Use modem control lines", OPT_PRIO
| 1 },
226 { "local", o_bool
, &modem
,
227 "Don't use modem control lines", OPT_PRIOSUB
| 0 },
229 { "sync", o_bool
, &sync_serial
,
230 "Use synchronous HDLC serial encoding", 1 },
232 { "datarate", o_int
, &max_data_rate
,
233 "Maximum data rate in bytes/sec (with pty, notty or record option)",
236 { "escape", o_special
, (void *)setescape
,
237 "List of character codes to escape on transmission",
238 OPT_A2PRINTER
, (void *)printescape
},
244 struct channel tty_channel
= {
246 &tty_process_extra_options
,
251 &tty_disestablish_ppp
,
259 * setspeed - Set the serial port baud rate.
260 * If doit is 0, the call is to check whether this option is
261 * potentially a speed value.
264 setspeed(arg
, argv
, doit
)
272 spd
= strtol(arg
, &ptr
, 0);
273 if (ptr
== arg
|| *ptr
!= 0 || spd
== 0)
277 slprintf(speed_str
, sizeof(speed_str
), "%d", spd
);
284 * setdevname - Set the device name.
285 * If doit is 0, the call is to check whether this option is
286 * potentially a device name.
289 setdevname(cp
, argv
, doit
)
295 char dev
[MAXPATHLEN
];
301 strlcpy(dev
, "/dev/", sizeof(dev
));
302 strlcat(dev
, cp
, sizeof(dev
));
307 * Check if there is a character device by this name.
309 if (stat(cp
, &statbuf
) < 0) {
311 return errno
!= ENOENT
;
312 option_error("Couldn't stat %s: %m", cp
);
315 if (!S_ISCHR(statbuf
.st_mode
)) {
317 option_error("%s is not a character device", cp
);
322 strlcpy(devnam
, cp
, sizeof(devnam
));
334 lcp_wantoptions
[0].asyncmap
|= 0x000A0000; /* escape ^S and ^Q */
335 lcp_wantoptions
[0].neg_asyncmap
= 1;
342 * setescape - add chars to the set we escape on transmission.
354 n
= strtol(p
, &endp
, 16);
356 option_error("escape parameter contains invalid hex number '%s'",
361 if (n
< 0 || n
== 0x5E || n
> 0xFF) {
362 option_error("can't escape character 0x%x", n
);
365 xmit_accm
[n
>> 5] |= 1 << (n
& 0x1F);
366 while (*p
== ',' || *p
== ' ')
369 lcp_allowoptions
[0].asyncmap
= xmit_accm
[0];
374 printescape(opt
, printer
, arg
)
376 void (*printer
) __P((void *, char *, ...));
382 for (n
= 0; n
< 256; ++n
) {
384 n
+= 2; /* skip 7d, 7e */
385 if (xmit_accm
[n
>> 5] & (1 << (n
& 0x1f))) {
390 printer(arg
, "%x", n
);
394 printer(arg
, "oops # nothing escaped");
398 * tty_init - do various tty-related initializations.
402 add_notifier(&pidchange
, maybe_relock
, 0);
403 the_channel
= &tty_channel
;
404 xmit_accm
[3] = 0x60000000;
408 * tty_process_extra_options - work out which tty device we are using
409 * and read its options file.
411 void tty_process_extra_options()
413 using_pty
= notty
|| ptycommand
!= NULL
|| pty_socket
!= NULL
;
416 if (default_device
) {
418 if (!isatty(0) || (p
= ttyname(0)) == NULL
) {
419 option_error("no device specified and stdin is not a tty");
420 exit(EXIT_OPTION_ERROR
);
422 strlcpy(devnam
, p
, sizeof(devnam
));
423 if (stat(devnam
, &devstat
) < 0)
424 fatal("Couldn't stat default device %s: %m", devnam
);
429 * Parse the tty options file.
430 * The per-tty options file should not change
431 * ptycommand, pty_socket, notty or devnam.
432 * options_for_tty doesn't override options set on the command line,
433 * except for some privileged options.
435 if (!options_for_tty())
436 exit(EXIT_OPTION_ERROR
);
440 * tty_check_options - do consistency checks on the options we were given.
448 if (demand
&& notty
) {
449 option_error("demand-dialling is incompatible with notty");
450 exit(EXIT_OPTION_ERROR
);
452 if (demand
&& connect_script
== 0 && ptycommand
== NULL
453 && pty_socket
== NULL
) {
454 option_error("connect script is required for demand-dialling\n");
455 exit(EXIT_OPTION_ERROR
);
457 /* default holdoff to 0 if no connect script has been given */
458 if (connect_script
== 0 && !holdoff_specified
)
462 if (!default_device
) {
463 option_error("%s option precludes specifying device name",
464 pty_socket
? "socket": notty
? "notty": "pty");
465 exit(EXIT_OPTION_ERROR
);
467 if (ptycommand
!= NULL
&& notty
) {
468 option_error("pty option is incompatible with notty option");
469 exit(EXIT_OPTION_ERROR
);
471 if (pty_socket
!= NULL
&& (ptycommand
!= NULL
|| notty
)) {
472 option_error("socket option is incompatible with pty and notty");
473 exit(EXIT_OPTION_ERROR
);
475 default_device
= notty
;
478 if (notty
&& log_to_fd
<= 1)
482 * If the user has specified a device which is the same as
483 * the one on stdin, pretend they didn't specify any.
484 * If the device is already open read/write on stdin,
485 * we assume we don't need to lock it, and we can open it
488 if (fstat(0, &statbuf
) >= 0 && S_ISCHR(statbuf
.st_mode
)
489 && statbuf
.st_rdev
== devstat
.st_rdev
) {
491 fdflags
= fcntl(0, F_GETFL
);
492 if (fdflags
!= -1 && (fdflags
& O_ACCMODE
) == O_RDWR
)
500 * Don't send log messages to the serial port, it tends to
501 * confuse the peer. :-)
503 if (log_to_fd
>= 0 && fstat(log_to_fd
, &statbuf
) >= 0
504 && S_ISCHR(statbuf
.st_mode
) && statbuf
.st_rdev
== devstat
.st_rdev
)
509 * connect_tty - get the serial port ready to start doing PPP.
510 * That is, open the serial port, set its speed and mode, and run
511 * the connector and/or welcomer.
523 * Get a pty master/slave pair if the pty, notty, socket,
524 * or record options were specified.
526 strlcpy(ppp_devnam
, devnam
, sizeof(ppp_devnam
));
530 if (using_pty
|| record_file
!= NULL
) {
531 if (!get_pty(&pty_master
, &pty_slave
, ppp_devnam
, uid
)) {
532 error("Couldn't allocate pseudo-tty");
533 status
= EXIT_FATAL_ERROR
;
536 set_up_tty(pty_slave
, 1);
540 * Lock the device if we've been asked to.
542 status
= EXIT_LOCK_FAILED
;
543 if (lockflag
&& !privopen
) {
544 if (lock(devnam
) < 0)
550 * Open the serial device and set it up to be the ppp interface.
551 * First we open it in non-blocking mode so we can set the
552 * various termios flags appropriately. If we aren't dialling
553 * out and we want to use the modem lines, we reopen it later
554 * in order to wait for the carrier detect signal from the modem.
558 connector
= doing_callback
? callback_script
: connect_script
;
559 if (devnam
[0] != 0) {
561 /* If the user specified the device name, become the
562 user before opening it. */
565 prio
= privopen
? OPRIO_ROOT
: tty_options
[0].priority
;
566 if (prio
< OPRIO_ROOT
)
568 real_ttyfd
= open(devnam
, O_NONBLOCK
| O_RDWR
, 0);
570 if (prio
< OPRIO_ROOT
)
576 error("Failed to open %s: %m", devnam
);
577 status
= EXIT_OPEN_FAILED
;
579 if (!persist
|| err
!= EINTR
)
583 if ((fdflags
= fcntl(ttyfd
, F_GETFL
)) == -1
584 || fcntl(ttyfd
, F_SETFL
, fdflags
& ~O_NONBLOCK
) < 0)
585 warn("Couldn't reset non-blocking mode on device: %m");
589 * Linux 2.4 and above blocks normal writes to the tty
590 * when it is in PPP line discipline, so this isn't needed.
593 * Do the equivalent of `mesg n' to stop broadcast messages.
595 if (fstat(ttyfd
, &statbuf
) < 0
596 || fchmod(ttyfd
, statbuf
.st_mode
& ~(S_IWGRP
| S_IWOTH
)) < 0) {
597 warn("Couldn't restrict write permissions to %s: %m", devnam
);
599 tty_mode
= statbuf
.st_mode
;
600 #endif /* __linux__ */
603 * Set line speed, flow control, etc.
604 * If we have a non-null connection or initializer script,
605 * on most systems we set CLOCAL for now so that we can talk
606 * to the modem before carrier comes up. But this has the
607 * side effect that we might miss it if CD drops before we
608 * get to clear CLOCAL below. On systems where we can talk
609 * successfully to the modem with CLOCAL clear and CD down,
610 * we could clear CLOCAL at this point.
612 set_up_tty(ttyfd
, ((connector
!= NULL
&& connector
[0] != 0)
613 || initializer
!= NULL
));
617 * If the pty, socket, notty and/or record option was specified,
618 * start up the character shunt now.
620 status
= EXIT_PTYCMD_FAILED
;
621 if (ptycommand
!= NULL
) {
622 if (record_file
!= NULL
) {
623 int ipipe
[2], opipe
[2], ok
;
625 if (pipe(ipipe
) < 0 || pipe(opipe
) < 0)
626 fatal("Couldn't create pipes for record option: %m");
628 /* don't leak these to the ptycommand */
629 (void) fcntl(ipipe
[0], F_SETFD
, FD_CLOEXEC
);
630 (void) fcntl(opipe
[1], F_SETFD
, FD_CLOEXEC
);
632 ok
= device_script(ptycommand
, opipe
[0], ipipe
[1], 1) == 0
633 && start_charshunt(ipipe
[0], opipe
[1]);
641 if (device_script(ptycommand
, pty_master
, pty_master
, 1) < 0)
644 } else if (pty_socket
!= NULL
) {
645 int fd
= open_socket(pty_socket
);
648 if (!start_charshunt(fd
, fd
))
652 if (!start_charshunt(0, 1))
660 } else if (record_file
!= NULL
) {
662 if (!start_charshunt(fd
, fd
))
666 if (using_pty
|| record_file
!= NULL
) {
672 /* run connection script */
673 if ((connector
&& connector
[0]) || initializer
) {
674 if (real_ttyfd
!= -1) {
675 /* XXX do this if doing_callback == CALLBACK_DIALIN? */
676 if (!default_device
&& modem
) {
677 setdtr(real_ttyfd
, 0); /* in case modem is off hook */
679 setdtr(real_ttyfd
, 1);
683 if (initializer
&& initializer
[0]) {
684 if (device_script(initializer
, ttyfd
, ttyfd
, 0) < 0) {
685 error("Initializer script failed");
686 status
= EXIT_INIT_FAILED
;
693 info("Serial port initialized.");
696 if (connector
&& connector
[0]) {
697 if (device_script(connector
, ttyfd
, ttyfd
, 0) < 0) {
698 error("Connect script failed");
699 status
= EXIT_CONNECT_FAILED
;
706 info("Serial connection established.");
709 /* set line speed, flow control, etc.;
710 clear CLOCAL if modem option */
711 if (real_ttyfd
!= -1)
712 set_up_tty(real_ttyfd
, 0);
714 if (doing_callback
== CALLBACK_DIALIN
)
718 /* reopen tty if necessary to wait for carrier */
719 if (connector
== NULL
&& modem
&& devnam
[0] != 0) {
722 if ((i
= open(devnam
, O_RDWR
)) >= 0)
724 if (errno
!= EINTR
) {
725 error("Failed to reopen %s: %m", devnam
);
726 status
= EXIT_OPEN_FAILED
;
728 if (!persist
|| errno
!= EINTR
|| hungup
|| got_sigterm
)
734 slprintf(numbuf
, sizeof(numbuf
), "%d", baud_rate
);
735 script_setenv("SPEED", numbuf
, 0);
737 /* run welcome script, if any */
738 if (welcomer
&& welcomer
[0]) {
739 if (device_script(welcomer
, ttyfd
, ttyfd
, 0) < 0)
740 warn("Welcome script failed");
744 * If we are initiating this connection, wait for a short
745 * time for something from the peer. This can avoid bouncing
746 * our packets off his tty before he has it set up.
748 if (connector
!= NULL
|| ptycommand
!= NULL
|| pty_socket
!= NULL
)
749 listen_time
= connect_delay
;
754 if (pty_master
>= 0) {
765 void disconnect_tty()
767 if (disconnect_script
== NULL
|| hungup
)
770 set_up_tty(real_ttyfd
, 1);
771 if (device_script(disconnect_script
, ttyfd
, ttyfd
, 0) < 0) {
772 warn("disconnect script failed");
774 info("Serial link disconnected.");
782 if (real_ttyfd
>= 0) {
786 /* N.B. ttyfd will == either pty_slave or real_ttyfd */
801 * tty_do_send_config - set transmit-side PPP configuration.
802 * We set the extended transmit ACCM here as well.
805 tty_do_send_config(mtu
, accm
, pcomp
, accomp
)
810 tty_set_xaccm(xmit_accm
);
811 tty_send_config(mtu
, accm
, pcomp
, accomp
);
815 * finish_tty - restore the terminal device to its original settings
820 /* drop dtr to hang up */
821 if (!default_device
&& modem
) {
822 setdtr(real_ttyfd
, 0);
824 * This sleep is in case the serial port has CLOCAL set by default,
825 * and consequently will reassert DTR when we close the device.
830 restore_tty(real_ttyfd
);
833 if (tty_mode
!= (mode_t
) -1) {
834 if (fchmod(real_ttyfd
, tty_mode
) != 0)
835 error("Couldn't restore tty permissions");
837 #endif /* __linux__ */
844 * maybe_relock - our PID has changed, maybe update the lock file.
847 maybe_relock(arg
, pid
)
856 * open_socket - establish a stream socket connection to the nominated
863 char *sep
, *endp
= NULL
;
866 struct hostent
*hent
;
867 struct sockaddr_in sad
;
869 /* parse host:port and resolve host to an IP address */
870 sep
= strchr(dest
, ':');
872 port
= strtol(sep
+1, &endp
, 10);
873 if (port
< 0 || endp
== sep
+1 || sep
== dest
) {
874 error("Can't parse host:port for socket destination");
878 host
= inet_addr(dest
);
879 if (host
== (u_int32_t
) -1) {
880 hent
= gethostbyname(dest
);
882 error("%s: unknown host in socket option", dest
);
886 host
= *(u_int32_t
*)(hent
->h_addr_list
[0]);
890 /* get a socket and connect it to the other end */
891 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
893 error("Can't create socket: %m");
896 memset(&sad
, 0, sizeof(sad
));
897 sad
.sin_family
= AF_INET
;
898 sad
.sin_port
= htons(port
);
899 sad
.sin_addr
.s_addr
= host
;
900 if (connect(sock
, (struct sockaddr
*)&sad
, sizeof(sad
)) < 0) {
901 error("Can't connect to %s: %m", dest
);
911 * start_charshunt - create a child process to run the character shunt.
914 start_charshunt(ifd
, ofd
)
919 cpid
= safe_fork(ifd
, ofd
, (log_to_fd
>= 0? log_to_fd
: 2));
921 error("Can't fork process for character shunt: %m");
929 else if (log_to_fd
>= 0)
934 fatal("setuid failed");
935 charshunt(0, 1, record_file
);
938 charshunt_pid
= cpid
;
939 add_notifier(&sigreceived
, stop_charshunt
, 0);
940 record_child(cpid
, "pppd (charshunt)", charshunt_done
, NULL
);
952 stop_charshunt(arg
, sig
)
957 kill(charshunt_pid
, (sig
== SIGINT
? sig
: SIGTERM
));
961 * charshunt - the character shunt, which passes characters between
962 * the pty master side and the serial port (or stdin/stdout).
963 * This runs as the user (not as root).
964 * (We assume ofd >= ifd which is true the way this gets called. :-).
967 charshunt(ifd
, ofd
, record_file
)
972 fd_set ready
, writey
;
973 u_char
*ibufp
, *obufp
;
976 int pty_readable
, stdin_readable
;
977 struct timeval lasttime
;
978 FILE *recordf
= NULL
;
979 int ilevel
, olevel
, max_level
;
980 struct timeval levelt
, tout
, *top
;
981 extern u_char inpacket_buf
[];
984 * Reset signal handlers.
986 signal(SIGHUP
, SIG_IGN
); /* Hangup */
987 signal(SIGINT
, SIG_DFL
); /* Interrupt */
988 signal(SIGTERM
, SIG_DFL
); /* Terminate */
989 signal(SIGCHLD
, SIG_DFL
);
990 signal(SIGUSR1
, SIG_DFL
);
991 signal(SIGUSR2
, SIG_DFL
);
992 signal(SIGABRT
, SIG_DFL
);
993 signal(SIGALRM
, SIG_DFL
);
994 signal(SIGFPE
, SIG_DFL
);
995 signal(SIGILL
, SIG_DFL
);
996 signal(SIGPIPE
, SIG_DFL
);
997 signal(SIGQUIT
, SIG_DFL
);
998 signal(SIGSEGV
, SIG_DFL
);
1000 signal(SIGBUS
, SIG_DFL
);
1003 signal(SIGEMT
, SIG_DFL
);
1006 signal(SIGPOLL
, SIG_DFL
);
1009 signal(SIGPROF
, SIG_DFL
);
1012 signal(SIGSYS
, SIG_DFL
);
1015 signal(SIGTRAP
, SIG_DFL
);
1018 signal(SIGVTALRM
, SIG_DFL
);
1021 signal(SIGXCPU
, SIG_DFL
);
1024 signal(SIGXFSZ
, SIG_DFL
);
1028 * Check that the fds won't overrun the fd_sets
1030 if (ifd
>= FD_SETSIZE
|| ofd
>= FD_SETSIZE
|| pty_master
>= FD_SETSIZE
)
1031 fatal("internal error: file descriptor too large (%d, %d, %d)",
1032 ifd
, ofd
, pty_master
);
1035 * Open the record file if required.
1037 if (record_file
!= NULL
) {
1038 recordf
= fopen(record_file
, "a");
1039 if (recordf
== NULL
)
1040 error("Couldn't create record file %s: %m", record_file
);
1043 /* set all the fds to non-blocking mode */
1044 flags
= fcntl(pty_master
, F_GETFL
);
1046 || fcntl(pty_master
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1047 warn("couldn't set pty master to nonblock: %m");
1048 flags
= fcntl(ifd
, F_GETFL
);
1050 || fcntl(ifd
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1051 warn("couldn't set %s to nonblock: %m", (ifd
==0? "stdin": "tty"));
1053 flags
= fcntl(ofd
, F_GETFL
);
1055 || fcntl(ofd
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1056 warn("couldn't set stdout to nonblock: %m");
1060 ibufp
= obufp
= NULL
;
1061 pty_readable
= stdin_readable
= 1;
1063 ilevel
= olevel
= 0;
1064 gettimeofday(&levelt
, NULL
);
1065 if (max_data_rate
) {
1066 max_level
= max_data_rate
/ 10;
1067 if (max_level
< 100)
1070 max_level
= PPP_MRU
+ PPP_HDRLEN
+ 1;
1072 nfds
= (ofd
> pty_master
? ofd
: pty_master
) + 1;
1073 if (recordf
!= NULL
) {
1074 gettimeofday(&lasttime
, NULL
);
1075 putc(7, recordf
); /* put start marker */
1076 putc(lasttime
.tv_sec
>> 24, recordf
);
1077 putc(lasttime
.tv_sec
>> 16, recordf
);
1078 putc(lasttime
.tv_sec
>> 8, recordf
);
1079 putc(lasttime
.tv_sec
, recordf
);
1080 lasttime
.tv_usec
= 0;
1083 while (nibuf
!= 0 || nobuf
!= 0 || pty_readable
|| stdin_readable
) {
1086 tout
.tv_usec
= 10000;
1090 if (ilevel
>= max_level
)
1093 FD_SET(pty_master
, &writey
);
1094 } else if (stdin_readable
)
1095 FD_SET(ifd
, &ready
);
1097 if (olevel
>= max_level
)
1100 FD_SET(ofd
, &writey
);
1101 } else if (pty_readable
)
1102 FD_SET(pty_master
, &ready
);
1103 if (select(nfds
, &ready
, &writey
, NULL
, top
) < 0) {
1108 if (max_data_rate
) {
1113 gettimeofday(&now
, NULL
);
1114 dt
= (now
.tv_sec
- levelt
.tv_sec
1115 + (now
.tv_usec
- levelt
.tv_usec
) / 1e6
);
1116 nbt
= (int)(dt
* max_data_rate
);
1117 ilevel
= (nbt
< 0 || nbt
> ilevel
)? 0: ilevel
- nbt
;
1118 olevel
= (nbt
< 0 || nbt
> olevel
)? 0: olevel
- nbt
;
1121 ilevel
= olevel
= 0;
1122 if (FD_ISSET(ifd
, &ready
)) {
1123 ibufp
= inpacket_buf
;
1124 nibuf
= read(ifd
, ibufp
, PPP_MRU
+ PPP_HDRLEN
);
1125 if (nibuf
< 0 && errno
== EIO
)
1128 if (!(errno
== EINTR
|| errno
== EAGAIN
)) {
1129 error("Error reading standard input: %m");
1133 } else if (nibuf
== 0) {
1134 /* end of file from stdin */
1137 if (!record_write(recordf
, 4, NULL
, 0, &lasttime
))
1140 FD_SET(pty_master
, &writey
);
1142 if (!record_write(recordf
, 2, ibufp
, nibuf
, &lasttime
))
1146 if (FD_ISSET(pty_master
, &ready
)) {
1147 obufp
= outpacket_buf
;
1148 nobuf
= read(pty_master
, obufp
, PPP_MRU
+ PPP_HDRLEN
);
1149 if (nobuf
< 0 && errno
== EIO
)
1152 if (!(errno
== EINTR
|| errno
== EAGAIN
)) {
1153 error("Error reading pseudo-tty master: %m");
1157 } else if (nobuf
== 0) {
1158 /* end of file from the pty - slave side has closed */
1160 stdin_readable
= 0; /* pty is not writable now */
1164 if (!record_write(recordf
, 3, NULL
, 0, &lasttime
))
1167 FD_SET(ofd
, &writey
);
1169 if (!record_write(recordf
, 1, obufp
, nobuf
, &lasttime
))
1172 } else if (!stdin_readable
)
1174 if (FD_ISSET(ofd
, &writey
)) {
1176 if (olevel
+ n
> max_level
)
1177 n
= max_level
- olevel
;
1178 n
= write(ofd
, obufp
, n
);
1183 } else if (errno
!= EAGAIN
&& errno
!= EINTR
) {
1184 error("Error writing standard output: %m");
1193 if (FD_ISSET(pty_master
, &writey
)) {
1195 if (ilevel
+ n
> max_level
)
1196 n
= max_level
- ilevel
;
1197 n
= write(pty_master
, ibufp
, n
);
1202 } else if (errno
!= EAGAIN
&& errno
!= EINTR
) {
1203 error("Error writing pseudo-tty master: %m");
1217 record_write(f
, code
, buf
, nb
, tp
)
1227 gettimeofday(&now
, NULL
);
1228 now
.tv_usec
/= 100000; /* actually 1/10 s, not usec now */
1229 diff
= (now
.tv_sec
- tp
->tv_sec
) * 10 + (now
.tv_usec
- tp
->tv_usec
);
1233 putc(diff
>> 24, f
);
1234 putc(diff
>> 16, f
);
1247 fwrite(buf
, nb
, 1, f
);
1251 error("Error writing record file: %m");