The system dependent communication routines for UNIX.
Copyright (C) 1991, 1992 Ian Lance Taylor
This file is part of the Taylor UUCP package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
The author of the program may be contacted at ian@airs.com or
c/o AIRS, P.O. Box 520, Waltham, MA 02254.
Revision 1.72 1992/04/03 17:43:39 ian
Petri Helenius: don't clobber undefined bits in termio or termios
Revision 1.71 1992/04/01 21:52:04 ian
T. William Wells: handle a system without <poll.h> or <stropts.h>
Revision 1.70 1992/03/30 15:29:58 ian
Added HAVE_SVR4_LOCKFILES
Revision 1.69 1992/03/29 22:25:27 ian
Always block and unblock read and write descriptors together
Revision 1.68 1992/03/28 04:12:17 ian
Gerben Wierda: minor cleanups
Revision 1.67 1992/03/28 03:06:04 ian
Don't use TIOCEXCL locking
Revision 1.66 1992/03/28 02:47:30 ian
Rework HAVE_UNBLOCKED_WRITES to work even if writes are unblocked
Revision 1.65 1992/03/17 15:35:28 ian
Log signals when they happen, even if we continue looping
Revision 1.64 1992/03/17 05:01:33 ian
Don't block when opening the write descriptor
Revision 1.63 1992/03/17 01:03:03 ian
Revision 1.62 1992/03/16 01:23:08 ian
Make blocking writes optional
Revision 1.61 1992/03/15 07:15:42 ian
T. William Wells: don't use unblocked writes
Revision 1.60 1992/03/15 04:51:17 ian
Keep an array of signals we've received rather than a single variable
Revision 1.59 1992/03/15 01:54:46 ian
All execs are now done in isspawn, all waits are done in iswait
Revision 1.58 1992/03/12 21:50:50 ian
Moved local header includes above sleep routine determination
Revision 1.57 1992/03/12 19:54:43 ian
Debugging based on types rather than number
Revision 1.56 1992/03/11 02:09:57 ian
Franc,ois Pinard: retry fork several times before giving up
Revision 1.55 1992/03/09 22:11:46 ian
Franc,ois Pinard: sleep for a second after closing a serial port
Revision 1.54 1992/03/09 22:07:36 ian
Wait for terminal output to drain at various points
Revision 1.53 1992/03/08 16:56:51 ian
Ted Lindgreen: if CRTSCTS is defined, don't turn on IXOFF
Revision 1.52 1992/03/08 04:56:21 ian
Peter da Silva: added ``lockname'' command for ports
Revision 1.51 1992/03/08 01:56:01 ian
Include <sys/ioctl.h> if we have it
Revision 1.50 1992/03/08 01:37:45 ian
Revision 1.49 1992/03/07 16:25:21 ian
Preserve unknown bits in c_cflag
Revision 1.48 1992/03/04 23:43:39 ian
Petri Helenius: didn't remove lock file if open failed
Revision 1.47 1992/03/04 01:40:51 ian
Thomas Fischer: tweaked a bit for the NeXT
Revision 1.46 1992/03/03 21:01:20 ian
Use strict timeout in fsserial_read, eliminate all race conditions
Revision 1.45 1992/03/03 04:25:00 ian
T. William Wells: don't arbitrarily extend read timeout
Revision 1.44 1992/03/02 04:53:07 ian
Marc Unangst: added HAVE_SCO_LOCKFILES configuration parameter
Revision 1.43 1992/02/28 05:06:15 ian
T. William Wells: fsysdep_catch must be a macro
Revision 1.42 1992/02/27 05:40:54 ian
T. William Wells: detach from controlling terminal, handle signals safely
Revision 1.41 1992/02/24 21:22:47 ian
The poll function takes milliseconds, not microseconds (my error)
Revision 1.40 1992/02/24 21:18:17 ian
Roberto Biancardi: use poll for sleeping if we haven't got anything else
Revision 1.39 1992/02/24 20:07:43 ian
John Theus: some systems don't have <fcntl.h>
Revision 1.38 1992/02/23 03:26:51 ian
Overhaul to use automatic configure shell script
Revision 1.37 1992/02/17 22:08:50 ian
Bob Denny: log chat script messages as LOG_NORMAL, not LOG_ERROR
Revision 1.36 1992/02/08 20:02:36 ian
Added HAVE_SETRET configuration option for systems without setjmp
Revision 1.35 1992/02/08 03:54:18 ian
Include <string.h> only in <uucp.h>, added 1992 copyright
Revision 1.34 1992/01/16 16:32:44 ian
Mike Park: ioctl is sometimes declared varadic, so we can't declare it
Revision 1.33 1992/01/15 21:06:11 ian
Mike Park: some systems can't include <sys/time.h> and <time.h> together
Revision 1.32 1992/01/15 20:40:04 ian
Mike Park: some systems don't have <limits.h>
Revision 1.31 1992/01/15 19:40:35 ian
Mike Park: handle HAVE_UNION_WAIT correctly and completely
Revision 1.30 1992/01/14 04:18:47 ian
Chip Salzenberg: added HAVE_USLEEP configuration parameter
Revision 1.29 1992/01/13 06:11:39 ian
David Nugent: can't declare open or fcntl
Revision 1.28 1991/12/31 04:16:19 ian
Chip Salzenberg: don't turn on IXON and IXOFF at the start
Revision 1.27 1991/12/29 04:04:18 ian
Added a bunch of extern definitions
Revision 1.26 1991/12/29 00:55:23 ian
Monty Solomon: added HAVE_UNION_WAIT
Revision 1.25 1991/12/22 22:14:19 ian
Monty Solomon: added HAVE_UNISTD_H configuration parameter
Revision 1.24 1991/12/20 02:23:10 ian
Don't change port settings if we don't have to
Revision 1.23 1991/12/19 04:25:57 ian
Terry Gardner: configuration parameter to not use both NONBLOCK and NDELAY
Revision 1.22 1991/12/17 23:14:08 ian
T. William Wells: allow dialer complete and abort to be chat scripts
Revision 1.21 1991/12/17 22:31:15 ian
Start in RAW mode, to avoid dropping characters when we switch to it
Revision 1.20 1991/12/17 05:24:01 ian
David Nugent: flush pending input in fsserial_open
Revision 1.19 1991/12/15 04:28:23 ian
Don't turn on ISTRIP initially
Revision 1.18 1991/12/10 19:45:05 ian
Added ulog_device to record device name for log file
Revision 1.17 1991/12/01 02:23:12 ian
Niels Baggesen: don't multiply include <unistd.h>
Revision 1.16 1991/11/26 01:50:30 ian
Set fread_blocking and fwrite_blocking correctly for TCP routines
Revision 1.15 1991/11/26 01:45:42 ian
Marty Shannon: configuration option to not include <sys/wait.h>
Revision 1.14 1991/11/22 06:05:57 ian
Gregory Gulik: fix wait status macro definitions
Revision 1.13 1991/11/21 20:58:18 ian
Brian Campbell: for HAVE_SIGSETJMP use sigjmp_buf, not jmp_buf
Revision 1.12 1991/11/15 23:13:53 ian
Fixed termio(s) version of fsserial_set
Revision 1.11 1991/11/13 20:38:00 ian
Added TCP port type for connections over TCP
Revision 1.10 1991/11/12 19:07:27 ian
Be careful to only call fsetterminfo on a terminal
Revision 1.9 1991/11/11 23:47:24 ian
Added chat-program to run a program to do a chat script
Revision 1.8 1991/11/11 00:39:45 ian
Open port in seven bit mode, added fport_set to change to eight bit
Revision 1.7 1991/11/08 22:52:34 ian
Brian Campbell: only include <sys/time.h> and <sys/ioctl.h> when needed
Revision 1.6 1991/11/08 22:11:45 ian
Brian Campbell: allow sigsetjmp as configuration option
Revision 1.5 1991/11/07 22:52:11 ian
Chip Salzenberg: force stdin and stdout to stay open in case of spawning
Revision 1.4 1991/11/07 21:43:59 ian
Chip Salzenberg: set terminal modes directly, don't or them in
Revision 1.3 1991/11/07 19:42:16 ian
Chip Salzenberg: declare inline functions consistently
Revision 1.2 1991/11/07 19:32:28 ian
Chip Salzenberg: allow LOCKDIR, and check that locking process exists
Revision 1.1 1991/09/10 19:45:50 ian
char sys2_unx_rcsid[] = "$Id: sys2.unx,v 1.72 1992/04/03 17:43:39 ian Rel $";
#if USE_STDIO && HAVE_UNISTD_H
/* Pick a timing routine to use. I somewhat arbitrarily picked usleep
above nap above napms above poll above select. */
#if HAVE_USLEEP || HAVE_NAP || HAVE_NAPMS || HAVE_POLL
#define USE_SELECT_TIMER 0
#define USE_SELECT_TIMER HAVE_SELECT
#if HAVE_USLEEP || HAVE_NAP || HAVE_NAPMS
#if HAVE_USLEEP || HAVE_NAP
#if USE_SELECT_TIMER || HAVE_BSD_TTY
#if ! HAVE_STROPTS_H && ! HAVE_POLL_H
/* We need a definition for struct pollfd, although it doesn't matter
what it contains. It's used in usysdep_pause. */
#endif /* ! HAVE_STROPTS_H && ! HAVE_POLL_H */
#if HAVE_SYS_TIME_AND_TIME_H || (! USE_SELECT_TIMER && ! HAVE_BSD_TTY)
/* Get definitions for both O_NONBLOCK and O_NDELAY. */
#else /* ! defined (FNDELAY) */
#endif /* ! defined (FNDELAY) */
#endif /* ! defined (O_NDELAY) */
#define O_NONBLOCK FNBLOCK
#else /* ! defined (FNBLOCK) */
#endif /* ! defined (FNBLOCK) */
#endif /* ! defined (O_NONBLOCK) */
#if O_NDELAY == 0 && O_NONBLOCK == 0
#error No way to do nonblocking I/O
/* If we can define them both together, do so. This is because some
ancient drivers on some systems appear to look for one but not the
other. Otherwise just use O_NONBLOCK. */
#define FILE_UNBLOCKED (O_NDELAY | O_NONBLOCK)
#define FILE_UNBLOCKED O_NONBLOCK
/* Get definitions for both EAGAIN and EWOULDBLOCK. */
#else /* defined (EWOULDBLOCK) */
#define EAGAIN EWOULDBLOCK
#endif /* defined (EWOULDBLOCK) */
#else /* defined (EAGAIN) */
#define EWOULDBLOCK EAGAIN
#endif /* ! defined (EWOULDBLOCK) */
#endif /* defined (EAGAIN) */
/* Make sure we have a definition for MAX_INPUT. */
/* Make sure we have definitions for major and minor. */
#define major(i) (((i) >> 8) & 0xff)
#define minor(i) ((i) & 0xff)
/* If we have the TIOCSINUSE ioctl call, we use it to lock a terminal.
Otherwise, if we have the TIOCEXCL ioctl call, we have to open the
terminal before we know that it is unlocked. */
#define HAVE_TIOCSINUSE 1
/* Determine bits to clear for the various terminal control fields for
HAVE_SYSV_TERMIO and HAVE_POSIX_TERMIOS. */
#define ICLEAR_IFLAG (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK \
| ISTRIP | INLCR | IGNCR | ICRNL | IUCLC \
#define ICLEAR_OFLAG (OPOST | OLCUC | ONLCR | OCRNL | ONOCR | ONLRET \
| OFILL | OFDEL | NLDLY | CRDLY | TABDLY | BSDLY \
#define ICLEAR_CFLAG (CBAUD | CLOCAL | CSIZE | PARENB | PARODD)
#define ISET_CFLAG (CS8 | CREAD | HUPCL)
#define ICLEAR_LFLAG (ISIG | ICANON | XCASE | ECHO | ECHOE | ECHOK \
#define ICLEAR_IFLAG (BRKINT | ICRNL | IGNBRK | IGNCR | IGNPAR \
| INLCR | INPCK | ISTRIP | IXOFF | IXON \
#define ICLEAR_OFLAG (OPOST)
#define ICLEAR_CFLAG (CLOCAL | CSIZE | PARENB | PARODD)
#define ISET_CFLAG (CS8 | CREAD | HUPCL)
#define ICLEAR_LFLAG (ECHO | ECHOE | ECHOK | ECHONL | ICANON | IEXTEN \
| ISIG | NOFLSH | TOSTOP)
/* External functions. */
extern int close (), pipe (), dup2 (), read (), write ();
#if USE_SELECT_TIMER || HAVE_BSD_TTY
static SIGtype usalarm P((int isig));
static boolean fsserial_lockfile P((boolean flok, const struct sport *,
static boolean fsserial_lock P((const struct sport *qport,
struct ssysdep_serial_port *q,
boolean fin, const char *zdevice));
static boolean fsserial_open P((const char *z, long ibaud, boolean fwait,
struct ssysdep_serial_port *q));
__inline__ static boolean fsblock P((struct ssysdep_serial_port *q,
static boolean fsserial_close P((struct ssysdep_serial_port *q));
static boolean fsserial_reset P((struct ssysdep_serial_port *q));
static boolean fsserial_read P((struct ssysdep_serial_port *q,
char *zbuf, int *pclen, int cmin,
int ctimeout, boolean freport,
static boolean fsserial_write P((struct ssysdep_serial_port *q,
const char *zwrite, int cwrite));
static boolean fsserial_io P((struct ssysdep_serial_port *q,
const char *zwrite, int *pcwrite,
char *zread, int *pcread));
static boolean fsserial_break P((struct ssysdep_serial_port *q));
static boolean fsserial_set P((struct ssysdep_serial_port *q,
enum tportsetting tset));
static boolean fsrun_chat P((int oread, int owrite, const char *zprog));
/* This code handles SIGALRM. See the discussion above fsserial_read.
Normally we ignore SIGALRM, but the handler will temporarily be set
to this function, which should set fSalarm and then either longjmp
or schedule another SIGALRM. fSalarm is never referred to outside
of this file, but we don't make it static to try to fool compilers
which don't understand volatile. */
volatile sig_atomic_t fSalarm;
#if ! HAVE_SIGACTION && ! HAVE_SIGVEC && ! HAVE_SIGSET
(void) signal (isig, usalarm);
#if HAVE_RESTARTABLE_SYSCALLS
/* We need a simple routine to block SIGINT, SIGQUIT, SIGTERM and
SIGPIPE and another to restore the original state. When these
functions are called (in fsysdep_modem_close) SIGHUP is being
ignored. The routines are isblocksigs, which returns a value of
type HELD_SIG_MASK and usunblocksigs which takes a single argument
of type HELD_SIG_MASK. */
/* Use the POSIX sigprocmask call. */
extern int sigprocmask ();
#define HELD_SIG_MASK sigset_t
static sigset_t isblocksigs P((void));
sigaddset (&sblock, SIGINT);
sigaddset (&sblock, SIGQUIT);
sigaddset (&sblock, SIGTERM);
sigaddset (&sblock, SIGPIPE);
sigprocmask (SIG_BLOCK, &sblock, &sold);
#define usunblocksigs(s) \
((void) sigprocmask (SIG_SETMASK, &(s), (sigset_t *) NULL))
#else /* ! HAVE_SIGPROCMASK */
/* Use the BSD sigblock and sigsetmask calls. */
extern int sigblock (), sigsetmask ();
#define HELD_SIG_MASK int
#define sigmask(i) (1 << ((i) - 1))
sigblock (sigmask (SIGINT) | sigmask (SIGQUIT) \
| sigmask (SIGTERM) | sigmask (SIGPIPE))
#define usunblocksigs(i) ((void) sigsetmask (i))
#else /* ! HAVE_SIGBLOCK */
/* Use the SVR3 sighold and sigrelse calls. */
extern int sighold (), sigrelse ();
#define HELD_SIG_MASK int
static int isblocksigs P((void));
static void usunblocksigs P((int));
#else /* ! HAVE_SIGHOLD */
/* We have no way to block signals. This system will suffer from a
race condition in fsysdep_modem_close. */
#define HELD_SIG_MASK int
#endif /* ! HAVE_SIGHOLD */
#endif /* ! HAVE_SIGBLOCK */
#endif /* ! HAVE_SIGPROCMASK */
/* Pause for half a second. This doesn't really belong in this file,
but all the timing routines are here. */
usleep (500 * (long) 1000);
/* We need to pass an unused pollfd structure because poll checks
the address before checking the number of elements. */
s.tv_usec = 500 * (long) 1000;
select (0, (int *) NULL, (int *) NULL, (int *) NULL, &s);
#endif /* USE_SELECT_TIMER */
#if ! HAVE_NAPMS && ! HAVE_NAP && ! HAVE_USLEEP
#if ! USE_SELECT_TIMER && ! HAVE_POLL
#endif /* ! USE_SELECT_TIMER && ! HAVE_POLL */
#endif /* ! HAVE_NAPMS && ! HAVE_NAP && ! HAVE_USLEEP */
/* This routine is used for both locking and unlocking. It is the
only routine which knows how to translate a device name into the
name of a lock file. If it can't figure out a name, it does
nothing and returns TRUE. */
fsserial_lockfile (flok, qport, zdevice)
const struct sport *qport;
#if ! HAVE_SVR4_LOCKFILES
if (strncmp (z, "/dev/", sizeof "/dev/" - 1) == 0)
zalc = (char *) alloca (strlen (z) + sizeof "LCK..");
sprintf (zalc, "LCK..%s", z);
strlwr (zalc + sizeof "LCK.." - 1);
#else /* HAVE_SVR4_LOCKFILES */
zalc = (char *) alloca (sizeof "/dev/" + strlen (z));
sprintf (zalc, "/dev/%s", z);
ulog (LOG_ERROR, "stat (%s): %s", z, strerror (errno));
zalc = (char *) alloca (sizeof "LK.123.123.123");
sprintf (zalc, "LK.%03d.%03d.%03d", major (s.st_dev),
major (s.st_rdev), minor (s.st_rdev));
#endif /* HAVE_SVR4_LOCKFILES */
return fsdo_lock (z, FALSE);
return fsdo_unlock (z, FALSE);
/* If we can mark a modem line in use, then when we lock a port we
must open it and mark it in use. We can't wait until the actual
open because we can't fail out if it is locked then. */
fsserial_lock (qport, q, fin, zdevice)
const struct sport *qport;
struct ssysdep_serial_port *q;
if (! fsserial_lockfile (TRUE, qport, zdevice))
#if HAVE_TIOCSINUSE || HAVE_TIOCEXCL
/* Open the line and, if possible, mark it in use. */
zcopy = (char *) alloca (sizeof "/dev/" + strlen (z));
sprintf (zcopy, "/dev/%s", z);
oread = open (z, O_RDWR | iflag);
ulog (LOG_ERROR, "open (%s): %s", z, strerror (errno));
(void) fsserial_lockfile (FALSE, qport, zdevice);
/* If we can't mark it in use, return FALSE to indicate that the
if (ioctl (oread, TIOCSINUSE, 0) < 0)
ulog (LOG_ERROR, "ioctl (TIOCSINUSE): %s", strerror (errno));
(void) fsserial_lockfile (FALSE, qport, zdevice);
/* On BSD 4.4, make it our controlling terminal. */
(void) ioctl (oread, TIOCSCTTY, 0);
q->oread = q->owrite = oread;
#endif /* HAVE_TIOCSINUSE || HAVE_TIOCEXCL */
/* We don't need to lock a stdin port. */
fsysdep_stdin_lock (qport, fin)
fsysdep_modem_lock (qport, fin)
return fsserial_lock (qport, &qport->u.smodem.s.s, fin,
qport->u.smodem.zdevice);
/* Lock a direct port. */
fsysdep_direct_lock (qport, fin)
return fsserial_lock (qport, &qport->u.sdirect.s.s, fin,
qport->u.sdirect.zdevice);
/* Open a serial line. This sets the terminal settings. We begin in
seven bit mode and let the protocol change if necessary. */
static struct sbaud_table
#else /* ! defined (B19200) */
#endif /* ! defined (B19200) */
#else /* ! defined (B38400) */
#endif /* ! defined (B38400) */
#define CBAUD_TABLE (sizeof asSbaud_table / sizeof asSbaud_table[0])
#if HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS
/* Hold the MIN value for the terminal to avoid setting it
#endif /* HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS */
fsserial_open (z, ibaud, fwait, q)
struct ssysdep_serial_port *q;
zport = zsysdep_port_name (&fdummy);
if (strncmp (z, "/dev/", sizeof "/dev/" - 1) == 0)
ulog_device (z + sizeof "/dev/" - 1);
for (i = 0; i < CBAUD_TABLE; i++)
if (asSbaud_table[i].ibaud == ibaud)
ulog (LOG_ERROR, "Unsupported baud rate %ld", ibaud);
if (q->oread != q->owrite)
(void) close (q->owrite);
ib = asSbaud_table[i].icode;
/* The port may have already been opened by the locking routine. */
zcopy = (char *) alloca (sizeof "/dev/" + strlen (z));
sprintf (zcopy, "/dev/%s", z);
oread = open (z, O_RDWR | iflag);
ulog (LOG_ERROR, "open (%s): %s", z, strerror (errno));
/* On BSD 4.4, make it our controlling terminal. */
(void) ioctl (oread, TIOCSCTTY, 0);
/* Make sure the ports are blocking. */
if (fcntl (q->oread, F_SETFL, 0) < 0
|| (q->oread != q->owrite
&& fcntl (q->owrite, F_SETFL, 0) < 0))
ulog (LOG_ERROR, "fcntl: %s", strerror (errno));
if (q->oread != q->owrite)
(void) close (q->owrite);
q->fread_blocking = TRUE;
if (! fgetterminfo (q->oread, &q->sorig))
q->snew.sg_flags = RAW | ANYP;
/* When the file is closed, hang up the line. This is a safety
measure in case the program crashes. */
(void) ioctl (q->oread, TIOCHPCL, 0);
/* Flush pending input. */
(void) ioctl (q->oread, TIOCFLUSH, &iparam);
#endif /* HAVE_BSD_TTY */
ib = q->snew.c_cflag & CBAUD;
q->snew.c_iflag &=~ ICLEAR_IFLAG;
q->snew.c_oflag &=~ ICLEAR_OFLAG;
q->snew.c_cflag &=~ ICLEAR_CFLAG;
q->snew.c_cflag |= (ib | ISET_CFLAG);
q->snew.c_lflag &=~ ICLEAR_LFLAG;
q->snew.c_cc[VMIN] = cSmin;
/* Flush pending input. */
(void) ioctl (q->oread, TCFLSH, 0);
#endif /* HAVE_SYSV_TERMIO */
ib = cfgetospeed (&q->snew);
q->snew.c_iflag &=~ ICLEAR_IFLAG;
q->snew.c_oflag &=~ ICLEAR_OFLAG;
q->snew.c_cflag &=~ ICLEAR_CFLAG;
q->snew.c_cflag |= ISET_CFLAG;
q->snew.c_lflag &=~ ICLEAR_LFLAG;
q->snew.c_cc[VMIN] = cSmin;
(void) cfsetospeed (&q->snew, ib);
(void) cfsetispeed (&q->snew, ib);
/* Flush pending input. */
(void) tcflush (q->oread, TCIFLUSH);
#endif /* HAVE_POSIX_TERMIOS */
if (! fsetterminfo (q->oread, &q->snew))
ulog (LOG_ERROR, "Can't set terminal settings: %s", strerror (errno));
if (q->oread != q->owrite)
(void) close (q->owrite);
for (i = 0; i < CBAUD_TABLE; i++)
if (asSbaud_table[i].icode == ib)
q->ibaud = asSbaud_table[i].ibaud;
DEBUG_MESSAGE1 (DEBUG_PORT,
"fsserial_open: Baud rate is %ld", q->ibaud);
fsysdep_stdin_open (qport, ibaud, fwait)
return fsserial_open ((const char *) NULL, ibaud, fwait,
fsysdep_modem_open (qport, ibaud, fwait)
z = qport->u.smodem.zdevice;
ulog (LOG_ERROR, "Port has no name or device");
ibaud = qport->u.smodem.ibaud;
fret = fsserial_open (z, ibaud, fwait, &qport->u.smodem.s.s);
(void) fsserial_lockfile (FALSE, qport, qport->u.smodem.zdevice);
/* Open a direct port. */
fsysdep_direct_open (qport, ibaud, fwait)
z = qport->u.sdirect.zdevice;
ulog (LOG_ERROR, "Port has no name or device");
ibaud = qport->u.sdirect.ibaud;
fret = fsserial_open (z, ibaud, fwait, &qport->u.sdirect.s.s);
(void) fsserial_lockfile (FALSE, qport, qport->u.sdirect.zdevice);
/* Change the blocking status of the port. We keep track of the
current blocking status to avoid calling fcntl unnecessarily; fcntl
turns out to be surprisingly expensive, at least on Ultrix. We
used to keep track of the blocking status of the read port and the
write port independently. Unfortunately, this is nonportable,
because on BSD, and probably some other systems, unblocking a
terminal applies to all descriptors to that terminal. Now, if
oread != owrite, we set both. */
struct ssysdep_serial_port *qs;
if (fblock ? ! qs->fread_blocking : qs->fread_blocking)
if (fcntl (qs->oread, F_SETFL, iset) < 0
|| (qs->oread != qs->owrite
&& fcntl (qs->owrite, F_SETFL, iset) < 0))
ulog (LOG_ERROR, "fcntl: %s", strerror (errno));
qs->fread_blocking = fblock;
/* Close a serial port. */
struct ssysdep_serial_port *q;
/* Use a 30 second timeout to avoid hanging while draining
usset_signal (SIGALRM, usalarm, TRUE, (boolean *) NULL);
(void) fsetterminfodrain (q->oread, &q->sorig);
usset_signal (SIGALRM, SIG_IGN, TRUE, (boolean *) NULL);
/* If we timed out, use the non draining call. Hopefully
(void) fsetterminfo (q->oread, &q->sorig);
if (q->oread != q->owrite)
(void) close (q->owrite);
q->oread = q->owrite = -1;
/* Sleep for a second to give the terminal a chance to settle,
in case we are about to call out again. */
/* Close a stdin port. */
fsysdep_stdin_close (qport, fsuccess)
return fsserial_close (&qport->u.sstdin.s.s);
/* Close a modem port. */
fsysdep_modem_close (qport, fsuccess)
/* We're no longer interested in carrier. */
(void) fsysdep_modem_no_carrier (qport);
if (qport->u.smodem.zdialer != NULL)
zcopy = (char *) alloca (strlen (qport->u.smodem.zdialer) + 1);
strcpy (zcopy, qport->u.smodem.zdialer);
zcopy[strcspn (zcopy, " \t")] = '\0';
if (! fread_dialer_info (zcopy, &sdial))
qdial = qport->u.smodem.qdialer;
sig_atomic_t afhold[INDEXSIG_COUNT];
const struct schat_info *qchat;
/* The port I/O routines check whether any signal has been
received, and abort if one has. While we are closing down
the modem, we don't care if we received a signal in the past,
but we do care if we receive a new signal (otherwise it would
be difficult to kill a uucico which was closing down a
modem). We never care if we get SIGHUP at this point. So we
turn off SIGHUP, remember what signals we've already seen,
and clear our notion of what signals we've seen. We have to
block the signals while we remember and clear the array,
since we might otherwise miss a signal which occurred between
the copy and the clear (old systems can't block signals; they
will just have to suffer the race). */
usset_signal (SIGHUP, SIG_IGN, FALSE, &fsighup_ignored);
for (i = 0; i < INDEXSIG_COUNT; i++)
qchat = &qdial->scomplete;
if (! fchat (qchat, (const struct ssysteminfo *) NULL,
(const struct sdialer *) NULL, (const char *) NULL,
FALSE, qport->zname, qport->u.smodem.s.s.ibaud))
/* Restore the old signal array and the SIGHUP handler. It is
not necessary to block signals here, since all we are doing
is exactly what the signal handler itself would do if the
for (i = 0; i < INDEXSIG_COUNT; i++)
usset_signal (SIGHUP, ussignal, TRUE, (boolean *) NULL);
if (! fsserial_close (&qport->u.smodem.s.s))
if (! fsserial_lockfile (FALSE, qport, qport->u.smodem.zdevice))
/* Close a direct port. */
fsysdep_direct_close (qport, fsuccess)
fret = fsserial_close (&qport->u.sdirect.s.s);
if (! fsserial_lockfile (FALSE, qport, qport->u.sdirect.zdevice))
/* Reset a serial port by hanging up. */
struct ssysdep_serial_port *q;
#else /* ! HAVE_BSD_TTY */
sbaud.c_cflag = (sbaud.c_cflag &~ CBAUD) | B0;
#endif /* ! HAVE_BSD_TTY */
if (! fsetterminfodrain (q->oread, &sbaud))
ulog (LOG_FATAL, "Can't hangup terminal: %s", strerror (errno));
/* Give the terminal a chance to settle. */
if (! fsetterminfo (q->oread, &q->snew))
ulog (LOG_FATAL, "Can't reopen terminal: %s", strerror (errno));
#else /* HAVE_POSIX_TERMIOS */
struct ssysdep_serial_port *q;
iin = cfgetispeed (&q->snew);
iout = cfgetospeed (&q->snew);
if (cfsetospeed (&sbaud, B0) != 0
|| ! fsetterminfodrain (q->oread, &sbaud))
ulog (LOG_FATAL, "Can't hangup terminal: %s", strerror (errno));
/* Give the terminal a chance to settle. */
if (cfsetispeed (&q->snew, iin) != 0
|| cfsetospeed (&q->snew, iout) != 0
|| ! fsetterminfo (q->oread, &q->snew))
ulog (LOG_FATAL, "Can't reopen terminal: %s", strerror (errno));
#endif /* HAVE_POSIX_TERMIOS */
/* Reset a stdin port. */
fsysdep_stdin_reset (qport)
return fsserial_reset (&qport->u.sstdin.s.s);
/* Reset a modem port. */
fsysdep_modem_reset (qport)
return fsserial_reset (&qport->u.smodem.s.s);
/* Reset a direct port. */
fsysdep_direct_reset (qport)
return fsserial_reset (&qport->u.sdirect.s.s);
/* Begin dialing out on a modem port. This opens the dialer device if
fsysdep_modem_begin_dial (qport, qdial)
/* If we can tell the modem to obey modem control, do so. */
(void) ioctl (qport->u.smodem.s.s.oread, TIOCMODEM, &iperm);
/* If we supposed to toggle DTR, do so. */
(void) ioctl (qport->u.smodem.s.s.oread, TIOCCDTR, 0);
(void) ioctl (qport->u.smodem.s.s.oread, TIOCSDTR, 0);
if (qdial->fdtr_toggle_wait)
if (! fsysdep_modem_no_carrier (qport))
/* Open the dial device if there is one. */
if (qport->u.smodem.zdial_device != NULL)
qport->u.smodem.s.s.oholdread = qport->u.smodem.s.s.oread;
qport->u.smodem.s.s.oholdwrite = qport->u.smodem.s.s.owrite;
z = qport->u.smodem.zdial_device;
zcopy = (char *) alloca (sizeof "/dev/" + strlen (z));
sprintf (zcopy, "/dev/%s", z);
oread = open (z, O_RDWR);
ulog (LOG_ERROR, "open (%s): %s", z, strerror (errno));
qport->u.smodem.s.s.oread = qport->u.smodem.s.s.owrite = oread;
/* Tell the port to not require carrier. I don't know how to do this
on a BSD system that doesn't support TIOCNCAR, if there are any
fsysdep_modem_no_carrier (qport)
if (! qport->u.smodem.s.s.fterminal)
/* Tell the modem to ignore carrier. */
if (ioctl (qport->u.smodem.s.s.oread, TIOCNCAR, 0) < 0)
ulog (LOG_ERROR, "ioctl (TIOCNCAR): %s", strerror (errno));
#if HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS
/* Put the modem into local mode (ignore carrier) to start the chat
qport->u.smodem.s.s.snew.c_cflag |= CLOCAL;
if (! fsetterminfo (qport->u.smodem.s.s.oread,
&qport->u.smodem.s.s.snew))
ulog (LOG_ERROR, "Can't set CLOCAL: %s", strerror (errno));
#endif /* HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS */
/* Tell the port to require carrier. If the port does not support
carrier, we do nothing. We do not need to worry whether the
dialer supports carrier, since this will only be called when
explicitly requested by a dialer chat script. */
fsysdep_modem_need_carrier (qport)
if (! qport->u.smodem.s.s.fterminal)
if (qport->u.smodem.fcarrier)
/* Tell the modem to pay attention to carrier. */
if (ioctl (qport->u.smodem.s.s.oread, TIOCCAR, 0) < 0)
ulog (LOG_ERROR, "ioctl (TIOCCAR): %s", strerror (errno));
#if HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS
/* Put the modem into nonlocal mode. */
qport->u.smodem.s.s.snew.c_cflag &=~ CLOCAL;
if (! fsetterminfo (qport->u.smodem.s.s.oread,
&qport->u.smodem.s.s.snew))
ulog (LOG_ERROR, "Can't clear CLOCAL: %s", strerror (errno));
#endif /* HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS */
/* Finish dialing out on a modem by closing any dialer device and waiting
fsysdep_modem_end_dial (qport, qdial)
if (qport->u.smodem.zdial_device != NULL)
(void) close (qport->u.smodem.s.s.oread);
qport->u.smodem.s.s.oread = qport->u.smodem.s.s.oholdread;
qport->u.smodem.s.s.owrite = qport->u.smodem.s.s.oholdwrite;
if (qport->u.smodem.fcarrier && qdial->fcarrier)
/* Tell the port that we need carrier. */
if (! fsysdep_modem_need_carrier (qport))
/* We know how to wait for carrier, so do so. */
/* If we already got a signal, just quit now. */
/* This bit of code handles signals just like fsserial_read
does. See that function for a longer explanation. */
/* Use fsysdep_catch to handle a longjmp from the signal
/* Start catching SIGALRM; normally we ignore it. */
usset_signal (SIGALRM, usalarm, TRUE, (boolean *) NULL);
(void) alarm (qdial->ccarrier_wait);
/* We really don't care if we get an error, since that will
probably just mean that TIOCWONLINE isn't supported in
which case there's nothing we can do anyhow. If we get
SIGINT we want to keep waiting for carrier, because
SIGINT just means don't start any new sessions. We don't
handle SIGINT correctly if we do a longjmp in the signal
while (ioctl (qport->u.smodem.s.s.oread, TIOCWONLINE, 0) < 0
ulog (LOG_ERROR, (const char *) NULL);
if (FGOT_QUIT_SIGNAL () || fSalarm)
/* Turn off the pending SIGALRM and ignore SIGALARM again. */
usset_signal (SIGALRM, SIG_IGN, TRUE, (boolean *) NULL);
/* If we got a random signal, just return FALSE. */
/* If we timed out, give an error. */
ulog (LOG_ERROR, "Timed out waiting for carrier");
/* Read data from a serial port, with a timeout.
This function should return when we have read cmin characters or
the timeout has occurred. We have to work a bit to get UNIX to do
this efficiently. The simple implementation schedules a SIGALRM
signal and then calls read; if there is a single character
available, the call to read will return immediately, so there must
be a loop which terminates when the SIGALRM is delivered or the
correct number of characters has been read. This can be very
inefficient with a fast CPU or a low baud rate (or both!), since
each call to read may return only one or two characters.
Under POSIX or System V, we can specify a minimum number of
characters to read, so there is no serious trouble.
Under BSD, we figure out how many characters we have left to read,
how long it will take for them to arrive at the current baud rate,
Doing this with a timeout and avoiding all possible race conditions
get very hairy, though. Basically, we're going to schedule a
SIGALRM for when the timeout expires. I don't really want to do a
longjmp in the SIGALRM handler, though, because that may lose data.
Therefore, I have the signal handler set a variable. However, this
means that there will be a span of time between the time the code
checks the variable and the time it calls the read system call; if
the SIGALRM occurs during that time, the read might hang forever.
To avoid this, the SIGALRM handler not only sets a global variable,
it also schedules another SIGALRM for one second in the future
(POSIX specifies that a signal handler is permitted to safely call
alarm). To avoid getting a continual sequence of SIGALRM
interrupts, we change the signal handler to ignore SIGALRM when
we're about to exit the function. This means that every time we
execute fsserial_read we make at least five system calls. It's the
best I've been able to come up with, though.
When fsserial_read finishes, there will be no SIGALRM scheduled and
SIGALRM will be ignored. */
fsserial_read (q, zbuf, pclen, cmin, ctimeout, freport, fpty)
struct ssysdep_serial_port *q;
/* Guard against a bad timeout. We return TRUE when a timeout
expires. It is possible to get a negative timeout here because
the calling code does not check user supplied timeouts for
/* We want to do a blocking read. */
/* We're going to set up an alarm signal to last for the entire
read. If the read system call cannot be interrupted, the signal
handler will do a longjmp causing fsysdep_catch (a macro) to
return FALSE. We handle that here. If read can be interrupted,
fsysdep_catch will be defined to TRUE. */
/* Prepare to catch SIGALRM and schedule the signal. */
usset_signal (SIGALRM, usalarm, TRUE, (boolean *) NULL);
/* We caught a signal. We don't actually have to do anything,
as all the appropriate checks are made at the start of the
#if HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS
/* If we can tell the terminal not to return until we have a
certain number of characters, do so. */
/* I'm not that confident about setting MIN to values larger
than 127, although up to 255 would probably work. */
q->snew.c_cc[VMIN] = csetmin;
if (! fsetterminfo (q->oread, &q->snew))
/* We turn off the signal before reporting the error
to minimize any problems with interrupted system
usset_signal (SIGALRM, SIG_IGN, TRUE, (boolean *) NULL);
ulog (LOG_ERROR, "Can't set MIN for terminal: %s",
#endif /* HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS */
/* If we've received a signal, get out now. */
/* If we've already gotten a SIGALRM, get out with whatever
/* Right here is the race condition which we avoid by having the
SIGALRM handler schedule another SIGALRM. */
cgot = read (q->oread, zbuf, cwant);
/* If the read returned an error, check for signals. */
ulog (LOG_ERROR, (const char *) NULL);
/* If read returned an error, get out. We just ignore EINTR
here, since it must be from some signal we don't care about.
If the read returned 0 then the line must have been hung up
(normally we would have received SIGHUP, but we can't count
on that). We turn off the signals before calling ulog to
reduce problems with interrupted system calls. */
if (cgot < 0 && errno == EINTR)
usset_signal (SIGALRM, SIG_IGN, TRUE, (boolean *) NULL);
ulog (LOG_ERROR, "Line disconnected");
ulog (LOG_ERROR, "read: %s", strerror (ierr));
/* If we have enough data, get out now. */
/* We still want more data, so sleep long enough for the rest of
it to arrive. We don't this for System V or POSIX because
setting MIN is good enough (we can't sleep longer than it
takes to get MAX_INPUT characters anyhow).
The baud rate is approximately 10 times the number of
characters which will arrive in one second, so the number of
characters * (milliseconds / character) ==
characters * (1000 * (seconds / character)) ==
characters * (1000 * (1 / (baud / 10))) ==
characters * (10000 / baud)
We arbitrarily reduce the sleep amount by 10 milliseconds to
attempt to account for the amount of time it takes to set up
the sleep. This is how long it takes to get half a character
at 19200 baud. We then don't bother to sleep for less than
10 milliseconds. We don't sleep if the read was interrupted.
We use select to sleep. It would be easy to use poll as
well, but it's unlikely that any system with BSD ttys would
have poll but not select. Using select avoids hassles with
the pending SIGALRM; if it hits the select will be
interrupted, and otherwise the select will not affect it. */
#error This code requires select; feel free to extend it
if (q->fterminal && ! fpty && cmin > 1 && cgot > 0)
/* We don't try to read all the way up to MAX_INPUT,
since that might drop a character. */
if (cmin <= MAX_INPUT - 10)
csleepchars = MAX_INPUT - 10;
isleep = (int) (((long) csleepchars * 10000L) / q->ibaud);
s.tv_sec = isleep / 1000;
s.tv_usec = (isleep % 1000) * 1000;
/* Some versions of select take a pointer to an int,
while some take a pointer to an fd_set. I just cast
the arguments to a generic pointer, and assume that
any machine which distinguishes int * from fd_set *
(I would be amazed if there are any such machines)
have an appropriate prototype somewhere or other. */
(void) select (0, (pointer) NULL, (pointer) NULL,
/* Here either the select finished sleeping or we got a
SIGALRM. If the latter occurred, fSalarm was set to
TRUE; it will be checked at the top of the loop. */
#endif /* HAVE_BSD_TTY */
/* Turn off the pending SIGALRM and return. */
usset_signal (SIGALRM, SIG_IGN, TRUE, (boolean *) NULL);
/* Read from a stdin port. */
fsysdep_stdin_read (qport, zbuf, pclen, cmin, ctimeout, freport)
return fsserial_read (&qport->u.sstdin.s.s, zbuf, pclen, cmin, ctimeout,
freport, qport->u.sstdin.s.fpty);
/* Read from a modem port. */
fsysdep_modem_read (qport, zbuf, pclen, cmin, ctimeout, freport)
return fsserial_read (&qport->u.smodem.s.s, zbuf, pclen, cmin, ctimeout,
/* Read from a direct port. */
fsysdep_direct_read (qport, zbuf, pclen, cmin, ctimeout, freport)
return fsserial_read (&qport->u.sdirect.s.s, zbuf, pclen, cmin, ctimeout,
/* Write data to a serial port. */
fsserial_write (q, zwrite, cwrite)
struct ssysdep_serial_port *q;
/* We want blocking writes here. */
/* If we've received a signal, don't continue. */
/* Loop until we don't get an interrupt. */
while ((cdid = write (q->owrite, zwrite, cwrite)) < 0
ulog (LOG_ERROR, (const char *) NULL);
if (errno != EWOULDBLOCK && errno != EAGAIN)
ulog (LOG_ERROR, "write: %s", strerror (errno));
/* On some systems write will return 0 if carrier is lost.
If we fail to write anything ten times in a row, we
assume that this has happened. This is hacked in like
this because there seems to be no reliable way to tell
exactly why the write returned 0. */
ulog (LOG_ERROR, "Line disconnected");
/* Write to a stdin port. */
fsysdep_stdin_write (qport, zwrite, cwrite)
return fsserial_write (&qport->u.sstdin.s.s, zwrite, cwrite);
/* Write to a modem port. */
fsysdep_modem_write (qport, zwrite, cwrite)
return fsserial_write (&qport->u.smodem.s.s, zwrite, cwrite);
/* Write to a direct port. */
fsysdep_direct_write (qport, zwrite, cwrite)
return fsserial_write (&qport->u.sdirect.s.s, zwrite, cwrite);
/* The fsysdep_io routine is supposed to both read and write data
until it has either filled its read buffer or written out all the
data it was given. This lets us write out large packets without
fsserial_io (q, zwrite, pcwrite, zread, pcread)
struct ssysdep_serial_port *q;
int cwrite, cread, czero;
/* If we've received a signal, don't continue. */
/* This used to always use nonblocking writes, but it turns out
that some systems don't support them on terminals.
The current algorithm is:
if read buffer full, return
if nothing to write, return
write up to SINGLE_WRITE bytes
if all data written, return
blocked write of up to SINGLE_WRITE bytes
This algorithm should work whether the system supports
unblocked writes on terminals or not. If the system supports
unblocked writes but HAVE_UNBLOCKED_WRITES is 0, then it will
call write more often than it needs to. If the system does
not support unblocked writes but HAVE_UNBLOCKED_WRITES is 1,
then the write may hang so long that incoming data is lost.
This is actually possible at high baud rates on any system
when a blocking write is done; there is no solution, except
/* Do an unblocked read. */
if (! fsblock (q, FALSE))
/* Loop until we get something (error or data) other than an
while ((cgot = read (q->oread, zread, cread)) < 0
ulog (LOG_ERROR, (const char *) NULL);
if (errno != EAGAIN && errno != EWOULDBLOCK)
ulog (LOG_ERROR, "read: %s", strerror (errno));
/* If we've filled the read buffer, or we have nothing left to
if (cread <= 0 || cwrite <= 0)
/* The port is currently unblocked. Do a write. */
#if ! HAVE_UNBLOCKED_WRITES
/* Loop until we get something besides EINTR. */
while ((cdid = write (q->owrite, zwrite, cdo)) < 0
ulog (LOG_ERROR, (const char *) NULL);
if (errno != EWOULDBLOCK && errno != EAGAIN)
ulog (LOG_ERROR, "write: %s", strerror (errno));
/* We wrote some data. If we wrote everything, return out.
Otherwise loop around and do another read. */
/* We didn't write any data. Do a blocking write. */
DEBUG_MESSAGE1 (DEBUG_PORT,
"fsserial_io: Blocking write of %d", cdo);
/* Loop until we get something besides EINTR. */
while ((cdid = write (q->owrite, zwrite, cdo)) < 0
ulog (LOG_ERROR, (const char *) NULL);
ulog (LOG_ERROR, "write: %s", strerror (errno));
/* On some systems write will return 0 if carrier is
lost. If we fail to write anything ten times in a
row, we assume that this has happened. This is
hacked in like this because there seems to be no
reliable way to tell exactly why the write returned
ulog (LOG_ERROR, "Line disconnected");
/* I/O to a stdin port. */
fsysdep_stdin_io (qport, zwrite, pcwrite, zread, pcread)
return fsserial_io (&qport->u.sstdin.s.s, zwrite, pcwrite, zread, pcread);
/* I/O to a modem port. */
fsysdep_modem_io (qport, zwrite, pcwrite, zread, pcread)
return fsserial_io (&qport->u.smodem.s.s, zwrite, pcwrite, zread, pcread);
/* I/O to a direct port. */
fsysdep_direct_io (qport, zwrite, pcwrite, zread, pcread)
return fsserial_io (&qport->u.sdirect.s.s, zwrite, pcwrite, zread, pcread);
/* Send a break character to a serial port. */
struct ssysdep_serial_port *q;
ioctl (q->owrite, TIOCSBRK, 0);
ioctl (q->owrite, TIOCCBRK, 0);
#endif /* HAVE_BSD_TTY */
ioctl (q->owrite, TCSBRK, 0);
#endif /* HAVE_SYSV_TERMIO */
return tcsendbreak (q->owrite, 0) == 0;
#endif /* HAVE_POSIX_TERMIOS */
/* Send a break character to a stdin port. */
fsysdep_stdin_break (qport)
return fsserial_break (&qport->u.sstdin.s.s);
/* Send a break character to a modem port. */
fsysdep_modem_break (qport)
return fsserial_break (&qport->u.smodem.s.s);
/* Send a break character to a direct port. */
fsysdep_direct_break (qport)
return fsserial_break (&qport->u.sdirect.s.s);
/* Change the setting of a serial port. */
struct ssysdep_serial_port *q;
if (q->snew.sg_flags == (RAW | ANYP))
q->snew.sg_flags = RAW | ANYP;
#if HAVE_SYSV_TERMIO || HAVE_POSIX_TERMIOS
if ((q->snew.c_iflag & ICLEAR_IFLAG) == 0)
q->snew.c_iflag &=~ ICLEAR_IFLAG;
if (! fsetterminfodrain (q->oread, &q->snew))
ulog (LOG_ERROR, "Can't go to raw mode: %s", strerror (errno));
if (q->snew.sg_flags == (CBREAK | ANYP | TANDEM))
q->snew.sg_flags = CBREAK | ANYP | TANDEM;
#endif /* HAVE_BSD_TTY */
#if HAVE_SYSV_TERMIO | HAVE_POSIX_TERMIOS
/* It would be nice to do this is in a more portable fashion,
but in any case this is apparently correct for SunOS. If
we are doing hardware flow control, we don't also send
start and stop characters; however, we do recognize
incoming start and stop characters. */
if ((q->snew.c_cflag & CRTSCTS) != 0)
iwant = ISTRIP | IXON | IXOFF;
#else /* ! defined (CRTSCTS) */
iwant = ISTRIP | IXON | IXOFF;
#endif /* ! defined (CRTSCTS) */
if ((q->snew.c_iflag & ICLEAR_IFLAG) == iwant)
q->snew.c_iflag &=~ ICLEAR_IFLAG;
q->snew.c_iflag |= iwant;
#endif /* HAVE_SYSV_TERMIO | HAVE_POSIX_TERMIOS */
if (! fsetterminfodrain (q->oread, &q->snew))
ulog (LOG_ERROR, "Can't go to seven bit mode: %s",
ulog (LOG_FATAL, "fsserial_set: Can't happen");
/* Change settings of a stdin port. */
fsysdep_stdin_set (qport, tset)
return fsserial_set (&qport->u.sstdin.s.s, tset);
/* Change settings of a modem port. */
fsysdep_modem_set (qport, tset)
return fsserial_set (&qport->u.smodem.s.s, tset);
/* Change settings of a direct port. */
fsysdep_direct_set (qport, tset)
return fsserial_set (&qport->u.sdirect.s.s, tset);
/* Run a chat program. */
fsrun_chat (oread, owrite, zprog)
/* Get the arguments into an array to pass to isspawn. */
zcopy = (char *) alloca (strlen (zprog) + 1);
for (zarg = strtok (zcopy, " \t");
zarg = strtok ((char *) NULL, " \t"))
azargs = (const char **) alloca ((cargs + 1) * sizeof (const char *));
for (zarg = strtok (zcopy, " \t");
zarg = strtok ((char *) NULL, " \t"))
aidescs[2] = SPAWN_READ_PIPE;
/* Pass fkeepuid, fkeepenv and fshell as TRUE. This puts the
responsibility of maintaing security on the chat program. */
ipid = isspawn (azargs, aidescs, TRUE, TRUE, (const char *) NULL,
FALSE, TRUE, (const char *) NULL,
(const char *) NULL, (const char *) NULL);
ulog (LOG_ERROR, "isspawn (%s): %s", azargs[0], strerror (errno));
e = fdopen (aidescs[2], (char *) "r");
ulog (LOG_ERROR, "fdopen: %s", strerror (errno));
(void) close (aidescs[2]);
(void) kill (ipid, SIGKILL);
(void) iswait ((unsigned long) ipid, (const char *) NULL);
/* The FILE e now is attached to stderr of the program. Forward
every line the program outputs to the log file. */
while ((z = zfgets (e, FALSE)) != NULL)
ulog (LOG_NORMAL, "chat: %s", z);
return iswait ((unsigned long) ipid, "Chat program") == 0;
/* Run a chat program on a stdin port. */
fsysdep_stdin_chat (qport, zprog)
return fsrun_chat (qport->u.sstdin.s.s.oread,
qport->u.sstdin.s.s.owrite,
/* Run a chat program on a modem port. */
fsysdep_modem_chat (qport, zprog)
return fsrun_chat (qport->u.smodem.s.s.oread,
qport->u.smodem.s.s.owrite,
/* Run a chat program on a direct port. */
fsysdep_direct_chat (qport, zprog)
return fsrun_chat (qport->u.sdirect.s.s.oread,
qport->u.sdirect.s.s.owrite,
/* Run a chat program on a TCP port. */
fsysdep_tcp_chat (qport, zprog)
return fsrun_chat (qport->u.stcp.o, qport->u.stcp.o, zprog);
/* Functions to return baud rates. */
/* Return baud rate of a stdin port. */
isysdep_stdin_baud (qport)
return qport->u.sstdin.s.s.ibaud;
/* Return baud rate of a modem port. */
isysdep_modem_baud (qport)
return qport->u.smodem.s.s.ibaud;
/* Return baud rate of a direct port. */
isysdep_direct_baud (qport)
return qport->u.sdirect.s.s.ibaud;
/* Some system dependent routines for TCP ports. These work by
setting up an ssysdep_serial_port structure to fake out the serial
port routines. I'm doing it this way to avoid having to write the
complicated timeout code twice, and because the serial port code
will work fine. It does mean that if the serial port code changes
this code will have to be considered. */
/* Read data from a TCP port. */
fsysdep_tcp_read (qport, zread, pclen, cmin, ctimeout, freport)
struct ssysdep_serial_port s;
s.oread = s.owrite = qport->u.stcp.o;
return fsserial_read (&s, zread, pclen, cmin, ctimeout, freport, FALSE);
/* Write data to a TCP port. */
fsysdep_tcp_write (qport, zwrite, cwrite)
struct ssysdep_serial_port s;
s.oread = s.owrite = qport->u.stcp.o;
return fsserial_write (&s, zwrite, cwrite);
/* Read and write data to and from a TCP port. We actually don't
bother to really implement this, since the system will buffer up
plenty of TCP data (only 256 bytes are buffered for a terminal,
so losing data becomes a real possibility). */
fsysdep_tcp_io (qport, zwrite, pcwrite, zread, pcread)
struct ssysdep_serial_port s;
s.oread = s.owrite = qport->u.stcp.o;
return fsserial_write (&s, zwrite, *pcwrite);