b7dd4d
From 07829c10b8dfff3c6d0bb8d30e77b9b3193b1f0f Mon Sep 17 00:00:00 2001
b7dd4d
From: Anita Zhang <the.anitazha@gmail.com>
b7dd4d
Date: Mon, 4 Nov 2019 18:29:55 -0800
b7dd4d
Subject: [PATCH] core: change top-level drop-in from -.service.d to service.d
b7dd4d
b7dd4d
Discussed in #13743, the -.service semantic conflicts with the
b7dd4d
existing root mount and slice names, making this feature not
b7dd4d
uniformly extensible to all types. Change the name to be
b7dd4d
<type>.d instead.
b7dd4d
b7dd4d
Updating to this format also extends the top-level dropin to
b7dd4d
unit types.
b7dd4d
b7dd4d
(cherry picked from commit 3e1db806b0c18fd6138886ce67fac2655f09caef)
b7dd4d
b7dd4d
Resolves: #2051520
b7dd4d
---
b7dd4d
 man/systemd.service.xml            | 13 -------------
b7dd4d
 man/systemd.special.xml            |  9 ---------
b7dd4d
 man/systemd.unit.xml               | 11 ++++++++---
b7dd4d
 src/basic/special.h                |  4 ----
b7dd4d
 src/basic/unit-name.c              | 30 ------------------------------
b7dd4d
 src/basic/unit-name.h              |  2 --
b7dd4d
 src/core/service.c                 |  5 -----
b7dd4d
 src/shared/dropin.c                | 22 ++++++++++++----------
b7dd4d
 src/test/test-unit-name.c          | 19 -------------------
b7dd4d
 test/TEST-15-DROPIN/test-dropin.sh |  8 ++++----
b7dd4d
 10 files changed, 24 insertions(+), 99 deletions(-)
b7dd4d
b7dd4d
diff --git a/man/systemd.service.xml b/man/systemd.service.xml
b7dd4d
index 4164402d0e..1e30a564df 100644
b7dd4d
--- a/man/systemd.service.xml
b7dd4d
+++ b/man/systemd.service.xml
b7dd4d
@@ -62,19 +62,6 @@
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 06798cd9e2..fe6324a4a0 100644
b7dd4d
--- a/man/systemd.special.xml
b7dd4d
+++ b/man/systemd.special.xml
b7dd4d
@@ -117,15 +117,6 @@
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 5aa3bd1699..6f213ccd56 100644
b7dd4d
--- a/man/systemd.unit.xml
b7dd4d
+++ b/man/systemd.unit.xml
b7dd4d
@@ -190,9 +190,14 @@
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
+    <para>Units also support a top-level drop-in with <filename><replaceable>type</replaceable>.d/</filename>,
b7dd4d
+    where <replaceable>type</replaceable> may be e.g. <literal>service</literal> or <literal>socket</literal>,
b7dd4d
+    that allows altering or adding to the settings of all corresponding unit files on the system.
b7dd4d
+    The formatting and precedence of applying drop-in configurations follow what is defined above.
b7dd4d
+    Configurations in <filename><replaceable>type</replaceable>.d/</filename> have the lowest precedence
b7dd4d
+    compared to settings in the name specific override directories. So the contents of
b7dd4d
+    <filename>foo-.service.d/10-override.conf</filename> would override
b7dd4d
+    <filename>service.d/10-override.conf</filename>.</para>
b7dd4d
 
b7dd4d
     
b7dd4d
          people to use .d/ drop-ins instead. -->
b7dd4d
diff --git a/src/basic/special.h b/src/basic/special.h
b7dd4d
index 2915122929..379a3d7979 100644
b7dd4d
--- a/src/basic/special.h
b7dd4d
+++ b/src/basic/special.h
b7dd4d
@@ -103,7 +103,3 @@
b7dd4d
 
b7dd4d
 /* The root directory. */
b7dd4d
 #define SPECIAL_ROOT_MOUNT "-.mount"
b7dd4d
-
b7dd4d
-/* Used to apply settings to all services through drop-ins.
b7dd4d
- * Should not exist as an actual service. */
b7dd4d
-#define SPECIAL_ROOT_SERVICE "-.service"
b7dd4d
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
b7dd4d
index 078628d6e8..614eb8649b 100644
b7dd4d
--- a/src/basic/unit-name.c
b7dd4d
+++ b/src/basic/unit-name.c
b7dd4d
@@ -668,36 +668,6 @@ good:
b7dd4d
         return 0;
b7dd4d
 }
b7dd4d
 
b7dd4d
-bool service_unit_name_is_valid(const char *name) {
b7dd4d
-        _cleanup_free_ char *prefix = NULL, *s = NULL;
b7dd4d
-        const char *e, *service_name = name;
b7dd4d
-
b7dd4d
-        if (!unit_name_is_valid(name, UNIT_NAME_ANY))
b7dd4d
-                return false;
b7dd4d
-
b7dd4d
-        e = endswith(name, ".service");
b7dd4d
-        if (!e)
b7dd4d
-                return false;
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
-                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
-        /* Reject reserved service name(s). */
b7dd4d
-        if (streq(service_name, SPECIAL_ROOT_SERVICE))
b7dd4d
-                return false;
b7dd4d
-
b7dd4d
-        return true;
b7dd4d
-}
b7dd4d
-
b7dd4d
 int slice_build_parent_slice(const char *slice, char **ret) {
b7dd4d
         char *s, *dash;
b7dd4d
         int r;
b7dd4d
diff --git a/src/basic/unit-name.h b/src/basic/unit-name.h
b7dd4d
index 21729cba83..61abcd585b 100644
b7dd4d
--- a/src/basic/unit-name.h
b7dd4d
+++ b/src/basic/unit-name.h
b7dd4d
@@ -60,8 +60,6 @@ static inline int unit_name_mangle(const char *name, UnitNameMangle flags, char
b7dd4d
         return unit_name_mangle_with_suffix(name, flags, ".service", ret);
b7dd4d
 }
b7dd4d
 
b7dd4d
-bool service_unit_name_is_valid(const char *name);
b7dd4d
-
b7dd4d
 int slice_build_parent_slice(const char *slice, char **ret);
b7dd4d
 int slice_build_subslice(const char *slice, const char*name, char **subslice);
b7dd4d
 bool slice_name_is_valid(const char *name);
b7dd4d
diff --git a/src/core/service.c b/src/core/service.c
b7dd4d
index b3ef79228f..e8ae1a5772 100644
b7dd4d
--- a/src/core/service.c
b7dd4d
+++ b/src/core/service.c
b7dd4d
@@ -556,11 +556,6 @@ static int service_verify(Service *s) {
b7dd4d
                 }
b7dd4d
         }
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 -EINVAL;
b7dd4d
-        }
b7dd4d
-
b7dd4d
         if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]
b7dd4d
             && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) {
b7dd4d
                 /* FailureAction= only makes sense if one of the start or stop commands is specified.
b7dd4d
diff --git a/src/shared/dropin.c b/src/shared/dropin.c
b7dd4d
index 78ca7f4452..bd2a3c0feb 100644
b7dd4d
--- a/src/shared/dropin.c
b7dd4d
+++ b/src/shared/dropin.c
b7dd4d
@@ -19,7 +19,6 @@
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
@@ -170,6 +169,10 @@ static int unit_file_find_dirs(
b7dd4d
                         return r;
b7dd4d
         }
b7dd4d
 
b7dd4d
+        /* Return early for top level drop-ins. */
b7dd4d
+        if (unit_type_from_string(name) >= 0)
b7dd4d
+                return 0;
b7dd4d
+
b7dd4d
         /* Let's see if there's a "-" prefix for this unit name. If so, let's invoke ourselves for it. This will then
b7dd4d
          * recursively do the same for all our prefixes. i.e. this means given "foo-bar-waldo.service" we'll also
b7dd4d
          * search "foo-bar-.service" and "foo-.service".
b7dd4d
@@ -250,16 +253,15 @@ int unit_file_find_dropin_paths(
b7dd4d
                                                name);
b7dd4d
         }
b7dd4d
 
b7dd4d
-        /* Special drop in for -.service. Add this first as it's the most generic
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
-        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
+        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
         SET_FOREACH(name, names, i)
b7dd4d
                 STRV_FOREACH(p, lookup_path)
b7dd4d
diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c
b7dd4d
index b629df5aea..2b00ef8cb7 100644
b7dd4d
--- a/src/test/test-unit-name.c
b7dd4d
+++ b/src/test/test-unit-name.c
b7dd4d
@@ -347,24 +347,6 @@ static void test_unit_name_build(void) {
b7dd4d
         free(t);
b7dd4d
 }
b7dd4d
 
b7dd4d
-static void test_service_unit_name_is_valid(void) {
b7dd4d
-        assert_se(service_unit_name_is_valid("foo.service"));
b7dd4d
-        assert_se(service_unit_name_is_valid("foo@bar.service"));
b7dd4d
-        assert_se(service_unit_name_is_valid("foo@bar@bar.service"));
b7dd4d
-        assert_se(service_unit_name_is_valid("--.service"));
b7dd4d
-        assert_se(service_unit_name_is_valid(".-.service"));
b7dd4d
-        assert_se(service_unit_name_is_valid("-foo-bar.service"));
b7dd4d
-        assert_se(service_unit_name_is_valid("-foo-bar-.service"));
b7dd4d
-        assert_se(service_unit_name_is_valid("foo-bar-.service"));
b7dd4d
-
b7dd4d
-        assert_se(!service_unit_name_is_valid("-.service"));
b7dd4d
-        assert_se(!service_unit_name_is_valid(""));
b7dd4d
-        assert_se(!service_unit_name_is_valid("foo.slice"));
b7dd4d
-        assert_se(!service_unit_name_is_valid("@.service"));
b7dd4d
-        assert_se(!service_unit_name_is_valid("@bar.service"));
b7dd4d
-        assert_se(!service_unit_name_is_valid("-@.service"));
b7dd4d
-}
b7dd4d
-
b7dd4d
 static void test_slice_name_is_valid(void) {
b7dd4d
         assert_se( slice_name_is_valid(SPECIAL_ROOT_SLICE));
b7dd4d
         assert_se( slice_name_is_valid("foo.slice"));
b7dd4d
@@ -851,7 +833,6 @@ int main(int argc, char* argv[]) {
b7dd4d
         test_unit_prefix_is_valid();
b7dd4d
         test_unit_name_change_suffix();
b7dd4d
         test_unit_name_build();
b7dd4d
-        test_service_unit_name_is_valid();
b7dd4d
         test_slice_name_is_valid();
b7dd4d
         test_build_subslice();
b7dd4d
         test_build_parent_slice();
b7dd4d
diff --git a/test/TEST-15-DROPIN/test-dropin.sh b/test/TEST-15-DROPIN/test-dropin.sh
b7dd4d
index def2e03304..7836c6535d 100755
b7dd4d
--- a/test/TEST-15-DROPIN/test-dropin.sh
b7dd4d
+++ b/test/TEST-15-DROPIN/test-dropin.sh
b7dd4d
@@ -102,18 +102,18 @@ test_basic_dropins () {
b7dd4d
         check_ok b Wants c.service
b7dd4d
         systemctl stop a c
b7dd4d
 
b7dd4d
-    echo "*** test -.service.d/ top level drop-in"
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
+    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
+    rm -rf /usr/lib/systemd/system/service.d
b7dd4d
 
b7dd4d
     clear_services a b c
b7dd4d
 }