2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <common/defaults.h>
28 #include <common/error.h>
29 #include <common/macros.h>
30 #include <common/utils.h>
34 struct handler_filter_args
{
36 config_entry_handler_cb handler
;
40 const char * const config_str_yes
= "yes";
41 const char * const config_str_true
= "true";
42 const char * const config_str_on
= "on";
43 const char * const config_str_no
= "no";
44 const char * const config_str_false
= "false";
45 const char * const config_str_off
= "off";
47 static int config_entry_handler_filter(struct handler_filter_args
*args
,
48 const char *section
, const char *name
, const char *value
)
51 struct config_entry entry
= { section
, name
, value
};
55 if (!section
|| !name
|| !value
) {
61 if (strcmp(args
->section
, section
)) {
66 ret
= args
->handler(&entry
, args
->user_data
);
72 int config_get_section_entries(const char *override_path
, const char *section
,
73 config_entry_handler_cb handler
, void *user_data
)
76 FILE *config_file
= NULL
;
77 struct handler_filter_args filter
= { section
, handler
, user_data
};
80 config_file
= fopen(override_path
, "r");
82 DBG("Loaded daemon configuration file at %s",
85 ERR("Failed to open daemon configuration file at %s",
91 char *path
= utils_get_home_dir();
93 /* Try to open the user's daemon configuration file */
95 ret
= asprintf(&path
, DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
101 config_file
= fopen(path
, "r");
103 DBG("Loaded daemon configuration file at %s", path
);
109 /* Try to open the system daemon configuration file */
111 config_file
= fopen(DEFAULT_DAEMON_HOME_CONFIGPATH
, "r");
116 DBG("No daemon configuration file found.");
120 ret
= ini_parse_file(config_file
,
121 (ini_entry_handler
) config_entry_handler_filter
, (void *) &filter
);
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
= 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
)) {