--exit-on-parent-death

This commit is contained in:
Devaev Maxim
2019-10-08 23:32:23 +03:00
parent 57132c46ac
commit a1c83fc765
2 changed files with 78 additions and 0 deletions

View File

@@ -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 <pin> ─ Set 1 while stream has at least one client. Default: disabled.\n\n");
printf(" --gpio-workers-busy-at <pin> ── 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");

60
src/process.h Normal file
View File

@@ -0,0 +1,60 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# 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 <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#if defined(__linux__)
# define HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH
#elif defined(__FreeBSD__)
# include <sys/param.h>
# if __FreeBSD_version >= 1102000
# define HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH
# endif
#endif
#ifdef HAS_PROCESS_SET_SIGTERM_ON_PARENT_DEATH
# include <signal.h>
# if defined(__linux__)
# include <sys/prctl.h>
# elif defined(__FreeBSD__)
# include <sys/procctl.h>
# 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