From a1c83fc7655180f6e779096bc42a80dc6eed05aa Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Tue, 8 Oct 2019 23:32:23 +0300 Subject: [PATCH] --exit-on-parent-death --- src/options.c | 18 ++++++++++++++++ src/process.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/process.h diff --git a/src/options.c b/src/options.c index 1edeae4..192a776 100644 --- a/src/options.c +++ b/src/options.c @@ -36,6 +36,7 @@ #include "config.h" #include "logging.h" +#include "process.h" #include "device.h" #include "encoder.h" #include "http/server.h" @@ -107,6 +108,10 @@ enum _OPT_VALUES { _O_GPIO_WORKERS_BUSY_AT, #endif +#ifdef HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH + _O_EXIT_ON_PARENT_DEATH, +#endif + _O_LOG_LEVEL, _O_PERF, _O_VERBOSE, @@ -171,6 +176,10 @@ static const struct option _LONG_OPTS[] = { {"gpio-workers-busy-at", required_argument, NULL, _O_GPIO_WORKERS_BUSY_AT}, #endif +#ifdef HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH + {"exit-on-parent-death", no_argument, NULL, _O_EXIT_ON_PARENT_DEATH}, +#endif + {"log-level", required_argument, NULL, _O_LOG_LEVEL}, {"perf", no_argument, NULL, _O_PERF}, {"verbose", no_argument, NULL, _O_VERBOSE}, @@ -333,6 +342,10 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t case _O_GPIO_WORKERS_BUSY_AT: OPT_NUMBER("--gpio-workers-busy-at", gpio_pin_workers_busy_at, 0, 256, 0); # endif +# ifdef HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH + case _O_EXIT_ON_PARENT_DEATH: process_set_sigterm_on_parent_death(); break; +# endif + case _O_LOG_LEVEL: OPT_NUMBER("--log-level", log_level, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, 0); case _O_PERF: OPT_SET(log_level, LOG_LEVEL_PERF); case _O_VERBOSE: OPT_SET(log_level, LOG_LEVEL_VERBOSE); @@ -538,6 +551,11 @@ static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_s printf(" --gpio-has-http-clients ─ Set 1 while stream has at least one client. Default: disabled.\n\n"); printf(" --gpio-workers-busy-at ── Set 1 on (pin + N) while worker with number N has a job.\n"); printf(" The worker's numbering starts from 0. Default: disabled\n\n"); +#endif +#ifdef HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH + printf("Process options:\n"); + printf("════════════════\n"); + printf(" --exit-on-parent-death ─ Exit the program if the parent process is dead. Default: disabled.\n\n"); #endif printf("Logging options:\n"); printf("════════════════\n"); diff --git a/src/process.h b/src/process.h new file mode 100644 index 0000000..f4550ea --- /dev/null +++ b/src/process.h @@ -0,0 +1,60 @@ +/***************************************************************************** +# # +# uStreamer - Lightweight and fast MJPG-HTTP streamer. # +# # +# Copyright (C) 2018 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +*****************************************************************************/ + + +#pragma once + +#if defined(__linux__) +# define HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH +#elif defined(__FreeBSD__) +# include +# if __FreeBSD_version >= 1102000 +# define HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH +# endif +#endif + +#ifdef HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH +# include + +# if defined(__linux__) +# include +# elif defined(__FreeBSD__) +# include +# endif + +# include "logging.h" + + +INLINE void process_set_sigterm_on_parent_death(void) { + int signum = SIGTERM; +# ifdef __linux__ + int retval = prctl(PR_SET_PDEATHSIG, signum); +# elif __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"); + } +} + +#endif