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 <bjorn.andersson@linaro.org>
This commit is contained in:
Bjorn Andersson
2018-12-10 07:49:41 -08:00
parent 5da489beee
commit dcccae8226
2 changed files with 17 additions and 5 deletions

9
bad.c
View File

@@ -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) {

View File

@@ -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;
}