From d383acad25f7f57692f88326e5505e9194ffe6dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 27 May 2020 16:39:35 +0200 Subject: [PATCH] core: when adding names to unit, require matching instance strings We would check that the instance is present in both units (or missing in both). But when it is defined, it should be the same in both. The comment in the code was explicitly saying that differing instance strings are allowed, but this mostly seems to be a left-over from old times. The man page is pretty clear: > the instance (if any) is always uniquely defined for a given unit and all its > aliases. --- src/core/unit.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/core/unit.c b/src/core/unit.c index 5c31559cfb..4412f6b7b7 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -272,10 +272,8 @@ int unit_add_name(Unit *u, const char *text) { if (instance && !unit_type_may_template(t)) return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), "templates are not allowed for name '%s': %m", name); - /* Ensure that this unit is either instanced or not instanced, - * but not both. Note that we do allow names with different - * instance names however! */ - if (u->type != _UNIT_TYPE_INVALID && !!u->instance != !!instance) + /* Ensure that this unit either has no instance, or that the instance matches. */ + if (u->type != _UNIT_TYPE_INVALID && !streq_ptr(u->instance, instance)) return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), "instance is illegal: u->type(%d), u->instance(%s) and i(%s) for name '%s': %m", u->type, u->instance, instance, name); @@ -962,15 +960,15 @@ int unit_merge(Unit *u, Unit *other) { if (u->type != other->type) return -EINVAL; - if (!u->instance != !other->instance) - return -EINVAL; - if (!unit_type_may_alias(u->type)) /* Merging only applies to unit names that support aliases */ return -EEXIST; if (!IN_SET(other->load_state, UNIT_STUB, UNIT_NOT_FOUND)) return -EEXIST; + if (!streq_ptr(u->instance, other->instance)) + return -EINVAL; + if (other->job) return -EEXIST;