Code

Renamed error recording / logging functions.
[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"
36 #include "utils/string.h"
38 #include "daemon/config.h"
40 #if HAVE_LIBGEN_H
41 #       include <libgen.h>
42 #else /* HAVE_LIBGEN_H */
43 #       define basename(path) (path)
44 #endif /* ! HAVE_LIBGEN_H */
46 #include <errno.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
51 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
57 #include <unistd.h>
59 #ifndef CONFIGFILE
60 #       define CONFIGFILE SYSCONFDIR"/sysdb/sysdbd.conf"
61 #endif
63 static sdb_plugin_loop_t plugin_main_loop = SDB_PLUGIN_LOOP_INIT;
65 static void
66 sigintterm_handler(int __attribute__((unused)) signo)
67 {
68         plugin_main_loop.do_loop = 0;
69 } /* sigintterm_handler */
71 static void
72 exit_usage(char *name, int status)
73 {
74         printf(
75 "Usage: %s <options>\n"
77 "\nOptions:\n"
78 "  -C FILE   the main configuration file\n"
79 "            default: "CONFIGFILE"\n"
80 "  -d        run in background (daemonize)\n"
81 "\n"
82 "  -h        display this help and exit\n"
83 "  -V        display the version number and copyright\n"
85 "\nSysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA", "PACKAGE_URL"\n",
86 basename(name));
87         exit(status);
88 } /* exit_usage */
90 static void
91 exit_version(void)
92 {
93         printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", "
94                         "built "BUILD_DATE"\n"
95                         "using libsysdb verion %s%s\n"
96                         "Copyright (C) 2012 "PACKAGE_MAINTAINER"\n"
98                         "\nThis is free software under the terms of the BSD license, see "
99                         "the source for\ncopying conditions. There is NO WARRANTY; not "
100                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
101                         "PURPOSE.\n", sdb_version_string(), sdb_version_extra());
102         exit(0);
103 } /* exit_version */
105 static int
106 daemonize(void)
108         pid_t pid;
110         if ((pid = fork()) < 0) {
111                 char errbuf[1024];
112                 sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s\n",
113                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
114                 return errno;
115         }
116         else if (pid != 0) {
117                 /* parent */
118                 exit(0);
119         }
121         if (chdir("/")) {
122                 char errbuf[1024];
123                 sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
124                                 "the root directory: %s\n",
125                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
126                 return errno;
127         }
129         /* detach from session */
130         setsid();
132         close(0);
133         if (open("/dev/null", O_RDWR)) {
134                 char errbuf[1024];
135                 sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': "
136                                 "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
137                 return errno;
138         }
140         close(1);
141         if (dup(0) != 1) {
142                 char errbuf[1024];
143                 sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': "
144                                 "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
145                 return errno;
146         }
148         close(2);
149         if (dup(0) != 2) {
150                 char errbuf[1024];
151                 sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': "
152                                 "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
153                 return errno;
154         }
155         return 0;
156 } /* daemonize */
158 int
159 main(int argc, char **argv)
161         char *config_filename = NULL;
162         _Bool do_daemonize = 0;
164         struct sigaction sa_intterm;
166         while (42) {
167                 int opt = getopt(argc, argv, "C:dhV");
169                 if (-1 == opt)
170                         break;
172                 switch (opt) {
173                         case 'C':
174                                 config_filename = optarg;
175                                 break;
176                         case 'd':
177                                 do_daemonize = 1;
178                                 break;
180                         case 'h':
181                                 exit_usage(argv[0], 0);
182                                 break;
183                         case 'V':
184                                 exit_version();
185                                 break;
186                         default:
187                                 exit_usage(argv[0], 1);
188                 }
189         }
191         if (optind < argc)
192                 exit_usage(argv[0], 1);
194         if (! config_filename)
195                 config_filename = CONFIGFILE;
197         if (daemon_parse_config(config_filename)) {
198                 sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.\n");
199                 exit(1);
200         }
202         memset(&sa_intterm, 0, sizeof(sa_intterm));
203         sa_intterm.sa_handler = sigintterm_handler;
204         sa_intterm.sa_flags = 0;
206         if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
207                 char errbuf[1024];
208                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
209                                 "SIGINT: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
210                 exit(1);
211         }
212         if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
213                 char errbuf[1024];
214                 sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
215                                 "SIGTERM: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
216                 exit(1);
217         }
219         if (do_daemonize)
220                 if (daemonize())
221                         exit(1);
223         sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
224                         SDB_VERSION_EXTRA " (pid %i) initialized successfully\n",
225                         (int)getpid());
227         sdb_plugin_init_all();
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)\n", (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 : */