dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
64ccc2
From 9e1e02a83538a865dae65454214363d306e69854 Mon Sep 17 00:00:00 2001
64ccc2
From: Topi Miettinen <toiwoton@gmail.com>
64ccc2
Date: Tue, 17 Dec 2019 15:47:37 +0200
64ccc2
Subject: [PATCH] shared/dropin: fix assert for invalid drop-in
64ccc2
64ccc2
Don't try to show top level drop-in for non-existent units or when trying to
64ccc2
instantiate non-instantiated units:
64ccc2
64ccc2
$ systemctl cat nonexistent@.service
64ccc2
Assertion 'name' failed at src/shared/dropin.c:143, function unit_file_find_dirs(). Aborting.
64ccc2
$ systemctl cat systemd-journald@.service
64ccc2
Assertion 'name' failed at src/shared/dropin.c:143, function unit_file_find_dirs(). Aborting.
64ccc2
64ccc2
(cherry picked from commit 7a670b1dd981c645064f69faf85b04620aadbafb)
64ccc2
64ccc2
Resolves: #2051520
64ccc2
---
64ccc2
 src/shared/dropin.c                | 23 ++++++++++++-----------
64ccc2
 test/TEST-15-DROPIN/test-dropin.sh | 14 ++++++++++++++
64ccc2
 2 files changed, 26 insertions(+), 11 deletions(-)
64ccc2
64ccc2
diff --git a/src/shared/dropin.c b/src/shared/dropin.c
64ccc2
index bd2a3c0feb..11ed4c7184 100644
64ccc2
--- a/src/shared/dropin.c
64ccc2
+++ b/src/shared/dropin.c
64ccc2
@@ -236,7 +236,6 @@ int unit_file_find_dropin_paths(
64ccc2
                 char ***ret) {
64ccc2
 
64ccc2
         _cleanup_strv_free_ char **dirs = NULL;
64ccc2
-        UnitType type = _UNIT_TYPE_INVALID;
64ccc2
         char *name, **p;
64ccc2
         Iterator i;
64ccc2
         int r;
64ccc2
@@ -246,22 +245,24 @@ int unit_file_find_dropin_paths(
64ccc2
         /* All the names in the unit are of the same type so just grab one. */
64ccc2
         name = (char*) set_first(names);
64ccc2
         if (name) {
64ccc2
+                UnitType type = _UNIT_TYPE_INVALID;
64ccc2
+
64ccc2
                 type = unit_name_to_type(name);
64ccc2
                 if (type < 0)
64ccc2
                         return log_error_errno(EINVAL,
64ccc2
                                                "Failed to to derive unit type from unit name: %s",
64ccc2
                                                name);
64ccc2
-        }
64ccc2
 
64ccc2
-        /* Special top level drop in for "<unit type>.<suffix>". Add this first as it's the most generic
64ccc2
-         * and should be able to be overridden by more specific drop-ins. */
64ccc2
-        STRV_FOREACH(p, lookup_path)
64ccc2
-                (void) unit_file_find_dirs(original_root,
64ccc2
-                                           unit_path_cache,
64ccc2
-                                           *p,
64ccc2
-                                           unit_type_to_string(type),
64ccc2
-                                           dir_suffix,
64ccc2
-                                           &dirs);
64ccc2
+                /* Special top level drop in for "<unit type>.<suffix>". Add this first as it's the most generic
64ccc2
+                 * and should be able to be overridden by more specific drop-ins. */
64ccc2
+                STRV_FOREACH(p, lookup_path)
64ccc2
+                        (void) unit_file_find_dirs(original_root,
64ccc2
+                                                   unit_path_cache,
64ccc2
+                                                   *p,
64ccc2
+                                                   unit_type_to_string(type),
64ccc2
+                                                   dir_suffix,
64ccc2
+                                                   &dirs);
64ccc2
+        }
64ccc2
 
64ccc2
         SET_FOREACH(name, names, i)
64ccc2
                 STRV_FOREACH(p, lookup_path)
64ccc2
diff --git a/test/TEST-15-DROPIN/test-dropin.sh b/test/TEST-15-DROPIN/test-dropin.sh
64ccc2
index 7836c6535d..5419169f7b 100755
64ccc2
--- a/test/TEST-15-DROPIN/test-dropin.sh
64ccc2
+++ b/test/TEST-15-DROPIN/test-dropin.sh
64ccc2
@@ -289,9 +289,23 @@ EOF
64ccc2
         clear_services a b
64ccc2
 }
64ccc2
 
64ccc2
+test_invalid_dropins () {
64ccc2
+    echo "Testing invalid dropins..."
64ccc2
+    # Assertion failed on earlier versions, command exits unsuccessfully on later versions
64ccc2
+    systemctl cat nonexistent@.service || true
64ccc2
+    create_services a
64ccc2
+    systemctl daemon-reload
64ccc2
+    # Assertion failed on earlier versions, command exits unsuccessfully on later versions
64ccc2
+    systemctl cat a@.service || true
64ccc2
+    systemctl stop a
64ccc2
+    clear_services a
64ccc2
+    return 0
64ccc2
+}
64ccc2
+
64ccc2
 test_basic_dropins
64ccc2
 test_template_dropins
64ccc2
 test_alias_dropins
64ccc2
 test_masked_dropins
64ccc2
+test_invalid_dropins
64ccc2
 
64ccc2
 touch /testok