SIGUSR1 saves the database
Costa Tsaousis (ktsaou) committed
Jul 15, 2016 at 21:42 UTC
d806c88d4b61a1bec08a51eb17cbf3f4eb4e1e30
4 files changed
+24
-4
src/daemon.c
+9
-1
@@ -27,7 +27,7 @@
27
28
char pidfile[FILENAME_MAX + 1] = "";
29
30
-void sig_handler(int signo)
30
+void sig_handler_exit(int signo)
31
{
32
if(signo) {
33
error_log_limit_unlimited();
@@ -36,6 +36,14 @@ void sig_handler(int signo)
36
}
37
}
38
39
+void sig_handler_save(int signo)
40
+{
41
+ if(signo) {
42
+ info("Received signal %d to save the database...", signo);
43
+ rrdset_save_all();
44
+ }
45
+}
46
+
47
static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
48
if(fd == -1) return;
49
src/daemon.h
+2
-1
@@ -1,7 +1,8 @@
1
#ifndef NETDATA_DAEMON_H
2
#define NETDATA_DAEMON_H 1
3
4
-extern void sig_handler(int signo);
4
+extern void sig_handler_exit(int signo);
5
+extern void sig_handler_save(int signo);
6
7
extern int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd);
8
src/main.c
+9
-2
@@ -591,7 +591,7 @@ int main(int argc, char **argv)
591
sigaddset(&sa.sa_mask, SIGHUP);
592
sigaddset(&sa.sa_mask, SIGINT);
593
sigaddset(&sa.sa_mask, SIGTERM);
594
- sa.sa_handler = sig_handler;
594
+ sa.sa_handler = sig_handler_exit;
595
sa.sa_flags = 0;
596
if(sigaction(SIGHUP, &sa, NULL) == -1) {
597
error("Failed to change signal handler for SIGHUP");
@@ -602,12 +602,19 @@ int main(int argc, char **argv)
602
if(sigaction(SIGTERM, &sa, NULL) == -1) {
603
error("Failed to change signal handler for SIGTERM");
604
}
605
+
606
+ // save database on SIGUSR1
607
+ sa.sa_handler = sig_handler_save;
608
+ if(sigaction(SIGUSR1, &sa, NULL) == -1) {
609
+ error("Failed to change signal handler for SIGUSR1");
610
+ }
611
+
612
// Ignore SIGPIPE completely.
613
// INFO: If we add signals here we have to unblock them
614
// at popen.c when running a external plugin.
615
sa.sa_handler = SIG_IGN;
616
if(sigaction(SIGPIPE, &sa, NULL) == -1) {
610
- error("Failed to change signal handler for SIGTERM");
617
+ error("Failed to change signal handler for SIGPIPE");
618
}
619
620
// --------------------------------------------------------------------
src/popen.c
+4
@@ -129,6 +129,10 @@ FILE *mypopen(const char *command, pid_t *pidptr)
129
sigemptyset(&sa.sa_mask);
130
sa.sa_handler = SIG_DFL;
131
sa.sa_flags = 0;
132
+
133
+ if(sigaction(SIGUSR1, &sa, NULL) == -1)
134
+ error("pre-execution of command '%s' on pid %d: failed to set default signal handler for SIGUSR1.", command, getpid());
135
+
136
if(sigaction(SIGPIPE, &sa, NULL) == -1)
137
error("pre-execution of command '%s' on pid %d: failed to set default signal handler for SIGPIPE.", command, getpid());
138
}