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/sock.h"
39 #include "tools/sysdbd/configfile.h"
41 #if HAVE_LIBGEN_H
42 # include <libgen.h>
43 #else /* HAVE_LIBGEN_H */
44 # define basename(path) (path)
45 #endif /* ! HAVE_LIBGEN_H */
47 #include <errno.h>
49 #include <sys/stat.h>
50 #include <fcntl.h>
52 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
58 #include <unistd.h>
60 #include <pthread.h>
62 #ifndef CONFIGFILE
63 # define CONFIGFILE SYSCONFDIR"/sysdb/sysdbd.conf"
64 #endif
66 #ifndef DEFAULT_SOCKET
67 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
68 #endif
70 static sdb_plugin_loop_t plugin_main_loop = SDB_PLUGIN_LOOP_INIT;
71 static sdb_fe_loop_t frontend_main_loop = SDB_FE_LOOP_INIT;
73 static char *default_listen_addresses[] = {
74 DEFAULT_SOCKET,
75 };
77 static void
78 sigintterm_handler(int __attribute__((unused)) signo)
79 {
80 frontend_main_loop.do_loop = 0;
81 } /* sigintterm_handler */
83 static void
84 exit_usage(char *name, int status)
85 {
86 printf(
87 "Usage: %s <options>\n"
89 "\nOptions:\n"
90 " -C FILE the main configuration file\n"
91 " default: "CONFIGFILE"\n"
92 " -D do not run in background (daemonize)\n"
93 "\n"
94 " -h display this help and exit\n"
95 " -V display the version number and copyright\n"
97 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
98 basename(name));
99 exit(status);
100 } /* exit_usage */
102 static void
103 exit_version(void)
104 {
105 printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
106 "built "BUILD_DATE"\n"
107 "using libsysdb version %s%s\n"
108 "Copyright (C) 2012-2013 "PACKAGE_MAINTAINER"\n"
110 "\nThis is free software under the terms of the BSD license, see "
111 "the source for\ncopying conditions. There is NO WARRANTY; not "
112 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
113 "PURPOSE.\n", sdb_version_string(), sdb_version_extra());
114 exit(0);
115 } /* exit_version */
117 static int
118 daemonize(void)
119 {
120 pid_t pid;
122 if ((pid = fork()) < 0) {
123 char errbuf[1024];
124 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
125 sdb_strerror(errno, errbuf, sizeof(errbuf)));
126 return errno;
127 }
128 else if (pid != 0) {
129 /* parent */
130 exit(0);
131 }
133 if (chdir("/")) {
134 char errbuf[1024];
135 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
136 "the root directory: %s",
137 sdb_strerror(errno, errbuf, sizeof(errbuf)));
138 return errno;
139 }
141 /* detach from session */
142 setsid();
144 close(0);
145 if (open("/dev/null", O_RDWR)) {
146 char errbuf[1024];
147 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
148 sdb_strerror(errno, errbuf, sizeof(errbuf)));
149 return errno;
150 }
152 close(1);
153 if (dup(0) != 1) {
154 char errbuf[1024];
155 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
156 sdb_strerror(errno, errbuf, sizeof(errbuf)));
157 return errno;
158 }
160 close(2);
161 if (dup(0) != 2) {
162 char errbuf[1024];
163 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
164 sdb_strerror(errno, errbuf, sizeof(errbuf)));
165 return errno;
166 }
167 return 0;
168 } /* daemonize */
170 static void *
171 backend_handler(void __attribute__((unused)) *data)
172 {
173 sdb_plugin_collector_loop(&plugin_main_loop);
174 sdb_log(SDB_LOG_INFO, "Shutting down backend thread");
175 return NULL;
176 } /* backend_handler */
178 int
179 main(int argc, char **argv)
180 {
181 char *config_filename = NULL;
182 _Bool do_daemonize = 1;
184 pthread_t backend_thread;
186 struct sigaction sa_intterm;
187 int status;
189 sdb_error_set_logger(sdb_plugin_log);
191 while (42) {
192 int opt = getopt(argc, argv, "C:DhV");
194 if (-1 == opt)
195 break;
197 switch (opt) {
198 case 'C':
199 config_filename = optarg;
200 break;
201 case 'D':
202 do_daemonize = 0;
203 break;
205 case 'h':
206 exit_usage(argv[0], 0);
207 break;
208 case 'V':
209 exit_version();
210 break;
211 default:
212 exit_usage(argv[0], 1);
213 }
214 }
216 if (optind < argc)
217 exit_usage(argv[0], 1);
219 if (! config_filename)
220 config_filename = CONFIGFILE;
222 if ((status = daemon_parse_config(config_filename))) {
223 if (status > 0)
224 sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
225 else
226 sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
227 "\tCheck other error messages for details.");
228 exit(1);
229 }
231 if (! listen_addresses) {
232 listen_addresses = default_listen_addresses;
233 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
234 }
236 memset(&sa_intterm, 0, sizeof(sa_intterm));
237 sa_intterm.sa_handler = sigintterm_handler;
238 sa_intterm.sa_flags = 0;
240 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
241 char errbuf[1024];
242 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
243 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
244 exit(1);
245 }
246 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
247 char errbuf[1024];
248 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
249 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
250 exit(1);
251 }
253 if (do_daemonize)
254 if (daemonize())
255 exit(1);
257 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
258 SDB_VERSION_EXTRA " (pid %i) initialized successfully",
259 (int)getpid());
261 sdb_plugin_init_all();
262 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
264 memset(&backend_thread, 0, sizeof(backend_thread));
265 if (pthread_create(&backend_thread, /* attr = */ NULL,
266 backend_handler, /* arg = */ NULL)) {
267 char buf[1024];
268 sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
269 sdb_strerror(errno, buf, sizeof(buf)));
271 plugin_main_loop.do_loop = 0;
272 }
273 else {
274 size_t i;
276 sdb_fe_socket_t *sock = sdb_fe_sock_create();
277 for (i = 0; i < listen_addresses_num; ++i)
278 if (sdb_fe_sock_add_listener(sock, listen_addresses[i]))
279 break;
281 /* break on error */
282 if (i >= listen_addresses_num)
283 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
285 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
286 plugin_main_loop.do_loop = 0;
287 pthread_kill(backend_thread, SIGINT);
288 pthread_join(backend_thread, NULL);
289 sdb_fe_sock_destroy(sock);
290 }
292 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
293 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
294 return 0;
295 } /* main */
297 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */