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