1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-24 21:34:08 +03:00

sysctl: split out code for applying glob option

This commit is contained in:
Yu Watanabe 2022-08-17 04:10:30 +09:00
parent c01404fdf1
commit 7177ac4572

View File

@ -109,39 +109,30 @@ static int sysctl_write_or_warn(const char *key, const char *value, bool ignore_
return 0;
}
static int apply_all(OrderedHashmap *sysctl_options) {
Option *option;
int r = 0;
ORDERED_HASHMAP_FOREACH(option, sysctl_options) {
int k;
/* Ignore "negative match" options, they are there only to exclude stuff from globs. */
if (!option->value)
continue;
if (string_is_glob(option->key)) {
static int apply_glob_option(OrderedHashmap *sysctl_options, Option *option) {
_cleanup_strv_free_ char **paths = NULL;
_cleanup_free_ char *pattern = NULL;
int r, k;
assert(sysctl_options);
assert(option);
pattern = path_join("/proc/sys", option->key);
if (!pattern)
return log_oom();
k = glob_extend(&paths, pattern, GLOB_NOCHECK);
if (k < 0) {
if (option->ignore_failure || ERRNO_IS_PRIVILEGE(k))
log_debug_errno(k, "Failed to resolve glob '%s', ignoring: %m",
option->key);
else {
log_error_errno(k, "Couldn't resolve glob '%s': %m",
option->key);
if (r == 0)
r = k;
}
} else if (strv_isempty(paths))
r = glob_extend(&paths, pattern, GLOB_NOCHECK);
if (r < 0) {
if (r == -ENOENT) {
log_debug("No match for glob: %s", option->key);
return 0;
}
if (option->ignore_failure || ERRNO_IS_PRIVILEGE(r)) {
log_debug_errno(r, "Failed to resolve glob '%s', ignoring: %m", option->key);
return 0;
} else
return log_error_errno(r, "Couldn't resolve glob '%s': %m", option->key);
}
STRV_FOREACH(s, paths) {
const char *key;
@ -157,16 +148,31 @@ static int apply_all(OrderedHashmap *sysctl_options) {
}
k = sysctl_write_or_warn(key, option->value, option->ignore_failure);
if (r == 0)
if (k < 0 && r >= 0)
r = k;
}
} else {
return r;
}
static int apply_all(OrderedHashmap *sysctl_options) {
Option *option;
int r = 0;
ORDERED_HASHMAP_FOREACH(option, sysctl_options) {
int k;
/* Ignore "negative match" options, they are there only to exclude stuff from globs. */
if (!option->value)
continue;
if (string_is_glob(option->key))
k = apply_glob_option(sysctl_options, option);
else
k = sysctl_write_or_warn(option->key, option->value, option->ignore_failure);
if (r == 0)
if (k < 0 && r >= 0)
r = k;
}
}
return r;
}