Skip to content

Commit

Permalink
add an option to skip draining on tty close (#105)
Browse files Browse the repository at this point in the history
* add an option to skip draining on tty close

When ssh connections to buses are terminated bst is blocking while
draining the pty.  The signal handler does to run in this case and
the process hangs around until the bus is rebooted.

Added an option to control whether or not the pty is drained when
stdin is closed.  The b5c bst invocation will pass --tty=-drain.
  • Loading branch information
mstory21 authored Dec 23, 2024
1 parent ff53083 commit 3f97bb7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ int main(int argc, char *argv[], char *envp[])
{
opts.tty = 1;
opts.ttyopts.ptmx = tty_default_ptmx;
opts.ttyopts.drain = true;

/* 128 is enough to support everything */
struct kvlist kvlist[128];
Expand Down
3 changes: 3 additions & 0 deletions man/bst.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ spacetime process.
- *ptmx*=<path>: use the specified ptmx device (relative to the target root)
to allocated the pty.

- *drain*: whether to drain the tty on stdin closure. The default is true,
so prepend a _'-'_ to this option to prevent draining.

By default bst inherits the parent's terminal device (or lack thereof). Use
the --tty option to allocate a new pty for the child process.

Expand Down
31 changes: 23 additions & 8 deletions tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ static struct tty_parent_info_s {
int termfd;
struct termios orig;
bool stdinIsatty;
bool drain;
} info = {
.termfd = -1,
.drain = true,
};

static ssize_t io_copy(int out_fd, int in_fd, struct buffer *buf)
Expand Down Expand Up @@ -145,15 +147,17 @@ static void tty_parent_resetterm(void)

static void tty_parent_drain(void)
{
/* Drain any remaining data in the terminal buffer */
set_nonblock(STDOUT_FILENO, 0);
set_nonblock(info.termfd, 0);
struct buffer drain = {
.size = 0,
};
if (info.drain) {
/* Drain any remaining data in the terminal buffer */
set_nonblock(STDOUT_FILENO, 0);
set_nonblock(info.termfd, 0);
struct buffer drain = {
.size = 0,
};

if (io_copy(STDOUT_FILENO, info.termfd, &drain) == -1 && errno != EIO) {
warn("copy tty -> stdout");
if (io_copy(STDOUT_FILENO, info.termfd, &drain) == -1 && errno != EIO) {
warn("copy tty -> stdout");
}
}

close_null(STDOUT_FILENO);
Expand Down Expand Up @@ -358,6 +362,8 @@ void tty_parent_setup(struct tty_opts *opts, int epollfd, int socket)

struct termios tios;

info.drain = opts->drain;

info.stdinIsatty = tcgetattr(STDIN_FILENO, &tios) == 0;
if (!info.stdinIsatty && errno != ENOTTY) {
err(1, "tty_parent: tcgetattr");
Expand Down Expand Up @@ -656,6 +662,14 @@ static void parse_ptmx(struct tty_opts *opts, const char *key, const char *val,
opts->ptmx = val;
}

static void parse_drain(struct tty_opts *opts, const char *key, const char *val, const void *cookie)
{
if (val != NULL) {
errx(2, "tty option '%s' must have no value", key);
}
opts->drain = (key[0] != '-');
}

static int cmp_flag(const void *key, const void *elem)
{
return strcmp(key, ((const struct valmap *)elem)->name);
Expand All @@ -675,6 +689,7 @@ void tty_opt_parse(struct tty_opts *opts, const char *key, const char *val)
{ "cread", parse_flag, .cookie = &(const struct termios) { .c_cflag = CREAD } },
{ "crtscts", parse_flag, .cookie = &(const struct termios) { .c_cflag = CRTSCTS } },
{ "cstopb", parse_flag, .cookie = &(const struct termios) { .c_cflag = CSTOPB } },
{ "drain", parse_drain, .cookie = NULL },
{ "echo", parse_flag, .cookie = &(const struct termios) { .c_lflag = ECHO } },
{ "echoctl", parse_flag, .cookie = &(const struct termios) { .c_lflag = ECHOCTL } },
{ "echoe", parse_flag, .cookie = &(const struct termios) { .c_lflag = ECHOE } },
Expand Down
1 change: 1 addition & 0 deletions tty.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct tty_opts {
const char *ptmx;
struct termios termios;
struct termios neg_termios;
bool drain;
};

extern const char *tty_default_ptmx;
Expand Down

0 comments on commit 3f97bb7

Please sign in to comment.