Module Name: src Committed By: thorpej Date: Sat Mar 30 06:42:10 UTC 2024
Modified Files: src/distrib/sets/lists/base: mi src/share/examples/devpubd/hooks: Makefile Added Files: src/share/examples/devpubd/hooks: 99-ugen-perms-tigard Log Message: Add an example devpubd hook that detects the generic USB interface portion of a Tigard debug board and changes the permissions of the appropriate /dev/ugenN.* nodes to allow access without superuser permissions, suitable for using e.g. openocd with the device. This example can be easily modified to support other generic USB devices that have user-space drivers where running as the superuser is not desired. To generate a diff of this commit: cvs rdiff -u -r1.1336 -r1.1337 src/distrib/sets/lists/base/mi cvs rdiff -u -r0 -r1.1 src/share/examples/devpubd/hooks/99-ugen-perms-tigard cvs rdiff -u -r1.1 -r1.2 src/share/examples/devpubd/hooks/Makefile Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/base/mi diff -u src/distrib/sets/lists/base/mi:1.1336 src/distrib/sets/lists/base/mi:1.1337 --- src/distrib/sets/lists/base/mi:1.1336 Sat Mar 30 06:29:01 2024 +++ src/distrib/sets/lists/base/mi Sat Mar 30 06:42:10 2024 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.1336 2024/03/30 06:29:01 thorpej Exp $ +# $NetBSD: mi,v 1.1337 2024/03/30 06:42:10 thorpej Exp $ # # Note: Don't delete entries from here - mark them as "obsolete" instead, # unless otherwise stated below. @@ -2488,6 +2488,7 @@ ./usr/share/examples/devpubd base-sys-examples ./usr/share/examples/devpubd/hooks base-sys-examples ./usr/share/examples/devpubd/hooks/99-ucom-symlinks base-sys-examples +./usr/share/examples/devpubd/hooks/99-ugen-perms-tigard base-sys-examples ./usr/share/examples/dhcp base-dhcpd-examples ./usr/share/examples/dhcpcd base-dhcpcd-examples ./usr/share/examples/dhcpcd/hooks base-dhcpcd-examples Index: src/share/examples/devpubd/hooks/Makefile diff -u src/share/examples/devpubd/hooks/Makefile:1.1 src/share/examples/devpubd/hooks/Makefile:1.2 --- src/share/examples/devpubd/hooks/Makefile:1.1 Sat Mar 30 06:29:01 2024 +++ src/share/examples/devpubd/hooks/Makefile Sat Mar 30 06:42:10 2024 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.1 2024/03/30 06:29:01 thorpej Exp $ +# $NetBSD: Makefile,v 1.2 2024/03/30 06:42:10 thorpej Exp $ NOOBJ= # defined @@ -6,6 +6,7 @@ NOOBJ= # defined .if ${MKSHARE} != "no" FILES= 99-ucom-symlinks +FILES+= 99-ugen-perms-tigard FILESDIR=/usr/share/examples/devpubd/hooks .endif Added files: Index: src/share/examples/devpubd/hooks/99-ugen-perms-tigard diff -u /dev/null src/share/examples/devpubd/hooks/99-ugen-perms-tigard:1.1 --- /dev/null Sat Mar 30 06:42:10 2024 +++ src/share/examples/devpubd/hooks/99-ugen-perms-tigard Sat Mar 30 06:42:10 2024 @@ -0,0 +1,105 @@ +#!/bin/sh - +# +# $NetBSD: 99-ugen-perms-tigard,v 1.1 2024/03/30 06:42:10 thorpej Exp $ +# +# Look for a Tigard (https://github.com/tigard-tools/tigard) debug +# board and change the permissions to 0660. +# +# Written by Jason R. Thorpe, March 2024. Public domain. +# + +export LC_ALL=C + +event="$1" +shift +devices=$@ + +orig_perms=0600 +new_perms=0660 + +orig_group=wheel +new_group=wheel + +device_name=tigard + +is_target_device() +{ + local vendor_string + local product_string + + vendor_string="$(drvctl -p $1 vendor-string)" + product_string="$(drvctl -p $1 product-string)" + + if [ x"$vendor_string" = x"SecuringHardware.com" -a \ + x"$product_string" = x"Tigard V1.1" ]; then + echo "yes" + return + fi + + echo "no" +} + +set_permissions() +{ + if [ x$(is_target_device $1) = xyes ]; then + chgrp $new_group /dev/"${2}".* + chmod $new_perms /dev/"${2}".* + # + # We need to create a symlink here to remember + # the ugen device node that was used, since we + # can't recover it from the device name that + # comes from the kernel later because we get the + # event *after* the device is gone, and thus + # cannot query any properties. + # + rm -f /dev/${1}-${device_name} + ln -sf ${2} /dev/${1}-${device_name} + fi +} + +restore_permissions() +{ + if [ -h "/dev/${1}-${device_name}" ]; then + devnode=$(readlink "/dev/${1}-${device_name}") + if [ x"$devnode" != x ]; then + chmod $orig_perms /dev/"${devnode}".* + chgrp $orig_group /dev/"${devnode}".* + fi + rm -f "/dev/${1}-${device_name}" + fi +} + +get_ugen_devnode() +{ + # Because "ugen" and "ugenif" share the same /dev/ugenN.* + # namespace, we have to query an additional property to + # determine which one it is. + local ugen_unit + + ugen_unit=$(drvctl -p $1 ugen-unit) + case "$ugen_unit" in + [0-9]*) + echo "ugen$ugen_unit" + ;; + esac +} + +for device in $devices; do + case $device in + ugensa*) + # Ignore ugensa(4). + ;; + ugen*) + case $event in + device-attach) + devnode=$(get_ugen_devnode $1) + if [ x"$devnode" != x ]; then + set_permissions $device $devnode + fi + ;; + device-detach) + restore_permissions $device + ;; + esac + esac +done