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 char *default_listen_addresses[] = {
81 DEFAULT_SOCKET,
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])) {
261 status = 1;
262 break;
263 }
264 }
266 /* break on error */
267 if (status)
268 break;
270 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
271 SDB_VERSION_EXTRA " (libsysdb %s%s, pid %i) initialized "
272 "successfully", sdb_version_string(), sdb_version_extra(),
273 (int)getpid());
275 sdb_connection_enable_logging();
276 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
278 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
279 plugin_main_loop.do_loop = 0;
280 /* send a signal to interrupt the sleep call
281 * and make the thread shut down faster */
282 pthread_kill(backend_thread, SIGINT);
283 pthread_join(backend_thread, NULL);
285 if (! reconfigure)
286 break;
288 reconfigure = 0;
289 sdb_fe_sock_clear_listeners(sock);
290 if (do_reconfigure()) {
291 sdb_log(SDB_LOG_ERR, "Reconfiguration failed");
292 status = 1;
293 break;
294 }
295 }
297 /* clean up in case we exited the loop on error */
298 plugin_main_loop.do_loop = 0;
299 frontend_main_loop.do_loop = 0;
300 pthread_kill(backend_thread, SIGINT);
301 pthread_join(backend_thread, NULL);
303 sdb_fe_sock_destroy(sock);
304 return status;
305 } /* main_loop */
307 int
308 main(int argc, char **argv)
309 {
310 bool do_daemonize = 1;
312 struct sigaction sa_intterm;
313 struct sigaction sa_hup;
314 int status;
316 sdb_error_set_logger(sdb_plugin_log);
318 while (42) {
319 int opt = getopt(argc, argv, "C:DhV");
321 if (-1 == opt)
322 break;
324 switch (opt) {
325 case 'C':
326 config_filename = optarg;
327 break;
328 case 'D':
329 do_daemonize = 0;
330 break;
332 case 'h':
333 exit_usage(argv[0], 0);
334 break;
335 case 'V':
336 exit_version();
337 break;
338 default:
339 exit_usage(argv[0], 1);
340 }
341 }
343 if (optind < argc)
344 exit_usage(argv[0], 1);
346 if (! config_filename)
347 config_filename = CONFIGFILE;
348 if ((status = configure()))
349 exit(status);
351 memset(&sa_intterm, 0, sizeof(sa_intterm));
352 sa_intterm.sa_handler = sigintterm_handler;
353 sa_intterm.sa_flags = 0;
355 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
356 char errbuf[1024];
357 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
358 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
359 exit(1);
360 }
361 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
362 char errbuf[1024];
363 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
364 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
365 exit(1);
366 }
368 if (do_daemonize)
369 if (daemonize())
370 exit(1);
372 SSL_load_error_strings();
373 OpenSSL_add_ssl_algorithms();
375 sdb_plugin_init_all();
376 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
378 memset(&sa_hup, 0, sizeof(sa_hup));
379 sa_hup.sa_handler = sighup_handler;
380 sa_hup.sa_flags = 0;
382 if (sigaction(SIGHUP, &sa_hup, /* old action */ NULL)) {
383 char errbuf[1024];
384 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
385 "SIGHUP: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
386 exit(1);
387 }
389 /* ignore, we see this, for example, if a client disconnects without
390 * closing the connection cleanly */
391 signal(SIGPIPE, SIG_IGN);
393 status = main_loop();
395 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
396 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
397 sdb_plugin_shutdown_all();
399 ERR_free_strings();
400 return status;
401 } /* main */
403 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */