Skip to content

Commit

Permalink
tty: ensure we send the right VEOF control character
Browse files Browse the repository at this point in the history
I noticed that bst would hang when the inner shell changed its eof
control character. This is because we assumed that the inner tty would
simply keep accepting EOT as end of input.

This does not solve this class of problems, however; it's possible that
an inner process can still hang itself by racing a change of the VEOF
character with the outer end of input. This is very unlikely to happen
however, and only causes the inner process to stop functioning.

For the common, sane case, probing the termios before sending the right
VEOF is the correct thing to do.
  • Loading branch information
Snaipe committed Jun 14, 2021
1 parent c4046bc commit 7ea4b44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions test/tty.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ Check that redirections still work

$ bst --tty echo hello | cat
hello

Ensure we send the correct VEOF control character

$ yes '' | head -c 32768 | timeout 1 bst --tty sh -c 'stty eof ^B && cat' >/dev/null
16 changes: 13 additions & 3 deletions tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ static void set_nonblock(int fd, int nonblock)
}
}

static void send_veof(int ptm)
{
struct termios tios;
if (tcgetattr(info.termfd, &tios) == -1) {
err(1, "send_veof: tcgetattr");
}

if (write(ptm, &tios.c_cc[VEOF], 1) == -1) {
err(1, "send_veof: write");
}
}

void tty_parent_cleanup(void)
{
if (info.termfd >= 0) {
Expand Down Expand Up @@ -296,9 +308,7 @@ static int tty_handle_io(int epollfd, const struct epoll_event *ev, int fd, pid_
err(1, "epoll_ctl_mod stdin");
}
} else if ((inbound_handler.ready & (WRITE_READY | HANGUP)) == (WRITE_READY | HANGUP)) {
if (write(inbound_handler.peer_fd, &(char){4}, 1) < 0) {
err(1, "writing EOT to terminal");
}
send_veof(inbound_handler.peer_fd);
inbound_handler.ready &= ~HANGUP;
}

Expand Down

0 comments on commit 7ea4b44

Please sign in to comment.