Merge pull request #13743 from anitazha/dropin_all_the_things

core: support top level drop-ins through -.service.d for service units
This commit is contained in:
Chris Down 2019-10-16 23:10:05 -04:00 committed by GitHub
commit 959daf9bfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 124 additions and 2 deletions

View file

@ -63,6 +63,19 @@
<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>

View file

@ -119,6 +119,15 @@
</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>

View file

@ -192,6 +192,10 @@
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. -->

View file

@ -102,8 +102,8 @@ static inline void *set_steal_first(Set *s) {
/* no set_steal_first_key */
/* no set_first_key */
static inline void *set_first(Set *s) {
return internal_hashmap_first_key_and_value(HASHMAP_BASE(s), false, NULL);
static inline void *set_first(const Set *s) {
return internal_hashmap_first_key_and_value(HASHMAP_BASE((Set *) s), false, NULL);
}
/* no set_next */

View file

@ -105,3 +105,7 @@
/* The root directory. */
#define SPECIAL_ROOT_MOUNT "-.mount"
/* Used to apply settings to all services through drop-ins.
* Should not exist as an actual service. */
#define SPECIAL_ROOT_SERVICE "-.service"

View file

@ -665,6 +665,36 @@ good:
return 0;
}
bool service_unit_name_is_valid(const char *name) {
_cleanup_free_ char *prefix = NULL, *s = NULL;
const char *e, *service_name = name;
if (!unit_name_is_valid(name, UNIT_NAME_ANY))
return false;
e = endswith(name, ".service");
if (!e)
return false;
/* 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)) {
if (unit_name_to_prefix(name, &prefix) < 0)
return false;
s = strjoin(prefix, ".service");
if (!s)
return false;
service_name = s;
}
/* Reject reserved service name(s). */
if (streq(service_name, SPECIAL_ROOT_SERVICE))
return false;
return true;
}
int slice_build_parent_slice(const char *slice, char **ret) {
char *s, *dash;
int r;

View file

@ -58,6 +58,8 @@ static inline int unit_name_mangle(const char *name, UnitNameMangle flags, char
return unit_name_mangle_with_suffix(name, flags, ".service", ret);
}
bool service_unit_name_is_valid(const char *name);
int slice_build_parent_slice(const char *slice, char **ret);
int slice_build_subslice(const char *slice, const char *name, char **subslice);
bool slice_name_is_valid(const char *name);

View file

@ -550,6 +550,11 @@ static int service_verify(Service *s) {
assert(s);
assert(UNIT(s)->load_state == UNIT_LOADED);
if (!service_unit_name_is_valid(UNIT(s)->id)) {
log_unit_error(UNIT(s), "Service name is invalid or reserved. Refusing.");
return -EINVAL;
}
if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]
&& UNIT(s)->success_action == EMERGENCY_ACTION_NONE) {
/* FailureAction= only makes sense if one of the start or stop commands is specified.

View 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);

View file

@ -355,6 +355,24 @@ static void test_unit_name_build(void) {
free(t);
}
static void test_service_unit_name_is_valid(void) {
assert_se(service_unit_name_is_valid("foo.service"));
assert_se(service_unit_name_is_valid("foo@bar.service"));
assert_se(service_unit_name_is_valid("foo@bar@bar.service"));
assert_se(service_unit_name_is_valid("--.service"));
assert_se(service_unit_name_is_valid(".-.service"));
assert_se(service_unit_name_is_valid("-foo-bar.service"));
assert_se(service_unit_name_is_valid("-foo-bar-.service"));
assert_se(service_unit_name_is_valid("foo-bar-.service"));
assert_se(!service_unit_name_is_valid("-.service"));
assert_se(!service_unit_name_is_valid(""));
assert_se(!service_unit_name_is_valid("foo.slice"));
assert_se(!service_unit_name_is_valid("@.service"));
assert_se(!service_unit_name_is_valid("@bar.service"));
assert_se(!service_unit_name_is_valid("-@.service"));
}
static void test_slice_name_is_valid(void) {
assert_se( slice_name_is_valid(SPECIAL_ROOT_SLICE));
assert_se( slice_name_is_valid("foo.slice"));
@ -840,6 +858,7 @@ int main(int argc, char* argv[]) {
test_unit_prefix_is_valid();
test_unit_name_change_suffix();
test_unit_name_build();
test_service_unit_name_is_valid();
test_slice_name_is_valid();
test_build_subslice();
test_build_parent_slice();

View file

@ -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
}