]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
Merge pull request #608 from candrews/patch-1
[netdata.git] / src / appconfig.c
1
2 /*
3  * TODO
4  *
5  * 1. Re-write this using DICTIONARY
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12 #include <pthread.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "avl.h"
17 #include "common.h"
18 #include "appconfig.h"
19 #include "log.h"
20
21 #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
22
23 pthread_mutex_t config_mutex = PTHREAD_MUTEX_INITIALIZER;
24
25 // ----------------------------------------------------------------------------
26 // definitions
27
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
32
33 struct config_value {
34         avl avl;                                // the index - this has to be first!
35
36         uint8_t flags;
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
39
40         char *name;
41         char *value;
42
43         struct config_value *next; // config->mutex protects just this
44 };
45
46 struct config {
47         avl avl;
48
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
51
52         char *name;
53
54         struct config *next;    // gloabl config_mutex protects just this
55
56         struct config_value *values;
57         avl_tree_lock values_index;
58
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;
62
63
64 // ----------------------------------------------------------------------------
65 // locking
66
67 static inline void config_global_write_lock(void) {
68         pthread_mutex_lock(&config_mutex);
69 }
70
71 static inline void config_global_unlock(void) {
72         pthread_mutex_unlock(&config_mutex);
73 }
74
75 static inline void config_section_write_lock(struct config *co) {
76         pthread_mutex_lock(&co->mutex);
77 }
78
79 static inline void config_section_unlock(struct config *co) {
80         pthread_mutex_unlock(&co->mutex);
81 }
82
83
84 // ----------------------------------------------------------------------------
85 // config name-value index
86
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);
91 }
92
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))
95
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;
100
101         return (struct config_value *)avl_search_lock(&(co->values_index), (avl *) &tmp);
102 }
103
104
105 // ----------------------------------------------------------------------------
106 // config sections index
107
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);
112 }
113
114 avl_tree_lock config_root_index = {
115                 { NULL, config_compare },
116                 AVL_LOCK_INITIALIZER
117 };
118
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))
121
122 static struct config *config_index_find(const char *name, uint32_t hash) {
123         struct config tmp;
124         tmp.hash = (hash)?hash:simple_hash(name);
125         tmp.name = (char *)name;
126
127         return (struct config *)avl_search_lock(&config_root_index, (avl *) &tmp);
128 }
129
130
131 // ----------------------------------------------------------------------------
132 // config section methods
133
134 static inline struct config *config_section_find(const char *section) {
135         return config_index_find(section, 0);
136 }
137
138 static inline struct config *config_section_create(const char *section)
139 {
140         debug(D_CONFIG, "Creating section '%s'.", section);
141
142         struct config *co = calloc(1, sizeof(struct config));
143         if(!co) fatal("Cannot allocate config");
144
145         co->name = strdup(section);
146         if(!co->name) fatal("Cannot allocate config.name");
147         co->hash = simple_hash(co->name);
148
149         avl_init_lock(&co->values_index, config_value_compare);
150
151         config_index_add(co);
152
153         config_global_write_lock();
154         struct config *co2 = config_root;
155         if(co2) {
156                 while (co2->next) co2 = co2->next;
157                 co2->next = co;
158         }
159         else config_root = co;
160         config_global_unlock();
161
162         return co;
163 }
164
165
166 // ----------------------------------------------------------------------------
167 // config name-value methods
168
169 static inline struct config_value *config_value_create(struct config *co, const char *name, const char *value)
170 {
171         debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
172
173         struct config_value *cv = calloc(1, sizeof(struct config_value));
174         if(!cv) fatal("Cannot allocate config_value");
175
176         cv->name = strdup(name);
177         if(!cv->name) fatal("Cannot allocate config.name");
178         cv->hash = simple_hash(cv->name);
179
180         cv->value = strdup(value);
181         if(!cv->value) fatal("Cannot allocate config.value");
182
183         config_value_index_add(co, cv);
184
185         config_section_write_lock(co);
186         struct config_value *cv2 = co->values;
187         if(cv2) {
188                 while (cv2->next) cv2 = cv2->next;
189                 cv2->next = cv;
190         }
191         else co->values = cv;
192         config_section_unlock(co);
193
194         return cv;
195 }
196
197 int config_exists(const char *section, const char *name) {
198         struct config_value *cv;
199
200         debug(D_CONFIG, "request to get config in section '%s', name '%s'", section, name);
201
202         struct config *co = config_section_find(section);
203         if(!co) return 0;
204
205         cv = config_value_index_find(co, name, 0);
206         if(!cv) return 0;
207
208         return 1;
209 }
210
211 int config_rename(const char *section, const char *old, const char *new) {
212         struct config_value *cv, *cv2;
213
214         debug(D_CONFIG, "request to rename config in section '%s', old name '%s', new name '%s'", section, old, new);
215
216         struct config *co = config_section_find(section);
217         if(!co) return -1;
218
219         config_section_write_lock(co);
220
221         cv = config_value_index_find(co, old, 0);
222         if(!cv) goto cleanup;
223
224         cv2 = config_value_index_find(co, new, 0);
225         if(cv2) goto cleanup;
226
227         config_value_index_del(co, cv);
228
229         free(cv->name);
230         cv->name = strdup(new);
231         if(!cv->name) fatal("Cannot allocate memory for config_rename()");
232
233         cv->hash = simple_hash(cv->name);
234
235         config_value_index_add(co, cv);
236         config_section_unlock(co);
237
238         return 0;
239
240 cleanup:
241         config_section_unlock(co);
242         return -1;
243 }
244
245 char *config_get(const char *section, const char *name, const char *default_value)
246 {
247         struct config_value *cv;
248
249         debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
250
251         struct config *co = config_section_find(section);
252         if(!co) co = config_section_create(section);
253
254         cv = config_value_index_find(co, name, 0);
255         if(!cv) {
256                 cv = config_value_create(co, name, default_value);
257                 if(!cv) return NULL;
258         }
259         cv->flags |= CONFIG_VALUE_USED;
260
261         if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
262                 // this is a loaded value from the config file
263                 // if it is different that the default, mark it
264                 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
265                         if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
266                         cv->flags |= CONFIG_VALUE_CHECKED;
267                 }
268         }
269
270         return(cv->value);
271 }
272
273 long long config_get_number(const char *section, const char *name, long long value)
274 {
275         char buffer[100], *s;
276         sprintf(buffer, "%lld", value);
277
278         s = config_get(section, name, buffer);
279         if(!s) return value;
280
281         return strtoll(s, NULL, 0);
282 }
283
284 int config_get_boolean(const char *section, const char *name, int value)
285 {
286         char *s;
287         if(value) s = "yes";
288         else s = "no";
289
290         s = config_get(section, name, s);
291         if(!s) return value;
292
293         if(!strcmp(s, "yes") || !strcmp(s, "auto") || !strcmp(s, "on demand")) return 1;
294         return 0;
295 }
296
297 int config_get_boolean_ondemand(const char *section, const char *name, int value)
298 {
299         char *s;
300
301         if(value == CONFIG_ONDEMAND_ONDEMAND)
302                 s = "auto";
303
304         else if(value == CONFIG_ONDEMAND_NO)
305                 s = "no";
306
307         else
308                 s = "yes";
309
310         s = config_get(section, name, s);
311         if(!s) return value;
312
313         if(!strcmp(s, "yes"))
314                 return CONFIG_ONDEMAND_YES;
315         else if(!strcmp(s, "no"))
316                 return CONFIG_ONDEMAND_NO;
317         else if(!strcmp(s, "auto") || !strcmp(s, "on demand"))
318                 return CONFIG_ONDEMAND_ONDEMAND;
319
320         return value;
321 }
322
323 const char *config_set_default(const char *section, const char *name, const char *value)
324 {
325         struct config_value *cv;
326
327         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
328
329         struct config *co = config_section_find(section);
330         if(!co) return config_set(section, name, value);
331
332         cv = config_value_index_find(co, name, 0);
333         if(!cv) return config_set(section, name, value);
334
335         cv->flags |= CONFIG_VALUE_USED;
336
337         if(cv->flags & CONFIG_VALUE_LOADED)
338                 return cv->value;
339
340         if(strcmp(cv->value, value) != 0) {
341                 cv->flags |= CONFIG_VALUE_CHANGED;
342
343                 free(cv->value);
344                 cv->value = strdup(value);
345                 if(!cv->value) fatal("Cannot allocate config.value");
346         }
347
348         return cv->value;
349 }
350
351 const char *config_set(const char *section, const char *name, const char *value)
352 {
353         struct config_value *cv;
354
355         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
356
357         struct config *co = config_section_find(section);
358         if(!co) co = config_section_create(section);
359
360         cv = config_value_index_find(co, name, 0);
361         if(!cv) cv = config_value_create(co, name, value);
362         cv->flags |= CONFIG_VALUE_USED;
363
364         if(strcmp(cv->value, value) != 0) {
365                 cv->flags |= CONFIG_VALUE_CHANGED;
366
367                 free(cv->value);
368                 cv->value = strdup(value);
369                 if(!cv->value) fatal("Cannot allocate config.value");
370         }
371
372         return value;
373 }
374
375 long long config_set_number(const char *section, const char *name, long long value)
376 {
377         char buffer[100];
378         sprintf(buffer, "%lld", value);
379
380         config_set(section, name, buffer);
381
382         return value;
383 }
384
385 int config_set_boolean(const char *section, const char *name, int value)
386 {
387         char *s;
388         if(value) s = "yes";
389         else s = "no";
390
391         config_set(section, name, s);
392
393         return value;
394 }
395
396
397 // ----------------------------------------------------------------------------
398 // config load/save
399
400 int load_config(char *filename, int overwrite_used)
401 {
402         int line = 0;
403         struct config *co = NULL;
404
405         char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
406
407         if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
408         FILE *fp = fopen(filename, "r");
409         if(!fp) {
410                 error("Cannot open file '%s'", filename);
411                 return 0;
412         }
413
414         while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
415                 buffer[CONFIG_FILE_LINE_MAX] = '\0';
416                 line++;
417
418                 s = trim(buffer);
419                 if(!s) {
420                         debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
421                         continue;
422                 }
423
424                 int len = (int) strlen(s);
425                 if(*s == '[' && s[len - 1] == ']') {
426                         // new section
427                         s[len - 1] = '\0';
428                         s++;
429
430                         co = config_section_find(s);
431                         if(!co) co = config_section_create(s);
432
433                         continue;
434                 }
435
436                 if(!co) {
437                         // line outside a section
438                         error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
439                         continue;
440                 }
441
442                 char *name = s;
443                 char *value = strchr(s, '=');
444                 if(!value) {
445                         error("Ignoring line %d ('%s'), there is no = in it.", line, s);
446                         continue;
447                 }
448                 *value = '\0';
449                 value++;
450
451                 name = trim(name);
452                 value = trim(value);
453
454                 if(!name) {
455                         error("Ignoring line %d, name is empty.", line);
456                         continue;
457                 }
458                 if(!value) {
459                         debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
460                         continue;
461                 }
462
463                 struct config_value *cv = config_value_index_find(co, name, 0);
464
465                 if(!cv) cv = config_value_create(co, name, value);
466                 else {
467                         if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
468                                 debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
469                                 free(cv->value);
470                                 cv->value = strdup(value);
471                                 if(!cv->value) fatal("Cannot allocate config.value");
472                         }
473                         else
474                                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
475                 }
476                 cv->flags |= CONFIG_VALUE_LOADED;
477         }
478
479         fclose(fp);
480
481         return 1;
482 }
483
484 void generate_config(BUFFER *wb, int only_changed)
485 {
486         int i, pri;
487         struct config *co;
488         struct config_value *cv;
489
490         for(i = 0; i < 3 ;i++) {
491                 switch(i) {
492                         case 0:
493                                 buffer_strcat(wb,
494                                         "# NetData Configuration\n"
495                                         "# You can uncomment and change any of the options below.\n"
496                                         "# The value shown in the commented settings, is the default value.\n"
497                                         "\n# global netdata configuration\n");
498                                 break;
499
500                         case 1:
501                                 buffer_strcat(wb, "\n\n# per plugin configuration\n");
502                                 break;
503
504                         case 2:
505                                 buffer_strcat(wb, "\n\n# per chart configuration\n");
506                                 break;
507                 }
508
509                 config_global_write_lock();
510                 for(co = config_root; co ; co = co->next) {
511                         if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0 || strcmp(co->name, "registry") == 0) pri = 0;
512                         else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
513                         else pri = 2;
514
515                         if(i == pri) {
516                                 int used = 0;
517                                 int changed = 0;
518                                 int count = 0;
519
520                                 config_section_write_lock(co);
521                                 for(cv = co->values; cv ; cv = cv->next) {
522                                         used += (cv->flags & CONFIG_VALUE_USED)?1:0;
523                                         changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
524                                         count++;
525                                 }
526                                 config_section_unlock(co);
527
528                                 if(!count) continue;
529                                 if(only_changed && !changed) continue;
530
531                                 if(!used) {
532                                         buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
533                                 }
534
535                                 buffer_sprintf(wb, "\n[%s]\n", co->name);
536
537                                 config_section_write_lock(co);
538                                 for(cv = co->values; cv ; cv = cv->next) {
539
540                                         if(used && !(cv->flags & CONFIG_VALUE_USED)) {
541                                                 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
542                                         }
543                                         buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
544                                 }
545                                 config_section_unlock(co);
546                         }
547                 }
548                 config_global_unlock();
549         }
550 }