1#!/usr/bin/env bash
2set -eux
3
4systemd-analyze log-level debug
5
6# Multiple level process tree, parent process stays up
7cat >/tmp/test56-exit-cgroup.sh <<EOF
8#!/usr/bin/env bash
9set -eux
10
11# process tree: systemd -> sleep
12sleep infinity &
13disown
14
15# process tree: systemd -> bash -> bash -> sleep
16((sleep infinity); true) &
17
18systemd-notify --ready
19
20# Run the stop/kill command
21\$1 &
22
23# process tree: systemd -> bash -> sleep
24sleep infinity
25EOF
26chmod +x /tmp/test56-exit-cgroup.sh
27
28# service should be stopped cleanly
29systemd-run --wait --unit=one -p Type=notify -p ExitType=cgroup \
30    /tmp/test56-exit-cgroup.sh 'systemctl stop one'
31
32# same thing with a truthy exec condition
33systemd-run --wait --unit=two -p Type=notify -p ExitType=cgroup \
34    -p ExecCondition=true \
35    /tmp/test56-exit-cgroup.sh 'systemctl stop two'
36
37# false exec condition: systemd-run should exit immediately with status code: 1
38systemd-run --wait --unit=three -p Type=notify -p ExitType=cgroup \
39    -p ExecCondition=false \
40    /tmp/test56-exit-cgroup.sh \
41    && { echo 'unexpected success'; exit 1; }
42
43# service should exit uncleanly (main process exits with SIGKILL)
44systemd-run --wait --unit=four -p Type=notify -p ExitType=cgroup \
45    /tmp/test56-exit-cgroup.sh 'systemctl kill --signal 9 four' \
46    && { echo 'unexpected success'; exit 1; }
47
48
49# Multiple level process tree, parent process exits quickly
50cat >/tmp/test56-exit-cgroup-parentless.sh <<EOF
51#!/usr/bin/env bash
52set -eux
53
54# process tree: systemd -> sleep
55sleep infinity &
56
57# process tree: systemd -> bash -> sleep
58((sleep infinity); true) &
59
60systemd-notify --ready
61
62# Run the stop/kill command after this bash process exits
63(sleep 1; \$1) &
64EOF
65chmod +x /tmp/test56-exit-cgroup-parentless.sh
66
67# service should be stopped cleanly
68systemd-run --wait --unit=five -p Type=notify -p ExitType=cgroup \
69    /tmp/test56-exit-cgroup-parentless.sh 'systemctl stop five'
70
71# service should still exit cleanly despite SIGKILL (the main process already exited cleanly)
72systemd-run --wait --unit=six -p Type=notify -p ExitType=cgroup \
73    /tmp/test56-exit-cgroup-parentless.sh 'systemctl kill --signal 9 six'
74
75
76systemd-analyze log-level info
77
78echo OK >/testok
79
80exit 0
81