2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "ini-config.h"
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>
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";
24 struct handler_filter_args
{
26 config_entry_handler_cb handler
;
30 static int config_entry_handler_filter(struct handler_filter_args
*args
,
31 const char *section
, const char *name
, const char *value
)
34 struct config_entry entry
= { section
, name
, value
};
38 if (!section
|| !name
|| !value
) {
44 if (strcmp(args
->section
, section
)) {
49 ret
= args
->handler(&entry
, args
->user_data
);
54 int config_get_section_entries(const char *override_path
, const char *section
,
55 config_entry_handler_cb handler
, void *user_data
)
59 FILE *config_file
= NULL
;
60 struct handler_filter_args filter
= { section
, handler
, user_data
};
62 /* First, try system-wide conf. file. */
63 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
65 config_file
= fopen(path
, "r");
67 DBG("Loading daemon conf file at %s", path
);
69 * Return value is not very important here since error or not, we
70 * continue and try the next possible conf. file.
72 (void) ini_parse_file(config_file
,
73 (ini_entry_handler
) config_entry_handler_filter
,
78 /* Second is the user local configuration. */
79 path
= utils_get_home_dir();
81 char fullpath
[PATH_MAX
];
83 ret
= snprintf(fullpath
, sizeof(fullpath
),
84 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
86 PERROR("snprintf user conf. path");
90 config_file
= fopen(fullpath
, "r");
92 DBG("Loading daemon user conf file at %s", path
);
94 * Return value is not very important here since error or not, we
95 * continue and try the next possible conf. file.
97 (void) ini_parse_file(config_file
,
98 (ini_entry_handler
) config_entry_handler_filter
,
104 /* Final path is the one that the user might have provided. */
106 config_file
= fopen(override_path
, "r");
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
,
114 ERR("Failed to open daemon configuration file at %s",
121 /* Everything went well. */
128 int config_parse_value(const char *value
)
131 char *endptr
, *lower_str
;
141 v
= strtoul(value
, &endptr
, 10);
142 if (endptr
!= value
) {
147 lower_str
= (char *) zmalloc(len
+ 1);
154 for (i
= 0; i
< len
; i
++) {
155 lower_str
[i
] = tolower(value
[i
]);
158 if (!strcmp(lower_str
, config_str_yes
) ||
159 !strcmp(lower_str
, config_str_true
) ||
160 !strcmp(lower_str
, config_str_on
)) {
162 } else if (!strcmp(lower_str
, config_str_no
) ||
163 !strcmp(lower_str
, config_str_false
) ||
164 !strcmp(lower_str
, config_str_off
)) {