core: when a unit template is specified in SYSTEMD_WANTS=, instantiate it with sysfs path

This should make cases like the user's setup in #7109 a lot easier to
handle, as in that case we'll do the right escaping automatically.
This commit is contained in:
Lennart Poettering
2017-11-10 19:52:41 +01:00
parent 3ac62a0ee4
commit dcebc9bae4
2 changed files with 51 additions and 39 deletions
+21 -4
View File
@@ -277,11 +277,28 @@ static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
if (r == -ENOMEM)
return log_oom();
if (r < 0)
return log_unit_error_errno(u, r, "Failed to add parse %s: %m", property);
return log_unit_error_errno(u, r, "Failed to parse property %s with value %s: %m", property, wants);
r = unit_name_mangle(word, UNIT_NAME_NOGLOB, &k);
if (r < 0)
return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word);
if (unit_name_is_valid(word, UNIT_NAME_TEMPLATE) && DEVICE(u)->sysfs) {
_cleanup_free_ char *escaped = NULL;
/* If the unit name is specified as template, then automatically fill in the sysfs path of the
* device as instance name, properly escaped. */
r = unit_name_path_escape(DEVICE(u)->sysfs, &escaped);
if (r < 0)
return log_unit_error_errno(u, r, "Failed to escape %s: %m", DEVICE(u)->sysfs);
r = unit_name_replace_instance(word, escaped, &k);
if (r < 0)
return log_unit_error_errno(u, r, "Failed to build %s instance of template %s: %m", escaped, word);
} else {
/* If this is not a template, then let's mangle it so, that it becomes a valid unit name. */
r = unit_name_mangle(word, UNIT_NAME_NOGLOB, &k);
if (r < 0)
return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word);
}
r = unit_add_dependency_by_name(u, UNIT_WANTS, k, NULL, true, UNIT_DEPENDENCY_UDEV);
if (r < 0)