HALF ECHO LEARN / REFERENCE COLLECTION
v262.2test/test-udev.py
Git blob: c2ee8b1bc1440cefc7a546c7393d72ec854c7e6d
Lines 2001–2471 of 2,471
'TEST relative path',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['relative'],
),
rules=r'''
KERNEL=="sda", TEST=="size", SYMLINK+="relative"
''',
),
Rules.new(
'TEST wildcard substitution (find queue/nr_requests)',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['found-subdir'],
),
rules=r'''
KERNEL=="sda", TEST=="*/nr_requests", SYMLINK+="found-subdir"
''',
),
Rules.new(
'TEST MODE=0000',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_perms='0:0:0000',
),
rules=r'''
KERNEL=="sda", MODE="0000"
''',
),
Rules.new(
'TEST PROGRAM feeds OWNER, GROUP, MODE',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_perms='1:1:0400',
),
rules=r'''
KERNEL=="sda", MODE="666"
KERNEL=="sda", PROGRAM=="/bin/echo 1 1 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
''',
),
Rules.new(
'TEST PROGRAM feeds MODE with overflow',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_perms='0:0:0440',
),
rules=r'''
KERNEL=="sda", MODE="440"
KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
''',
),
Rules.new(
'magic [subsys/sysname] attribute substitution',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['sda-8741C4G-end'],
exp_perms='0:0:0600',
),
rules=r'''
KERNEL=="sda", SYMLINK+="%k-%s{[dmi/id]product_name}-end"
''',
),
Rules.new(
'builtin path_id',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0'],
),
rules=r'''
KERNEL=="sda", IMPORT{builtin}="path_id"
KERNEL=="sda", ENV{ID_PATH}=="?*", SYMLINK+="disk/by-path/$env{ID_PATH}"
''',
),
Rules.new(
'builtin path_id for PNP device with ACPI firmware node',
Device(
'/devices/pnp0/00:07/rtc/rtc0',
exp_links=['rtc/by-path/acpi-PNP0B00:00'],
),
rules=r'''
KERNEL=="rtc0", IMPORT{builtin}="path_id"
KERNEL=="rtc0", ENV{ID_PATH}=="?*", SYMLINK+="rtc/by-path/$env{ID_PATH}"
''',
),
Rules.new(
'add and match tag',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['found'],
not_exp_links=['bad'],
),
rules=r'''
SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", TAG+="green"
TAGS=="green", SYMLINK+="found"
TAGS=="blue", SYMLINK+="bad"
''',
),
Rules.new(
"don't crash with lots of tags",
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['found'],
),
rules=f'''
{rules_10k_tags}
TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="found"
''',
),
Rules.new(
'continuations',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['found'],
not_exp_links=['bad'],
),
rules=f'''
{rules_10k_tags_continuation}
TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="bad"
KERNEL=="sda",\\
# comment in continuation
TAG+="hoge1",\\
# space before comment
TAG+="hoge2",\\
# spaces before and after token are dropped
TAG+="hoge3", \\
\\
\\
TAG+="hoge4"
TAGS=="hoge1", TAGS=="hoge2", TAGS=="hoge3", TAGS=="hoge4", SYMLINK+="found"
''',
),
Rules.new(
'continuations with empty line',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['found'],
not_exp_links=['bad'],
),
rules=r'''
# empty line finishes continuation
KERNEL=="sda", TAG+="foo" \
KERNEL=="sdb", TAG+="hoge"
KERNEL=="sda", TAG+="aaa" \
KERNEL=="sdb", TAG+="bbb"
TAGS=="foo", SYMLINK+="found"
TAGS=="aaa", SYMLINK+="bad"
''',
),
Rules.new(
'continuations with space only line',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
exp_links=['found'],
not_exp_links=['bad'],
),
rules='''
# space only line finishes continuation
KERNEL=="sda", TAG+="foo" \\
\t
KERNEL=="sdb", TAG+="hoge"
KERNEL=="sda", TAG+="aaa" \\
KERNEL=="sdb", TAG+="bbb"
TAGS=="foo", SYMLINK+="found"
TAGS=="aaa", SYMLINK+="bad"
''',
),
Rules.new(
'multiple devices',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
exp_links=['part-1'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5',
exp_links=['part-5'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6',
exp_links=['part-6'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7',
exp_links=['part-7'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8',
exp_links=['part-8'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9',
exp_links=['part-9'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10',
exp_links=['part-10'],
),
rules=r'''
SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
''',
),
Rules.new(
'multiple devices, same link name, positive prio',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
exp_links=['part-1'],
not_exp_links=['partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5',
exp_links=['part-5'],
not_exp_links=['partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6',
not_exp_links=['partition'],
exp_links=['part-6'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7',
exp_links=['part-7', 'partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8',
not_exp_links=['partition'],
exp_links=['part-8'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9',
not_exp_links=['partition'],
exp_links=['part-9'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10',
not_exp_links=['partition'],
exp_links=['part-10'],
),
repeat=100,
rules=r'''
SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
KERNEL=="*7", OPTIONS+="link_priority=10"
''',
),
Rules.new(
'multiple devices, same link name, negative prio',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
exp_links=['part-1'],
not_exp_links=['partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5',
exp_links=['part-5'],
not_exp_links=['partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6',
not_exp_links=['partition'],
exp_links=['part-6'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7',
exp_links=['part-7', 'partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8',
not_exp_links=['partition'],
exp_links=['part-8'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9',
not_exp_links=['partition'],
exp_links=['part-9'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10',
not_exp_links=['partition'],
exp_links=['part-10'],
),
rules=r'''
SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
KERNEL!="*7", OPTIONS+="link_priority=-10"
''',
),
Rules.new(
'multiple devices, same link name, positive prio, sleep',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
exp_links=['part-1'],
not_exp_links=['partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5',
exp_links=['part-5'],
not_exp_links=['partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6',
not_exp_links=['partition'],
exp_links=['part-6'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7',
exp_links=['part-7', 'partition'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8',
not_exp_links=['partition'],
exp_links=['part-8'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9',
not_exp_links=['partition'],
exp_links=['part-9'],
),
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10',
not_exp_links=['partition'],
exp_links=['part-10'],
),
delay=10000,
rules=r'''
SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
KERNEL=="*7", OPTIONS+="link_priority=10"
''',
),
Rules.new(
'all_block_devs',
device_generator=lambda: all_block_devs(
lambda name: (['blockdev'], None) if name.endswith('/sda6') else (None, None)
),
repeat=10,
rules=r'''
SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sd*", SYMLINK+="blockdev"
KERNEL=="sda6", OPTIONS+="link_priority=10"
''',
),
Rules.new(
'case insensitive match',
Device(
'/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
exp_links=['ok'],
),
rules=r'''
KERNEL==i"SDA1", SUBSYSTEMS==i"SCSI", ATTRS{vendor}==i"a?a", SYMLINK+="ok"
''',
),
]
def fork_and_run_udev(action: str, rules: Rules) -> None:
kinder = []
for k, device in enumerate(rules.devices):
# TODO: valgrind/gdb/strace
cmd = [UDEV_BIN, action, device.devpath]
if rules.delay:
cmd += [f'{k * rules.delay}']
kinder += [subprocess.Popen(cmd)]
good = True
for c in kinder:
if not good:
# once something fails, terminate all workers
c.terminate()
elif c.wait() != 0:
good = False
assert good
def environment_issue():
if os.getuid() != 0:
return 'Must be root to run properly'
c = subprocess.run(['systemd-detect-virt', '-r', '-q'], check=False)
if c.returncode == 0:
return 'Running in a chroot, skipping the test'
c = subprocess.run(['systemd-detect-virt', '-c', '-q'], check=False)
if c.returncode == 0:
return 'Running in a container, skipping the test'
return None
@pytest.fixture(scope='module')
def udev_setup():
issue = environment_issue()
if issue:
pytest.skip(issue)
global UDEV_RUN, UDEV_RULES, UDEV_DEV, UDEV_SYS
_tmpdir = tempfile.TemporaryDirectory()
tmpdir = Path(_tmpdir.name)
UDEV_RUN = tmpdir / 'run'
UDEV_RULES = UDEV_RUN / 'udev-test.rules'
udev_tmpfs = tmpdir / 'tmpfs'
UDEV_DEV = udev_tmpfs / 'dev'
UDEV_SYS = udev_tmpfs / 'sys'
subprocess.run(['umount', udev_tmpfs], stderr=subprocess.DEVNULL, check=False)
udev_tmpfs.mkdir(exist_ok=True, parents=True)
subprocess.check_call(
['mount', '-v', '-t', 'tmpfs', '-o', 'rw,mode=0755,nosuid,noexec', 'tmpfs', udev_tmpfs]
)
UDEV_DEV.mkdir(exist_ok=True)
# setting group and mode of udev_dev ensures the tests work
# even if the parent directory has setgid bit enabled.
os.chmod(UDEV_DEV, 0o755)
os.chown(UDEV_DEV, 0, 0)
os.mknod(UDEV_DEV / 'null', 0o600 | stat.S_IFCHR, os.makedev(1, 3))
# check if we are permitted to create block device nodes
sda = UDEV_DEV / 'sda'
os.mknod(sda, 0o600 | stat.S_IFBLK, os.makedev(8, 0))
sda.unlink()
subprocess.check_call([SYS_SCRIPT, UDEV_SYS.parent])
subprocess.check_call(['rm', '-rf', UDEV_RUN])
UDEV_RUN.mkdir(parents=True)
os.chdir(tmpdir)
if subprocess.run([UDEV_BIN, 'check'], check=False).returncode != 0:
pytest.skip(
f'{UDEV_BIN} failed to set up the environment, skipping the test', allow_module_level=True
)
yield
subprocess.check_call(['rm', '-rf', UDEV_RUN])
subprocess.check_call(['umount', '-v', udev_tmpfs])
udev_tmpfs.rmdir()
@pytest.mark.parametrize('rules', RULES, ids=(rule.desc for rule in RULES))
def test_udev(rules: Rules, udev_setup):
assert udev_setup is None
rules.create_rules_file()
rules.generate_devices()
for _ in range(rules.repeat):
fork_and_run_udev('add', rules)
for device in rules.devices:
device.check_add()
fork_and_run_udev('remove', rules)
for device in rules.devices:
device.check_remove()
if __name__ == '__main__':
issue = environment_issue()
if issue:
print(issue, file=sys.stderr)
sys.exit(77)
sys.exit(pytest.main(sys.argv))