]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
Merge pull request #405 from ktsaou/registry
[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_iterator(avl *a) { if(a) {}; return 0; }
88
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);
93 }
94
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))
97
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;
102
103         avl_search_lock(&(co->values_index), (avl *) &tmp, config_value_iterator, (avl **) &result);
104         return result;
105 }
106
107
108 // ----------------------------------------------------------------------------
109 // config sections index
110
111 static int config_iterator(avl *a) { if(a) {}; return 0; }
112
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);
117 }
118
119 avl_tree_lock config_root_index = {
120                 { NULL, config_compare },
121                 AVL_LOCK_INITIALIZER
122 };
123
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))
126
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;
131
132         avl_search_lock(&config_root_index, (avl *) &tmp, config_iterator, (avl **) &result);
133         return result;
134 }
135
136
137 // ----------------------------------------------------------------------------
138 // config section methods
139
140 static inline struct config *config_section_find(const char *section) {
141         return config_index_find(section, 0);
142 }
143
144 static inline struct config *config_section_create(const char *section)
145 {
146         debug(D_CONFIG, "Creating section '%s'.", section);
147
148         struct config *co = calloc(1, sizeof(struct config));
149         if(!co) fatal("Cannot allocate config");
150
151         co->name = strdup(section);
152         if(!co->name) fatal("Cannot allocate config.name");
153         co->hash = simple_hash(co->name);
154
155         avl_init_lock(&co->values_index, config_value_compare);
156
157         config_index_add(co);
158
159         config_global_write_lock();
160         struct config *co2 = config_root;
161         if(co2) {
162                 while (co2->next) co2 = co2->next;
163                 co2->next = co;
164         }
165         else config_root = co;
166         config_global_unlock();
167
168         return co;
169 }
170
171
172 // ----------------------------------------------------------------------------
173 // config name-value methods
174
175 static inline struct config_value *config_value_create(struct config *co, const char *name, const char *value)
176 {
177         debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
178
179         struct config_value *cv = calloc(1, sizeof(struct config_value));
180         if(!cv) fatal("Cannot allocate config_value");
181
182         cv->name = strdup(name);
183         if(!cv->name) fatal("Cannot allocate config.name");
184         cv->hash = simple_hash(cv->name);
185
186         cv->value = strdup(value);
187         if(!cv->value) fatal("Cannot allocate config.value");
188
189         config_value_index_add(co, cv);
190
191         config_section_write_lock(co);
192         struct config_value *cv2 = co->values;
193         if(cv2) {
194                 while (cv2->next) cv2 = cv2->next;
195                 cv2->next = cv;
196         }
197         else co->values = cv;
198         config_section_unlock(co);
199
200         return cv;
201 }
202
203 char *config_get(const char *section, const char *name, const char *default_value)
204 {
205         struct config_value *cv;
206
207         debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
208
209         struct config *co = config_section_find(section);
210         if(!co) co = config_section_create(section);
211
212         cv = config_value_index_find(co, name, 0);
213         if(!cv) {
214                 cv = config_value_create(co, name, default_value);
215                 if(!cv) return NULL;
216         }
217         cv->flags |= CONFIG_VALUE_USED;
218
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;
225                 }
226         }
227
228         return(cv->value);
229 }
230
231 long long config_get_number(const char *section, const char *name, long long value)
232 {
233         char buffer[100], *s;
234         sprintf(buffer, "%lld", value);
235
236         s = config_get(section, name, buffer);
237         if(!s) return value;
238
239         return strtoll(s, NULL, 0);
240 }
241
242 int config_get_boolean(const char *section, const char *name, int value)
243 {
244         char *s;
245         if(value) s = "yes";
246         else s = "no";
247
248         s = config_get(section, name, s);
249         if(!s) return value;
250
251         if(!strcmp(s, "yes")) return 1;
252         else return 0;
253 }
254
255 int config_get_boolean_ondemand(const char *section, const char *name, int value)
256 {
257         char *s;
258
259         if(value == CONFIG_ONDEMAND_ONDEMAND)
260                 s = "on demand";
261
262         else if(value == CONFIG_ONDEMAND_NO)
263                 s = "no";
264
265         else
266                 s = "yes";
267
268         s = config_get(section, name, s);
269         if(!s) return value;
270
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;
277
278         return value;
279 }
280
281 const char *config_set_default(const char *section, const char *name, const char *value)
282 {
283         struct config_value *cv;
284
285         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
286
287         struct config *co = config_section_find(section);
288         if(!co) return config_set(section, name, value);
289
290         cv = config_value_index_find(co, name, 0);
291         if(!cv) return config_set(section, name, value);
292
293         cv->flags |= CONFIG_VALUE_USED;
294
295         if(cv->flags & CONFIG_VALUE_LOADED)
296                 return cv->value;
297
298         if(strcmp(cv->value, value) != 0) {
299                 cv->flags |= CONFIG_VALUE_CHANGED;
300
301                 free(cv->value);
302                 cv->value = strdup(value);
303                 if(!cv->value) fatal("Cannot allocate config.value");
304         }
305
306         return cv->value;
307 }
308
309 const char *config_set(const char *section, const char *name, const char *value)
310 {
311         struct config_value *cv;
312
313         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
314
315         struct config *co = config_section_find(section);
316         if(!co) co = config_section_create(section);
317
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;
321
322         if(strcmp(cv->value, value) != 0) {
323                 cv->flags |= CONFIG_VALUE_CHANGED;
324
325                 free(cv->value);
326                 cv->value = strdup(value);
327                 if(!cv->value) fatal("Cannot allocate config.value");
328         }
329
330         return value;
331 }
332
333 long long config_set_number(const char *section, const char *name, long long value)
334 {
335         char buffer[100];
336         sprintf(buffer, "%lld", value);
337
338         config_set(section, name, buffer);
339
340         return value;
341 }
342
343 int config_set_boolean(const char *section, const char *name, int value)
344 {
345         char *s;
346         if(value) s = "yes";
347         else s = "no";
348
349         config_set(section, name, s);
350
351         return value;
352 }
353
354
355 // ----------------------------------------------------------------------------
356 // config load/save
357
358 int load_config(char *filename, int overwrite_used)
359 {
360         int line = 0;
361         struct config *co = NULL;
362
363         char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
364
365         if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
366         FILE *fp = fopen(filename, "r");
367         if(!fp) {
368                 error("Cannot open file '%s'", filename);
369                 return 0;
370         }
371
372         while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
373                 buffer[CONFIG_FILE_LINE_MAX] = '\0';
374                 line++;
375
376                 s = trim(buffer);
377                 if(!s) {
378                         debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
379                         continue;
380                 }
381
382                 int len = (int) strlen(s);
383                 if(*s == '[' && s[len - 1] == ']') {
384                         // new section
385                         s[len - 1] = '\0';
386                         s++;
387
388                         co = config_section_find(s);
389                         if(!co) co = config_section_create(s);
390
391                         continue;
392                 }
393
394                 if(!co) {
395                         // line outside a section
396                         error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
397                         continue;
398                 }
399
400                 char *name = s;
401                 char *value = strchr(s, '=');
402                 if(!value) {
403                         error("Ignoring line %d ('%s'), there is no = in it.", line, s);
404                         continue;
405                 }
406                 *value = '\0';
407                 value++;
408
409                 name = trim(name);
410                 value = trim(value);
411
412                 if(!name) {
413                         error("Ignoring line %d, name is empty.", line);
414                         continue;
415                 }
416                 if(!value) {
417                         debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
418                         continue;
419                 }
420
421                 struct config_value *cv = config_value_index_find(co, name, 0);
422
423                 if(!cv) cv = config_value_create(co, name, value);
424                 else {
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);
427                                 free(cv->value);
428                                 cv->value = strdup(value);
429                                 if(!cv->value) fatal("Cannot allocate config.value");
430                         }
431                         else
432                                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
433                 }
434                 cv->flags |= CONFIG_VALUE_LOADED;
435         }
436
437         fclose(fp);
438
439         return 1;
440 }
441
442 void generate_config(BUFFER *wb, int only_changed)
443 {
444         int i, pri;
445         struct config *co;
446         struct config_value *cv;
447
448         for(i = 0; i < 3 ;i++) {
449                 switch(i) {
450                         case 0:
451                                 buffer_strcat(wb,
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");
456                                 break;
457
458                         case 1:
459                                 buffer_strcat(wb, "\n\n# per plugin configuration\n");
460                                 break;
461
462                         case 2:
463                                 buffer_strcat(wb, "\n\n# per chart configuration\n");
464                                 break;
465                 }
466
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;
471                         else pri = 2;
472
473                         if(i == pri) {
474                                 int used = 0;
475                                 int changed = 0;
476                                 int count = 0;
477
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;
482                                         count++;
483                                 }
484                                 config_section_unlock(co);
485
486                                 if(!count) continue;
487                                 if(only_changed && !changed) continue;
488
489                                 if(!used) {
490                                         buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
491                                 }
492
493                                 buffer_sprintf(wb, "\n[%s]\n", co->name);
494
495                                 config_section_write_lock(co);
496                                 for(cv = co->values; cv ; cv = cv->next) {
497
498                                         if(used && !(cv->flags & CONFIG_VALUE_USED)) {
499                                                 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
500                                         }
501                                         buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
502                                 }
503                                 config_section_unlock(co);
504                         }
505                 }
506                 config_global_unlock();
507         }
508 }