]> arthur.barton.de Git - netdata.git/blobdiff - src/daemon.c
proper log file management; re-opening logs on SIGHUP; updated logrotate; updated...
[netdata.git] / src / daemon.c
index 925abbf302a2bd082b3712408c1fd6371d08ec0a..5d24b6cabe39ab7951b9c683d45eda7c5b8b083d 100644 (file)
@@ -5,22 +5,32 @@ char pidfile[FILENAME_MAX + 1] = "";
 
 void sig_handler_exit(int signo)
 {
-       if(signo) {
-               error_log_limit_unlimited();
-               error("Received signal %d. Exiting...", signo);
-               netdata_exit = 1;
-       }
+    if(signo) {
+        error_log_limit_unlimited();
+        error("Received signal %d. Exiting...", signo);
+        netdata_exit = 1;
+    }
+}
+
+void sig_handler_logrotate(int signo)
+{
+    if(signo) {
+        error_log_limit_reset();
+        info("Received signal %d to re-open the log files", signo);
+        reopen_all_log_files();
+    }
 }
 
 void sig_handler_save(int signo)
 {
        if(signo) {
+        error_log_limit_reset();
                info("Received signal %d to save the database...", signo);
                rrdset_save_all();
        }
 }
 
-static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
+static void chown_open_file(int fd, uid_t uid, gid_t gid) {
        if(fd == -1) return;
 
        struct stat buf;
@@ -36,7 +46,7 @@ static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid)
        }
 }
 
-int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
+int become_user(const char *username, int pid_fd)
 {
        struct passwd *pw = getpwnam(username);
        if(!pw) {
@@ -59,10 +69,10 @@ int become_user(const char *username, int access_fd, int output_fd, int error_fd
                }
        }
 
-       properly_chown_netdata_generated_file(access_fd, uid, gid);
-       properly_chown_netdata_generated_file(output_fd, uid, gid);
-       properly_chown_netdata_generated_file(error_fd, uid, gid);
-       properly_chown_netdata_generated_file(pid_fd, uid, gid);
+       chown_open_file(STDOUT_FILENO, uid, gid);
+       chown_open_file(STDERR_FILENO, uid, gid);
+       chown_open_file(stdaccess_fd, uid, gid);
+       chown_open_file(pid_fd, uid, gid);
 
        if(supplementary_groups && ngroups) {
                if(setgroups(ngroups, supplementary_groups) == -1)
@@ -133,81 +143,8 @@ int sched_setscheduler_idle(void) {
        return i;
 }
 
-int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
+int become_daemon(int dont_fork, const char *user)
 {
-       fflush(NULL);
-
-       // open the files before forking
-       int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
-
-       if(input && *input) {
-               if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
-                       error("Cannot open input file '%s'.", input);
-                       return -1;
-               }
-       }
-
-       if(output && *output && strcmp(output, "/dev/null") != 0) {
-               if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
-                       error("Cannot open output log file '%s'", output);
-                       if(input_fd != -1) close(input_fd);
-                       return -1;
-               }
-       }
-
-       if(error && *error && strcmp(error, "/dev/null") != 0) {
-               if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
-                       error("Cannot open error log file '%s'.", error);
-                       if(input_fd != -1) close(input_fd);
-                       if(output_fd != -1) close(output_fd);
-                       return -1;
-               }
-       }
-
-       if(access && *access && access_fd && strcmp(access, "/dev/null") != 0) {
-               if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
-                       error("Cannot open access log file '%s'", access);
-                       if(input_fd != -1) close(input_fd);
-                       if(output_fd != -1) close(output_fd);
-                       if(error_fd != -1) close(error_fd);
-                       return -1;
-               }
-
-               if(access_fp) {
-                       *access_fp = fdopen(*access_fd, "w");
-                       if(!*access_fp) {
-                               error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
-                               if(input_fd != -1) close(input_fd);
-                               if(output_fd != -1) close(output_fd);
-                               if(error_fd != -1) close(error_fd);
-                               close(*access_fd);
-                               *access_fd = -1;
-                               return -1;
-                       }
-                       if(setvbuf(*access_fp, NULL, _IOLBF, 0) != 0)
-                               error("Cannot set line buffering on access.log");
-               }
-       }
-
-       if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
-               perror("Cannot open /dev/null");
-               if(input_fd != -1) close(input_fd);
-               if(output_fd != -1) close(output_fd);
-               if(error_fd != -1) close(error_fd);
-               if(access && access_fd && *access_fd != -1) {
-                       close(*access_fd);
-                       *access_fd = -1;
-                       if(access_fp) {
-                               fclose(*access_fp);
-                               *access_fp = NULL;
-                       }
-               }
-               return -1;
-       }
-
-       // all files opened
-       // lets do it
-
        if(!dont_fork) {
                int i = fork();
                if(i == -1) {
@@ -235,70 +172,10 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
                }
        }
 
-       // close all files
-       if(close_all_files) {
-               int i;
-               for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
-                       if(
-                               ((access_fd && i != *access_fd) || !access_fd)
-                               && i != dev_null
-                               && i != input_fd
-                               && i != output_fd
-                               && i != error_fd
-                               && fd_is_valid(i)
-                               ) close(i);
-       }
-       else {
-               close(STDIN_FILENO);
-               close(STDOUT_FILENO);
-               close(STDERR_FILENO);
-       }
-
-       // put the opened files
-       // to our standard file descriptors
-       if(input_fd != -1) {
-               if(input_fd != STDIN_FILENO) {
-                       dup2(input_fd, STDIN_FILENO);
-                       close(input_fd);
-               }
-               input_fd = -1;
-       }
-       else dup2(dev_null, STDIN_FILENO);
-
-       if(output_fd != -1) {
-               if(output_fd != STDOUT_FILENO) {
-                       dup2(output_fd, STDOUT_FILENO);
-                       close(output_fd);
-               }
-
-               if(setvbuf(stdout, NULL, _IOLBF, 0) != 0)
-                       error("Cannot set line buffering on debug.log");
-
-               output_fd = STDOUT_FILENO;
-       }
-       else dup2(dev_null, STDOUT_FILENO);
-
-       if(error_fd != -1) {
-               if(error_fd != STDERR_FILENO) {
-                       dup2(error_fd, STDERR_FILENO);
-                       close(error_fd);
-               }
-
-               if(setvbuf(stderr, NULL, _IOLBF, 0) != 0)
-                       error("Cannot set line buffering on error.log");
-
-               error_fd = STDERR_FILENO;
-       }
-       else dup2(dev_null, STDERR_FILENO);
-
-       // close /dev/null
-       if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
-               close(dev_null);
-
        // generate our pid file
        int pidfd = -1;
        if(pidfile[0]) {
-               pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
+               pidfd = open(pidfile, O_WRONLY | O_CREAT, 0644);
                if(pidfd >= 0) {
                        if(ftruncate(pidfd, 0) != 0)
                                error("Cannot truncate pidfile '%s'.", pidfile);
@@ -325,7 +202,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
        }
 
        if(user && *user) {
-               if(become_user(user, (access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
+               if(become_user(user, pidfd) != 0) {
                        error("Cannot become user '%s'. Continuing as we are.", user);
                }
                else info("Successfully became user '%s'.", user);