Skip to content

Commit af207e7

Browse files
committed
xspawn: Unblock signals between fork() and exec()
Otherwise the child process runs with all signals blocked. Fixes: 423cfa4 ("xspawn: Mask signals before fork()")
1 parent 62928dd commit af207e7

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/xspawn.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "bfstd.h"
99
#include "diag.h"
1010
#include "list.h"
11+
#include "sighook.h"
1112

1213
#include <errno.h>
1314
#include <fcntl.h>
@@ -535,7 +536,7 @@ static bool bfs_use_posix_spawn(const struct bfs_resolver *res, const struct bfs
535536

536537
/** Actually exec() the new process. */
537538
_noreturn
538-
static void bfs_spawn_exec(struct bfs_resolver *res, const struct bfs_spawn *ctx, char **argv, char **envp, int pipefd[2]) {
539+
static void bfs_spawn_exec(struct bfs_resolver *res, const struct bfs_spawn *ctx, char **argv, char **envp, const sigset_t *mask, int pipefd[2]) {
539540
xclose(pipefd[0]);
540541

541542
for_slist (const struct bfs_spawn_action, action, ctx) {
@@ -596,6 +597,18 @@ static void bfs_spawn_exec(struct bfs_resolver *res, const struct bfs_spawn *ctx
596597
goto fail;
597598
}
598599

600+
// Reset signal handlers to their original values before we unblock
601+
// signals, so that handlers don't run in both the parent and the child
602+
if (sigreset() != 0) {
603+
goto fail;
604+
}
605+
606+
// Restore the original signal mask for the child process
607+
errno = pthread_sigmask(SIG_SETMASK, mask, NULL);
608+
if (errno != 0) {
609+
goto fail;
610+
}
611+
599612
execve(res->exe, argv, envp);
600613

601614
fail:;
@@ -635,7 +648,7 @@ static pid_t bfs_fork_spawn(struct bfs_resolver *res, const struct bfs_spawn *ct
635648
#endif
636649
if (pid == 0) {
637650
// Child
638-
bfs_spawn_exec(res, ctx, argv, envp, pipefd);
651+
bfs_spawn_exec(res, ctx, argv, envp, &old_mask, pipefd);
639652
}
640653

641654
// Restore the original signal mask

0 commit comments

Comments
 (0)