summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1e227f7)
raw | patch | inline | side by side (parent: 1e227f7)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 22 Oct 2013 06:54:20 +0000 (08:54 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 22 Oct 2013 06:54:20 +0000 (08:54 +0200) |
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.
through the frontend socket module. The backend collector loop now runs in its
own background thread.
src/Makefile.am | patch | blob | history | |
src/daemon/config.c | patch | blob | history | |
src/daemon/sysdbd.c | patch | blob | history | |
src/include/daemon/config.h | patch | blob | history |
diff --git a/src/Makefile.am b/src/Makefile.am
index c70d64e2702ec8bdcc871a2a8f43508649630e39..de2f5cf642ef30c1eba8434256ea96bb89a66b5f 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
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 91746b10267cab4e304f0f1eb3a98b64a6c8eb0d..beb0217baa323d5bbd17aff02891aeaed73a5ea7 100644 (file)
--- a/src/daemon/config.c
+++ b/src/daemon/config.c
#include "liboconfig/utils.h"
#include <assert.h>
+#include <errno.h>
+
+#include <stdlib.h>
+#include <string.h>
#include <strings.h>
/*
return 0;
} /* config_get_interval */
+/*
+ * public parse results
+ */
+
+char **listen_addresses = NULL;
+size_t listen_addresses_num = 0;
+
/*
* token parser
*/
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)
{
} /* 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 98d3d26ce52cce93050a49ceb6be053ba202ae91..6e75c039bb21f1f5761df3f1e363b13b8f7136fe 100644 (file)
--- a/src/daemon/sysdbd.c
+++ b/src/daemon/sysdbd.c
#include "core/store.h"
#include "core/error.h"
+#include "frontend/sock.h"
+
#include "daemon/config.h"
#if HAVE_LIBGEN_H
#include <unistd.h>
+#include <pthread.h>
+
#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
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;
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;
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());
index e249bfe1971be86adece26bb0fe3af9787b0cfeb..0fe278d731ccd3e639f294506e02eafeb0743a7d 100644 (file)
#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.