From 2c363f561e7aa9da67143cb21f039857be36a72f Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 12 May 2020 13:44:57 +0200 Subject: [PATCH] tty: flush pending data when fd is ready if there was any data already received, make sure it is flushed to the tty as soon as it is opened. Closes: https://github.com/containers/libpod/issues/4397 Signed-off-by: Giuseppe Scrivano --- src/conn_sock.c | 10 +++++++--- src/conn_sock.h | 1 + src/ctrl.c | 3 +++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/conn_sock.c b/src/conn_sock.c index 4ffa5783..3a09eba6 100644 --- a/src/conn_sock.c +++ b/src/conn_sock.c @@ -180,8 +180,7 @@ static gboolean read_conn_sock(struct conn_sock_s *sock) /* Not everything was written to stdin, let's wait for the fd to be ready. */ if (sock->remaining) - g_unix_fd_add(masterfd_stdin, G_IO_OUT, masterfd_write_cb, NULL); - + schedule_master_stdin_write(); return G_SOURCE_CONTINUE; } @@ -240,7 +239,7 @@ static void write_to_masterfd_stdin(gpointer data, gpointer user_data) static void sock_try_write_to_masterfd_stdin(struct conn_sock_s *sock) { - if (!sock->remaining) + if (!sock->remaining || masterfd_stdin < 0) return; ssize_t w = write(masterfd_stdin, sock->buf + sock->off, sock->remaining); @@ -264,3 +263,8 @@ static gboolean masterfd_write_cb(G_GNUC_UNUSED int fd, G_GNUC_UNUSED GIOConditi return G_SOURCE_CONTINUE; return G_SOURCE_REMOVE; } + +void schedule_master_stdin_write() +{ + g_unix_fd_add(masterfd_stdin, G_IO_OUT, masterfd_write_cb, NULL); +} diff --git a/src/conn_sock.h b/src/conn_sock.h index cf876e70..24286f78 100644 --- a/src/conn_sock.h +++ b/src/conn_sock.h @@ -18,5 +18,6 @@ struct conn_sock_s { char *setup_console_socket(void); char *setup_attach_socket(void); void conn_sock_shutdown(struct conn_sock_s *sock, int how); +void schedule_master_stdin_write(); #endif // CONN_SOCK_H diff --git a/src/ctrl.c b/src/ctrl.c index 48f0f0cd..d6764d91 100644 --- a/src/ctrl.c +++ b/src/ctrl.c @@ -5,6 +5,7 @@ #include "globals.h" #include "config.h" #include "ctr_logging.h" +#include "conn_sock.h" #include "cmsg.h" #include "cli.h" // opt_bundle_path @@ -59,6 +60,8 @@ gboolean terminal_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC masterfd_stdin = console.fd; masterfd_stdout = console.fd; + schedule_master_stdin_write(); + /* now that we've set masterfd_stdout, we can register the ctrl_winsz_cb * if we didn't set it here, we'd risk attempting to run ioctl on * a negative fd, and fail to resize the window */