git » nmdb » commit ef3c2b1

Make SIGUSR1 reopen the log file

author Alberto Bertogli
2008-06-01 03:57:35 UTC
committer Alberto Bertogli
2008-06-01 03:58:21 UTC
parent eb583f656d3f611816198ffb48775d1228561689

Make SIGUSR1 reopen the log file

It's quite common for log rotators to move the log file and then signal
the daemon to reopen it. This patch allows this by making SIGUSR1 causing
a reopening of the log file.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

nmdb/log.c +15 -0
nmdb/log.h +1 -0
nmdb/net.c +11 -1

diff --git a/nmdb/log.c b/nmdb/log.c
index 10e223d..8162493 100644
--- a/nmdb/log.c
+++ b/nmdb/log.c
@@ -35,6 +35,21 @@ int log_init(void)
 	return 1;
 }
 
+int log_reopen(void)
+{
+	if (settings.logfname == NULL)
+		return 1;
+
+	if (strcmp(settings.logfname, "-") == 0)
+		return 1;
+
+	logfd = open(settings.logfname, O_WRONLY | O_APPEND | O_CREAT, 0660);
+	if (logfd < 0)
+		return 0;
+
+	return 1;
+}
+
 void wlog(const char *fmt, ...)
 {
 	int r, tr;
diff --git a/nmdb/log.h b/nmdb/log.h
index b187cc0..3e5672b 100644
--- a/nmdb/log.h
+++ b/nmdb/log.h
@@ -6,6 +6,7 @@
 #define MAX_LOG_STR 512
 
 int log_init(void);
+int log_reopen(void);
 
 /* Normal logging, printf()-alike */
 void wlog(const char *fmt, ...);
diff --git a/nmdb/net.c b/nmdb/net.c
index fc01220..e1cc3b9 100644
--- a/nmdb/net.c
+++ b/nmdb/net.c
@@ -29,6 +29,12 @@ static void passive_to_active_sighandler(int fd, short event, void *arg)
 	settings.passive = !settings.passive;
 }
 
+static void logfd_reopen_sighandler(int fd, short event, void *arg)
+{
+	if (log_reopen())
+		wlog("Log reopened\n");
+}
+
 void net_loop(void)
 {
 	int tipc_fd = -1;
@@ -36,7 +42,7 @@ void net_loop(void)
 	int udp_fd = -1;
 	int sctp_fd = -1;
 	struct event tipc_evt, tcp_evt, udp_evt, sctp_evt,
-		     sigterm_evt, sigint_evt, sigusr2_evt;
+		     sigterm_evt, sigint_evt, sigusr1_evt, sigusr2_evt;
 
 	event_init();
 
@@ -95,6 +101,9 @@ void net_loop(void)
 	signal_add(&sigterm_evt, NULL);
 	signal_set(&sigint_evt, SIGINT, exit_sighandler, &sigint_evt);
 	signal_add(&sigint_evt, NULL);
+	signal_set(&sigusr1_evt, SIGUSR1, logfd_reopen_sighandler,
+			&sigusr1_evt);
+	signal_add(&sigusr1_evt, NULL);
 	signal_set(&sigusr2_evt, SIGUSR2, passive_to_active_sighandler,
 			&sigusr2_evt);
 	signal_add(&sigusr2_evt, NULL);
@@ -112,6 +121,7 @@ void net_loop(void)
 
 	signal_del(&sigterm_evt);
 	signal_del(&sigint_evt);
+	signal_del(&sigusr1_evt);
 	signal_del(&sigusr2_evt);
 
 	tipc_close(tipc_fd);