1#!/bin/bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -ex
4set -o pipefail
5
6# tests for udev watch
7
8function check_validity() {
9    local f ID_OR_HANDLE
10
11    for f in /run/udev/watch/*; do
12        ID_OR_HANDLE="$(readlink "$f")"
13        test -L "/run/udev/watch/${ID_OR_HANDLE}"
14        test "$(readlink "/run/udev/watch/${ID_OR_HANDLE}")" = "$(basename "$f")"
15    done
16}
17
18function check() {
19    local i j
20
21    for ((i=0;i<2;i++)); do
22        systemctl restart systemd-udevd.service
23        udevadm control --ping
24        udevadm settle
25        check_validity
26
27        for ((j=0;j<2;j++)); do
28            udevadm trigger -w --action add --subsystem-match=block
29            check_validity
30        done
31
32        for ((j=0;j<2;j++)); do
33            udevadm trigger -w --action change --subsystem-match=block
34            check_validity
35        done
36    done
37}
38
39mkdir -p /run/udev/rules.d/
40
41cat >/run/udev/rules.d/00-debug.rules <<EOF
42SUBSYSTEM=="block", KERNEL=="sda*", OPTIONS="log_level=debug"
43EOF
44
45cat >/run/udev/rules.d/50-testsuite.rules <<EOF
46ACTION=="add", SUBSYSTEM=="block", KERNEL=="sda", OPTIONS:="watch"
47EOF
48
49check
50
51MAJOR=$(udevadm info /dev/sda | grep -e '^E: MAJOR=' | sed -e 's/^E: MAJOR=//')
52MINOR=$(udevadm info /dev/sda | grep -e '^E: MINOR=' | sed -e 's/^E: MINOR=//')
53test -L "/run/udev/watch/b${MAJOR}:${MINOR}"
54
55cat >/run/udev/rules.d/50-testsuite.rules <<EOF
56ACTION=="change", SUBSYSTEM=="block", KERNEL=="sda", OPTIONS:="nowatch"
57EOF
58
59check
60
61MAJOR=$(udevadm info /dev/sda | grep -e '^E: MAJOR=' | sed -e 's/^E: MAJOR=//')
62MINOR=$(udevadm info /dev/sda | grep -e '^E: MINOR=' | sed -e 's/^E: MINOR=//')
63test ! -e "/run/udev/watch/b${MAJOR}:${MINOR}"
64
65rm /run/udev/rules.d/00-debug.rules
66rm /run/udev/rules.d/50-testsuite.rules
67
68udevadm control --reload
69
70exit 0
71