Code

sysdbd: Moved main loop handling into a separate function.
[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 *default_listen_addresses[] = {
75         DEFAULT_SOCKET,
76 };
78 static void
79 sigintterm_handler(int __attribute__((unused)) signo)
80 {
81         frontend_main_loop.do_loop = 0;
82 } /* sigintterm_handler */
84 static void
85 exit_usage(char *name, int status)
86 {
87         printf(
88 "Usage: %s <options>\n"
90 "\nOptions:\n"
91 "  -C FILE   the main configuration file\n"
92 "            default: "CONFIGFILE"\n"
93 "  -D        do not run in background (daemonize)\n"
94 "\n"
95 "  -h        display this help and exit\n"
96 "  -V        display the version number and copyright\n"
98 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
99 basename(name));
100         exit(status);
101 } /* exit_usage */
103 static void
104 exit_version(void)
106         printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
107                         "built "BUILD_DATE"\n"
108                         "using libsysdb version %s%s\n"
109                         "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
111                         "\nThis is free software under the terms of the BSD license, see "
112                         "the source for\ncopying conditions. There is NO WARRANTY; not "
113                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
114                         "PURPOSE.\n", sdb_version_string(), sdb_version_extra());
115         exit(0);
116 } /* exit_version */
118 static int
119 daemonize(void)
121         pid_t pid;
123         if ((pid = fork()) < 0) {
124                 char errbuf[1024];
125                 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
126                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
127                 return errno;
128         }
129         else if (pid != 0) {
130                 /* parent */
131                 exit(0);
132         }
134         if (chdir("/")) {
135                 char errbuf[1024];
136                 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
137                                 "the root directory: %s",
138                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
139                 return errno;
140         }
142         /* detach from session */
143         setsid();
145         close(0);
146         if (open("/dev/null", O_RDWR)) {
147                 char errbuf[1024];
148                 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
149                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
150                 return errno;
151         }
153         close(1);
154         if (dup(0) != 1) {
155                 char errbuf[1024];
156                 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
157                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
158                 return errno;
159         }
161         close(2);
162         if (dup(0) != 2) {
163                 char errbuf[1024];
164                 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
165                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
166                 return errno;
167         }
168         return 0;
169 } /* daemonize */
171 static void *
172 backend_handler(void __attribute__((unused)) *data)
174         sdb_plugin_collector_loop(&plugin_main_loop);
175         sdb_log(SDB_LOG_INFO, "Shutting down backend thread");
176         return NULL;
177 } /* backend_handler */
179 static int
180 main_loop(void)
182         pthread_t backend_thread;
183         size_t i;
185         plugin_main_loop.do_loop = 1;
186         frontend_main_loop.do_loop = 1;
188         memset(&backend_thread, 0, sizeof(backend_thread));
189         if (pthread_create(&backend_thread, /* attr = */ NULL,
190                                 backend_handler, /* arg = */ NULL)) {
191                 char buf[1024];
192                 sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
193                                 sdb_strerror(errno, buf, sizeof(buf)));
195                 plugin_main_loop.do_loop = 0;
196                 return 1;
197         }
199         sdb_fe_socket_t *sock = sdb_fe_sock_create();
200         for (i = 0; i < listen_addresses_num; ++i)
201                 if (sdb_fe_sock_add_listener(sock, listen_addresses[i]))
202                         break;
204         /* break on error */
205         if (i >= listen_addresses_num)
206                 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
208         sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
209         plugin_main_loop.do_loop = 0;
210         /* send a signal to interrupt the sleep call
211          * and make the thread shut down faster */
212         pthread_kill(backend_thread, SIGINT);
213         pthread_join(backend_thread, NULL);
214         sdb_fe_sock_destroy(sock);
216         return 0;
217 } /* main_loop */
219 int
220 main(int argc, char **argv)
222         char *config_filename = NULL;
223         _Bool do_daemonize = 1;
225         struct sigaction sa_intterm;
226         int status;
228         sdb_error_set_logger(sdb_plugin_log);
230         while (42) {
231                 int opt = getopt(argc, argv, "C:DhV");
233                 if (-1 == opt)
234                         break;
236                 switch (opt) {
237                         case 'C':
238                                 config_filename = optarg;
239                                 break;
240                         case 'D':
241                                 do_daemonize = 0;
242                                 break;
244                         case 'h':
245                                 exit_usage(argv[0], 0);
246                                 break;
247                         case 'V':
248                                 exit_version();
249                                 break;
250                         default:
251                                 exit_usage(argv[0], 1);
252                 }
253         }
255         if (optind < argc)
256                 exit_usage(argv[0], 1);
258         if (! config_filename)
259                 config_filename = CONFIGFILE;
261         if ((status = daemon_parse_config(config_filename))) {
262                 if (status > 0)
263                         sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
264                 else
265                         sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
266                                         "\tCheck other error messages for details.");
267                 exit(1);
268         }
270         if (! listen_addresses) {
271                 listen_addresses = default_listen_addresses;
272                 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
273         }
275         memset(&sa_intterm, 0, sizeof(sa_intterm));
276         sa_intterm.sa_handler = sigintterm_handler;
277         sa_intterm.sa_flags = 0;
279         if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
280                 char errbuf[1024];
281                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
282                                 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
283                 exit(1);
284         }
285         if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
286                 char errbuf[1024];
287                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
288                                 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
289                 exit(1);
290         }
292         if (do_daemonize)
293                 if (daemonize())
294                         exit(1);
296         sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
297                         SDB_VERSION_EXTRA " (pid %i) initialized successfully",
298                         (int)getpid());
300         sdb_plugin_init_all();
301         plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
303         /* ignore, we see this, for example, if a client disconnects without
304          * closing the connection cleanly */
305         signal(SIGPIPE, SIG_IGN);
307         sdb_connection_enable_logging();
308         status = main_loop();
310         sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
311                         SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
312         return status;
313 } /* main */
315 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */