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