f70c3a1aac147ae2938b7f3574743ae0daba356d — Simon Ser 12 days ago ef21854
shell/job: ignore signals before grabbing control

setpgid might send SIGTTOU to the process, e.g. under strace. We need
to ignore this signal before calling setpgid.
3 files changed, 7 insertions(+), 5 deletions(-)

M include/shell/trap.h
M shell/job.c
M shell/trap.c
M include/shell/trap.h => include/shell/trap.h +1 -1
@@ 17,7 17,7 @@ enum mrsh_trap_action {
 
 bool set_trap(struct mrsh_state *state, int sig, enum mrsh_trap_action action,
 	struct mrsh_program *program);
-bool set_job_control_traps(struct mrsh_state *state);
+bool set_job_control_traps(struct mrsh_state *state, bool enabled);
 bool reset_caught_traps(struct mrsh_state *state);
 bool run_pending_traps(struct mrsh_state *state);
 bool run_exit_trap(struct mrsh_state *state);

M shell/job.c => shell/job.c +4 -2
@@ 32,6 32,9 @@ bool mrsh_set_job_control(struct mrsh_state *state, bool enabled) {
 			kill(-pgid, SIGTTIN);
 		}
 
+		// Ignore interactive and job-control signals
+		set_job_control_traps(state, true);
+
 		// Put ourselves in our own process group, if we aren't the session
 		// leader
 		priv->pgid = getpid();


@@ 53,11 56,10 @@ bool mrsh_set_job_control(struct mrsh_state *state, bool enabled) {
 			return false;
 		}
 	} else {
-		// TODO
+		set_job_control_traps(state, false);
 	}
 
 	priv->job_control = enabled;
-	set_job_control_traps(state);
 	return true;
 }
 

M shell/trap.c => shell/trap.c +2 -2
@@ 82,7 82,7 @@ bool set_trap(struct mrsh_state *state, int sig, enum mrsh_trap_action action,
 	return true;
 }
 
-bool set_job_control_traps(struct mrsh_state *state) {
+bool set_job_control_traps(struct mrsh_state *state, bool enabled) {
 	struct mrsh_state_priv *priv = state_get_priv(state);
 
 	for (size_t i = 0; i < ignored_job_control_sigs_len; i++) {


@@ 90,7 90,7 @@ bool set_job_control_traps(struct mrsh_state *state) {
 		struct mrsh_trap *trap = &priv->traps[i];
 
 		struct sigaction sa = {0};
-		if (priv->job_control) {
+		if (enabled) {
 			sa.sa_handler = SIG_IGN;
 		} else {
 			sa.sa_handler = SIG_DFL;