diff --git a/common/debug.c b/common/debug.c index 1605767..94fe59f 100644 --- a/common/debug.c +++ b/common/debug.c @@ -21,14 +21,14 @@ static int _debug = 0; -inline void +void dset(int threshold) { _debug = threshold; dbg_printf(3, "Debugging threshold is now %d\n", threshold); } -inline int +int dget(void) { return _debug; diff --git a/config/config-stack.h b/config/config-stack.h index 3781cd9..ebc7f7c 100644 --- a/config/config-stack.h +++ b/config/config-stack.h @@ -29,4 +29,9 @@ extern struct value *val_list; extern struct node *node_list; extern struct parser_context *context_stack; +int _sc_value_add(char *id, char *val, struct value **list); +int _sc_node_add(char *id, struct value *vallist, struct node *nodelist, + struct node **list); + + #endif diff --git a/config/config.l b/config/config.l index a077ec7..bc4ce1d 100644 --- a/config/config.l +++ b/config/config.l @@ -90,11 +90,15 @@ main(int argc, char *argv[]) c = sc_init(); sc_parse(c, NULL); sc_dump(c, stdout); - if (argc >= 2) { + if (argc == 2) { if (sc_get(c, argv[1], value, sizeof(value)) == 0) printf("%s = %s\n", argv[1], value); else printf("Not found\n"); + } else if (argc == 3) { + printf("---------\n"); + if (sc_set(c, argv[1], argv[2]) == 0) + sc_dump(c, stdout); } sc_release(c); diff --git a/config/config.y b/config/config.y index eb4dea7..e2013e8 100644 --- a/config/config.y +++ b/config/config.y @@ -8,8 +8,8 @@ extern int yylex (void); int yyerror(const char *foo); -static int -value_add(char *id, char *val, struct value **list) +int +_sc_value_add(char *id, char *val, struct value **list) { struct value *v; @@ -31,8 +31,8 @@ value_add(char *id, char *val, struct value **list) } -static int -node_add(char *id, struct value *vallist, struct node *nodelist, +int +_sc_node_add(char *id, struct value *vallist, struct node *nodelist, struct node **list) { struct node *n; @@ -72,7 +72,7 @@ node: struct parser_context *c = NULL; c = context_stack; - node_add($1, val_list, node_list, &c->node_list); + _sc_node_add($1, val_list, node_list, &c->node_list); val_list = c->val_list; node_list = c->node_list; context_stack = c->next; @@ -84,7 +84,7 @@ node: struct parser_context *c = NULL; c = context_stack; - node_add($1, val_list, node_list, &c->node_list); + _sc_node_add($1, val_list, node_list, &c->node_list); val_list = c->val_list; node_list = c->node_list; context_stack = c->next; @@ -99,7 +99,7 @@ stuff: assign: T_ID T_EQ T_VAL T_SEMI { - value_add($1, $3, &val_list); + _sc_value_add($1, $3, &val_list); } ; %% diff --git a/config/simpleconfig.c b/config/simpleconfig.c index f358d91..635e2bf 100644 --- a/config/simpleconfig.c +++ b/config/simpleconfig.c @@ -180,15 +180,16 @@ _sc_get(config_info_t *config, const char *key, char *value, size_t valuesz) return 1; } - if (ptr[0] == '@') { - ++ptr; - found = 0; + if (ptr[0] != '@') + return 1; - for (v = values; v; v = v->next) { - if (!strcasecmp(v->id, ptr)) { - snprintf(value, valuesz, "%s", v->val); - return 0; - } + ++ptr; + found = 0; + + for (v = values; v; v = v->next) { + if (!strcasecmp(v->id, ptr)) { + snprintf(value, valuesz, "%s", v->val); + return 0; } } @@ -196,6 +197,76 @@ _sc_get(config_info_t *config, const char *key, char *value, size_t valuesz) } +static int +_sc_set(config_info_t *config, const char *key, const char *value) +{ + char buf[1024]; + struct node *n, **nodes = &((struct parser_context *)config)->node_list; + struct value *v, **values = &((struct parser_context *)config)->val_list; + char *ptr; + char *slash; + char *id_dup, *val_dup; + int found = 0; + + ptr = (char *)key; + while ((slash = strchr(ptr, '/'))) { + memset(buf, 0, sizeof(buf)); + strncpy(buf, ptr, (slash - ptr)); + ptr = ++slash; + found = 0; + + for (n = *nodes; n; n = n->next) { + if (strcasecmp(n->id, buf)) + continue; + + nodes = &n->nodes; + values = &n->values; + found = 1; + break; + } + + if (!found) { + id_dup = strdup(buf); + if (!id_dup) + return -1; + _sc_node_add(id_dup, NULL, NULL, nodes); + n = *nodes; + nodes = &n->nodes; + values = &n->values; + } + } + + if (ptr[0] != '@') + return 1; + ++ptr; + + for (v = *values; v; v = v->next) { + if (strcasecmp(v->id, ptr)) + continue; + + ptr = v->val; + v->val = strdup(value); + if (!v->val) { + v->val = ptr; + return -1; + } + free(ptr); + + return 0; + } + + id_dup = strdup(ptr); + if (!id_dup) + return -1; + val_dup = strdup(value); + if (!val_dup) + return -1; + _sc_value_add(id_dup, val_dup, values); + + return 0; +} + + static int _sc_parse(const char *filename, config_info_t **config) { @@ -239,6 +310,7 @@ _sc_parse(const char *filename, config_info_t **config) static const config_object_t sc_object = { .get = _sc_get, + .set = _sc_set, .parse = _sc_parse, .free = _sc_free, .dump = _sc_dump, diff --git a/config/simpleconfig.h b/config/simpleconfig.h deleted file mode 100644 index 6a65b3b..0000000 --- a/config/simpleconfig.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef _SIMPLECONFIG_H -#define _SIMPLECONFIG_H - -typedef void config_info_t; - -typedef int (*config_get_t)(config_info_t *config, const char *key, - char *value, size_t valuesz); -typedef int (*config_parse_t)(const char *filename, config_info_t **config); -typedef int (*config_free_t)(config_info_t *config); -typedef void (*config_dump_t)(config_info_t *config, FILE *fp); - -/* - * We use an abstract object here so we do not have to link loadable - * modules against the configuration library. - */ - -typedef struct { - config_get_t get; - config_parse_t parse; - config_free_t free; - config_dump_t dump; - config_info_t *info; -} config_object_t; - -/* - * These macros may be called from within a loadable module - */ -#define sc_get(obj, key, value, valuesz) \ - obj->get(obj->info, key, value, valuesz) -#define sc_parse(obj, filename) \ - obj->parse(filename, &obj->info) -#define sc_free(obj) \ - obj->free(obj->info) -#define sc_dump(obj, fp) \ - obj->dump(obj->info, fp) - -/* - * Do not call the below functions from loadable modules. Doing so - * requires linking the configuration library in to the modules, which - * is what we want to avoid. - */ - -/* Returns a copy of our simple config object */ -config_object_t *sc_init(void); - -/* Frees a previously-allocated copy of our simple config object */ -void sc_release(config_object_t *c); - -#endif diff --git a/include/simpleconfig.h b/include/simpleconfig.h index 6a65b3b..ada10b4 100644 --- a/include/simpleconfig.h +++ b/include/simpleconfig.h @@ -5,6 +5,8 @@ typedef void config_info_t; typedef int (*config_get_t)(config_info_t *config, const char *key, char *value, size_t valuesz); +typedef int (*config_set_t)(config_info_t *config, const char *key, + const char *value); typedef int (*config_parse_t)(const char *filename, config_info_t **config); typedef int (*config_free_t)(config_info_t *config); typedef void (*config_dump_t)(config_info_t *config, FILE *fp); @@ -16,6 +18,7 @@ typedef void (*config_dump_t)(config_info_t *config, FILE *fp); typedef struct { config_get_t get; + config_set_t set; config_parse_t parse; config_free_t free; config_dump_t dump; @@ -27,6 +30,8 @@ typedef struct { */ #define sc_get(obj, key, value, valuesz) \ obj->get(obj->info, key, value, valuesz) +#define sc_set(obj, key, value) \ + obj->set(obj->info, key, value) #define sc_parse(obj, filename) \ obj->parse(filename, &obj->info) #define sc_free(obj) \ diff --git a/server/libvirt.c b/server/libvirt.c index 5fff382..97a2914 100644 --- a/server/libvirt.c +++ b/server/libvirt.c @@ -346,8 +346,12 @@ libvirt_init(backend_context_t *c, config_object_t *config) if (!info) return -1; + dbg_printf(5, "[%s:%d %s]\n", __FILE__, __LINE__, __FUNCTION__); memset(info, 0, sizeof(*info)); + if (sc_get(config, "fence_virtd/@debug", value, sizeof(value))==0) + dset(atoi(value)); + if (sc_get(config, "backends/libvirt/@uri", value, sizeof(value)) == 0) { uri = strdup(value); diff --git a/server/main.c b/server/main.c index 341be7e..e124216 100644 --- a/server/main.c +++ b/server/main.c @@ -41,8 +41,7 @@ main(int argc, char **argv) config_file = optarg; break; case 'd': - dset(atoi(optarg)); - debug_set = 1; + debug_set = atoi(optarg); break; default: return -1; @@ -54,11 +53,16 @@ main(int argc, char **argv) return -1; } - if (!debug_set) { - if (sc_get(config, "fence_virtd/@debug", - val, sizeof(val)) == 0) - dset(atoi(val)); + if (debug_set) { + snprintf(val, sizeof(val), "%d", debug_set); + sc_set(config, "fence_virtd/@debug", val); + } else { + if (sc_get(config, "fence_virtd/@debug", val, sizeof(val))==0) + debug_set = atoi(val); } + + dset(debug_set); + if (!foreground) { if (sc_get(config, "fence_virtd/@foreground", val, sizeof(val)) == 0) diff --git a/server/mcast.c b/server/mcast.c index ef8f545..49b1fef 100644 --- a/server/mcast.c +++ b/server/mcast.c @@ -343,6 +343,9 @@ mcast_config(config_object_t *config, mcast_options *args) char value[1024]; int errors = 0; + if (sc_get(config, "fence_virtd/@debug", value, sizeof(value))==0) + dset(atoi(value)); + if (sc_get(config, "listeners/multicast/@key_file", value, sizeof(value)-1) == 0) { dbg_printf(1, "Got %s for key_file\n", value); diff --git a/server/null.c b/server/null.c index 16871d3..506365c 100644 --- a/server/null.c +++ b/server/null.c @@ -26,9 +26,6 @@ #include #include -/* Local includes */ -#include "debug.h" - #define NAME "null" #define VERSION "0.8"