Permalink
Browse files

Merge pull request #432 from pandax381/fix-process-increase

Fix process increase

I will produce a patch to resolve the one outstanding issue of terminating child processes of any scripts of the script has been sent SIGTERM after timing out.
  • Loading branch information...
2 parents 66c870a + cebfbf5 commit dc85fdfc739b605f2bd47e1eb3b6b59e94892351 @pqarmitage pqarmitage committed on GitHub Nov 3, 2016
View
@@ -57,7 +57,7 @@ sigend(__attribute__((unused)) void *v, __attribute__((unused)) int sig)
static void
signal_init(void)
{
- signal_handler_init();
+ signal_handler_init(1);
signal_set(SIGHUP, sigend, NULL);
signal_set(SIGINT, sigend, NULL);
signal_set(SIGTERM, sigend, NULL);
@@ -35,6 +35,7 @@
#include "pidfile.h"
#include "daemon.h"
#include "signals.h"
+#include "notify.h"
#include "process.h"
#include "logger.h"
#include "list.h"
@@ -54,6 +55,9 @@ static char *check_syslog_ident;
static void
stop_check(int status)
{
+ /* Terminate all script process */
+ script_killall(master, SIGTERM);
+
/* Destroy master thread */
signal_handler_destroy();
thread_destroy_master(master);
@@ -202,7 +206,7 @@ sigend_check(__attribute__((unused)) void *v, __attribute__((unused)) int sig)
static void
check_signal_init(void)
{
- signal_handler_init();
+ signal_handler_init(0);
signal_set(SIGHUP, sighup_check, NULL);
signal_set(SIGINT, sigend_check, NULL);
signal_set(SIGTERM, sigend_check, NULL);
@@ -218,6 +222,9 @@ reload_check_thread(__attribute__((unused)) thread_t * thread)
log_message(LOG_INFO, "Got SIGHUP, reloading checker configuration");
+ /* Terminate all script process */
+ script_killall(master, SIGTERM);
+
/* Destroy master thread */
#ifdef _WITH_VRRP_
kernel_netlink_close();
@@ -163,7 +163,7 @@ misc_check_child_thread(thread_t * thread)
, checker->rs);
}
- kill(pid, SIGTERM);
+ kill(-pid, SIGTERM);
thread_add_child(thread->master, misc_check_child_timeout_thread,
checker, pid, 2);
return 0;
@@ -225,7 +225,7 @@ misc_check_child_timeout_thread(thread_t * thread)
/* OK, it still hasn't exited. Now really kill it off. */
pid = THREAD_CHILD_PID(thread);
- if (kill(pid, SIGKILL) < 0) {
+ if (kill(-pid, SIGKILL) < 0) {
/* Its possible it finished while we're handing this */
if (errno != ESRCH) {
DBG("kill error: %s", strerror(errno));
@@ -430,7 +430,7 @@ sigend(__attribute__((unused)) void *v, __attribute__((unused)) int sig)
static void
signal_init(void)
{
- signal_handler_init();
+ signal_handler_init(1);
signal_set(SIGHUP, propogate_signal, NULL);
signal_set(SIGUSR1, propogate_signal, NULL);
signal_set(SIGUSR2, propogate_signal, NULL);
@@ -45,6 +45,7 @@
#include "daemon.h"
#include "logger.h"
#include "signals.h"
+#include "notify.h"
#include "process.h"
#include "bitops.h"
#include "rttables.h"
@@ -119,6 +120,9 @@ stop_vrrp(int status)
}
#endif
+ /* Terminate all script process */
+ script_killall(master, SIGTERM);
+
/* We mustn't receive a SIGCHLD after master is destroyed */
signal_handler_destroy();
@@ -328,7 +332,7 @@ sigend_vrrp(__attribute__((unused)) void *v, __attribute__((unused)) int sig)
static void
vrrp_signal_init(void)
{
- signal_handler_init();
+ signal_handler_init(0);
signal_set(SIGHUP, sighup_vrrp, NULL);
signal_set(SIGINT, sigend_vrrp, NULL);
signal_set(SIGTERM, sigend_vrrp, NULL);
@@ -344,6 +348,9 @@ reload_vrrp_thread(__attribute__((unused)) thread_t * thread)
/* set the reloading flag */
SET_RELOAD;
+ /* Terminate all script process */
+ script_killall(master, SIGTERM);
+
/* Destroy master thread */
vrrp_dispatcher_release(vrrp_data);
kernel_netlink_close();
@@ -1098,7 +1098,7 @@ vrrp_script_child_thread(thread_t * thread)
log_message(LOG_INFO, "VRRP_Script(%s) timed out", vscript->sname);
vscript->result = 0;
}
- kill(pid, SIGTERM);
+ kill(-pid, SIGTERM);
thread_add_child(thread->master, vrrp_script_child_timeout_thread,
vscript, pid, 2);
return 0;
@@ -1143,7 +1143,7 @@ vrrp_script_child_timeout_thread(thread_t * thread)
/* OK, it still hasn't exited. Now really kill it off. */
pid = THREAD_CHILD_PID(thread);
- if (kill(pid, SIGKILL) < 0) {
+ if (kill(-pid, SIGKILL) < 0) {
/* Its possible it finished while we're handing this */
if (errno != ESRCH) {
DBG("kill error: %s", strerror(errno));
View
@@ -110,6 +110,8 @@ system_call_script(thread_master_t *m, int (*func) (thread_t *), void * arg, uns
}
/* Child part */
+ setpgid(0, 0);
+
script_setup();
status = system_call(script);
@@ -119,3 +121,33 @@ system_call_script(thread_master_t *m, int (*func) (thread_t *), void * arg, uns
exit(WEXITSTATUS(status));
}
+
+void
+script_killall(thread_master_t *m, int signo)
+{
+ sigset_t old_set, child_wait;
+ thread_t *thread;
+ pid_t p_pgid, c_pgid;
+
+ sigprocmask(0, NULL, &old_set);
+ if (!sigismember(&old_set, SIGCHLD)) {
+ sigemptyset(&child_wait);
+ sigaddset(&child_wait, SIGCHLD);
+ sigprocmask(SIG_BLOCK, &child_wait, NULL);
+ }
+
+ thread = m->child.head;
+
+ p_pgid = getpgid(0);
+
+ while (thread) {
+ c_pgid = getpgid(thread->u.c.pid);
+ if (c_pgid != p_pgid) {
+ kill(-c_pgid, signo);
+ }
+ thread = thread->next;
+ }
+
+ if (!sigismember(&old_set, SIGCHLD))
+ sigprocmask(SIG_UNBLOCK, &child_wait, NULL);
+}
View
@@ -28,5 +28,6 @@
/* system includes */
extern int system_call_script(thread_master_t *m, int (*func) (thread_t *), void * arg, unsigned long timer, const char* script);
extern int notify_exec(char *cmd);
+extern void script_killall(thread_master_t *m, int signo);
#endif
View
@@ -179,7 +179,7 @@ signal_ignore(int signo)
/* Handlers intialization */
void
-signal_handler_init(void)
+signal_handler_init(int remember)
{
sigset_t sset;
int sig;
@@ -224,8 +224,10 @@ signal_handler_init(void)
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
- sigemptyset(&ign_sig);
- sigemptyset(&dfl_sig);
+ if (remember) {
+ sigemptyset(&ign_sig);
+ sigemptyset(&dfl_sig);
+ }
for (sig = 1; sig <= SIGRTMAX; sig++) {
if (sigismember(&sset, sig)){
@@ -234,11 +236,14 @@ signal_handler_init(void)
/* Remember the original disposition, and ignore
* any default action signals
*/
- if (oact.sa_handler == SIG_IGN)
- sigaddset(&ign_sig, sig);
+ if (oact.sa_handler == SIG_IGN) {
+ if (remember)
+ sigaddset(&ign_sig, sig);
+ }
else {
sigaction(sig, &act, NULL);
- sigaddset(&dfl_sig, sig);
+ if (remember)
+ sigaddset(&dfl_sig, sig);
}
}
}
View
@@ -27,7 +27,7 @@
/* Prototypes */
extern void *signal_set(int signo, void (*func) (void *, int), void *);
extern void *signal_ignore(int signo);
-extern void signal_handler_init(void);
+extern void signal_handler_init(int);
extern void signal_handler_destroy(void);
extern void signal_handler_script(void);
extern void signal_run_callback(void);

0 comments on commit dc85fdf

Please sign in to comment.