shared/dropin: fix assert for invalid drop-in

Don't try to show top level drop-in for non-existent units or when trying to
instantiate non-instantiated units:

$ systemctl cat nonexistent@.service
Assertion 'name' failed at src/shared/dropin.c:143, function unit_file_find_dirs(). Aborting.
$ systemctl cat systemd-journald@.service
Assertion 'name' failed at src/shared/dropin.c:143, function unit_file_find_dirs(). Aborting.
This commit is contained in:
Topi Miettinen 2019-12-17 15:47:37 +02:00 committed by Zbigniew Jędrzejewski-Szmek
parent 51692fab56
commit 7a670b1dd9
2 changed files with 26 additions and 11 deletions

View file

@ -230,7 +230,6 @@ int unit_file_find_dropin_paths(
char ***ret) { char ***ret) {
_cleanup_strv_free_ char **dirs = NULL; _cleanup_strv_free_ char **dirs = NULL;
UnitType type = _UNIT_TYPE_INVALID;
char *name, **p; char *name, **p;
Iterator i; Iterator i;
int r; int r;
@ -240,22 +239,24 @@ int unit_file_find_dropin_paths(
/* All the names in the unit are of the same type so just grab one. */ /* All the names in the unit are of the same type so just grab one. */
name = (char*) set_first(names); name = (char*) set_first(names);
if (name) { if (name) {
UnitType type = _UNIT_TYPE_INVALID;
type = unit_name_to_type(name); type = unit_name_to_type(name);
if (type < 0) if (type < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to to derive unit type from unit name: %s", "Failed to to derive unit type from unit name: %s",
name); name);
}
/* Special top level drop in for "<unit type>.<suffix>". Add this first as it's the most generic /* Special top level drop in for "<unit type>.<suffix>". Add this first as it's the most generic
* and should be able to be overridden by more specific drop-ins. */ * and should be able to be overridden by more specific drop-ins. */
STRV_FOREACH(p, lookup_path) STRV_FOREACH(p, lookup_path)
(void) unit_file_find_dirs(original_root, (void) unit_file_find_dirs(original_root,
unit_path_cache, unit_path_cache,
*p, *p,
unit_type_to_string(type), unit_type_to_string(type),
dir_suffix, dir_suffix,
&dirs); &dirs);
}
SET_FOREACH(name, names, i) SET_FOREACH(name, names, i)
STRV_FOREACH(p, lookup_path) STRV_FOREACH(p, lookup_path)

View file

@ -419,9 +419,23 @@ EOF
clear_services a b clear_services a b
} }
test_invalid_dropins () {
echo "Testing invalid dropins..."
# Assertion failed on earlier versions, command exits unsuccessfully on later versions
systemctl cat nonexistent@.service || true
create_services a
systemctl daemon-reload
# Assertion failed on earlier versions, command exits unsuccessfully on later versions
systemctl cat a@.service || true
systemctl stop a
clear_services a
return 0
}
test_basic_dropins test_basic_dropins
test_template_dropins test_template_dropins
test_alias_dropins test_alias_dropins
test_masked_dropins test_masked_dropins
test_invalid_dropins
touch /testok touch /testok