Code

daemon: Added "Listen" config option.
[sysdb.git] / src / daemon / sysdbd.c
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());