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 "daemon/config.h"
39 #if HAVE_LIBGEN_H
40 # include <libgen.h>
41 #else /* HAVE_LIBGEN_H */
42 # define basename(path) (path)
43 #endif /* ! HAVE_LIBGEN_H */
45 #include <errno.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
50 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
56 #include <unistd.h>
58 #ifndef CONFIGFILE
59 # define CONFIGFILE SYSCONFDIR"/sysdb/sysdbd.conf"
60 #endif
62 static sdb_plugin_loop_t plugin_main_loop = SDB_PLUGIN_LOOP_INIT;
64 static void
65 sigintterm_handler(int __attribute__((unused)) signo)
66 {
67 plugin_main_loop.do_loop = 0;
68 } /* sigintterm_handler */
70 static void
71 exit_usage(char *name, int status)
72 {
73 printf(
74 "Usage: %s <options>\n"
76 "\nOptions:\n"
77 " -C FILE the main configuration file\n"
78 " default: "CONFIGFILE"\n"
79 " -d run in background (daemonize)\n"
80 "\n"
81 " -h display this help and exit\n"
82 " -V display the version number and copyright\n"
84 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
85 basename(name));
86 exit(status);
87 } /* exit_usage */
89 static void
90 exit_version(void)
91 {
92 printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
93 "built "BUILD_DATE"\n"
94 "using libsysdb verion %s%s\n"
95 "Copyright (C) 2012 "PACKAGE_MAINTAINER"\n"
97 "\nThis is free software under the terms of the BSD license, see "
98 "the source for\ncopying conditions. There is NO WARRANTY; not "
99 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
100 "PURPOSE.\n", sdb_version_string(), sdb_version_extra());
101 exit(0);
102 } /* exit_version */
104 static int
105 daemonize(void)
106 {
107 pid_t pid;
109 if ((pid = fork()) < 0) {
110 char errbuf[1024];
111 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
112 sdb_strerror(errno, errbuf, sizeof(errbuf)));
113 return errno;
114 }
115 else if (pid != 0) {
116 /* parent */
117 exit(0);
118 }
120 if (chdir("/")) {
121 char errbuf[1024];
122 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
123 "the root directory: %s",
124 sdb_strerror(errno, errbuf, sizeof(errbuf)));
125 return errno;
126 }
128 /* detach from session */
129 setsid();
131 close(0);
132 if (open("/dev/null", O_RDWR)) {
133 char errbuf[1024];
134 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
135 sdb_strerror(errno, errbuf, sizeof(errbuf)));
136 return errno;
137 }
139 close(1);
140 if (dup(0) != 1) {
141 char errbuf[1024];
142 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
143 sdb_strerror(errno, errbuf, sizeof(errbuf)));
144 return errno;
145 }
147 close(2);
148 if (dup(0) != 2) {
149 char errbuf[1024];
150 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
151 sdb_strerror(errno, errbuf, sizeof(errbuf)));
152 return errno;
153 }
154 return 0;
155 } /* daemonize */
157 int
158 main(int argc, char **argv)
159 {
160 char *config_filename = NULL;
161 _Bool do_daemonize = 0;
163 struct sigaction sa_intterm;
165 while (42) {
166 int opt = getopt(argc, argv, "C:dhV");
168 if (-1 == opt)
169 break;
171 switch (opt) {
172 case 'C':
173 config_filename = optarg;
174 break;
175 case 'd':
176 do_daemonize = 1;
177 break;
179 case 'h':
180 exit_usage(argv[0], 0);
181 break;
182 case 'V':
183 exit_version();
184 break;
185 default:
186 exit_usage(argv[0], 1);
187 }
188 }
190 if (optind < argc)
191 exit_usage(argv[0], 1);
193 if (! config_filename)
194 config_filename = CONFIGFILE;
196 if (daemon_parse_config(config_filename)) {
197 sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
198 exit(1);
199 }
201 memset(&sa_intterm, 0, sizeof(sa_intterm));
202 sa_intterm.sa_handler = sigintterm_handler;
203 sa_intterm.sa_flags = 0;
205 if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
206 char errbuf[1024];
207 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
208 "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
209 exit(1);
210 }
211 if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
212 char errbuf[1024];
213 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
214 "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
215 exit(1);
216 }
218 if (do_daemonize)
219 if (daemonize())
220 exit(1);
222 sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
223 SDB_VERSION_EXTRA " (pid %i) initialized successfully",
224 (int)getpid());
226 sdb_plugin_init_all();
227 plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
228 sdb_plugin_collector_loop(&plugin_main_loop);
230 sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
231 SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
233 fprintf(stderr, "Store dump:\n");
234 sdb_store_dump(stderr);
235 return 0;
236 } /* main */
238 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */