Skip to content

Commit

Permalink
tty: cleanly handle EOF
Browse files Browse the repository at this point in the history
The old code would loop forever trying to read 0 bytes and thinking that
the input fd was read-ready. This is normally not an issue, but because
we're emulating regular files as eventfds (since epoll won't support
them), things like bst --tty cat </dev/null would never terminate.
  • Loading branch information
Snaipe committed Jun 28, 2024
1 parent 3e280fd commit 2461d3d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
20 changes: 20 additions & 0 deletions fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <err.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -101,3 +102,22 @@ void rebind_fds_and_close_rest(int start_fd, ...)
err(1, "close_range");
}
}

/* close_null closes fd by rebinding it to /dev/null.
This is done to avoid leaving the old fd number unoccupied,
which can cause issues for the standard file descriptor numbers. */
void close_null(int fd)
{
int nfd = open("/dev/null", O_RDWR | O_CLOEXEC);
if (nfd == -1) {
err(1, "close_null: open /dev/null");
}

if (dup3(nfd, fd, O_CLOEXEC) == -1) {
err(1, "close_null: dup2 fd %d -> /dev/null", fd);
}

if (close(nfd) == -1) {
err(1, "close_null: close");
}
}
1 change: 1 addition & 0 deletions fd.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
int recv_fd(int socket);
void send_fd(int socket, int fd);
void rebind_fds_and_close_rest(int start_fd, ...);
void close_null(int fd);

#endif /* !FD_H */
60 changes: 43 additions & 17 deletions tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ static ssize_t io_copy(int out_fd, int in_fd, struct buffer *buf)
}
return -1;
}
if (rd == 0) {
return copied;
}
buf->size = (size_t) rd;
buf->index = 0;
}
Expand Down Expand Up @@ -134,26 +137,37 @@ static int send_veof(int ptm)
return write(ptm, &tios.c_cc[VEOF], 1);
}

void tty_parent_cleanup(void)
static void tty_parent_resetterm(void)
{
if (info.termfd >= 0) {
/* Drain any remaining data in the terminal buffer */
set_nonblock(STDOUT_FILENO, 0);
set_nonblock(info.termfd, 0);
struct buffer drain = {
.size = 0,
};
tcsetattr(STDIN_FILENO, TCSADRAIN, &info.orig);
info.stdinIsatty = false;
}

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

close_null(STDOUT_FILENO);
close(info.termfd);
info.termfd = -1;
}

close(info.termfd);
info.termfd = -1;
void tty_parent_cleanup(void)
{
if (info.termfd >= 0) {
tty_parent_drain();
}
if (info.stdinIsatty) {
tcsetattr(STDIN_FILENO, TCSADRAIN, &info.orig);
info.stdinIsatty = false;
tty_parent_resetterm();
}
}

Expand Down Expand Up @@ -235,6 +249,11 @@ static int tty_handle_io(int epollfd, const struct epoll_event *ev, int fd, pid_

inbound_handler.ready &= ~(READ_READY|WRITE_READY);

if (copied == 0) {
tty_parent_drain();
return EPOLL_HANDLER_CONTINUE;
}

struct epoll_event newev = {
.events = EPOLLIN | EPOLLONESHOT,
.data.ptr = &inbound_handler,
Expand Down Expand Up @@ -276,12 +295,18 @@ static int tty_handle_io(int epollfd, const struct epoll_event *ev, int fd, pid_
/* outbound_handler.fd might contain our eventfd workaround */
int write_fd = STDOUT_FILENO;

if (io_copy(write_fd, read_fd, &outbound_buffer) == -1) {
ssize_t copied = io_copy(write_fd, read_fd, &outbound_buffer);
if (copied == -1) {
err(1, "copy tty -> stdout");
}

outbound_handler.ready = 0;

if (copied == 0) {
close_null(write_fd);
return EPOLL_HANDLER_CONTINUE;
}

struct epoll_event newev = {
.events = EPOLLOUT | EPOLLONESHOT,
.data.ptr = &outbound_handler,
Expand Down Expand Up @@ -338,8 +363,9 @@ void tty_parent_setup(struct tty_opts *opts, int epollfd, int socket)
/* We changed the terminal to raw mode. Line-endings now need carriage
returns in order to be palatable. */
err_line_ending = "\r\n";

atexit(tty_parent_resetterm);
}
atexit(tty_parent_cleanup);

// Wait for the child to create the pty pair and pass the master back.
info.termfd = recv_fd(socket);
Expand Down

0 comments on commit 2461d3d

Please sign in to comment.