9cfb40
From d3292ee776448de67241bf7efc8f9bb23f166752 Mon Sep 17 00:00:00 2001
9cfb40
From: Quentin Armitage <quentin@armitage.org.uk>
9cfb40
Date: Fri, 2 Feb 2018 10:12:06 +0000
9cfb40
Subject: [PATCH 1/8] Add child_wait_time to allow longer shutdown time of
9cfb40
 child processes
9cfb40
9cfb40
With very large configurations it can be necessary to allow longer than
9cfb40
the default 5 seconds for child processes to cleanup and terminate.
9cfb40
9cfb40
Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
9cfb40
---
9cfb40
 doc/keepalived.conf.SYNOPSIS    |  5 ++++-
9cfb40
 doc/man/man5/keepalived.conf.5  |  3 +++
9cfb40
 keepalived/core/global_parser.c | 19 +++++++++++++++++++
9cfb40
 keepalived/core/main.c          | 12 +++++++-----
9cfb40
 keepalived/include/main.h       |  3 +++
9cfb40
 lib/parser.c                    |  2 ++
9cfb40
 6 files changed, 38 insertions(+), 6 deletions(-)
9cfb40
9cfb40
diff --git a/doc/keepalived.conf.SYNOPSIS b/doc/keepalived.conf.SYNOPSIS
9cfb40
index 90eb83d4..3d042b1c 100644
9cfb40
--- a/doc/keepalived.conf.SYNOPSIS
9cfb40
+++ b/doc/keepalived.conf.SYNOPSIS
9cfb40
@@ -176,7 +176,10 @@ use_pid_dir				   # Create pid files in /var/run/keepalived
9cfb40
 
9cfb40
 linkbeat_use_polling			   # Use media link failure detection polling fashion
9cfb40
 
9cfb40
-	1.2. Static addresses
9cfb40
+child_wait_time SECS                          # Time for main process to allow for child processes to exit on termination
9cfb40
+                                              #   in seconds (default 5). This can be needed for very large configurations.
9cfb40
+
9cfb40
+    1.2. Static addresses
9cfb40
 
9cfb40
 	The configuration block looks like :
9cfb40
 
9cfb40
diff --git a/doc/man/man5/keepalived.conf.5 b/doc/man/man5/keepalived.conf.5
9cfb40
index 1dabdec7..d0d04727 100644
9cfb40
--- a/doc/man/man5/keepalived.conf.5
9cfb40
+++ b/doc/man/man5/keepalived.conf.5
9cfb40
@@ -219,6 +219,9 @@ and
9cfb40
 
9cfb40
  linkbeat_use_polling         # Poll to detect media link failure otherwise attempt to use ETHTOOL or MII interface
9cfb40
 
9cfb40
+ child_wait_time SECS         # Time for main process to allow for child processes to exit on termination
9cfb40
+                              #   in seconds (default 5). This can be needed for very large configurations.
9cfb40
+
9cfb40
 .SH Static routes/addresses/rules
9cfb40
 .PP
9cfb40
 Keepalived can configure static addresses, routes, and rules. These addresses are
9cfb40
diff --git a/keepalived/core/global_parser.c b/keepalived/core/global_parser.c
9cfb40
index a59fbc0b..e8ed6333 100644
9cfb40
--- a/keepalived/core/global_parser.c
9cfb40
+++ b/keepalived/core/global_parser.c
9cfb40
@@ -719,6 +719,24 @@ script_security_handler(__attribute__((unused)) vector_t *strvec)
9cfb40
 	global_data->script_security = true;
9cfb40
 }
9cfb40
 
9cfb40
+static void
9cfb40
+child_wait_handler(vector_t *strvec)
9cfb40
+{
9cfb40
+	char *endptr;
9cfb40
+	unsigned long secs;
9cfb40
+
9cfb40
+	if (!strvec)
9cfb40
+		return;
9cfb40
+
9cfb40
+	secs = strtoul(strvec_slot(strvec,1), &endptr, 10);
9cfb40
+	if (*endptr) {
9cfb40
+		log_message(LOG_INFO, "Invalid child_wait_time %s", FMT_STR_VSLOT(strvec, 1));
9cfb40
+		return;
9cfb40
+	}
9cfb40
+
9cfb40
+	child_wait_time = secs;
9cfb40
+}
9cfb40
+
9cfb40
 void
9cfb40
 init_global_keywords(bool global_active)
9cfb40
 {
9cfb40
@@ -730,6 +748,7 @@ init_global_keywords(bool global_active)
9cfb40
 #endif
9cfb40
 	install_keyword_root("use_pid_dir", &use_pid_dir_handler, !global_active);
9cfb40
 	install_keyword_root("instance", &instance_handler, !global_active);
9cfb40
+	install_keyword_root("child_wait_time", &child_wait_handler, !global_active);
9cfb40
 	install_keyword_root("global_defs", NULL, global_active);
9cfb40
 	install_keyword("router_id", &routerid_handler);
9cfb40
 	install_keyword("notification_email_from", &emailfrom_handler);
9cfb40
diff --git a/keepalived/core/main.c b/keepalived/core/main.c
9cfb40
index 95bb76a9..51f83a07 100644
9cfb40
--- a/keepalived/core/main.c
9cfb40
+++ b/keepalived/core/main.c
9cfb40
@@ -93,6 +93,8 @@ bool namespace_with_ipsets;				/* Override for using namespaces and ipsets with
9cfb40
 static char *override_namespace;			/* If namespace specified on command line */
9cfb40
 #endif
9cfb40
 
9cfb40
+unsigned child_wait_time = CHILD_WAIT_SECS;		/* Time to wait for children to exit */
9cfb40
+
9cfb40
 /* Log facility table */
9cfb40
 static struct {
9cfb40
 	int facility;
9cfb40
@@ -359,7 +361,7 @@ sigend(__attribute__((unused)) void *v, __attribute__((unused)) int sig)
9cfb40
 	int wait_count = 0;
9cfb40
 	sigset_t old_set, child_wait;
9cfb40
 	struct timespec timeout = {
9cfb40
-		.tv_sec = CHILD_WAIT_SECS,
9cfb40
+		.tv_sec = child_wait_time,
9cfb40
 		.tv_nsec = 0
9cfb40
 	};
9cfb40
 	struct timeval start_time, now;
9cfb40
@@ -416,17 +418,17 @@ sigend(__attribute__((unused)) void *v, __attribute__((unused)) int sig)
9cfb40
 			gettimeofday(&now, NULL);
9cfb40
 			if (now.tv_usec < start_time.tv_usec) {
9cfb40
 				timeout.tv_nsec = (start_time.tv_usec - now.tv_usec) * 1000;
9cfb40
-				timeout.tv_sec = CHILD_WAIT_SECS - (now.tv_sec - start_time.tv_sec);
9cfb40
+				timeout.tv_sec = child_wait_time - (now.tv_sec - start_time.tv_sec);
9cfb40
 			} else if (now.tv_usec == start_time.tv_usec) {
9cfb40
 				timeout.tv_nsec = 0;
9cfb40
-				timeout.tv_sec = CHILD_WAIT_SECS - (now.tv_sec - start_time.tv_sec);
9cfb40
+				timeout.tv_sec = child_wait_time - (now.tv_sec - start_time.tv_sec);
9cfb40
 			} else {
9cfb40
 				timeout.tv_nsec = (1000000L + start_time.tv_usec - now.tv_usec) * 1000;
9cfb40
-				timeout.tv_sec = CHILD_WAIT_SECS - (now.tv_sec - start_time.tv_sec + 1);
9cfb40
+				timeout.tv_sec = child_wait_time - (now.tv_sec - start_time.tv_sec + 1);
9cfb40
 			}
9cfb40
 
9cfb40
 			timeout.tv_nsec = (start_time.tv_usec - now.tv_usec) * 1000;
9cfb40
-			timeout.tv_sec = CHILD_WAIT_SECS - (now.tv_sec - start_time.tv_sec);
9cfb40
+			timeout.tv_sec = child_wait_time - (now.tv_sec - start_time.tv_sec);
9cfb40
 			if (timeout.tv_nsec < 0) {
9cfb40
 				timeout.tv_nsec += 1000000000L;
9cfb40
 				timeout.tv_sec--;
9cfb40
diff --git a/keepalived/include/main.h b/keepalived/include/main.h
9cfb40
index eebde77e..b03a7efd 100644
9cfb40
--- a/keepalived/include/main.h
9cfb40
+++ b/keepalived/include/main.h
9cfb40
@@ -82,4 +82,7 @@ extern void free_parent_mallocs_exit(void);
9cfb40
 extern char *make_syslog_ident(const char*);
9cfb40
 
9cfb40
 extern int keepalived_main(int, char**); /* The "real" main function */
9cfb40
+
9cfb40
+extern unsigned child_wait_time;
9cfb40
+
9cfb40
 #endif
9cfb40
diff --git a/lib/parser.c b/lib/parser.c
9cfb40
index a5c3465b..2e1beac8 100644
9cfb40
--- a/lib/parser.c
9cfb40
+++ b/lib/parser.c
9cfb40
@@ -120,6 +120,8 @@ install_sublevel_end(void)
9cfb40
 void
9cfb40
 install_keyword_root(const char *string, void (*handler) (vector_t *), bool active)
9cfb40
 {
9cfb40
+	/* If the root keyword is inactive, the handler will still be called,
9cfb40
+	 * but with a NULL strvec */
9cfb40
 	keyword_alloc(keywords, string, handler, active);
9cfb40
 }
9cfb40
 
9cfb40
-- 
9cfb40
2.20.1
9cfb40