From 7177ac45723a2d716d34b66fb5d8691df5f2c6c8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 17 Aug 2022 04:10:30 +0900 Subject: sysctl: split out code for applying glob option --- src/sysctl/sysctl.c | 94 ++++++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 44 deletions(-) (limited to 'src/sysctl') diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index ae079b7a71..9bf2b610a5 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -109,63 +109,69 @@ 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; +static int apply_glob_option(OrderedHashmap *sysctl_options, Option *option) { + _cleanup_strv_free_ char **paths = NULL; + _cleanup_free_ char *pattern = NULL; + int r, k; - ORDERED_HASHMAP_FOREACH(option, sysctl_options) { - int k; + assert(sysctl_options); + assert(option); - /* Ignore "negative match" options, they are there only to exclude stuff from globs. */ - if (!option->value) - continue; + pattern = path_join("/proc/sys", option->key); + if (!pattern) + return log_oom(); - if (string_is_glob(option->key)) { - _cleanup_strv_free_ char **paths = NULL; - _cleanup_free_ char *pattern = NULL; + 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); + } - pattern = path_join("/proc/sys", option->key); - if (!pattern) - return log_oom(); + STRV_FOREACH(s, paths) { + const char *key; - 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; - } + assert_se(key = path_startswith(*s, "/proc/sys")); - } else if (strv_isempty(paths)) - log_debug("No match for glob: %s", option->key); + if (!test_prefix(key)) + continue; - STRV_FOREACH(s, paths) { - const char *key; + if (ordered_hashmap_contains(sysctl_options, key)) { + log_debug("Not setting %s (explicit setting exists).", key); + continue; + } - assert_se(key = path_startswith(*s, "/proc/sys")); + k = sysctl_write_or_warn(key, option->value, option->ignore_failure); + if (k < 0 && r >= 0) + r = k; + } - if (!test_prefix(key)) - continue; + return r; +} - if (ordered_hashmap_contains(sysctl_options, key)) { - log_debug("Not setting %s (explicit setting exists).", key); - continue; - } +static int apply_all(OrderedHashmap *sysctl_options) { + Option *option; + int r = 0; - k = sysctl_write_or_warn(key, option->value, option->ignore_failure); - if (r == 0) - r = k; - } + ORDERED_HASHMAP_FOREACH(option, sysctl_options) { + int k; - } else { + /* 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) - r = k; - } + if (k < 0 && r >= 0) + r = k; } return r; -- cgit v1.2.1