]> 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 fdedb08129ce9e0a3253e0be1390cc7532e07498..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,21 +71,23 @@ 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);
 
     char *s = config_get("global", "web compression strategy", "default");
-    if(!strsame(s, "default"))
+    if(!strcmp(s, "default"))
         web_gzip_strategy = Z_DEFAULT_STRATEGY;
-    else if(!strsame(s, "filtered"))
+    else if(!strcmp(s, "filtered"))
         web_gzip_strategy = Z_FILTERED;
-    else if(!strsame(s, "huffman only"))
+    else if(!strcmp(s, "huffman only"))
         web_gzip_strategy = Z_HUFFMAN_ONLY;
-    else if(!strsame(s, "rle"))
+    else if(!strcmp(s, "rle"))
         web_gzip_strategy = Z_RLE;
-    else if(!strsame(s, "fixed"))
+    else if(!strcmp(s, "fixed"))
         web_gzip_strategy = Z_FIXED;
     else {
         error("Invalid compression strategy '%s'. Valid strategies are 'default', 'filtered', 'huffman only', 'rle' and 'fixed'. Proceeding with 'default'.", s);
@@ -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;
@@ -351,22 +350,22 @@ int main(int argc, char **argv)
     {
         i = 1;
         while(i < argc) {
-            if(strsame(argv[i], "-pidfile") == 0 && (i+1) < argc) {
+            if(strcmp(argv[i], "-pidfile") == 0 && (i+1) < argc) {
                 strncpyz(pidfile, argv[i+1], FILENAME_MAX);
                 fprintf(stderr, "%s: deprecated option -- %s -- please use -P instead.\n", argv[0], argv[i]);
                 remove_option(i, &argc, argv);
             }
-            else if(strsame(argv[i], "-nodaemon") == 0 || strsame(argv[i], "-nd") == 0) {
+            else if(strcmp(argv[i], "-nodaemon") == 0 || strcmp(argv[i], "-nd") == 0) {
                 dont_fork = 1;
                 fprintf(stderr, "%s: deprecated option -- %s -- please use -D instead.\n ", argv[0], argv[i]);
                 remove_option(i, &argc, argv);
             }
-            else if(strsame(argv[i], "-ch") == 0 && (i+1) < argc) {
+            else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) {
                 config_set("global", "host access prefix", argv[i+1]);
                 fprintf(stderr, "%s: deprecated option -- %s -- please use -s instead.\n", argv[0], argv[i]);
                 remove_option(i, &argc, argv);
             }
-            else if(strsame(argv[i], "-l") == 0 && (i+1) < argc) {
+            else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) {
                 config_set("global", "history", argv[i+1]);
                 fprintf(stderr, "%s: deprecated option -- %s -- This option will be removed with V2.*.\n", argv[0], argv[i]);
                 remove_option(i, &argc, argv);
@@ -436,21 +435,20 @@ 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':
                     {
                         char* stacksize_string = "stacksize=";
                         char* debug_flags_string = "debug_flags=";
-                        if(strsame(optarg, "unittest") == 0) {
+                        if(strcmp(optarg, "unittest") == 0) {
                             rrd_update_every = 1;
                             if(run_all_mockup_tests()) exit(1);
                             if(unit_test_storage()) exit(1);
                             fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
                             exit(0);
                         }
-                        else if(strsame(optarg, "simple-pattern") == 0) {
+                        else if(strcmp(optarg, "simple-pattern") == 0) {
                             if(optind + 2 > argc) {
                                 fprintf(stderr, "%s", "\nUSAGE: -W simple-pattern 'pattern' 'string'\n\n"
                                         " Checks if 'pattern' matches the given 'string'.\n"
@@ -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 */