Code

Fix compilation with GCC when _GNU_SOURCE isn't defined.
[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>
50 #include <time.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
55 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
61 #include <unistd.h>
63 #include <pthread.h>
65 #ifndef CONFIGFILE
66 #       define CONFIGFILE SYSCONFDIR"/sysdb/sysdbd.conf"
67 #endif
69 #ifndef DEFAULT_SOCKET
70 #       define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
71 #endif
73 static sdb_plugin_loop_t plugin_main_loop = SDB_PLUGIN_LOOP_INIT;
74 static sdb_fe_loop_t frontend_main_loop = SDB_FE_LOOP_INIT;
76 static char *config_filename = NULL;
77 static int reconfigure = 0;
79 static daemon_listener_t default_listen_addresses[] = {
80         { DEFAULT_SOCKET, SDB_SSL_DEFAULT_OPTIONS },
81 };
83 static void
84 sigintterm_handler(int __attribute__((unused)) signo)
85 {
86         frontend_main_loop.do_loop = 0;
87 } /* sigintterm_handler */
89 static void
90 sighup_handler(int __attribute__((unused)) signo)
91 {
92         /* (temporarily) terminate the plugin loop ... */
93         frontend_main_loop.do_loop = 0;
94         /* ... and tell the main loop to reconfigure the daemon */
95         reconfigure = 1;
96 } /* sighup_handler */
98 static void
99 exit_usage(char *name, int status)
101         printf(
102 "Usage: %s <options>\n"
104 "\nOptions:\n"
105 "  -C FILE   the main configuration file\n"
106 "            default: "CONFIGFILE"\n"
107 "  -D        do not run in background (daemonize)\n"
108 "\n"
109 "  -h        display this help and exit\n"
110 "  -V        display the version number and copyright\n"
112 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
113 basename(name));
114         exit(status);
115 } /* exit_usage */
117 static void
118 exit_version(void)
120         printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
121                         "built "BUILD_DATE"\n"
122                         "using libsysdb version %s%s\n"
123                         "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
125                         "\nThis is free software under the terms of the BSD license, see "
126                         "the source for\ncopying conditions. There is NO WARRANTY; not "
127                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
128                         "PURPOSE.\n", sdb_version_string(), sdb_version_extra());
129         exit(0);
130 } /* exit_version */
132 static int
133 daemonize(void)
135         pid_t pid;
137         if ((pid = fork()) < 0) {
138                 char errbuf[1024];
139                 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
140                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
141                 return errno;
142         }
143         else if (pid != 0) {
144                 /* parent */
145                 exit(0);
146         }
148         if (chdir("/")) {
149                 char errbuf[1024];
150                 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
151                                 "the root directory: %s",
152                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
153                 return errno;
154         }
156         /* detach from session */
157         setsid();
159         close(0);
160         if (open("/dev/null", O_RDWR)) {
161                 char errbuf[1024];
162                 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
163                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
164                 return errno;
165         }
167         close(1);
168         if (dup(0) != 1) {
169                 char errbuf[1024];
170                 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
171                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
172                 return errno;
173         }
175         close(2);
176         if (dup(0) != 2) {
177                 char errbuf[1024];
178                 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
179                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
180                 return errno;
181         }
182         return 0;
183 } /* daemonize */
185 static int
186 configure(void)
188         int status;
190         if ((status = daemon_parse_config(config_filename))) {
191                 if (status > 0)
192                         sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
193                 else
194                         sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
195                                         "\tCheck other error messages for details.");
196                 return 1;
197         }
199         if (! listen_addresses) {
200                 listen_addresses = default_listen_addresses;
201                 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
202         }
203         return 0;
204 } /* configure */
206 static int
207 do_reconfigure(void)
209         int status;
211         sdb_log(SDB_LOG_INFO, "Reconfiguring SysDB daemon");
213         if (listen_addresses != default_listen_addresses)
214                 daemon_free_listen_addresses();
215         listen_addresses = NULL;
217         sdb_plugin_reconfigure_init();
218         if ((status = configure()))
219                 return status;
220         sdb_plugin_init_all();
221         sdb_plugin_reconfigure_finish();
222         return 0;
223 } /* do_reconfigure */
225 static void *
226 backend_handler(void __attribute__((unused)) *data)
228         sdb_plugin_collector_loop(&plugin_main_loop);
229         sdb_log(SDB_LOG_INFO, "Shutting down backend thread");
230         return NULL;
231 } /* backend_handler */
233 static int
234 main_loop(void)
236         sdb_fe_socket_t *sock = sdb_fe_sock_create();
237         pthread_t backend_thread;
239         int status = 0;
241         while (status == 0) {
242                 size_t i;
244                 plugin_main_loop.do_loop = 1;
245                 frontend_main_loop.do_loop = 1;
247                 memset(&backend_thread, 0, sizeof(backend_thread));
248                 if (pthread_create(&backend_thread, /* attr = */ NULL,
249                                         backend_handler, /* arg = */ NULL)) {
250                         char buf[1024];
251                         sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
252                                         sdb_strerror(errno, buf, sizeof(buf)));
254                         plugin_main_loop.do_loop = 0;
255                         break;
256                 }
258                 for (i = 0; i < listen_addresses_num; ++i) {
259                         if (sdb_fe_sock_add_listener(sock, listen_addresses[i].address,
260                                                 &listen_addresses[i].ssl_opts)) {
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)
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         if (sdb_ssl_init())
373                 exit(1);
374         sdb_plugin_init_all();
375         plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
377         memset(&sa_hup, 0, sizeof(sa_hup));
378         sa_hup.sa_handler = sighup_handler;
379         sa_hup.sa_flags = 0;
381         if (sigaction(SIGHUP, &sa_hup, /* old action */ NULL)) {
382                 char errbuf[1024];
383                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
384                                 "SIGHUP: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
385                 exit(1);
386         }
388         /* ignore, we see this, for example, if a client disconnects without
389          * closing the connection cleanly */
390         signal(SIGPIPE, SIG_IGN);
392         status = main_loop();
394         sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
395                         SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
396         sdb_plugin_shutdown_all();
397         sdb_plugin_unregister_all();
398         sdb_ssl_shutdown();
399         return status;
400 } /* main */
402 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */