]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: when adding names to unit, require matching instance strings
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 27 May 2020 14:39:35 +0000 (16:39 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 10 Jun 2020 07:42:20 +0000 (09:42 +0200)
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

index 5c31559cfb3455f03770f1d2a73b6a33be34d37f..4412f6b7b77a46f2c6908a0a06b5587ab56b4a2d 100644 (file)
@@ -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;