X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fdaemon%2Fsysdbd.c;h=21350f308b15bc75d624baed8f99b878011caf5c;hp=f1992610f2a2164af9d189c8f1324ea6a3265daf;hb=ddb7ffc175e49abfa69c82777b88d73e1f1103fb;hpb=fa5c130fbd0c75f3c47f1b28feb8d76a4791a93e diff --git a/src/daemon/sysdbd.c b/src/daemon/sysdbd.c index f199261..21350f3 100644 --- a/src/daemon/sysdbd.c +++ b/src/daemon/sysdbd.c @@ -32,7 +32,9 @@ #include "sysdb.h" #include "core/plugin.h" #include "core/store.h" -#include "core/error.h" +#include "utils/error.h" + +#include "frontend/sock.h" #include "daemon/config.h" @@ -55,16 +57,27 @@ #include +#include + #ifndef CONFIGFILE # define CONFIGFILE SYSCONFDIR"/sysdb/sysdbd.conf" #endif +#ifndef DEFAULT_SOCKET +# define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock" +#endif + static sdb_plugin_loop_t plugin_main_loop = SDB_PLUGIN_LOOP_INIT; +static sdb_fe_loop_t frontend_main_loop = SDB_FE_LOOP_INIT; + +static char *default_listen_addresses[] = { + DEFAULT_SOCKET, +}; static void sigintterm_handler(int __attribute__((unused)) signo) { - plugin_main_loop.do_loop = 0; + frontend_main_loop.do_loop = 0; } /* sigintterm_handler */ static void @@ -76,7 +89,7 @@ exit_usage(char *name, int status) "\nOptions:\n" " -C FILE the main configuration file\n" " default: "CONFIGFILE"\n" -" -d run in background (daemonize)\n" +" -D do not run in background (daemonize)\n" "\n" " -h display this help and exit\n" " -V display the version number and copyright\n" @@ -91,8 +104,8 @@ exit_version(void) { printf("SysDBd version "SDB_VERSION_STRING SDB_VERSION_EXTRA", " "built "BUILD_DATE"\n" - "using libsysdb verion %s%s\n" - "Copyright (C) 2012 "PACKAGE_MAINTAINER"\n" + "using libsysdb version %s%s\n" + "Copyright (C) 2012-2013 "PACKAGE_MAINTAINER"\n" "\nThis is free software under the terms of the BSD license, see " "the source for\ncopying conditions. There is NO WARRANTY; not " @@ -154,16 +167,29 @@ daemonize(void) return 0; } /* daemonize */ +static void * +backend_handler(void __attribute__((unused)) *data) +{ + sdb_plugin_collector_loop(&plugin_main_loop); + sdb_log(SDB_LOG_INFO, "Shutting down backend thread"); + return NULL; +} /* backend_handler */ + int main(int argc, char **argv) { char *config_filename = NULL; - _Bool do_daemonize = 0; + _Bool do_daemonize = 1; + + pthread_t backend_thread; struct sigaction sa_intterm; + int status; + + sdb_error_set_logger(sdb_plugin_log); while (42) { - int opt = getopt(argc, argv, "C:dhV"); + int opt = getopt(argc, argv, "C:DhV"); if (-1 == opt) break; @@ -172,8 +198,8 @@ main(int argc, char **argv) case 'C': config_filename = optarg; break; - case 'd': - do_daemonize = 1; + case 'D': + do_daemonize = 0; break; case 'h': @@ -193,11 +219,20 @@ main(int argc, char **argv) if (! config_filename) config_filename = CONFIGFILE; - if (daemon_parse_config(config_filename)) { - sdb_log(SDB_LOG_ERR, "Failed to parse configuration file."); + if ((status = daemon_parse_config(config_filename))) { + if (status > 0) + sdb_log(SDB_LOG_ERR, "Failed to parse configuration file."); + else + sdb_log(SDB_LOG_ERR, "Failed to load configuration file.\n" + "\tCheck other error messages for details."); exit(1); } + if (! listen_addresses) { + listen_addresses = default_listen_addresses; + listen_addresses_num = SDB_STATIC_ARRAY_LEN(default_listen_addresses); + } + memset(&sa_intterm, 0, sizeof(sa_intterm)); sa_intterm.sa_handler = sigintterm_handler; sa_intterm.sa_flags = 0; @@ -225,13 +260,37 @@ main(int argc, char **argv) sdb_plugin_init_all(); plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60); - sdb_plugin_collector_loop(&plugin_main_loop); + + memset(&backend_thread, 0, sizeof(backend_thread)); + if (pthread_create(&backend_thread, /* attr = */ NULL, + backend_handler, /* arg = */ NULL)) { + char buf[1024]; + sdb_log(SDB_LOG_ERR, "Failed to create backend handler thread: %s", + sdb_strerror(errno, buf, sizeof(buf))); + + plugin_main_loop.do_loop = 0; + } + else { + size_t i; + + sdb_fe_socket_t *sock = sdb_fe_sock_create(); + for (i = 0; i < listen_addresses_num; ++i) + if (sdb_fe_sock_add_listener(sock, listen_addresses[i])) + break; + + /* break on error */ + if (i >= listen_addresses_num) + sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop); + + sdb_log(SDB_LOG_INFO, "Waiting for backend thread to terminate"); + plugin_main_loop.do_loop = 0; + pthread_kill(backend_thread, SIGINT); + pthread_join(backend_thread, NULL); + sdb_fe_sock_destroy(sock); + } sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA" (pid %i)", (int)getpid()); - - fprintf(stderr, "Store dump:\n"); - sdb_store_dump(stderr); return 0; } /* main */