System dependent routines for cu.
Copyright (C) 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 Infinity Development Systems, P.O. Box 520, Waltham, MA 02254.
const char cusub_rcsid
[] = "$Id: cusub.c,v 1.1 1993/08/04 19:32:09 jtc Exp $";
/* Get definitions for EAGAIN, EWOULDBLOCK and ENODATA. */
#else /* defined (EWOULDBLOCK) */
#define EAGAIN EWOULDBLOCK
#endif /* defined (EWOULDBLOCK) */
#else /* defined (EAGAIN) */
#define EWOULDBLOCK EAGAIN
#endif /* ! defined (EWOULDBLOCK) */
#endif /* defined (EAGAIN) */
/* The EOF character, as set by fsysdep_terminal_raw. */
/* The SUSP character, as set by fsysdep_terminal_raw. */
static const char *zsport_line
P((const struct uuconf_port
*qport
));
static void uscu_child
P((struct sconnection
*qconn
, int opipe
));
static RETSIGTYPE uscu_alarm
P((int isig
));
static int cscu_escape
P((char *pbcmd
, const char *zlocalname
));
static RETSIGTYPE uscu_alarm_kill
P((int isig
));
/* Return the device name for a port, or NULL if none. */
const struct uuconf_port
*qport
;
switch (qport
->uuconf_ttype
)
case UUCONF_PORTTYPE_STDIN
:
case UUCONF_PORTTYPE_MODEM
:
zline
= qport
->uuconf_u
.uuconf_smodem
.uuconf_zdevice
;
case UUCONF_PORTTYPE_DIRECT
:
zline
= qport
->uuconf_u
.uuconf_sdirect
.uuconf_zdevice
;
case UUCONF_PORTTYPE_TCP
:
case UUCONF_PORTTYPE_TLI
:
zline
= qport
->uuconf_zname
;
/* Check whether the user has legitimate access to a port. */
fsysdep_port_access (qport
)
struct uuconf_port
*qport
;
zline
= zsport_line (qport
);
zfree
= zbufalc (sizeof "/dev/" + strlen (zline
));
sprintf (zfree
, "/dev/%s", zline
);
fret
= access (zline
, R_OK
| W_OK
) == 0;
/* Return whether the given port is named by the given line. */
fsysdep_port_is_line (qport
, zline
)
struct uuconf_port
*qport
;
zpline
= zsport_line (qport
);
if (strcmp (zline
, zpline
) == 0)
zfree1
= zbufalc (sizeof "/dev/" + strlen (zline
));
sprintf (zfree1
, "/dev/%s", zline
);
zfree2
= zbufalc (sizeof "/dev/" + strlen (zpline
));
sprintf (zfree2
, "/dev/%s", zpline
);
fret
= strcmp (zline
, zpline
) == 0;
/* The cu program wants the system dependent layer to handle the
details of copying data from the communications port to the
terminal. This copying need only be done while executing
fsysdep_cu. On Unix, however, we set up a subprocess to do it all
the time. This subprocess must be controllable via the
fsysdep_cu_copy function.
We keep a pipe open to the subprocess. When we want it to stop we
send it a signal, and then wait for it to write a byte to us over
/* The subprocess pid. */
static volatile pid_t iSchild
;
/* The pipe from the subprocess. */
/* When we tell the child to stop, it sends this. */
#define CHILD_STOPPED ('S')
/* When we tell the child to start, it sends this. */
#define CHILD_STARTED ('G')
/* Initialize the subprocess, and have it start copying data. */
struct sconnection
*qconn
;
/* Write out anything we may have buffered up during the chat
script. We do this before forking the child only to make it easy
to move the child into a separate executable. */
while (iPrecend
!= iPrecstart
)
z
= abPrecbuf
+ iPrecstart
;
if (iPrecend
> iPrecstart
)
c
= iPrecend
- iPrecstart
;
c
= CRECBUFLEN
- iPrecstart
;
iPrecstart
= (iPrecstart
+ c
) % CRECBUFLEN
;
cwrote
= write (1, z
, c
);
ulog (LOG_ERROR
, "write: %s", strerror (errno
));
ulog (LOG_ERROR
, "Line disconnected");
ulog (LOG_ERROR
, "pipe: %s", strerror (errno
));
ulog (LOG_ERROR
, "fork: %s", strerror (errno
));
uscu_child (qconn
, ai
[1]);
/* Copy all data from the terminal to the communications port. If we
see an escape character following a newline character, read the
next character and return it. */
fsysdep_cu (qconn
, pbcmd
, zlocalname
)
struct sconnection
*qconn
;
ulog (LOG_ERROR
, (const char *) NULL
);
if (fstart
&& b
== *zCuvar_escape
)
c
= cscu_escape (pbcmd
, zlocalname
);
/* For Unix, we let the eof character be the same as
'.', and we let the suspend character (if any) be the
if (! fconn_write (qconn
, &b
, (size_t) 1))
fstart
= strchr (zCuvar_eol
, b
) != NULL
;
ulog (LOG_ERROR
, "read: %s", strerror (errno
));
ulog (LOG_ERROR
, (const char *) NULL
);
/* I'm not sure what's best in this case. */
ulog (LOG_ERROR
, "End of file on terminal");
/* A SIGALRM handler that sets fScu_alarm and optionally longjmps. */
volatile sig_atomic_t fScu_alarm
;
#if ! HAVE_SIGACTION && ! HAVE_SIGVEC && ! HAVE_SIGSET
(void) signal (isig
, uscu_alarm
);
#if HAVE_RESTARTABLE_SYSCALLS
/* We've just seen an escape character. We print the host name,
optionally after a 1 second delay. We read the next character from
the terminal and return it. The 1 second delay on the host name is
mostly to be fancy; it lets ~~ look smoother. */
cscu_escape (pbcmd
, zlocalname
)
write (1, zCuvar_escape
, 1);
usset_signal (SIGALRM
, uscu_alarm
, TRUE
, (boolean
*) NULL
);
write (1, zlocalname
, strlen (zlocalname
));
if (c
>= 0 || errno
!= EINTR
)
usset_signal (SIGALRM
, SIG_IGN
, TRUE
, (boolean
*) NULL
);
/* A SIGALRM handler which does nothing but send a signal to the child
process and schedule another alarm. POSIX.1 permits kill and alarm
from a signal handler. The reference to static data may or may not
static volatile sig_atomic_t iSsend_sig
;
#if ! HAVE_SIGACTION && ! HAVE_SIGVEC && ! HAVE_SIGSET
(void) signal (isig
, uscu_alarm_kill
);
(void) kill (iSchild
, iSsend_sig
);
/* Start or stop copying data from the communications port to the
terminal. We send a signal to the child process to tell it what to
do. Unfortunately, there are race conditions in the child, so we
keep sending it a signal once a second until it responds. We send
SIGUSR1 to make it start copying, and SIGUSR2 to make it stop. */
usset_signal (SIGALRM
, uscu_alarm_kill
, TRUE
, (boolean
*) NULL
);
uscu_alarm_kill (SIGALRM
);
c
= read (oSpipe
, &b
, 1);
DEBUG_MESSAGE1 (DEBUG_INCOMING
,
"fsysdep_cu_copy: Got '%d'", b
);
if ((c
< 0 && errno
!= EINTR
)
|| (c
> 0 && b
== (fcopy
? CHILD_STARTED
: CHILD_STOPPED
)))
/* If none of the above conditions were true, then we either got
an EINTR error, in which case we probably timed out and the
SIGALRM handler resent the signal, or we read the wrong
character, in which case we will just read again from the
usset_signal (SIGALRM
, SIG_IGN
, TRUE
, (boolean
*) NULL
);
ulog (LOG_ERROR
, "EOF on child pipe");
ulog (LOG_ERROR
, "read: %s", strerror (ierr
));
/* Shut down cu by killing the child process. */
/* We hit the child with SIGTERM, give it two seconds to die, and
if (kill (iSchild
, SIGTERM
) < 0)
/* Don't give an error if the child has already died. */
ulog (LOG_ERROR
, "kill: %s", strerror (errno
));
usset_signal (SIGALRM
, uscu_alarm_kill
, TRUE
, (boolean
*) NULL
);
(void) ixswait ((unsigned long) iSchild
, "child");
usset_signal (SIGALRM
, SIG_IGN
, TRUE
, (boolean
*) NULL
);
/* Code for the child process. */
/* This signal handler just records the signal. In this case we only
care about which signal we received most recently. */
static volatile sig_atomic_t iSchild_sig
;
uscu_child_handler (isig
)
#if ! HAVE_SIGACTION && ! HAVE_SIGVEC && ! HAVE_SIGSET
(void) signal (isig
, uscu_child_handler
);
#if HAVE_RESTARTABLE_SYSCALLS
#endif /* HAVE_RESTARTABLE_SYSCALLS */
/* The child process. This copies the port to the terminal, except
when it is stopped by a signal. It would be reasonable to write a
separate program for this, probably passing it the port on stdin.
This would reduce the memory requirements, since we wouldn't need a
second process holding all the configuration stuff, and also let it
work reasonably on 680x0 versions of MINIX. */
uscu_child (qconn
, opipe
)
struct sconnection
*qconn
;
CATCH_PROTECT boolean fstopped
, fgot
;
CATCH_PROTECT
int cwrite
;
CATCH_PROTECT
char abbuf
[1024];
/* It would be nice if we could just use fsserial_read, but that
will log signals that we don't want logged. There should be a
generic way to extract the file descriptor from the port. */
if (qconn
->qport
== NULL
)
switch (qconn
->qport
->uuconf_ttype
)
ulog (LOG_FATAL
, "uscu_child: Can't happen");
case UUCONF_PORTTYPE_STDIN
:
case UUCONF_PORTTYPE_MODEM
:
case UUCONF_PORTTYPE_DIRECT
:
case UUCONF_PORTTYPE_TCP
:
case UUCONF_PORTTYPE_TLI
:
oport
= ((struct ssysdep_conn
*) qconn
->psysdep
)->o
;
usset_signal (SIGUSR1
, uscu_child_handler
, TRUE
, (boolean
*) NULL
);
usset_signal (SIGUSR2
, uscu_child_handler
, TRUE
, (boolean
*) NULL
);
usset_signal (SIGINT
, SIG_IGN
, TRUE
, (boolean
*) NULL
);
usset_signal (SIGQUIT
, SIG_IGN
, TRUE
, (boolean
*) NULL
);
usset_signal (SIGPIPE
, SIG_DFL
, TRUE
, (boolean
*) NULL
);
usset_signal (SIGTERM
, uscu_child_handler
, TRUE
, (boolean
*) NULL
);
/* There is a race condition here between checking the signal
and receiving a new and possibly different one. This is
solved by having the parent resend the signal until it gets a
c
= write (opipe
, &b
, 1);
/* Apparently on some systems we can get EAGAIN here. */
(errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== ENODATA
))
/* Should we give an error message here? */
(void) kill (getppid (), SIGHUP
);
c
= write (1, zbuf
, cwrite
);
/* Apparently on some systems we can get EAGAIN here. */
if (c
< 0 && errno
== EINTR
)
/* Should we give an error message here? */
(void) kill (getppid (), SIGHUP
);
/* On some systems apparently read will return 0 until
something has been written to the port. We therefore
accept a 0 return until after we have managed to read
something. Setting errno to 0 apparently avoids a
c
= read (oport
, abbuf
, sizeof abbuf
);
/* Apparently on some systems we can get EAGAIN here. */
(errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== ENODATA
))
|| (c
< 0 && errno
!= EINTR
))
/* This can be a normal way to exit, depending on just
how the connection is dropped. */
(void) kill (getppid (), SIGHUP
);
/* Terminal control routines. */
/* Whether file descriptor 0 is attached to a terminal or not. */
/* Whether we are doing local echoing. */
static boolean fSlocalecho
;
/* The original state of the terminal. */
static sterminal sSterm_orig
;
/* The new state of the terminal. */
static sterminal sSterm_new
;
/* Whether SIGTSTP is being ignored. */
static boolean fStstp_ignored
;
/* Set the terminal into raw mode. */
fsysdep_terminal_raw (flocalecho
)
fSlocalecho
= flocalecho
;
/* This defaults may be overriden below. */
if (! fgetterminfo (0, &sSterm_orig
))
sSterm_new
= sSterm_orig
;
/* We use CBREAK mode rather than RAW mode, because RAW mode turns
off all output processing, which we don't want to do. This means
that we have to disable the interrupt characters, which we do by
bSeof
= sSterm_orig
.stchars
.t_eofc
;
sSterm_new
.stchars
.t_intrc
= -1;
sSterm_new
.stchars
.t_quitc
= -1;
sSterm_new
.stchars
.t_startc
= -1;
sSterm_new
.stchars
.t_stopc
= -1;
sSterm_new
.stchars
.t_eofc
= -1;
sSterm_new
.stchars
.t_brkc
= -1;
bStstp
= sSterm_orig
.sltchars
.t_suspc
;
sSterm_new
.sltchars
.t_suspc
= -1;
sSterm_new
.sltchars
.t_dsuspc
= -1;
sSterm_new
.sltchars
.t_rprntc
= -1;
sSterm_new
.sltchars
.t_flushc
= -1;
sSterm_new
.sltchars
.t_werasc
= -1;
sSterm_new
.sltchars
.t_lnextc
= -1;
sSterm_new
.stty
.sg_flags
|= (CBREAK
| ANYP
);
sSterm_new
.stty
.sg_flags
&=~ (ECHO
| CRMOD
| TANDEM
);
sSterm_new
.stty
.sg_flags
|= (CBREAK
| ANYP
| ECHO
);
sSterm_new
.stty
.sg_flags
&=~ (CRMOD
| TANDEM
);
#endif /* HAVE_BSD_TTY */
bSeof
= sSterm_new
.c_cc
[VEOF
];
sSterm_new
.c_lflag
&=~ (ICANON
| ISIG
| ECHO
| ECHOE
| ECHOK
| ECHONL
);
sSterm_new
.c_lflag
&=~ (ICANON
| ISIG
);
sSterm_new
.c_iflag
&=~ (INLCR
| IGNCR
| ICRNL
);
sSterm_new
.c_oflag
&=~ (OPOST
);
sSterm_new
.c_cc
[VMIN
] = 1;
sSterm_new
.c_cc
[VTIME
] = 0;
#endif /* HAVE_SYSV_TERMIO */
bSeof
= sSterm_new
.c_cc
[VEOF
];
bStstp
= sSterm_new
.c_cc
[VSUSP
];
(ICANON
| IEXTEN
| ISIG
| ECHO
| ECHOE
| ECHOK
| ECHONL
);
sSterm_new
.c_lflag
&=~ (ICANON
| IEXTEN
| ISIG
);
sSterm_new
.c_iflag
&=~ (INLCR
| IGNCR
| ICRNL
);
sSterm_new
.c_oflag
&=~ (OPOST
);
sSterm_new
.c_cc
[VMIN
] = 1;
sSterm_new
.c_cc
[VTIME
] = 0;
#endif /* HAVE_POSIX_TERMIOS */
if (! fsetterminfo (0, &sSterm_new
))
ulog (LOG_ERROR
, "Can't set terminal settings: %s", strerror (errno
));
/* Restore the terminal to its original setting. */
fsysdep_terminal_restore ()
if (! fsetterminfo (0, &sSterm_orig
))
ulog (LOG_ERROR
, "Can't restore terminal: %s", strerror (errno
));
/* Read a line from the terminal. This will be called after
fsysdep_terminal_raw has been called. */
zsysdep_terminal_line (zprompt
)
CATCH_PROTECT
size_t cbuf
= 0;
CATCH_PROTECT
char *zbuf
= NULL
;
CATCH_PROTECT
size_t cgot
= 0;
if (zprompt
!= NULL
&& *zprompt
!= '\0')
(void) write (1, zprompt
, strlen (zprompt
));
/* Forgot about any previous SIGINT or SIGQUIT signals we may have
received. We don't worry about the race condition here, since we
can't get these signals from the terminal at the moment and it's
not too likely that somebody else will be sending them to us. */
afSignal
[INDEXSIG_SIGINT
] = 0;
afSignal
[INDEXSIG_SIGQUIT
] = 0;
if (! fsysdep_terminal_restore ())
if (afSignal
[INDEXSIG_SIGINT
]
|| afSignal
[INDEXSIG_SIGQUIT
])
/* Make sure the signal is logged. */
ulog (LOG_ERROR
, (const char *) NULL
);
/* Return an empty string. */
/* There's a race here between checking the signals and calling
read. It just means that the user will have to hit ^C more
ulog (LOG_ERROR
, "read: %s", strerror (errno
));
(void) fsysdep_terminal_raw (fSlocalecho
);
/* I'm not quite sure what to do here. */
ulog (LOG_ERROR
, "EOF on terminal");
(void) fsysdep_terminal_raw (fSlocalecho
);
memcpy (znew
, zbuf
, cgot
);
memcpy (znew
, zbuf
, cgot
);
if (! fsysdep_terminal_raw (fSlocalecho
))
/* Write a line to the terminal with a trailing newline. */
fsysdep_terminal_puts (zline
)
zalc
= zbufalc (clen
+ 2);
memcpy (zalc
, zline
, clen
);
c
= write (1, zprint
, clen
);
ulog (LOG_ERROR
, "write: %s", strerror (errno
));
/* Allow or disallow signals from the terminal. */
fsysdep_terminal_signals (faccept
)
sSterm_new
.stchars
.t_intrc
= sSterm_orig
.stchars
.t_intrc
;
sSterm_new
.stchars
.t_quitc
= sSterm_orig
.stchars
.t_quitc
;
sSterm_new
.stchars
.t_intrc
= -1;
sSterm_new
.stchars
.t_quitc
= -1;
#else /* ! HAVE_BSD_TTY */
sSterm_new
.c_lflag
|= ISIG
;
sSterm_new
.c_lflag
&=~ ISIG
;
/* We only want to get SIGINT and SIGQUIT, not SIGTSTP. This
function will be called with faccept TRUE before it is called
with faccept FALSE, so fStstp_ignored will be correctly
usset_signal (SIGTSTP
, SIG_IGN
, FALSE
, &fStstp_ignored
);
else if (! fStstp_ignored
)
usset_signal (SIGTSTP
, SIG_DFL
, TRUE
, (boolean
*) NULL
);
#endif /* ! HAVE_BSD_TTY */
if (! fsetterminfo (0, &sSterm_new
))
ulog (LOG_ERROR
, "Can't set terminal: %s", strerror (errno
));
/* Start up a command, or possibly just a shell. Optionally attach
stdin or stdout to the port. We attach directly to the port,
rather than copying the data ourselves. */
fsysdep_shell (qconn
, zcmd
, tcmd
)
struct sconnection
*qconn
;
if (zcmd
== NULL
|| *zcmd
== '\0')
if (qconn
->qport
== NULL
)
switch (qconn
->qport
->uuconf_ttype
)
case UUCONF_PORTTYPE_STDIN
:
case UUCONF_PORTTYPE_MODEM
:
case UUCONF_PORTTYPE_DIRECT
:
case UUCONF_PORTTYPE_TCP
:
case UUCONF_PORTTYPE_TLI
:
oread
= owrite
= ((struct ssysdep_conn
*) qconn
->psysdep
)->o
;
if (tcmd
== SHELL_STDIN_FROM_PORT
|| tcmd
== SHELL_STDIO_ON_PORT
)
if (tcmd
== SHELL_STDOUT_TO_PORT
|| tcmd
== SHELL_STDIO_ON_PORT
)
ipid
= ixsspawn (azargs
, aidescs
, FALSE
, TRUE
, (const char *) NULL
,
FALSE
, FALSE
, (const char *) NULL
,
(const char *) NULL
, (const char *) NULL
);
ulog (LOG_ERROR
, "ixsspawn (/bin/sh): %s", strerror (errno
));
return ixswait ((unsigned long) ipid
, "shell") == 0;
/* Change directories. */
if (zdir
== NULL
|| *zdir
== '\0')
ulog (LOG_ERROR
, "HOME not defined");
ulog (LOG_ERROR
, "chdir (%s): %s", zdir
, strerror (errno
));
/* Suspend the current process. */
return fsysdep_terminal_puts ("[process suspension not supported]");
return kill (getpid (), SIGTSTP
) == 0;