From f4518b215893f380f929796c4a8116b8c006e063 Mon Sep 17 00:00:00 2001 From: Quentin Armitage Date: Wed, 9 Dec 2015 17:58:30 +0000 Subject: Move common code for opening fd 0/1/2 into a function The code for setting fd 0/1/2 to /dev/null before running a script was in several places. All the common code is moved into a function and the function called from the relevant places. It is only necessary to reopen fd 0/1/2 if keepalived is running with the --dont-fork option, since without that option the fds are already open on /dev/null. Signed-off-by: Quentin Armitage (cherry picked from commit ec009964f8bd2afe0366270eb7f1e79689db8ff1) --- keepalived/core/daemon.c | 14 ++------------ lib/notify.c | 48 ++++++++++++++++-------------------------------- lib/utils.c | 28 +++++++++++++++++++--------- lib/utils.h | 2 ++ 4 files changed, 39 insertions(+), 53 deletions(-) diff --git a/keepalived/core/daemon.c b/keepalived/core/daemon.c index 23692e3..3585331 100644 --- a/keepalived/core/daemon.c +++ b/keepalived/core/daemon.c @@ -63,18 +63,8 @@ xdaemon(int nochdir, int noclose, int exitflag) } /* File descriptor close. */ - if (!noclose) { - int fd; - - fd = open("/dev/null", O_RDWR, 0); - if (fd != -1) { - dup2(fd, STDIN_FILENO); - dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); - if (fd > 2) - close(fd); - } - } + if (!noclose) + set_std_fd(true); umask(0); return 0; diff --git a/lib/notify.c b/lib/notify.c index 5251085..5e375ab 100644 --- a/lib/notify.c +++ b/lib/notify.c @@ -29,6 +29,7 @@ #include "notify.h" #include "signals.h" #include "logger.h" +#include "utils.h" /* perform a system call */ static int @@ -61,12 +62,21 @@ closeall(int fd) close(fd++); } +static void +script_setup(void) +{ + closeall(0); + + signal_handler_script(); + + set_std_fd(false); +} + /* Execute external script/program */ int notify_exec(char *cmd) { pid_t pid; - int ret; pid = fork(); @@ -80,20 +90,7 @@ notify_exec(char *cmd) if (pid) return 0; - signal_handler_script(); - closeall(0); - - open("/dev/null", O_RDWR); - - ret = dup(0); - if (ret < 0) { - log_message(LOG_INFO, "dup(0) error"); - } - - ret = dup(0); - if (ret < 0) { - log_message(LOG_INFO, "dup(0) error"); - } + script_setup(); system_call(cmd); @@ -103,7 +100,7 @@ notify_exec(char *cmd) int system_call_script(thread_master_t *m, int (*func) (thread_t *), void * arg, long timer, const char* script) { - int status, ret; + int status; pid_t pid; /* Daemonization to not degrade our scheduling timer */ @@ -122,25 +119,12 @@ system_call_script(thread_master_t *m, int (*func) (thread_t *), void * arg, lon } /* Child part */ - signal_handler_script(); - closeall(0); - open("/dev/null", O_RDWR); - ret = dup(0); - if (ret < 0) { - log_message(LOG_INFO, "dup(0) error"); - } - - ret = dup(0); - if (ret < 0) { - log_message(LOG_INFO, "dup(0) error"); - } + script_setup(); status = system_call(script); if (status < 0 || !WIFEXITED(status)) - status = 0; /* Script errors aren't server errors */ - else - status = WEXITSTATUS(status); + exit(0); /* Script errors aren't server errors */ - exit(status); + exit(WEXITSTATUS(status)); } diff --git a/lib/utils.c b/lib/utils.c index 367bdfe..485cea7 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -26,6 +26,7 @@ #include #include "utils.h" #include "signals.h" +#include "bitops.h" /* global vars */ unsigned long debug = 0; @@ -490,11 +491,27 @@ string_equal(const char *str1, const char *str2) return (*str1 == 0 && *str2 == 0); } +void +set_std_fd(int force) +{ + int fd; + + if (force || __test_bit(DONT_FORK_BIT, &debug)) { + fd = open("/dev/null", O_RDWR); + if (fd != -1) { + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + if (fd > 2) + close(fd); + } + } +} + int fork_exec(char **argv) { pid_t pid; - int fd; int status; pid = fork(); @@ -503,14 +520,7 @@ fork_exec(char **argv) /* Child */ if (pid == 0) { - fd = open("/dev/null", O_RDWR, 0); - if (fd != -1) { - dup2(fd, STDIN_FILENO); - dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); - if (fd > 2) - close(fd); - } + set_std_fd(false); signal_handler_script(); diff --git a/lib/utils.h b/lib/utils.h index 0d6a95f..da36332 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -71,6 +72,7 @@ uint32_t inet_broadcast(uint32_t, uint32_t); uint32_t inet_cidrtomask(uint8_t); extern char *get_local_name(void); extern int string_equal(const char *, const char *); +extern void set_std_fd(int); extern int fork_exec(char **argv); #endif -- 1.7.12.1