manual check if the parent process is alive after prctl()

This commit is contained in:
Devaev Maxim
2019-10-10 12:00:42 +03:00
parent 6175e6974c
commit be2de06b09
2 changed files with 18 additions and 4 deletions

View File

@@ -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);

View File

@@ -33,6 +33,7 @@
#ifdef HAS_PDEATHSIG
# include <signal.h>
# include <unistd.h>
# if defined(__linux__)
# include <sys/prctl.h>
@@ -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