2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "ini-config.hpp"
10 #include <common/defaults.hpp>
11 #include <common/error.hpp>
12 #include <common/ini-config/ini.hpp>
13 #include <common/macros.hpp>
14 #include <common/utils.hpp>
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";
25 struct handler_filter_args
{
27 config_entry_handler_cb handler
;
32 static int config_entry_handler_filter(struct handler_filter_args
*args
,
33 const char *section
, const char *name
, const char *value
)
36 struct config_entry entry
= { section
, name
, value
};
40 if (!section
|| !name
|| !value
) {
46 if (strcmp(args
->section
, section
)) {
51 ret
= args
->handler(&entry
, args
->user_data
);
56 int config_get_section_entries(const char *override_path
, const char *section
,
57 config_entry_handler_cb handler
, void *user_data
)
61 FILE *config_file
= NULL
;
62 struct handler_filter_args filter
= { section
, handler
, user_data
};
64 /* First, try system-wide conf. file. */
65 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
67 config_file
= fopen(path
, "r");
69 DBG("Loading daemon conf file at %s", path
);
71 * Return value is not very important here since error or not, we
72 * continue and try the next possible conf. file.
74 (void) ini_parse_file(config_file
,
75 (ini_entry_handler
) config_entry_handler_filter
,
80 /* Second is the user local configuration. */
81 path
= utils_get_home_dir();
83 char fullpath
[PATH_MAX
];
85 ret
= snprintf(fullpath
, sizeof(fullpath
),
86 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
88 PERROR("snprintf user conf. path");
92 config_file
= fopen(fullpath
, "r");
94 DBG("Loading daemon user conf file at %s", path
);
96 * Return value is not very important here since error or not, we
97 * continue and try the next possible conf. file.
99 (void) ini_parse_file(config_file
,
100 (ini_entry_handler
) config_entry_handler_filter
,
106 /* Final path is the one that the user might have provided. */
108 config_file
= fopen(override_path
, "r");
110 DBG("Loading daemon command line conf file at %s", override_path
);
111 (void) ini_parse_file(config_file
,
112 (ini_entry_handler
) config_entry_handler_filter
,
116 ERR("Failed to open daemon configuration file at %s",
123 /* Everything went well. */
130 int config_parse_value(const char *value
)
133 char *endptr
, *lower_str
;
143 v
= strtoul(value
, &endptr
, 10);
144 if (endptr
!= value
) {
149 lower_str
= zmalloc
<char>(len
+ 1);
156 for (i
= 0; i
< len
; i
++) {
157 lower_str
[i
] = tolower(value
[i
]);
160 if (!strcmp(lower_str
, config_str_yes
) ||
161 !strcmp(lower_str
, config_str_true
) ||
162 !strcmp(lower_str
, config_str_on
)) {
164 } else if (!strcmp(lower_str
, config_str_no
) ||
165 !strcmp(lower_str
, config_str_false
) ||
166 !strcmp(lower_str
, config_str_off
)) {