Code

Renamed error recording / logging functions.
[sysdb.git] / src / daemon / config.c
1 /*
2  * SysDB - src/daemon/config.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "sysdb.h"
29 #include "core/plugin.h"
30 #include "utils/error.h"
31 #include "utils/time.h"
33 #include "daemon/config.h"
35 #include "liboconfig/oconfig.h"
36 #include "liboconfig/utils.h"
38 #include <assert.h>
39 #include <strings.h>
41 /*
42  * private variables
43  */
45 static sdb_time_t default_interval = 0;
47 /*
48  * private helper functions
49  */
51 static int
52 config_get_interval(oconfig_item_t *ci, sdb_time_t *interval)
53 {
54         double interval_dbl = 0.0;
56         assert(ci && interval);
58         if (oconfig_get_number(ci, &interval_dbl)) {
59                 sdb_log(SDB_LOG_ERR, "config: Interval requires "
60                                 "a single numeric argument\n"
61                                 "\tUsage: Interval SECONDS\n");
62                 return -1;
63         }
65         if (interval_dbl <= 0.0) {
66                 sdb_log(SDB_LOG_ERR, "config: Invalid interval: %f\n"
67                                 "\tInterval may not be less than or equal to zero.\n",
68                                 interval_dbl);
69                 return -1;
70         }
72         *interval = DOUBLE_TO_SDB_TIME(interval_dbl);
73         return 0;
74 } /* config_get_interval */
76 /*
77  * token parser
78  */
80 typedef struct {
81         char *name;
82         int (*dispatcher)(oconfig_item_t *);
83 } token_parser_t;
85 static int
86 daemon_set_interval(oconfig_item_t *ci)
87 {
88         return config_get_interval(ci, &default_interval);
89 } /* daemon_set_interval */
91 static int
92 daemon_load_backend(oconfig_item_t *ci)
93 {
94         char  plugin_name[1024];
95         char *name;
97         sdb_plugin_ctx_t ctx = SDB_PLUGIN_CTX_INIT;
98         sdb_plugin_ctx_t old_ctx;
100         int status, i;
102         ctx.interval = default_interval;
104         if (oconfig_get_string(ci, &name)) {
105                 sdb_log(SDB_LOG_ERR, "config: LoadBackend requires a single "
106                                 "string argument\n"
107                                 "\tUsage: LoadBackend BACKEND\n");
108                 return -1;
109         }
111         snprintf(plugin_name, sizeof(plugin_name), "backend/%s", name);
113         for (i = 0; i < ci->children_num; ++i) {
114                 oconfig_item_t *child = ci->children + i;
116                 if (! strcasecmp(child->key, "Interval")) {
117                         if (config_get_interval(child, &ctx.interval))
118                                 return -1;
119                 }
120                 else {
121                         sdb_log(SDB_LOG_WARNING, "config: Unknown option '%s' "
122                                         "inside 'LoadBackend' -- see the documentation for "
123                                         "details.\n", child->key);
124                         continue;
125                 }
126         }
128         old_ctx = sdb_plugin_set_ctx(ctx);
129         status = sdb_plugin_load(plugin_name);
130         sdb_plugin_set_ctx(old_ctx);
131         return status;
132 } /* daemon_load_backend */
134 static int
135 daemon_configure_plugin(oconfig_item_t *ci)
137         char *name;
139         assert(ci);
141         if (oconfig_get_string(ci, &name)) {
142                 sdb_log(SDB_LOG_ERR, "config: %s requires a single "
143                                 "string argument\n"
144                                 "\tUsage: LoadBackend BACKEND\n",
145                                 ci->key);
146                 return -1;
147         }
149         return sdb_plugin_configure(name, ci);
150 } /* daemon_configure_backend */
152 static token_parser_t token_parser_list[] = {
153         { "Interval", daemon_set_interval },
154         { "LoadBackend", daemon_load_backend },
155         { "Backend", daemon_configure_plugin },
156         { "Plugin", daemon_configure_plugin },
157         { NULL, NULL },
158 };
160 /*
161  * public API
162  */
164 int
165 daemon_parse_config(const char *filename)
167         oconfig_item_t *ci;
168         int retval = 0, i;
170         ci = oconfig_parse_file(filename);
171         if (! ci)
172                 return -1;
174         for (i = 0; i < ci->children_num; ++i) {
175                 oconfig_item_t *child = ci->children + i;
176                 int status = 1, j;
178                 for (j = 0; token_parser_list[j].name; ++j) {
179                         if (! strcasecmp(token_parser_list[j].name, child->key))
180                                 status = token_parser_list[j].dispatcher(child);
181                 }
183                 if (status) {
184                         sdb_error_set("config: Failed to parse option '%s'\n",
185                                         child->key);
186                         if (status > 0)
187                                 sdb_error_append("\tUnknown option '%s' -- "
188                                                 "see the documentation for details\n",
189                                                 child->key);
190                         sdb_error_log(SDB_LOG_ERR);
191                         retval = -1;
192                 }
193         }
194         return retval;
195 } /* daemon_parse_config */
197 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */