From 45298931af5acc01b55a3fa7464638a8c85e8f4a Mon Sep 17 00:00:00 2001 From: Matthew Mondor Date: Wed, 14 Jun 2023 14:27:17 +0000 Subject: [PATCH] AnalogTerm2: Rework child process management. - SIGKILL was sent to the child process if it wasn't detected to have exited, now SIGHUP is sent to its process group instead. - The signal handler in tty.c could perform operations that were unsafe for a handler, these now only set flags that are processed outside of the handler. --- mmsoftware/analogterm2/TODO.txt | 2 +- mmsoftware/analogterm2/src/font.c | 4 ++-- mmsoftware/analogterm2/src/tty.c | 29 +++++++++++++++-------------- mmsoftware/analogterm2/src/tty.h | 1 + 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/mmsoftware/analogterm2/TODO.txt b/mmsoftware/analogterm2/TODO.txt index 344665f..f7e801c 100644 --- a/mmsoftware/analogterm2/TODO.txt +++ b/mmsoftware/analogterm2/TODO.txt @@ -40,7 +40,7 @@ https://en.wikipedia.org/wiki/Box-drawing_character - Overwrite/clear selections before freeing them - ≣ † ☆ ツ ⌘›🍺∴ ( ͡° ͜ʖ ͡°) ƒ ︵ ₂ 😈 θ ƒ › ʼ ƒ ∂ ʻ μ › ∫ ◇ ♪ - ► ə β ə ſ ρ ə ∴ ♪ 😱 † 😳 › ▛ ᵗ * ‽ ℣ Ω ⌘   + ► ə β ə ſ ρ ə ∴ ♪ 😱 † 😳 › ▛ ᵗ * ‽ ℣ Ω ⌘   ❇ - Verify if dead key support is incomplete for ISO-8859-4 and ISO-8859-10. There were special characters that were unicode since the start but also could have punctuation. And others that used two at a time... It might diff --git a/mmsoftware/analogterm2/src/font.c b/mmsoftware/analogterm2/src/font.c index 7b3f3fd..97e52a6 100644 --- a/mmsoftware/analogterm2/src/font.c +++ b/mmsoftware/analogterm2/src/font.c @@ -588,7 +588,7 @@ font_init(state_t *state, screen_t *screen) map_reset(); } -#define CLEANUP_NSIZE 64 +#define CLEANUP_NSIZE 16 void font_cleanup(state_t *state) @@ -711,7 +711,7 @@ font_glyph(state_t *state, uint32_t c, bool decgfx, bool raw) raw: - /* Contiguous character blocks */ + /* Contiguous character blocks */ for (font = state->font; font != NULL; font = font->next) { if (c < font->end && c >= font->start) { glyph = font->glyphs[c - font->start]; diff --git a/mmsoftware/analogterm2/src/tty.c b/mmsoftware/analogterm2/src/tty.c index 86f452b..184fe3b 100644 --- a/mmsoftware/analogterm2/src/tty.c +++ b/mmsoftware/analogterm2/src/tty.c @@ -116,8 +116,7 @@ sighandler(int sig) switch (sig) { case SIGTERM: /* FALLTHROUGH */ case SIGINT: - exit(EXIT_SUCCESS); - /* NOTREACHED */ + shell_done = true; break; case SIGCHLD: /* Our shell subprocess died */ @@ -131,20 +130,16 @@ sighandler(int sig) break; if (!WIFEXITED(status)) continue; - if (WEXITSTATUS(status) != 0) - warn("Shell subprocess %d exit status: %d %s", - (int)pid, WEXITSTATUS(status), - strerror(WEXITSTATUS(status))); - if (ttys != NULL && pid == ttys->shell_pid) + if (ttys != NULL && pid == ttys->shell_pid) { + ttys->exitpid = pid; ttys->shell_pid = -1; + if (WEXITSTATUS(status) != 0) + ttys->exitstatus = WEXITSTATUS(status); + } } - if (ttys != NULL && ttys->shell_pid == -1) { - warn("Shell subprocess gone."); + if (ttys != NULL && ttys->shell_pid == -1) shell_done = true; - } break; - default: - warn("sighandler() - Unexpected signal %d\n", sig); } } @@ -400,6 +395,8 @@ tty_shell(char **envp, struct winsize *winsize, const char *command) s->ttyname = ptsname(s->ptyfd); s->winsize = *winsize; s->command = command; + s->exitpid = -1; + s->exitstatus = 0; sighandler_setup(); @@ -413,9 +410,13 @@ void tty_cleanup(tty_t *s) { + if (s->shell_pid == -1 && s->exitpid != -1) + warn("Shell subprocess %d gone, exit status: %d %s", + s->exitpid, s->exitstatus, strerror(s->exitstatus)); + /* Kill child process if still exists */ - if (s->shell_pid != -1) - (void)kill(s->shell_pid, SIGKILL); + if (s->shell_pid > 0) + (void)kill(-(s->shell_pid), SIGHUP); (void)close(s->ptyfd); diff --git a/mmsoftware/analogterm2/src/tty.h b/mmsoftware/analogterm2/src/tty.h index dccb70c..172bed5 100644 --- a/mmsoftware/analogterm2/src/tty.h +++ b/mmsoftware/analogterm2/src/tty.h @@ -39,6 +39,7 @@ typedef struct tty_s { const char *ttyname; int ptyfd; pid_t shell_pid; + int exitpid, exitstatus; uint8_t rbuf[RBUF_SIZE]; struct winsize winsize; } tty_t; -- 2.9.0