]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-82.sh
pid1: add SurviveFinalKillSignal= to skip units on final sigterm/sigkill spree
[thirdparty/systemd.git] / test / units / testsuite-82.sh
CommitLineData
093d5456
LP
1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -ex
4set -o pipefail
5
6systemd-analyze log-level debug
7
8export SYSTEMD_LOG_LEVEL=debug
9
10if [ -f /run/testsuite82.touch3 ]; then
11 echo "This is the fourth boot!"
12 systemd-notify --status="Fourth Boot"
13
14 rm /run/testsuite82.touch3
15 mount
16 rmdir /original-root /run/nextroot
17
18 # Check that the fdstore entry still exists
19 test "$LISTEN_FDS" -eq 3
20 read -r x <&5
21 test "$x" = "oinkoink"
22
559214cb
LB
23 # Check that the surviving service is still around
24 test "$(systemctl show -P ActiveState testsuite-82-survive.service)" = "active"
093d5456
LP
25 test "$(systemctl show -P ActiveState testsuite-82-nosurvive.service)" != "active"
26
093d5456
LP
27 # All succeeded, exit cleanly now
28
29elif [ -f /run/testsuite82.touch2 ]; then
30 echo "This is the third boot!"
31 systemd-notify --status="Third Boot"
32
33 rm /run/testsuite82.touch2
34
35 # Check that the fdstore entry still exists
36 test "$LISTEN_FDS" -eq 2
37 read -r x <&4
38 test "$x" = "miaumiau"
39
40 # Upload another entry
41 T="/dev/shm/fdstore.$RANDOM"
42 echo "oinkoink" >"$T"
43 systemd-notify --fd=3 --pid=parent 3<"$T"
44 rm "$T"
45
559214cb
LB
46 # Check that the surviving service is still around
47 test "$(systemctl show -P ActiveState testsuite-82-survive.service)" = "active"
093d5456
LP
48 test "$(systemctl show -P ActiveState testsuite-82-nosurvive.service)" != "active"
49
50 # Test that we really are in the new overlayfs root fs
51 read -r x </lower
52 test "$x" = "miep"
663e2756 53 cmp /etc/os-release /run/systemd/propagate/.os-release-stage/os-release
3f37a825 54 grep -q MARKER=1 /etc/os-release
093d5456
LP
55
56 # Switch back to the original root, away from the overlayfs
57 mount --bind /original-root /run/nextroot
58 mount
59
559214cb
LB
60 # Restart the unit that is not supposed to survive
61 systemd-run -p Type=exec --unit=testsuite-82-nosurvive.service sleep infinity
62
093d5456
LP
63 # Now issue the soft reboot. We should be right back soon.
64 touch /run/testsuite82.touch3
65 systemctl --no-block soft-reboot
66
67 # Now block until the soft-boot killing spree kills us
68 exec sleep infinity
69
70elif [ -f /run/testsuite82.touch ]; then
71 echo "This is the second boot!"
72 systemd-notify --status="Second Boot"
73
74 # Clean up what we created earlier
75 rm /run/testsuite82.touch
76
77 # Check that the fdstore entry still exists
78 test "$LISTEN_FDS" -eq 1
79 read -r x <&3
80 test "$x" = "wuffwuff"
81
e4aab5cf
LB
82 # Check that we got a PrepareForShutdownWithMetadata signal with the right type
83 test "$(jq .payload.data[1].type.data </run/testsuite82.signal)" = "\"soft-reboot\""
84
093d5456
LP
85 # Upload another entry
86 T="/dev/shm/fdstore.$RANDOM"
87 echo "miaumiau" >"$T"
88 systemd-notify --fd=3 --pid=parent 3<"$T"
89 rm "$T"
90
559214cb
LB
91 # Check that the surviving service is still around
92 test "$(systemctl show -P ActiveState testsuite-82-survive.service)" = "active"
093d5456
LP
93 test "$(systemctl show -P ActiveState testsuite-82-nosurvive.service)" != "active"
94
95 # This time we test the /run/nextroot/ root switching logic. (We synthesize a new rootfs from the old via overlayfs)
96 mkdir -p /run/nextroot /tmp/nextroot-lower /original-root
97 mount -t tmpfs tmpfs /tmp/nextroot-lower
98 echo miep >/tmp/nextroot-lower/lower
3f37a825
LB
99
100 # Copy os-release away, so that we can manipulate it and check that it is updated in the propagate
bf85c239 101 # directory across soft reboots. Try to cover corner cases by truncating it.
3f37a825 102 mkdir -p /tmp/nextroot-lower/usr/lib
bf85c239 103 grep ID /etc/os-release >/tmp/nextroot-lower/usr/lib/os-release
3f37a825 104 echo MARKER=1 >>/tmp/nextroot-lower/usr/lib/os-release
663e2756 105 cmp /etc/os-release /run/systemd/propagate/.os-release-stage/os-release
3f37a825
LB
106 (! grep -q MARKER=1 /etc/os-release)
107
108 mount -t overlay nextroot /run/nextroot -o lowerdir=/tmp/nextroot-lower:/,ro
093d5456
LP
109
110 # Bind our current root into the target so that we later can return to it
111 mount --bind / /run/nextroot/original-root
112
559214cb
LB
113 # Restart the unit that is not supposed to survive
114 systemd-run -p Type=exec --unit=testsuite-82-nosurvive.service sleep infinity
115
093d5456
LP
116 # Now issue the soft reboot. We should be right back soon.
117 touch /run/testsuite82.touch2
118 systemctl --no-block soft-reboot
119
120 # Now block until the soft-boot killing spree kills us
121 exec sleep infinity
122else
123 # This is the first boot
124 systemd-notify --status="First Boot"
125
9a27ef09 126 # Let's upload an fd to the fdstore, so that we can verify fdstore passing works correctly
093d5456
LP
127 T="/dev/shm/fdstore.$RANDOM"
128 echo "wuffwuff" >"$T"
129 systemd-notify --fd=3 --pid=parent 3<"$T"
130 rm "$T"
131
559214cb
LB
132 # Configure this transient unit to survive the soft reboot - it will not conflict with shutdown.target
133 # and it will be ignored on the isolate that happens in the next boot.
134 systemd-run -p Type=exec --unit=testsuite-82-survive.service \
135 --property SurviveFinalKillSignal=yes \
136 --property IgnoreOnIsolate=yes \
137 --property DefaultDependencies=no \
138 --property After=basic.target \
139 --property "Conflicts=reboot.target kexec.target poweroff.target halt.target emergency.target rescue.target" \
140 --property "Before=reboot.target kexec.target poweroff.target halt.target emergency.target rescue.target" \
141 sleep infinity
142 systemd-run -p Type=exec --unit=testsuite-82-nosurvive.service sleep infinity
093d5456 143
e4aab5cf
LB
144 # Check that we can set up an inhibitor, and that busctl monitor sees the
145 # PrepareForShutdownWithMetadata signal and that it says 'soft-reboot'.
146 systemd-run --unit busctl.service --property StandardOutput=file:/run/testsuite82.signal \
147 busctl monitor --json=pretty --match 'sender=org.freedesktop.login1,path=/org/freedesktop/login1,interface=org.freedesktop.login1.Manager,member=PrepareForShutdownWithMetadata,type=signal'
148 systemd-run --unit inhibit.service \
149 systemd-inhibit --what=shutdown --who=test --why=test --mode=delay \
150 sleep infinity
151
093d5456
LP
152 # Now issue the soft reboot. We should be right back soon.
153 touch /run/testsuite82.touch
e4aab5cf 154 systemctl --no-block --check-inhibitors=yes soft-reboot
093d5456
LP
155
156 # Now block until the soft-boot killing spree kills us
157 exec sleep infinity
158fi
159
160systemd-analyze log-level info
093d5456 161
07268394 162touch /testok
093d5456 163systemctl --no-block poweroff