Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tty: flush pending data when fd is ready #150

Merged
merged 2 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static:
$(MAKE) git-vars bin/conmon PKG_CONFIG='$(PKG_CONFIG) --static' CFLAGS='-static' LDFLAGS='$(LDFLAGS) -s -w -static' LIBS='$(LIBS)'

bin/conmon: $(OBJS) | bin
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBS)

%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -o $@ -c $<
Expand Down
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();
giuseppe marked this conversation as resolved.
Show resolved Hide resolved

/* 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