ce54313542df26cd254847d601f01fc14413d74a
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"
36 #include "utils/ssl.h"
38 #include "frontend/connection.h"
39 #include "frontend/sock.h"
41 #include "tools/sysdbd/configfile.h"
43 #if HAVE_LIBGEN_H
44 # include <libgen.h>
45 #else /* HAVE_LIBGEN_H */
46 # define basename(path) (path)
47 #endif /* ! HAVE_LIBGEN_H */
49 #include <errno.h>
51 #include <sys/stat.h>
52 #include <fcntl.h>
54 #include <signal.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
60 #include <unistd.h>
62 #include <pthread.h>
64 #ifndef CONFIGFILE
65 # define CONFIGFILE SYSCONFDIR"/sysdb/sysdbd.conf"
66 #endif
68 #ifndef DEFAULT_SOCKET
69 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
70 #endif
72 static sdb_plugin_loop_t plugin_main_loop = SDB_PLUGIN_LOOP_INIT;
73 static sdb_fe_loop_t frontend_main_loop = SDB_FE_LOOP_INIT;
75 static char *config_filename = NULL;
76 static int reconfigure = 0;
78 static daemon_listener_t default_listen_addresses[] = {
79 { DEFAULT_SOCKET, SDB_SSL_DEFAULT_OPTIONS },
80 };
82 static void
83 sigintterm_handler(int __attribute__((unused)) signo)
84 {
85 frontend_main_loop.do_loop = 0;
86 } /* sigintterm_handler */
88 static void
89 sighup_handler(int __attribute__((unused)) signo)
90 {
91 /* (temporarily) terminate the plugin loop ... */
92 frontend_main_loop.do_loop = 0;
93 /* ... and tell the main loop to reconfigure the daemon */
94 reconfigure = 1;
95 } /* sighup_handler */
97 static void
98 exit_usage(char *name, int status)
99 {
100 printf(
101 "Usage: %s <options>\n"
103 "\nOptions:\n"
104 " -C FILE the main configuration file\n"
105 " default: "CONFIGFILE"\n"
106 " -D do not run in background (daemonize)\n"
107 "\n"
108 " -h display this help and exit\n"
109 " -V display the version number and copyright\n"
111 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
112 basename(name));
113 exit(status);
114 } /* exit_usage */
116 static void
117 exit_version(void)
118 {
119 printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
120 "built "BUILD_DATE"\n"
121 "using libsysdb version %s%s\n"
122 "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
124 "\nThis is free software under the terms of the BSD license, see "
125 "the source for\ncopying conditions. There is NO WARRANTY; not "
126 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
127 "PURPOSE.\n", sdb_version_string(), sdb_version_extra());
128 exit(0);
129 } /* exit_version */
131 static int
132 daemonize(void)
133 {
134 pid_t pid;
136 if ((pid = fork()) < 0) {
137 char errbuf[1024];
138 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
139 sdb_strerror(errno, errbuf, sizeof(errbuf)));
140 return errno;
141 }
142 else if (pid != 0) {
143 /* parent */
144 exit(0);
145 }
147 if (chdir("/")) {
148 char errbuf[1024];
149 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
150 "the root directory: %s",
151 sdb_strerror(errno, errbuf, sizeof(errbuf)));
152 return errno;
153 }
155 /* detach from session */
156 setsid();
158 close(0);
159 if (open("/dev/null", O_RDWR)) {
160 char errbuf[1024];
161 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
162 sdb_strerror(errno, errbuf, sizeof(errbuf)));
163 return errno;
164 }
166 close(1);
167 if (dup(0) != 1) {
168 char errbuf[1024];
169 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
170 sdb_strerror(errno, errbuf, sizeof(errbuf)));
171 return errno;
172 }
174 close(2);
175 if (dup(0) != 2) {
176 char errbuf[1024];
177 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
178 sdb_strerror(errno, errbuf, sizeof(errbuf)));
179 return errno;
180 }
181 return 0;
182 } /* daemonize */
184 static int
185 store_init(void)
186 {
187 sdb_store_t *store = sdb_store_create();
189 if (! store) {
190 sdb_log(SDB_LOG_ERR, "store: Failed to allocate store");
191 return -1;
192 }
193 if (sdb_plugin_register_writer("memstore",
194 &sdb_store_writer, SDB_OBJ(store))) {
195 sdb_object_deref(SDB_OBJ(store));
196 return -1;
197 }
198 if (sdb_plugin_register_reader("memstore",
199 &sdb_store_reader, SDB_OBJ(store))) {
200 sdb_object_deref(SDB_OBJ(store));
201 return -1;
202 }
204 /* Pass ownership to the plugins */
205 sdb_object_deref(SDB_OBJ(store));
206 return 0;
207 } /* store_init */
209 static int
210 configure(void)
211 {
212 int status;
214 if ((status = daemon_parse_config(config_filename))) {
215 if (status > 0)
216 sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
217 else
218 sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
219 "\tCheck other error messages for details.");
220 return 1;
221 }
222 if (store_init()) {
223 sdb_log(SDB_LOG_ERR, "Failed to initialize the store");
224 return 1;
225 }
227 if (! listen_addresses) {
228 listen_addresses = default_listen_addresses;
229 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
230 }
231 return 0;
232 } /* configure */
234 static int
235 do_reconfigure(void)
236 {
237 int status;
239 sdb_log(SDB_LOG_INFO, "Reconfiguring SysDB daemon");
241 if (listen_addresses != default_listen_addresses)
242 daemon_free_listen_addresses();
243 listen_addresses = NULL;
245 sdb_plugin_reconfigure_init();
246 if ((status = configure()))
247 return status;
248 sdb_plugin_init_all();
249 sdb_plugin_reconfigure_finish();
250 return 0;
251 } /* do_reconfigure */
253 static void *
254 backend_handler(void __attribute__((unused)) *data)
255 {
256 sdb_plugin_collector_loop(&plugin_main_loop);
257 sdb_log(SDB_LOG_INFO, "Shutting down backend thread");
258 return NULL;
259 } /* backend_handler */
261 static int
262 main_loop(void)
263 {
264 sdb_fe_socket_t *sock = sdb_fe_sock_create();
265 pthread_t backend_thread;
267 int status = 0;
269 while (status == 0) {
270 size_t i;
272 plugin_main_loop.do_loop = 1;
273 frontend_main_loop.do_loop = 1;
275 memset(&backend_thread, 0, sizeof(backend_thread));
276 if (pthread_create(&backend_thread, /* attr = */ NULL,
277 backend_handler, /* arg = */ NULL)) {
278 char buf[1024];
279 sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
280 sdb_strerror(errno, buf, sizeof(buf)));
282 plugin_main_loop.do_loop = 0;
283 break;
284 }
286 for (i = 0; i < listen_addresses_num; ++i) {
287 if (sdb_fe_sock_add_listener(sock, listen_addresses[i].address,
288 &listen_addresses[i].ssl_opts)) {
289 status = 1;
290 break;
291 }
292 }
294 /* break on error */
295 if (status)
296 break;
298 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
299 SDB_VERSION_EXTRA " (libsysdb %s%s, pid %i) initialized "
300 "successfully", sdb_version_string(), sdb_version_extra(),
301 (int)getpid());
303 sdb_connection_enable_logging();
304 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
306 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
307 plugin_main_loop.do_loop = 0;
308 /* send a signal to interrupt the sleep call
309 * and make the thread shut down faster */
310 pthread_kill(backend_thread, SIGINT);
311 pthread_join(backend_thread, NULL);
313 if (! reconfigure)
314 break;
316 reconfigure = 0;
317 sdb_fe_sock_clear_listeners(sock);
318 if (do_reconfigure()) {
319 sdb_log(SDB_LOG_ERR, "Reconfiguration failed");
320 status = 1;
321 break;
322 }
323 }
325 /* clean up in case we exited the loop on error */
326 plugin_main_loop.do_loop = 0;
327 frontend_main_loop.do_loop = 0;
328 pthread_kill(backend_thread, SIGINT);
329 pthread_join(backend_thread, NULL);
331 sdb_fe_sock_destroy(sock);
332 return status;
333 } /* main_loop */
335 int
336 main(int argc, char **argv)
337 {
338 bool do_daemonize = 1;
340 struct sigaction sa_intterm;
341 struct sigaction sa_hup;
342 int status;
344 sdb_error_set_logger(sdb_plugin_log);
346 while (42) {
347 int opt = getopt(argc, argv, "C:DhV");
349 if (-1 == opt)
350 break;
352 switch (opt) {
353 case 'C':
354 config_filename = optarg;
355 break;
356 case 'D':
357 do_daemonize = 0;
358 break;
360 case 'h':
361 exit_usage(argv[0], 0);
362 break;
363 case 'V':
364 exit_version();
365 break;
366 default:
367 exit_usage(argv[0], 1);
368 }
369 }
371 if (optind < argc)
372 exit_usage(argv[0], 1);
374 if (! config_filename)
375 config_filename = CONFIGFILE;
376 if ((status = configure()))
377 exit(status);
379 memset(&sa_intterm, 0, sizeof(sa_intterm));
380 sa_intterm.sa_handler = sigintterm_handler;
381 sa_intterm.sa_flags = 0;
383 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
384 char errbuf[1024];
385 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
386 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
387 exit(1);
388 }
389 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
390 char errbuf[1024];
391 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
392 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
393 exit(1);
394 }
396 if (do_daemonize)
397 if (daemonize())
398 exit(1);
400 if (sdb_ssl_init())
401 exit(1);
402 sdb_plugin_init_all();
403 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
405 memset(&sa_hup, 0, sizeof(sa_hup));
406 sa_hup.sa_handler = sighup_handler;
407 sa_hup.sa_flags = 0;
409 if (sigaction(SIGHUP, &sa_hup, /* old action */ NULL)) {
410 char errbuf[1024];
411 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
412 "SIGHUP: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
413 exit(1);
414 }
416 /* ignore, we see this, for example, if a client disconnects without
417 * closing the connection cleanly */
418 signal(SIGPIPE, SIG_IGN);
420 status = main_loop();
422 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
423 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
424 sdb_plugin_shutdown_all();
425 sdb_plugin_unregister_all();
426 sdb_ssl_shutdown();
427 return status;
428 } /* main */
430 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */