Skip to content

Commit

Permalink
dropin: when looking for dropins for a unit, also look within "-" pre…
Browse files Browse the repository at this point in the history
…fix unit dirs

This extends the logic by which we look for drop-ins for unit files when
loading them. Previously for a unit "foo-quux-bar.service" we'd look in
a directory "foo-quux-bar.service.d" accompanying it for extension
dropins. With this change we'll additionally look in:
"foo-quux-.service.d" and "foo-.service.d", i.e. we'll truncate the unit
name after every dash.

This is an alternative to templating for many services, as it permits
configuring defaults for sets of units that all use the same prefix in
the unit name. This is particularly useful in slice, mount and
automount units which reflect a hierarchy of concepts, as it permits
setting defaults for specific subsets of the tree. For example, in order
to provide every user with a memory of 1G it's now possible to do:

    # mkdir -p /etc/systemd/system/user-.slice.d
    # cat > /etc/systemd/system/user-.slice.d/50-memory.conf << EOF
    [Slice]
    MemoryMax=1G
    EOF
    # systemctl daemon-reload

This makes use of the fact that every user gets his own slice unit when
logging in, named "user-$UID.slice".

This doesn't precisely provide what is requested in systemd#2556, but it does
provide equivalent functionality.

Fixes: systemd#2556
See: systemd#3504 systemd#7599
  • Loading branch information
poettering committed Mar 28, 2018
1 parent 7e14cf2 commit 2f3f86a
Showing 1 changed file with 61 additions and 8 deletions.
69 changes: 61 additions & 8 deletions src/shared/dropin.c
Expand Up @@ -151,34 +151,87 @@ static int unit_file_find_dirs(
const char *suffix,
char ***dirs) {

_cleanup_free_ char *prefix = NULL, *instance = NULL, *usuffix = NULL, *built = NULL;
bool is_instance;
const char *dash;
bool chopped;
char *path;
size_t n;
int r;

assert(unit_path);
assert(name);
assert(suffix);

path = strjoina(unit_path, "/", name, suffix);

if (!unit_path_cache || set_get(unit_path_cache, path)) {
r = unit_file_find_dir(original_root, path, dirs);
if (r < 0)
return r;
}

if (unit_name_is_valid(name, UNIT_NAME_INSTANCE)) {
/* Also try the template dir */

is_instance = unit_name_is_valid(name, UNIT_NAME_INSTANCE);
if (is_instance) { /* Also try the template dir */
_cleanup_free_ char *template = NULL;

r = unit_name_template(name, &template);
if (r < 0)
return log_error_errno(r, "Failed to generate template from unit name: %m");

return unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs);
r = unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs);
if (r < 0)
return r;
}

return 0;
/* Let's see if there's a "-" prefix for this unit name. If so, let's invoke ourselves for it. This will then
* recursively do the same for all our prefixes. i.e. this means given "foo-bar-waldo.service" we'll also
* search "foo-bar-.service" and "foo-.service".
*
* Note the order in which we do it: we traverse up adding drop-ins on each step. This means the more specific
* drop-ins may override the more generic drop-ins, which is the intended behaviour. */

r = unit_name_to_prefix(name, &prefix);
if (r < 0)
return log_error_errno(r, "Failed to derive unit name prefix from unit name: %m");

chopped = false;
for (;;) {
dash = strrchr(prefix, '-');
if (!dash) /* No dash? if so we are done */
return 0;

n = (size_t) (dash - prefix);
if (n == 0) /* Leading dash? If so, we are done */
return 0;

if (prefix[n+1] != 0 || chopped) {
prefix[n+1] = 0;
break;
}

/* Trailing dash? If so, chop it off and try again, but not more than once. */
prefix[n] = 0;
chopped = true;
}

if (!unit_prefix_is_valid(prefix))
return 0;

r = unit_name_to_suffix(name, &usuffix);
if (r < 0)
return log_error_errno(r, "Failed to derive unit type from unit name: %m");

if (is_instance) {
r = unit_name_to_instance(name, &instance);
if (r < 0)
return log_error_errno(r, "Failed to derive unit name instance from unit name: %m");
}

r = unit_name_build(prefix, instance, usuffix, &built);
if (r < 0)
return log_error_errno(r, "Failed to build prefix unit name: %m");

return unit_file_find_dirs(original_root, unit_path_cache, unit_path, built, suffix, dirs);
}

int unit_file_find_dropin_paths(
Expand All @@ -191,15 +244,15 @@ int unit_file_find_dropin_paths(
char ***ret) {

_cleanup_strv_free_ char **dirs = NULL;
Iterator i;
char *t, **p;
Iterator i;
int r;

assert(ret);

SET_FOREACH(t, names, i)
STRV_FOREACH(p, lookup_path)
unit_file_find_dirs(original_root, unit_path_cache, *p, t, dir_suffix, &dirs);
(void) unit_file_find_dirs(original_root, unit_path_cache, *p, t, dir_suffix, &dirs);

if (strv_isempty(dirs)) {
*ret = NULL;
Expand Down

0 comments on commit 2f3f86a

Please sign in to comment.