[CRIU] [PATCH] service: rotate log by SIGHUP

Ruslan Kuprieiev kupruser at gmail.com
Mon Feb 10 22:41:23 PST 2014


Many existing daemons rotate logs by SIGHUP, so lets do the same with our service.

Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
---
 cr-service.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/cr-service.c b/cr-service.c
index 26dd3e2..49454e7 100644
--- a/cr-service.c
+++ b/cr-service.c
@@ -489,12 +489,21 @@ static void reap_worker(int signo)
 	}
 }
 
-static int setup_sigchld_handler()
+static void rotate_log(int signo)
+{
+	pr_info("Rotating log.\n");
+
+	if (log_rotate())
+		exit(1);
+}
+
+static int setup_signal_handlers()
 {
 	struct sigaction action;
 
 	sigemptyset(&action.sa_mask);
 	sigaddset(&action.sa_mask, SIGCHLD);
+	sigaddset(&action.sa_mask, SIGHUP);
 	action.sa_handler	= reap_worker;
 	action.sa_flags		= SA_RESTART;
 
@@ -503,15 +512,23 @@ static int setup_sigchld_handler()
 		return -1;
 	}
 
+	action.sa_handler	= rotate_log;
+
+	if (sigaction(SIGHUP, &action, NULL)) {
+		pr_perror("Can't setup SIGUSR1 handler");
+		return -1;
+	}
+
 	return 0;
 }
 
-static int restore_sigchld_handler()
+static int restore_signal_handlers()
 {
 	struct sigaction action;
 
 	sigemptyset(&action.sa_mask);
 	sigaddset(&action.sa_mask, SIGCHLD);
+	sigaddset(&action.sa_mask, SIGHUP);
 	action.sa_handler	= SIG_DFL;
 	action.sa_flags		= SA_RESTART;
 
@@ -520,6 +537,11 @@ static int restore_sigchld_handler()
 		return -1;
 	}
 
+	if (sigaction(SIGHUP, &action, NULL)) {
+		pr_perror("Can't restore SIGHUP handler");
+		return -1;
+	}
+
 	return 0;
 }
 
@@ -596,7 +618,7 @@ int cr_service(bool daemon_mode)
 		}
 	}
 
-	if (setup_sigchld_handler())
+	if (setup_signal_handlers())
 		goto err;
 
 	while (1) {
@@ -615,7 +637,7 @@ int cr_service(bool daemon_mode)
 		if (child_pid == 0) {
 			int ret;
 
-			if (restore_sigchld_handler())
+			if (restore_signal_handlers())
 				exit(1);
 
 			close(server_fd);
-- 
1.8.3.2



More information about the CRIU mailing list