On Wed, 10 Jun 2026 16:41:26 -0500
izzy Meyer <[email protected]> wrote:

> Hello misc@
> 
> I have slowly been working on this now-sprawling hotplugd(8) attach
> script but I'm not a fan of how messy and complex it has gotten. Any
> tips to clean it up and make it more modular? I don't like the usbdevs
> hack, it feels uncomfortable to me for some reason. How do your
> hotplugd(8) scripts look?
> 

Thanks for all your replies. I really appreciate the tips. 

I really don't want to have multiple different device management
operations split about the system, mainly cos then all my device
management stuff is all over the place. I have a crappy memory and
I'm terrible at taking notes, so any form of consolidating similar logic
helps me. So, branching some logic out to rc.local and similar is a
non-option for me.

About the webcam permissions. Yes. That is a bad idea. I want to
explain my logic, but not defend it here:

Me and my partner share this computer. My partner needs access to the
webcam for telehealth and work appointments, and so do I. So, I quickly
hacked together the non-optimal solution, chmod 777. It was a hack. 

I could hack XenoDM's GiveConsole here to dynamically assign ownership,
but I found a simpler solution: putting my user and my partner's user
in a new group:

~ $ cat /etc/group | grep webcam
webcam:*:1003:izder456,dracian

Then, doing this in my hotplug attach script so that group has access
but nothing else does:

        chown -RL root:webcam /dev/video*
        chmod -RL 660 /dev/video*

Note: the -L may or may not be needed, but I noticed /dev/video is a
symlink to /dev/video0 on my machine so I figured I needed to account
for that. Please correct me if this isn't needed. It feels wrong.

I added that midithru change to the midi case as I run -current on this
rig. It is a much better solution for what I need. I'd rather not run
midicat as a daemon every time I hook up my keyboard. Seems
overkill. Also- this new adjustment allows me to hotplug my keyboard
even when a program like audio/lmms is consuming the midithru port.
Before, with the midicat hack, I'd have to re-launch lmms every time I
changed midi device. Big improvement.

I also added logging to make the debugging a little easier.

This is what I ended up with as my hotplugd(8) attach script:

~ $ cat /etc/hotplug/attach 
#!/bin/ksh

DEVCLASS=$1
DEVNAME=$2

[ "$DEVCLASS" -eq 0 ] || exit 0

case "$DEVNAME" in
    ugen*)
        # printer for CUPS
        if usbdevs -v | grep -q '03f0:dd11'; then
            ugen=$(usbdevs -v | awk '
                /03f0:dd11/ { found=1 }
                found && /driver: ugen[0-9]+/ {
                    sub(/^[[:space:]]*driver: /, "")
                    print
                    exit
                }
            ')

            [ "$DEVNAME" = "$ugen" ] || exit 0

            logger -c -t hotplug "Printer attached"

            usbctl=$(usbdevs -v | awk -v dev="$ugen" '
                /^Controller \/dev\/usb[0-9]+:/ {
                    ctl=$2
                    sub(":", "", ctl)
                }
                $0 ~ ("driver: " dev "$") {
                    print ctl
                    exit
                }
            ')

            if [ -n "$usbctl" ]; then
                chown _cups:_saned /dev/"${ugen}".* "$usbctl"
                chmod 660 /dev/"${ugen}".* "$usbctl"
            fi
        fi
        ;;
    midi*)
        # usb midi keyboard setup for LMMS
        logger -c -t hotplug "USB Midi Keyboard Attached"
        sndioctl -f midithru/0 server.port=+${DEVNAME#midi}
        ;;
    uvideo*)
        # usb webcam setup for video calls
        logger -c -t hotplug "USB Webcam attached"

        chown -RL root:webcam /dev/video*
        chmod -RL 660 /dev/video*
        ;;
esac

Thanks all.

-- 
iz (she/her)

> I say mundane things
> so the uninteresting
> just might get noticed.

izder456 (dot) neocities (dot) org

Reply via email to