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