X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=src%2Fconfig.c;h=d76f20c9e0b27ef70bb9cfb0fb972e09fbde3013;hb=471ddd68074c546156a01216fe2b99a3a7e64a04;hp=c2fdb426a72da2f77c2fa6224f30745d20509a7f;hpb=037bbc245f57e45f9e6f56c705957e38586d7218;p=netdata.git diff --git a/src/config.c b/src/config.c index c2fdb426..d76f20c9 100755 --- a/src/config.c +++ b/src/config.c @@ -1,40 +1,105 @@ #include #include #include +#include +#include "avl.h" #include "common.h" #include "config.h" #include "log.h" -#define CONFIG_FILE_LINE_MAX 4096 +#define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2) pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER; +// ---------------------------------------------------------------------------- +// definitions + +#define CONFIG_VALUE_LOADED 0x01 // has been loaded from the config +#define CONFIG_VALUE_USED 0x02 // has been accessed from the program +#define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value +#define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default + struct config_value { - char name[CONFIG_MAX_NAME + 1]; - char value[CONFIG_MAX_VALUE + 1]; + avl avl; // the index - this has to be first! - unsigned long hash; // a simple hash to speed up searching + 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 - int loaded; // loaded from the user config - int used; // has been accessed from the program - int changed; // changed from the internal default + char *name; + char *value; + + uint8_t flags; struct config_value *next; }; struct config { - char name[CONFIG_MAX_NAME + 1]; + avl avl; - unsigned long hash; // a simple hash to speed up searching + 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; + struct config_value *values; + avl_tree values_index; struct config *next; } *config_root = NULL; + +// ---------------------------------------------------------------------------- +// config value + +static int config_value_iterator(avl *a) { if(a) {}; return 0; } + +static int config_value_compare(void* a, void* b) { + if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1; + else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1; + 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)) + +static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) { + struct config_value *result = NULL, 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; +} + +// ---------------------------------------------------------------------------- +// config + +static int config_iterator(avl *a) { if(a) {}; return 0; } + +static int config_compare(void* a, void* b) { + if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1; + else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1; + else return strcmp(((struct config *)a)->name, ((struct config *)b)->name); +} + +avl_tree config_root_index = { + NULL, + config_compare +}; + +#define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg)) +#define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg)) + +static struct config *config_index_find(const char *name, uint32_t hash) { + struct config *result = NULL, 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; +} + 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); @@ -42,10 +107,15 @@ struct config_value *config_value_create(struct config *co, const char *name, co struct config_value *cv = calloc(1, sizeof(struct config_value)); if(!cv) fatal("Cannot allocate config_value"); - strncpy(cv->name, name, CONFIG_MAX_NAME); - strncpy(cv->value, value, CONFIG_MAX_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() struct config_value *cv2 = co->values; @@ -65,9 +135,14 @@ struct config *config_create(const char *section) struct config *co = calloc(1, sizeof(struct config)); if(!co) fatal("Cannot allocate config"); - strncpy(co->name, section, CONFIG_MAX_NAME); + co->name = strdup(section); + if(!co->name) fatal("Cannot allocate config.name"); co->hash = simple_hash(co->name); + co->values_index.compar = config_value_compare; + + config_index_add(co); + // no need for string termination, due to calloc() struct config *co2 = config_root; @@ -82,15 +157,7 @@ struct config *config_create(const char *section) struct config *config_find_section(const char *section) { - struct config *co; - unsigned long hash = simple_hash(section); - - for(co = config_root; co ; co = co->next) - if(hash == co->hash) - if(strcmp(co->name, section) == 0) - break; - - return co; + return config_index_find(section, 0); } int load_config(char *filename, int overwrite_used) @@ -159,21 +226,20 @@ int load_config(char *filename, int overwrite_used) continue; } - struct config_value *cv; - for(cv = co->values; cv ; cv = cv->next) - if(strcmp(cv->name, name) == 0) break; + struct config_value *cv = config_value_index_find(co, name, 0); if(!cv) cv = config_value_create(co, name, value); else { - if((cv->used && overwrite_used) || !cv->used) { + if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) { debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name); - strncpy(cv->value, value, CONFIG_MAX_VALUE); - // termination is already there + 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->loaded = 1; + cv->flags |= CONFIG_VALUE_LOADED; } fclose(fp); @@ -193,24 +259,20 @@ char *config_get(const char *section, const char *name, const char *default_valu struct config *co = config_find_section(section); if(!co) co = config_create(section); - unsigned long hash = simple_hash(name); - for(cv = co->values; cv ; cv = cv->next) - if(hash == cv->hash) - if(strcmp(cv->name, name) == 0) - break; - - if(!cv) cv = config_value_create(co, name, default_value); - cv->used = 1; + cv = config_value_index_find(co, name, 0); + if(!cv) { + cv = config_value_create(co, name, default_value); + if(!cv) return NULL; + } + cv->flags |= CONFIG_VALUE_USED; - if(cv->loaded || cv->changed) { + if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) { // this is a loaded value from the config file // if it is different that the default, mark it - if(strcmp(cv->value, default_value) != 0) cv->changed = 1; - } - else { - // this is not loaded from the config - // copy the default value to it - strncpy(cv->value, default_value, CONFIG_MAX_VALUE); + if(!(cv->flags & CONFIG_VALUE_CHECKED)) { + if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED; + cv->flags |= CONFIG_VALUE_CHECKED; + } } pthread_rwlock_unlock(&config_rwlock); @@ -223,6 +285,8 @@ long long config_get_number(const char *section, const char *name, long long val sprintf(buffer, "%lld", value); s = config_get(section, name, buffer); + if(!s) return 0; + return strtoll(s, NULL, 0); } @@ -233,15 +297,10 @@ int config_get_boolean(const char *section, const char *name, int value) else s = "no"; s = config_get(section, name, s); + if(!s) return 0; - if(strcmp(s, "yes") == 0 || strcmp(s, "true") == 0 || strcmp(s, "1") == 0) { - strcpy(s, "yes"); - return 1; - } - else { - strcpy(s, "no"); - return 0; - } + if(!strcmp(s, "yes")) return 1; + else return 0; } const char *config_set(const char *section, const char *name, const char *value) @@ -255,19 +314,15 @@ const char *config_set(const char *section, const char *name, const char *value) struct config *co = config_find_section(section); if(!co) co = config_create(section); - unsigned long hash = simple_hash(name); - for(cv = co->values; cv ; cv = cv->next) - if(hash == cv->hash) - if(strcmp(cv->name, name) == 0) - break; - + cv = config_value_index_find(co, name, 0); if(!cv) cv = config_value_create(co, name, value); - cv->used = 1; + cv->flags |= CONFIG_VALUE_USED; - if(strcmp(cv->value, value) != 0) cv->changed = 1; + if(strcmp(cv->value, value) != 0) cv->flags |= CONFIG_VALUE_CHANGED; - strncpy(cv->value, value, CONFIG_MAX_VALUE); - // termination is already there + free(cv->value); + cv->value = strdup(value); + if(!cv->value) fatal("Cannot allocate config.value"); pthread_rwlock_unlock(&config_rwlock); @@ -307,7 +362,7 @@ void generate_config(struct web_buffer *wb, int only_changed) case 0: web_buffer_printf(wb, "# NetData Configuration\n" - "# You can uncomment and change any of the options bellow.\n" + "# You can uncomment and change any of the options below.\n" "# The value shown in the commented settings, is the default value.\n" "\n# global netdata configuration\n"); break; @@ -331,8 +386,8 @@ void generate_config(struct web_buffer *wb, int only_changed) int changed = 0; int count = 0; for(cv = co->values; cv ; cv = cv->next) { - used += cv->used; - changed += cv->changed; + used += (cv->flags && CONFIG_VALUE_USED)?1:0; + changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0; count++; } @@ -344,17 +399,17 @@ void generate_config(struct web_buffer *wb, int only_changed) web_buffer_printf(wb, "\n# node '%s' is not used.", co->name); } - web_buffer_increase(wb, CONFIG_MAX_NAME + 4); + web_buffer_increase(wb, CONFIG_FILE_LINE_MAX+1); web_buffer_printf(wb, "\n[%s]\n", co->name); for(cv = co->values; cv ; cv = cv->next) { - if(used && !cv->used) { - web_buffer_increase(wb, CONFIG_MAX_NAME + 200); + if(used && !(cv->flags & CONFIG_VALUE_USED)) { + web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1); web_buffer_printf(wb, "\n\t# option '%s' is not used.\n", cv->name); } - web_buffer_increase(wb, CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 5); - web_buffer_printf(wb, "\t%s%s = %s\n", (!cv->changed && cv->used)?"# ":"", cv->name, cv->value); + web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1); + web_buffer_printf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value); } } }