Home | History | Annotate | Line # | Download | only in hooks
      1  1.1  thorpej #!/bin/sh -
      2  1.1  thorpej #
      3  1.1  thorpej # $NetBSD: 99-ugen-perms-tigard,v 1.1 2024/03/30 06:42:10 thorpej Exp $
      4  1.1  thorpej #
      5  1.1  thorpej # Look for a Tigard (https://github.com/tigard-tools/tigard) debug
      6  1.1  thorpej # board and change the permissions to 0660.
      7  1.1  thorpej #
      8  1.1  thorpej # Written by Jason R. Thorpe, March 2024.  Public domain.
      9  1.1  thorpej #
     10  1.1  thorpej 
     11  1.1  thorpej export LC_ALL=C
     12  1.1  thorpej 
     13  1.1  thorpej event="$1"
     14  1.1  thorpej shift
     15  1.1  thorpej devices=$@
     16  1.1  thorpej 
     17  1.1  thorpej orig_perms=0600
     18  1.1  thorpej new_perms=0660
     19  1.1  thorpej 
     20  1.1  thorpej orig_group=wheel
     21  1.1  thorpej new_group=wheel
     22  1.1  thorpej 
     23  1.1  thorpej device_name=tigard
     24  1.1  thorpej 
     25  1.1  thorpej is_target_device()
     26  1.1  thorpej {
     27  1.1  thorpej 	local vendor_string
     28  1.1  thorpej 	local product_string
     29  1.1  thorpej 
     30  1.1  thorpej 	vendor_string="$(drvctl -p $1 vendor-string)"
     31  1.1  thorpej 	product_string="$(drvctl -p $1 product-string)"
     32  1.1  thorpej 
     33  1.1  thorpej 	if [ x"$vendor_string" = x"SecuringHardware.com" -a \
     34  1.1  thorpej 	     x"$product_string" = x"Tigard V1.1" ]; then
     35  1.1  thorpej 		echo "yes"
     36  1.1  thorpej 		return
     37  1.1  thorpej 	fi
     38  1.1  thorpej 
     39  1.1  thorpej 	echo "no"
     40  1.1  thorpej }
     41  1.1  thorpej 
     42  1.1  thorpej set_permissions()
     43  1.1  thorpej {
     44  1.1  thorpej 	if [ x$(is_target_device $1) = xyes ]; then
     45  1.1  thorpej 		chgrp $new_group /dev/"${2}".*
     46  1.1  thorpej 		chmod $new_perms /dev/"${2}".*
     47  1.1  thorpej 		#
     48  1.1  thorpej 		# We need to create a symlink here to remember
     49  1.1  thorpej 		# the ugen device node that was used, since we
     50  1.1  thorpej 		# can't recover it from the device name that
     51  1.1  thorpej 		# comes from the kernel later because we get the
     52  1.1  thorpej 		# event *after* the device is gone, and thus
     53  1.1  thorpej 		# cannot query any properties.
     54  1.1  thorpej 		#
     55  1.1  thorpej 		rm -f /dev/${1}-${device_name}
     56  1.1  thorpej 		ln -sf ${2} /dev/${1}-${device_name}
     57  1.1  thorpej 	fi
     58  1.1  thorpej }
     59  1.1  thorpej 
     60  1.1  thorpej restore_permissions()
     61  1.1  thorpej {
     62  1.1  thorpej 	if [ -h "/dev/${1}-${device_name}" ]; then
     63  1.1  thorpej 		devnode=$(readlink "/dev/${1}-${device_name}")
     64  1.1  thorpej 		if [ x"$devnode" != x ]; then
     65  1.1  thorpej 			chmod $orig_perms /dev/"${devnode}".*
     66  1.1  thorpej 			chgrp $orig_group /dev/"${devnode}".*
     67  1.1  thorpej 		fi
     68  1.1  thorpej 		rm -f "/dev/${1}-${device_name}"
     69  1.1  thorpej 	fi
     70  1.1  thorpej }
     71  1.1  thorpej 
     72  1.1  thorpej get_ugen_devnode()
     73  1.1  thorpej {
     74  1.1  thorpej 	# Because "ugen" and "ugenif" share the same /dev/ugenN.*
     75  1.1  thorpej 	# namespace, we have to query an additional property to
     76  1.1  thorpej 	# determine which one it is.
     77  1.1  thorpej 	local ugen_unit
     78  1.1  thorpej 
     79  1.1  thorpej 	ugen_unit=$(drvctl -p $1 ugen-unit)
     80  1.1  thorpej 	case "$ugen_unit" in
     81  1.1  thorpej 	[0-9]*)
     82  1.1  thorpej 		echo "ugen$ugen_unit"
     83  1.1  thorpej 		;;
     84  1.1  thorpej 	esac
     85  1.1  thorpej }
     86  1.1  thorpej 
     87  1.1  thorpej for device in $devices; do
     88  1.1  thorpej 	case $device in
     89  1.1  thorpej 	ugensa*)
     90  1.1  thorpej 		# Ignore ugensa(4).
     91  1.1  thorpej 		;;
     92  1.1  thorpej 	ugen*)
     93  1.1  thorpej 		case $event in
     94  1.1  thorpej 		device-attach)
     95  1.1  thorpej 			devnode=$(get_ugen_devnode $1)
     96  1.1  thorpej 			if [ x"$devnode" != x ]; then
     97  1.1  thorpej 				set_permissions $device $devnode
     98  1.1  thorpej 			fi
     99  1.1  thorpej 			;;
    100  1.1  thorpej 		device-detach)
    101  1.1  thorpej 			restore_permissions $device
    102  1.1  thorpej 			;;
    103  1.1  thorpej 		esac
    104  1.1  thorpej 	esac
    105  1.1  thorpej done
    106