free(array);
}
-static int prepare_dirs(const char *root, char * const *dirs, int *ret_rfd, char **ret_root, char ***ret_dirs) {
+static int conf_files_log_level(ConfFilesFlags flags) {
+ return FLAGS_SET(flags, CONF_FILES_WARN) ? LOG_WARNING : LOG_DEBUG;
+}
+
+static int prepare_dirs(const char *root, ConfFilesFlags flags, char * const *dirs, int *ret_rfd, char **ret_root, char ***ret_dirs) {
_cleanup_free_ char *root_abs = NULL;
_cleanup_strv_free_ char **dirs_abs = NULL;
int r;
assert(ret_root);
assert(ret_dirs || strv_isempty(dirs));
+ int log_level = conf_files_log_level(flags);
+
r = empty_or_root_harder_to_null(&root);
if (r < 0)
- return log_debug_errno(r, "Failed to determine if '%s' points to the root directory: %m", strempty(root));
+ return log_full_errno(log_level, r, "Failed to determine if '%s' points to the root directory: %m", strempty(root));
if (ret_dirs) {
dirs_abs = strv_copy(dirs);
if (!dirs_abs)
- return log_oom();
+ return log_oom_full(log_level);
}
if (root) {
* necessary to modify each config directories here. but needs to normalize the root directory. */
r = path_make_absolute_cwd(root, &root_abs);
if (r < 0)
- return log_debug_errno(r, "Failed to make '%s' absolute: %m", root);
+ return log_full_errno(log_level, r, "Failed to make '%s' absolute: %m", root);
path_simplify(root_abs);
} else if (ret_dirs) {
* each config directory absolute if relative. */
r = path_strv_make_absolute_cwd(dirs_abs);
if (r < 0)
- return log_debug_errno(r, "Failed to make directories absolute: %m");
+ return log_full_errno(log_level, r, "Failed to make directories absolute: %m");
}
_cleanup_close_ int rfd = open(empty_to_root(root_abs), O_CLOEXEC|O_DIRECTORY|O_PATH);
if (rfd < 0)
- return log_debug_errno(errno, "Failed to open '%s': %m", empty_to_root(root_abs));
+ return log_full_errno(log_level, errno, "Failed to open '%s': %m", empty_to_root(root_abs));
*ret_rfd = TAKE_FD(rfd);
*ret_root = TAKE_PTR(root_abs);
return 0;
}
-static int conf_file_prefix_root(ConfFile *c, const char *root) {
+static int conf_file_prefix_root(ConfFile *c, const char *root, ConfFilesFlags flags) {
char *p;
int r;
assert(c);
+ int log_level = conf_files_log_level(flags);
+
r = chaseat_prefix_root(c->result, root, &p);
if (r < 0)
- return log_debug_errno(r, "Failed to prefix '%s' with root '%s': %m", c->result, root);
+ return log_full_errno(log_level, r, "Failed to prefix '%s' with root '%s': %m", c->result, root);
free_and_replace(c->result, p);
r = chaseat_prefix_root(c->resolved_path, root, &p);
if (r < 0)
- return log_debug_errno(r, "Failed to prefix '%s' with root '%s': %m", c->resolved_path, root);
+ return log_full_errno(log_level, r, "Failed to prefix '%s' with root '%s': %m", c->resolved_path, root);
free_and_replace(c->resolved_path, p);
/* Do not use chaseat_prefix_root(), as it is for the result of chaseat(), but the path is not chased. */
p = path_join(empty_to_root(root), skip_leading_slash(c->original_path));
if (!p)
- return log_oom_debug();
- path_simplify(p);
- free_and_replace(c->original_path, p);
+ return log_oom_full(log_level);
- return 0;
+ path_simplify(p);
+ return free_and_replace(c->original_path, p);
}
static bool conf_files_need_stat(ConfFilesFlags flags) {
assert(path);
assert(name);
+ int log_level = conf_files_log_level(flags);
+
root = empty_to_root(root);
r = chaseat(rfd, path, conf_files_chase_flags(flags), &resolved_path, &fd);
if (r < 0)
- return log_debug_errno(r, "Failed to chase '%s%s': %m",
- root, skip_leading_slash(original_path));
+ return log_full_errno(log_level, r, "Failed to chase '%s%s': %m",
+ root, skip_leading_slash(original_path));
if (r == 0) {
if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK)) {
/* If the path points to /dev/null in a image or so, then the device node may not exist. */
/* Mark this one as masked */
r = set_put_strdup(masked, name);
if (r < 0)
- return log_oom_debug();
+ return log_oom_full(log_level);
}
return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
if (conf_files_need_stat(flags))
/* If we need to have stat, skip the entry. */
- return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to chase '%s%s': %m",
- root, skip_leading_slash(original_path));
+ return log_full_errno(log_level, SYNTHETIC_ERRNO(ENOENT), "Failed to chase '%s%s': %m",
+ root, skip_leading_slash(original_path));
}
/* Even if we do not need stat, let's take stat now. The caller may use the info later. */
if (fd >= 0 && fstat(fd, &st) < 0)
- return log_debug_errno(errno, "Failed to stat '%s%s': %m",
- root, skip_leading_slash(original_path));
+ return log_full_errno(log_level, errno, "Failed to stat '%s%s': %m",
+ root, skip_leading_slash(original_path));
/* Is this a masking entry? */
if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK) && stat_may_be_dev_null(&st)) {
/* Mark this one as masked */
r = set_put_strdup(masked, name);
if (r < 0)
- return log_oom_debug();
+ return log_oom_full(log_level);
}
return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
/* Mark this one as masked */
r = set_put_strdup(masked, name);
if (r < 0)
- return log_oom_debug();
+ return log_oom_full(log_level);
}
return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
assert(rfd >= 0 || rfd == AT_FDCWD);
assert(ret);
+ int log_level = conf_files_log_level(flags);
+
_cleanup_free_ char *root = NULL;
- if (rfd >= 0 && DEBUG_LOGGING)
+ if (rfd >= 0)
(void) fd_get_path(rfd, &root);
_cleanup_(conf_file_freep) ConfFile *c = new(ConfFile, 1);
if (!c)
- return log_oom_debug();
+ return log_oom_full(log_level);
*c = (ConfFile) {
.original_path = strdup(path),
};
if (!c->original_path)
- return log_oom_debug();
+ return log_oom_full(log_level);
r = path_extract_filename(path, &c->name);
if (r < 0)
- return log_debug_errno(r, "Failed to extract filename from '%s': %m", path);
+ return log_full_errno(log_level, r, "Failed to extract filename from '%s': %m", path);
_cleanup_free_ char *dirpath = NULL, *resolved_dirpath = NULL;
r = path_extract_directory(path, &dirpath);
if (r < 0 && r != -EDESTADDRREQ)
- return log_debug_errno(r, "Failed to extract directory from '%s': %m", path);
+ return log_full_errno(log_level, r, "Failed to extract directory from '%s': %m", path);
if (r >= 0) {
r = chaseat(rfd, dirpath,
CHASE_MUST_BE_DIRECTORY | conf_files_chase_flags(flags),
&resolved_dirpath, /* ret_fd= */ NULL);
if (r < 0)
- return log_debug_errno(r, "Failed to chase '%s%s': %m", empty_to_root(root), skip_leading_slash(dirpath));
+ return log_full_errno(log_level, r, "Failed to chase '%s%s': %m", empty_to_root(root), skip_leading_slash(dirpath));
}
c->result = path_join(resolved_dirpath, c->name);
if (!c->result)
- return log_oom_debug();
+ return log_oom_full(log_level);
r = conf_file_chase_and_verify(
rfd,
_cleanup_free_ char *root_abs = NULL;
_cleanup_close_ int rfd = -EBADF;
- r = prepare_dirs(root, /* dirs= */ NULL, &rfd, &root_abs, /* ret_dirs= */ NULL);
+ r = prepare_dirs(root, flags, /* dirs= */ NULL, &rfd, &root_abs, /* ret_dirs= */ NULL);
if (r < 0)
return r;
if (!root_abs) {
r = path_make_absolute_cwd(path, &path_abs);
if (r < 0)
- return log_debug_errno(r, "Failed to make '%s' absolute: %m", path);
+ return log_full_errno(conf_files_log_level(flags), r, "Failed to make '%s' absolute: %m", path);
path = path_abs;
}
if (r < 0)
return r;
- r = conf_file_prefix_root(c, root_abs);
+ r = conf_file_prefix_root(c, root_abs, flags);
if (r < 0)
return r;
assert(files);
assert(masked);
+ int log_level = conf_files_log_level(flags);
+
root = empty_to_root(root);
- FOREACH_DIRENT(de, dir, return log_debug_errno(errno, "Failed to read directory '%s%s': %m",
- root, skip_leading_slash(original_dirpath))) {
+ FOREACH_DIRENT(de, dir, return log_full_errno(log_level, errno, "Failed to read directory '%s%s': %m",
+ root, skip_leading_slash(original_dirpath))) {
_cleanup_free_ char *original_path = path_join(original_dirpath, de->d_name);
if (!original_path)
- return log_oom_debug();
+ return log_oom_full(log_level);
/* Does this match the suffix? */
if (suffix && !endswith(de->d_name, suffix)) {
_cleanup_free_ char *p = path_join(resolved_dirpath, de->d_name);
if (!p)
- return log_oom_debug();
+ return log_oom_full(log_level);
_cleanup_free_ char *resolved_path = NULL;
_cleanup_close_ int fd = -EBADF;
_cleanup_(conf_file_freep) ConfFile *c = new(ConfFile, 1);
if (!c)
- return log_oom_debug();
+ return log_oom_full(log_level);
*c = (ConfFile) {
.name = strdup(de->d_name),
};
if (!c->name)
- return log_oom_debug();
+ return log_oom_full(log_level);
r = hashmap_ensure_put(files, &conf_file_hash_ops, c->name, c);
if (r < 0) {
assert(r == -ENOMEM);
- return log_oom_debug();
+ return log_oom_full(log_level);
}
assert(r > 0);
return 0;
}
-static int dump_files(Hashmap *fh, const char *root, ConfFile ***ret_files, size_t *ret_n_files) {
+static int dump_files(Hashmap *fh, const char *root, ConfFilesFlags flags, ConfFile ***ret_files, size_t *ret_n_files) {
ConfFile **files = NULL;
size_t n_files = 0;
int r;
/* The entries in the array given by hashmap_dump_sorted() are still owned by the hashmap. */
r = hashmap_dump_sorted(fh, (void***) &files, &n_files);
if (r < 0)
- return log_oom_debug();
+ return log_oom_full(conf_files_log_level(flags));
/* Hence, we need to remove them from the hashmap. */
FOREACH_ARRAY(i, files, n_files)
if (root)
FOREACH_ARRAY(i, files, n_files) {
- r = conf_file_prefix_root(*i, root);
+ r = conf_file_prefix_root(*i, root, flags);
if (r < 0)
return r;
}
assert(ret);
+ int log_level = conf_files_log_level(flags);
+
/* The entries in the array given by hashmap_dump_sorted() are still owned by the hashmap.
* Hence, do not use conf_file_free_many() for 'entries' */
r = hashmap_dump_sorted(fh, (void***) &files, &n_files);
if (r < 0)
- return log_oom_debug();
+ return log_oom_full(log_level);
FOREACH_ARRAY(i, files, n_files) {
ConfFile *c = *i;
r = chaseat_prefix_root(c->result, root, &p);
if (r < 0)
- return log_debug_errno(r, "Failed to prefix '%s' with root '%s': %m", c->result, root);
+ return log_full_errno(log_level, r, "Failed to prefix '%s' with root '%s': %m", c->result, root);
if (FLAGS_SET(flags, CONF_FILES_TRUNCATE_SUFFIX) && suffix) {
char *e = endswith(p, suffix);
}
if (strv_consume_with_size(&results, &n_results, TAKE_PTR(p)) < 0)
- return log_oom_debug();
+ return log_oom_full(log_level);
continue;
} else
_cleanup_free_ char *n = strndup(add, e - add);
if (!n)
- return log_oom_debug();
+ return log_oom_full(log_level);
r = strv_consume_with_size(&results, &n_results, TAKE_PTR(n));
} else
r = strv_extend_with_size(&results, &n_results, add);
if (r < 0)
- return log_oom_debug();
+ return log_oom_full(log_level);
}
*ret = TAKE_PTR(results);
return 0;
}
-static int insert_replacement(Hashmap **fh, ConfFile *replacement, const ConfFile **ret) {
+static int insert_replacement(Hashmap **fh, ConfFile *replacement, ConfFilesFlags flags, const ConfFile **ret) {
_cleanup_(conf_file_freep) ConfFile *c = ASSERT_PTR(replacement);
int r;
r = hashmap_ensure_put(fh, &conf_file_hash_ops, c->name, c);
if (r < 0) {
assert(r == -ENOMEM);
- return log_oom_debug();
+ return log_oom_full(conf_files_log_level(flags));
}
assert(r > 0);
root = empty_to_root(root);
if (replacement) {
- r = conf_file_new_at(replacement, rfd, /* flags= */ 0, &c);
+ r = conf_file_new_at(replacement, rfd, flags & CONF_FILES_WARN, &c);
if (r < 0)
return r;
}
r = chase_and_opendirat(rfd, *p, CHASE_AT_RESOLVE_IN_ROOT, &path, &dir);
if (r < 0) {
if (r != -ENOENT)
- log_debug_errno(r, "Failed to chase and open directory '%s%s', ignoring: %m",
- root, skip_leading_slash(*p));
+ log_full_errno(conf_files_log_level(flags), r,
+ "Failed to chase and open directory '%s%s', ignoring: %m",
+ root, skip_leading_slash(*p));
continue;
}
if (c && streq_ptr(path_startswith(c->result, path), c->name)) {
- r = insert_replacement(&fh, TAKE_PTR(c), &inserted);
+ r = insert_replacement(&fh, TAKE_PTR(c), flags, &inserted);
if (r < 0)
return r;
}
}
if (c) {
- r = insert_replacement(&fh, TAKE_PTR(c), &inserted);
+ r = insert_replacement(&fh, TAKE_PTR(c), flags, &inserted);
if (r < 0)
return r;
}
assert(ret);
- r = prepare_dirs(root, (char**) dirs, &rfd, &root_abs, &dirs_abs);
+ r = prepare_dirs(root, flags, (char**) dirs, &rfd, &root_abs, &dirs_abs);
if (r < 0)
return r;
assert(ret_files);
assert(ret_n_files);
- r = prepare_dirs(root, (char**) dirs, &rfd, &root_abs, &dirs_abs);
+ r = prepare_dirs(root, flags, (char**) dirs, &rfd, &root_abs, &dirs_abs);
if (r < 0)
return r;
if (r < 0)
return r;
- return dump_files(fh, empty_to_root(root_abs), ret_files, ret_n_files);
+ return dump_files(fh, empty_to_root(root_abs), flags, ret_files, ret_n_files);
}
int conf_files_list_strv_at(
assert(rfd >= 0 || rfd == AT_FDCWD);
assert(ret);
- if (rfd >= 0 && DEBUG_LOGGING)
+ if (rfd >= 0)
(void) fd_get_path(rfd, &root); /* for logging */
r = conf_files_list_impl(suffix, rfd, root, flags, dirs, /* replacement= */ NULL, &fh, /* ret_inserted= */ NULL);
assert(ret_files);
assert(ret_n_files);
- if (rfd >= 0 && DEBUG_LOGGING)
+ if (rfd >= 0)
(void) fd_get_path(rfd, &root); /* for logging */
r = conf_files_list_impl(suffix, rfd, root, flags, dirs, /* replacement= */ NULL, &fh, /* ret_inserted= */ NULL);
if (r < 0)
return r;
- return dump_files(fh, /* root= */ NULL, ret_files, ret_n_files);
+ return dump_files(fh, /* root= */ NULL, flags, ret_files, ret_n_files);
}
int conf_files_list(char ***ret, const char *suffix, const char *root, ConfFilesFlags flags, const char *dir) {
d = strv_split_nulstr(dirs);
if (!d)
- return -ENOMEM;
+ return log_oom_full(conf_files_log_level(flags));
return conf_files_list_strv(ret, suffix, root, flags, (const char**) d);
}
d = strv_split_nulstr(dirs);
if (!d)
- return -ENOMEM;
+ return log_oom_full(conf_files_log_level(flags));
return conf_files_list_strv_full(suffix, root, flags, (const char**) d, ret_files, ret_n_files);
}
d = strv_split_nulstr(dirs);
if (!d)
- return -ENOMEM;
+ return log_oom_full(conf_files_log_level(flags));
return conf_files_list_strv_at(ret, suffix, rfd, flags, (const char**) d);
}
d = strv_split_nulstr(dirs);
if (!d)
- return -ENOMEM;
+ return log_oom_full(conf_files_log_level(flags));
return conf_files_list_strv_at_full(suffix, rfd, flags, (const char**) d, ret_files, ret_n_files);
}
_cleanup_hashmap_free_ Hashmap *fh = NULL;
_cleanup_free_ char *inserted = NULL;
- ConfFilesFlags flags = CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED_BY_SYMLINK;
+ ConfFilesFlags flags = CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED_BY_SYMLINK | CONF_FILES_WARN;
_cleanup_close_ int rfd = -EBADF;
_cleanup_free_ char *root_abs = NULL;
_cleanup_strv_free_ char **dirs_abs = NULL;
assert(ret_files);
- r = prepare_dirs(root, config_dirs, &rfd, &root_abs, &dirs_abs);
+ r = prepare_dirs(root, flags, config_dirs, &rfd, &root_abs, &dirs_abs);
if (r < 0)
return r;
if (c) {
r = chaseat_prefix_root(c->result, root_abs, &inserted);
if (r < 0)
- return log_debug_errno(r, "Failed to prefix '%s' with root '%s': %m", c->result, empty_to_root(root_abs));
+ return log_full_errno(conf_files_log_level(flags), r,
+ "Failed to prefix '%s' with root '%s': %m",
+ c->result, empty_to_root(root_abs));
}
r = copy_and_sort_files_from_hashmap(fh, ".conf", empty_to_root(root_abs), flags, ret_files);
char ***ret,
const char *dropin_dirname,
const char *root,
+ ConfFilesFlags flags,
const char * const *dirs) {
_cleanup_strv_free_ char **dropin_dirs = NULL;
suffix = strjoina("/", dropin_dirname);
r = strv_extend_strv_concat(&dropin_dirs, dirs, suffix);
if (r < 0)
- return r;
+ return log_oom_full(conf_files_log_level(flags));
- return conf_files_list_strv(ret, ".conf", root, 0, (const char* const*) dropin_dirs);
+ return conf_files_list_strv(ret, ".conf", root, flags, (const char* const*) dropin_dirs);
}
/**