From be2de06b09ae5523fb64ca5a913242cda9572679 Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Thu, 10 Oct 2019 12:00:42 +0300 Subject: [PATCH] manual check if the parent process is alive after prctl() --- src/options.c | 6 +++++- src/process.h | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) 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