]> git.ipfire.org Git - thirdparty/systemd.git/commit
shared/install: correctly install alias for units outside search path
authorNick Rosbrook <enr0n@ubuntu.com>
Fri, 15 Mar 2024 19:14:05 +0000 (15:14 -0400)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 18 Mar 2024 11:58:07 +0000 (11:58 +0000)
commit6fec0fed108593ed9f011aa388aa70675f5ba733
tree8cbcf5680126e1e8a67b1be67853f84beb44e5fd
parentce880172552534e7416ae3af697053c0df58b770
shared/install: correctly install alias for units outside search path

Currently, if a unit file is enabled from outside of the search path,
and that unit has an alias, then the symlink ends up pointing outside of
the search path too. For example:

 $ cat /tmp/a.service
 [Service]
 ExecStart=sleep infinity

 [Install]
 Alias=b.service
 WantedBy=multi-user.target

 $ systemctl enable /tmp/a.service
 Created symlink /etc/systemd/system/a.service → /tmp/a.service.
 Created symlink /etc/systemd/system/b.service → /tmp/a.service.
 Created symlink /etc/systemd/system/multi-user.target.wants/a.service → /tmp/a.service.

This then means the alias is treated as a separate unit:

 $ systemctl start a.service
 $ sudo systemctl status a
 ● a.service
  Loaded: loaded (/etc/systemd/system/a.service; enabled; preset: enabled)
  Active: active (running) since Fri 2024-03-15 15:17:49 EDT; 9s ago
 Main PID: 769593 (sleep)
   Tasks: 1 (limit: 18898)
  Memory: 220.0K
     CPU: 5ms
  CGroup: /system.slice/a.service
          └─769593 sleep infinity

 Mar 15 15:17:49 six systemd[1]: Started a.service.
 $ sudo systemctl status b
 ○ b.service
  Loaded: loaded (/etc/systemd/system/b.service; alias)
  Active: inactive (dead)

To fix this, make sure the alias uses a target that is inside the search
path. Since the unit file itself is outside of the search path, a
symlink inside the search path will have been created already. Hence,
just point the alias symlink to that recently created symlink.
src/shared/install.c
src/test/test-install-root.c
test/test-systemctl-enable.sh