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 #include <openssl/ssl.h>
64 #include <openssl/err.h>
66 #ifndef CONFIGFILE
67 # define CONFIGFILE SYSCONFDIR"/sysdb/sysdbd.conf"
68 #endif
70 #ifndef DEFAULT_SOCKET
71 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
72 #endif
74 static sdb_plugin_loop_t plugin_main_loop = SDB_PLUGIN_LOOP_INIT;
75 static sdb_fe_loop_t frontend_main_loop = SDB_FE_LOOP_INIT;
77 static char *config_filename = NULL;
78 static int reconfigure = 0;
80 static daemon_listener_t default_listen_addresses[] = {
81 { DEFAULT_SOCKET, SDB_SSL_DEFAULT_OPTIONS },
82 };
84 static void
85 sigintterm_handler(int __attribute__((unused)) signo)
86 {
87 frontend_main_loop.do_loop = 0;
88 } /* sigintterm_handler */
90 static void
91 sighup_handler(int __attribute__((unused)) signo)
92 {
93 /* (temporarily) terminate the plugin loop ... */
94 frontend_main_loop.do_loop = 0;
95 /* ... and tell the main loop to reconfigure the daemon */
96 reconfigure = 1;
97 } /* sighup_handler */
99 static void
100 exit_usage(char *name, int status)
101 {
102 printf(
103 "Usage: %s <options>\n"
105 "\nOptions:\n"
106 " -C FILE the main configuration file\n"
107 " default: "CONFIGFILE"\n"
108 " -D do not run in background (daemonize)\n"
109 "\n"
110 " -h display this help and exit\n"
111 " -V display the version number and copyright\n"
113 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
114 basename(name));
115 exit(status);
116 } /* exit_usage */
118 static void
119 exit_version(void)
120 {
121 printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
122 "built "BUILD_DATE"\n"
123 "using libsysdb version %s%s\n"
124 "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
126 "\nThis is free software under the terms of the BSD license, see "
127 "the source for\ncopying conditions. There is NO WARRANTY; not "
128 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
129 "PURPOSE.\n", sdb_version_string(), sdb_version_extra());
130 exit(0);
131 } /* exit_version */
133 static int
134 daemonize(void)
135 {
136 pid_t pid;
138 if ((pid = fork()) < 0) {
139 char errbuf[1024];
140 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
141 sdb_strerror(errno, errbuf, sizeof(errbuf)));
142 return errno;
143 }
144 else if (pid != 0) {
145 /* parent */
146 exit(0);
147 }
149 if (chdir("/")) {
150 char errbuf[1024];
151 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
152 "the root directory: %s",
153 sdb_strerror(errno, errbuf, sizeof(errbuf)));
154 return errno;
155 }
157 /* detach from session */
158 setsid();
160 close(0);
161 if (open("/dev/null", O_RDWR)) {
162 char errbuf[1024];
163 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
164 sdb_strerror(errno, errbuf, sizeof(errbuf)));
165 return errno;
166 }
168 close(1);
169 if (dup(0) != 1) {
170 char errbuf[1024];
171 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
172 sdb_strerror(errno, errbuf, sizeof(errbuf)));
173 return errno;
174 }
176 close(2);
177 if (dup(0) != 2) {
178 char errbuf[1024];
179 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
180 sdb_strerror(errno, errbuf, sizeof(errbuf)));
181 return errno;
182 }
183 return 0;
184 } /* daemonize */
186 static int
187 configure(void)
188 {
189 int status;
191 if ((status = daemon_parse_config(config_filename))) {
192 if (status > 0)
193 sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
194 else
195 sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
196 "\tCheck other error messages for details.");
197 return 1;
198 }
200 if (! listen_addresses) {
201 listen_addresses = default_listen_addresses;
202 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
203 }
204 return 0;
205 } /* configure */
207 static int
208 do_reconfigure(void)
209 {
210 int status;
212 sdb_log(SDB_LOG_INFO, "Reconfiguring SysDB daemon");
214 if (listen_addresses != default_listen_addresses)
215 daemon_free_listen_addresses();
216 listen_addresses = NULL;
218 sdb_plugin_reconfigure_init();
219 if ((status = configure()))
220 return status;
221 sdb_plugin_init_all();
222 sdb_plugin_reconfigure_finish();
223 return 0;
224 } /* do_reconfigure */
226 static void *
227 backend_handler(void __attribute__((unused)) *data)
228 {
229 sdb_plugin_collector_loop(&plugin_main_loop);
230 sdb_log(SDB_LOG_INFO, "Shutting down backend thread");
231 return NULL;
232 } /* backend_handler */
234 static int
235 main_loop(void)
236 {
237 sdb_fe_socket_t *sock = sdb_fe_sock_create();
238 pthread_t backend_thread;
240 int status = 0;
242 while (status == 0) {
243 size_t i;
245 plugin_main_loop.do_loop = 1;
246 frontend_main_loop.do_loop = 1;
248 memset(&backend_thread, 0, sizeof(backend_thread));
249 if (pthread_create(&backend_thread, /* attr = */ NULL,
250 backend_handler, /* arg = */ NULL)) {
251 char buf[1024];
252 sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
253 sdb_strerror(errno, buf, sizeof(buf)));
255 plugin_main_loop.do_loop = 0;
256 break;
257 }
259 for (i = 0; i < listen_addresses_num; ++i) {
260 if (sdb_fe_sock_add_listener(sock, listen_addresses[i].address,
261 &listen_addresses[i].ssl_opts)) {
262 status = 1;
263 break;
264 }
265 }
267 /* break on error */
268 if (status)
269 break;
271 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
272 SDB_VERSION_EXTRA " (libsysdb %s%s, pid %i) initialized "
273 "successfully", sdb_version_string(), sdb_version_extra(),
274 (int)getpid());
276 sdb_connection_enable_logging();
277 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
279 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
280 plugin_main_loop.do_loop = 0;
281 /* send a signal to interrupt the sleep call
282 * and make the thread shut down faster */
283 pthread_kill(backend_thread, SIGINT);
284 pthread_join(backend_thread, NULL);
286 if (! reconfigure)
287 break;
289 reconfigure = 0;
290 sdb_fe_sock_clear_listeners(sock);
291 if (do_reconfigure()) {
292 sdb_log(SDB_LOG_ERR, "Reconfiguration failed");
293 status = 1;
294 break;
295 }
296 }
298 /* clean up in case we exited the loop on error */
299 plugin_main_loop.do_loop = 0;
300 frontend_main_loop.do_loop = 0;
301 pthread_kill(backend_thread, SIGINT);
302 pthread_join(backend_thread, NULL);
304 sdb_fe_sock_destroy(sock);
305 return status;
306 } /* main_loop */
308 int
309 main(int argc, char **argv)
310 {
311 bool do_daemonize = 1;
313 struct sigaction sa_intterm;
314 struct sigaction sa_hup;
315 int status;
317 sdb_error_set_logger(sdb_plugin_log);
319 while (42) {
320 int opt = getopt(argc, argv, "C:DhV");
322 if (-1 == opt)
323 break;
325 switch (opt) {
326 case 'C':
327 config_filename = optarg;
328 break;
329 case 'D':
330 do_daemonize = 0;
331 break;
333 case 'h':
334 exit_usage(argv[0], 0);
335 break;
336 case 'V':
337 exit_version();
338 break;
339 default:
340 exit_usage(argv[0], 1);
341 }
342 }
344 if (optind < argc)
345 exit_usage(argv[0], 1);
347 if (! config_filename)
348 config_filename = CONFIGFILE;
349 if ((status = configure()))
350 exit(status);
352 memset(&sa_intterm, 0, sizeof(sa_intterm));
353 sa_intterm.sa_handler = sigintterm_handler;
354 sa_intterm.sa_flags = 0;
356 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
357 char errbuf[1024];
358 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
359 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
360 exit(1);
361 }
362 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
363 char errbuf[1024];
364 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
365 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
366 exit(1);
367 }
369 if (do_daemonize)
370 if (daemonize())
371 exit(1);
373 SSL_load_error_strings();
374 OpenSSL_add_ssl_algorithms();
376 sdb_plugin_init_all();
377 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
379 memset(&sa_hup, 0, sizeof(sa_hup));
380 sa_hup.sa_handler = sighup_handler;
381 sa_hup.sa_flags = 0;
383 if (sigaction(SIGHUP, &sa_hup, /* old action */ NULL)) {
384 char errbuf[1024];
385 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
386 "SIGHUP: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
387 exit(1);
388 }
390 /* ignore, we see this, for example, if a client disconnects without
391 * closing the connection cleanly */
392 signal(SIGPIPE, SIG_IGN);
394 status = main_loop();
396 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
397 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
398 sdb_plugin_shutdown_all();
400 ERR_free_strings();
401 return status;
402 } /* main */
404 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */