Code

6d7ed7ccae70b8f06b8a9646c12318bba2b0fd88
[sysdb.git] / src / tools / sysdbd / main.c
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)
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)
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)
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)
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         sdb_connection_enable_logging();
221         return 0;
222 } /* do_reconfigure */
224 static void *
225 backend_handler(void __attribute__((unused)) *data)
227         sdb_plugin_collector_loop(&plugin_main_loop);
228         sdb_log(SDB_LOG_INFO, "Shutting down backend thread");
229         return NULL;
230 } /* backend_handler */
232 static int
233 main_loop(void)
235         sdb_fe_socket_t *sock = sdb_fe_sock_create();
237         pthread_t backend_thread;
239         while (42) {
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                                 break;
260                 /* break on error */
261                 if (i >= listen_addresses_num)
262                         sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
264                 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
265                 plugin_main_loop.do_loop = 0;
266                 /* send a signal to interrupt the sleep call
267                  * and make the thread shut down faster */
268                 pthread_kill(backend_thread, SIGINT);
269                 pthread_join(backend_thread, NULL);
271                 if (! reconfigure)
272                         break;
274                 reconfigure = 0;
275                 sdb_fe_sock_clear_listeners(sock);
276                 if (do_reconfigure()) {
277                         sdb_log(SDB_LOG_ERR, "Reconfiguration failed");
278                         break;
279                 }
280         }
282         sdb_fe_sock_destroy(sock);
283         return 0;
284 } /* main_loop */
286 int
287 main(int argc, char **argv)
289         _Bool do_daemonize = 1;
291         struct sigaction sa_intterm;
292         struct sigaction sa_hup;
293         int status;
295         sdb_error_set_logger(sdb_plugin_log);
297         while (42) {
298                 int opt = getopt(argc, argv, "C:DhV");
300                 if (-1 == opt)
301                         break;
303                 switch (opt) {
304                         case 'C':
305                                 config_filename = optarg;
306                                 break;
307                         case 'D':
308                                 do_daemonize = 0;
309                                 break;
311                         case 'h':
312                                 exit_usage(argv[0], 0);
313                                 break;
314                         case 'V':
315                                 exit_version();
316                                 break;
317                         default:
318                                 exit_usage(argv[0], 1);
319                 }
320         }
322         if (optind < argc)
323                 exit_usage(argv[0], 1);
325         if (! config_filename)
326                 config_filename = CONFIGFILE;
327         if ((status = configure()))
328                 exit(status);
330         memset(&sa_intterm, 0, sizeof(sa_intterm));
331         sa_intterm.sa_handler = sigintterm_handler;
332         sa_intterm.sa_flags = 0;
334         if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
335                 char errbuf[1024];
336                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
337                                 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
338                 exit(1);
339         }
340         if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
341                 char errbuf[1024];
342                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
343                                 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
344                 exit(1);
345         }
347         if (do_daemonize)
348                 if (daemonize())
349                         exit(1);
351         sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
352                         SDB_VERSION_EXTRA " (pid %i) initialized successfully",
353                         (int)getpid());
355         sdb_plugin_init_all();
356         plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
358         memset(&sa_hup, 0, sizeof(sa_hup));
359         sa_hup.sa_handler = sighup_handler;
360         sa_hup.sa_flags = 0;
362         if (sigaction(SIGHUP, &sa_hup, /* old action */ NULL)) {
363                 char errbuf[1024];
364                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
365                                 "SIGHUP: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
366                 exit(1);
367         }
369         /* ignore, we see this, for example, if a client disconnects without
370          * closing the connection cleanly */
371         signal(SIGPIPE, SIG_IGN);
373         sdb_connection_enable_logging();
374         status = main_loop();
376         sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
377                         SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
378         sdb_plugin_shutdown_all();
379         return status;
380 } /* main */
382 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */