diff --git a/src/conf.c b/src/conf.c index 44e70d4..77b08d0 100644 --- a/src/conf.c +++ b/src/conf.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -701,9 +702,17 @@ int conf_parse_file(struct conf_option *opts, size_t len, const char *path) size_t size, pos; char *buf, *tmp; - if (!path) + if (!opts || !len || !path) return -EINVAL; + if (access(path, F_OK)) + return 0; + + if (access(path, R_OK)) { + log_error("read access to config file %s denied", path); + return -EACCES; + } + log_info("reading config file %s", path); fd = open(path, O_RDONLY | O_CLOEXEC | O_NOCTTY); if (fd < 0) { @@ -747,45 +756,46 @@ out_free: return ret; } -int conf_parse_all_files(struct conf_option *opts, size_t len) +int conf_parse_file_f(struct conf_option *opts, size_t len, + const char *format, ...) { - int ret; - const char *file, *home; + va_list list; char *path; + int ret; - ret = 0; + if (!opts || !len || !format) + return -EINVAL; - file = "/etc/kmscon.conf"; - if (!access(file, F_OK)) { - if (access(file, R_OK)) - log_warning("config file %s exists but read access was denied", - file); - else - ret = conf_parse_file(opts, len, file); + va_start(list, format); + ret = vasprintf(&path, format, list); + va_end(list); + + if (ret < 0) { + log_error("cannot allocate memory for config-file path"); + return -ENOMEM; } + ret = conf_parse_file(opts, len, path); + free(path); + return ret; +} + +int conf_parse_standard_files(struct conf_option *opts, size_t len, + const char *fname) +{ + int ret; + const char *home; + + ret = conf_parse_file_f(opts, len, "/etc/%s.conf", fname); if (ret) - goto err_out; + return ret; home = getenv("HOME"); if (home) { - ret = asprintf(&path, "%s/.kmscon.conf", home); - if (ret < 0) { - log_warning("cannot allocate enough resources to build a config-path"); - ret = -ENOMEM; - } else { - ret = 0; - if (!access(path, F_OK)) { - if (access(path, R_OK)) - log_warning("config file %s exists but read access was denied", - path); - else - ret = conf_parse_file(opts, len, path); - } - free(path); - } + ret = conf_parse_file_f(opts, len, "%s/.%s.conf", home, fname); + if (ret) + return ret; } -err_out: - return ret; + return 0; } diff --git a/src/conf.h b/src/conf.h index 3a65af6..8d194fe 100644 --- a/src/conf.h +++ b/src/conf.h @@ -169,6 +169,9 @@ void conf_free(struct conf_option *opts, size_t len); int conf_parse_argv(struct conf_option *opts, size_t len, int argc, char **argv); int conf_parse_file(struct conf_option *opts, size_t len, const char *path); -int conf_parse_all_files(struct conf_option *opts, size_t len); +int conf_parse_file_f(struct conf_option *opts, size_t len, + const char *format, ...); +int conf_parse_standard_files(struct conf_option *opts, size_t len, + const char *fname); #endif /* CONFIG_CONFIG_H */ diff --git a/src/kmscon_conf.c b/src/kmscon_conf.c index 453ef17..db7d817 100644 --- a/src/kmscon_conf.c +++ b/src/kmscon_conf.c @@ -344,7 +344,7 @@ int kmscon_load_config(int argc, char **argv) log_print_init("kmscon"); - ret = conf_parse_all_files(options, onum); + ret = conf_parse_standard_files(options, onum, "kmscon"); if (ret) return ret;