edit-config should use . instead of source with /bin/sh (#4360)
* source is . in /bin/sh * code cleanup and documentation
Costa Tsaousis committed
Oct 6, 2018 at 21:08 UTC
a441c0a2c8726b145c0a3fe62319d6fed71269a1
3 files changed
+89
-8
conf.d/edit-config.in
+1
-1
@@ -1,6 +1,6 @@
1
#!/usr/bin/env sh
2
3
-[ -f /etc/profile ] && source /etc/profile
3
+[ -f /etc/profile ] && . /etc/profile
4
5
file="${1}"
6
src/appconfig.c
+13
-7
@@ -9,11 +9,11 @@
9
10
#define CONFIG_VALUE_LOADED 0x01 // has been loaded from the config
11
#define CONFIG_VALUE_USED 0x02 // has been accessed from the program
12
-#define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value
12
+#define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value or the internal default value
13
#define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default
14
15
struct config_option {
16
- avl avl; // the index - this has to be first!
16
+ avl avl; // the index entry of this entry - this has to be first!
17
18
uint8_t flags;
19
uint32_t hash; // a simple hash to speed up searching
@@ -26,7 +26,7 @@ struct config_option {
26
};
27
28
struct section {
29
- avl avl;
29
+ avl avl; // the index entry of this section - this has to be first!
30
31
uint32_t hash; // a simple hash to speed up searching
32
// we first compare hashes, and only if the hashes are equal we do string comparisons
@@ -48,8 +48,11 @@ struct config netdata_config = {
48
.sections = NULL,
49
.mutex = NETDATA_MUTEX_INITIALIZER,
50
.index = {
51
- { NULL, appconfig_section_compare },
52
- AVL_LOCK_INITIALIZER
51
+ .avl_tree = {
52
+ .root = NULL,
53
+ .compar = appconfig_section_compare
54
+ },
55
+ .rwlock = AVL_LOCK_INITIALIZER
56
}
57
};
58
@@ -57,8 +60,11 @@ struct config stream_config = {
60
.sections = NULL,
61
.mutex = NETDATA_MUTEX_INITIALIZER,
62
.index = {
60
- { NULL, appconfig_section_compare },
61
- AVL_LOCK_INITIALIZER
63
+ .avl_tree = {
64
+ .root = NULL,
65
+ .compar = appconfig_section_compare
66
+ },
67
+ .rwlock = AVL_LOCK_INITIALIZER
68
}
69
};
70
src/appconfig.h
+75
@@ -1,5 +1,80 @@
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
+/*
4
+ * This section manages ini config files, like netdata.conf and stream.conf
5
+ *
6
+ * It is organized like this:
7
+ *
8
+ * struct config (i.e. netdata.conf or stream.conf)
9
+ * .sections = a linked list of struct section
10
+ * .mutex = a mutex to protect the above linked list due to multi-threading
11
+ * .index = an AVL tree of struct section
12
+ *
13
+ * struct section (i.e. [global] or [health] of netdata.conf)
14
+ * .value = a linked list of struct config_option
15
+ * .mutex = a mutex to protect the above linked list due to multi-threading
16
+ * .value_index = an AVL tree of struct config_option
17
+ *
18
+ * struct config_option (ie. a name-value pair for each ini file option)
19
+ *
20
+ * The following operations on name-value options are supported:
21
+ * SET to set the value of an option
22
+ * SET DEFAULT to set the value and the default value of an option
23
+ * GET to get the value of an option
24
+ * EXISTS to check if an option exists
25
+ * MOVE to move an option from a section to another section, and/or rename it
26
+ *
27
+ * GET and SET operations are provided for the following data types:
28
+ * STRING
29
+ * NUMBER (long long)
30
+ * FLOAT (long double)
31
+ * BOOLEAN (false, true)
32
+ * BOOLEAN ONDEMAND (false, true, auto)
33
+ *
34
+ * GET and SET operations create struct config_option, if it is not already present.
35
+ * This allows netdata to run even without netdata.conf and stream.conf. The internal
36
+ * defaults are used to create the structure that should exist in the ini file and the config
37
+ * file can be downloaded from the server.
38
+ *
39
+ * Also 2 operations are supported for the whole config file:
40
+ *
41
+ * LOAD To load the ini file from disk
42
+ * GENERATE To generate the ini file (this is used to download the ini file from the server)
43
+ *
44
+ * For each option (name-value pair), the system maintains 4 flags:
45
+ * LOADED to indicate that the value has been loaded from the file
46
+ * USED to indicate that netdata used the value
47
+ * CHANGED to indicate that the value has been changed from the loaded value or the internal default value
48
+ * CHECKED is used internally for optimization (to avoid an strcmp() every time GET is called).
49
+ *
50
+ * TODO:
51
+ * 1. The linked lists and the mutexes can be removed and the AVL trees can become DICTIONARY.
52
+ * This part of the code was written before we add traversal to AVL.
53
+ *
54
+ * 2. High level data types could be supported, to simplify the rest of the code:
55
+ * MULTIPLE CHOICE to let the user select one of the supported keywords
56
+ * this would allow users see in comments the available options
57
+ *
58
+ * SIMPLE PATTERN to let the user define netdata SIMPLE PATTERNS
59
+ *
60
+ * 3. Sorting of options should be supported.
61
+ * Today, when the ini file is downloaded from the server, the options are shown in the order
62
+ * they appear in the linked list (the order they were added, listing changed options first).
63
+ * If we remove the linked list, the order they appear in the AVL tree will be used (which is
64
+ * random due to simple_hash()).
65
+ * Ideally, we support sorting of options when generating the ini file.
66
+ *
67
+ * 4. There is no free() operation. So, memory is freed on netdata exit.
68
+ *
69
+ * 5. Avoid memory fragmentation
70
+ * Since entries are created from multiple threads and a lot of allocations are required
71
+ * for each config_option, fragmentation can be a problem for IoT.
72
+ *
73
+ * 6. Although this way of managing options is quite flexible and dynamic, it wastes memory
74
+ * for the names of the options. Since most of the option names are static, we could provide
75
+ * a method to allocate only the dynamic option names.
76
+ */
77
+
78
#include "common.h"
79
80
#ifndef NETDATA_CONFIG_H