5 * 1. Re-write this using DICTIONARY
18 #include "appconfig.h"
21 #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
23 pthread_mutex_t config_mutex = PTHREAD_MUTEX_INITIALIZER;
25 // ----------------------------------------------------------------------------
28 #define CONFIG_VALUE_LOADED 0x01 // has been loaded from the config
29 #define CONFIG_VALUE_USED 0x02 // has been accessed from the program
30 #define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value
31 #define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default
34 avl avl; // the index - this has to be first!
37 uint32_t hash; // a simple hash to speed up searching
38 // we first compare hashes, and only if the hashes are equal we do string comparisons
43 struct config_value *next; // config->mutex protects just this
49 uint32_t hash; // a simple hash to speed up searching
50 // we first compare hashes, and only if the hashes are equal we do string comparisons
54 struct config *next; // gloabl config_mutex protects just this
56 struct config_value *values;
57 avl_tree_lock values_index;
59 pthread_mutex_t mutex; // this locks only the writers, to ensure atomic updates
60 // readers are protected using the rwlock in avl_tree_lock
61 } *config_root = NULL;
64 // ----------------------------------------------------------------------------
67 static inline void config_global_write_lock(void) {
68 pthread_mutex_lock(&config_mutex);
71 static inline void config_global_unlock(void) {
72 pthread_mutex_unlock(&config_mutex);
75 static inline void config_section_write_lock(struct config *co) {
76 pthread_mutex_lock(&co->mutex);
79 static inline void config_section_unlock(struct config *co) {
80 pthread_mutex_unlock(&co->mutex);
84 // ----------------------------------------------------------------------------
85 // config name-value index
87 static int config_value_compare(void* a, void* b) {
88 if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
89 else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1;
90 else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name);
93 #define config_value_index_add(co, cv) avl_insert_lock(&((co)->values_index), (avl *)(cv))
94 #define config_value_index_del(co, cv) avl_remove_lock(&((co)->values_index), (avl *)(cv))
96 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
97 struct config_value tmp;
98 tmp.hash = (hash)?hash:simple_hash(name);
99 tmp.name = (char *)name;
101 return (struct config_value *)avl_search_lock(&(co->values_index), (avl *) &tmp);
105 // ----------------------------------------------------------------------------
106 // config sections index
108 static int config_compare(void* a, void* b) {
109 if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
110 else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
111 else return strcmp(((struct config *)a)->name, ((struct config *)b)->name);
114 avl_tree_lock config_root_index = {
115 { NULL, config_compare },
119 #define config_index_add(cfg) avl_insert_lock(&config_root_index, (avl *)(cfg))
120 #define config_index_del(cfg) avl_remove_lock(&config_root_index, (avl *)(cfg))
122 static struct config *config_index_find(const char *name, uint32_t hash) {
124 tmp.hash = (hash)?hash:simple_hash(name);
125 tmp.name = (char *)name;
127 return (struct config *)avl_search_lock(&config_root_index, (avl *) &tmp);
131 // ----------------------------------------------------------------------------
132 // config section methods
134 static inline struct config *config_section_find(const char *section) {
135 return config_index_find(section, 0);
138 static inline struct config *config_section_create(const char *section)
140 debug(D_CONFIG, "Creating section '%s'.", section);
142 struct config *co = calloc(1, sizeof(struct config));
143 if(!co) fatal("Cannot allocate config");
145 co->name = strdup(section);
146 if(!co->name) fatal("Cannot allocate config.name");
147 co->hash = simple_hash(co->name);
149 avl_init_lock(&co->values_index, config_value_compare);
151 config_index_add(co);
153 config_global_write_lock();
154 struct config *co2 = config_root;
156 while (co2->next) co2 = co2->next;
159 else config_root = co;
160 config_global_unlock();
166 // ----------------------------------------------------------------------------
167 // config name-value methods
169 static inline struct config_value *config_value_create(struct config *co, const char *name, const char *value)
171 debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
173 struct config_value *cv = calloc(1, sizeof(struct config_value));
174 if(!cv) fatal("Cannot allocate config_value");
176 cv->name = strdup(name);
177 if(!cv->name) fatal("Cannot allocate config.name");
178 cv->hash = simple_hash(cv->name);
180 cv->value = strdup(value);
181 if(!cv->value) fatal("Cannot allocate config.value");
183 config_value_index_add(co, cv);
185 config_section_write_lock(co);
186 struct config_value *cv2 = co->values;
188 while (cv2->next) cv2 = cv2->next;
191 else co->values = cv;
192 config_section_unlock(co);
197 char *config_get(const char *section, const char *name, const char *default_value)
199 struct config_value *cv;
201 debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
203 struct config *co = config_section_find(section);
204 if(!co) co = config_section_create(section);
206 cv = config_value_index_find(co, name, 0);
208 cv = config_value_create(co, name, default_value);
211 cv->flags |= CONFIG_VALUE_USED;
213 if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
214 // this is a loaded value from the config file
215 // if it is different that the default, mark it
216 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
217 if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
218 cv->flags |= CONFIG_VALUE_CHECKED;
225 long long config_get_number(const char *section, const char *name, long long value)
227 char buffer[100], *s;
228 sprintf(buffer, "%lld", value);
230 s = config_get(section, name, buffer);
233 return strtoll(s, NULL, 0);
236 int config_get_boolean(const char *section, const char *name, int value)
242 s = config_get(section, name, s);
245 if(!strcmp(s, "yes") || !strcmp(s, "auto") || !strcmp(s, "on demand")) return 1;
249 int config_get_boolean_ondemand(const char *section, const char *name, int value)
253 if(value == CONFIG_ONDEMAND_ONDEMAND)
256 else if(value == CONFIG_ONDEMAND_NO)
262 s = config_get(section, name, s);
265 if(!strcmp(s, "yes"))
266 return CONFIG_ONDEMAND_YES;
267 else if(!strcmp(s, "no"))
268 return CONFIG_ONDEMAND_NO;
269 else if(!strcmp(s, "auto") || !strcmp(s, "on demand"))
270 return CONFIG_ONDEMAND_ONDEMAND;
275 const char *config_set_default(const char *section, const char *name, const char *value)
277 struct config_value *cv;
279 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
281 struct config *co = config_section_find(section);
282 if(!co) return config_set(section, name, value);
284 cv = config_value_index_find(co, name, 0);
285 if(!cv) return config_set(section, name, value);
287 cv->flags |= CONFIG_VALUE_USED;
289 if(cv->flags & CONFIG_VALUE_LOADED)
292 if(strcmp(cv->value, value) != 0) {
293 cv->flags |= CONFIG_VALUE_CHANGED;
296 cv->value = strdup(value);
297 if(!cv->value) fatal("Cannot allocate config.value");
303 const char *config_set(const char *section, const char *name, const char *value)
305 struct config_value *cv;
307 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
309 struct config *co = config_section_find(section);
310 if(!co) co = config_section_create(section);
312 cv = config_value_index_find(co, name, 0);
313 if(!cv) cv = config_value_create(co, name, value);
314 cv->flags |= CONFIG_VALUE_USED;
316 if(strcmp(cv->value, value) != 0) {
317 cv->flags |= CONFIG_VALUE_CHANGED;
320 cv->value = strdup(value);
321 if(!cv->value) fatal("Cannot allocate config.value");
327 long long config_set_number(const char *section, const char *name, long long value)
330 sprintf(buffer, "%lld", value);
332 config_set(section, name, buffer);
337 int config_set_boolean(const char *section, const char *name, int value)
343 config_set(section, name, s);
349 // ----------------------------------------------------------------------------
352 int load_config(char *filename, int overwrite_used)
355 struct config *co = NULL;
357 char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
359 if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
360 FILE *fp = fopen(filename, "r");
362 error("Cannot open file '%s'", filename);
366 while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
367 buffer[CONFIG_FILE_LINE_MAX] = '\0';
372 debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
376 int len = (int) strlen(s);
377 if(*s == '[' && s[len - 1] == ']') {
382 co = config_section_find(s);
383 if(!co) co = config_section_create(s);
389 // line outside a section
390 error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
395 char *value = strchr(s, '=');
397 error("Ignoring line %d ('%s'), there is no = in it.", line, s);
407 error("Ignoring line %d, name is empty.", line);
411 debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
415 struct config_value *cv = config_value_index_find(co, name, 0);
417 if(!cv) cv = config_value_create(co, name, value);
419 if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
420 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
422 cv->value = strdup(value);
423 if(!cv->value) fatal("Cannot allocate config.value");
426 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
428 cv->flags |= CONFIG_VALUE_LOADED;
436 void generate_config(BUFFER *wb, int only_changed)
440 struct config_value *cv;
442 for(i = 0; i < 3 ;i++) {
446 "# NetData Configuration\n"
447 "# You can uncomment and change any of the options below.\n"
448 "# The value shown in the commented settings, is the default value.\n"
449 "\n# global netdata configuration\n");
453 buffer_strcat(wb, "\n\n# per plugin configuration\n");
457 buffer_strcat(wb, "\n\n# per chart configuration\n");
461 config_global_write_lock();
462 for(co = config_root; co ; co = co->next) {
463 if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0 || strcmp(co->name, "registry") == 0) pri = 0;
464 else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
472 config_section_write_lock(co);
473 for(cv = co->values; cv ; cv = cv->next) {
474 used += (cv->flags & CONFIG_VALUE_USED)?1:0;
475 changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
478 config_section_unlock(co);
481 if(only_changed && !changed) continue;
484 buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
487 buffer_sprintf(wb, "\n[%s]\n", co->name);
489 config_section_write_lock(co);
490 for(cv = co->values; cv ; cv = cv->next) {
492 if(used && !(cv->flags & CONFIG_VALUE_USED)) {
493 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
495 buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
497 config_section_unlock(co);
500 config_global_unlock();