]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
layout: remove executable from unrelated files
[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 #ifdef AVL_LOCK_WITH_MUTEX
94                 PTHREAD_MUTEX_INITIALIZER
95 #else
96                 PTHREAD_RWLOCK_INITIALIZER
97 #endif
98 };
99
100 #define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg))
101 #define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg))
102
103 static struct config *config_index_find(const char *name, uint32_t hash) {
104         struct config *result = NULL, tmp;
105         tmp.hash = (hash)?hash:simple_hash(name);
106         tmp.name = (char *)name;
107
108         avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
109         return result;
110 }
111
112 struct config_value *config_value_create(struct config *co, const char *name, const char *value)
113 {
114         debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
115
116         struct config_value *cv = calloc(1, sizeof(struct config_value));
117         if(!cv) fatal("Cannot allocate config_value");
118
119         cv->name = strdup(name);
120         if(!cv->name) fatal("Cannot allocate config.name");
121         cv->hash = simple_hash(cv->name);
122
123         cv->value = strdup(value);
124         if(!cv->value) fatal("Cannot allocate config.value");
125
126         config_value_index_add(co, cv);
127
128         // no need for string termination, due to calloc()
129
130         pthread_rwlock_wrlock(&co->rwlock);
131
132         struct config_value *cv2 = co->values;
133         if(cv2) {
134                 while (cv2->next) cv2 = cv2->next;
135                 cv2->next = cv;
136         }
137         else co->values = cv;
138
139         pthread_rwlock_unlock(&co->rwlock);
140
141         return cv;
142 }
143
144 struct config *config_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         pthread_rwlock_init(&co->rwlock, NULL);
156         avl_init(&co->values_index, config_value_compare);
157
158         config_index_add(co);
159
160         // no need for string termination, due to calloc()
161
162         pthread_rwlock_wrlock(&config_rwlock);
163
164         struct config *co2 = config_root;
165         if(co2) {
166                 while (co2->next) co2 = co2->next;
167                 co2->next = co;
168         }
169         else config_root = co;
170
171         pthread_rwlock_unlock(&config_rwlock);
172
173         return co;
174 }
175
176 struct config *config_find_section(const char *section)
177 {
178         return config_index_find(section, 0);
179 }
180
181 int load_config(char *filename, int overwrite_used)
182 {
183         int line = 0;
184         struct config *co = NULL;
185
186         char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
187
188         if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
189         FILE *fp = fopen(filename, "r");
190         if(!fp) {
191                 error("Cannot open file '%s'", filename);
192                 return 0;
193         }
194
195         while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
196                 buffer[CONFIG_FILE_LINE_MAX] = '\0';
197                 line++;
198
199                 s = trim(buffer);
200                 if(!s) {
201                         debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
202                         continue;
203                 }
204
205                 int len = (int) strlen(s);
206                 if(*s == '[' && s[len - 1] == ']') {
207                         // new section
208                         s[len - 1] = '\0';
209                         s++;
210
211                         co = config_find_section(s);
212                         if(!co) co = config_create(s);
213
214                         continue;
215                 }
216
217                 if(!co) {
218                         // line outside a section
219                         error("Ignoring line %d ('%s'), it is outsize all sections.", line, s);
220                         continue;
221                 }
222
223                 char *name = s;
224                 char *value = strchr(s, '=');
225                 if(!value) {
226                         error("Ignoring line %d ('%s'), there is no = in it.", line, s);
227                         continue;
228                 }
229                 *value = '\0';
230                 value++;
231
232                 name = trim(name);
233                 value = trim(value);
234
235                 if(!name) {
236                         error("Ignoring line %d, name is empty.", line);
237                         continue;
238                 }
239                 if(!value) {
240                         debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
241                         continue;
242                 }
243
244                 struct config_value *cv = config_value_index_find(co, name, 0);
245
246                 if(!cv) cv = config_value_create(co, name, value);
247                 else {
248                         if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
249                                 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
250                                 free(cv->value);
251                                 cv->value = strdup(value);
252                                 if(!cv->value) fatal("Cannot allocate config.value");
253                         }
254                         else
255                                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
256                 }
257                 cv->flags |= CONFIG_VALUE_LOADED;
258         }
259
260         fclose(fp);
261
262         return 1;
263 }
264
265 char *config_get(const char *section, const char *name, const char *default_value)
266 {
267         struct config_value *cv;
268
269         debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
270
271         struct config *co = config_find_section(section);
272         if(!co) co = config_create(section);
273
274         cv = config_value_index_find(co, name, 0);
275         if(!cv) {
276                 cv = config_value_create(co, name, default_value);
277                 if(!cv) return NULL;
278         }
279         cv->flags |= CONFIG_VALUE_USED;
280
281         if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
282                 // this is a loaded value from the config file
283                 // if it is different that the default, mark it
284                 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
285                         if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
286                         cv->flags |= CONFIG_VALUE_CHECKED;
287                 }
288         }
289
290         return(cv->value);
291 }
292
293 long long config_get_number(const char *section, const char *name, long long value)
294 {
295         char buffer[100], *s;
296         sprintf(buffer, "%lld", value);
297
298         s = config_get(section, name, buffer);
299         if(!s) return value;
300
301         return strtoll(s, NULL, 0);
302 }
303
304 int config_get_boolean(const char *section, const char *name, int value)
305 {
306         char *s;
307         if(value) s = "yes";
308         else s = "no";
309
310         s = config_get(section, name, s);
311         if(!s) return value;
312
313         if(!strcmp(s, "yes")) return 1;
314         else return 0;
315 }
316
317 int config_get_boolean_ondemand(const char *section, const char *name, int value)
318 {
319         char *s;
320
321         if(value == CONFIG_ONDEMAND_ONDEMAND)
322                 s = "on demand";
323
324         else if(value == CONFIG_ONDEMAND_NO)
325                 s = "no";
326
327         else
328                 s = "yes";
329
330         s = config_get(section, name, s);
331         if(!s) return value;
332
333         if(!strcmp(s, "yes"))
334                 return CONFIG_ONDEMAND_YES;
335         else if(!strcmp(s, "no"))
336                 return CONFIG_ONDEMAND_NO;
337         else if(!strcmp(s, "on demand"))
338                 return CONFIG_ONDEMAND_ONDEMAND;
339
340         return value;
341 }
342
343 const char *config_set_default(const char *section, const char *name, const char *value)
344 {
345         struct config_value *cv;
346
347         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
348
349         struct config *co = config_find_section(section);
350         if(!co) return config_set(section, name, value);
351
352         cv = config_value_index_find(co, name, 0);
353         if(!cv) return config_set(section, name, value);
354
355         cv->flags |= CONFIG_VALUE_USED;
356
357         if(cv->flags & CONFIG_VALUE_LOADED)
358                 return cv->value;
359
360         if(strcmp(cv->value, value) != 0) {
361                 cv->flags |= CONFIG_VALUE_CHANGED;
362
363                 free(cv->value);
364                 cv->value = strdup(value);
365                 if(!cv->value) fatal("Cannot allocate config.value");
366         }
367
368         return cv->value;
369 }
370
371 const char *config_set(const char *section, const char *name, const char *value)
372 {
373         struct config_value *cv;
374
375         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
376
377         struct config *co = config_find_section(section);
378         if(!co) co = config_create(section);
379
380         cv = config_value_index_find(co, name, 0);
381         if(!cv) cv = config_value_create(co, name, value);
382         cv->flags |= CONFIG_VALUE_USED;
383
384         if(strcmp(cv->value, value) != 0) {
385                 cv->flags |= CONFIG_VALUE_CHANGED;
386
387                 free(cv->value);
388                 cv->value = strdup(value);
389                 if(!cv->value) fatal("Cannot allocate config.value");
390         }
391
392         return value;
393 }
394
395 long long config_set_number(const char *section, const char *name, long long value)
396 {
397         char buffer[100];
398         sprintf(buffer, "%lld", value);
399
400         config_set(section, name, buffer);
401
402         return value;
403 }
404
405 int config_set_boolean(const char *section, const char *name, int value)
406 {
407         char *s;
408         if(value) s = "yes";
409         else s = "no";
410
411         config_set(section, name, s);
412
413         return value;
414 }
415
416 void generate_config(BUFFER *wb, int only_changed)
417 {
418         int i, pri;
419         struct config *co;
420         struct config_value *cv;
421
422         for(i = 0; i < 3 ;i++) {
423                 switch(i) {
424                         case 0:
425                                 buffer_strcat(wb,
426                                         "# NetData Configuration\n"
427                                         "# You can uncomment and change any of the options below.\n"
428                                         "# The value shown in the commented settings, is the default value.\n"
429                                         "\n# global netdata configuration\n");
430                                 break;
431
432                         case 1:
433                                 buffer_strcat(wb, "\n\n# per plugin configuration\n");
434                                 break;
435
436                         case 2:
437                                 buffer_strcat(wb, "\n\n# per chart configuration\n");
438                                 break;
439                 }
440
441                 pthread_rwlock_wrlock(&config_rwlock);
442                 for(co = config_root; co ; co = co->next) {
443                         if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0) pri = 0;
444                         else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
445                         else pri = 2;
446
447                         if(i == pri) {
448                                 int used = 0;
449                                 int changed = 0;
450                                 int count = 0;
451
452                                 pthread_rwlock_wrlock(&co->rwlock);
453
454                                 for(cv = co->values; cv ; cv = cv->next) {
455                                         used += (cv->flags && CONFIG_VALUE_USED)?1:0;
456                                         changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
457                                         count++;
458                                 }
459
460                                 pthread_rwlock_unlock(&co->rwlock);
461
462                                 if(!count) continue;
463                                 if(only_changed && !changed) continue;
464
465                                 if(!used) {
466                                         buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
467                                 }
468
469                                 buffer_sprintf(wb, "\n[%s]\n", co->name);
470
471                                 pthread_rwlock_wrlock(&co->rwlock);
472                                 for(cv = co->values; cv ; cv = cv->next) {
473
474                                         if(used && !(cv->flags & CONFIG_VALUE_USED)) {
475                                                 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
476                                         }
477                                         buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
478                                 }
479                                 pthread_rwlock_unlock(&co->rwlock);
480                         }
481                 }
482                 pthread_rwlock_unlock(&config_rwlock);
483         }
484 }
485