]> arthur.barton.de Git - netdata.git/blobdiff - src/main.c
Merge pull request #1801 from l2isbad/freeradius_plugin_p26_fix
[netdata.git] / src / main.c
index 36c022f8cdec4bbb926bd7860884c5389761f31a..278e0aafd827c968539300e619c7ba3ab3956f85 100644 (file)
@@ -46,8 +46,8 @@ struct netdata_static_thread static_threads[] = {
 #else
     {"proc",               "plugins",   "proc",       1, NULL, NULL, proc_main},
     {"diskspace",          "plugins",   "diskspace",  1, NULL, NULL, proc_diskspace_main},
-#endif /* __FreeBSD__, __APPLE__*/
     {"cgroups",            "plugins",   "cgroups",    1, NULL, NULL, cgroups_main},
+#endif /* __FreeBSD__, __APPLE__*/
     {"check",              "plugins",   "checks",     0, NULL, NULL, checks_main},
     {"backends",            NULL,       NULL,         1, NULL, NULL, backends_main},
     {"health",              NULL,       NULL,         1, NULL, NULL, health_main},
@@ -71,7 +71,9 @@ void web_server_threading_selection(void) {
 
     web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
 
-    web_donotrack_comply = config_get_boolean("global", "respect web browser do not track policy", web_donotrack_comply);
+    respect_web_browser_do_not_track_policy = config_get_boolean("global", "respect web browser do not track policy", respect_web_browser_do_not_track_policy);
+    web_x_frame_options = config_get("global", "web x-frame-options header", "");
+    if(!*web_x_frame_options) web_x_frame_options = NULL;
 
 #ifdef NETDATA_WITH_ZLIB
     web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
@@ -161,17 +163,19 @@ void kill_childs()
         pthread_cancel(w->thread);
         // it is detached
         // pthread_join(w->thread, NULL);
+
+        w->obsolete = 1;
     }
 
     int i;
     for (i = 0; static_threads[i].name != NULL ; i++) {
-        if(static_threads[i].enabled && static_threads[i].thread) {
+        if(static_threads[i].enabled) {
             info("Stopping %s thread", static_threads[i].name);
             pthread_cancel(*static_threads[i].thread);
             // it is detached
             // pthread_join(*static_threads[i].thread, NULL);
 
-            static_threads[i].thread = NULL;
+            static_threads[i].enabled = 0;
         }
     }
 
@@ -179,19 +183,15 @@ void kill_childs()
         info("Killing tc-qos-helper process %d", tc_child_pid);
         if(killpid(tc_child_pid, SIGTERM) != -1)
             waitid(P_PID, (id_t) tc_child_pid, &info, WEXITED);
+
+        tc_child_pid = 0;
     }
-    tc_child_pid = 0;
 
     struct plugind *cd;
     for(cd = pluginsd_root ; cd ; cd = cd->next) {
         if(cd->enabled && !cd->obsolete) {
-            if(cd->thread != (pthread_t)NULL) {
-                info("Stopping %s plugin thread", cd->id);
-                pthread_cancel(cd->thread);
-                // they are detached
-                // pthread_join(cd->thread, NULL);
-                cd->thread = (pthread_t)NULL;
-            }
+            info("Stopping %s plugin thread", cd->id);
+            pthread_cancel(cd->thread);
 
             if(cd->pid) {
                 info("killing %s plugin child process pid %d", cd->id, cd->pid);
@@ -257,8 +257,8 @@ void help(int exitcode) {
             " |   '-'   '-'   '-'   '-'   real-time performance monitoring, done right!   \n"
             " +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->\n"
             "\n"
-            " Copyright (C) 2017, Costa Tsaousis <costa@tsaousis.gr>\n"
-            " Released under GNU Public License v3 or later.\n"
+            " Copyright (C) 2016-2017, Costa Tsaousis <costa@tsaousis.gr>\n"
+            " Released under GNU General Public License v3 or later.\n"
             " All rights reserved.\n"
             "\n"
             " Home Page  : https://my-netdata.io\n"
@@ -334,8 +334,7 @@ static const char *verify_required_directory(const char *dir) {
     return dir;
 }
 
-int main(int argc, char **argv)
-{
+int main(int argc, char **argv) {
     char *hostname = "localhost";
     int i, check_config = 0;
     int config_loaded = 0;
@@ -436,8 +435,7 @@ int main(int argc, char **argv)
                     config_set("global", "run as user", optarg);
                     break;
                 case 'v':
-                    // TODO: Outsource version to makefile which can compute version from git.
-                    printf("netdata %s\n", VERSION);
+                    printf("%s %s\n", program_name, program_version);
                     return 0;
                 case 'W':
                     {
@@ -510,6 +508,16 @@ int main(int argc, char **argv)
         }
     }
 
+#ifdef _SC_OPEN_MAX
+    // close all open file descriptors, except the standard ones
+    // the caller may have left open files (lxc-attach has this issue)
+    {
+        int fd;
+        for(fd = (int) (sysconf(_SC_OPEN_MAX) - 1); fd > 2; fd--)
+            if(fd_is_valid(fd)) close(fd);
+    }
+#endif
+
     if(!config_loaded)
         load_config(NULL, 0);
 
@@ -524,18 +532,26 @@ int main(int argc, char **argv)
             mallopt(M_ARENA_MAX, 1);
 #endif
 
-        char *config_dir = config_get("global", "config directory", CONFIG_DIR);
-
         // prepare configuration environment variables for the plugins
-        setenv("NETDATA_CONFIG_DIR" , verify_required_directory(config_dir) , 1);
-        setenv("NETDATA_PLUGINS_DIR", verify_required_directory(config_get("global", "plugins directory"  , PLUGINS_DIR)), 1);
-        setenv("NETDATA_WEB_DIR"    , verify_required_directory(config_get("global", "web files directory", WEB_DIR))    , 1);
-        setenv("NETDATA_CACHE_DIR"  , verify_required_directory(config_get("global", "cache directory"    , CACHE_DIR))  , 1);
-        setenv("NETDATA_LIB_DIR"    , verify_required_directory(config_get("global", "lib directory"      , VARLIB_DIR)) , 1);
-        setenv("NETDATA_LOG_DIR"    , verify_required_directory(config_get("global", "log directory"      , LOG_DIR))    , 1);
 
-        setenv("NETDATA_HOST_PREFIX", config_get("global", "host access prefix" , "")         , 1);
-        setenv("HOME"               , config_get("global", "home directory"     , CACHE_DIR)  , 1);
+        netdata_configured_config_dir  = config_get("global", "config directory",    CONFIG_DIR);
+        netdata_configured_log_dir     = config_get("global", "log directory",       LOG_DIR);
+        netdata_configured_plugins_dir = config_get("global", "plugins directory",   PLUGINS_DIR);
+        netdata_configured_web_dir     = config_get("global", "web files directory", WEB_DIR);
+        netdata_configured_cache_dir   = config_get("global", "cache directory",     CACHE_DIR);
+        netdata_configured_varlib_dir  = config_get("global", "lib directory",       VARLIB_DIR);
+        netdata_configured_home_dir    = config_get("global", "home directory",      CACHE_DIR);
+
+        setenv("NETDATA_CONFIG_DIR" , verify_required_directory(netdata_configured_config_dir),  1);
+        setenv("NETDATA_PLUGINS_DIR", verify_required_directory(netdata_configured_plugins_dir), 1);
+        setenv("NETDATA_WEB_DIR"    , verify_required_directory(netdata_configured_web_dir),     1);
+        setenv("NETDATA_CACHE_DIR"  , verify_required_directory(netdata_configured_cache_dir),   1);
+        setenv("NETDATA_LIB_DIR"    , verify_required_directory(netdata_configured_varlib_dir),  1);
+        setenv("NETDATA_LOG_DIR"    , verify_required_directory(netdata_configured_log_dir),     1);
+        setenv("HOME"               , verify_required_directory(netdata_configured_home_dir),    1);
+
+        netdata_configured_host_prefix = config_get("global", "host access prefix", "");
+        setenv("NETDATA_HOST_PREFIX", netdata_configured_host_prefix, 1);
 
         // disable buffering for python plugins
         setenv("PYTHONUNBUFFERED", "1", 1);
@@ -547,13 +563,17 @@ int main(int argc, char **argv)
         // work while we are cd into config_dir
         // to allow the plugins refer to their config
         // files using relative filenames
-        if(chdir(config_dir) == -1)
-            fatal("Cannot cd to '%s'", config_dir);
+        if(chdir(netdata_configured_config_dir) == -1)
+            fatal("Cannot cd to '%s'", netdata_configured_config_dir);
 
         char path[1024 + 1], *p = getenv("PATH");
         if(!p) p = "/bin:/usr/bin";
         snprintfz(path, 1024, "%s:%s", p, "/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
         setenv("PATH", config_get("plugins", "PATH environment variable", path), 1);
+
+        p = getenv("PYTHONPATH");
+        if(!p) p = "";
+        setenv("PYTHONPATH", config_get("plugins", "PYTHONPATH environment variable", p), 1);
     }
 
     char *user = NULL;
@@ -569,9 +589,9 @@ int main(int argc, char **argv)
             if(setrlimit(RLIMIT_CORE, &rl) != 0)
                 error("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
 
-#if !(defined(__FreeBSD__) || defined(__APPLE__))
+#ifdef HAVE_SYS_PRCTL_H
             prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
-#endif /* __FreeBSD__ || __APPLE__*/
+#endif
         }
 
         // --------------------------------------------------------------------
@@ -584,18 +604,23 @@ int main(int argc, char **argv)
 
         // --------------------------------------------------------------------
 
-        global_host_prefix = config_get("global", "host access prefix", "");
-        setenv("NETDATA_HOST_PREFIX", global_host_prefix, 1);
-
         get_system_HZ();
         get_system_cpus();
         get_system_pid_max();
         
         // --------------------------------------------------------------------
 
-        stdout_filename    = config_get("global", "debug log",  LOG_DIR "/debug.log");
-        stderr_filename    = config_get("global", "error log",  LOG_DIR "/error.log");
-        stdaccess_filename = config_get("global", "access log", LOG_DIR "/access.log");
+        {
+            char filename[FILENAME_MAX + 1];
+            snprintfz(filename, FILENAME_MAX, "%s/debug.log", netdata_configured_log_dir);
+            stdout_filename    = config_get("global", "debug log",  filename);
+
+            snprintfz(filename, FILENAME_MAX, "%s/error.log", netdata_configured_log_dir);
+            stderr_filename    = config_get("global", "error log",  filename);
+
+            snprintfz(filename, FILENAME_MAX, "%s/access.log", netdata_configured_log_dir);
+            stdaccess_filename = config_get("global", "access log", filename);
+        }
 
         error_log_throttle_period_backup =
             error_log_throttle_period = config_get_number("global", "errors flood protection period", error_log_throttle_period);
@@ -746,9 +771,9 @@ int main(int argc, char **argv)
         struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
         if(setrlimit(RLIMIT_CORE, &rl) != 0)
             error("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
-#if !(defined(__FreeBSD__) || defined(__APPLE__))
+#ifdef HAVE_SYS_PRCTL_H
         prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
-#endif /* __FreeBSD__ || __APPLE__*/
+#endif
     }
 #endif /* NETDATA_INTERNAL_CHECKS */