aboutsummaryrefslogtreecommitdiff
path: root/src/Program.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Program.c')
-rw-r--r--src/Program.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/Program.c b/src/Program.c
index 3246d54..2509f6f 100644
--- a/src/Program.c
+++ b/src/Program.c
@@ -1,9 +1,11 @@
#include "../include/Program.h"
#include <unistd.h>
#include <sys/wait.h>
+#include <sys/prctl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
+#include <stdlib.h>
#include <assert.h>
#define READ_END 0
@@ -20,11 +22,22 @@ int exec_program(const char **args, ProgramOutputCallback output_callback, void
return -2;
}
+ pid_t parent_pid = getpid();
+
pid_t pid = fork();
if(pid == -1) {
perror("Failed to fork");
return -3;
} else if(pid == 0) { /* child */
+ if(prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
+ perror("prctl(PR_SET_PDEATHSIG, SIGTERM) failed");
+ exit(127);
+ }
+
+ /* Test if the parent died before the above call to prctl */
+ if(getppid() != parent_pid)
+ exit(127);
+
dup2(fd[WRITE_END], STDOUT_FILENO);
close(fd[READ_END]);
close(fd[WRITE_END]);
@@ -105,12 +118,23 @@ int exec_program_async(const char **args, pid_t *result_process_id) {
if(args[0] == NULL)
return -1;
+ pid_t parent_pid = getpid();
+
pid_t pid = fork();
if(pid == -1) {
int err = errno;
perror("Failed to fork");
return -err;
} else if(pid == 0) { /* child */
+ if(prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
+ perror("prctl(PR_SET_PDEATHSIG, SIGTERM) failed");
+ exit(127);
+ }
+
+ /* Test if the parent died before the above call to prctl */
+ if(getppid() != parent_pid)
+ exit(127);
+
execvp(args[0], args);
} else { /* parent */
if(result_process_id)