From fd20e846ba1b4850badd2809b4ca9f75a6fceb23 Mon Sep 17 00:00:00 2001 From: Quentin Armitage Date: Sun, 29 Nov 2015 14:33:26 +0000 Subject: Remove some code duplication re running scripts misc_check_thread and vrrp_script_thread were virtually identical so move duplicate code into new function system_call_script in notify.c. Signed-off-by: Quentin Armitage (cherry picked from commit 4a725b3a9b7780af0190e4f23aca9321336c35fd) --- keepalived/check/check_misc.c | 48 ++++----------------------------------- keepalived/vrrp/vrrp_scheduler.c | 45 ++++-------------------------------- lib/notify.c | 49 ++++++++++++++++++++++++++++++++++++++-- lib/notify.h | 4 +++- 4 files changed, 58 insertions(+), 88 deletions(-) diff --git a/keepalived/check/check_misc.c b/keepalived/check/check_misc.c index 4e59b27..acc566d 100644 --- a/keepalived/check/check_misc.c +++ b/keepalived/check/check_misc.c @@ -108,8 +108,6 @@ misc_check_thread(thread_t * thread) { checker_t *checker; misc_checker_t *misck_checker; - int status, ret; - pid_t pid; checker = THREAD_ARG(thread); misck_checker = CHECKER_ARG(checker); @@ -129,48 +127,10 @@ misc_check_thread(thread_t * thread) thread_add_timer(thread->master, misc_check_thread, checker, checker->vs->delay_loop); - /* Daemonization to not degrade our scheduling timer */ - pid = fork(); - - /* In case of fork is error. */ - if (pid < 0) { - log_message(LOG_INFO, "Failed fork process"); - return -1; - } - - /* In case of this is parent process */ - if (pid) { - long timeout; - timeout = (misck_checker->timeout) ? misck_checker->timeout : checker->vs->delay_loop; - - thread_add_child(thread->master, misc_check_child_thread, - checker, pid, timeout); - return 0; - } - - /* Child part */ - signal_handler_notify(); - 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"); - } - - status = system_call(misck_checker->path); - - if (status < 0 || !WIFEXITED(status)) - status = 0; /* Script errors aren't server errors */ - else - status = WEXITSTATUS(status); - - exit(status); + /* Execute the script in a child process. Parent returns, child doesn't */ + return system_call_script(thread->master, misc_check_child_thread, + checker, (misck_checker->timeout) ? misck_checker->timeout : checker->vs->delay_loop, + misck_checker->path); } int diff --git a/keepalived/vrrp/vrrp_scheduler.c b/keepalived/vrrp/vrrp_scheduler.c index 73c5dfe..aace9e3 100644 --- a/keepalived/vrrp/vrrp_scheduler.c +++ b/keepalived/vrrp/vrrp_scheduler.c @@ -979,52 +979,15 @@ static int vrrp_script_thread(thread_t * thread) { vrrp_script_t *vscript = THREAD_ARG(thread); - int status, ret; - pid_t pid; /* Register next timer tracker */ thread_add_timer(thread->master, vrrp_script_thread, vscript, vscript->interval); - /* Daemonization to not degrade our scheduling timer */ - pid = fork(); - - /* In case of fork is error. */ - if (pid < 0) { - log_message(LOG_INFO, "Failed fork process"); - return -1; - } - - /* In case of this is parent process */ - if (pid) { - thread_add_child(thread->master, vrrp_script_child_thread, - vscript, pid, - (vscript->timeout) ? vscript->timeout : vscript->interval); - return 0; - } - - /* Child part */ - signal_handler_notify(); - 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"); - } - - status = system_call(vscript->script); - - if (status < 0 || !WIFEXITED(status)) - status = 0; /* Script errors aren't server errors */ - else - status = WEXITSTATUS(status); - - exit(status); + /* Execute the script in a child process. Parent returns, child doesn't */ + return system_call_script(thread->master, vrrp_script_child_thread, + vscript, (vscript->timeout) ? vscript->timeout : vscript->interval, + vscript->script); } static int diff --git a/lib/notify.c b/lib/notify.c index f71efa9..0908f89 100644 --- a/lib/notify.c +++ b/lib/notify.c @@ -30,8 +30,8 @@ #include "logger.h" /* perform a system call */ -int -system_call(char *cmdline) +static int +system_call(const char *cmdline) { int retval; @@ -95,3 +95,48 @@ notify_exec(char *cmd) exit(0); } + +int +system_call_script(thread_master_t *m, int (*func) (thread_t *), void * arg, long timer, const char* script) +{ + int status, ret; + pid_t pid; + + /* Daemonization to not degrade our scheduling timer */ + pid = fork(); + + /* In case of fork is error. */ + if (pid < 0) { + log_message(LOG_INFO, "Failed fork process"); + return -1; + } + + /* In case of this is parent process */ + if (pid) { + thread_add_child(m, func, arg, pid, timer); + return 0; + } + + /* Child part */ + signal_handler_notify(); + 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"); + } + + status = system_call(script); + + if (status < 0 || !WIFEXITED(status)) + status = 0; /* Script errors aren't server errors */ + else + status = WEXITSTATUS(status); + + exit(status); +} diff --git a/lib/notify.h b/lib/notify.h index a17cb75..6dbc561 100644 --- a/lib/notify.h +++ b/lib/notify.h @@ -23,8 +23,10 @@ #ifndef _NOTIFY_H #define _NOTIFY_H +#include "scheduler.h" + /* system includes */ -extern int system_call(char *cmdline); +extern int system_call_script(thread_master_t *m, int (*func) (thread_t *), void * arg, long timer, const char* script); extern void closeall(int fd); extern int notify_exec(char *cmd); -- 1.7.12.1