From b5bda9c367d78a28c6523071d6c8c8e7b6551f02 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 10 May 2026 21:42:34 -0400 Subject: Close all file descriptors in child process, except STDIN, STDOUT, STDERR. * Rename fd to redirect so I can remember why this is here --- src/lib/core/multiprocessing.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/core/multiprocessing.c b/src/lib/core/multiprocessing.c index f431dd3..2fe02bb 100644 --- a/src/lib/core/multiprocessing.c +++ b/src/lib/core/multiprocessing.c @@ -85,14 +85,22 @@ int child(struct MultiProcessingPool *pool, struct MultiProcessingTask *task) { return -1; } - int fd = -1; - if ((fd = dup2(STDOUT_FILENO, STDERR_FILENO)) < 0) { + const int redirect = dup2(STDOUT_FILENO, STDERR_FILENO); + if (redirect < 0) { SYSERROR("%s", "Unable to redirect stderr to stdout"); SYSERROR("Unable to redirect stderr to stdout"); fclose(fp_log); return -1; } + // Close child file descriptors + for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); fd++) { + if (fd == redirect) { + continue; + } + close(fd); + } + // Generate timestamp for log header const time_t t = time(NULL); char *timebuf = ctime(&t); @@ -114,7 +122,10 @@ int child(struct MultiProcessingPool *pool, struct MultiProcessingTask *task) { fflush(stdout); fflush(stderr); char *args[] = {"bash", "--norc", task->parent_script, (char *) NULL}; - return execvp("/bin/bash", args); + semaphore_post(&pool->semaphore); + execvp("bash", args); + SYSERROR("execvp failed (%s)", strerror(errno)); + _exit(127); } int parent(struct MultiProcessingPool *pool, struct MultiProcessingTask *task, pid_t pid, int *child_status) { -- cgit