5 * 1. Re-write this using DICTIONARY
8 * The way it is now, concurrency locking is incomplete!
9 * It makes sure that only one writes to the structures
10 * but at the same time there may be unlimited readers.
11 * This can cause crashes.
13 * Of course, rewriting this to use DICTIONARY instead of
14 * directly accessing AVL structures, will solve the problem.
27 #include "appconfig.h"
30 #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
32 pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
34 // ----------------------------------------------------------------------------
37 #define CONFIG_VALUE_LOADED 0x01 // has been loaded from the config
38 #define CONFIG_VALUE_USED 0x02 // has been accessed from the program
39 #define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value
40 #define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default
43 avl avl; // the index - this has to be first!
45 uint32_t hash; // a simple hash to speed up searching
46 // we first compare hashes, and only if the hashes are equal we do string comparisons
53 struct config_value *next;
59 uint32_t hash; // a simple hash to speed up searching
60 // we first compare hashes, and only if the hashes are equal we do string comparisons
64 struct config_value *values;
65 avl_tree_lock values_index;
69 pthread_rwlock_t rwlock;
70 } *config_root = NULL;
73 // ----------------------------------------------------------------------------
76 static inline void config_global_read_lock(void) {
77 pthread_rwlock_rdlock(&config_rwlock);
80 static inline void config_global_write_lock(void) {
81 pthread_rwlock_wrlock(&config_rwlock);
84 static inline void config_global_unlock(void) {
85 pthread_rwlock_unlock(&config_rwlock);
88 static inline void config_section_read_lock(struct config *co) {
89 pthread_rwlock_rdlock(&co->rwlock);
92 static inline void config_section_write_lock(struct config *co) {
93 pthread_rwlock_wrlock(&co->rwlock);
96 static inline void config_section_unlock(struct config *co) {
97 pthread_rwlock_unlock(&co->rwlock);
101 // ----------------------------------------------------------------------------
102 // config name-value index
104 static int config_value_iterator(avl *a) { if(a) {}; return 0; }
106 static int config_value_compare(void* a, void* b) {
107 if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
108 else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1;
109 else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name);
112 #define config_value_index_add(co, cv) avl_insert_lock(&((co)->values_index), (avl *)(cv))
113 #define config_value_index_del(co, cv) avl_remove_lock(&((co)->values_index), (avl *)(cv))
115 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
116 struct config_value *result = NULL, tmp;
117 tmp.hash = (hash)?hash:simple_hash(name);
118 tmp.name = (char *)name;
120 avl_search_lock(&(co->values_index), (avl *) &tmp, config_value_iterator, (avl **) &result);
125 // ----------------------------------------------------------------------------
126 // config sections index
128 static int config_iterator(avl *a) { if(a) {}; return 0; }
130 static int config_compare(void* a, void* b) {
131 if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
132 else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
133 else return strcmp(((struct config *)a)->name, ((struct config *)b)->name);
136 avl_tree_lock config_root_index = {
137 { NULL, config_compare },
141 #define config_index_add(cfg) avl_insert_lock(&config_root_index, (avl *)(cfg))
142 #define config_index_del(cfg) avl_remove_lock(&config_root_index, (avl *)(cfg))
144 static struct config *config_index_find(const char *name, uint32_t hash) {
145 struct config *result = NULL, tmp;
146 tmp.hash = (hash)?hash:simple_hash(name);
147 tmp.name = (char *)name;
149 avl_search_lock(&config_root_index, (avl *) &tmp, config_iterator, (avl **) &result);
154 // ----------------------------------------------------------------------------
155 // config section methods
157 static inline struct config *config_section_find(const char *section) {
158 return config_index_find(section, 0);
161 static inline struct config *config_section_create(const char *section)
163 debug(D_CONFIG, "Creating section '%s'.", section);
165 struct config *co = calloc(1, sizeof(struct config));
166 if(!co) fatal("Cannot allocate config");
168 co->name = strdup(section);
169 if(!co->name) fatal("Cannot allocate config.name");
170 co->hash = simple_hash(co->name);
172 avl_init_lock(&co->values_index, config_value_compare);
174 config_global_write_lock();
176 config_index_add(co);
178 struct config *co2 = config_root;
180 while (co2->next) co2 = co2->next;
183 else config_root = co;
185 config_global_unlock();
191 // ----------------------------------------------------------------------------
192 // config name-value methods
194 static inline struct config_value *config_value_create(struct config *co, const char *name, const char *value)
196 debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
198 struct config_value *cv = calloc(1, sizeof(struct config_value));
199 if(!cv) fatal("Cannot allocate config_value");
201 cv->name = strdup(name);
202 if(!cv->name) fatal("Cannot allocate config.name");
203 cv->hash = simple_hash(cv->name);
205 cv->value = strdup(value);
206 if(!cv->value) fatal("Cannot allocate config.value");
208 config_section_write_lock(co);
210 config_value_index_add(co, cv);
212 struct config_value *cv2 = co->values;
214 while (cv2->next) cv2 = cv2->next;
217 else co->values = cv;
219 config_section_unlock(co);
224 char *config_get(const char *section, const char *name, const char *default_value)
226 struct config_value *cv;
228 debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
230 struct config *co = config_section_find(section);
231 if(!co) co = config_section_create(section);
233 cv = config_value_index_find(co, name, 0);
235 cv = config_value_create(co, name, default_value);
238 cv->flags |= CONFIG_VALUE_USED;
240 if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
241 // this is a loaded value from the config file
242 // if it is different that the default, mark it
243 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
244 if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
245 cv->flags |= CONFIG_VALUE_CHECKED;
252 long long config_get_number(const char *section, const char *name, long long value)
254 char buffer[100], *s;
255 sprintf(buffer, "%lld", value);
257 s = config_get(section, name, buffer);
260 return strtoll(s, NULL, 0);
263 int config_get_boolean(const char *section, const char *name, int value)
269 s = config_get(section, name, s);
272 if(!strcmp(s, "yes")) return 1;
276 int config_get_boolean_ondemand(const char *section, const char *name, int value)
280 if(value == CONFIG_ONDEMAND_ONDEMAND)
283 else if(value == CONFIG_ONDEMAND_NO)
289 s = config_get(section, name, s);
292 if(!strcmp(s, "yes"))
293 return CONFIG_ONDEMAND_YES;
294 else if(!strcmp(s, "no"))
295 return CONFIG_ONDEMAND_NO;
296 else if(!strcmp(s, "on demand"))
297 return CONFIG_ONDEMAND_ONDEMAND;
302 const char *config_set_default(const char *section, const char *name, const char *value)
304 struct config_value *cv;
306 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
308 struct config *co = config_section_find(section);
309 if(!co) return config_set(section, name, value);
311 cv = config_value_index_find(co, name, 0);
312 if(!cv) return config_set(section, name, value);
314 cv->flags |= CONFIG_VALUE_USED;
316 if(cv->flags & CONFIG_VALUE_LOADED)
319 if(strcmp(cv->value, value) != 0) {
320 cv->flags |= CONFIG_VALUE_CHANGED;
323 cv->value = strdup(value);
324 if(!cv->value) fatal("Cannot allocate config.value");
330 const char *config_set(const char *section, const char *name, const char *value)
332 struct config_value *cv;
334 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
336 struct config *co = config_section_find(section);
337 if(!co) co = config_section_create(section);
339 cv = config_value_index_find(co, name, 0);
340 if(!cv) cv = config_value_create(co, name, value);
341 cv->flags |= CONFIG_VALUE_USED;
343 if(strcmp(cv->value, value) != 0) {
344 cv->flags |= CONFIG_VALUE_CHANGED;
347 cv->value = strdup(value);
348 if(!cv->value) fatal("Cannot allocate config.value");
354 long long config_set_number(const char *section, const char *name, long long value)
357 sprintf(buffer, "%lld", value);
359 config_set(section, name, buffer);
364 int config_set_boolean(const char *section, const char *name, int value)
370 config_set(section, name, s);
376 // ----------------------------------------------------------------------------
379 int load_config(char *filename, int overwrite_used)
382 struct config *co = NULL;
384 char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
386 if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
387 FILE *fp = fopen(filename, "r");
389 error("Cannot open file '%s'", filename);
393 while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
394 buffer[CONFIG_FILE_LINE_MAX] = '\0';
399 debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
403 int len = (int) strlen(s);
404 if(*s == '[' && s[len - 1] == ']') {
409 co = config_section_find(s);
410 if(!co) co = config_section_create(s);
416 // line outside a section
417 error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
422 char *value = strchr(s, '=');
424 error("Ignoring line %d ('%s'), there is no = in it.", line, s);
434 error("Ignoring line %d, name is empty.", line);
438 debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
442 struct config_value *cv = config_value_index_find(co, name, 0);
444 if(!cv) cv = config_value_create(co, name, value);
446 if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
447 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
449 cv->value = strdup(value);
450 if(!cv->value) fatal("Cannot allocate config.value");
453 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
455 cv->flags |= CONFIG_VALUE_LOADED;
463 void generate_config(BUFFER *wb, int only_changed)
467 struct config_value *cv;
469 for(i = 0; i < 3 ;i++) {
473 "# NetData Configuration\n"
474 "# You can uncomment and change any of the options below.\n"
475 "# The value shown in the commented settings, is the default value.\n"
476 "\n# global netdata configuration\n");
480 buffer_strcat(wb, "\n\n# per plugin configuration\n");
484 buffer_strcat(wb, "\n\n# per chart configuration\n");
488 config_global_write_lock();
489 for(co = config_root; co ; co = co->next) {
490 if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0 || strcmp(co->name, "registry") == 0) pri = 0;
491 else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
499 config_section_write_lock(co);
500 for(cv = co->values; cv ; cv = cv->next) {
501 used += (cv->flags & CONFIG_VALUE_USED)?1:0;
502 changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
505 config_section_unlock(co);
508 if(only_changed && !changed) continue;
511 buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
514 buffer_sprintf(wb, "\n[%s]\n", co->name);
516 config_section_write_lock(co);
517 for(cv = co->values; cv ; cv = cv->next) {
519 if(used && !(cv->flags & CONFIG_VALUE_USED)) {
520 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
522 buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
524 config_section_unlock(co);
527 config_global_unlock();