2021-03-05 12:36:04 +03:00
#!/usr/bin/env bash
2021-10-17 19:13:06 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2024-01-02 22:23:26 +03:00
set -eux
2021-03-05 12:36:04 +03:00
2024-01-02 22:23:26 +03:00
TMPDIR =
2023-12-08 20:38:41 +03:00
TEST_RULE = "/run/udev/rules.d/49-test.rules"
2024-01-05 18:10:50 +03:00
TEST_CONF = "/run/udev/udev.conf.d/test-17.conf"
2022-03-29 08:58:58 +03:00
KILL_PID =
2021-03-05 12:36:04 +03:00
setup( ) {
2023-12-08 20:38:41 +03:00
mkdir -p " ${ TEST_RULE %/* } "
2024-01-05 18:10:50 +03:00
mkdir -p /run/udev/udev.conf.d
2023-12-08 20:38:41 +03:00
cat >" ${ TEST_RULE } " <<EOF
2024-01-02 22:23:26 +03:00
ACTION!= "add" , GOTO = "test_end"
SUBSYSTEM!= "mem" , GOTO = "test_end"
KERNEL!= "null" , GOTO = "test_end"
OPTIONS = "log_level=debug"
PROGRAM = = "/bin/touch /tmp/test-udev-marker"
PROGRAM!= "/bin/sleep 60" , ENV{ PROGRAM_RESULT} = "KILLED"
LABEL = "test_end"
2021-03-07 08:18:07 +03:00
EOF
2024-01-05 18:10:50 +03:00
cat >" $TEST_CONF " <<EOF
2022-01-19 13:03:52 +03:00
event_timeout = 10
timeout_signal = SIGABRT
EOF
2021-03-05 12:36:04 +03:00
systemctl restart systemd-udevd.service
}
2023-05-16 21:55:45 +03:00
# shellcheck disable=SC2317
2021-03-05 12:36:04 +03:00
teardown( ) {
set +e
2022-03-29 08:58:58 +03:00
if [ [ -n " $KILL_PID " ] ] ; then
kill " $KILL_PID "
fi
rm -rf " $TMPDIR "
2024-01-05 18:10:50 +03:00
rm -f " $TEST_RULE " " $TEST_CONF "
2021-03-05 12:36:04 +03:00
systemctl restart systemd-udevd.service
}
2024-01-02 22:23:26 +03:00
run_test_timeout( ) {
TMPDIR = $( mktemp -d -p /tmp udev-tests.XXXXXX)
udevadm monitor --udev --property --subsystem-match= mem >" $TMPDIR " /monitor.txt &
KILL_PID = " $! "
SYSTEMD_LOG_LEVEL = debug udevadm trigger --verbose --action add /dev/null
for _ in { 1..40} ; do
if grep -q 'PROGRAM_RESULT=KILLED' " $TMPDIR " /monitor.txt; then
sleep .5
kill " $KILL_PID "
KILL_PID =
cat " $TMPDIR " /monitor.txt
( ! grep -q 'UDEV_WORKER_FAILED=1' " $TMPDIR " /monitor.txt)
( ! grep -q 'UDEV_WORKER_SIGNAL=6' " $TMPDIR " /monitor.txt)
( ! grep -q 'UDEV_WORKER_SIGNAL_NAME=ABRT' " $TMPDIR " /monitor.txt)
grep -q 'PROGRAM_RESULT=KILLED' " $TMPDIR " /monitor.txt
rm -rf " $TMPDIR "
return 0
fi
sleep .5
done
return 1
}
2022-03-29 08:58:58 +03:00
2024-01-02 22:23:26 +03:00
run_test_killed( ) {
local killed =
2021-03-05 12:36:04 +03:00
2022-03-29 08:58:58 +03:00
TMPDIR = $( mktemp -d -p /tmp udev-tests.XXXXXX)
udevadm monitor --udev --property --subsystem-match= mem >" $TMPDIR " /monitor.txt &
KILL_PID = " $! "
2021-03-05 12:36:04 +03:00
2024-01-02 22:23:26 +03:00
rm -f /tmp/test-udev-marker
2022-03-29 08:58:58 +03:00
SYSTEMD_LOG_LEVEL = debug udevadm trigger --verbose --action add /dev/null
for _ in { 1..40} ; do
2024-01-02 22:23:26 +03:00
if [ [ -z " $killed " ] ] ; then
if [ [ -e /tmp/test-udev-marker ] ] ; then
killall --signal ABRT --regexp udev-worker
killed = 1
fi
elif grep -q 'UDEV_WORKER_FAILED=1' " $TMPDIR " /monitor.txt; then
sleep .5
2022-03-29 08:58:58 +03:00
kill " $KILL_PID "
KILL_PID =
cat " $TMPDIR " /monitor.txt
grep -q 'UDEV_WORKER_FAILED=1' " $TMPDIR " /monitor.txt
grep -q 'UDEV_WORKER_SIGNAL=6' " $TMPDIR " /monitor.txt
grep -q 'UDEV_WORKER_SIGNAL_NAME=ABRT' " $TMPDIR " /monitor.txt
2024-01-02 22:23:26 +03:00
( ! grep -q 'PROGRAM_RESULT=KILLED' " $TMPDIR " /monitor.txt)
rm -rf " $TMPDIR "
2021-03-05 12:36:04 +03:00
return 0
fi
2021-09-16 00:14:18 +03:00
sleep .5
2021-03-05 12:36:04 +03:00
done
return 1
}
trap teardown EXIT
setup
2024-01-02 22:23:26 +03:00
run_test_timeout
run_test_killed
2021-03-05 12:36:04 +03:00
exit 0