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>
18 LTTNG_EXPORT
const char *config_str_yes
= "yes";
19 LTTNG_EXPORT
const char *config_str_true
= "true";
20 LTTNG_EXPORT
const char *config_str_on
= "on";
21 LTTNG_EXPORT
const char *config_str_no
= "no";
22 LTTNG_EXPORT
const char *config_str_false
= "false";
23 LTTNG_EXPORT
const char *config_str_off
= "off";
26 struct handler_filter_args
{
28 config_entry_handler_cb handler
;
33 static int config_entry_handler_filter(struct handler_filter_args
*args
,
39 struct config_entry entry
= { section
, name
, value
};
43 if (!section
|| !name
|| !value
) {
49 if (strcmp(args
->section
, section
) != 0) {
54 ret
= args
->handler(&entry
, args
->user_data
);
59 int config_get_section_entries(const char *override_path
,
61 config_entry_handler_cb handler
,
66 FILE *config_file
= nullptr;
67 struct handler_filter_args filter
= { section
, handler
, user_data
};
69 /* First, try system-wide conf. file. */
70 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
72 config_file
= fopen(path
, "r");
74 DBG("Loading daemon conf file at %s", path
);
76 * Return value is not very important here since error or not, we
77 * continue and try the next possible conf. file.
79 (void) ini_parse_file(config_file
,
80 (ini_entry_handler
) config_entry_handler_filter
,
85 /* Second is the user local configuration. */
86 path
= utils_get_home_dir();
88 char fullpath
[PATH_MAX
];
90 ret
= snprintf(fullpath
, sizeof(fullpath
), DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
92 PERROR("snprintf user conf. path");
96 config_file
= fopen(fullpath
, "r");
98 DBG("Loading daemon user conf file at %s", path
);
100 * Return value is not very important here since error or not, we
101 * continue and try the next possible conf. file.
103 (void) ini_parse_file(config_file
,
104 (ini_entry_handler
) config_entry_handler_filter
,
110 /* Final path is the one that the user might have provided. */
112 config_file
= fopen(override_path
, "r");
114 DBG("Loading daemon command line conf file at %s", override_path
);
115 (void) ini_parse_file(config_file
,
116 (ini_entry_handler
) config_entry_handler_filter
,
120 ERR("Failed to open daemon configuration file at %s", override_path
);
126 /* Everything went well. */
133 int config_parse_value(const char *value
)
136 char *endptr
, *lower_str
;
146 v
= strtoul(value
, &endptr
, 10);
147 if (endptr
!= value
) {
152 lower_str
= zmalloc
<char>(len
+ 1);
159 for (i
= 0; i
< len
; i
++) {
160 lower_str
[i
] = tolower(value
[i
]);
163 if (!strcmp(lower_str
, config_str_yes
) || !strcmp(lower_str
, config_str_true
) ||
164 !strcmp(lower_str
, config_str_on
)) {
166 } else if (!strcmp(lower_str
, config_str_no
) || !strcmp(lower_str
, config_str_false
) ||
167 !strcmp(lower_str
, config_str_off
)) {