Files
snapd/systemd/systemdtest
Fred Lotter 8f12a7dd63 systemd: add support for systemd unit alias names
systemd/systemd.go provides a systemctl interface to control and
monitor systemd units (such as services, targets etc...)

The code currently assumes that the unit name given to
systemctl will match the 'Id' DBUS property returned. This
assumption has worked fine so far, but does not work when using
unit name aliases to perform actions.

A unit alias (defined in the [Install] section of the real unit
file with Alias="...") creates a symlink to the real unit file,
when the unit is enabled.

For example:

bash$ systemctl enable reboot.target

Created symlink /etc/systemd/system/ctrl-alt-del.target →
                /lib/systemd/system/reboot.target.

bash$ systemctl show --property Id, Names ctrl-alt-del.target

Id=reboot.target
Names=ctrl-alt-del.target runlevel6.target reboot.target

If this request and result is added to the systemd unit test,
the following error is reported:

FAIL: systemd_test.go:238: SystemdTestSuite.TestStatus

systemd_test.go:277:
"cannot get unit status: queried status of ctrl-alt-del.target
but got status of reboot.target"

The 'Id' DBUS property represents the unit the alias points to.
The 'Names' DBUS property is constructed by systemd and consists
of both the id and all aliases defined for the unit.

The assumption made that the 'Id' is part of the 'Names' list
simplifies the code, and can be verified here (this has been
stable for at least the last 8 years):

src/core/dbus-unit.c:96 property_get_names(...)

- Add the 'Names' DBUS property to the list of requested
  properties when querying the unit status

- Add support to use the "requested" unit name (which could be
  an alias) as the real handle even if the underlying unit Id
  is different.

- Change the unit name verification code to check against the
  'Names' property which will include the id and aliases.

- Fix unit tests affected by the extra property and add a test
  case for aliased names.

Signed-off-by: Fred Lotter <fred.lotter@canonical.com>
2021-11-30 18:08:58 +02:00
..