aboutsummaryrefslogtreecommitdiff
path: root/terminal.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-02-11 17:56:45 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-02-11 17:56:45 +0000
commite5edf7a52b93f2124eb1a44b56089f7d27826b53 (patch)
tree9920d99ef172122314d80b60b76a8e5173cf4f09 /terminal.c
parentMerge branch 'xassert-false' (diff)
parentreaper: monitor SIGCHLD using the FDM instead of via a signalfd (diff)
Merge branch 'fdm-signal-handling'
Diffstat (limited to 'terminal.c')
-rw-r--r--terminal.c110
1 files changed, 59 insertions, 51 deletions
diff --git a/terminal.c b/terminal.c
index c080c0c4..6c54543e 100644
--- a/terminal.c
+++ b/terminal.c
@@ -984,22 +984,25 @@ load_fonts_from_conf(struct terminal *term)
return reload_fonts(term);
}
-static bool
-slave_died(struct reaper *reaper, pid_t pid, void *data)
+static void
+slave_died(struct reaper *reaper, pid_t pid, int status, void *data)
{
struct terminal *term = data;
LOG_DBG("slave (PID=%u) died", pid);
+ term->slave_has_been_reaped = true;
+ term->exit_status = status;
+
if (term->conf->hold_at_exit) {
/* The PTMX FDM handler may already have closed our end */
if (term->ptmx >= 0) {
fdm_del(term->fdm, term->ptmx);
term->ptmx = -1;
}
- return true;
+ return;
}
- return term_shutdown(term);
+ term_shutdown(term);
}
struct terminal *
@@ -1075,7 +1078,6 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper,
.fdm = fdm,
.reaper = reaper,
.conf = conf,
- .quit = false,
.ptmx = ptmx,
.ptmx_buffers = tll_init(),
.ptmx_paste_buffers = tll_init(),
@@ -1503,70 +1505,76 @@ term_destroy(struct terminal *term)
int ret = EXIT_SUCCESS;
if (term->slave > 0) {
- LOG_DBG("waiting for slave (PID=%u) to die", term->slave);
+ int exit_status;
- /*
- * Note: we've closed ptmx, so the slave *should* exit...
- *
- * But, since it is possible to write clients that ignore
- * this, we need to handle it in *some* way.
- *
- * So, what we do is register a SIGALRM handler, and configure
- * a 2 second alarm. If the slave hasn't died after this time,
- * we send it a SIGTERM, then wait another 2 seconds (using
- * the same alarm mechanism). If it still hasn't died, we send
- * it a SIGKILL.
- *
- * Note that this solution is *not* asynchronous, and any
- * other events etc will be ignored during this time. This of
- * course only applies to a 'foot --server' instance, where
- * there might be other terminals running.
- */
- sigaction(SIGALRM, &(const struct sigaction){.sa_handler = &sig_alarm}, NULL);
- alarm(2);
+ if (term->slave_has_been_reaped)
+ exit_status = term->exit_status;
+ else {
+ LOG_DBG("waiting for slave (PID=%u) to die", term->slave);
+
+ /*
+ * Note: we've closed ptmx, so the slave *should* exit...
+ *
+ * But, since it is possible to write clients that ignore
+ * this, we need to handle it in *some* way.
+ *
+ * So, what we do is register a SIGALRM handler, and configure
+ * a 2 second alarm. If the slave hasn't died after this time,
+ * we send it a SIGTERM, then wait another 2 seconds (using
+ * the same alarm mechanism). If it still hasn't died, we send
+ * it a SIGKILL.
+ *
+ * Note that this solution is *not* asynchronous, and any
+ * other events etc will be ignored during this time. This of
+ * course only applies to a 'foot --server' instance, where
+ * there might be other terminals running.
+ */
+ sigaction(SIGALRM, &(const struct sigaction){.sa_handler = &sig_alarm}, NULL);
+ alarm(2);
- int status;
- int kill_signal = SIGTERM;
+ int kill_signal = SIGTERM;
- while (true) {
- int r = waitpid(term->slave, &status, 0);
+ while (true) {
+ int r = waitpid(term->slave, &exit_status, 0);
- if (r == term->slave)
- break;
+ if (r == term->slave)
+ break;
- if (r == -1) {
- xassert(errno == EINTR);
+ if (r == -1) {
+ xassert(errno == EINTR);
- if (alarm_raised) {
- LOG_DBG("slave hasn't died yet, sending: %s (%d)",
- kill_signal == SIGTERM ? "SIGTERM" : "SIGKILL",
- kill_signal);
+ if (alarm_raised) {
+ LOG_DBG("slave hasn't died yet, sending: %s (%d)",
+ kill_signal == SIGTERM ? "SIGTERM" : "SIGKILL",
+ kill_signal);
- kill(term->slave, kill_signal);
+ kill(term->slave, kill_signal);
- alarm_raised = 0;
- if (kill_signal != SIGKILL)
- alarm(2);
+ alarm_raised = 0;
+ if (kill_signal != SIGKILL)
+ alarm(2);
- kill_signal = SIGKILL;
+ kill_signal = SIGKILL;
+ }
}
}
- }
- /* Cancel alarm */
- alarm(0);
- sigaction(SIGALRM, &(const struct sigaction){.sa_handler = SIG_DFL}, NULL);
+ /* Cancel alarm */
+ alarm(0);
+ sigaction(SIGALRM, &(const struct sigaction){.sa_handler = SIG_DFL}, NULL);
+ }
ret = EXIT_FAILURE;
- if (WIFEXITED(status)) {
- ret = WEXITSTATUS(status);
+ if (WIFEXITED(exit_status)) {
+ ret = WEXITSTATUS(exit_status);
LOG_DBG("slave exited with code %d", ret);
- } else if (WIFSIGNALED(status)) {
- ret = WTERMSIG(status);
+ } else if (WIFSIGNALED(exit_status)) {
+ ret = WTERMSIG(exit_status);
LOG_WARN("slave exited with signal %d (%s)", ret, strsignal(ret));
} else {
- LOG_WARN("slave exited for unknown reason (status = 0x%08x)", status);
+ LOG_WARN("slave exited for unknown reason (status = 0x%08x)",
+ exit_status);
}
}