]> arthur.barton.de Git - netdata.git/commitdiff
Squashed commit of the following:
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sun, 3 Apr 2016 20:55:53 +0000 (23:55 +0300)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sun, 3 Apr 2016 20:55:53 +0000 (23:55 +0300)
commit 950f04df117bf246cfa9a2fa99bf12a64d6bd0a4
Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr>
Date:   Sun Apr 3 23:47:06 2016 +0300

    dont keep the pidfd open

commit bd45505d0177703940b2393a5e36b3e80b912ff1
Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr>
Date:   Sun Apr 3 23:33:18 2016 +0300

    dont unlink pidfile at start

commit 4afd30e81d3cd70400162c7603b659fd35160c8b
Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr>
Date:   Sun Apr 3 23:30:47 2016 +0300

    close pidfile then unlink

commit faf66f4f5f39bda8d0b5e947afbdb8cd1ac505be
Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr>
Date:   Sun Apr 3 19:38:49 2016 +0300

    fix to keep close the file if it has been successfully chwoned

commit 0af1e73e6db3b681e8303ee394fc2ac003db6e67
Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr>
Date:   Sun Apr 3 19:35:31 2016 +0300

    chown pidfile only when dropping privileges; do not keep the pidfile open when not dropping privileges

commit 550c9f8e9757f5a5767d11764a80f7736c197b37
Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr>
Date:   Sun Apr 3 19:15:32 2016 +0300

    do not chown pidfile

commit ca808d2231ed175397408ea436a50642e10cd281
Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr>
Date:   Sun Apr 3 18:57:30 2016 +0300

    added command line parameter -pidfile; the pidfile is only generated if this parameter is passed; #151 #156

.gitignore
netdata-installer.sh
src/daemon.c
src/daemon.h
src/main.c
system/netdata-openrc.in
system/netdata.service.in

index e6f03c7ad5d2e5d65bb3ff94d6f5d3516ef3b3c8..78a63f72c2515fdde00ad1b583c01366632564d8 100644 (file)
@@ -56,3 +56,4 @@ web/control.html
 web/datasource.css
 web/gadget.xml
 web/index_new.html
+system/netdata-openrc
index 88333478d9ba08108cde8890ba4f93ce414770bb..2b316ff784da449241692031baf33ffed16af691 100755 (executable)
@@ -314,7 +314,7 @@ do
 
        count=$((count + 1))
 
-       pid=$(cat /var/run/netdata/netdata.pid 2>/dev/null)
+       pid=$(cat /var/run/netdata/netdata.pid 2>/dev/null || cat /var/run/netdata.pid 2>/dev/null)
        isnetdata $pid || pid=
        if [ ! -z "${pid}" ]
                then
@@ -334,7 +334,7 @@ echo >&2
 # run netdata
 
 echo >&2 "Starting netdata..."
-run ${NETDATA_PREFIX}/usr/sbin/netdata "${@}"
+run ${NETDATA_PREFIX}/usr/sbin/netdata -pidfile /var/run/netdata.pid "${@}"
 
 if [ $? -ne 0 ]
        then
index 1428a6153f7e232152770c109327431af8255148..2dd7b09c85ab6278e7b68b9e54fb95fec8da94ab 100644 (file)
@@ -24,6 +24,9 @@
 #include "main.h"
 #include "daemon.h"
 
+char pidfile[FILENAME_MAX + 1] = "";
+int pidfd = -1;
+
 void sig_handler(int signo)
 {
        switch(signo) {
@@ -71,23 +74,6 @@ void sig_handler(int signo)
        }
 }
 
-char rundir[FILENAME_MAX + 1] = RUN_DIR;
-char pidfile[FILENAME_MAX + 1] = "";
-void prepare_rundir() {
-       if(getuid() != 0) {
-               mkdir("/run/user", 0775);
-               snprintf(rundir, FILENAME_MAX, "/run/user/%d", getuid());
-               mkdir(rundir, 0775);
-               snprintf(rundir, FILENAME_MAX, "/run/user/%d/netdata", getuid());
-       }
-
-       snprintf(pidfile, FILENAME_MAX, "%s/netdata.pid", rundir);
-
-       if(mkdir(rundir, 0775) != 0) {
-               if(errno != EEXIST) error("Cannot create directory '%s'.", rundir);
-       }
-}
-
 int become_user(const char *username)
 {
        struct passwd *pw = getpwnam(username);
@@ -96,9 +82,21 @@ int become_user(const char *username)
                return -1;
        }
 
-       if(chown(rundir, pw->pw_uid, pw->pw_gid) != 0) {
-               error("Cannot chown directory '%s' to user %s.", rundir, username);
-               return -1;
+       if(pidfile[0] && getuid() != pw->pw_uid) {
+               // we are dropping privileges
+               if(chown(pidfile, pw->pw_uid, pw->pw_gid) != 0)
+                       error("Cannot chown pidfile '%s' to user '%s'", pidfile, username);
+
+               else if(pidfd != -1) {
+                       // not need to keep it open
+                       close(pidfd);
+                       pidfd = -1;
+               }
+       }
+       else if(pidfd != -1) {
+               // not need to keep it open
+               close(pidfd);
+               pidfd = -1;
        }
 
        if(setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
@@ -293,15 +291,20 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
                close(dev_null);
 
        // generate our pid file
-       {
-               unlink(pidfile);
-               int fd = open(pidfile, O_RDWR | O_CREAT, 0666);
-               if(fd >= 0) {
+       if(pidfile[0]) {
+               pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
+               if(pidfd >= 0) {
+                       if(ftruncate(pidfd, 0) != 0)
+                               error("Cannot truncate pidfile '%s'.", pidfile);
+
                        char b[100];
                        sprintf(b, "%d\n", getpid());
-                       ssize_t i = write(fd, b, strlen(b));
-                       if(i <= 0) perror("Cannot write pid to file.");
-                       close(fd);
+                       ssize_t i = write(pidfd, b, strlen(b));
+                       if(i <= 0)
+                               error("Cannot write pidfile '%s'.", pidfile);
+
+                       // don't close it, we might need it at exit
+                       // close(pidfd);
                }
        }
 
@@ -311,6 +314,8 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
                }
                else info("Successfully became user '%s'.", user);
        }
+       else if(pidfd != -1)
+               close(pidfd);
 
        return(0);
 }
index 77186daae2756b397b7abd33bc88d61b0d9b8ae4..0642be3c0f9f5f163f75cb0b465ac546b41df650 100644 (file)
@@ -3,12 +3,13 @@
 
 extern void sig_handler(int signo);
 
-extern void prepare_rundir();
-
 extern int become_user(const char *username);
 
 extern 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);
 
 extern void netdata_cleanup_and_exit(int i);
 
+extern char pidfile[];
+extern int pidfd;
+
 #endif /* NETDATA_DAEMON_H */
index 3d8edcfe90a9b7d0eb82e9d23b0b4cc37f710a9c..1ba8e461e4753110e868d92e328ed0a93f0b4584 100644 (file)
@@ -44,7 +44,20 @@ void netdata_cleanup_and_exit(int ret)
        netdata_exit = 1;
        rrdset_save_all();
        // kill_childs();
-       unlink(RUN_DIR "/netdata.pid");
+
+       if(pidfd != -1) {
+               if(ftruncate(pidfd, 0) != 0)
+                       error("Cannot truncate pidfile '%s'.", pidfile);
+
+               close(pidfd);
+               pidfd = -1;
+       }
+
+       if(pidfile[0]) {
+               if(unlink(pidfile) != 0)
+                       error("Cannot unlink pidfile '%s'.", pidfile);
+       }
+
        info("NetData exiting. Bye bye...");
        exit(ret);
 }
@@ -220,6 +233,11 @@ int main(int argc, char **argv)
                else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) { config_set("global", "host access prefix", argv[i+1]); i++; }
                else if(strcmp(argv[i], "-stacksize") == 0 && (i+1) < argc) { config_set("global", "pthread stack size", argv[i+1]); i++; }
                else if(strcmp(argv[i], "-nodaemon") == 0 || strcmp(argv[i], "-nd") == 0) dont_fork = 1;
+               else if(strcmp(argv[i], "-pidfile") == 0 && (i+1) < argc) {
+                       i++;
+                       strncpy(pidfile, argv[i], FILENAME_MAX);
+                       pidfile[FILENAME_MAX] = '\0';
+               }
                else if(strcmp(argv[i], "--unittest")  == 0) {
                        rrd_update_every = 1;
                        if(run_all_mockup_tests()) exit(1);
@@ -239,6 +257,7 @@ int main(int argc, char **argv)
                        fprintf(stderr, "  -nd or -nodeamon to disable forking in the background. Default: unset.\n");
                        fprintf(stderr, "  -df FLAGS debug options. Default: 0x%08llx.\n", debug_flags);
                        fprintf(stderr, "  -stacksize BYTES to overwrite the pthread stack size.\n");
+                       fprintf(stderr, "  -pidfile FILENAME to save a pid while running.\n");
                        exit(1);
                }
        }
@@ -401,7 +420,6 @@ int main(int argc, char **argv)
 
                // --------------------------------------------------------------------
 
-               prepare_rundir();
                user = config_get("global", "run as user"    , (getuid() == 0)?NETDATA_USER:"");
                web_files_uid();
 
index 8ac87f869eb7b11cf40f4936ee82adb00ace9358..06bb7ffb20dcd9e7fde31c86cddb4125ea1251af 100755 (executable)
@@ -25,7 +25,7 @@ extra_started_commands="getconf"
 pidfile="/run/netdata/netdata.pid"
 command="${NETDATA_INSTALL_PATH}/usr/sbin/netdata"
 command_background="yes"
-command_args="${NETDATA_EXTRA_ARGS}"
+command_args="-pidfile ${pidfile} ${NETDATA_EXTRA_ARGS}"
 start_stop_daemon_args="-u ${NETDATA_OWNER}"
 
 depend() {
index 266bc65b1c4fe732363c9eb512d6dc37be114c2d..e828707ab6c185a0bfe45f238dac85be3836bb1d 100644 (file)
@@ -8,7 +8,7 @@ WorkingDirectory=/tmp
 User=root
 Group=root
 PIDFile=@localstatedir_POST@/run/netdata/netdata.pid
-ExecStart=@sbindir_POST@/netdata
+ExecStart=@sbindir_POST@/netdata -pidfile $PIDFile
 ExecStop=/bin/kill -SIGTERM $MAINPID
 TimeoutStopSec=30