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