Code

effc615244b35c38adb5977636a3144caddf342e
[sysdb.git] / src / daemon / syscollectord.c
1 /*
2  * syscollector - src/syscollecord.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "syscollector.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/string.h"
37 #include "daemon/config.h"
39 #if HAVE_LIBGEN_H
40 #       include <libgen.h>
41 #else /* HAVE_LIBGEN_H */
42 #       define basename(path) (path)
43 #endif /* ! HAVE_LIBGEN_H */
45 #include <errno.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
50 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
56 #include <unistd.h>
58 #ifndef CONFIGFILE
59 #       define CONFIGFILE SYSCONFDIR"/syscollector/syscollectord.conf"
60 #endif
62 static sc_plugin_loop_t plugin_main_loop = SC_PLUGIN_LOOP_INIT;
64 static void
65 sigintterm_handler(int __attribute__((unused)) signo)
66 {
67         plugin_main_loop.do_loop = 0;
68 } /* sigintterm_handler */
70 static void
71 exit_usage(char *name, int status)
72 {
73         printf(
74 "Usage: %s <options>\n"
76 "\nOptions:\n"
77 "  -C FILE   the main configuration file\n"
78 "            default: "CONFIGFILE"\n"
79 "  -d        run in background (daemonize)\n"
80 "\n"
81 "  -h        display this help and exit\n"
82 "  -V        display the version number and copyright\n"
84 "\nsyscollectord "SC_VERSION_STRING SC_VERSION_EXTRA", "PACKAGE_URL"\n",
85 basename(name));
86         exit(status);
87 } /* exit_usage */
89 static void
90 exit_version(void)
91 {
92         printf("syscollectord version "SC_VERSION_STRING SC_VERSION_EXTRA", "
93                         "built "BUILD_DATE"\n"
94                         "using libsyscollection verion %s%s\n"
95                         "Copyright (C) 2012 "PACKAGE_MAINTAINER"\n"
97                         "\nThis is free software under the terms of the BSD license, see "
98                         "the source for\ncopying conditions. There is NO WARRANTY; not "
99                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
100                         "PURPOSE.\n", sc_version_string(), sc_version_extra());
101         exit(0);
102 } /* exit_version */
104 static int
105 daemonize(void)
107         pid_t pid;
109         if ((pid = fork()) < 0) {
110                 char errbuf[1024];
111                 fprintf(stderr, "Failed to fork to background: %s\n",
112                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
113                 return errno;
114         }
115         else if (pid != 0) {
116                 /* parent */
117                 exit(0);
118         }
120         if (chdir("/")) {
121                 char errbuf[1024];
122                 fprintf(stderr, "Failed to change working directory to /: %s\n",
123                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
124                 return errno;
125         }
127         /* detach from session */
128         setsid();
130         close(0);
131         if (open("/dev/null", O_RDWR)) {
132                 char errbuf[1024];
133                 fprintf(stderr, "Failed to connect stdin to '/dev/null': %s\n",
134                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
135                 return errno;
136         }
138         close(1);
139         if (dup(0) != 1) {
140                 char errbuf[1024];
141                 fprintf(stderr, "Could not connect stdout to '/dev/null': %s\n",
142                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
143                 return errno;
144         }
146         close(2);
147         if (dup(0) != 2) {
148                 char errbuf[1024];
149                 fprintf(stdout, "Could not connect stderr to '/dev/null': %s\n",
150                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
151                 return errno;
152         }
153         return 0;
154 } /* daemonize */
156 int
157 main(int argc, char **argv)
159         char *config_filename = NULL;
160         _Bool daemon = 0;
162         struct sigaction sa_intterm;
164         while (42) {
165                 int opt = getopt(argc, argv, "C:dhV");
167                 if (-1 == opt)
168                         break;
170                 switch (opt) {
171                         case 'C':
172                                 config_filename = optarg;
173                                 break;
174                         case 'd':
175                                 daemon = 1;
176                                 break;
178                         case 'h':
179                                 exit_usage(argv[0], 0);
180                                 break;
181                         case 'V':
182                                 exit_version();
183                                 break;
184                         default:
185                                 exit_usage(argv[0], 1);
186                 }
187         }
189         if (optind < argc)
190                 exit_usage(argv[0], 1);
192         if (! config_filename)
193                 config_filename = CONFIGFILE;
195         if (daemon_parse_config(config_filename)) {
196                 fprintf(stderr, "Failed to parse configuration file.\n");
197                 exit(1);
198         }
200         memset(&sa_intterm, 0, sizeof(sa_intterm));
201         sa_intterm.sa_handler = sigintterm_handler;
202         sa_intterm.sa_flags = 0;
204         if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
205                 char errbuf[1024];
206                 fprintf(stderr, "Failed to install signal handler for SIGINT: %s\n",
207                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
208                 exit(1);
209         }
210         if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
211                 char errbuf[1024];
212                 fprintf(stderr, "Failed to install signal handler for SIGTERM: %s\n",
213                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
214                 exit(1);
215         }
217         if (daemon)
218                 if (daemonize())
219                         exit(1);
221         fprintf(stderr, "syscollectord "SC_VERSION_STRING" (pid %i) "
222                         "initialized successfully\n", (int)getpid());
224         sc_plugin_init_all();
225         sc_plugin_collector_loop(&plugin_main_loop);
227         fprintf(stderr, "Shutting down syscollector "SC_VERSION_STRING
228                         " (pid %i)\n", (int)getpid());
230         fprintf(stderr, "Store dump:\n");
231         sc_store_dump(stderr);
232         return 0;
233 } /* main */
235 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */