#!/bin/sh
# Coldplug like systemd-udev-trigger.service and systemd-udev-settle.service:
# mdevd-coldplug -O 4 returns once mdevd has handled the last event it
# triggered. Then the static nodes that load their module on first open get
# the group and mode of udev's static_node= rules. Skipped with read-only
# /sys (ConditionPathIsReadWrite=/sys).
if awk '$2 == "/sys" { split($4, o, ","); if (o[1] == "ro") f = 1 } END { exit !f }' /proc/self/mounts; then
	mkdir -p /run/svctl/skipped
	printf '%s\n' 'ro /sys' >/run/svctl/skipped/mdevd-coldplug
	echo "mdevd-coldplug: skipped (/sys read-only)"
	exit 0
fi
mdevd-coldplug -O 4 || exit
while read -r node group mode; do
	[ -c "$node" ] || continue
	chgrp "$group" "$node" && chmod "$mode" "$node"
done <<'NODES'
/dev/snd/seq audio 0660
/dev/snd/timer audio 0660
/dev/loop-control disk 0660
/dev/net/tun root 0666
/dev/fuse root 0666
/dev/vfio/vfio root 0666
/dev/kvm kvm 0660
/dev/vhost-net kvm 0660
/dev/vhost-vsock kvm 0660
NODES
exit 0
