mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-25 06:03:40 +03:00
596e447076
This introduces `ExitType=main|cgroup` for services. Similar to how `Type` specifies the launch of a service, `ExitType` is concerned with how systemd determines that a service exited. - If set to `main` (the current behavior), the service manager will consider the unit stopped when the main process exits. - The `cgroup` exit type is meant for applications whose forking model is not known ahead of time and which might not have a specific main process. The service will stay running as long as at least one process in the cgroup is running. This is intended for transient or automatically generated services, such as graphical applications inside of a desktop environment. Motivation for this is #16805. The original PR (#18782) was reverted (#20073) after realizing that the exit status of "the last process in the cgroup" can't reliably be known (#19385) This version instead uses the main process exit status if there is one and just listens to the cgroup empty event otherwise. The advantages of a service with `ExitType=cgroup` over scopes are: - Integrated logging / stdout redirection - Avoids the race / synchronisation issue between launch and scope creation - More extensive use of drop-ins and thus distro-level configuration: by moving from scopes to services we can have drop ins that will affect properties that can only be set during service creation, like `OOMPolicy` and security-related properties - It makes systemd-xdg-autostart-generator usable by fixing [1], as obviously only services can be used in the generator, not scopes. [1] https://bugs.kde.org/show_bug.cgi?id=433299
80 lines
2.2 KiB
Bash
Executable File
80 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -eux
|
|
|
|
systemd-analyze log-level debug
|
|
|
|
# Multiple level process tree, parent process stays up
|
|
cat >/tmp/test56-exit-cgroup.sh <<EOF
|
|
#!/usr/bin/env bash
|
|
set -eux
|
|
|
|
# process tree: systemd -> sleep
|
|
sleep infinity &
|
|
disown
|
|
|
|
# process tree: systemd -> bash -> bash -> sleep
|
|
((sleep infinity); true) &
|
|
|
|
systemd-notify --ready
|
|
|
|
# process tree: systemd -> bash -> sleep
|
|
sleep infinity
|
|
EOF
|
|
chmod +x /tmp/test56-exit-cgroup.sh
|
|
|
|
# service should be stopped cleanly
|
|
systemd-run --wait --unit=one -p Type=notify -p ExitType=cgroup \
|
|
-p ExecStartPost='bash -c "systemctl stop one &"' \
|
|
/tmp/test56-exit-cgroup.sh
|
|
|
|
# same thing with a truthy exec condition
|
|
systemd-run --wait --unit=two -p Type=notify -p ExitType=cgroup \
|
|
-p ExecCondition=true \
|
|
-p ExecStartPost='bash -c "systemctl stop two &"' \
|
|
/tmp/test56-exit-cgroup.sh
|
|
|
|
# false exec condition: systemd-run should exit immediately with status code: 1
|
|
systemd-run --wait --unit=three -p Type=notify -p ExitType=cgroup \
|
|
-p ExecCondition=false \
|
|
/tmp/test56-exit-cgroup.sh \
|
|
&& { echo 'unexpected success'; exit 1; }
|
|
|
|
# service should exit uncleanly (main process exits with SIGKILL)
|
|
systemd-run --wait --unit=four -p Type=notify -p ExitType=cgroup \
|
|
-p ExecStartPost='bash -c "systemctl kill --signal 9 four &"' \
|
|
/tmp/test56-exit-cgroup.sh \
|
|
&& { echo 'unexpected success'; exit 1; }
|
|
|
|
|
|
# Multiple level process tree, parent process exits quickly
|
|
cat >/tmp/test56-exit-cgroup-parentless.sh <<EOF
|
|
#!/usr/bin/env bash
|
|
set -eux
|
|
|
|
# process tree: systemd -> sleep
|
|
sleep infinity &
|
|
|
|
# process tree: systemd -> bash -> sleep
|
|
((sleep infinity); true) &
|
|
|
|
systemd-notify --ready
|
|
EOF
|
|
chmod +x /tmp/test56-exit-cgroup-parentless.sh
|
|
|
|
# service should be stopped cleanly
|
|
systemd-run --wait --unit=five -p Type=notify -p ExitType=cgroup \
|
|
-p ExecStartPost='bash -c "systemctl stop five &"' \
|
|
/tmp/test56-exit-cgroup-parentless.sh
|
|
|
|
# service should still exit cleanly despite SIGKILL (the main process already exited cleanly)
|
|
systemd-run --wait --unit=six -p Type=notify -p ExitType=cgroup \
|
|
-p ExecStartPost='bash -c "systemctl kill --signal 9 six &"' \
|
|
/tmp/test56-exit-cgroup-parentless.sh
|
|
|
|
|
|
systemd-analyze log-level info
|
|
|
|
echo OK >/testok
|
|
|
|
exit 0
|