Code

Added framework for frontend connection handling.
[sysdb.git] / src / daemon / sysdbd.c
index d4851f8e42f123fa71dbbceae972fb0a24897390..98d3d26ce52cce93050a49ceb6be053ba202ae91 100644 (file)
@@ -32,8 +32,7 @@
 #include "sysdb.h"
 #include "core/plugin.h"
 #include "core/store.h"
-#include "utils/error.h"
-#include "utils/string.h"
+#include "core/error.h"
 
 #include "daemon/config.h"
 
@@ -109,7 +108,7 @@ daemonize(void)
 
        if ((pid = fork()) < 0) {
                char errbuf[1024];
-               sdb_error_set(SDB_LOG_ERR, "Failed to fork to background: %s\n",
+               sdb_log(SDB_LOG_ERR, "Failed to fork to background: %s",
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return errno;
        }
@@ -120,8 +119,8 @@ daemonize(void)
 
        if (chdir("/")) {
                char errbuf[1024];
-               sdb_error_set(SDB_LOG_ERR, "Failed to change working directory to "
-                               "the root directory: %s\n",
+               sdb_log(SDB_LOG_ERR, "Failed to change working directory to "
+                               "the root directory: %s",
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return errno;
        }
@@ -132,24 +131,24 @@ daemonize(void)
        close(0);
        if (open("/dev/null", O_RDWR)) {
                char errbuf[1024];
-               sdb_error_set(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': "
-                               "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               sdb_log(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': %s",
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return errno;
        }
 
        close(1);
        if (dup(0) != 1) {
                char errbuf[1024];
-               sdb_error_set(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': "
-                               "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               sdb_log(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': %s",
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return errno;
        }
 
        close(2);
        if (dup(0) != 2) {
                char errbuf[1024];
-               sdb_error_set(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': "
-                               "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               sdb_log(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': %s",
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return errno;
        }
        return 0;
@@ -162,6 +161,7 @@ main(int argc, char **argv)
        _Bool do_daemonize = 0;
 
        struct sigaction sa_intterm;
+       int status;
 
        while (42) {
                int opt = getopt(argc, argv, "C:dhV");
@@ -194,8 +194,12 @@ main(int argc, char **argv)
        if (! config_filename)
                config_filename = CONFIGFILE;
 
-       if (daemon_parse_config(config_filename)) {
-               sdb_error_set(SDB_LOG_ERR, "Failed to parse configuration file.\n");
+       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);
        }
 
@@ -205,14 +209,14 @@ main(int argc, char **argv)
 
        if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) {
                char errbuf[1024];
-               sdb_error_set(SDB_LOG_ERR, "Failed to install signal handler for "
-                               "SIGINT: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
+                               "SIGINT: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
                exit(1);
        }
        if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) {
                char errbuf[1024];
-               sdb_error_set(SDB_LOG_ERR, "Failed to install signal handler for "
-                               "SIGTERM: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               sdb_log(SDB_LOG_ERR, "Failed to install signal handler for "
+                               "SIGTERM: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
                exit(1);
        }
 
@@ -220,15 +224,16 @@ main(int argc, char **argv)
                if (daemonize())
                        exit(1);
 
-       sdb_error_set(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
-                       SDB_VERSION_EXTRA " (pid %i) initialized successfully\n",
+       sdb_log(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING
+                       SDB_VERSION_EXTRA " (pid %i) initialized successfully",
                        (int)getpid());
 
        sdb_plugin_init_all();
+       plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60);
        sdb_plugin_collector_loop(&plugin_main_loop);
 
-       sdb_error_set(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
-                       SDB_VERSION_EXTRA" (pid %i)\n", (int)getpid());
+       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);