From f28c4b0fa75eb288b49a1ee6222693cac7e9c4c0 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 22 Oct 2013 08:54:20 +0200 Subject: [PATCH] daemon: Added "Listen" config option. The main thread listens on all configured addresses and serves requests through the frontend socket module. The backend collector loop now runs in its own background thread. --- src/Makefile.am | 1 + src/daemon/config.c | 47 +++++++++++++++++++++++++++++++++++ src/daemon/sysdbd.c | 49 ++++++++++++++++++++++++++++++++++++- src/include/daemon/config.h | 7 ++++++ 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/src/Makefile.am b/src/Makefile.am index c70d64e..de2f5cf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,6 +3,7 @@ SUBDIRS = liboconfig AM_CFLAGS = @STRICT_CFLAGS@ AM_CPPFLAGS = -Iinclude AM_CPPFLAGS += -DSYSCONFDIR='"${sysconfdir}"' +AM_CPPFLAGS += -DLOCALSTATEDIR='"${localstatedir}"' AM_CPPFLAGS += -DPKGLIBDIR='"${pkglibdir}"' BUILT_SOURCES = include/sysdb.h diff --git a/src/daemon/config.c b/src/daemon/config.c index 91746b1..beb0217 100644 --- a/src/daemon/config.c +++ b/src/daemon/config.c @@ -36,6 +36,10 @@ #include "liboconfig/utils.h" #include +#include + +#include +#include #include /* @@ -87,6 +91,13 @@ config_get_interval(oconfig_item_t *ci, sdb_time_t *interval) return 0; } /* config_get_interval */ +/* + * public parse results + */ + +char **listen_addresses = NULL; +size_t listen_addresses_num = 0; + /* * token parser */ @@ -96,6 +107,41 @@ typedef struct { int (*dispatcher)(oconfig_item_t *); } token_parser_t; +static int +daemon_add_listener(oconfig_item_t *ci) +{ + char **tmp; + char *address; + + if (oconfig_get_string(ci, &address)) { + sdb_log(SDB_LOG_ERR, "config: Listen requires a single " + "string argument\n" + "\tUsage: Listen ADDRESS"); + return ERR_INVALID_ARG; + } + + tmp = realloc(listen_addresses, + (listen_addresses_num + 1) * sizeof(*listen_addresses)); + if (! tmp) { + char buf[1024]; + sdb_log(SDB_LOG_ERR, "config: Failed to allocate memory: %s", + sdb_strerror(errno, buf, sizeof(buf))); + return -1; + } + + tmp[listen_addresses_num] = strdup(address); + if (! tmp[listen_addresses_num]) { + char buf[1024]; + sdb_log(SDB_LOG_ERR, "config: Failed to allocate memory: %s", + sdb_strerror(errno, buf, sizeof(buf))); + return -1; + } + + listen_addresses = tmp; + ++listen_addresses_num; + return 0; +} /* daemon_add_listener */ + static int daemon_set_interval(oconfig_item_t *ci) { @@ -187,6 +233,7 @@ daemon_configure_plugin(oconfig_item_t *ci) } /* daemon_configure_backend */ static token_parser_t token_parser_list[] = { + { "Listen", daemon_add_listener }, { "Interval", daemon_set_interval }, { "LoadPlugin", daemon_load_plugin }, { "LoadBackend", daemon_load_backend }, diff --git a/src/daemon/sysdbd.c b/src/daemon/sysdbd.c index 98d3d26..6e75c03 100644 --- a/src/daemon/sysdbd.c +++ b/src/daemon/sysdbd.c @@ -34,6 +34,8 @@ #include "core/store.h" #include "core/error.h" +#include "frontend/sock.h" + #include "daemon/config.h" #if HAVE_LIBGEN_H @@ -55,16 +57,28 @@ #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 @@ -154,12 +168,21 @@ daemonize(void) return 0; } /* daemonize */ +static void * +backend_handler(void __attribute__((unused)) *data) +{ + sdb_plugin_collector_loop(&plugin_main_loop); + return NULL; +} /* backend_handler */ + int main(int argc, char **argv) { char *config_filename = NULL; _Bool do_daemonize = 0; + pthread_t backend_thread; + struct sigaction sa_intterm; int status; @@ -203,6 +226,11 @@ main(int argc, char **argv) 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; @@ -230,7 +258,26 @@ 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) + sdb_fe_sock_add_listener(sock, listen_addresses[i]); + sdb_fe_sock_listen_and_serve(sock, &frontend_main_loop); + + pthread_join(backend_thread, NULL); + } sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA" (pid %i)", (int)getpid()); diff --git a/src/include/daemon/config.h b/src/include/daemon/config.h index e249bfe..0fe278d 100644 --- a/src/include/daemon/config.h +++ b/src/include/daemon/config.h @@ -28,6 +28,13 @@ #ifndef DAEMON_CONFIG_H #define DAEMON_CONFIG_H 1 +/* + * parse result values + */ + +extern char **listen_addresses; +extern size_t listen_addresses_num; + /* * daemon_parse_config: * Parse the specified configuration file. -- 2.30.2