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