]> arthur.barton.de Git - netdata.git/blobdiff - src/main.c
fixed all crashes; empty responses are now formatted properly; fixed exit procedure...
[netdata.git] / src / main.c
index 44dff9d744491e5e4a52935e2aa3bc3d4858fbe0..d73dd5901d85a071185efae4869df6c2c2da3d3d 100755 (executable)
 
 #include "main.h"
 
+int netdata_exit = 0;
+
+void netdata_cleanup_and_exit(int ret)
+{
+       kill_childs();
+       rrdset_free_all();
+       //unlink("/var/run/netdata.pid");
+       info("NetData exiting. Bye bye...");
+       exit(ret);
+}
+
 struct netdata_static_thread {
        char *name;
 
@@ -74,7 +85,7 @@ int killpid(pid_t pid, int sig)
        errno = 0;
        if(kill(pid, 0) == -1) {
                switch(errno) {
-                       case ESRCH: 
+                       case ESRCH:
                                error("Request to kill pid %d, but it is not running.", pid);
                                break;
 
@@ -89,8 +100,8 @@ int killpid(pid_t pid, int sig)
        }
        else {
                errno = 0;
-               
-               void (*old)(int);               
+
+               void (*old)(int);
                old = signal(sig, SIG_IGN);
                if(old == SIG_ERR) {
                        error("Cannot overwrite signal handler for signal %d", sig);
@@ -104,7 +115,7 @@ int killpid(pid_t pid, int sig)
 
                if(ret == -1) {
                        switch(errno) {
-                               case ESRCH: 
+                               case ESRCH:
                                        error("Cannot kill pid %d, but it is not running.", pid);
                                        break;
 
@@ -133,7 +144,7 @@ void kill_childs()
                pthread_join(w->thread, NULL);
        }
 
-       int i;  
+       int i;
        for (i = 0; static_threads[i].name != NULL ; i++) {
                if(static_threads[i].thread) {
                        debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
@@ -146,7 +157,7 @@ void kill_childs()
        if(tc_child_pid) {
                info("Killing tc-qos-helper procees");
                if(killpid(tc_child_pid, SIGTERM) != -1)
-                       waitid(tc_child_pid, 0, &info, WEXITED);
+                       waitid(P_PID, tc_child_pid, &info, WEXITED);
        }
        tc_child_pid = 0;
 
@@ -159,10 +170,14 @@ void kill_childs()
                if(cd->pid && !cd->obsolete) {
                        debug(D_EXIT, "killing %s plugin process", cd->id);
                        if(killpid(cd->pid, SIGTERM) != -1)
-                               waitid(cd->pid, 0, &info, WEXITED);
+                               waitid(P_PID, cd->pid, &info, WEXITED);
                }
        }
 
+       // if, for any reason there is any child exited
+       // catch it here
+       waitid(P_PID, 0, &info, WEXITED|WNOHANG);
+
        debug(D_EXIT, "All threads/childs stopped.");
 }
 
@@ -235,6 +250,20 @@ int main(int argc, char **argv)
 
        if(!config_loaded) load_config(NULL, 0);
 
+       // prepare configuration environment variables for the plugins
+       setenv("NETDATA_CONFIG_DIR" , config_get("global", "config directory"   , CONFIG_DIR) , 1);
+       setenv("NETDATA_PLUGINS_DIR", config_get("global", "plugins directory"  , PLUGINS_DIR), 1);
+       setenv("NETDATA_WEB_DIR"    , config_get("global", "web files directory", WEB_DIR)    , 1);
+       setenv("NETDATA_CACHE_DIR"  , config_get("global", "cache directory"    , CACHE_DIR)  , 1);
+       setenv("NETDATA_LOG_DIR"    , config_get("global", "log directory"      , LOG_DIR)    , 1);
+
+       // avoid extended to stat(/etc/localtime)
+       // http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
+       setenv("TZ", ":/etc/localtime", 0);
+
+       // cd to /tmp to avoid any plugins writing files at random places
+       if(chdir("/tmp")) fprintf(stderr, "netdata: ERROR: Cannot cd to /tmp: %s", strerror(errno));
+
        char *input_log_file = NULL;
        char *output_log_file = NULL;
        char *error_log_file = NULL;
@@ -247,6 +276,8 @@ int main(int argc, char **argv)
 
                sprintf(buffer, "0x%08llx", 0ULL);
                char *flags = config_get("global", "debug flags", buffer);
+               setenv("NETDATA_DEBUG_FLAGS", flags, 1);
+
                debug_flags = strtoull(flags, NULL, 0);
                debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
 
@@ -259,6 +290,7 @@ int main(int argc, char **argv)
                // --------------------------------------------------------------------
 
                global_host_prefix = config_get("global", "host access prefix", "");
+               setenv("NETDATA_HOST_PREFIX", global_host_prefix, 1);
 
                // --------------------------------------------------------------------
 
@@ -332,8 +364,15 @@ int main(int argc, char **argv)
                }
                else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
 
+               // let the plugins know the min update_every
+               {
+                       char buffer[50];
+                       snprintf(buffer, 50, "%d", rrd_update_every);
+                       setenv("NETDATA_UPDATE_EVERY", buffer, 1);
+               }
+
                // --------------------------------------------------------------------
-               
+
                for (i = 0; static_threads[i].name != NULL ; i++) {
                        struct netdata_static_thread *st = &static_threads[i];
 
@@ -344,7 +383,8 @@ int main(int argc, char **argv)
                // --------------------------------------------------------------------
 
                prepare_rundir();
-               user = config_get("global", "run as user", (getuid() == 0)?NETDATA_USER:"");
+               user = config_get("global", "run as user"    , (getuid() == 0)?NETDATA_USER:"");
+               web_files_uid();
 
                // --------------------------------------------------------------------
 
@@ -420,7 +460,17 @@ int main(int argc, char **argv)
        }
 
        // for future use - the main thread
-       while(1) sleep(60);
+       while(1) {
+               if(netdata_exit != 0) {
+                       netdata_exit++;
+
+                       if(netdata_exit > 5) {
+                               netdata_cleanup_and_exit(0);
+                               exit(0);
+                       }
+               }
+               sleep(2);
+       }
 
        exit(0);
 }