1 /*
2 * SysDB - src/sysdbd.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 "core/error.h"
37 #include "frontend/sock.h"
39 #include "daemon/config.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 return NULL;
175 } /* backend_handler */
177 int
178 main(int argc, char **argv)
179 {
180 char *config_filename = NULL;
181 _Bool do_daemonize = 1;
183 pthread_t backend_thread;
185 struct sigaction sa_intterm;
186 int status;
188 while (42) {
189 int opt = getopt(argc, argv, "C:DhV");
191 if (-1 == opt)
192 break;
194 switch (opt) {
195 case 'C':
196 config_filename = optarg;
197 break;
198 case 'D':
199 do_daemonize = 0;
200 break;
202 case 'h':
203 exit_usage(argv[0], 0);
204 break;
205 case 'V':
206 exit_version();
207 break;
208 default:
209 exit_usage(argv[0], 1);
210 }
211 }
213 if (optind < argc)
214 exit_usage(argv[0], 1);
216 if (! config_filename)
217 config_filename = CONFIGFILE;
219 if ((status = daemon_parse_config(config_filename))) {
220 if (status > 0)
221 sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
222 else
223 sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
224 "\tCheck other error messages for details.");
225 exit(1);
226 }
228 if (! listen_addresses) {
229 listen_addresses = default_listen_addresses;
230 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
231 }
233 memset(&sa_intterm, 0, sizeof(sa_intterm));
234 sa_intterm.sa_handler = sigintterm_handler;
235 sa_intterm.sa_flags = 0;
237 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
238 char errbuf[1024];
239 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
240 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
241 exit(1);
242 }
243 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
244 char errbuf[1024];
245 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
246 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
247 exit(1);
248 }
250 if (do_daemonize)
251 if (daemonize())
252 exit(1);
254 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
255 SDB_VERSION_EXTRA " (pid %i) initialized successfully",
256 (int)getpid());
258 sdb_plugin_init_all();
259 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
261 memset(&backend_thread, 0, sizeof(backend_thread));
262 if (pthread_create(&backend_thread, /* attr = */ NULL,
263 backend_handler, /* arg = */ NULL)) {
264 char buf[1024];
265 sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
266 sdb_strerror(errno, buf, sizeof(buf)));
268 plugin_main_loop.do_loop = 0;
269 }
270 else {
271 size_t i;
273 sdb_fe_socket_t *sock = sdb_fe_sock_create();
274 for (i = 0; i < listen_addresses_num; ++i)
275 if (sdb_fe_sock_add_listener(sock, listen_addresses[i]))
276 break;
278 /* break on error */
279 if (i >= listen_addresses_num)
280 sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
282 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
283 plugin_main_loop.do_loop = 0;
284 pthread_join(backend_thread, NULL);
285 sdb_fe_sock_destroy(sock);
286 }
288 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
289 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
290 return 0;
291 } /* main */
293 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */