1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -ex
4
5test_rule="/run/udev/rules.d/49-test.rules"
6KILL_PID=
7
8setup() {
9    mkdir -p "${test_rule%/*}"
10    cp -f /etc/udev/udev.conf /etc/udev/udev.conf.bckp
11    cat >"${test_rule}" <<EOF
12ACTION=="add", SUBSYSTEM=="mem", KERNEL=="null", OPTIONS="log_level=debug"
13ACTION=="add", SUBSYSTEM=="mem", KERNEL=="null", PROGRAM=="/bin/sleep 60"
14EOF
15    cat >>/etc/udev/udev.conf <<EOF
16event_timeout=10
17timeout_signal=SIGABRT
18EOF
19
20    systemctl restart systemd-udevd.service
21}
22
23teardown() {
24    set +e
25
26    if [[ -n "$KILL_PID" ]]; then
27        kill "$KILL_PID"
28    fi
29
30    rm -rf "$TMPDIR"
31
32    mv -f /etc/udev/udev.conf.bckp /etc/udev/udev.conf
33    rm -f "$test_rule"
34    systemctl restart systemd-udevd.service
35}
36
37run_test() {
38    local since
39
40    since="$(date '+%F %T')"
41
42    TMPDIR=$(mktemp -d -p /tmp udev-tests.XXXXXX)
43    udevadm monitor --udev --property --subsystem-match=mem >"$TMPDIR"/monitor.txt &
44    KILL_PID="$!"
45
46    SYSTEMD_LOG_LEVEL=debug udevadm trigger --verbose --action add /dev/null
47
48    for _ in {1..40}; do
49        if coredumpctl --since "$since" --no-legend --no-pager | grep /bin/udevadm ; then
50            kill "$KILL_PID"
51            KILL_PID=
52
53            cat "$TMPDIR"/monitor.txt
54            grep -q 'UDEV_WORKER_FAILED=1' "$TMPDIR"/monitor.txt
55            grep -q 'UDEV_WORKER_SIGNAL=6' "$TMPDIR"/monitor.txt
56            grep -q 'UDEV_WORKER_SIGNAL_NAME=ABRT' "$TMPDIR"/monitor.txt
57            return 0
58        fi
59        sleep .5
60    done
61
62    return 1
63}
64
65trap teardown EXIT
66
67setup
68run_test
69
70exit 0
71