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-10-26 17:12:44 +02:00
parent 3ac62a0ee4
commit dcebc9bae4
2 changed files with 51 additions and 39 deletions

View file

@ -112,35 +112,36 @@
<refsect1> <refsect1>
<title>The udev Database</title> <title>The udev Database</title>
<para>The settings of device units may either be configured via <para>Unit settings of device units may either be configured via unit files, or directly from the udev
unit files, or directly from the udev database (which is database. The following udev device properties are understood by the service manager:</para>
recommended). The following udev device properties are understood
by systemd:</para>
<variablelist class='udev-directives'> <variablelist class='udev-directives'>
<varlistentry> <varlistentry>
<term><varname>SYSTEMD_WANTS=</varname></term> <term><varname>SYSTEMD_WANTS=</varname></term>
<term><varname>SYSTEMD_USER_WANTS=</varname></term> <term><varname>SYSTEMD_USER_WANTS=</varname></term>
<listitem><para>Adds dependencies of type <listitem><para>Adds dependencies of type <varname>Wants=</varname> from the device unit to the specified
<varname>Wants</varname> from the device unit to all listed units. <varname>SYSTEMD_WANTS=</varname> is read by the system service manager,
units. The first form is used by the system systemd instance, <varname>SYSTEMD_USER_WANTS=</varname> by user service manager instances. These properties may be used to
the second by user systemd instances. Those settings may be activate arbitrary units when a specific device becomes available.</para>
used to activate arbitrary units when a specific device
becomes available.</para>
<para>Note that this and the other tags are not taken into <para>Note that this and the other udev device properties are not taken into account unless the device is
account unless the device is tagged with the tagged with the <literal>systemd</literal> tag in the udev database, because otherwise the device is not
<literal>systemd</literal> string in the udev database, exposed as a systemd unit (see above).</para>
because otherwise the device is not exposed as a systemd unit
(see above).</para>
<para>Note that systemd will only act on <para>Note that systemd will only act on <varname>Wants=</varname> dependencies when a device first becomes
<varname>Wants</varname> dependencies when a device first active. It will not act on them if they are added to devices that are already active. Use
becomes active. It will not act on them if they are added to <varname>SYSTEMD_READY=</varname> (see below) to configure when a udev device shall be considered active, and
devices that are already active. Use thus when to trigger the dependencies.</para>
<varname>SYSTEMD_READY=</varname> (see below) to influence on
which udev event to trigger the dependencies. <!-- Note that we don't document here that we actually apply unit_name_mangle() to all specified names, since
</para></listitem> that's kinda ugly, and people should instead specify correctly escaped names -->
<para>The specified property value should be a space-separated list of valid unit names. If a unit template
name is specified (that is, a unit name containing an <literal>@</literal> character indicating a unit name to
use for multiple instantiation, but with an empty instance name following the <literal>@</literal>), it will be
automatically instantiated by the device's <literal>sysfs</literal> path (that is: the path is escaped and
inserted as instance name into the template unit name). This is useful in order to instantiate a specific
template unit once for each device that appears and matches specific properties.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
@ -152,20 +153,14 @@
<varlistentry> <varlistentry>
<term><varname>SYSTEMD_READY=</varname></term> <term><varname>SYSTEMD_READY=</varname></term>
<listitem><para>If set to 0, systemd will consider this device <listitem><para>If set to 0, systemd will consider this device unplugged even if it shows up in the udev
unplugged even if it shows up in the udev tree. If this tree. If this property is unset or set to 1, the device will be considered plugged if it is visible in the udev
property is unset or set to 1, the device will be considered tree.</para>
plugged if it is visible in the udev tree. This property has
no influence on the behavior when a device disappears from the
udev tree.</para>
<para>This option is useful to support devices that initially <para>This option is useful for devices that initially show up in an uninitialized state in the tree, and for
show up in an uninitialized state in the tree, and for which a which a <literal>changed</literal> event is generated the moment they are fully set up. Note that
<literal>changed</literal> event is generated the moment they <varname>SYSTEMD_WANTS=</varname> (see above) is not acted on as long as <varname>SYSTEMD_READY=0</varname> is
are fully set up. Note that <varname>SYSTEMD_WANTS=</varname> set for a device.</para></listitem>
(see above) is not acted on as long as
<varname>SYSTEMD_READY=0</varname> is set for a
device.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View file

@ -277,11 +277,28 @@ static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
if (r == -ENOMEM) if (r == -ENOMEM)
return log_oom(); return log_oom();
if (r < 0) 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 (unit_name_is_valid(word, UNIT_NAME_TEMPLATE) && DEVICE(u)->sysfs) {
if (r < 0) _cleanup_free_ char *escaped = NULL;
return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word);
/* 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); r = unit_add_dependency_by_name(u, UNIT_WANTS, k, NULL, true, UNIT_DEPENDENCY_UDEV);
if (r < 0) if (r < 0)