]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
avl is now threat safe (found a condition where the trees could cause netdata to...
[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 } *config_root = NULL;
52
53
54 // ----------------------------------------------------------------------------
55 // config value
56
57 static int config_value_iterator(avl *a) { if(a) {}; return 0; }
58
59 static int config_value_compare(void* a, void* b) {
60         if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
61         else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1;
62         else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name);
63 }
64
65 #define config_value_index_add(co, cv) avl_insert(&((co)->values_index), (avl *)(cv))
66 #define config_value_index_del(co, cv) avl_remove(&((co)->values_index), (avl *)(cv))
67
68 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
69         struct config_value *result = NULL, tmp;
70         tmp.hash = (hash)?hash:simple_hash(name);
71         tmp.name = (char *)name;
72
73         avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result);
74         return result;
75 }
76
77 // ----------------------------------------------------------------------------
78 // config
79
80 static int config_iterator(avl *a) { if(a) {}; return 0; }
81
82 static int config_compare(void* a, void* b) {
83         if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
84         else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
85         else return strcmp(((struct config *)a)->name, ((struct config *)b)->name);
86 }
87
88 avl_tree config_root_index = {
89                 NULL,
90                 config_compare,
91                 PTHREAD_RWLOCK_INITIALIZER
92 };
93
94 #define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg))
95 #define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg))
96
97 static struct config *config_index_find(const char *name, uint32_t hash) {
98         struct config *result = NULL, tmp;
99         tmp.hash = (hash)?hash:simple_hash(name);
100         tmp.name = (char *)name;
101
102         avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
103         return result;
104 }
105
106 struct config_value *config_value_create(struct config *co, const char *name, const char *value)
107 {
108         debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
109
110         struct config_value *cv = calloc(1, sizeof(struct config_value));
111         if(!cv) fatal("Cannot allocate config_value");
112
113         cv->name = strdup(name);
114         if(!cv->name) fatal("Cannot allocate config.name");
115         cv->hash = simple_hash(cv->name);
116
117         cv->value = strdup(value);
118         if(!cv->value) fatal("Cannot allocate config.value");
119
120         config_value_index_add(co, cv);
121
122         // no need for string termination, due to calloc()
123
124         struct config_value *cv2 = co->values;
125         if(cv2) {
126                 while (cv2->next) cv2 = cv2->next;
127                 cv2->next = cv;
128         }
129         else co->values = cv;
130
131         return cv;
132 }
133
134 struct config *config_create(const char *section)
135 {
136         debug(D_CONFIG, "Creating section '%s'.", section);
137
138         struct config *co = calloc(1, sizeof(struct config));
139         if(!co) fatal("Cannot allocate config");
140
141         co->name = strdup(section);
142         if(!co->name) fatal("Cannot allocate config.name");
143         co->hash = simple_hash(co->name);
144
145         avl_init(&co->values_index, config_value_compare);
146
147         config_index_add(co);
148
149         // no need for string termination, due to calloc()
150
151         struct config *co2 = config_root;
152         if(co2) {
153                 while (co2->next) co2 = co2->next;
154                 co2->next = co;
155         }
156         else config_root = co;
157
158         return co;
159 }
160
161 struct config *config_find_section(const char *section)
162 {
163         return config_index_find(section, 0);
164 }
165
166 int load_config(char *filename, int overwrite_used)
167 {
168         int line = 0;
169         struct config *co = NULL;
170
171         pthread_rwlock_wrlock(&config_rwlock);
172
173         char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
174
175         if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
176         FILE *fp = fopen(filename, "r");
177         if(!fp) {
178                 error("Cannot open file '%s'", CONFIG_DIR "/" CONFIG_FILENAME);
179                 pthread_rwlock_unlock(&config_rwlock);
180                 return 0;
181         }
182
183         while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
184                 buffer[CONFIG_FILE_LINE_MAX] = '\0';
185                 line++;
186
187                 s = trim(buffer);
188                 if(!s) {
189                         debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
190                         continue;
191                 }
192
193                 int len = strlen(s);
194                 if(*s == '[' && s[len - 1] == ']') {
195                         // new section
196                         s[len - 1] = '\0';
197                         s++;
198
199                         co = config_find_section(s);
200                         if(!co) co = config_create(s);
201
202                         continue;
203                 }
204
205                 if(!co) {
206                         // line outside a section
207                         error("Ignoring line %d ('%s'), it is outsize all sections.", line, s);
208                         continue;
209                 }
210
211                 char *name = s;
212                 char *value = strchr(s, '=');
213                 if(!value) {
214                         error("Ignoring line %d ('%s'), there is no = in it.", line, s);
215                         continue;
216                 }
217                 *value = '\0';
218                 value++;
219
220                 name = trim(name);
221                 value = trim(value);
222
223                 if(!name) {
224                         error("Ignoring line %d, name is empty.", line);
225                         continue;
226                 }
227                 if(!value) {
228                         debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
229                         continue;
230                 }
231
232                 struct config_value *cv = config_value_index_find(co, name, 0);
233
234                 if(!cv) cv = config_value_create(co, name, value);
235                 else {
236                         if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
237                                 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
238                                 free(cv->value);
239                                 cv->value = strdup(value);
240                                 if(!cv->value) fatal("Cannot allocate config.value");
241                         }
242                         else
243                                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
244                 }
245                 cv->flags |= CONFIG_VALUE_LOADED;
246         }
247
248         fclose(fp);
249
250         pthread_rwlock_unlock(&config_rwlock);
251         return 1;
252 }
253
254 char *config_get(const char *section, const char *name, const char *default_value)
255 {
256         struct config_value *cv;
257
258         debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
259
260         pthread_rwlock_rdlock(&config_rwlock);
261
262         struct config *co = config_find_section(section);
263         if(!co) co = config_create(section);
264
265         cv = config_value_index_find(co, name, 0);
266         if(!cv) {
267                 cv = config_value_create(co, name, default_value);
268                 if(!cv) return NULL;
269         }
270         cv->flags |= CONFIG_VALUE_USED;
271
272         if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
273                 // this is a loaded value from the config file
274                 // if it is different that the default, mark it
275                 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
276                         if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
277                         cv->flags |= CONFIG_VALUE_CHECKED;
278                 }
279         }
280
281         pthread_rwlock_unlock(&config_rwlock);
282         return(cv->value);
283 }
284
285 long long config_get_number(const char *section, const char *name, long long value)
286 {
287         char buffer[100], *s;
288         sprintf(buffer, "%lld", value);
289
290         s = config_get(section, name, buffer);
291         if(!s) return 0;
292
293         return strtoll(s, NULL, 0);
294 }
295
296 int config_get_boolean(const char *section, const char *name, int value)
297 {
298         char *s;
299         if(value) s = "yes";
300         else s = "no";
301
302         s = config_get(section, name, s);
303         if(!s) return 0;
304
305         if(!strcmp(s, "yes")) return 1;
306         else return 0;
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         pthread_rwlock_wrlock(&config_rwlock);
316
317         struct config *co = config_find_section(section);
318         if(!co) co = config_create(section);
319
320         cv = config_value_index_find(co, name, 0);
321         if(!cv) cv = config_value_create(co, name, value);
322         cv->flags |= CONFIG_VALUE_USED;
323
324         if(strcmp(cv->value, value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
325
326         free(cv->value);
327         cv->value = strdup(value);
328         if(!cv->value) fatal("Cannot allocate config.value");
329
330         pthread_rwlock_unlock(&config_rwlock);
331
332         return value;
333 }
334
335 long long config_set_number(const char *section, const char *name, long long value)
336 {
337         char buffer[100];
338         sprintf(buffer, "%lld", value);
339
340         config_set(section, name, buffer);
341
342         return value;
343 }
344
345 int config_set_boolean(const char *section, const char *name, int value)
346 {
347         char *s;
348         if(value) s = "yes";
349         else s = "no";
350
351         config_set(section, name, s);
352
353         return value;
354 }
355
356 void generate_config(struct web_buffer *wb, int only_changed)
357 {
358         int i, pri;
359         struct config *co;
360         struct config_value *cv;
361
362         for(i = 0; i < 3 ;i++) {
363                 switch(i) {
364                         case 0:
365                                 web_buffer_strcat(wb,
366                                         "# NetData Configuration\n"
367                                         "# You can uncomment and change any of the options below.\n"
368                                         "# The value shown in the commented settings, is the default value.\n"
369                                         "\n# global netdata configuration\n");
370                                 break;
371
372                         case 1:
373                                 web_buffer_strcat(wb, "\n\n# per plugin configuration\n");
374                                 break;
375
376                         case 2:
377                                 web_buffer_strcat(wb, "\n\n# per chart configuration\n");
378                                 break;
379                 }
380
381                 for(co = config_root; co ; co = co->next) {
382                         if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0) pri = 0;
383                         else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
384                         else pri = 2;
385
386                         if(i == pri) {
387                                 int used = 0;
388                                 int changed = 0;
389                                 int count = 0;
390                                 for(cv = co->values; cv ; cv = cv->next) {
391                                         used += (cv->flags && CONFIG_VALUE_USED)?1:0;
392                                         changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
393                                         count++;
394                                 }
395
396                                 if(!count) continue;
397                                 if(only_changed && !changed) continue;
398
399                                 if(!used) {
400                                         web_buffer_snprintf(wb, CONFIG_FILE_LINE_MAX+1, "\n# node '%s' is not used.", co->name);
401                                 }
402
403                                 web_buffer_snprintf(wb, CONFIG_FILE_LINE_MAX+1, "\n[%s]\n", co->name);
404
405                                 for(cv = co->values; cv ; cv = cv->next) {
406
407                                         if(used && !(cv->flags & CONFIG_VALUE_USED)) {
408                                                 web_buffer_snprintf(wb, CONFIG_FILE_LINE_MAX + 1, "\n\t# option '%s' is not used.\n", cv->name);
409                                         }
410                                         web_buffer_snprintf(wb, CONFIG_FILE_LINE_MAX + 1, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
411                                 }
412                         }
413                 }
414         }
415 }
416