diff --git a/src/options.c b/src/options.c index 5e3045d..7430a9c 100644 --- a/src/options.c +++ b/src/options.c @@ -338,7 +338,11 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t # endif # ifdef HAS_PDEATHSIG - case _O_EXIT_ON_PARENT_DEATH: process_set_pdeathsig_sigterm(); break; + case _O_EXIT_ON_PARENT_DEATH: + if (process_track_parent_death() < 0) { + return -1; + }; + break; # endif case _O_LOG_LEVEL: OPT_NUMBER("--log-level", log_level, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, 0); diff --git a/src/process.h b/src/process.h index 965e68a..e8b89fe 100644 --- a/src/process.h +++ b/src/process.h @@ -33,6 +33,7 @@ #ifdef HAS_PDEATHSIG # include +# include # if defined(__linux__) # include @@ -42,18 +43,27 @@ # include "logging.h" -INLINE void process_set_pdeathsig_sigterm(void) { + +INLINE int process_track_parent_death(void) { int signum = SIGTERM; -# ifdef __linux__ +# if defined(__linux__) int retval = prctl(PR_SET_PDEATHSIG, signum); -# elif __FreeBSD__ +# elif defined(__FreeBSD__) int retval = procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum); # else # error WTF? # endif if (retval < 0) { LOG_PERROR("Can't set to receive SIGTERM on parent process death"); + return -1; } + + if (kill(getppid(), 0) < 0) { + LOG_PERROR("The parent process is already dead"); + return -1; + } + + return 0; } #endif // HAS_PDEATHSIG