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