AnalogTerm2: Rework child process management.
authorMatthew Mondor <mmondor@pulsar-zone.net>
Wed, 14 Jun 2023 14:27:17 +0000 (14:27 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Wed, 14 Jun 2023 14:27:17 +0000 (14:27 +0000)
- 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
mmsoftware/analogterm2/src/font.c
mmsoftware/analogterm2/src/tty.c
mmsoftware/analogterm2/src/tty.h

index 344665f..f7e801c 100644 (file)
@@ -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
index 7b3f3fd..97e52a6 100644 (file)
@@ -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];
index 86f452b..184fe3b 100644 (file)
@@ -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);
 
index dccb70c..172bed5 100644 (file)
@@ -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;