git » nmdb » commit 18874ee

nmdb: Add a signal handler to change to read-only mode

author Alberto Bertogli
2010-05-03 04:44:51 UTC
committer Alberto Bertogli
2010-05-03 07:19:02 UTC
parent 81777f85eb0e6f4c3db6f8301911c0fb057da143

nmdb: Add a signal handler to change to read-only mode

This patch adds a new signal handler that can be used to change to
read-only mode.

As SIGUSR1 is a nice signal to assign to this, but it is taken by the
log reopening. Since SIGHUP is the one traditionally used for that
purpose, we reassign them so now we have:

 - SIGHUP: reopen log file
 - SIGUSR1: switch to read-only mode
 - SIGUSR2: toggle passive mode

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

UPGRADING +5 -0
nmdb/net.c +16 -2
tests/coverage/coverage +2 -0

diff --git a/UPGRADING b/UPGRADING
index bd906cd..f839937 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -5,6 +5,11 @@ While normally nothing should be done, sometimes things change in incompatible
 ways. Here's the listing for the releases, you should always check it before
 upgrading.
 
+0.22 -> 0.23
+ * The server signal handling for SIGUSR1 has been remapped: to reopen the
+   file descriptors send SIGHUP instead of SIGUSR1. SIGUSR1 now puts the
+   server in read-only mode.
+
 0.20 -> 0.21
  * nmdb_get() and nmdb_cache_get() now return -1 instead of 0 when there is a
    missing key. This was done to allow setting a value to be "" (no data). All
diff --git a/nmdb/net.c b/nmdb/net.c
index e1cc3b9..2020e62 100644
--- a/nmdb/net.c
+++ b/nmdb/net.c
@@ -35,6 +35,16 @@ static void logfd_reopen_sighandler(int fd, short event, void *arg)
 		wlog("Log reopened\n");
 }
 
+static void enable_read_only_sighandler(int fd, short event, void *arg)
+{
+	if (!settings.read_only) {
+		wlog("Changing to read-only mode\n");
+		settings.read_only = 1;
+	} else {
+		wlog("Got signal, but already in read-only mode\n");
+	}
+}
+
 void net_loop(void)
 {
 	int tipc_fd = -1;
@@ -42,7 +52,8 @@ 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, sigusr1_evt, sigusr2_evt;
+		     sigterm_evt, sigint_evt,
+		     sighup_evt, sigusr1_evt, sigusr2_evt;
 
 	event_init();
 
@@ -101,7 +112,10 @@ 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,
+	signal_set(&sighup_evt, SIGHUP, logfd_reopen_sighandler,
+			&sighup_evt);
+	signal_add(&sighup_evt, NULL);
+	signal_set(&sigusr1_evt, SIGUSR1, enable_read_only_sighandler,
 			&sigusr1_evt);
 	signal_add(&sigusr1_evt, NULL);
 	signal_set(&sigusr2_evt, SIGUSR2, passive_to_active_sighandler,
diff --git a/tests/coverage/coverage b/tests/coverage/coverage
index 4e4ad08..bcdfb93 100755
--- a/tests/coverage/coverage
+++ b/tests/coverage/coverage
@@ -93,7 +93,9 @@ nmdb_and_kill -d /dev/null	# supposed to fail
 
 out "+ signals"
 nmdb
+run killall -HUP nmdb
 run killall -USR1 nmdb
+run killall -USR1 nmdb		# the second time gets a different message
 run killall -USR2 nmdb
 run killall -TERM nmdb
 wait `pidof nmdb`