diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-07-22 18:48:30 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-22 18:48:30 +0200 |
commit | e9e41c02d4309cd50b5584a8dfc0d7e01cb27e10 (patch) | |
tree | 365b00dba760153e8d88fa143fb7e15a2a1a6448 | |
parent | 93db3b8726f6d7574b71926285e86135e822f9d3 (diff) |
Kill child process when parent dies
-rw-r--r-- | src/command.c | 19 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/std/buffer.c | 8 | ||||
-rw-r--r-- | src/std_gc/list.c | 2 |
4 files changed, 21 insertions, 10 deletions
diff --git a/src/command.c b/src/command.c index c413502..d44c773 100644 --- a/src/command.c +++ b/src/command.c @@ -1,10 +1,13 @@ #include "../include/command.h" #include <unistd.h> #include <sys/wait.h> +#include <sys/prctl.h> +#include <signal.h> #include <errno.h> #include <string.h> #include <stdio.h> #include <assert.h> +#include <stdlib.h> #define READ_END 0 #define WRITE_END 1 @@ -12,6 +15,7 @@ int tsl_command_exec(char **args, ProgramOutputCallback output_callback, void *userdata) { int fd[2]; pid_t pid; + pid_t parent_pid = getpid(); /* 1 arguments */ if(args[0] == NULL) @@ -25,14 +29,26 @@ int tsl_command_exec(char **args, ProgramOutputCallback output_callback, void *u pid = fork(); if(pid == -1) { perror("Failed to fork"); + close(fd[READ_END]); + close(fd[WRITE_END]); 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]); execvp(args[0], args); - return 0; + perror("execvp"); + exit(127); } else { /* parent */ int result = 0; int status; @@ -71,7 +87,6 @@ int tsl_command_exec(char **args, ProgramOutputCallback output_callback, void *u if(exit_status != 0) { char **arg = args; fprintf(stderr, "Failed to execute program ("); - while(*arg) { if(arg != args) fputc(' ', stderr); @@ -1,5 +1,4 @@ #include "../include/parser.h" -#include <string.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> @@ -38,6 +37,7 @@ static char* file_get_content(const char *filepath, size_t *filesize) { result[*filesize] = '\0'; if((size_t)read(fd, result, *filesize) != *filesize) { free(result); + result = NULL; *filesize = 0; fprintf(stderr, "Error: Failed to read all data from file %s\n", filepath); goto cleanup; diff --git a/src/std/buffer.c b/src/std/buffer.c index b0099bb..0936573 100644 --- a/src/std/buffer.c +++ b/src/std/buffer.c @@ -12,6 +12,7 @@ void tsl_buffer_init(TslBuffer *self) { void tsl_buffer_deinit(TslBuffer *self) { free(self->data); + self->data = NULL; self->size = 0; self->capacity = 0; } @@ -26,7 +27,7 @@ static int tsl_buffer_ensure_capacity(TslBuffer *self, size_t new_size) { new_capacity = 8; while(new_capacity < new_size) { - new_capacity <<= 1; + new_capacity += (new_capacity >> 1); } new_ptr = realloc(self->data, new_capacity); @@ -54,11 +55,6 @@ void* tsl_buffer_pop(TslBuffer *self, size_t size) { return (char*)self->data + self->size; } -void* tsl_buffer_top(TslBuffer *self, size_t data_size) { - tsl_buffer_pop(self, data_size); - return (char*)self->data + self->size; -} - void* tsl_buffer_begin(TslBuffer *self) { return self->data; } diff --git a/src/std_gc/list.c b/src/std_gc/list.c index 7c56d64..e5272b8 100644 --- a/src/std_gc/list.c +++ b/src/std_gc/list.c @@ -24,7 +24,7 @@ static int tsl_list_ensure_capacity(TslList *self, size_t new_size) { new_capacity = 8; while(new_capacity < new_size) { - new_capacity <<= 1; + new_capacity += (new_capacity >> 1); } new_ptr = GC_REALLOC(self->data, new_capacity); |