From dcccae8226cd5c11f3081ff36015a16495333f7c Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Mon, 10 Dec 2018 07:49:41 -0800 Subject: [PATCH] conmux: Handle EOF from conmux If conmux goes away, e.g. by someone invoking ~$quit, the read() from the conmux fd will continously return 0, which is not discarded as an error, but sent as a console output message to the client, continuously. Quit bad when this happens, instead. Signed-off-by: Bjorn Andersson --- bad.c | 9 ++++++++- conmux.c | 13 +++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/bad.c b/bad.c index e0abf52..b38635d 100644 --- a/bad.c +++ b/bad.c @@ -228,6 +228,13 @@ void watch_add_readfd(int fd, int (*cb)(int, void*), void *data) list_add(&read_watches, &w->node); } +static bool quit_invoked; + +void watch_quit(void) +{ + quit_invoked = true; +} + int main(int argc, char **argv) { struct watch *w; @@ -243,7 +250,7 @@ int main(int argc, char **argv) flags = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); - for (;;) { + while (!quit_invoked) { nfds = 0; list_for_each_entry(w, &read_watches, node) { diff --git a/conmux.c b/conmux.c index b2e50b2..050b70c 100644 --- a/conmux.c +++ b/conmux.c @@ -215,10 +215,15 @@ static int conmux_data(int fd, void *data) if (n < 0) return n; - hdr.type = MSG_CONSOLE; - hdr.len = n; - write(STDOUT_FILENO, &hdr, sizeof(hdr)); - write(STDOUT_FILENO, buf, n); + if (!n) { + fprintf(stderr, "Received EOF from conmux\n"); + watch_quit(); + } else { + hdr.type = MSG_CONSOLE; + hdr.len = n; + write(STDOUT_FILENO, &hdr, sizeof(hdr)); + write(STDOUT_FILENO, buf, n); + } return 0; }