Commit | Line | Data |
---|---|---|
3299fd31 SM |
1 | /* |
2 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "ini-config.h" | |
9 | ||
10 | #include <common/defaults.h> | |
11 | #include <common/error.h> | |
12 | #include <common/ini-config/ini.h> | |
13 | #include <common/macros.h> | |
14 | #include <common/utils.h> | |
15 | #include <ctype.h> | |
16 | ||
17 | LTTNG_EXPORT const char *config_str_yes = "yes"; | |
18 | LTTNG_EXPORT const char *config_str_true = "true"; | |
19 | LTTNG_EXPORT const char *config_str_on = "on"; | |
20 | LTTNG_EXPORT const char *config_str_no = "no"; | |
21 | LTTNG_EXPORT const char *config_str_false = "false"; | |
22 | LTTNG_EXPORT const char *config_str_off = "off"; | |
23 | ||
24 | struct handler_filter_args { | |
25 | const char* section; | |
26 | config_entry_handler_cb handler; | |
27 | void *user_data; | |
28 | }; | |
29 | ||
30 | static int config_entry_handler_filter(struct handler_filter_args *args, | |
31 | const char *section, const char *name, const char *value) | |
32 | { | |
33 | int ret = 0; | |
34 | struct config_entry entry = { section, name, value }; | |
35 | ||
36 | LTTNG_ASSERT(args); | |
37 | ||
38 | if (!section || !name || !value) { | |
39 | ret = -EIO; | |
40 | goto end; | |
41 | } | |
42 | ||
43 | if (args->section) { | |
44 | if (strcmp(args->section, section)) { | |
45 | goto end; | |
46 | } | |
47 | } | |
48 | ||
49 | ret = args->handler(&entry, args->user_data); | |
50 | end: | |
51 | return ret; | |
52 | } | |
53 | ||
54 | int config_get_section_entries(const char *override_path, const char *section, | |
55 | config_entry_handler_cb handler, void *user_data) | |
56 | { | |
57 | int ret = 0; | |
58 | const char *path; | |
59 | FILE *config_file = NULL; | |
60 | struct handler_filter_args filter = { section, handler, user_data }; | |
61 | ||
62 | /* First, try system-wide conf. file. */ | |
63 | path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH; | |
64 | ||
65 | config_file = fopen(path, "r"); | |
66 | if (config_file) { | |
67 | DBG("Loading daemon conf file at %s", path); | |
68 | /* | |
69 | * Return value is not very important here since error or not, we | |
70 | * continue and try the next possible conf. file. | |
71 | */ | |
72 | (void) ini_parse_file(config_file, | |
73 | (ini_entry_handler) config_entry_handler_filter, | |
74 | (void *) &filter); | |
75 | fclose(config_file); | |
76 | } | |
77 | ||
78 | /* Second is the user local configuration. */ | |
79 | path = utils_get_home_dir(); | |
80 | if (path) { | |
81 | char fullpath[PATH_MAX]; | |
82 | ||
83 | ret = snprintf(fullpath, sizeof(fullpath), | |
84 | DEFAULT_DAEMON_HOME_CONFIGPATH, path); | |
85 | if (ret < 0) { | |
86 | PERROR("snprintf user conf. path"); | |
87 | goto error; | |
88 | } | |
89 | ||
90 | config_file = fopen(fullpath, "r"); | |
91 | if (config_file) { | |
92 | DBG("Loading daemon user conf file at %s", path); | |
93 | /* | |
94 | * Return value is not very important here since error or not, we | |
95 | * continue and try the next possible conf. file. | |
96 | */ | |
97 | (void) ini_parse_file(config_file, | |
98 | (ini_entry_handler) config_entry_handler_filter, | |
99 | (void *) &filter); | |
100 | fclose(config_file); | |
101 | } | |
102 | } | |
103 | ||
104 | /* Final path is the one that the user might have provided. */ | |
105 | if (override_path) { | |
106 | config_file = fopen(override_path, "r"); | |
107 | if (config_file) { | |
108 | DBG("Loading daemon command line conf file at %s", override_path); | |
109 | (void) ini_parse_file(config_file, | |
110 | (ini_entry_handler) config_entry_handler_filter, | |
111 | (void *) &filter); | |
112 | fclose(config_file); | |
113 | } else { | |
114 | ERR("Failed to open daemon configuration file at %s", | |
115 | override_path); | |
116 | ret = -ENOENT; | |
117 | goto error; | |
118 | } | |
119 | } | |
120 | ||
121 | /* Everything went well. */ | |
122 | ret = 0; | |
123 | ||
124 | error: | |
125 | return ret; | |
126 | } | |
127 | ||
128 | int config_parse_value(const char *value) | |
129 | { | |
130 | int i, ret = 0; | |
131 | char *endptr, *lower_str; | |
132 | size_t len; | |
133 | unsigned long v; | |
134 | ||
135 | len = strlen(value); | |
136 | if (!len) { | |
137 | ret = -1; | |
138 | goto end; | |
139 | } | |
140 | ||
141 | v = strtoul(value, &endptr, 10); | |
142 | if (endptr != value) { | |
143 | ret = v; | |
144 | goto end; | |
145 | } | |
146 | ||
147 | lower_str = (char *) zmalloc(len + 1); | |
148 | if (!lower_str) { | |
149 | PERROR("zmalloc"); | |
150 | ret = -errno; | |
151 | goto end; | |
152 | } | |
153 | ||
154 | for (i = 0; i < len; i++) { | |
155 | lower_str[i] = tolower(value[i]); | |
156 | } | |
157 | ||
158 | if (!strcmp(lower_str, config_str_yes) || | |
159 | !strcmp(lower_str, config_str_true) || | |
160 | !strcmp(lower_str, config_str_on)) { | |
161 | ret = 1; | |
162 | } else if (!strcmp(lower_str, config_str_no) || | |
163 | !strcmp(lower_str, config_str_false) || | |
164 | !strcmp(lower_str, config_str_off)) { | |
165 | ret = 0; | |
166 | } else { | |
167 | ret = -1; | |
168 | } | |
169 | ||
170 | free(lower_str); | |
171 | end: | |
172 | return ret; | |
173 | } |