Sample Header Ad - 728x90

Udev rule for syfs class/device attribute ownership

1 vote
1 answer
236 views
This was originally asked on [Stack Overflow](https://stackoverflow.com/questions/79329706/udev-rule-for-syfs-class-device-attributes) , but it was closed for being off-topic. Hopefully this is the right forum for the question. I'm writing a character device driver that exposes class and device attributes under sysfs. I'm using udev to make the devices that appear in /dev read/writable by a certain group, but I can't figure out how to make the sysfs attributes writable by the same group without a shell script.
$ ls -l /dev/foobar
crw-rw---- 1 root my_group 241, 0 Jan  4 21:57 /dev/foobar # ownership applied by udev
$ ls -l /sys/class/my_class/wo_attribute
--w--w---- 1 root root 4096 Jan  4 22:02 wo_attribute # still in root group :/
$ ls -l /sys/class/my_class/my_device/rw_attribute
-rw-rw-r-- 1 root root 4096 Jan  4 22:13 rw_attribute # ditto
I've tried using udevadm info to match the KERNEL and SUBSYSTEM keys of the sysfs path to no effect. I've also found the udev_event and get_ownership fields of struct class; I haven't had luck with the former and the latter works for device attributes, but I have to hard code my_group's gid which is not ideal. Here's an example of trying to find the right match keys for the class:
$ udevadm info -a /sys/class/my_module
 looking at device '/class/my_class':
    KERNEL=="my_class"
    SUBSYSTEM=="subsystem"
    DRIVER==""
    ATTR{wo_attribute}=="(not readable)"

  looking at parent device '/class':
    KERNELS=="class"
    SUBSYSTEMS==""
    DRIVERS==""
    ATTRS{devcoredump/disabled}=="0"
    ATTRS{drm/version}=="drm 1.1.0 20060810"
    ATTRS{firmware/timeout}=="60"
    ATTRS{gpio/export}=="(not readable)"
    ATTRS{gpio/unexport}=="(not readable)"
    ATTRS{my_class/wo_attribute}=="(not readable)"
But the udev rule KERNEL=="my_class", GROUP="my_group" doesn't work. Does udev only work with devices under /dev, or is there a way to apply the rules to sysfs attributes?
Asked by olishmollie (13 rep)
Jan 7, 2025, 06:08 PM
Last activity: Jan 7, 2025, 07:51 PM