From 62844c2509a96feca2e7beb0db7bac3bb504ae3a Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Sun, 15 Sep 2019 19:06:47 +0800 Subject: [PATCH] src: fix exit code check on linux --- src/protocol.c | 17 +++++++++++------ src/server.c | 8 ++++++++ src/server.h | 1 + 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/protocol.c b/src/protocol.c index 108284f..ce496c7 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -133,9 +133,9 @@ tty_client_destroy(struct tty_client *client) { lwsl_err("kill: %d, errno: %d (%s)\n", pid, errno, strerror(errno)); } pid_t pid_out; - int status = wait_proc(client->pid, &pid_out); + client->exit_status = wait_proc(client->pid, &pid_out); if (pid_out > 0) { - lwsl_notice("process exited with code %d, pid: %d\n", status, pid_out); + lwsl_notice("process exited with code %d, pid: %d\n", client->exit_status, pid_out); } close(client->pty); @@ -196,7 +196,14 @@ spawn_process(struct tty_client *client) { void tty_client_poll(struct tty_client *client) { - if (!client->running || client->state == STATE_READY) return; + if (client->pid <= 0 || client->state == STATE_READY) return; + + if (!client->running) { + memset(client->pty_buffer, 0, sizeof(client->pty_buffer)); + client->pty_len = client->exit_status; + client->state = STATE_READY; + return; + } fd_set des_set; FD_ZERO (&des_set); @@ -372,9 +379,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, return -1; } } - if (spawn_process(client) != 0) { - return 1; - } + if (spawn_process(client) != 0) return 1; break; default: lwsl_warn("ignored unknown message type: %c\n", command); diff --git a/src/server.c b/src/server.c index 4e90374..9757736 100644 --- a/src/server.c +++ b/src/server.c @@ -186,6 +186,14 @@ sigchld_handler() { int status = wait_proc(-1, &pid); if (pid > 0) { lwsl_notice("process exited with code %d, pid: %d\n", status, pid); + struct tty_client *iterator; + LIST_FOREACH(iterator, &server->clients, list) { + if (iterator->pid == pid) { + iterator->running = false; + iterator->exit_status = status; + break; + } + } } } diff --git a/src/server.h b/src/server.h index 1539435..84ca065 100644 --- a/src/server.h +++ b/src/server.h @@ -44,6 +44,7 @@ struct tty_client { int pid; int pty; + int exit_status; enum pty_state state; char pty_buffer[LWS_PRE + 1 + BUF_SIZE]; ssize_t pty_len; -- 2.43.4