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