]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/dropin: support -.service.d/ top level drop-in for service units 13743/head
authorAnita Zhang <the.anitazha@gmail.com>
Sat, 5 Oct 2019 00:39:34 +0000 (17:39 -0700)
committerAnita Zhang <the.anitazha@gmail.com>
Tue, 15 Oct 2019 18:14:54 +0000 (11:14 -0700)
Closes #12830

man/systemd.service.xml
man/systemd.special.xml
man/systemd.unit.xml
src/basic/unit-name.c
src/core/service.c
src/shared/dropin.c
test/TEST-15-DROPIN/test-dropin.sh

index af14eedfcc6fac2b6681da021bf3c149896bcaa7..7d120c32435fcd4efb49035ab9e49cab8df02ab7 100644 (file)
     <para>The <citerefentry><refentrytitle>systemd-run</refentrytitle><manvolnum>1</manvolnum></citerefentry>
     command allows creating <filename>.service</filename> and <filename>.scope</filename> units dynamically
     and transiently from the command line.</para>
+
+    <para>In addition to the various drop-in behaviors described in
+    <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+    services also support a top-level drop-in with <filename>-.service.d/</filename> that allows
+    altering or adding to the settings of all services on the system.
+    The formatting and precedence of applying drop-in configurations follow what is defined in
+    <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
+    However, configurations in <filename>-.service.d/</filename> have the lowest precedence compared to settings
+    in the service specific override directories. For example, for <filename>foo-bar-baz.service</filename>,
+    drop-ins in <filename>foo-bar-baz.service.d/</filename> override the ones in
+    <filename>foo-bar-.service.d/</filename>, which override the ones <filename>foo-.service.d/</filename>,
+    which override the ones in <filename>-.service.d/</filename>.
+    </para>
   </refsect1>
 
   <refsect1>
index 248fb924dba4935a383af512162aeb64dbe314ca..afd14b977c043154acb90ffd74ea9eb6f45b845d 100644 (file)
           </listitem>
         </varlistentry>
 
+        <varlistentry>
+          <term><filename>-.service</filename></term>
+          <listitem>
+            <para>This is a reserved unit name used to support top-level drop-ins for services. See
+            <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+            for details.</para>
+          </listitem>
+        </varlistentry>
+
         <varlistentry>
           <term><filename>basic.target</filename></term>
           <listitem>
index d812108d3cd7ab8283adc5265292b16db0fe8ddf..f6e9d2a17caf30999be72abd9df4ed782c29da06 100644 (file)
     over unit files wherever located. Multiple drop-in files with different names are applied in
     lexicographic order, regardless of which of the directories they reside in.</para>
 
+    <para>Service units also support a top-level drop-in directory for modifying the settings of all service units. See
+    <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+    for details.</para>
+
     <!-- Note that we do not document .include here, as we consider it mostly obsolete, and want
          people to use .d/ drop-ins instead. -->
 
index ecbf5ae7f5de3da2e339d3ca984257e17d3e5961..bcd01f8515aa5c4d968aee5c50c4865b66771ccc 100644 (file)
@@ -678,8 +678,13 @@ bool service_unit_name_is_valid(const char *name) {
 
         /* If it's a template or instance, get the prefix as a service name. */
         if (unit_name_is_valid(name, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) {
-                assert_se(unit_name_to_prefix(name, &prefix) == 0);
-                assert_se(s = strjoin(prefix, ".service"));
+                if (unit_name_to_prefix(name, &prefix) < 0)
+                        return false;
+
+                s = strjoin(prefix, ".service");
+                if (!s)
+                        return false;
+
                 service_name = s;
         }
 
index 6880b24535b1686fd4193723323e6afa12d40f80..24ad1e77fa913a17f1603578fe85fc6dfe6fea5a 100644 (file)
@@ -554,7 +554,7 @@ static int service_verify(Service *s) {
 
         if (!service_unit_name_is_valid(UNIT(s)->id)) {
                 log_unit_error(UNIT(s), "Service name is invalid or reserved. Refusing.");
-                return -ENOEXEC;
+                return -EINVAL;
         }
 
         if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]
index 4a29bd09c5683444f2bd67371911e455d072eb70..2f67a44bf0b90b9a87f9a616466f9440cb7cc318 100644 (file)
@@ -19,6 +19,7 @@
 #include "mkdir.h"
 #include "path-util.h"
 #include "set.h"
+#include "special.h"
 #include "string-util.h"
 #include "strv.h"
 #include "unit-name.h"
@@ -226,12 +227,34 @@ int unit_file_find_dropin_paths(
                 char ***ret) {
 
         _cleanup_strv_free_ char **dirs = NULL;
+        UnitType type = _UNIT_TYPE_INVALID;
         char *name, **p;
         Iterator i;
         int r;
 
         assert(ret);
 
+        /* All the names in the unit are of the same type so just grab one. */
+        name = (char*) set_first(names);
+        if (name) {
+                type = unit_name_to_type(name);
+                if (type < 0)
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                               "Failed to to derive unit type from unit name: %s",
+                                               name);
+        }
+
+        /* Special drop in for -.service. Add this first as it's the most generic
+         * and should be able to be overridden by more specific drop-ins. */
+        if (type == UNIT_SERVICE)
+                STRV_FOREACH(p, lookup_path)
+                        (void) unit_file_find_dirs(original_root,
+                                                   unit_path_cache,
+                                                   *p,
+                                                   SPECIAL_ROOT_SERVICE,
+                                                   dir_suffix,
+                                                   &dirs);
+
         SET_FOREACH(name, names, i)
                 STRV_FOREACH(p, lookup_path)
                         (void) unit_file_find_dirs(original_root, unit_path_cache, *p, name, dir_suffix, &dirs);
index 2cef5a3c5b5987bfed8eb6c884cb2ec3208a7605..75c36df2b8265eea1dd520b4749e64189936022f 100755 (executable)
@@ -101,6 +101,19 @@ test_basic_dropins () {
     check_ok b Wants c.service
     systemctl stop a c
 
+    echo "*** test -.service.d/ top level drop-in"
+    create_services a b
+    check_ko a ExecCondition "/bin/echo a"
+    check_ko b ExecCondition "/bin/echo b"
+    mkdir -p /usr/lib/systemd/system/-.service.d
+    cat >/usr/lib/systemd/system/-.service.d/override.conf <<EOF
+[Service]
+ExecCondition=/bin/echo %n
+EOF
+    check_ok a ExecCondition "/bin/echo a"
+    check_ok b ExecCondition "/bin/echo b"
+    rm -rf /usr/lib/systemd/system/-.service.d
+
     clear_services a b c
 }