]> arthur.barton.de Git - netdata.git/blob - src/main.c
Merge remote-tracking branch 'upstream/master' into health
[netdata.git] / src / main.c
1 #include "common.h"
2
3 extern void *cgroups_main(void *ptr);
4
5 void netdata_cleanup_and_exit(int ret) {
6     netdata_exit = 1;
7
8     error_log_limit_unlimited();
9
10     info("Called: netdata_cleanup_and_exit()");
11 #ifdef NETDATA_INTERNAL_CHECKS
12     rrdset_free_all();
13 #else
14     rrdset_save_all();
15 #endif
16     // kill_childs();
17
18     if(pidfile[0]) {
19         if(unlink(pidfile) != 0)
20             error("Cannot unlink pidfile '%s'.", pidfile);
21     }
22
23     info("NetData exiting. Bye bye...");
24     exit(ret);
25 }
26
27 struct netdata_static_thread {
28     char *name;
29
30     char *config_section;
31     char *config_name;
32
33     int enabled;
34
35     pthread_t *thread;
36
37     void (*init_routine) (void);
38     void *(*start_routine) (void *);
39 } static_threads[] = {
40 #ifdef INTERNAL_PLUGIN_NFACCT
41 // nfacct requires root access
42     // so, we build it as an external plugin with setuid to root
43     {"nfacct",              "plugins",  "nfacct",     1, NULL, NULL, nfacct_main},
44 #endif
45
46     {"tc",                 "plugins",   "tc",         1, NULL, NULL, tc_main},
47     {"idlejitter",         "plugins",   "idlejitter", 1, NULL, NULL, cpuidlejitter_main},
48     {"proc",               "plugins",   "proc",       1, NULL, NULL, proc_main},
49     {"cgroups",            "plugins",   "cgroups",    1, NULL, NULL, cgroups_main},
50     {"check",              "plugins",   "checks",     0, NULL, NULL, checks_main},
51     {"health",              NULL,       NULL,         1, NULL, NULL, health_main},
52     {"plugins.d",           NULL,       NULL,         1, NULL, NULL, pluginsd_main},
53     {"web",                 NULL,       NULL,         1, NULL, NULL, socket_listen_main_multi_threaded},
54     {"web-single-threaded", NULL,       NULL,         0, NULL, NULL, socket_listen_main_single_threaded},
55     {NULL,                  NULL,       NULL,         0, NULL, NULL, NULL}
56 };
57
58 void web_server_threading_selection(void) {
59     int threaded = config_get_boolean("global", "multi threaded web server", 1);
60
61     int i;
62     for(i = 0; static_threads[i].name ; i++) {
63         if(static_threads[i].start_routine == socket_listen_main_multi_threaded)
64             static_threads[i].enabled = threaded?1:0;
65
66         if(static_threads[i].start_routine == socket_listen_main_single_threaded)
67             static_threads[i].enabled = threaded?0:1;
68     }
69
70     web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
71
72     web_donotrack_comply = config_get_boolean("global", "respect web browser do not track policy", web_donotrack_comply);
73
74 #ifdef NETDATA_WITH_ZLIB
75     web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
76
77     char *s = config_get("global", "web compression strategy", "default");
78     if(!strcmp(s, "default"))
79         web_gzip_strategy = Z_DEFAULT_STRATEGY;
80     else if(!strcmp(s, "filtered"))
81         web_gzip_strategy = Z_FILTERED;
82     else if(!strcmp(s, "huffman only"))
83         web_gzip_strategy = Z_HUFFMAN_ONLY;
84     else if(!strcmp(s, "rle"))
85         web_gzip_strategy = Z_RLE;
86     else if(!strcmp(s, "fixed"))
87         web_gzip_strategy = Z_FIXED;
88     else {
89         error("Invalid compression strategy '%s'. Valid strategies are 'default', 'filtered', 'huffman only', 'rle' and 'fixed'. Proceeding with 'default'.", s);
90         web_gzip_strategy = Z_DEFAULT_STRATEGY;
91     }
92
93     web_gzip_level = (int)config_get_number("global", "web compression level", 3);
94     if(web_gzip_level < 1) {
95         error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 1 (fastest compression).", web_gzip_level);
96         web_gzip_level = 1;
97     }
98     else if(web_gzip_level > 9) {
99         error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 9 (best compression).", web_gzip_level);
100         web_gzip_level = 9;
101     }
102 #endif /* NETDATA_WITH_ZLIB */
103 }
104
105
106 int killpid(pid_t pid, int sig)
107 {
108     int ret = -1;
109     debug(D_EXIT, "Request to kill pid %d", pid);
110
111     errno = 0;
112     if(kill(pid, 0) == -1) {
113         switch(errno) {
114             case ESRCH:
115                 error("Request to kill pid %d, but it is not running.", pid);
116                 break;
117
118             case EPERM:
119                 error("Request to kill pid %d, but I do not have enough permissions.", pid);
120                 break;
121
122             default:
123                 error("Request to kill pid %d, but I received an error.", pid);
124                 break;
125         }
126     }
127     else {
128         errno = 0;
129         ret = kill(pid, sig);
130         if(ret == -1) {
131             switch(errno) {
132                 case ESRCH:
133                     error("Cannot kill pid %d, but it is not running.", pid);
134                     break;
135
136                 case EPERM:
137                     error("Cannot kill pid %d, but I do not have enough permissions.", pid);
138                     break;
139
140                 default:
141                     error("Cannot kill pid %d, but I received an error.", pid);
142                     break;
143             }
144         }
145     }
146
147     return ret;
148 }
149
150 void kill_childs()
151 {
152     siginfo_t info;
153
154     struct web_client *w;
155     for(w = web_clients; w ; w = w->next) {
156         debug(D_EXIT, "Stopping web client %s", w->client_ip);
157         pthread_cancel(w->thread);
158         pthread_join(w->thread, NULL);
159     }
160
161     int i;
162     for (i = 0; static_threads[i].name != NULL ; i++) {
163         if(static_threads[i].thread) {
164             debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
165             pthread_cancel(*static_threads[i].thread);
166             pthread_join(*static_threads[i].thread, NULL);
167             static_threads[i].thread = NULL;
168         }
169     }
170
171     if(tc_child_pid) {
172         info("Killing tc-qos-helper procees");
173         if(killpid(tc_child_pid, SIGTERM) != -1)
174             waitid(P_PID, (id_t) tc_child_pid, &info, WEXITED);
175     }
176     tc_child_pid = 0;
177
178     struct plugind *cd;
179     for(cd = pluginsd_root ; cd ; cd = cd->next) {
180         debug(D_EXIT, "Stopping %s plugin thread", cd->id);
181         pthread_cancel(cd->thread);
182         pthread_join(cd->thread, NULL);
183
184         if(cd->pid && !cd->obsolete) {
185             debug(D_EXIT, "killing %s plugin process", cd->id);
186             if(killpid(cd->pid, SIGTERM) != -1)
187                 waitid(P_PID, (id_t) cd->pid, &info, WEXITED);
188         }
189     }
190
191     // if, for any reason there is any child exited
192     // catch it here
193     waitid(P_PID, 0, &info, WEXITED|WNOHANG);
194
195     debug(D_EXIT, "All threads/childs stopped.");
196 }
197
198 struct option_def options[] = {
199     // opt description                                                       arg name                     default value
200     {'c', "Load alternate configuration file",                               "config_file",                          CONFIG_DIR "/" CONFIG_FILENAME},
201     {'D', "Disable fork into background",                                    NULL,                                   NULL},
202     {'h', "Display help message",                                            NULL,                                   NULL},
203     {'P', "File to save a pid while running",                                "FILE",                                 NULL},
204     {'i', "The IP address to listen to.",                                    "address",                              "All addresses"},
205     {'k', "Check daemon configuration.",                                     NULL,                                   NULL},
206     {'p', "Port to listen. Can be from 1 to 65535.",                         "port_number",                          "19999"},
207     {'s', "Path to access host /proc and /sys when running in a container.", "PATH",                                 NULL},
208     {'t', "The frequency in seconds, for data collection. \
209 Same as 'update every' config file option.",                                 "seconds",                              "1"},
210     {'u', "System username to run as.",                                      "username",                             "netdata"},
211     {'v', "Version of the program",                                          NULL,                                   NULL},
212     {'W', "vendor options.",                                                 "stacksize=<size>|unittest|debug_flag", NULL},
213 };
214
215 void help(int exitcode) {
216     FILE *stream;
217     if(exitcode == 0)
218         stream = stdout;
219     else
220         stream = stderr;
221
222     int num_opts = sizeof(options) / sizeof(struct option_def);
223     int i;
224     int max_len_arg = 0;
225
226     // Compute maximum argument length
227     for( i = 0; i < num_opts; i++ ) {
228         if(options[i].arg_name) {
229             int len_arg = (int)strlen(options[i].arg_name);
230             if(len_arg > max_len_arg) max_len_arg = len_arg;
231         }
232     }
233
234     fprintf(stream, "SYNOPSIS: netdata [options]\n");
235     fprintf(stream, "\n");
236     fprintf(stream, "Options:\n");
237
238     // Output options description.
239     for( i = 0; i < num_opts; i++ ) {
240         fprintf(stream, "  -%c %-*s  %s", options[i].val, max_len_arg, options[i].arg_name ? options[i].arg_name : "", options[i].description);
241         if(options[i].default_value) {
242             fprintf(stream, " Default: %s\n", options[i].default_value);
243         } else {
244             fprintf(stream, "\n");
245         }
246     }
247
248     fflush(stream);
249     exit(exitcode);
250 }
251
252 // TODO: Remove this function with the nix major release.
253 void remove_option(int opt_index, int *argc, char **argv) {
254     int i = opt_index;
255     // remove the options.
256     do {
257         *argc = *argc - 1;
258         for(i = opt_index; i < *argc; i++) {
259             argv[i] = argv[i+1];
260         }
261         i = opt_index;
262     } while(argv[i][0] != '-' && opt_index >= *argc);
263 }
264
265
266 int main(int argc, char **argv)
267 {
268     int i, check_config = 0;
269     int config_loaded = 0;
270     int dont_fork = 0;
271     size_t wanted_stacksize = 0, stacksize = 0;
272     pthread_attr_t attr;
273
274     // global initialization
275     get_HZ();
276
277     // set the name for logging
278     program_name = "netdata";
279
280     // parse command line.
281
282     // parse depercated options
283     // TODO: Remove this block with the next major release.
284     {
285         i = 1;
286         while(i < argc) {
287             if(strcmp(argv[i], "-pidfile") == 0 && (i+1) < argc) {
288                 strncpyz(pidfile, argv[i+1], FILENAME_MAX);
289                 fprintf(stderr, "%s: deprecated option -- %s -- please use -P instead.\n", argv[0], argv[i]);
290                 remove_option(i, &argc, argv);
291             }
292             else if(strcmp(argv[i], "-nodaemon") == 0 || strcmp(argv[i], "-nd") == 0) {
293                 dont_fork = 1;
294                 fprintf(stderr, "%s: deprecated option -- %s -- please use -D instead.\n ", argv[0], argv[i]);
295                 remove_option(i, &argc, argv);
296             }
297             else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) {
298                 config_set("global", "host access prefix", argv[i+1]);
299                 fprintf(stderr, "%s: deprecated option -- %s -- please use -s instead.\n", argv[0], argv[i]);
300                 remove_option(i, &argc, argv);
301             }
302             else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) {
303                 config_set("global", "history", argv[i+1]);
304                 fprintf(stderr, "%s: deprecated option -- %s -- This option will be removed with V2.*.\n", argv[0], argv[i]);
305                 remove_option(i, &argc, argv);
306             }
307             else i++;
308         }
309     }
310
311     // parse options
312     {
313         int num_opts = sizeof(options) / sizeof(struct option_def);
314         char optstring[(num_opts * 2) + 1];
315
316         int string_i = 0;
317         for( i = 0; i < num_opts; i++ ) {
318             optstring[string_i] = options[i].val;
319             string_i++;
320             if(options[i].arg_name) {
321                 optstring[string_i] = ':';
322                 string_i++;
323             }
324         }
325
326         int opt;
327         while( (opt = getopt(argc, argv, optstring)) != -1 ) {
328             switch(opt) {
329                 case 'c':
330                     if(load_config(optarg, 1) != 1) {
331                         error("Cannot load configuration file %s.", optarg);
332                         exit(1);
333                     }
334                     else {
335                         debug(D_OPTIONS, "Configuration loaded from %s.", optarg);
336                         config_loaded = 1;
337                     }
338                     break;
339                 case 'D':
340                     dont_fork = 1;
341                     break;
342                 case 'h':
343                     help(0);
344                     break;
345                 case 'i':
346                     config_set("global", "bind to", optarg);
347                     break;
348                 case 'k':
349                     dont_fork = 1;
350                     check_config = 1;
351                     break;
352                 case 'P':
353                     strncpy(pidfile, optarg, FILENAME_MAX);
354                     pidfile[FILENAME_MAX] = '\0';
355                     break;
356                 case 'p':
357                     config_set("global", "default port", optarg);
358                     break;
359                 case 's':
360                     config_set("global", "host access prefix", optarg);
361                     break;
362                 case 't':
363                     config_set("global", "update every", optarg);
364                     break;
365                 case 'u':
366                     config_set("global", "run as user", optarg);
367                     break;
368                 case 'v':
369                     // TODO: Outsource version to makefile which can compute version from git.
370                     printf("netdata 1.2.1_master\n");
371                     return 0;
372                 case 'W':
373                     {
374                         char* stacksize_string = "stacksize=";
375                         char* debug_flags_string = "debug_flags=";
376                         if(strcmp(optarg, "unittest") == 0) {
377                             rrd_update_every = 1;
378                             if(run_all_mockup_tests()) exit(1);
379                             if(unit_test_storage()) exit(1);
380                             fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
381                             exit(0);
382                         } else if(strncmp(optarg, stacksize_string, strlen(stacksize_string)) == 0) {
383                             optarg += strlen(stacksize_string);
384                             config_set("global", "pthread stack size", optarg);
385                         } else if(strncmp(optarg, debug_flags_string, strlen(debug_flags_string)) == 0) {
386                             optarg += strlen(debug_flags_string);
387                             config_set("global", "debug flags",  optarg);
388                             debug_flags = strtoull(optarg, NULL, 0);
389                         }
390                     }
391                     break;
392                 default: /* ? */
393                     help(1);
394                     break;
395             }
396         }
397     }
398
399     if(!config_loaded) load_config(NULL, 0);
400
401     // prepare configuration environment variables for the plugins
402     setenv("NETDATA_CONFIG_DIR" , config_get("global", "config directory"   , CONFIG_DIR) , 1);
403     setenv("NETDATA_PLUGINS_DIR", config_get("global", "plugins directory"  , PLUGINS_DIR), 1);
404     setenv("NETDATA_WEB_DIR"    , config_get("global", "web files directory", WEB_DIR)    , 1);
405     setenv("NETDATA_CACHE_DIR"  , config_get("global", "cache directory"    , CACHE_DIR)  , 1);
406     setenv("NETDATA_LIB_DIR"    , config_get("global", "lib directory"      , VARLIB_DIR) , 1);
407     setenv("NETDATA_LOG_DIR"    , config_get("global", "log directory"      , LOG_DIR)    , 1);
408     setenv("NETDATA_HOST_PREFIX", config_get("global", "host access prefix" , "")         , 1);
409     setenv("HOME"               , config_get("global", "home directory"     , CACHE_DIR)  , 1);
410
411     // disable buffering for python plugins
412     setenv("PYTHONUNBUFFERED", "1", 1);
413
414     // avoid flood calls to stat(/etc/localtime)
415     // http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
416     setenv("TZ", ":/etc/localtime", 0);
417
418     {
419         char path[1024 + 1], *p = getenv("PATH");
420         if(!p) p = "/bin:/usr/bin";
421         snprintfz(path, 1024, "%s:%s", p, "/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
422         setenv("PATH", config_get("plugins", "PATH environment variable", path), 1);
423     }
424
425     // cd to /tmp to avoid any plugins writing files at random places
426     if(chdir("/tmp")) error("netdata: ERROR: Cannot cd to /tmp");
427
428     char *user = NULL;
429     {
430         char *flags = config_get("global", "debug flags",  "0x00000000");
431         setenv("NETDATA_DEBUG_FLAGS", flags, 1);
432
433         debug_flags = strtoull(flags, NULL, 0);
434         debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
435
436         if(debug_flags != 0) {
437             struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
438             if(setrlimit(RLIMIT_CORE, &rl) != 0)
439                 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
440             prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
441         }
442
443         // --------------------------------------------------------------------
444
445 #ifdef MADV_MERGEABLE
446         enable_ksm = config_get_boolean("global", "memory deduplication (ksm)", enable_ksm);
447 #else
448 #warning "Kernel memory deduplication (KSM) is not available"
449 #endif
450
451         // --------------------------------------------------------------------
452
453         global_host_prefix = config_get("global", "host access prefix", "");
454         setenv("NETDATA_HOST_PREFIX", global_host_prefix, 1);
455
456         // --------------------------------------------------------------------
457
458         stdout_filename    = config_get("global", "debug log",  LOG_DIR "/debug.log");
459         stderr_filename    = config_get("global", "error log",  LOG_DIR "/error.log");
460         stdaccess_filename = config_get("global", "access log", LOG_DIR "/access.log");
461
462         error_log_throttle_period = config_get_number("global", "errors flood protection period", error_log_throttle_period);
463         setenv("NETDATA_ERRORS_THROTTLE_PERIOD", config_get("global", "errors flood protection period"    , ""), 1);
464
465         error_log_errors_per_period = (unsigned long)config_get_number("global", "errors to trigger flood protection", error_log_errors_per_period);
466         setenv("NETDATA_ERRORS_PER_PERIOD"     , config_get("global", "errors to trigger flood protection", ""), 1);
467
468         if(check_config) {
469             stdout_filename = stderr_filename = stdaccess_filename = "system";
470             error_log_throttle_period = 0;
471             error_log_errors_per_period = 0;
472         }
473
474         // --------------------------------------------------------------------
475
476         rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
477
478         // --------------------------------------------------------------------
479
480         {
481             char hostnamebuf[HOSTNAME_MAX + 1];
482             if(gethostname(hostnamebuf, HOSTNAME_MAX) == -1)
483                 error("WARNING: Cannot get machine hostname.");
484             hostname = config_get("global", "hostname", hostnamebuf);
485             debug(D_OPTIONS, "hostname set to '%s'", hostname);
486         }
487
488         // --------------------------------------------------------------------
489
490         rrd_default_history_entries = (int) config_get_number("global", "history", RRD_DEFAULT_HISTORY_ENTRIES);
491         if(rrd_default_history_entries < 5 || rrd_default_history_entries > RRD_HISTORY_ENTRIES_MAX) {
492             info("Invalid save lines %d given. Defaulting to %d.", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
493             rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
494         }
495         else {
496             debug(D_OPTIONS, "save lines set to %d.", rrd_default_history_entries);
497         }
498
499         // --------------------------------------------------------------------
500
501         rrd_update_every = (int) config_get_number("global", "update every", UPDATE_EVERY);
502         if(rrd_update_every < 1 || rrd_update_every > 600) {
503             info("Invalid update timer %d given. Defaulting to %d.", rrd_update_every, UPDATE_EVERY_MAX);
504             rrd_update_every = UPDATE_EVERY;
505         }
506         else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
507
508         // let the plugins know the min update_every
509         {
510             char buf[16];
511             snprintfz(buf, 15, "%d", rrd_update_every);
512             setenv("NETDATA_UPDATE_EVERY", buf, 1);
513         }
514
515         // --------------------------------------------------------------------
516
517         // block signals while initializing threads.
518         // this causes the threads to block signals.
519         sigset_t sigset;
520         sigfillset(&sigset);
521
522         if(pthread_sigmask(SIG_BLOCK, &sigset, NULL) == -1) {
523             error("Could not block signals for threads");
524         }
525
526         // Catch signals which we want to use
527         struct sigaction sa;
528         sigemptyset(&sa.sa_mask);
529         sigaddset(&sa.sa_mask, SIGINT);
530         sigaddset(&sa.sa_mask, SIGTERM);
531         sa.sa_handler = sig_handler_exit;
532         sa.sa_flags = 0;
533         if(sigaction(SIGINT, &sa, NULL) == -1) {
534             error("Failed to change signal handler for SIGINT");
535         }
536         if(sigaction(SIGTERM, &sa, NULL) == -1) {
537             error("Failed to change signal handler for SIGTERM");
538         }
539
540         sigemptyset(&sa.sa_mask);
541         sigaddset(&sa.sa_mask, SIGHUP);
542         sa.sa_handler = sig_handler_logrotate;
543         sa.sa_flags = 0;
544         if(sigaction(SIGHUP, &sa, NULL) == -1) {
545             error("Failed to change signal handler for SIGHUP");
546         }
547
548         // save database on SIGUSR1
549         sigemptyset(&sa.sa_mask);
550         sigaddset(&sa.sa_mask, SIGUSR1);
551         sa.sa_handler = sig_handler_save;
552         if(sigaction(SIGUSR1, &sa, NULL) == -1) {
553             error("Failed to change signal handler for SIGUSR1");
554         }
555
556         // Ignore SIGPIPE completely.
557         // INFO: If we add signals here we have to unblock them
558         // at popen.c when running a external plugin.
559         sigemptyset(&sa.sa_mask);
560         sigaddset(&sa.sa_mask, SIGPIPE);
561         sa.sa_handler = SIG_IGN;
562         if(sigaction(SIGPIPE, &sa, NULL) == -1) {
563             error("Failed to change signal handler for SIGPIPE");
564         }
565
566         // --------------------------------------------------------------------
567
568         i = pthread_attr_init(&attr);
569         if(i != 0)
570             fatal("pthread_attr_init() failed with code %d.", i);
571
572         i = pthread_attr_getstacksize(&attr, &stacksize);
573         if(i != 0)
574             fatal("pthread_attr_getstacksize() failed with code %d.", i);
575         else
576             debug(D_OPTIONS, "initial pthread stack size is %zu bytes", stacksize);
577
578         wanted_stacksize = (size_t)config_get_number("global", "pthread stack size", (long)stacksize);
579
580         // --------------------------------------------------------------------
581
582         for (i = 0; static_threads[i].name != NULL ; i++) {
583             struct netdata_static_thread *st = &static_threads[i];
584
585             if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
586             if(st->enabled && st->init_routine) st->init_routine();
587         }
588
589         // --------------------------------------------------------------------
590
591         // get the user we should run
592         // IMPORTANT: this is required before web_files_uid()
593         user = config_get("global", "run as user"    , (getuid() == 0)?NETDATA_USER:"");
594
595         // IMPORTANT: these have to run once, while single threaded
596         web_files_uid(); // IMPORTANT: web_files_uid() before web_files_gid()
597         web_files_gid();
598
599         // --------------------------------------------------------------------
600
601         if(!check_config)
602             create_listen_sockets();
603     }
604
605     // initialize the log files
606     open_all_log_files();
607
608 #ifdef NETDATA_INTERNAL_CHECKS
609     if(debug_flags != 0) {
610         struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
611         if(setrlimit(RLIMIT_CORE, &rl) != 0)
612             info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
613         prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
614     }
615 #endif /* NETDATA_INTERNAL_CHECKS */
616
617     // fork, switch user, create pid file, set process priority
618     if(become_daemon(dont_fork, user) == -1)
619         fatal("Cannot demonize myself.");
620
621     info("NetData started on pid %d", getpid());
622
623
624     // ------------------------------------------------------------------------
625     // get default pthread stack size
626
627     if(stacksize < wanted_stacksize) {
628         i = pthread_attr_setstacksize(&attr, wanted_stacksize);
629         if(i != 0)
630             fatal("pthread_attr_setstacksize() to %zu bytes, failed with code %d.", wanted_stacksize, i);
631         else
632             info("Successfully set pthread stacksize to %zu bytes", wanted_stacksize);
633     }
634
635     // ------------------------------------------------------------------------
636     // initialize the registry
637
638     registry_init();
639
640     // ------------------------------------------------------------------------
641     // initialize health monitoring
642
643     health_init();
644
645     if(check_config)
646         exit(1);
647
648     // ------------------------------------------------------------------------
649     // spawn the threads
650
651     web_server_threading_selection();
652
653     for (i = 0; static_threads[i].name != NULL ; i++) {
654         struct netdata_static_thread *st = &static_threads[i];
655
656         if(st->enabled) {
657             st->thread = mallocz(sizeof(pthread_t));
658
659             info("Starting thread %s.", st->name);
660
661             if(pthread_create(st->thread, &attr, st->start_routine, NULL))
662                 error("failed to create new thread for %s.", st->name);
663
664             else if(pthread_detach(*st->thread))
665                 error("Cannot request detach of newly created %s thread.", st->name);
666         }
667         else info("Not starting thread %s.", st->name);
668     }
669
670     // ------------------------------------------------------------------------
671     // block signals while initializing threads.
672     sigset_t sigset;
673     sigfillset(&sigset);
674
675     if(pthread_sigmask(SIG_UNBLOCK, &sigset, NULL) == -1) {
676         error("Could not unblock signals for threads");
677     }
678
679     // Handle flags set in the signal handler.
680     while(1) {
681         pause();
682         if(netdata_exit) {
683             info("Exit main loop of netdata.");
684             netdata_cleanup_and_exit(0);
685             exit(0);
686         }
687     }
688 }