]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
Merge remote-tracking branch 'firehol/master'
[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         config_value_index_add(co, cv);
234         config_section_unlock(co);
235
236         return 0;
237
238 cleanup:
239         config_section_unlock(co);
240         return -1;
241 }
242
243 char *config_get(const char *section, const char *name, const char *default_value)
244 {
245         struct config_value *cv;
246
247         debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
248
249         struct config *co = config_section_find(section);
250         if(!co) co = config_section_create(section);
251
252         cv = config_value_index_find(co, name, 0);
253         if(!cv) {
254                 cv = config_value_create(co, name, default_value);
255                 if(!cv) return NULL;
256         }
257         cv->flags |= CONFIG_VALUE_USED;
258
259         if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
260                 // this is a loaded value from the config file
261                 // if it is different that the default, mark it
262                 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
263                         if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
264                         cv->flags |= CONFIG_VALUE_CHECKED;
265                 }
266         }
267
268         return(cv->value);
269 }
270
271 long long config_get_number(const char *section, const char *name, long long value)
272 {
273         char buffer[100], *s;
274         sprintf(buffer, "%lld", value);
275
276         s = config_get(section, name, buffer);
277         if(!s) return value;
278
279         return strtoll(s, NULL, 0);
280 }
281
282 int config_get_boolean(const char *section, const char *name, int value)
283 {
284         char *s;
285         if(value) s = "yes";
286         else s = "no";
287
288         s = config_get(section, name, s);
289         if(!s) return value;
290
291         if(!strcmp(s, "yes") || !strcmp(s, "auto") || !strcmp(s, "on demand")) return 1;
292         return 0;
293 }
294
295 int config_get_boolean_ondemand(const char *section, const char *name, int value)
296 {
297         char *s;
298
299         if(value == CONFIG_ONDEMAND_ONDEMAND)
300                 s = "auto";
301
302         else if(value == CONFIG_ONDEMAND_NO)
303                 s = "no";
304
305         else
306                 s = "yes";
307
308         s = config_get(section, name, s);
309         if(!s) return value;
310
311         if(!strcmp(s, "yes"))
312                 return CONFIG_ONDEMAND_YES;
313         else if(!strcmp(s, "no"))
314                 return CONFIG_ONDEMAND_NO;
315         else if(!strcmp(s, "auto") || !strcmp(s, "on demand"))
316                 return CONFIG_ONDEMAND_ONDEMAND;
317
318         return value;
319 }
320
321 const char *config_set_default(const char *section, const char *name, const char *value)
322 {
323         struct config_value *cv;
324
325         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
326
327         struct config *co = config_section_find(section);
328         if(!co) return config_set(section, name, value);
329
330         cv = config_value_index_find(co, name, 0);
331         if(!cv) return config_set(section, name, value);
332
333         cv->flags |= CONFIG_VALUE_USED;
334
335         if(cv->flags & CONFIG_VALUE_LOADED)
336                 return cv->value;
337
338         if(strcmp(cv->value, value) != 0) {
339                 cv->flags |= CONFIG_VALUE_CHANGED;
340
341                 free(cv->value);
342                 cv->value = strdup(value);
343                 if(!cv->value) fatal("Cannot allocate config.value");
344         }
345
346         return cv->value;
347 }
348
349 const char *config_set(const char *section, const char *name, const char *value)
350 {
351         struct config_value *cv;
352
353         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
354
355         struct config *co = config_section_find(section);
356         if(!co) co = config_section_create(section);
357
358         cv = config_value_index_find(co, name, 0);
359         if(!cv) cv = config_value_create(co, name, value);
360         cv->flags |= CONFIG_VALUE_USED;
361
362         if(strcmp(cv->value, value) != 0) {
363                 cv->flags |= CONFIG_VALUE_CHANGED;
364
365                 free(cv->value);
366                 cv->value = strdup(value);
367                 if(!cv->value) fatal("Cannot allocate config.value");
368         }
369
370         return value;
371 }
372
373 long long config_set_number(const char *section, const char *name, long long value)
374 {
375         char buffer[100];
376         sprintf(buffer, "%lld", value);
377
378         config_set(section, name, buffer);
379
380         return value;
381 }
382
383 int config_set_boolean(const char *section, const char *name, int value)
384 {
385         char *s;
386         if(value) s = "yes";
387         else s = "no";
388
389         config_set(section, name, s);
390
391         return value;
392 }
393
394
395 // ----------------------------------------------------------------------------
396 // config load/save
397
398 int load_config(char *filename, int overwrite_used)
399 {
400         int line = 0;
401         struct config *co = NULL;
402
403         char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
404
405         if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
406         FILE *fp = fopen(filename, "r");
407         if(!fp) {
408                 error("Cannot open file '%s'", filename);
409                 return 0;
410         }
411
412         while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
413                 buffer[CONFIG_FILE_LINE_MAX] = '\0';
414                 line++;
415
416                 s = trim(buffer);
417                 if(!s) {
418                         debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
419                         continue;
420                 }
421
422                 int len = (int) strlen(s);
423                 if(*s == '[' && s[len - 1] == ']') {
424                         // new section
425                         s[len - 1] = '\0';
426                         s++;
427
428                         co = config_section_find(s);
429                         if(!co) co = config_section_create(s);
430
431                         continue;
432                 }
433
434                 if(!co) {
435                         // line outside a section
436                         error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
437                         continue;
438                 }
439
440                 char *name = s;
441                 char *value = strchr(s, '=');
442                 if(!value) {
443                         error("Ignoring line %d ('%s'), there is no = in it.", line, s);
444                         continue;
445                 }
446                 *value = '\0';
447                 value++;
448
449                 name = trim(name);
450                 value = trim(value);
451
452                 if(!name) {
453                         error("Ignoring line %d, name is empty.", line);
454                         continue;
455                 }
456                 if(!value) {
457                         debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
458                         continue;
459                 }
460
461                 struct config_value *cv = config_value_index_find(co, name, 0);
462
463                 if(!cv) cv = config_value_create(co, name, value);
464                 else {
465                         if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
466                                 debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
467                                 free(cv->value);
468                                 cv->value = strdup(value);
469                                 if(!cv->value) fatal("Cannot allocate config.value");
470                         }
471                         else
472                                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
473                 }
474                 cv->flags |= CONFIG_VALUE_LOADED;
475         }
476
477         fclose(fp);
478
479         return 1;
480 }
481
482 void generate_config(BUFFER *wb, int only_changed)
483 {
484         int i, pri;
485         struct config *co;
486         struct config_value *cv;
487
488         for(i = 0; i < 3 ;i++) {
489                 switch(i) {
490                         case 0:
491                                 buffer_strcat(wb,
492                                         "# NetData Configuration\n"
493                                         "# You can uncomment and change any of the options below.\n"
494                                         "# The value shown in the commented settings, is the default value.\n"
495                                         "\n# global netdata configuration\n");
496                                 break;
497
498                         case 1:
499                                 buffer_strcat(wb, "\n\n# per plugin configuration\n");
500                                 break;
501
502                         case 2:
503                                 buffer_strcat(wb, "\n\n# per chart configuration\n");
504                                 break;
505                 }
506
507                 config_global_write_lock();
508                 for(co = config_root; co ; co = co->next) {
509                         if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0 || strcmp(co->name, "registry") == 0) pri = 0;
510                         else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
511                         else pri = 2;
512
513                         if(i == pri) {
514                                 int used = 0;
515                                 int changed = 0;
516                                 int count = 0;
517
518                                 config_section_write_lock(co);
519                                 for(cv = co->values; cv ; cv = cv->next) {
520                                         used += (cv->flags & CONFIG_VALUE_USED)?1:0;
521                                         changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
522                                         count++;
523                                 }
524                                 config_section_unlock(co);
525
526                                 if(!count) continue;
527                                 if(only_changed && !changed) continue;
528
529                                 if(!used) {
530                                         buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
531                                 }
532
533                                 buffer_sprintf(wb, "\n[%s]\n", co->name);
534
535                                 config_section_write_lock(co);
536                                 for(cv = co->values; cv ; cv = cv->next) {
537
538                                         if(used && !(cv->flags & CONFIG_VALUE_USED)) {
539                                                 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
540                                         }
541                                         buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
542                                 }
543                                 config_section_unlock(co);
544                         }
545                 }
546                 config_global_unlock();
547         }
548 }