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