Code

Moved core/error to utils/error.
[sysdb.git] / src / daemon / sysdbd.c
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 "utils/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)
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)
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)
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)
181         char *config_filename = NULL;
182         _Bool do_daemonize = 1;
184         pthread_t backend_thread;
186         struct sigaction sa_intterm;
187         int status;
189         while (42) {
190                 int opt = getopt(argc, argv, "C:DhV");
192                 if (-1 == opt)
193                         break;
195                 switch (opt) {
196                         case 'C':
197                                 config_filename = optarg;
198                                 break;
199                         case 'D':
200                                 do_daemonize = 0;
201                                 break;
203                         case 'h':
204                                 exit_usage(argv[0], 0);
205                                 break;
206                         case 'V':
207                                 exit_version();
208                                 break;
209                         default:
210                                 exit_usage(argv[0], 1);
211                 }
212         }
214         if (optind < argc)
215                 exit_usage(argv[0], 1);
217         if (! config_filename)
218                 config_filename = CONFIGFILE;
220         if ((status = daemon_parse_config(config_filename))) {
221                 if (status > 0)
222                         sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
223                 else
224                         sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n"
225                                         "\tCheck other error messages for details.");
226                 exit(1);
227         }
229         if (! listen_addresses) {
230                 listen_addresses = default_listen_addresses;
231                 listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses);
232         }
234         memset(&sa_intterm, 0, sizeof(sa_intterm));
235         sa_intterm.sa_handler = sigintterm_handler;
236         sa_intterm.sa_flags = 0;
238         if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
239                 char errbuf[1024];
240                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
241                                 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
242                 exit(1);
243         }
244         if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
245                 char errbuf[1024];
246                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
247                                 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
248                 exit(1);
249         }
251         if (do_daemonize)
252                 if (daemonize())
253                         exit(1);
255         sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
256                         SDB_VERSION_EXTRA " (pid %i) initialized successfully",
257                         (int)getpid());
259         sdb_plugin_init_all();
260         plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
262         memset(&backend_thread, 0, sizeof(backend_thread));
263         if (pthread_create(&backend_thread, /* attr = */ NULL,
264                                 backend_handler, /* arg = */ NULL)) {
265                 char buf[1024];
266                 sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s",
267                                 sdb_strerror(errno, buf, sizeof(buf)));
269                 plugin_main_loop.do_loop = 0;
270         }
271         else {
272                 size_t i;
274                 sdb_fe_socket_t *sock = sdb_fe_sock_create();
275                 for (i = 0; i < listen_addresses_num; ++i)
276                         if (sdb_fe_sock_add_listener(sock, listen_addresses[i]))
277                                 break;
279                 /* break on error */
280                 if (i >= listen_addresses_num)
281                         sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop);
283                 sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate");
284                 plugin_main_loop.do_loop = 0;
285                 pthread_kill(backend_thread, SIGINT);
286                 pthread_join(backend_thread, NULL);
287                 sdb_fe_sock_destroy(sock);
288         }
290         sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
291                         SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
292         return 0;
293 } /* main */
295 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */