]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysctl: split out code for applying glob option
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 16 Aug 2022 19:10:30 +0000 (04:10 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Aug 2022 05:30:20 +0000 (14:30 +0900)
src/sysctl/sysctl.c

index ae079b7a716e3676a16d9d5c3e3ed1343eafcd7d..9bf2b610a5fb1651c38bbea09a710a1928378784 100644 (file)
@@ -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;