1 /*
2 * SysDB - src/tools/sysdbd/main.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 "utils/error.h"
37 #include "frontend/connection.h"
38 #include "frontend/sock.h"
40 #include "tools/sysdbd/configfile.h"
42 #if HAVE_LIBGEN_H
43 # include <libgen.h>
44 #else /* HAVE_LIBGEN_H */
45 # define basename(path) (path)
46 #endif /* ! HAVE_LIBGEN_H */
48 #include <errno.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
53 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
59 #include <unistd.h>
61 #include <pthread.h>
63 #ifndef CONFIGFILE
64 # define CONFIGFILE SYSCONFDIR"/sysdb/sysdbd.conf"
65 #endif
67 #ifndef DEFAULT_SOCKET
68 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
69 #endif
71 static sdb_plugin_loop_t plugin_main_loop = SDB_PLUGIN_LOOP_INIT;
72 static sdb_fe_loop_t frontend_main_loop = SDB_FE_LOOP_INIT;
74 static char *config_filename = NULL;
75 static int reconfigure = 0;
77 static char *default_listen_addresses[] = {
78 DEFAULT_SOCKET,
79 };
81 static void
82 sigintterm_handler(int __attribute__((unused)) signo)
83 {
84 frontend_main_loop.do_loop = 0;
85 } /* sigintterm_handler */
87 static void
88 sighup_handler(int __attribute__((unused)) signo)
89 {
90 /* (temporarily) terminate the plugin loop ... */
91 frontend_main_loop.do_loop = 0;
92 /* ... and tell the main loop to reconfigure the daemon */
93 reconfigure = 1;
94 } /* sighup_handler */
96 static void
97 exit_usage(char *name, int status)
98 {
99 printf(
100 "Usage: %s <options>\n"
102 "\nOptions:\n"
103 " -C FILE the main configuration file\n"
104 " default: "CONFIGFILE"\n"
105 " -D do not run in background (daemonize)\n"
106 "\n"
107 " -h display this help and exit\n"
108 " -V display the version number and copyright\n"
110 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
111 basename(name));
112 exit(status);
113 } /* exit_usage */
115 static void
116 exit_version(void)
117 {
118 printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
119 "built "BUILD_DATE"\n"
120 "using libsysdb version %s%s\n"
121 "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
123 "\nThis is free software under the terms of the BSD license, see "
124 "the source for\ncopying conditions. There is NO WARRANTY; not "
125 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
126 "PURPOSE.\n", sdb_version_string(), sdb_version_extra());
127 exit(0);
128 } /* exit_version */
130 static int
131 daemonize(void)
132 {
133 pid_t pid;
135 if ((pid = fork()) < 0) {
136 char errbuf[1024];
137 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
138 sdb_strerror(errno, errbuf, sizeof(errbuf)));
139 return errno;
140 }
141 else if (pid != 0) {
142 /* parent */
143 exit(0);
144 }
146 if (chdir("/")) {
147 char errbuf[1024];
148 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
149 "the root directory: %s",
150 sdb_strerror(errno, errbuf, sizeof(errbuf)));
151 return errno;
152 }
154 /* detach from session */
155 setsid();
157 close(0);
158 if (open("/dev/null", O_RDWR)) {
159 char errbuf[1024];
160 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
161 sdb_strerror(errno, errbuf, sizeof(errbuf)));
162 return errno;
163 }
165 close(1);
166 if (dup(0) != 1) {
167 char errbuf[1024];
168 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
169 sdb_strerror(errno, errbuf, sizeof(errbuf)));
170 return errno;
171 }
173 close(2);
174 if (dup(0) != 2) {
175 char errbuf[1024];
176 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
177 sdb_strerror(errno, errbuf, sizeof(errbuf)));
178 return errno;
179 }
180 return 0;
181 } /* daemonize */
183 static int
184 configure(void)
185 {
186 int status;
188 if ((status = daemon_parse_config(config_filename))) {
189 if (status > 0)
190 sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
191 else
192 sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
193 "\tCheck other error messages for details.");
194 return 1;
195 }
197 if (! listen_addresses) {
198 listen_addresses = default_listen_addresses;
199 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
200 }
201 return 0;
202 } /* configure */
204 static int
205 do_reconfigure(void)
206 {
207 int status;
209 sdb_log(SDB_LOG_INFO, "Reconfiguring SysDB daemon");
211 if (listen_addresses != default_listen_addresses)
212 daemon_free_listen_addresses();
213 listen_addresses = NULL;
215 sdb_plugin_reconfigure_init();
216 if ((status = configure()))
217 return status;
218 sdb_plugin_init_all();
219 sdb_plugin_reconfigure_finish();
220 return 0;
221 } /* do_reconfigure */
223 static void *
224 backend_handler(void __attribute__((unused)) *data)
225 {
226 sdb_plugin_collector_loop(&plugin_main_loop);
227 sdb_log(SDB_LOG_INFO, "Shutting down backend thread");
228 return NULL;
229 } /* backend_handler */
231 static int
232 main_loop(void)
233 {
234 sdb_fe_socket_t *sock = sdb_fe_sock_create();
235 pthread_t backend_thread;
237 int status = 0;
239 while (status == 0) {
240 size_t i;
242 plugin_main_loop.do_loop = 1;
243 frontend_main_loop.do_loop = 1;
245 memset(&backend_thread, 0, sizeof(backend_thread));
246 if (pthread_create(&backend_thread, /* attr = */ NULL,
247 backend_handler, /* arg = */ NULL)) {
248 char buf[1024];
249 sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
250 sdb_strerror(errno, buf, sizeof(buf)));
252 plugin_main_loop.do_loop = 0;
253 break;
254 }
256 for (i = 0; i < listen_addresses_num; ++i) {
257 if (sdb_fe_sock_add_listener(sock, listen_addresses[i])) {
258 status = 1;
259 break;
260 }
261 }
263 /* break on error */
264 if (status)
265 break;
267 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
268 SDB_VERSION_EXTRA " (libsysdb %s%s, pid %i) initialized "
269 "successfully", sdb_version_string(), sdb_version_extra(),
270 (int)getpid());
272 sdb_connection_enable_logging();
273 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
275 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
276 plugin_main_loop.do_loop = 0;
277 /* send a signal to interrupt the sleep call
278 * and make the thread shut down faster */
279 pthread_kill(backend_thread, SIGINT);
280 pthread_join(backend_thread, NULL);
282 if (! reconfigure)
283 break;
285 reconfigure = 0;
286 sdb_fe_sock_clear_listeners(sock);
287 if (do_reconfigure()) {
288 sdb_log(SDB_LOG_ERR, "Reconfiguration failed");
289 status = 1;
290 break;
291 }
292 }
294 /* clean up in case we exited the loop on error */
295 plugin_main_loop.do_loop = 0;
296 frontend_main_loop.do_loop = 0;
297 pthread_kill(backend_thread, SIGINT);
298 pthread_join(backend_thread, NULL);
300 sdb_fe_sock_destroy(sock);
301 return status;
302 } /* main_loop */
304 int
305 main(int argc, char **argv)
306 {
307 _Bool do_daemonize = 1;
309 struct sigaction sa_intterm;
310 struct sigaction sa_hup;
311 int status;
313 sdb_error_set_logger(sdb_plugin_log);
315 while (42) {
316 int opt = getopt(argc, argv, "C:DhV");
318 if (-1 == opt)
319 break;
321 switch (opt) {
322 case 'C':
323 config_filename = optarg;
324 break;
325 case 'D':
326 do_daemonize = 0;
327 break;
329 case 'h':
330 exit_usage(argv[0], 0);
331 break;
332 case 'V':
333 exit_version();
334 break;
335 default:
336 exit_usage(argv[0], 1);
337 }
338 }
340 if (optind < argc)
341 exit_usage(argv[0], 1);
343 if (! config_filename)
344 config_filename = CONFIGFILE;
345 if ((status = configure()))
346 exit(status);
348 memset(&sa_intterm, 0, sizeof(sa_intterm));
349 sa_intterm.sa_handler = sigintterm_handler;
350 sa_intterm.sa_flags = 0;
352 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
353 char errbuf[1024];
354 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
355 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
356 exit(1);
357 }
358 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
359 char errbuf[1024];
360 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
361 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
362 exit(1);
363 }
365 if (do_daemonize)
366 if (daemonize())
367 exit(1);
369 sdb_plugin_init_all();
370 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
372 memset(&sa_hup, 0, sizeof(sa_hup));
373 sa_hup.sa_handler = sighup_handler;
374 sa_hup.sa_flags = 0;
376 if (sigaction(SIGHUP, &sa_hup, /* old action */ NULL)) {
377 char errbuf[1024];
378 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
379 "SIGHUP: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
380 exit(1);
381 }
383 /* ignore, we see this, for example, if a client disconnects without
384 * closing the connection cleanly */
385 signal(SIGPIPE, SIG_IGN);
387 status = main_loop();
389 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
390 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
391 sdb_plugin_shutdown_all();
392 return status;
393 } /* main */
395 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */