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