Skip to content

Commit

Permalink
tty: flush pending data when fd is ready
Browse files Browse the repository at this point in the history
if there was any data already received, make sure it is flushed to the
tty as soon as it is opened.

Closes: containers/podman#4397

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe authored and haircommander committed May 12, 2020
1 parent d4da866 commit edd4aaa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/conn_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
1 change: 1 addition & 0 deletions src/conn_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -59,6 +60,10 @@ gboolean terminal_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC
masterfd_stdin = console.fd;
masterfd_stdout = console.fd;

/* Now that we have a fd to the tty, make sure we handle any pending data
* that was already buffered. */
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 */
Expand Down

0 comments on commit edd4aaa

Please sign in to comment.