b7dd4d
From adc0a99b18153535ef73cf1b6ce2bc64ca501c81 Mon Sep 17 00:00:00 2001
b7dd4d
From: Anita Zhang <the.anitazha@gmail.com>
b7dd4d
Date: Fri, 4 Oct 2019 17:39:34 -0700
b7dd4d
Subject: [PATCH] shared/dropin: support -.service.d/ top level drop-in for
b7dd4d
 service units
b7dd4d
b7dd4d
(cherry picked from commit 272467882c9c3c3d4faca5fd7a1f44c5ef2f064)
b7dd4d
b7dd4d
Resolves: #2051520
b7dd4d
---
b7dd4d
 man/systemd.service.xml            | 13 +++++++++++++
b7dd4d
 man/systemd.special.xml            |  9 +++++++++
b7dd4d
 man/systemd.unit.xml               |  4 ++++
b7dd4d
 src/basic/unit-name.c              |  9 +++++++--
b7dd4d
 src/core/service.c                 |  2 +-
b7dd4d
 src/shared/dropin.c                | 29 ++++++++++++++++++++++++++---
b7dd4d
 test/TEST-15-DROPIN/test-dropin.sh | 15 ++++++++++++++-
b7dd4d
 7 files changed, 74 insertions(+), 7 deletions(-)
b7dd4d
b7dd4d
diff --git a/man/systemd.service.xml b/man/systemd.service.xml
b7dd4d
index 1e30a564df..4164402d0e 100644
b7dd4d
--- a/man/systemd.service.xml
b7dd4d
+++ b/man/systemd.service.xml
b7dd4d
@@ -62,6 +62,19 @@
b7dd4d
     about the incompatibilities, see the 
b7dd4d
     url="https://www.freedesktop.org/wiki/Software/systemd/Incompatibilities">Incompatibilities
b7dd4d
     with SysV</ulink> document.</para>
b7dd4d
+
b7dd4d
+    <para>In addition to the various drop-in behaviors described in
b7dd4d
+    <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
b7dd4d
+    services also support a top-level drop-in with <filename>-.service.d/</filename> that allows
b7dd4d
+    altering or adding to the settings of all services on the system.
b7dd4d
+    The formatting and precedence of applying drop-in configurations follow what is defined in
b7dd4d
+    <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
b7dd4d
+    However, configurations in <filename>-.service.d/</filename> have the lowest precedence compared to settings
b7dd4d
+    in the service specific override directories. For example, for <filename>foo-bar-baz.service</filename>,
b7dd4d
+    drop-ins in <filename>foo-bar-baz.service.d/</filename> override the ones in
b7dd4d
+    <filename>foo-bar-.service.d/</filename>, which override the ones <filename>foo-.service.d/</filename>,
b7dd4d
+    which override the ones in <filename>-.service.d/</filename>.
b7dd4d
+    </para>
b7dd4d
   </refsect1>
b7dd4d
 
b7dd4d
   <refsect1>
b7dd4d
diff --git a/man/systemd.special.xml b/man/systemd.special.xml
b7dd4d
index fe6324a4a0..06798cd9e2 100644
b7dd4d
--- a/man/systemd.special.xml
b7dd4d
+++ b/man/systemd.special.xml
b7dd4d
@@ -117,6 +117,15 @@
b7dd4d
         </listitem>
b7dd4d
       </varlistentry>
b7dd4d
 
b7dd4d
+      <varlistentry>
b7dd4d
+        <term><filename>-.service</filename></term>
b7dd4d
+        <listitem>
b7dd4d
+          <para>This is a reserved unit name used to support top-level drop-ins for services. See
b7dd4d
+          <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
b7dd4d
+          for details.</para>
b7dd4d
+        </listitem>
b7dd4d
+      </varlistentry>
b7dd4d
+
b7dd4d
       <varlistentry>
b7dd4d
         <term><filename>basic.target</filename></term>
b7dd4d
         <listitem>
b7dd4d
diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
b7dd4d
index e80c760dd6..5aa3bd1699 100644
b7dd4d
--- a/man/systemd.unit.xml
b7dd4d
+++ b/man/systemd.unit.xml
b7dd4d
@@ -190,6 +190,10 @@
b7dd4d
     over unit files wherever located. Multiple drop-in files with different names are applied in
b7dd4d
     lexicographic order, regardless of which of the directories they reside in.</para>
b7dd4d
 
b7dd4d
+    <para>Service units also support a top-level drop-in directory for modifying the settings of all service units. See
b7dd4d
+    <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
b7dd4d
+    for details.</para>
b7dd4d
+
b7dd4d
     
b7dd4d
          people to use .d/ drop-ins instead. -->
b7dd4d
 
b7dd4d
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
b7dd4d
index 82a666a481..078628d6e8 100644
b7dd4d
--- a/src/basic/unit-name.c
b7dd4d
+++ b/src/basic/unit-name.c
b7dd4d
@@ -681,8 +681,13 @@ bool service_unit_name_is_valid(const char *name) {
b7dd4d
 
b7dd4d
         /* If it's a template or instance, get the prefix as a service name. */
b7dd4d
         if (unit_name_is_valid(name, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) {
b7dd4d
-                assert_se(unit_name_to_prefix(name, &prefix) == 0);
b7dd4d
-                assert_se(s = strjoin(prefix, ".service"));
b7dd4d
+                if (unit_name_to_prefix(name, &prefix) < 0)
b7dd4d
+                        return false;
b7dd4d
+
b7dd4d
+                s = strjoin(prefix, ".service");
b7dd4d
+                if (!s)
b7dd4d
+                        return false;
b7dd4d
+
b7dd4d
                 service_name = s;
b7dd4d
         }
b7dd4d
 
b7dd4d
diff --git a/src/core/service.c b/src/core/service.c
b7dd4d
index b7eb10c044..b3ef79228f 100644
b7dd4d
--- a/src/core/service.c
b7dd4d
+++ b/src/core/service.c
b7dd4d
@@ -558,7 +558,7 @@ static int service_verify(Service *s) {
b7dd4d
 
b7dd4d
         if (!service_unit_name_is_valid(UNIT(s)->id)) {
b7dd4d
                 log_unit_error(UNIT(s), "Service name is invalid or reserved. Refusing.");
b7dd4d
-                return -ENOEXEC;
b7dd4d
+                return -EINVAL;
b7dd4d
         }
b7dd4d
 
b7dd4d
         if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]
b7dd4d
diff --git a/src/shared/dropin.c b/src/shared/dropin.c
b7dd4d
index 357c66d800..78ca7f4452 100644
b7dd4d
--- a/src/shared/dropin.c
b7dd4d
+++ b/src/shared/dropin.c
b7dd4d
@@ -19,6 +19,7 @@
b7dd4d
 #include "mkdir.h"
b7dd4d
 #include "path-util.h"
b7dd4d
 #include "set.h"
b7dd4d
+#include "special.h"
b7dd4d
 #include "string-util.h"
b7dd4d
 #include "strv.h"
b7dd4d
 #include "unit-name.h"
b7dd4d
@@ -232,15 +233,37 @@ int unit_file_find_dropin_paths(
b7dd4d
                 char ***ret) {
b7dd4d
 
b7dd4d
         _cleanup_strv_free_ char **dirs = NULL;
b7dd4d
-        char *t, **p;
b7dd4d
+        UnitType type = _UNIT_TYPE_INVALID;
b7dd4d
+        char *name, **p;
b7dd4d
         Iterator i;
b7dd4d
         int r;
b7dd4d
 
b7dd4d
         assert(ret);
b7dd4d
 
b7dd4d
-        SET_FOREACH(t, names, i)
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
+                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 drop in for -.service. Add this first as it's the most generic
b7dd4d
+         * and should be able to be overridden by more specific drop-ins. */
b7dd4d
+        if (type == UNIT_SERVICE)
b7dd4d
+                STRV_FOREACH(p, lookup_path)
b7dd4d
+                        (void) unit_file_find_dirs(original_root,
b7dd4d
+                                                   unit_path_cache,
b7dd4d
+                                                   *p,
b7dd4d
+                                                   SPECIAL_ROOT_SERVICE,
b7dd4d
+                                                   dir_suffix,
b7dd4d
+                                                   &dirs);
b7dd4d
+
b7dd4d
+        SET_FOREACH(name, names, i)
b7dd4d
                 STRV_FOREACH(p, lookup_path)
b7dd4d
-                        (void) unit_file_find_dirs(original_root, unit_path_cache, *p, t, dir_suffix, &dirs);
b7dd4d
+                        (void) unit_file_find_dirs(original_root, unit_path_cache, *p, name, dir_suffix, &dirs);
b7dd4d
 
b7dd4d
         if (strv_isempty(dirs)) {
b7dd4d
                 *ret = NULL;
b7dd4d
diff --git a/test/TEST-15-DROPIN/test-dropin.sh b/test/TEST-15-DROPIN/test-dropin.sh
b7dd4d
index ab0a58caea..def2e03304 100755
b7dd4d
--- a/test/TEST-15-DROPIN/test-dropin.sh
b7dd4d
+++ b/test/TEST-15-DROPIN/test-dropin.sh
b7dd4d
@@ -102,7 +102,20 @@ test_basic_dropins () {
b7dd4d
         check_ok b Wants c.service
b7dd4d
         systemctl stop a c
b7dd4d
 
b7dd4d
-        clear_services a b c
b7dd4d
+    echo "*** test -.service.d/ top level drop-in"
b7dd4d
+    create_services a b
b7dd4d
+    check_ko a ExecCondition "/bin/echo a"
b7dd4d
+    check_ko b ExecCondition "/bin/echo b"
b7dd4d
+    mkdir -p /usr/lib/systemd/system/-.service.d
b7dd4d
+    cat >/usr/lib/systemd/system/-.service.d/override.conf <
b7dd4d
+[Service]
b7dd4d
+ExecCondition=/bin/echo %n
b7dd4d
+EOF
b7dd4d
+    check_ok a ExecCondition "/bin/echo a"
b7dd4d
+    check_ok b ExecCondition "/bin/echo b"
b7dd4d
+    rm -rf /usr/lib/systemd/system/-.service.d
b7dd4d
+
b7dd4d
+    clear_services a b c
b7dd4d
 }
b7dd4d
 
b7dd4d
 test_template_dropins () {