Code

store::memory: Add a plugin providing an in-memory store.
[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"
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)
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)
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 configure(void)
187         int status;
189         if ((status = daemon_parse_config(config_filename))) {
190                 if (status > 0)
191                         sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
192                 else
193                         sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
194                                         "\tCheck other error messages for details.");
195                 return 1;
196         }
198         if (! listen_addresses) {
199                 listen_addresses = default_listen_addresses;
200                 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
201         }
202         return 0;
203 } /* configure */
205 static int
206 do_reconfigure(void)
208         int status;
210         sdb_log(SDB_LOG_INFO, "Reconfiguring SysDB daemon");
212         if (listen_addresses != default_listen_addresses)
213                 daemon_free_listen_addresses();
214         listen_addresses = NULL;
216         sdb_plugin_reconfigure_init();
217         if ((status = configure()))
218                 return status;
219         sdb_plugin_init_all();
220         sdb_plugin_reconfigure_finish();
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();
236         pthread_t backend_thread;
238         int status = 0;
240         while (status == 0) {
241                 size_t i;
243                 plugin_main_loop.do_loop = 1;
244                 frontend_main_loop.do_loop = 1;
246                 memset(&backend_thread, 0, sizeof(backend_thread));
247                 if (pthread_create(&backend_thread, /* attr = */ NULL,
248                                         backend_handler, /* arg = */ NULL)) {
249                         char buf[1024];
250                         sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
251                                         sdb_strerror(errno, buf, sizeof(buf)));
253                         plugin_main_loop.do_loop = 0;
254                         break;
255                 }
257                 for (i = 0; i < listen_addresses_num; ++i) {
258                         if (sdb_fe_sock_add_listener(sock, listen_addresses[i].address,
259                                                 &listen_addresses[i].ssl_opts)) {
260                                 status = 1;
261                                 break;
262                         }
263                 }
265                 /* break on error */
266                 if (status)
267                         break;
269                 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
270                                 SDB_VERSION_EXTRA " (libsysdb %s%s, pid %i) initialized "
271                                 "successfully", sdb_version_string(), sdb_version_extra(),
272                                 (int)getpid());
274                 sdb_connection_enable_logging();
275                 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
277                 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
278                 plugin_main_loop.do_loop = 0;
279                 /* send a signal to interrupt the sleep call
280                  * and make the thread shut down faster */
281                 pthread_kill(backend_thread, SIGINT);
282                 pthread_join(backend_thread, NULL);
284                 if (! reconfigure)
285                         break;
287                 reconfigure = 0;
288                 sdb_fe_sock_clear_listeners(sock);
289                 if (do_reconfigure()) {
290                         sdb_log(SDB_LOG_ERR, "Reconfiguration failed");
291                         status = 1;
292                         break;
293                 }
294         }
296         /* clean up in case we exited the loop on error */
297         plugin_main_loop.do_loop = 0;
298         frontend_main_loop.do_loop = 0;
299         pthread_kill(backend_thread, SIGINT);
300         pthread_join(backend_thread, NULL);
302         sdb_fe_sock_destroy(sock);
303         return status;
304 } /* main_loop */
306 int
307 main(int argc, char **argv)
309         bool do_daemonize = 1;
311         struct sigaction sa_intterm;
312         struct sigaction sa_hup;
313         int status;
315         sdb_error_set_logger(sdb_plugin_log);
317         while (42) {
318                 int opt = getopt(argc, argv, "C:DhV");
320                 if (-1 == opt)
321                         break;
323                 switch (opt) {
324                         case 'C':
325                                 config_filename = optarg;
326                                 break;
327                         case 'D':
328                                 do_daemonize = 0;
329                                 break;
331                         case 'h':
332                                 exit_usage(argv[0], 0);
333                                 break;
334                         case 'V':
335                                 exit_version();
336                                 break;
337                         default:
338                                 exit_usage(argv[0], 1);
339                 }
340         }
342         if (optind < argc)
343                 exit_usage(argv[0], 1);
345         if (! config_filename)
346                 config_filename = CONFIGFILE;
347         if ((status = configure()))
348                 exit(status);
350         memset(&sa_intterm, 0, sizeof(sa_intterm));
351         sa_intterm.sa_handler = sigintterm_handler;
352         sa_intterm.sa_flags = 0;
354         if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
355                 char errbuf[1024];
356                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
357                                 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
358                 exit(1);
359         }
360         if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
361                 char errbuf[1024];
362                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
363                                 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
364                 exit(1);
365         }
367         if (do_daemonize)
368                 if (daemonize())
369                         exit(1);
371         if (sdb_ssl_init())
372                 exit(1);
373         sdb_plugin_init_all();
374         plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
376         memset(&sa_hup, 0, sizeof(sa_hup));
377         sa_hup.sa_handler = sighup_handler;
378         sa_hup.sa_flags = 0;
380         if (sigaction(SIGHUP, &sa_hup, /* old action */ NULL)) {
381                 char errbuf[1024];
382                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
383                                 "SIGHUP: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
384                 exit(1);
385         }
387         /* ignore, we see this, for example, if a client disconnects without
388          * closing the connection cleanly */
389         signal(SIGPIPE, SIG_IGN);
391         status = main_loop();
393         sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
394                         SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
395         sdb_plugin_shutdown_all();
396         sdb_plugin_unregister_all();
397         sdb_ssl_shutdown();
398         return status;
399 } /* main */
401 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */