ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
da2bf9
From 516fc73142e803a7a6cbd126c338e1c3c73d6843 Mon Sep 17 00:00:00 2001
da2bf9
From: Jan Macku <jamacku@redhat.com>
da2bf9
Date: Fri, 21 Jan 2022 12:10:45 +0100
da2bf9
Subject: [PATCH] sysctl: if options are prefixed with "-" ignore write errors
da2bf9
da2bf9
(cherry picked from commit dec02d6e1993d420a0a94c7fec294605df55e88e)
da2bf9
da2bf9
Resolves: #2037807
da2bf9
---
da2bf9
 src/sysctl/sysctl.c | 115 ++++++++++++++++++++++++++++++--------------
da2bf9
 1 file changed, 80 insertions(+), 35 deletions(-)
da2bf9
da2bf9
diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c
da2bf9
index 0151f7dabe..7b0528877c 100644
da2bf9
--- a/src/sysctl/sysctl.c
da2bf9
+++ b/src/sysctl/sysctl.c
da2bf9
@@ -26,25 +26,71 @@ static char **arg_prefixes = NULL;
da2bf9
 static bool arg_cat_config = false;
da2bf9
 static bool arg_no_pager = false;
da2bf9
 
da2bf9
+typedef struct Option {
da2bf9
+        char *key;
da2bf9
+        char *value;
da2bf9
+        bool ignore_failure;
da2bf9
+} Option;
da2bf9
+
da2bf9
+static Option *option_free(Option *o) {
da2bf9
+        if (!o)
da2bf9
+                return NULL;
da2bf9
+
da2bf9
+        free(o->key);
da2bf9
+        free(o->value);
da2bf9
+
da2bf9
+        return mfree(o);
da2bf9
+}
da2bf9
+
da2bf9
+DEFINE_TRIVIAL_CLEANUP_FUNC(Option*, option_free);
da2bf9
+DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(option_hash_ops, char, string_hash_func, string_compare_func, Option, option_free);
da2bf9
+
da2bf9
+static Option *option_new(
da2bf9
+                const char *key,
da2bf9
+                const char *value,
da2bf9
+                bool ignore_failure) {
da2bf9
+
da2bf9
+        _cleanup_(option_freep) Option *o = NULL;
da2bf9
+
da2bf9
+        assert(key);
da2bf9
+        assert(value);
da2bf9
+
da2bf9
+        o = new(Option, 1);
da2bf9
+        if (!o)
da2bf9
+                return NULL;
da2bf9
+
da2bf9
+        *o = (Option) {
da2bf9
+                .key = strdup(key),
da2bf9
+                .value = strdup(value),
da2bf9
+                .ignore_failure = ignore_failure,
da2bf9
+        };
da2bf9
+
da2bf9
+        if (!o->key || !o->value)
da2bf9
+                return NULL;
da2bf9
+
da2bf9
+        return TAKE_PTR(o);
da2bf9
+}
da2bf9
+
da2bf9
 static int apply_all(OrderedHashmap *sysctl_options) {
da2bf9
-        char *property, *value;
da2bf9
+        Option *option;
da2bf9
         Iterator i;
da2bf9
         int r = 0;
da2bf9
 
da2bf9
-        ORDERED_HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) {
da2bf9
+        ORDERED_HASHMAP_FOREACH(option, sysctl_options, i) {
da2bf9
                 int k;
da2bf9
 
da2bf9
-                k = sysctl_write(property, value);
da2bf9
+                k = sysctl_write(option->key, option->value);
da2bf9
                 if (k < 0) {
da2bf9
-                        /* If the sysctl is not available in the kernel or we are running with reduced privileges and
da2bf9
-                         * cannot write it, then log about the issue at LOG_NOTICE level, and proceed without
da2bf9
-                         * failing. (EROFS is treated as a permission problem here, since that's how container managers
da2bf9
-                         * usually protected their sysctls.) In all other cases log an error and make the tool fail. */
da2bf9
-
da2bf9
-                        if (IN_SET(k, -EPERM, -EACCES, -EROFS, -ENOENT))
da2bf9
-                                log_notice_errno(k, "Couldn't write '%s' to '%s', ignoring: %m", value, property);
da2bf9
+                        /* If the sysctl is not available in the kernel or we are running with reduced
da2bf9
+                         * privileges and cannot write it, then log about the issue at LOG_NOTICE level, and
da2bf9
+                         * proceed without failing. (EROFS is treated as a permission problem here, since
da2bf9
+                         * that's how container managers usually protected their sysctls.) In all other cases
da2bf9
+                         * log an error and make the tool fail. */
da2bf9
+
da2bf9
+                        if (IN_SET(k, -EPERM, -EACCES, -EROFS, -ENOENT) || option->ignore_failure)
da2bf9
+                                log_notice_errno(k, "Couldn't write '%s' to '%s', ignoring: %m", option->value, option->key);
da2bf9
                         else {
da2bf9
-                                log_error_errno(k, "Couldn't write '%s' to '%s': %m", value, property);
da2bf9
+                                log_error_errno(k, "Couldn't write '%s' to '%s': %m", option->value, option->key);
da2bf9
                                 if (r == 0)
da2bf9
                                         r = k;
da2bf9
                         }
da2bf9
@@ -90,9 +136,11 @@ static int parse_file(OrderedHashmap *sysctl_options, const char *path, bool ign
da2bf9
 
da2bf9
         log_debug("Parsing %s", path);
da2bf9
         for (;;) {
da2bf9
-                char *p, *value, *new_value, *property, *existing;
da2bf9
+                _cleanup_(option_freep) Option *new_option = NULL;
da2bf9
                 _cleanup_free_ char *l = NULL;
da2bf9
-                void *v;
da2bf9
+                bool ignore_failure;
da2bf9
+                Option *existing;
da2bf9
+                char *p, *value;
da2bf9
                 int k;
da2bf9
 
da2bf9
                 k = read_line(f, LONG_LINE_MAX, &l);
da2bf9
@@ -122,39 +170,37 @@ static int parse_file(OrderedHashmap *sysctl_options, const char *path, bool ign
da2bf9
                 *value = 0;
da2bf9
                 value++;
da2bf9
 
da2bf9
-                p = sysctl_normalize(strstrip(p));
da2bf9
+                p = strstrip(p);
da2bf9
+                ignore_failure = p[0] == '-';
da2bf9
+                if (ignore_failure)
da2bf9
+                        p++;
da2bf9
+
da2bf9
+                p = sysctl_normalize(p);
da2bf9
                 value = strstrip(value);
da2bf9
 
da2bf9
                 if (!test_prefix(p))
da2bf9
                         continue;
da2bf9
 
da2bf9
-                existing = ordered_hashmap_get2(sysctl_options, p, &v);
da2bf9
+                existing = ordered_hashmap_get(sysctl_options, p);
da2bf9
                 if (existing) {
da2bf9
-                        if (streq(value, existing))
da2bf9
+                        if (streq(value, existing->value)) {
da2bf9
+                                existing->ignore_failure = existing->ignore_failure || ignore_failure;
da2bf9
                                 continue;
da2bf9
+                        }
da2bf9
 
da2bf9
                         log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, path, c);
da2bf9
-                        free(ordered_hashmap_remove(sysctl_options, p));
da2bf9
-                        free(v);
da2bf9
+                        option_free(ordered_hashmap_remove(sysctl_options, p));
da2bf9
                 }
da2bf9
 
da2bf9
-                property = strdup(p);
da2bf9
-                if (!property)
da2bf9
+                new_option = option_new(p, value, ignore_failure);
da2bf9
+                if (!new_option)
da2bf9
                         return log_oom();
da2bf9
 
da2bf9
-                new_value = strdup(value);
da2bf9
-                if (!new_value) {
da2bf9
-                        free(property);
da2bf9
-                        return log_oom();
da2bf9
-                }
da2bf9
+                k = ordered_hashmap_put(sysctl_options, new_option->key, new_option);
da2bf9
+                if (k < 0)
da2bf9
+                        return log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", p);
da2bf9
 
da2bf9
-                k = ordered_hashmap_put(sysctl_options, property, new_value);
da2bf9
-                if (k < 0) {
da2bf9
-                        log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", property);
da2bf9
-                        free(property);
da2bf9
-                        free(new_value);
da2bf9
-                        return k;
da2bf9
-                }
da2bf9
+                TAKE_PTR(new_option);
da2bf9
         }
da2bf9
 
da2bf9
         return r;
da2bf9
@@ -251,7 +297,7 @@ static int parse_argv(int argc, char *argv[]) {
da2bf9
 }
da2bf9
 
da2bf9
 int main(int argc, char *argv[]) {
da2bf9
-        OrderedHashmap *sysctl_options = NULL;
da2bf9
+        _cleanup_(ordered_hashmap_freep) OrderedHashmap *sysctl_options = NULL;
da2bf9
         int r = 0, k;
da2bf9
 
da2bf9
         r = parse_argv(argc, argv);
da2bf9
@@ -264,7 +310,7 @@ int main(int argc, char *argv[]) {
da2bf9
 
da2bf9
         umask(0022);
da2bf9
 
da2bf9
-        sysctl_options = ordered_hashmap_new(&path_hash_ops);
da2bf9
+        sysctl_options = ordered_hashmap_new(&option_hash_ops);
da2bf9
         if (!sysctl_options) {
da2bf9
                 r = log_oom();
da2bf9
                 goto finish;
da2bf9
@@ -311,7 +357,6 @@ int main(int argc, char *argv[]) {
da2bf9
 finish:
da2bf9
         pager_close();
da2bf9
 
da2bf9
-        ordered_hashmap_free_free_free(sysctl_options);
da2bf9
         strv_free(arg_prefixes);
da2bf9
 
da2bf9
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;