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_iterator(avl *a) { if(a) {}; return 0; }
89 static int config_value_compare(void* a, void* b) {
90 if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
91 else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1;
92 else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name);
95 #define config_value_index_add(co, cv) avl_insert_lock(&((co)->values_index), (avl *)(cv))
96 #define config_value_index_del(co, cv) avl_remove_lock(&((co)->values_index), (avl *)(cv))
98 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
99 struct config_value *result = NULL, tmp;
100 tmp.hash = (hash)?hash:simple_hash(name);
101 tmp.name = (char *)name;
103 avl_search_lock(&(co->values_index), (avl *) &tmp, config_value_iterator, (avl **) &result);
108 // ----------------------------------------------------------------------------
109 // config sections index
111 static int config_iterator(avl *a) { if(a) {}; return 0; }
113 static int config_compare(void* a, void* b) {
114 if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
115 else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
116 else return strcmp(((struct config *)a)->name, ((struct config *)b)->name);
119 avl_tree_lock config_root_index = {
120 { NULL, config_compare },
124 #define config_index_add(cfg) avl_insert_lock(&config_root_index, (avl *)(cfg))
125 #define config_index_del(cfg) avl_remove_lock(&config_root_index, (avl *)(cfg))
127 static struct config *config_index_find(const char *name, uint32_t hash) {
128 struct config *result = NULL, tmp;
129 tmp.hash = (hash)?hash:simple_hash(name);
130 tmp.name = (char *)name;
132 avl_search_lock(&config_root_index, (avl *) &tmp, config_iterator, (avl **) &result);
137 // ----------------------------------------------------------------------------
138 // config section methods
140 static inline struct config *config_section_find(const char *section) {
141 return config_index_find(section, 0);
144 static inline struct config *config_section_create(const char *section)
146 debug(D_CONFIG, "Creating section '%s'.", section);
148 struct config *co = calloc(1, sizeof(struct config));
149 if(!co) fatal("Cannot allocate config");
151 co->name = strdup(section);
152 if(!co->name) fatal("Cannot allocate config.name");
153 co->hash = simple_hash(co->name);
155 avl_init_lock(&co->values_index, config_value_compare);
157 config_index_add(co);
159 config_global_write_lock();
160 struct config *co2 = config_root;
162 while (co2->next) co2 = co2->next;
165 else config_root = co;
166 config_global_unlock();
172 // ----------------------------------------------------------------------------
173 // config name-value methods
175 static inline struct config_value *config_value_create(struct config *co, const char *name, const char *value)
177 debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
179 struct config_value *cv = calloc(1, sizeof(struct config_value));
180 if(!cv) fatal("Cannot allocate config_value");
182 cv->name = strdup(name);
183 if(!cv->name) fatal("Cannot allocate config.name");
184 cv->hash = simple_hash(cv->name);
186 cv->value = strdup(value);
187 if(!cv->value) fatal("Cannot allocate config.value");
189 config_value_index_add(co, cv);
191 config_section_write_lock(co);
192 struct config_value *cv2 = co->values;
194 while (cv2->next) cv2 = cv2->next;
197 else co->values = cv;
198 config_section_unlock(co);
203 char *config_get(const char *section, const char *name, const char *default_value)
205 struct config_value *cv;
207 debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
209 struct config *co = config_section_find(section);
210 if(!co) co = config_section_create(section);
212 cv = config_value_index_find(co, name, 0);
214 cv = config_value_create(co, name, default_value);
217 cv->flags |= CONFIG_VALUE_USED;
219 if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
220 // this is a loaded value from the config file
221 // if it is different that the default, mark it
222 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
223 if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
224 cv->flags |= CONFIG_VALUE_CHECKED;
231 long long config_get_number(const char *section, const char *name, long long value)
233 char buffer[100], *s;
234 sprintf(buffer, "%lld", value);
236 s = config_get(section, name, buffer);
239 return strtoll(s, NULL, 0);
242 int config_get_boolean(const char *section, const char *name, int value)
248 s = config_get(section, name, s);
251 if(!strcmp(s, "yes")) return 1;
255 int config_get_boolean_ondemand(const char *section, const char *name, int value)
259 if(value == CONFIG_ONDEMAND_ONDEMAND)
262 else if(value == CONFIG_ONDEMAND_NO)
268 s = config_get(section, name, s);
271 if(!strcmp(s, "yes"))
272 return CONFIG_ONDEMAND_YES;
273 else if(!strcmp(s, "no"))
274 return CONFIG_ONDEMAND_NO;
275 else if(!strcmp(s, "on demand"))
276 return CONFIG_ONDEMAND_ONDEMAND;
281 const char *config_set_default(const char *section, const char *name, const char *value)
283 struct config_value *cv;
285 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
287 struct config *co = config_section_find(section);
288 if(!co) return config_set(section, name, value);
290 cv = config_value_index_find(co, name, 0);
291 if(!cv) return config_set(section, name, value);
293 cv->flags |= CONFIG_VALUE_USED;
295 if(cv->flags & CONFIG_VALUE_LOADED)
298 if(strcmp(cv->value, value) != 0) {
299 cv->flags |= CONFIG_VALUE_CHANGED;
302 cv->value = strdup(value);
303 if(!cv->value) fatal("Cannot allocate config.value");
309 const char *config_set(const char *section, const char *name, const char *value)
311 struct config_value *cv;
313 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
315 struct config *co = config_section_find(section);
316 if(!co) co = config_section_create(section);
318 cv = config_value_index_find(co, name, 0);
319 if(!cv) cv = config_value_create(co, name, value);
320 cv->flags |= CONFIG_VALUE_USED;
322 if(strcmp(cv->value, value) != 0) {
323 cv->flags |= CONFIG_VALUE_CHANGED;
326 cv->value = strdup(value);
327 if(!cv->value) fatal("Cannot allocate config.value");
333 long long config_set_number(const char *section, const char *name, long long value)
336 sprintf(buffer, "%lld", value);
338 config_set(section, name, buffer);
343 int config_set_boolean(const char *section, const char *name, int value)
349 config_set(section, name, s);
355 // ----------------------------------------------------------------------------
358 int load_config(char *filename, int overwrite_used)
361 struct config *co = NULL;
363 char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
365 if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
366 FILE *fp = fopen(filename, "r");
368 error("Cannot open file '%s'", filename);
372 while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
373 buffer[CONFIG_FILE_LINE_MAX] = '\0';
378 debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
382 int len = (int) strlen(s);
383 if(*s == '[' && s[len - 1] == ']') {
388 co = config_section_find(s);
389 if(!co) co = config_section_create(s);
395 // line outside a section
396 error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
401 char *value = strchr(s, '=');
403 error("Ignoring line %d ('%s'), there is no = in it.", line, s);
413 error("Ignoring line %d, name is empty.", line);
417 debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
421 struct config_value *cv = config_value_index_find(co, name, 0);
423 if(!cv) cv = config_value_create(co, name, value);
425 if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
426 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
428 cv->value = strdup(value);
429 if(!cv->value) fatal("Cannot allocate config.value");
432 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
434 cv->flags |= CONFIG_VALUE_LOADED;
442 void generate_config(BUFFER *wb, int only_changed)
446 struct config_value *cv;
448 for(i = 0; i < 3 ;i++) {
452 "# NetData Configuration\n"
453 "# You can uncomment and change any of the options below.\n"
454 "# The value shown in the commented settings, is the default value.\n"
455 "\n# global netdata configuration\n");
459 buffer_strcat(wb, "\n\n# per plugin configuration\n");
463 buffer_strcat(wb, "\n\n# per chart configuration\n");
467 config_global_write_lock();
468 for(co = config_root; co ; co = co->next) {
469 if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0 || strcmp(co->name, "registry") == 0) pri = 0;
470 else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
478 config_section_write_lock(co);
479 for(cv = co->values; cv ; cv = cv->next) {
480 used += (cv->flags & CONFIG_VALUE_USED)?1:0;
481 changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
484 config_section_unlock(co);
487 if(only_changed && !changed) continue;
490 buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
493 buffer_sprintf(wb, "\n[%s]\n", co->name);
495 config_section_write_lock(co);
496 for(cv = co->values; cv ; cv = cv->next) {
498 if(used && !(cv->flags & CONFIG_VALUE_USED)) {
499 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
501 buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
503 config_section_unlock(co);
506 config_global_unlock();