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