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();
236 pthread_t backend_thread;
238 while (42) {
239 size_t i;
241 plugin_main_loop.do_loop = 1;
242 frontend_main_loop.do_loop = 1;
244 memset(&backend_thread, 0, sizeof(backend_thread));
245 if (pthread_create(&backend_thread, /* attr = */ NULL,
246 backend_handler, /* arg = */ NULL)) {
247 char buf[1024];
248 sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
249 sdb_strerror(errno, buf, sizeof(buf)));
251 plugin_main_loop.do_loop = 0;
252 break;
253 }
255 for (i = 0; i < listen_addresses_num; ++i)
256 if (sdb_fe_sock_add_listener(sock, listen_addresses[i]))
257 break;
259 /* break on error */
260 if (i >= listen_addresses_num) {
261 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
262 SDB_VERSION_EXTRA " (pid %i) initialized successfully",
263 (int)getpid());
265 sdb_connection_enable_logging();
267 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
268 }
270 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
271 plugin_main_loop.do_loop = 0;
272 /* send a signal to interrupt the sleep call
273 * and make the thread shut down faster */
274 pthread_kill(backend_thread, SIGINT);
275 pthread_join(backend_thread, NULL);
277 if (! reconfigure)
278 break;
280 reconfigure = 0;
281 sdb_fe_sock_clear_listeners(sock);
282 if (do_reconfigure()) {
283 sdb_log(SDB_LOG_ERR, "Reconfiguration failed");
284 break;
285 }
286 }
288 sdb_fe_sock_destroy(sock);
289 return 0;
290 } /* main_loop */
292 int
293 main(int argc, char **argv)
294 {
295 _Bool do_daemonize = 1;
297 struct sigaction sa_intterm;
298 struct sigaction sa_hup;
299 int status;
301 sdb_error_set_logger(sdb_plugin_log);
303 while (42) {
304 int opt = getopt(argc, argv, "C:DhV");
306 if (-1 == opt)
307 break;
309 switch (opt) {
310 case 'C':
311 config_filename = optarg;
312 break;
313 case 'D':
314 do_daemonize = 0;
315 break;
317 case 'h':
318 exit_usage(argv[0], 0);
319 break;
320 case 'V':
321 exit_version();
322 break;
323 default:
324 exit_usage(argv[0], 1);
325 }
326 }
328 if (optind < argc)
329 exit_usage(argv[0], 1);
331 if (! config_filename)
332 config_filename = CONFIGFILE;
333 if ((status = configure()))
334 exit(status);
336 memset(&sa_intterm, 0, sizeof(sa_intterm));
337 sa_intterm.sa_handler = sigintterm_handler;
338 sa_intterm.sa_flags = 0;
340 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
341 char errbuf[1024];
342 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
343 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
344 exit(1);
345 }
346 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
347 char errbuf[1024];
348 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
349 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
350 exit(1);
351 }
353 if (do_daemonize)
354 if (daemonize())
355 exit(1);
357 sdb_plugin_init_all();
358 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
360 memset(&sa_hup, 0, sizeof(sa_hup));
361 sa_hup.sa_handler = sighup_handler;
362 sa_hup.sa_flags = 0;
364 if (sigaction(SIGHUP, &sa_hup, /* old action */ NULL)) {
365 char errbuf[1024];
366 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
367 "SIGHUP: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
368 exit(1);
369 }
371 /* ignore, we see this, for example, if a client disconnects without
372 * closing the connection cleanly */
373 signal(SIGPIPE, SIG_IGN);
375 status = main_loop();
377 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
378 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
379 sdb_plugin_shutdown_all();
380 return status;
381 } /* main */
383 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */