]>
Commit | Line | Data |
---|---|---|
ebefce6e FS |
1 | #!/usr/bin/env bash |
2 | # SPDX-License-Identifier: LGPL-2.1-or-later | |
3 | set -eux | |
4 | set -o pipefail | |
5 | ||
6 | if systemd-detect-virt -cq; then | |
7 | echo "This test requires a VM, skipping the test" | |
8 | exit 0 | |
9 | fi | |
10 | ||
11 | # shellcheck disable=SC2317 | |
12 | at_exit() { | |
13 | local EC=$? | |
14 | ||
15 | if [[ $EC -ne 0 ]] && [[ -e /tmp/console.dump ]]; then | |
16 | cat /tmp/console.dump | |
17 | fi | |
18 | ||
19 | if mountpoint -q /var/log/journal; then | |
20 | journalctl --relinquish-var | |
21 | umount /var/log/journal | |
22 | journalctl --flush | |
23 | fi | |
24 | ||
8f7c876b FS |
25 | rm -f /run/systemd/journald.conf.d/99-forward-to-console.conf |
26 | systemctl restart systemd-journald | |
27 | ||
ebefce6e FS |
28 | return 0 |
29 | } | |
30 | ||
31 | vcs_dump_and_check() { | |
32 | local expected_message="${1:?}" | |
33 | ||
34 | # It might take a while before the systemd-bsod stuff appears on the VCS, | |
35 | # so try it a couple of times | |
36 | for _ in {0..9}; do | |
37 | setterm --term linux --dump --file /tmp/console.dump | |
4457a2e0 | 38 | if grep -aq "Press any key to exit" /tmp/console.dump |
ebefce6e | 39 | grep -aq "$expected_message" /tmp/console.dump |
4457a2e0 | 40 | grep -aq "The current boot has failed" /tmp/console.dump; then |
ebefce6e FS |
41 | |
42 | return 0 | |
43 | fi | |
44 | ||
45 | sleep .5 | |
46 | done | |
47 | ||
48 | return 1 | |
49 | } | |
50 | ||
51 | # Since systemd-bsod always fetches only the first emergency message from the | |
52 | # current boot, let's temporarily overmount /var/log/journal with a tmpfs, | |
53 | # as we're going to wipe it multiple times, but we need to keep the original | |
54 | # journal intact for the other tests to work correctly. | |
8f7c876b FS |
55 | # |
56 | # Also, since we'll eventually lose the journal from this test, let's temporarily | |
57 | # forward everything to console, to make potential fails debug-able. | |
ebefce6e | 58 | trap at_exit EXIT |
8f7c876b FS |
59 | mkdir -p /run/systemd/journald.conf.d/ |
60 | echo -ne '[Journal]\nForwardToConsole=yes' >/run/systemd/journald.conf.d/99-forward-to-console.conf | |
ebefce6e FS |
61 | mount -t tmpfs tmpfs /var/log/journal |
62 | systemctl restart systemd-journald | |
63 | ||
64 | systemctl stop systemd-bsod | |
65 | ||
66 | # Since we just wiped the journal, there should be no emergency messages and | |
67 | # systemd-bsod should be just a no-op | |
68 | timeout 10s /usr/lib/systemd/systemd-bsod | |
69 | setterm --term linux --dump --file /tmp/console.dump | |
70 | (! grep "The current boot has failed" /tmp/console.dump) | |
71 | ||
72 | # systemd-bsod should pick up emergency messages only with UID=0, so let's check | |
73 | # that as well | |
74 | systemd-run --user --machine testuser@ --wait --pipe systemd-cat -p emerg echo "User emergency message" | |
75 | systemd-cat -p emerg echo "Root emergency message" | |
76 | journalctl --sync | |
77 | # Set $SYSTEMD_COLORS so systemd-bsod also prints out the QR code | |
78 | SYSTEMD_COLORS=256 /usr/lib/systemd/systemd-bsod & | |
79 | PID=$! | |
80 | vcs_dump_and_check "Root emergency message" | |
81 | grep -aq "Scan the QR code" /tmp/console.dump | |
82 | # TODO: check if systemd-bsod exits on a key press (didn't figure this one out yet) | |
83 | kill $PID | |
84 | timeout 10 bash -c "while kill -0 $PID; do sleep .5; done" | |
85 | ||
86 | # Wipe the journal | |
87 | journalctl --vacuum-size=1 --rotate | |
88 | (! journalctl -q -b -p emerg --grep .) | |
89 | ||
90 | # Check the systemd-bsod.service as well | |
91 | # Note: the systemd-bsod.service unit has ConditionVirtualization=no, so let's | |
92 | # temporarily override it just for the test | |
93 | mkdir /run/systemd/system/systemd-bsod.service.d | |
94 | printf '[Unit]\nConditionVirtualization=\n' >/run/systemd/system/systemd-bsod.service.d/99-override.conf | |
95 | systemctl daemon-reload | |
96 | systemctl start systemd-bsod | |
97 | systemd-cat -p emerg echo "Service emergency message" | |
98 | vcs_dump_and_check "Service emergency message" | |
7ec22d7d FS |
99 | systemctl stop systemd-bsod |
100 | ||
101 | # Wipe the journal | |
102 | journalctl --vacuum-size=1 --rotate | |
103 | (! journalctl -q -b -p emerg --grep .) | |
104 | ||
105 | # Same as above, but make sure the service responds to signals even when there are | |
106 | # no "emerg" messages, see systemd/systemd#30084 | |
107 | (! systemctl is-active systemd-bsod) | |
108 | systemctl start systemd-bsod | |
109 | timeout 5s bash -xec 'until systemctl is-active systemd-bsod; do sleep .5; done' | |
110 | timeout 5s systemctl stop systemd-bsod | |
111 | timeout 5s bash -xec 'while systemctl is-active systemd-bsod; do sleep .5; done' |