1 /*
2 * SysDB - src/sysdbd.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 "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "core/error.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"/sysdb/sysdbd.conf"
60 #endif
62 static sdb_plugin_loop_t plugin_main_loop = SDB_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 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
85 basename(name));
86 exit(status);
87 } /* exit_usage */
89 static void
90 exit_version(void)
91 {
92 printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
93 "built "BUILD_DATE"\n"
94 "using libsysdb 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", sdb_version_string(), sdb_version_extra());
101 exit(0);
102 } /* exit_version */
104 static int
105 daemonize(void)
106 {
107 pid_t pid;
109 if ((pid = fork()) < 0) {
110 char errbuf[1024];
111 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
112 sdb_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 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
123 "the root directory: %s",
124 sdb_strerror(errno, errbuf, sizeof(errbuf)));
125 return errno;
126 }
128 /* detach from session */
129 setsid();
131 close(0);
132 if (open("/dev/null", O_RDWR)) {
133 char errbuf[1024];
134 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
135 sdb_strerror(errno, errbuf, sizeof(errbuf)));
136 return errno;
137 }
139 close(1);
140 if (dup(0) != 1) {
141 char errbuf[1024];
142 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
143 sdb_strerror(errno, errbuf, sizeof(errbuf)));
144 return errno;
145 }
147 close(2);
148 if (dup(0) != 2) {
149 char errbuf[1024];
150 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
151 sdb_strerror(errno, errbuf, sizeof(errbuf)));
152 return errno;
153 }
154 return 0;
155 } /* daemonize */
157 int
158 main(int argc, char **argv)
159 {
160 char *config_filename = NULL;
161 _Bool do_daemonize = 0;
163 struct sigaction sa_intterm;
164 int status;
166 while (42) {
167 int opt = getopt(argc, argv, "C:dhV");
169 if (-1 == opt)
170 break;
172 switch (opt) {
173 case 'C':
174 config_filename = optarg;
175 break;
176 case 'd':
177 do_daemonize = 1;
178 break;
180 case 'h':
181 exit_usage(argv[0], 0);
182 break;
183 case 'V':
184 exit_version();
185 break;
186 default:
187 exit_usage(argv[0], 1);
188 }
189 }
191 if (optind < argc)
192 exit_usage(argv[0], 1);
194 if (! config_filename)
195 config_filename = CONFIGFILE;
197 if ((status = daemon_parse_config(config_filename))) {
198 if (status > 0)
199 sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
200 else
201 sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
202 "\tCheck other error messages for details.");
203 exit(1);
204 }
206 memset(&sa_intterm, 0, sizeof(sa_intterm));
207 sa_intterm.sa_handler = sigintterm_handler;
208 sa_intterm.sa_flags = 0;
210 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
211 char errbuf[1024];
212 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
213 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
214 exit(1);
215 }
216 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
217 char errbuf[1024];
218 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
219 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
220 exit(1);
221 }
223 if (do_daemonize)
224 if (daemonize())
225 exit(1);
227 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
228 SDB_VERSION_EXTRA " (pid %i) initialized successfully",
229 (int)getpid());
231 sdb_plugin_init_all();
232 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
233 sdb_plugin_collector_loop(&plugin_main_loop);
235 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
236 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
238 fprintf(stderr, "Store dump:\n");
239 sdb_store_dump(stderr);
240 return 0;
241 } /* main */
243 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */