From 3ba9640fb0c8b56e44e1d7d5b021f440ca1b2a51 Mon Sep 17 00:00:00 2001 From: Klaus Wenninger Date: Thu, 23 Sep 2021 10:22:37 +0200 Subject: [PATCH] Feature: sbd-inquisitor: don't fork if no pid-file given --- src/sbd-common.c | 32 ++++++++++++++++++++------------ src/sbd-inquisitor.c | 9 ++++++++- src/sbd.h | 1 + 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/sbd-common.c b/src/sbd-common.c index 3abf75f..66489d1 100644 --- a/src/sbd-common.c +++ b/src/sbd-common.c @@ -1160,11 +1160,28 @@ sbd_cdtocoredir(void) return rc; } +void +sbd_detach(void) +{ + const char *devnull = "/dev/null"; + + maximize_priority(); + sysrq_init(); + + umask(022); + close(0); + (void)open(devnull, O_RDONLY); + close(1); + (void)open(devnull, O_WRONLY); + close(2); + (void)open(devnull, O_WRONLY); + sbd_cdtocoredir(); +} + pid_t make_daemon(void) { pid_t pid; - const char * devnull = "/dev/null"; pid = fork(); if (pid < 0) { @@ -1179,17 +1196,8 @@ make_daemon(void) qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE); /* This is the child; ensure privileges have not been lost. */ - maximize_priority(); - sysrq_init(); - - umask(022); - close(0); - (void)open(devnull, O_RDONLY); - close(1); - (void)open(devnull, O_WRONLY); - close(2); - (void)open(devnull, O_WRONLY); - sbd_cdtocoredir(); + sbd_detach(); + return 0; } diff --git a/src/sbd-inquisitor.c b/src/sbd-inquisitor.c index d917cd1..3266cfe 100644 --- a/src/sbd-inquisitor.c +++ b/src/sbd-inquisitor.c @@ -1387,7 +1387,14 @@ int main(int argc, char **argv, char **envp) cl_log(LOG_NOTICE, "%s flush + write \'%c\' to sysrq in case of timeout", do_flush?"Do":"Skip", timeout_sysrq_char); - exit_status = inquisitor(); + if (pidfile) { + exit_status = inquisitor(); + } else { + sbd_detach(); + inquisitor_child(); + /* not reached */ + exit(0); + } } else { exit_status = -2; } diff --git a/src/sbd.h b/src/sbd.h index bbdc6f1..b65fea2 100644 --- a/src/sbd.h +++ b/src/sbd.h @@ -136,6 +136,7 @@ void do_reset(void); void do_off(void); void do_timeout_action(void); pid_t make_daemon(void); +void sbd_detach(void); void maximize_priority(void); void sbd_get_uname(void); void sbd_set_format_string(int method, const char *daemon);