From 972ca8ab949ee342569dbd66b47cc4a17b28247b Mon Sep 17 00:00:00 2001 From: Jeremy Bobbin Date: Sat, 30 Jan 2021 01:02:27 -0800 Subject: [PATCH] fix bug where attaching to dead session won't give underlying exit code If dvtm dies, and you run `:|abduco -a session`, abduco will often exit without giving you the exit code, and without removing the socket from ~/.abduco. We resolve this by first sending the client either an EXIT or an empty CONTENT packet. Only after the client recieves it, we set the socket to non-blocking. Fixes: https://github.com/martanne/abduco/issues/44 --- abduco.c | 24 +++++++++++++++++++----- client.c | 7 ------- server.c | 17 ++++++++++++++++- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/abduco.c b/abduco.c index b56e6f4..e4c2138 100644 --- a/abduco.c +++ b/abduco.c @@ -518,8 +518,6 @@ static bool attach_session(const char *name, const bool terminate) { close(server.socket); if ((server.socket = session_connect(name)) == -1) return false; - if (server_set_socket_non_blocking(server.socket) == -1) - return false; struct sigaction sa; sa.sa_flags = 0; @@ -529,9 +527,25 @@ static bool attach_session(const char *name, const bool terminate) { sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); - client_setup_terminal(); - int status = client_mainloop(); - client_restore_terminal(); + Packet pkt = { + .type = MSG_ATTACH, + .u.i = client.flags, + .len = sizeof(pkt.u.i), + }; + client_send_packet(&pkt); + + int status; + if (client_recv_packet(&pkt) && pkt.type == MSG_PID) { + if (server_set_socket_non_blocking(server.socket) == -1) + return false; + client_setup_terminal(); + status = client_mainloop(); + client_restore_terminal(); + } else if (pkt.type == MSG_EXIT) { + status = pkt.u.i; + client_send_packet(&pkt); + close(server.socket); + } if (status == -1) { info("detached"); } else if (status == -EIO) { diff --git a/client.c b/client.c index 3d6d82b..c536e92 100644 --- a/client.c +++ b/client.c @@ -63,13 +63,6 @@ static int client_mainloop(void) { sigprocmask(SIG_BLOCK, &blockset, NULL); client.need_resize = true; - Packet pkt = { - .type = MSG_ATTACH, - .u.i = client.flags, - .len = sizeof(pkt.u.i), - }; - client_send_packet(&pkt); - while (server.running) { fd_set fds; FD_ZERO(&fds); diff --git a/server.c b/server.c index e57773a..babd90b 100644 --- a/server.c +++ b/server.c @@ -222,11 +222,26 @@ static void server_mainloop(void) { case MSG_CONTENT: server_write_pty(&client_packet); break; - case MSG_ATTACH: + case MSG_ATTACH: { + Packet pkt; + if (!server.running && server.exit_status != -1) { + pkt = (Packet) { + .type = MSG_EXIT, + .u.i = server.exit_status, + .len = sizeof(pkt.u.i), + }; + } else { + pkt = (Packet) { + .type = MSG_CONTENT, + .len = 0, + }; + } + server_send_packet(c, &pkt); c->flags = client_packet.u.i; if (c->flags & CLIENT_LOWPRIORITY) server_sink_client(); break; + } case MSG_RESIZE: c->state = STATE_ATTACHED; if (!(c->flags & CLIENT_READONLY) && c == server.clients) {