dcavalca / rpms / systemd

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