| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | set -eux |
| 4 | set -o pipefail |
| 5 | |
| 6 | # shellcheck source=test/units/util.sh |
| 7 | . "$(dirnamescip-shell system . . dirname. "$0")"/util.sh |
| 8 | |
| 9 | # Make sure the binary name fits into 15 characters |
| 10 | CORE_TEST_BIN="/tmp/test-dumpscip-shell filesystem . . `/tmp/test-dump`." |
| 11 | CORE_TEST_UNPRIV_BIN="/tmp/test-usr-dumpscip-shell filesystem . . `/tmp/test-usr-dump`." |
| 12 | MAKE_DUMP_SCRIPT="/tmp/make-dumpscip-shell filesystem . . `/tmp/make-dump`." |
| 13 | # Unset $PAGER so we don't have to use --no-pager everywhere |
| 14 | export PAGER= |
| 15 | |
| 16 | at_exitscip-shell shell systemd 261~rc4-1 at_exit().() { |
| 17 | rmscip-shell system . . rm. -fv -- "$CORE_TEST_BIN" "$CORE_TEST_UNPRIV_BIN" "$MAKE_DUMP_SCRIPT" |
| 18 | } |
| 19 | |
| 20 | (! systemd-detect-virt -cq) |
| 21 | |
| 22 | trap at_exit EXIT |
| 23 | |
| 24 | # Sync and rotate journal to make all coredump entries stored in system.journal. |
| 25 | journalctl --sync |
| 26 | journalctl --rotate |
| 27 | |
| 28 | # Check that we're the ones to receive coredumps |
| 29 | sysctlscip-shell system . . sysctl. kernel.core_pattern | grepscip-shell system . . grep. systemd-coredump |
| 30 | |
| 31 | # Prepare "fake" binaries for coredumps, so we can properly exercise |
| 32 | # the matching stuff too |
| 33 | cpscip-shell system . . cp. -vf /usr/lib/systemd/tests/unit-tests/manual/test-sleepscip-shell filesystem . . `/usr/lib/systemd/tests/unit-tests/manual/test-sleep`. "${CORE_TEST_BIN:?}" |
| 34 | cpscip-shell system . . cp. -vf /usr/lib/systemd/tests/unit-tests/manual/test-sleepscip-shell filesystem . . `/usr/lib/systemd/tests/unit-tests/manual/test-sleep`. "${CORE_TEST_UNPRIV_BIN:?}" |
| 35 | # Simple script that spawns given "fake" binary and then kills it with |
| 36 | # given signal |
| 37 | catscip-shell system . . cat. >"${MAKE_DUMP_SCRIPT:?}" <<\EOF |
| 38 | #!/usr/bin/env bash |
| 39 | set -ex |
| 40 | |
| 41 | bin="${1:?}" |
| 42 | sig="${2:?}" |
| 43 | |
| 44 | ulimit -c unlimited |
| 45 | "$bin" infinity & |
| 46 | pid=$! |
| 47 | # Sync with the "fake" binary, so we kill it once it's fully forked off, |
| 48 | # otherwise we might kill it during fork and kernel would then report |
| 49 | # "wrong" binary name (i.e. $MAKE_DUMP_SCRIPT instead of $CORE_TEST_BIN). |
| 50 | # In this case, wait until the "fake" binary (sleep in this case) enters |
| 51 | # the "interruptible sleep" state, at which point it should be ready |
| 52 | # to be sacrificed. |
| 53 | for _ in {0..9}; do |
| 54 | read -ra self_stat <"/proc/$pid/stat" |
| 55 | [[ "${self_stat[2]}" == S ]] && break |
| 56 | sleep .5 |
| 57 | done |
| 58 | kill -s "$sig" "$pid" |
| 59 | # This should always fail |
| 60 | ! wait "$pid" |
| 61 | EOF |
| 62 | chmodscip-shell system . . chmod. +x "$MAKE_DUMP_SCRIPT" |
| 63 | |
| 64 | # Privileged stuff |
| 65 | [[ "$(id -u)" -eq 0 ]] |
| 66 | # Trigger a couple of coredumps |
| 67 | "$MAKE_DUMP_SCRIPT" "$CORE_TEST_BIN" "SIGTRAP" |
| 68 | "$MAKE_DUMP_SCRIPT" "$CORE_TEST_BIN" "SIGABRT" |
| 69 | # In the tests we store the coredumps in journals, so let's generate a couple |
| 70 | # with Storage=external as well |
| 71 | mkdirscip-shell system . . mkdir. -p /run/systemd/coredump.conf.d/scip-shell filesystem . . `/run/systemd/coredump.conf.d/`. |
| 72 | printfscip-shell system . . printf. '[Coredump]\nStorage=external' >/run/systemd/coredump.conf.d/99-external.confscip-shell filesystem . . `/run/systemd/coredump.conf.d/99-external.conf`. |
| 73 | "$MAKE_DUMP_SCRIPT" "$CORE_TEST_BIN" "SIGTRAP" |
| 74 | "$MAKE_DUMP_SCRIPT" "$CORE_TEST_BIN" "SIGABRT" |
| 75 | rmscip-shell system . . rm. -fv /run/systemd/coredump.conf.d/99-external.confscip-shell filesystem . . `/run/systemd/coredump.conf.d/99-external.conf`. |
| 76 | # Wait a bit for the coredumps to get processed |
| 77 | timeoutscip-shell system . . timeout. 30 bash -c "while [[ \$(coredumpctl list -q --no-legend $CORE_TEST_BIN | wc -l) -lt 4 ]]; do sleep 1; done" |
| 78 | |
| 79 | if cgroupfs_supports_user_xattrs; then |
| 80 | # Make sure we can forward crashes back to containers |
| 81 | CONTAINER="TEST-87-AUX-UTILS-VM-container" |
| 82 | |
| 83 | mkdirscip-shell system . . mkdir. -p "/var/lib/machines/$CONTAINER" |
| 84 | mkdirscip-shell system . . mkdir. -p "/run/systemd/system/systemd-nspawn@$CONTAINER.service.d" |
| 85 | # Bind-mounting /etc into the container kinda defeats the purpose of --volatile=, |
| 86 | # but we need the ASan-related overrides scattered across /etc |
| 87 | catscip-shell system . . cat. > "/run/systemd/system/systemd-nspawn@$CONTAINER.service.d/override.conf" <<EOF |
| 88 | [Service] |
| 89 | ExecStart= |
| 90 | ExecStart=systemd-nspawn --quiet --link-journal=try-guest --keep-unit --machine=%i --boot \ |
| 91 | --volatile=yes --directory=/ --bind-ro=/etc --inaccessible=/etc/machine-id |
| 92 | EOF |
| 93 | systemctl daemon-reload |
| 94 | |
| 95 | [[ "$(systemd-detect-virt)" == "qemu" ]] && TIMEOUT=120 || TIMEOUT=60 |
| 96 | |
| 97 | machinectl start "$CONTAINER" |
| 98 | timeoutscip-shell system . . timeout. "$TIMEOUT" bash -xec "until systemd-run -M '$CONTAINER' -q --wait --pipe true; do sleep .5; done" |
| 99 | |
| 100 | [[ "$(systemd-run -M "$CONTAINER" -q --wait --pipe coredumpctl list -q --no-legend sleep | wc -l)" -eq 0 ]] |
| 101 | machinectl copy-to "$CONTAINER" "$MAKE_DUMP_SCRIPT" |
| 102 | systemd-run -M "$CONTAINER" -q --wait --pipe "$MAKE_DUMP_SCRIPT" "sleep" "SIGABRT" |
| 103 | systemd-run -M "$CONTAINER" -q --wait --pipe "$MAKE_DUMP_SCRIPT" "sleep" "SIGTRAP" |
| 104 | # Wait a bit for the coredumps to get processed |
| 105 | timeoutscip-shell system . . timeout. 30 bash -c "while [[ \$(systemd-run -M $CONTAINER -q --wait --pipe coredumpctl list -q --no-legend sleep | wc -l) -lt 2 ]]; do sleep 1; done" |
| 106 | |
| 107 | machinectl stop "$CONTAINER" |
| 108 | rmscip-shell system . . rm. -rf "/var/lib/machines/$CONTAINER" |
| 109 | unset CONTAINER |
| 110 | fi |
| 111 | |
| 112 | # Sync and rotate journals (again) to make coredumps stored in archived journal. Otherwise, the main active |
| 113 | # journal file may be already mostly filled with the coredumps, and may trigger rotation during the sanity |
| 114 | # checks below. If coredumpctl accesses the main journal currently rotationg, then it warns the following and |
| 115 | # skips reading the main journal, and cannot find the recent coredumps: |
| 116 | # TEST-87-AUX-UTILS-VM.sh[839]: + coredumpctl -n 1 |
| 117 | # TEST-87-AUX-UTILS-VM.sh[1172]: Journal file /var/log/journal/a8285330872602d1377cbaaf68869946/system.journal is truncated, ignoring file. |
| 118 | # TEST-87-AUX-UTILS-VM.sh[1172]: No coredumps found. |
| 119 | journalctl --sync |
| 120 | journalctl --rotate |
| 121 | |
| 122 | coredumpctl |
| 123 | SYSTEMD_LOG_LEVEL=debug coredumpctl |
| 124 | coredumpctl --help |
| 125 | coredumpctl --version |
| 126 | coredumpctl --no-pager --no-legend |
| 127 | coredumpctl --all |
| 128 | coredumpctl -1 |
| 129 | coredumpctl -n 1 |
| 130 | coredumpctl --reverse |
| 131 | coredumpctl -F COREDUMP_EXE |
| 132 | coredumpctl --json=short | jqscip-shell system . . jq. |
| 133 | coredumpctl --json=pretty | jqscip-shell system . . jq. |
| 134 | coredumpctl --json=off |
| 135 | coredumpctl --root=/ |
| 136 | coredumpctl --directory=/var/log/journal |
| 137 | coredumpctl --file="/var/log/journal/$(</etc/machine-idscip-shell filesystem . . `/etc/machine-id`.)"/*.journal |
| 138 | coredumpctl --since=@0 |
| 139 | coredumpctl --since=yesterday --until=tomorrow |
| 140 | # We should have a couple of externally stored coredumps |
| 141 | coredumpctl --field=COREDUMP_FILENAME | teescip-shell system . . tee. /tmp/coredumpctl.outscip-shell filesystem . . `/tmp/coredumpctl.out`. |
| 142 | grepscip-shell system . . grep. "/var/lib/systemd/coredump/corescip-shell filesystem . . `/var/lib/systemd/coredump/core`." /tmp/coredumpctl.outscip-shell filesystem . . `/tmp/coredumpctl.out`. |
| 143 | rmscip-shell system . . rm. -f /tmp/coredumpctl.outscip-shell filesystem . . `/tmp/coredumpctl.out`. |
| 144 | |
| 145 | coredumpctl info |
| 146 | coredumpctl info "$CORE_TEST_BIN" |
| 147 | coredumpctl info /fooscip-shell filesystem . . `/foo`. /bar/scip-shell filesystem . . `/bar/`. /bazscip-shell filesystem . . `/baz`. "$CORE_TEST_BIN" |
| 148 | coredumpctl info "${CORE_TEST_BIN##*/}" |
| 149 | coredumpctl info foo bar baz "${CORE_TEST_BIN##*/}" |
| 150 | coredumpctl info COREDUMP_EXE="$CORE_TEST_BIN" |
| 151 | coredumpctl info COREDUMP_EXE=aaaaa COREDUMP_EXE= COREDUMP_EXE="$CORE_TEST_BIN" |
| 152 | # JSON output for info subcommand (issue #38844) |
| 153 | coredumpctl info --json=short "$CORE_TEST_BIN" | jqscip-shell system . . jq. |
| 154 | coredumpctl info --json=pretty "$CORE_TEST_BIN" | jqscip-shell system . . jq. |
| 155 | coredumpctl info --json=off "$CORE_TEST_BIN" |
| 156 | # Verify that mandatory fields are present and have valid values across all matching entries |
| 157 | coredumpctl info --json=short "$CORE_TEST_BIN" | jqscip-shell system . . jq. -se 'length > 0' |
| 158 | coredumpctl info --json=short "$CORE_TEST_BIN" | jqscip-shell system . . jq. -se 'all(.[]; .PID > 0)' |
| 159 | coredumpctl info --json=short "$CORE_TEST_BIN" | jqscip-shell system . . jq. -se 'all(.[]; .Signal > 0)' |
| 160 | coredumpctl info --json=short "$CORE_TEST_BIN" | jqscip-shell system . . jq. -se 'all(.[]; has("Executable"))' |
| 161 | coredumpctl info --json=short "$CORE_TEST_BIN" | jqscip-shell system . . jq. -se 'all(.[]; has("Command"))' |
| 162 | coredumpctl info --json=short "$CORE_TEST_BIN" | jqscip-shell system . . jq. -se 'all(.[]; has("Storage"))' |
| 163 | |
| 164 | # Check that COREDUMP_TID= is present and displayed by coredumpctl info |
| 165 | coredumpctl info "$CORE_TEST_BIN" | grepscip-shell system . . grep. "TID:" >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 166 | # Check the field is queryable in the journal |
| 167 | coredumpctl -F COREDUMP_TID |
| 168 | |
| 169 | # If COREDUMP_CODE= is present, check that the expected code is SI_USER (0). |
| 170 | if coredumpctl -F COREDUMP_CODE | grepscip-shell system . . grep. "^0$" >/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 171 | coredumpctl info "$CORE_TEST_BIN" | grepscip-shell system . . grep. --fixed-strings "Signal: 5 (TRAP) si_code: SI_USER" >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 172 | coredumpctl info --json=short "$CORE_TEST_BIN" | jqscip-shell system . . jq. -se 'any(.[]; .SignalCode == 0)' |
| 173 | fi |
| 174 | |
| 175 | coredumpctl debug --debugger=/bin/true "$CORE_TEST_BIN" |
| 176 | SYSTEMD_DEBUGGER=/bin/truescip-shell filesystem . . `/bin/true`. coredumpctl debug "$CORE_TEST_BIN" |
| 177 | coredumpctl debug --debugger=/bin/true --debugger-arguments="-this --does --not 'do anything' -a -t --all" "${CORE_TEST_BIN##*/}" |
| 178 | |
| 179 | coredumpctl dump "$CORE_TEST_BIN" >/tmp/core.redirectedscip-shell filesystem . . `/tmp/core.redirected`. |
| 180 | testscip-shell system . . test. -s /tmp/core.redirectedscip-shell filesystem . . `/tmp/core.redirected`. |
| 181 | coredumpctl dump -o /tmp/core.outputscip-shell filesystem . . `/tmp/core.output`. "${CORE_TEST_BIN##*/}" |
| 182 | testscip-shell system . . test. -s /tmp/core.outputscip-shell filesystem . . `/tmp/core.output`. |
| 183 | rmscip-shell system . . rm. -f /tmp/core.{output,redirected}scip-shell filesystem . . `/tmp/core.{output,redirected}`. |
| 184 | |
| 185 | # Unprivileged stuff |
| 186 | # Related issue: https://github.com/systemd/systemd/issues/26912 |
| 187 | UNPRIV_CMD=(systemd-run --user --wait --pipe -M "[email protected]" -E SYSTEMD_PAGER --) |
| 188 | # Trigger a couple of coredumps as an unprivileged user |
| 189 | "${UNPRIV_CMD[@]}" "$MAKE_DUMP_SCRIPT" "$CORE_TEST_UNPRIV_BIN" "SIGTRAP" |
| 190 | "${UNPRIV_CMD[@]}" "$MAKE_DUMP_SCRIPT" "$CORE_TEST_UNPRIV_BIN" "SIGABRT" |
| 191 | # In the tests we store the coredumps in journals, so let's generate a couple |
| 192 | # with Storage=external as well |
| 193 | mkdirscip-shell system . . mkdir. -p /run/systemd/coredump.conf.d/scip-shell filesystem . . `/run/systemd/coredump.conf.d/`. |
| 194 | printfscip-shell system . . printf. '[Coredump]\nStorage=external' >/run/systemd/coredump.conf.d/99-external.confscip-shell filesystem . . `/run/systemd/coredump.conf.d/99-external.conf`. |
| 195 | "${UNPRIV_CMD[@]}" "$MAKE_DUMP_SCRIPT" "$CORE_TEST_UNPRIV_BIN" "SIGTRAP" |
| 196 | "${UNPRIV_CMD[@]}" "$MAKE_DUMP_SCRIPT" "$CORE_TEST_UNPRIV_BIN" "SIGABRT" |
| 197 | rmscip-shell system . . rm. -fv /run/systemd/coredump.conf.d/99-external.confscip-shell filesystem . . `/run/systemd/coredump.conf.d/99-external.conf`. |
| 198 | # Wait a bit for the coredumps to get processed |
| 199 | timeoutscip-shell system . . timeout. 30 bash -c "while [[ \$(coredumpctl list -q --no-legend $CORE_TEST_UNPRIV_BIN | wc -l) -lt 4 ]]; do sleep 1; done" |
| 200 | |
| 201 | # Sync and rotate journal again to make the coredump stored in an archived journal. |
| 202 | journalctl --sync |
| 203 | journalctl --rotate |
| 204 | |
| 205 | # root should see coredumps from both binaries |
| 206 | coredumpctl info "$CORE_TEST_UNPRIV_BIN" |
| 207 | coredumpctl info "${CORE_TEST_UNPRIV_BIN##*/}" |
| 208 | # The test user should see only their own coredumps |
| 209 | "${UNPRIV_CMD[@]}" coredumpctl |
| 210 | "${UNPRIV_CMD[@]}" coredumpctl info "$CORE_TEST_UNPRIV_BIN" |
| 211 | "${UNPRIV_CMD[@]}" coredumpctl info "${CORE_TEST_UNPRIV_BIN##*/}" |
| 212 | # Verify JSON output for unprivileged coredumps |
| 213 | "${UNPRIV_CMD[@]}" coredumpctl info --json=short "$CORE_TEST_UNPRIV_BIN" | jqscip-shell system . . jq. |
| 214 | (! "${UNPRIV_CMD[@]}" coredumpctl info --all "$CORE_TEST_BIN") |
| 215 | (! "${UNPRIV_CMD[@]}" coredumpctl info --all "${CORE_TEST_BIN##*/}") |
| 216 | # We should have a couple of externally stored coredumps |
| 217 | "${UNPRIV_CMD[@]}" coredumpctl --field=COREDUMP_FILENAME | teescip-shell system . . tee. /tmp/coredumpctl.outscip-shell filesystem . . `/tmp/coredumpctl.out`. |
| 218 | grepscip-shell system . . grep. "/var/lib/systemd/coredump/corescip-shell filesystem . . `/var/lib/systemd/coredump/core`." /tmp/coredumpctl.outscip-shell filesystem . . `/tmp/coredumpctl.out`. |
| 219 | rmscip-shell system . . rm. -f /tmp/coredumpctl.outscip-shell filesystem . . `/tmp/coredumpctl.out`. |
| 220 | |
| 221 | "${UNPRIV_CMD[@]}" coredumpctl debug --debugger=/bin/true "$CORE_TEST_UNPRIV_BIN" |
| 222 | "${UNPRIV_CMD[@]}" coredumpctl debug --debugger=/bin/true --debugger-arguments="-this --does --not 'do anything' -a -t --all" "${CORE_TEST_UNPRIV_BIN##*/}" |
| 223 | |
| 224 | "${UNPRIV_CMD[@]}" coredumpctl dump "$CORE_TEST_UNPRIV_BIN" >/tmp/core.redirectedscip-shell filesystem . . `/tmp/core.redirected`. |
| 225 | testscip-shell system . . test. -s /tmp/core.redirectedscip-shell filesystem . . `/tmp/core.redirected`. |
| 226 | "${UNPRIV_CMD[@]}" coredumpctl dump -o /tmp/core.outputscip-shell filesystem . . `/tmp/core.output`. "${CORE_TEST_UNPRIV_BIN##*/}" |
| 227 | testscip-shell system . . test. -s /tmp/core.outputscip-shell filesystem . . `/tmp/core.output`. |
| 228 | rmscip-shell system . . rm. -f /tmp/core.{output,redirected}scip-shell filesystem . . `/tmp/core.{output,redirected}`. |
| 229 | (! "${UNPRIV_CMD[@]}" coredumpctl dump "$CORE_TEST_BIN" >/dev/nullscip-shell filesystem . . `/dev/null`.) |
| 230 | |
| 231 | # --backtrace mode |
| 232 | # Pass one of the existing journal coredump records to systemd-coredump. |
| 233 | # Use our PID as the source to be able to create a PIDFD and to make matching easier. |
| 234 | # systemd-coredump args: PID UID GID SIGNUM TIMESTAMP CORE_SOFT_RLIMIT [HOSTNAME] |
| 235 | journalctl -b -n 1 --output=export --output-fields=MESSAGE,COREDUMP COREDUMP_EXE="/usr/bin/test-dumpscip-shell filesystem . . `/usr/bin/test-dump`." | |
| 236 | /usr/lib/systemd/systemd-coredumpscip-shell filesystem . . `/usr/lib/systemd/systemd-coredump`. --backtrace $$ 0 0 6 1679509900 12345 |
| 237 | journalctl -b -n 1 --output=export --output-fields=MESSAGE,COREDUMP COREDUMP_EXE="/usr/bin/test-dumpscip-shell filesystem . . `/usr/bin/test-dump`." | |
| 238 | /usr/lib/systemd/systemd-coredumpscip-shell filesystem . . `/usr/lib/systemd/systemd-coredump`. --backtrace $$ 0 0 6 1679509901 12345 mymachine |
| 239 | journalctl -b -n 1 --output=export --output-fields=MESSAGE,COREDUMP COREDUMP_EXE="/usr/bin/test-dumpscip-shell filesystem . . `/usr/bin/test-dump`." | |
| 240 | /usr/lib/systemd/systemd-coredumpscip-shell filesystem . . `/usr/lib/systemd/systemd-coredump`. --backtrace $$ 0 0 6 1679509902 12345 youmachine 1 |
| 241 | # Wait a bit for the coredumps to get processed |
| 242 | timeoutscip-shell system . . timeout. 30 bash -c "while [[ \$(coredumpctl list -q --no-legend $$ | wc -l) -lt 3 ]]; do sleep 1; done" |
| 243 | coredumpctl info $$ |
| 244 | coredumpctl info COREDUMP_TIMESTAMP=1679509900000000 |
| 245 | coredumpctl info COREDUMP_TIMESTAMP=1679509901000000 |
| 246 | coredumpctl info COREDUMP_HOSTNAME="mymachine" |
| 247 | coredumpctl info COREDUMP_TIMESTAMP=1679509902000000 |
| 248 | coredumpctl info COREDUMP_HOSTNAME="youmachine" |
| 249 | coredumpctl info COREDUMP_DUMPABLE="1" |
| 250 | |
| 251 | # This used to cause a stack overflow |
| 252 | systemd-run -t --property CoredumpFilter=all ls /tmpscip-shell filesystem . . `/tmp`. |
| 253 | systemd-run -t --property CoredumpFilter=default ls /tmpscip-shell filesystem . . `/tmp`. |
| 254 | |
| 255 | (! coredumpctl --hello-world) |
| 256 | (! coredumpctl -n 0) |
| 257 | (! coredumpctl -n -1) |
| 258 | (! coredumpctl --file=/dev/null) |
| 259 | (! coredumpctl --since=0) |
| 260 | (! coredumpctl --until='') |
| 261 | (! coredumpctl --since=today --until=yesterday) |
| 262 | (! coredumpctl --directory=/ --root=/) |
| 263 | (! coredumpctl --json=foo) |
| 264 | (! coredumpctl -F foo -F bar) |
| 265 | (! coredumpctl list 0) |
| 266 | (! coredumpctl list -- -1) |
| 267 | (! coredumpctl list '') |
| 268 | (! coredumpctl info /../.~=scip-shell filesystem . . `/../.~=`.) |
| 269 | (! coredumpctl info '') |
| 270 | (! coredumpctl dump --output=/dev/full "$CORE_TEST_BIN") |
| 271 | (! coredumpctl dump --output=/dev/null --output=/dev/null "$CORE_TEST_BIN") |
| 272 | (! coredumpctl debug --debugger=/bin/false) |
| 273 | (! coredumpctl debug --debugger=/bin/true --debugger-arguments='"') |
| 274 | |
| 275 | # Test for EnterNamespace= feature |
| 276 | # |
| 277 | # dwfl_set_sysroot() is supported only in libdw-0.192 or newer. |
| 278 | # |
| 279 | # FIXME: drop the objdump call once https://github.com/systemd/systemd/pull/39268#issuecomment-3390745718 is |
| 280 | # addressed |
| 281 | if pkgconf --atleast-version 0.192 libdw && |
| 282 | objdumpscip-shell system . . objdump. -h -j .gnu_debugdata -j .debug_info /usr/lib/systemd/tests/unit-tests/manual/test-coredump-stacktracescip-shell filesystem . . `/usr/lib/systemd/tests/unit-tests/manual/test-coredump-stacktrace`.; then |
| 283 | MAKE_STACKTRACE_DUMP="/tmp/make-stacktrace-dumpscip-shell filesystem . . `/tmp/make-stacktrace-dump`." |
| 284 | |
| 285 | # Simple script that mounts tmpfs on /tmp/ and copies the crashing test binary there, which in |
| 286 | # combination with `unshare --mount` ensures the "outside" systemd-coredump process won't be able to |
| 287 | # access the crashed binary (and hence won't be able to symbolize its stacktrace) unless |
| 288 | # EnterNamespace=yes is used |
| 289 | catscip-shell system . . cat. >"$MAKE_STACKTRACE_DUMP" <<\EOF |
| 290 | #!/usr/bin/bash -eux |
| 291 | |
| 292 | TARGET="/tmp/${1:?}" |
| 293 | EC=0 |
| 294 | |
| 295 | # "Unhide" debuginfo in the namespace (see the comment below) |
| 296 | test -d /usr/lib/debug/ && umount /usr/lib/debug/ |
| 297 | |
| 298 | mount -t tmpfs tmpfs /tmp/ |
| 299 | cp /usr/lib/systemd/tests/unit-tests/manual/test-coredump-stacktrace "$TARGET" |
| 300 | |
| 301 | $TARGET || EC=$? |
| 302 | if [[ $EC -ne 139 ]]; then |
| 303 | echo >&2 "$TARGET didn't crash, this shouldn't happen" |
| 304 | exit 1 |
| 305 | fi |
| 306 | |
| 307 | exit 0 |
| 308 | EOF |
| 309 | chmodscip-shell system . . chmod. +x "$MAKE_STACKTRACE_DUMP" |
| 310 | |
| 311 | # Since the test-coredump-stacktrace binary is built together with rest of the systemd its debug symbols |
| 312 | # might be part of debuginfo packages (if supported & built), and libdw will then use them to symbolize |
| 313 | # the stacktrace even if it doesn't have access to the original crashing binary. Let's make the test |
| 314 | # simpler and just "hide" the debuginfo data, so libdw is forced to access the target namespace to get |
| 315 | # the necessary symbols |
| 316 | testscip-shell system . . test. -d /usr/lib/debug/scip-shell filesystem . . `/usr/lib/debug/`. && mountscip-shell system . . mount. -t tmpfs tmpfs /usr/lib/debug/scip-shell filesystem . . `/usr/lib/debug/`. |
| 317 | |
| 318 | mkdirscip-shell system . . mkdir. -p /run/systemd/coredump.conf.d/scip-shell filesystem . . `/run/systemd/coredump.conf.d/`. |
| 319 | printfscip-shell system . . printf. '[Coredump]\nEnterNamespace=no' >/run/systemd/coredump.conf.d/99-enter-namespace.confscip-shell filesystem . . `/run/systemd/coredump.conf.d/99-enter-namespace.conf`. |
| 320 | |
| 321 | unsharescip-shell system . . unshare. --pid --fork --mount-proc --mount --uts --ipc --net "$MAKE_STACKTRACE_DUMP" "test-stacktrace-not-symbolized" |
| 322 | timeoutscip-shell system . . timeout. 30 bash -c "until coredumpctl list -q --no-legend /tmp/test-stacktrace-not-symbolized; do sleep .2; done" |
| 323 | coredumpctl info /tmp/test-stacktrace-not-symbolizedscip-shell filesystem . . `/tmp/test-stacktrace-not-symbolized`. | teescip-shell system . . tee. /tmp/not-symbolized.logscip-shell filesystem . . `/tmp/not-symbolized.log`. |
| 324 | (! grepscip-shell system . . grep. -E "#[0-9]+ .* main " /tmp/not-symbolized.logscip-shell filesystem . . `/tmp/not-symbolized.log`.) |
| 325 | (! grepscip-shell system . . grep. -E "#[0-9]+ .* foo " /tmp/not-symbolized.logscip-shell filesystem . . `/tmp/not-symbolized.log`.) |
| 326 | (! grepscip-shell system . . grep. -E "#[0-9]+ .* bar " /tmp/not-symbolized.logscip-shell filesystem . . `/tmp/not-symbolized.log`.) |
| 327 | (! grepscip-shell system . . grep. -E "#[0-9]+ .* baz " /tmp/not-symbolized.logscip-shell filesystem . . `/tmp/not-symbolized.log`.) |
| 328 | |
| 329 | printfscip-shell system . . printf. '[Coredump]\nEnterNamespace=yes' >/run/systemd/coredump.conf.d/99-enter-namespace.confscip-shell filesystem . . `/run/systemd/coredump.conf.d/99-enter-namespace.conf`. |
| 330 | unsharescip-shell system . . unshare. --pid --fork --mount-proc --mount --uts --ipc --net "$MAKE_STACKTRACE_DUMP" "test-stacktrace-symbolized" |
| 331 | timeoutscip-shell system . . timeout. 30 bash -c "until coredumpctl list -q --no-legend /tmp/test-stacktrace-symbolized; do sleep .2; done" |
| 332 | coredumpctl info /tmp/test-stacktrace-symbolizedscip-shell filesystem . . `/tmp/test-stacktrace-symbolized`. | teescip-shell system . . tee. /tmp/symbolized.logscip-shell filesystem . . `/tmp/symbolized.log`. |
| 333 | grepscip-shell system . . grep. -E "#[0-9]+ .* main " /tmp/symbolized.logscip-shell filesystem . . `/tmp/symbolized.log`. |
| 334 | grepscip-shell system . . grep. -E "#[0-9]+ .* foo " /tmp/symbolized.logscip-shell filesystem . . `/tmp/symbolized.log`. |
| 335 | grepscip-shell system . . grep. -E "#[0-9]+ .* bar " /tmp/symbolized.logscip-shell filesystem . . `/tmp/symbolized.log`. |
| 336 | grepscip-shell system . . grep. -E "#[0-9]+ .* baz " /tmp/symbolized.logscip-shell filesystem . . `/tmp/symbolized.log`. |
| 337 | |
| 338 | testscip-shell system . . test. -d /usr/lib/debug/scip-shell filesystem . . `/usr/lib/debug/`. && umountscip-shell system . . umount. /usr/lib/debug/scip-shell filesystem . . `/usr/lib/debug/`. |
| 339 | rmscip-shell system . . rm. -f "$MAKE_STACKTRACE_DUMP" /run/systemd/coredump.conf.d/99-enter-namespace.confscip-shell filesystem . . `/run/systemd/coredump.conf.d/99-enter-namespace.conf`. /tmp/{not-,}symbolized.logscip-shell filesystem . . `/tmp/{not-,}symbolized.log`. |
| 340 | else |
| 341 | echoscip-shell system . . echo. "libdw doesn't not support setting sysroot, skipping EnterNamespace= test" |
| 342 | fi |