Code

daemon: Added "Listen" config option.
authorSebastian Harl <sh@tokkee.org>
Tue, 22 Oct 2013 06:54:20 +0000 (08:54 +0200)
committerSebastian 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.

src/Makefile.am
src/daemon/config.c
src/daemon/sysdbd.c
src/include/daemon/config.h

index c70d64e2702ec8bdcc871a2a8f43508649630e39..de2f5cf642ef30c1eba8434256ea96bb89a66b5f 100644 (file)
@@ -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
index 91746b10267cab4e304f0f1eb3a98b64a6c8eb0d..beb0217baa323d5bbd17aff02891aeaed73a5ea7 100644 (file)
 #include "liboconfig/utils.h"
 
 #include <assert.h>
+#include <errno.h>
+
+#include <stdlib.h>
+#include <string.h>
 #include <strings.h>
 
 /*
@@ -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 },
index 98d3d26ce52cce93050a49ceb6be053ba202ae91..6e75c039bb21f1f5761df3f1e363b13b8f7136fe 100644 (file)
@@ -34,6 +34,8 @@
 #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
@@ -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());
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.