summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:44:42 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:44:42 -0600
commit270ac6676c05470223c5fc09fb3230b97acfbe2b (patch)
treefe3830363bc1eabf12f7d656ca62b33bc3d4a9d5
parentde0d26a1e6b2916996867c4c11f5146302720587 (diff)
downloadzash-270ac6676c05470223c5fc09fb3230b97acfbe2b.tar.gz
fix(signal): install SIG_DFL with sigaction, not signal()
Two places reset a signal to its default: main() for an inherited SIGCHLD (waitpid() fails with ECHILD when a parent left it ignored) and the pipeline child that writes a long builtin output and must die on SIGPIPE. Both went through signal(), which differs from sigaction() in restart and mask behaviour; they now share trap.c's set_handler() through a small trap_sig_default(), the function trap_reset() already uses.
-rw-r--r--src/exec.c2
-rw-r--r--src/exec.h1
-rw-r--r--src/main.c2
-rw-r--r--src/trap.c11
4 files changed, 14 insertions, 2 deletions
diff --git a/src/exec.c b/src/exec.c
index 43765f6..e7365ba 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -1372,7 +1372,7 @@ static void run_deferred(stage *st, int nc, int i, bool stream) {
s->out = -1;
for (int j = 0; j < nc; j++)
close_stage(&st[j]); /* or we would never see EPIPE */
- signal(SIGPIPE, SIG_DFL);
+ trap_sig_default(SIGPIPE);
output direct = {o.buf, o.len, o.len, out, 0};
out_flush(&direct);
_exit(0);
diff --git a/src/exec.h b/src/exec.h
index 89df201..6bbe6f6 100644
--- a/src/exec.h
+++ b/src/exec.h
@@ -215,6 +215,7 @@ void trap_poll(void);
void trap_reset_child(void);
void trap_reset_exec(void);
bool trap_has_exit(void);
+void trap_sig_default(int sig);
bool trap_after_last(void);
void trap_run_exit(void);
const sigset_t *trap_spawn_defaults(void);
diff --git a/src/main.c b/src/main.c
index 518dc45..8b4b516 100644
--- a/src/main.c
+++ b/src/main.c
@@ -426,7 +426,7 @@ int main(int argc, char **argv) {
stack_init();
/* A parent may leave SIGCHLD ignored, and then waitpid fails with ECHILD
* for every child: reset it before anything is spawned. */
- signal(SIGCHLD, SIG_DFL);
+ trap_sig_default(SIGCHLD);
invoked_as = argv[0] ? argv[0] : "zash";
arg0 = invoked_as[0] == '-' ? invoked_as + 1 : invoked_as;
if (arg0[0] != '/' && strchr(arg0, '/')) {
diff --git a/src/trap.c b/src/trap.c
index 0e5a0a2..4e57d7c 100644
--- a/src/trap.c
+++ b/src/trap.c
@@ -132,6 +132,17 @@ static void set_handler(int sig, void (*handler)(int)) {
}
/**
+ * @brief Put a signal back to SIG_DFL.
+ *
+ * For the two places that must not inherit or keep a handler: a parent's
+ * ignored SIGCHLD (waitpid then fails with ECHILD for every child) and a
+ * pipeline child that must die on SIGPIPE.
+ *
+ * @param sig The signal.
+ */
+void trap_sig_default(int sig) { set_handler(sig, SIG_DFL); }
+
+/**
* @brief What a signal does in this shell when no trap is set for it.
*/
static void (*default_handler(int sig))(int) {