Auto-Merge pull request #2474 from octo/maintainer-guide
[collectd.git] / src / daemon / collectd.c
1 /**
2  * collectd - src/collectd.c
3  * Copyright (C) 2005-2007  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  *   Alvaro Barcellos <alvaro.barcellos at gmail.com>
26  **/
27
28 #include "collectd.h"
29
30 #include "common.h"
31 #include "configfile.h"
32 #include "plugin.h"
33
34 #include <netdb.h>
35 #include <sys/types.h>
36 #include <sys/un.h>
37
38 #if HAVE_LOCALE_H
39 #include <locale.h>
40 #endif
41
42 #if HAVE_STATGRAB_H
43 #include <statgrab.h>
44 #endif
45
46 #ifndef COLLECTD_LOCALE
47 #define COLLECTD_LOCALE "C"
48 #endif
49
50 static int loop = 0;
51
52 static void *do_flush(void __attribute__((unused)) * arg) {
53   INFO("Flushing all data.");
54   plugin_flush(/* plugin = */ NULL,
55                /* timeout = */ 0,
56                /* ident = */ NULL);
57   INFO("Finished flushing all data.");
58   pthread_exit(NULL);
59   return NULL;
60 }
61
62 static void sig_int_handler(int __attribute__((unused)) signal) { loop++; }
63
64 static void sig_term_handler(int __attribute__((unused)) signal) { loop++; }
65
66 static void sig_usr1_handler(int __attribute__((unused)) signal) {
67   pthread_t thread;
68   pthread_attr_t attr;
69
70   /* flushing the data might take a while,
71    * so it should be done asynchronously */
72   pthread_attr_init(&attr);
73   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
74   pthread_create(&thread, &attr, do_flush, NULL);
75   pthread_attr_destroy(&attr);
76 }
77
78 static int init_hostname(void) {
79   const char *str;
80
81   struct addrinfo *ai_list;
82   int status;
83
84   long hostname_len = sysconf(_SC_HOST_NAME_MAX);
85   if (hostname_len == -1) {
86     hostname_len = NI_MAXHOST;
87   }
88   char hostname[hostname_len];
89
90   str = global_option_get("Hostname");
91   if ((str != NULL) && (str[0] != 0)) {
92     hostname_set(str);
93     return 0;
94   }
95
96   if (gethostname(hostname, hostname_len) != 0) {
97     fprintf(stderr, "`gethostname' failed and no "
98                     "hostname was configured.\n");
99     return -1;
100   }
101
102   str = global_option_get("FQDNLookup");
103   if (IS_FALSE(str))
104     return 0;
105
106   struct addrinfo ai_hints = {.ai_flags = AI_CANONNAME};
107
108   status = getaddrinfo(hostname, NULL, &ai_hints, &ai_list);
109   if (status != 0) {
110     ERROR("Looking up \"%s\" failed. You have set the "
111           "\"FQDNLookup\" option, but I cannot resolve "
112           "my hostname to a fully qualified domain "
113           "name. Please fix the network "
114           "configuration.",
115           hostname);
116     return -1;
117   }
118
119   for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
120        ai_ptr = ai_ptr->ai_next) {
121     if (ai_ptr->ai_canonname == NULL)
122       continue;
123
124     hostname_set(ai_ptr->ai_canonname);
125     break;
126   }
127
128   freeaddrinfo(ai_list);
129   return 0;
130 } /* int init_hostname */
131
132 static int init_global_variables(void) {
133   char const *str;
134
135   interval_g = cf_get_default_interval();
136   assert(interval_g > 0);
137   DEBUG("interval_g = %.3f;", CDTIME_T_TO_DOUBLE(interval_g));
138
139   str = global_option_get("Timeout");
140   if (str == NULL)
141     str = "2";
142   timeout_g = atoi(str);
143   if (timeout_g <= 1) {
144     fprintf(stderr, "Cannot set the timeout to a correct value.\n"
145                     "Please check your settings.\n");
146     return -1;
147   }
148   DEBUG("timeout_g = %i;", timeout_g);
149
150   if (init_hostname() != 0)
151     return -1;
152   DEBUG("hostname_g = %s;", hostname_g);
153
154   return 0;
155 } /* int init_global_variables */
156
157 static int change_basedir(const char *orig_dir, _Bool create) {
158   char *dir;
159   size_t dirlen;
160   int status;
161
162   dir = strdup(orig_dir);
163   if (dir == NULL) {
164     char errbuf[1024];
165     ERROR("strdup failed: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
166     return -1;
167   }
168
169   dirlen = strlen(dir);
170   while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
171     dir[--dirlen] = '\0';
172
173   if (dirlen == 0) {
174     free(dir);
175     return -1;
176   }
177
178   status = chdir(dir);
179   if (status == 0) {
180     free(dir);
181     return 0;
182   } else if (!create || (errno != ENOENT)) {
183     char errbuf[1024];
184     ERROR("change_basedir: chdir (%s): %s", dir,
185           sstrerror(errno, errbuf, sizeof(errbuf)));
186     free(dir);
187     return -1;
188   }
189
190   status = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
191   if (status != 0) {
192     char errbuf[1024];
193     ERROR("change_basedir: mkdir (%s): %s", dir,
194           sstrerror(errno, errbuf, sizeof(errbuf)));
195     free(dir);
196     return -1;
197   }
198
199   status = chdir(dir);
200   if (status != 0) {
201     char errbuf[1024];
202     ERROR("change_basedir: chdir (%s): %s", dir,
203           sstrerror(errno, errbuf, sizeof(errbuf)));
204     free(dir);
205     return -1;
206   }
207
208   free(dir);
209   return 0;
210 } /* static int change_basedir (char *dir) */
211
212 #if HAVE_LIBKSTAT
213 static void update_kstat(void) {
214   if (kc == NULL) {
215     if ((kc = kstat_open()) == NULL)
216       ERROR("Unable to open kstat control structure");
217   } else {
218     kid_t kid;
219     kid = kstat_chain_update(kc);
220     if (kid > 0) {
221       INFO("kstat chain has been updated");
222       plugin_init_all();
223     } else if (kid < 0)
224       ERROR("kstat chain update failed");
225     /* else: everything works as expected */
226   }
227
228   return;
229 } /* static void update_kstat (void) */
230 #endif /* HAVE_LIBKSTAT */
231
232 /* TODO
233  * Remove all settings but `-f' and `-C'
234  */
235 __attribute__((noreturn)) static void exit_usage(int status) {
236   printf("Usage: " PACKAGE_NAME " [OPTIONS]\n\n"
237
238          "Available options:\n"
239          "  General:\n"
240          "    -C <file>       Configuration file.\n"
241          "                    Default: " CONFIGFILE "\n"
242          "    -t              Test config and exit.\n"
243          "    -T              Test plugin read and exit.\n"
244          "    -P <file>       PID-file.\n"
245          "                    Default: " PIDFILE "\n"
246 #if COLLECT_DAEMON
247          "    -f              Don't fork to the background.\n"
248 #endif
249          "    -B              Don't create the BaseDir\n"
250          "    -h              Display help (this message)\n"
251          "\nBuiltin defaults:\n"
252          "  Config file       " CONFIGFILE "\n"
253          "  PID file          " PIDFILE "\n"
254          "  Plugin directory  " PLUGINDIR "\n"
255          "  Data directory    " PKGLOCALSTATEDIR "\n"
256          "\n" PACKAGE_NAME " " PACKAGE_VERSION ", http://collectd.org/\n"
257          "by Florian octo Forster <octo@collectd.org>\n"
258          "for contributions see `AUTHORS'\n");
259   exit(status);
260 } /* static void exit_usage (int status) */
261
262 static int do_init(void) {
263 #if HAVE_SETLOCALE
264   if (setlocale(LC_NUMERIC, COLLECTD_LOCALE) == NULL)
265     WARNING("setlocale (\"%s\") failed.", COLLECTD_LOCALE);
266
267   /* Update the environment, so that libraries that are calling
268    * setlocale(LC_NUMERIC, "") don't accidentally revert these changes. */
269   unsetenv("LC_ALL");
270   setenv("LC_NUMERIC", COLLECTD_LOCALE, /* overwrite = */ 1);
271 #endif
272
273 #if HAVE_LIBKSTAT
274   kc = NULL;
275   update_kstat();
276 #endif
277
278 #if HAVE_LIBSTATGRAB
279   if (sg_init(
280 #if HAVE_LIBSTATGRAB_0_90
281           0
282 #endif
283           )) {
284     ERROR("sg_init: %s", sg_str_error(sg_get_error()));
285     return -1;
286   }
287
288   if (sg_drop_privileges()) {
289     ERROR("sg_drop_privileges: %s", sg_str_error(sg_get_error()));
290     return -1;
291   }
292 #endif
293
294   return plugin_init_all();
295 } /* int do_init () */
296
297 static int do_loop(void) {
298   cdtime_t interval = cf_get_default_interval();
299   cdtime_t wait_until;
300
301   wait_until = cdtime() + interval;
302
303   while (loop == 0) {
304     cdtime_t now;
305
306 #if HAVE_LIBKSTAT
307     update_kstat();
308 #endif
309
310     /* Issue all plugins */
311     plugin_read_all();
312
313     now = cdtime();
314     if (now >= wait_until) {
315       WARNING("Not sleeping because the next interval is "
316               "%.3f seconds in the past!",
317               CDTIME_T_TO_DOUBLE(now - wait_until));
318       wait_until = now + interval;
319       continue;
320     }
321
322     struct timespec ts_wait = CDTIME_T_TO_TIMESPEC(wait_until - now);
323     wait_until = wait_until + interval;
324
325     while ((loop == 0) && (nanosleep(&ts_wait, &ts_wait) != 0)) {
326       if (errno != EINTR) {
327         char errbuf[1024];
328         ERROR("nanosleep failed: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
329         return -1;
330       }
331     }
332   } /* while (loop == 0) */
333
334   return 0;
335 } /* int do_loop */
336
337 static int do_shutdown(void) {
338   return plugin_shutdown_all();
339 } /* int do_shutdown */
340
341 #if COLLECT_DAEMON
342 static int pidfile_create(void) {
343   FILE *fh;
344   const char *file = global_option_get("PIDFile");
345
346   if ((fh = fopen(file, "w")) == NULL) {
347     char errbuf[1024];
348     ERROR("fopen (%s): %s", file, sstrerror(errno, errbuf, sizeof(errbuf)));
349     return 1;
350   }
351
352   fprintf(fh, "%i\n", (int)getpid());
353   fclose(fh);
354
355   return 0;
356 } /* static int pidfile_create (const char *file) */
357
358 static int pidfile_remove(void) {
359   const char *file = global_option_get("PIDFile");
360   if (file == NULL)
361     return 0;
362
363   return unlink(file);
364 } /* static int pidfile_remove (const char *file) */
365 #endif /* COLLECT_DAEMON */
366
367 #ifdef KERNEL_LINUX
368 static int notify_upstart(void) {
369   char const *upstart_job = getenv("UPSTART_JOB");
370
371   if (upstart_job == NULL)
372     return 0;
373
374   if (strcmp(upstart_job, "collectd") != 0) {
375     WARNING("Environment specifies unexpected UPSTART_JOB=\"%s\", expected "
376             "\"collectd\". Ignoring the variable.",
377             upstart_job);
378     return 0;
379   }
380
381   NOTICE("Upstart detected, stopping now to signal readyness.");
382   raise(SIGSTOP);
383   unsetenv("UPSTART_JOB");
384
385   return 1;
386 }
387
388 static int notify_systemd(void) {
389   int fd;
390   const char *notifysocket;
391   struct sockaddr_un su = {0};
392   size_t su_size;
393   char buffer[] = "READY=1\n";
394
395   notifysocket = getenv("NOTIFY_SOCKET");
396   if (notifysocket == NULL)
397     return 0;
398
399   if ((strlen(notifysocket) < 2) ||
400       ((notifysocket[0] != '@') && (notifysocket[0] != '/'))) {
401     ERROR("invalid notification socket NOTIFY_SOCKET=\"%s\": path must be "
402           "absolute",
403           notifysocket);
404     return 0;
405   }
406   NOTICE("Systemd detected, trying to signal readyness.");
407
408   unsetenv("NOTIFY_SOCKET");
409
410 #if defined(SOCK_CLOEXEC)
411   fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, /* protocol = */ 0);
412 #else
413   fd = socket(AF_UNIX, SOCK_DGRAM, /* protocol = */ 0);
414 #endif
415   if (fd < 0) {
416     char errbuf[1024];
417     ERROR("creating UNIX socket failed: %s",
418           sstrerror(errno, errbuf, sizeof(errbuf)));
419     return 0;
420   }
421
422   su.sun_family = AF_UNIX;
423   if (notifysocket[0] != '@') {
424     /* regular UNIX socket */
425     sstrncpy(su.sun_path, notifysocket, sizeof(su.sun_path));
426     su_size = sizeof(su);
427   } else {
428     /* Linux abstract namespace socket: specify address as "\0foo", i.e.
429      * start with a null byte. Since null bytes have no special meaning in
430      * that case, we have to set su_size correctly to cover only the bytes
431      * that are part of the address. */
432     sstrncpy(su.sun_path, notifysocket, sizeof(su.sun_path));
433     su.sun_path[0] = 0;
434     su_size = sizeof(sa_family_t) + strlen(notifysocket);
435     if (su_size > sizeof(su))
436       su_size = sizeof(su);
437   }
438
439   if (sendto(fd, buffer, strlen(buffer), MSG_NOSIGNAL, (void *)&su,
440              (socklen_t)su_size) < 0) {
441     char errbuf[1024];
442     ERROR("sendto(\"%s\") failed: %s", notifysocket,
443           sstrerror(errno, errbuf, sizeof(errbuf)));
444     close(fd);
445     return 0;
446   }
447
448   unsetenv("NOTIFY_SOCKET");
449   close(fd);
450   return 1;
451 }
452 #endif /* KERNEL_LINUX */
453
454 struct cmdline_config {
455   _Bool test_config;
456   _Bool test_readall;
457   _Bool create_basedir;
458   const char *configfile;
459   _Bool daemonize;
460 };
461
462 void read_cmdline(int argc, char **argv, struct cmdline_config *config) {
463   /* read options */
464   while (1) {
465     int c;
466     c = getopt(argc, argv, "htTC:"
467 #if COLLECT_DAEMON
468                            "fP:"
469 #endif
470                );
471
472     if (c == -1)
473       break;
474
475     switch (c) {
476     case 'B':
477       config->create_basedir = 0;
478       break;
479     case 'C':
480       config->configfile = optarg;
481       break;
482     case 't':
483       config->test_config = 1;
484       break;
485     case 'T':
486       config->test_readall = 1;
487       global_option_set("ReadThreads", "-1", 1);
488 #if COLLECT_DAEMON
489       config->daemonize = 0;
490 #endif /* COLLECT_DAEMON */
491       break;
492 #if COLLECT_DAEMON
493     case 'P':
494       global_option_set("PIDFile", optarg, 1);
495       break;
496     case 'f':
497       config->daemonize = 0;
498       break;
499 #endif /* COLLECT_DAEMON */
500     case 'h':
501       exit_usage(0);
502       break;
503     default:
504       exit_usage(1);
505     } /* switch (c) */
506   }   /* while (1) */
507 }
508
509 int configure_collectd(struct cmdline_config *config) {
510   const char *basedir;
511   /*
512    * Read options from the config file, the environment and the command
513    * line (in that order, with later options overwriting previous ones in
514    * general).
515    * Also, this will automatically load modules.
516    */
517   if (cf_read(config->configfile)) {
518     fprintf(stderr, "Error: Reading the config file failed!\n"
519                     "Read the logs for details.\n");
520     return 1;
521   }
522
523   /*
524    * Change directory. We do this _after_ reading the config and loading
525    * modules to relative paths work as expected.
526    */
527   if ((basedir = global_option_get("BaseDir")) == NULL) {
528     fprintf(stderr,
529             "Don't have a basedir to use. This should not happen. Ever.");
530     return 1;
531   } else if (change_basedir(basedir, config->create_basedir)) {
532     fprintf(stderr, "Error: Unable to change to directory `%s'.\n", basedir);
533     return 1;
534   }
535
536   /*
537    * Set global variables or, if that fails, exit. We cannot run with
538    * them being uninitialized. If nothing is configured, then defaults
539    * are being used. So this means that the user has actually done
540    * something wrong.
541    */
542   if (init_global_variables() != 0)
543     return 1;
544
545   return 0;
546 }
547
548 int main(int argc, char **argv) {
549 #if COLLECT_DAEMON
550   pid_t pid;
551 #endif
552   int exit_status = 0;
553
554   struct cmdline_config config = {
555       .daemonize = 1, .create_basedir = 1, .configfile = CONFIGFILE,
556   };
557
558   read_cmdline(argc, argv, &config);
559
560   if (config.test_config)
561     return 0;
562
563   if (optind < argc)
564     exit_usage(1);
565
566   plugin_init_ctx();
567
568   int status;
569   if ((status = configure_collectd(&config)) != 0)
570     exit(EXIT_FAILURE);
571
572 #if COLLECT_DAEMON
573   /*
574    * fork off child
575    */
576   struct sigaction sig_chld_action = {.sa_handler = SIG_IGN};
577
578   sigaction(SIGCHLD, &sig_chld_action, NULL);
579
580   /*
581    * Only daemonize if we're not being supervised
582    * by upstart or systemd (when using Linux).
583    */
584   if (config.daemonize
585 #ifdef KERNEL_LINUX
586       && notify_upstart() == 0 && notify_systemd() == 0
587 #endif
588       ) {
589     int status;
590
591     if ((pid = fork()) == -1) {
592       /* error */
593       char errbuf[1024];
594       fprintf(stderr, "fork: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
595       return 1;
596     } else if (pid != 0) {
597       /* parent */
598       /* printf ("Running (PID %i)\n", pid); */
599       return 0;
600     }
601
602     /* Detach from session */
603     setsid();
604
605     /* Write pidfile */
606     if (pidfile_create())
607       exit(2);
608
609     /* close standard descriptors */
610     close(2);
611     close(1);
612     close(0);
613
614     status = open("/dev/null", O_RDWR);
615     if (status != 0) {
616       ERROR("Error: Could not connect `STDIN' to `/dev/null' (status %d)",
617             status);
618       return 1;
619     }
620
621     status = dup(0);
622     if (status != 1) {
623       ERROR("Error: Could not connect `STDOUT' to `/dev/null' (status %d)",
624             status);
625       return 1;
626     }
627
628     status = dup(0);
629     if (status != 2) {
630       ERROR("Error: Could not connect `STDERR' to `/dev/null', (status %d)",
631             status);
632       return 1;
633     }
634   }    /* if (config.daemonize) */
635 #endif /* COLLECT_DAEMON */
636
637   struct sigaction sig_pipe_action = {.sa_handler = SIG_IGN};
638
639   sigaction(SIGPIPE, &sig_pipe_action, NULL);
640
641   /*
642    * install signal handlers
643    */
644   struct sigaction sig_int_action = {.sa_handler = sig_int_handler};
645
646   if (0 != sigaction(SIGINT, &sig_int_action, NULL)) {
647     char errbuf[1024];
648     ERROR("Error: Failed to install a signal handler for signal INT: %s",
649           sstrerror(errno, errbuf, sizeof(errbuf)));
650     return 1;
651   }
652
653   struct sigaction sig_term_action = {.sa_handler = sig_term_handler};
654
655   if (0 != sigaction(SIGTERM, &sig_term_action, NULL)) {
656     char errbuf[1024];
657     ERROR("Error: Failed to install a signal handler for signal TERM: %s",
658           sstrerror(errno, errbuf, sizeof(errbuf)));
659     return 1;
660   }
661
662   struct sigaction sig_usr1_action = {.sa_handler = sig_usr1_handler};
663
664   if (0 != sigaction(SIGUSR1, &sig_usr1_action, NULL)) {
665     char errbuf[1024];
666     ERROR("Error: Failed to install a signal handler for signal USR1: %s",
667           sstrerror(errno, errbuf, sizeof(errbuf)));
668     return 1;
669   }
670
671   /*
672    * run the actual loops
673    */
674   if (do_init() != 0) {
675     ERROR("Error: one or more plugin init callbacks failed.");
676     exit_status = 1;
677   }
678
679   if (config.test_readall) {
680     if (plugin_read_all_once() != 0) {
681       ERROR("Error: one or more plugin read callbacks failed.");
682       exit_status = 1;
683     }
684   } else {
685     INFO("Initialization complete, entering read-loop.");
686     do_loop();
687   }
688
689   /* close syslog */
690   INFO("Exiting normally.");
691
692   if (do_shutdown() != 0) {
693     ERROR("Error: one or more plugin shutdown callbacks failed.");
694     exit_status = 1;
695   }
696
697 #if COLLECT_DAEMON
698   if (config.daemonize)
699     pidfile_remove();
700 #endif /* COLLECT_DAEMON */
701
702   return exit_status;
703 } /* int main */