summaryrefslogtreecommitdiff
path: root/src/sysctl
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-08-17 04:10:30 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2022-08-17 14:30:20 +0900
commit7177ac45723a2d716d34b66fb5d8691df5f2c6c8 (patch)
tree8f2a130ba539c8176482087ab6aebff699105259 /src/sysctl
parentc01404fdf100b03dafa8a366d07f74f3c30d5330 (diff)
downloadsystemd-7177ac45723a2d716d34b66fb5d8691df5f2c6c8.tar.gz
sysctl: split out code for applying glob option
Diffstat (limited to 'src/sysctl')
-rw-r--r--src/sysctl/sysctl.c94
1 files changed, 50 insertions, 44 deletions
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;