aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2026-05-10 21:42:34 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2026-05-11 15:56:09 -0400
commitb5bda9c367d78a28c6523071d6c8c8e7b6551f02 (patch)
treeb74577dcb27c9203c61d3ed8479c6696a368075c
parent98d50a05c62b772a6c391d05f175f44d8764c8a5 (diff)
downloadstasis-b5bda9c367d78a28c6523071d6c8c8e7b6551f02.tar.gz
Close all file descriptors in child process, except STDIN, STDOUT, STDERR.
* Rename fd to redirect so I can remember why this is here
-rw-r--r--src/lib/core/multiprocessing.c17
1 files 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) {