summaryrefslogtreecommitdiffstats
path: root/src/appconfig.c
diff options
context:
space:
mode:
authorLennart Weller <lhw@ring0.de>2016-05-25 10:36:24 +0000
committerLennart Weller <lhw@ring0.de>2016-05-25 10:36:24 +0000
commitb4f64f72a3e4bf590c60b0cbd6cd365aa1a58542 (patch)
treee6706c727a1fedb44da614453ad3e429a7403a9b /src/appconfig.c
parentImported Upstream version 1.1.0 (diff)
downloadnetdata-b4f64f72a3e4bf590c60b0cbd6cd365aa1a58542.tar.xz
netdata-b4f64f72a3e4bf590c60b0cbd6cd365aa1a58542.zip
Imported Upstream version 1.2.0upstream/1.2.0
Diffstat (limited to 'src/appconfig.c')
-rw-r--r--src/appconfig.c341
1 files changed, 179 insertions, 162 deletions
diff --git a/src/appconfig.c b/src/appconfig.c
index 73b946508..0ec4cad32 100644
--- a/src/appconfig.c
+++ b/src/appconfig.c
@@ -1,3 +1,11 @@
+
+/*
+ * TODO
+ *
+ * 1. Re-write this using DICTIONARY
+ *
+ */
+
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
@@ -12,7 +20,7 @@
#define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
-pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
+pthread_mutex_t config_mutex = PTHREAD_MUTEX_INITIALIZER;
// ----------------------------------------------------------------------------
// definitions
@@ -25,15 +33,14 @@ pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
struct config_value {
avl avl; // the index - this has to be first!
+ uint8_t flags;
uint32_t hash; // a simple hash to speed up searching
// we first compare hashes, and only if the hashes are equal we do string comparisons
char *name;
char *value;
- uint8_t flags;
-
- struct config_value *next;
+ struct config_value *next; // config->mutex protects just this
};
struct config {
@@ -44,19 +51,38 @@ struct config {
char *name;
- struct config_value *values;
- avl_tree values_index;
+ struct config *next; // gloabl config_mutex protects just this
- struct config *next;
+ struct config_value *values;
+ avl_tree_lock values_index;
- pthread_rwlock_t rwlock;
+ pthread_mutex_t mutex; // this locks only the writers, to ensure atomic updates
+ // readers are protected using the rwlock in avl_tree_lock
} *config_root = NULL;
// ----------------------------------------------------------------------------
-// config value
+// locking
+
+static inline void config_global_write_lock(void) {
+ pthread_mutex_lock(&config_mutex);
+}
+
+static inline void config_global_unlock(void) {
+ pthread_mutex_unlock(&config_mutex);
+}
+
+static inline void config_section_write_lock(struct config *co) {
+ pthread_mutex_lock(&co->mutex);
+}
+
+static inline void config_section_unlock(struct config *co) {
+ pthread_mutex_unlock(&co->mutex);
+}
-static int config_value_iterator(avl *a) { if(a) {}; return 0; }
+
+// ----------------------------------------------------------------------------
+// config name-value index
static int config_value_compare(void* a, void* b) {
if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
@@ -64,22 +90,20 @@ static int config_value_compare(void* a, void* b) {
else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name);
}
-#define config_value_index_add(co, cv) avl_insert(&((co)->values_index), (avl *)(cv))
-#define config_value_index_del(co, cv) avl_remove(&((co)->values_index), (avl *)(cv))
+#define config_value_index_add(co, cv) avl_insert_lock(&((co)->values_index), (avl *)(cv))
+#define config_value_index_del(co, cv) avl_remove_lock(&((co)->values_index), (avl *)(cv))
static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
- struct config_value *result = NULL, tmp;
+ struct config_value tmp;
tmp.hash = (hash)?hash:simple_hash(name);
tmp.name = (char *)name;
- avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result);
- return result;
+ return (struct config_value *)avl_search_lock(&(co->values_index), (avl *) &tmp);
}
-// ----------------------------------------------------------------------------
-// config
-static int config_iterator(avl *a) { if(a) {}; return 0; }
+// ----------------------------------------------------------------------------
+// config sections index
static int config_compare(void* a, void* b) {
if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
@@ -87,61 +111,31 @@ static int config_compare(void* a, void* b) {
else return strcmp(((struct config *)a)->name, ((struct config *)b)->name);
}
-avl_tree config_root_index = {
- NULL,
- config_compare,
-#ifdef AVL_LOCK_WITH_MUTEX
- PTHREAD_MUTEX_INITIALIZER
-#else
- PTHREAD_RWLOCK_INITIALIZER
-#endif
+avl_tree_lock config_root_index = {
+ { NULL, config_compare },
+ AVL_LOCK_INITIALIZER
};
-#define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg))
-#define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg))
+#define config_index_add(cfg) avl_insert_lock(&config_root_index, (avl *)(cfg))
+#define config_index_del(cfg) avl_remove_lock(&config_root_index, (avl *)(cfg))
static struct config *config_index_find(const char *name, uint32_t hash) {
- struct config *result = NULL, tmp;
+ struct config tmp;
tmp.hash = (hash)?hash:simple_hash(name);
tmp.name = (char *)name;
- avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
- return result;
+ return (struct config *)avl_search_lock(&config_root_index, (avl *) &tmp);
}
-struct config_value *config_value_create(struct config *co, const char *name, const char *value)
-{
- debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
-
- struct config_value *cv = calloc(1, sizeof(struct config_value));
- if(!cv) fatal("Cannot allocate config_value");
-
- cv->name = strdup(name);
- if(!cv->name) fatal("Cannot allocate config.name");
- cv->hash = simple_hash(cv->name);
-
- cv->value = strdup(value);
- if(!cv->value) fatal("Cannot allocate config.value");
-
- config_value_index_add(co, cv);
-
- // no need for string termination, due to calloc()
- pthread_rwlock_wrlock(&co->rwlock);
-
- struct config_value *cv2 = co->values;
- if(cv2) {
- while (cv2->next) cv2 = cv2->next;
- cv2->next = cv;
- }
- else co->values = cv;
-
- pthread_rwlock_unlock(&co->rwlock);
+// ----------------------------------------------------------------------------
+// config section methods
- return cv;
+static inline struct config *config_section_find(const char *section) {
+ return config_index_find(section, 0);
}
-struct config *config_create(const char *section)
+static inline struct config *config_section_create(const char *section)
{
debug(D_CONFIG, "Creating section '%s'.", section);
@@ -152,114 +146,52 @@ struct config *config_create(const char *section)
if(!co->name) fatal("Cannot allocate config.name");
co->hash = simple_hash(co->name);
- pthread_rwlock_init(&co->rwlock, NULL);
- avl_init(&co->values_index, config_value_compare);
+ avl_init_lock(&co->values_index, config_value_compare);
config_index_add(co);
- // no need for string termination, due to calloc()
-
- pthread_rwlock_wrlock(&config_rwlock);
-
+ config_global_write_lock();
struct config *co2 = config_root;
if(co2) {
while (co2->next) co2 = co2->next;
co2->next = co;
}
else config_root = co;
-
- pthread_rwlock_unlock(&config_rwlock);
+ config_global_unlock();
return co;
}
-struct config *config_find_section(const char *section)
-{
- return config_index_find(section, 0);
-}
-
-int load_config(char *filename, int overwrite_used)
-{
- int line = 0;
- struct config *co = NULL;
- char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
-
- if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
- FILE *fp = fopen(filename, "r");
- if(!fp) {
- error("Cannot open file '%s'", filename);
- return 0;
- }
-
- while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
- buffer[CONFIG_FILE_LINE_MAX] = '\0';
- line++;
-
- s = trim(buffer);
- if(!s) {
- debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
- continue;
- }
-
- int len = (int) strlen(s);
- if(*s == '[' && s[len - 1] == ']') {
- // new section
- s[len - 1] = '\0';
- s++;
-
- co = config_find_section(s);
- if(!co) co = config_create(s);
-
- continue;
- }
+// ----------------------------------------------------------------------------
+// config name-value methods
- if(!co) {
- // line outside a section
- error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
- continue;
- }
+static inline struct config_value *config_value_create(struct config *co, const char *name, const char *value)
+{
+ debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
- char *name = s;
- char *value = strchr(s, '=');
- if(!value) {
- error("Ignoring line %d ('%s'), there is no = in it.", line, s);
- continue;
- }
- *value = '\0';
- value++;
+ struct config_value *cv = calloc(1, sizeof(struct config_value));
+ if(!cv) fatal("Cannot allocate config_value");
- name = trim(name);
- value = trim(value);
+ cv->name = strdup(name);
+ if(!cv->name) fatal("Cannot allocate config.name");
+ cv->hash = simple_hash(cv->name);
- if(!name) {
- error("Ignoring line %d, name is empty.", line);
- continue;
- }
- if(!value) {
- debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
- continue;
- }
+ cv->value = strdup(value);
+ if(!cv->value) fatal("Cannot allocate config.value");
- struct config_value *cv = config_value_index_find(co, name, 0);
+ config_value_index_add(co, cv);
- if(!cv) cv = config_value_create(co, name, value);
- else {
- if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
- debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
- free(cv->value);
- cv->value = strdup(value);
- if(!cv->value) fatal("Cannot allocate config.value");
- }
- else
- debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
- }
- cv->flags |= CONFIG_VALUE_LOADED;
+ config_section_write_lock(co);
+ struct config_value *cv2 = co->values;
+ if(cv2) {
+ while (cv2->next) cv2 = cv2->next;
+ cv2->next = cv;
}
+ else co->values = cv;
+ config_section_unlock(co);
- fclose(fp);
-
- return 1;
+ return cv;
}
char *config_get(const char *section, const char *name, const char *default_value)
@@ -268,8 +200,8 @@ char *config_get(const char *section, const char *name, const char *default_valu
debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
- struct config *co = config_find_section(section);
- if(!co) co = config_create(section);
+ struct config *co = config_section_find(section);
+ if(!co) co = config_section_create(section);
cv = config_value_index_find(co, name, 0);
if(!cv) {
@@ -346,7 +278,7 @@ const char *config_set_default(const char *section, const char *name, const char
debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
- struct config *co = config_find_section(section);
+ struct config *co = config_section_find(section);
if(!co) return config_set(section, name, value);
cv = config_value_index_find(co, name, 0);
@@ -374,8 +306,8 @@ const char *config_set(const char *section, const char *name, const char *value)
debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
- struct config *co = config_find_section(section);
- if(!co) co = config_create(section);
+ struct config *co = config_section_find(section);
+ if(!co) co = config_section_create(section);
cv = config_value_index_find(co, name, 0);
if(!cv) cv = config_value_create(co, name, value);
@@ -413,6 +345,94 @@ int config_set_boolean(const char *section, const char *name, int value)
return value;
}
+
+// ----------------------------------------------------------------------------
+// config load/save
+
+int load_config(char *filename, int overwrite_used)
+{
+ int line = 0;
+ struct config *co = NULL;
+
+ char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
+
+ if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
+ FILE *fp = fopen(filename, "r");
+ if(!fp) {
+ error("Cannot open file '%s'", filename);
+ return 0;
+ }
+
+ while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
+ buffer[CONFIG_FILE_LINE_MAX] = '\0';
+ line++;
+
+ s = trim(buffer);
+ if(!s) {
+ debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
+ continue;
+ }
+
+ int len = (int) strlen(s);
+ if(*s == '[' && s[len - 1] == ']') {
+ // new section
+ s[len - 1] = '\0';
+ s++;
+
+ co = config_section_find(s);
+ if(!co) co = config_section_create(s);
+
+ continue;
+ }
+
+ if(!co) {
+ // line outside a section
+ error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
+ continue;
+ }
+
+ char *name = s;
+ char *value = strchr(s, '=');
+ if(!value) {
+ error("Ignoring line %d ('%s'), there is no = in it.", line, s);
+ continue;
+ }
+ *value = '\0';
+ value++;
+
+ name = trim(name);
+ value = trim(value);
+
+ if(!name) {
+ error("Ignoring line %d, name is empty.", line);
+ continue;
+ }
+ if(!value) {
+ debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
+ continue;
+ }
+
+ struct config_value *cv = config_value_index_find(co, name, 0);
+
+ if(!cv) cv = config_value_create(co, name, value);
+ else {
+ if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
+ debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
+ free(cv->value);
+ cv->value = strdup(value);
+ if(!cv->value) fatal("Cannot allocate config.value");
+ }
+ else
+ debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
+ }
+ cv->flags |= CONFIG_VALUE_LOADED;
+ }
+
+ fclose(fp);
+
+ return 1;
+}
+
void generate_config(BUFFER *wb, int only_changed)
{
int i, pri;
@@ -438,9 +458,9 @@ void generate_config(BUFFER *wb, int only_changed)
break;
}
- pthread_rwlock_wrlock(&config_rwlock);
+ config_global_write_lock();
for(co = config_root; co ; co = co->next) {
- if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0) pri = 0;
+ if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0 || strcmp(co->name, "registry") == 0) pri = 0;
else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
else pri = 2;
@@ -449,15 +469,13 @@ void generate_config(BUFFER *wb, int only_changed)
int changed = 0;
int count = 0;
- pthread_rwlock_wrlock(&co->rwlock);
-
+ config_section_write_lock(co);
for(cv = co->values; cv ; cv = cv->next) {
- used += (cv->flags && CONFIG_VALUE_USED)?1:0;
+ used += (cv->flags & CONFIG_VALUE_USED)?1:0;
changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
count++;
}
-
- pthread_rwlock_unlock(&co->rwlock);
+ config_section_unlock(co);
if(!count) continue;
if(only_changed && !changed) continue;
@@ -468,7 +486,7 @@ void generate_config(BUFFER *wb, int only_changed)
buffer_sprintf(wb, "\n[%s]\n", co->name);
- pthread_rwlock_wrlock(&co->rwlock);
+ config_section_write_lock(co);
for(cv = co->values; cv ; cv = cv->next) {
if(used && !(cv->flags & CONFIG_VALUE_USED)) {
@@ -476,10 +494,9 @@ void generate_config(BUFFER *wb, int only_changed)
}
buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
}
- pthread_rwlock_unlock(&co->rwlock);
+ config_section_unlock(co);
}
}
- pthread_rwlock_unlock(&config_rwlock);
+ config_global_unlock();
}
}
-