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