teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0745-shared-dropin-support-.service.d-top-level-drop-in-f.patch

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