Code

daemon: Treat parser errors different from daemon/libsysdb errors.
authorSebastian Harl <sh@tokkee.org>
Wed, 2 Oct 2013 19:39:13 +0000 (21:39 +0200)
committerSebastian Harl <sh@tokkee.org>
Wed, 2 Oct 2013 19:39:13 +0000 (21:39 +0200)
src/daemon/config.c
src/daemon/sysdbd.c
src/include/daemon/config.h

index d15081f469446ee1f3b3214b6dbe8efcdf6e7253..91746b10267cab4e304f0f1eb3a98b64a6c8eb0d 100644 (file)
 #include <assert.h>
 #include <strings.h>
 
+/*
+ * Parser error values:
+ *  - Values less than zero indicate an error in the daemon or libsysdb.
+ *  - Zero indicates success.
+ *  - Any other values indicate parsing errors.
+ */
+
+enum {
+       ERR_UNKNOWN_OPTION = 1,
+       ERR_UNKNOWN_ARG    = 2,
+       ERR_INVALID_ARG    = 3,
+       ERR_PARSE_FAILED   = 4,
+};
+
 /*
  * private variables
  */
@@ -59,14 +73,14 @@ config_get_interval(oconfig_item_t *ci, sdb_time_t *interval)
                sdb_log(SDB_LOG_ERR, "config: Interval requires "
                                "a single numeric argument\n"
                                "\tUsage: Interval SECONDS");
-               return -1;
+               return ERR_INVALID_ARG;
        }
 
        if (interval_dbl <= 0.0) {
                sdb_log(SDB_LOG_ERR, "config: Invalid interval: %f\n"
                                "\tInterval may not be less than or equal to zero.",
                                interval_dbl);
-               return -1;
+               return ERR_INVALID_ARG;
        }
 
        *interval = DOUBLE_TO_SDB_TIME(interval_dbl);
@@ -98,7 +112,7 @@ daemon_load_plugin(oconfig_item_t *ci)
                sdb_log(SDB_LOG_ERR, "config: LoadPlugin requires a single "
                                "string argument\n"
                                "\tUsage: LoadPlugin PLUGIN");
-               return -1;
+               return ERR_INVALID_ARG;
        }
 
        for (i = 0; i < ci->children_num; ++i) {
@@ -111,6 +125,7 @@ daemon_load_plugin(oconfig_item_t *ci)
                continue;
        }
 
+       /* returns a negative value on error */
        return sdb_plugin_load(name, NULL);
 } /* daemon_load_plugin */
 
@@ -130,7 +145,7 @@ daemon_load_backend(oconfig_item_t *ci)
                sdb_log(SDB_LOG_ERR, "config: LoadBackend requires a single "
                                "string argument\n"
                                "\tUsage: LoadBackend BACKEND");
-               return -1;
+               return ERR_INVALID_ARG;
        }
 
        snprintf(plugin_name, sizeof(plugin_name), "backend::%s", name);
@@ -140,7 +155,7 @@ daemon_load_backend(oconfig_item_t *ci)
 
                if (! strcasecmp(child->key, "Interval")) {
                        if (config_get_interval(child, &ctx.interval))
-                               return -1;
+                               return ERR_INVALID_ARG;
                }
                else {
                        sdb_log(SDB_LOG_WARNING, "config: Unknown option '%s' "
@@ -165,7 +180,7 @@ daemon_configure_plugin(oconfig_item_t *ci)
                                "string argument\n"
                                "\tUsage: LoadBackend BACKEND",
                                ci->key);
-               return -1;
+               return ERR_INVALID_ARG;
        }
 
        return sdb_plugin_configure(name, ci);
@@ -192,11 +207,11 @@ daemon_parse_config(const char *filename)
 
        ci = oconfig_parse_file(filename);
        if (! ci)
-               return -1;
+               return ERR_PARSE_FAILED;
 
        for (i = 0; i < ci->children_num; ++i) {
                oconfig_item_t *child = ci->children + i;
-               int status = 1, j;
+               int status = ERR_UNKNOWN_OPTION, j;
 
                for (j = 0; token_parser_list[j].name; ++j) {
                        if (! strcasecmp(token_parser_list[j].name, child->key))
@@ -206,13 +221,13 @@ daemon_parse_config(const char *filename)
                if (status) {
                        sdb_error_set("config: Failed to parse option '%s'\n",
                                        child->key);
-                       if (status > 0)
+                       if (status == ERR_UNKNOWN_OPTION)
                                sdb_error_append("\tUnknown option '%s' -- "
                                                "see the documentation for details\n",
                                                child->key);
                        sdb_error_chomp();
                        sdb_error_log(SDB_LOG_ERR);
-                       retval = -1;
+                       retval = status;
                }
        }
        return retval;
index f1992610f2a2164af9d189c8f1324ea6a3265daf..98d3d26ce52cce93050a49ceb6be053ba202ae91 100644 (file)
@@ -161,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");
@@ -193,8 +194,12 @@ main(int argc, char **argv)
        if (! config_filename)
                config_filename = CONFIGFILE;
 
-       if (daemon_parse_config(config_filename)) {
-               sdb_log(SDB_LOG_ERR, "Failed to parse configuration file.");
+       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);
        }
 
index 6d4b0d3bac074ffd1bc748216dcfbc3a56e0080e..e249bfe1971be86adece26bb0fe3af9787b0cfeb 100644 (file)
 #ifndef DAEMON_CONFIG_H
 #define DAEMON_CONFIG_H 1
 
+/*
+ * daemon_parse_config:
+ * Parse the specified configuration file.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value when loading the configuration failed because of errors
+ *    in the daemon or libsysdb
+ *  - a positive value on parser errors
+ */
 int
 daemon_parse_config(const char *filename);