0

I am writing a custom module kernel that boots at startup. I can write in the sysFS file using this :

echo -e "(0,0,255,750)\n" | sudo tee /sys/fs/led_sysfs/led_value

But I will have another software that will need to write to the sysFs file. This software can only be launched in user mode and not using sudo.

By default the permission of the SysFs is -rwxrwx--- And it's in the rules of creating a SysFs file that it can't be 777. What I found would be the best solution is to create a new group where only my user and the SysFs will be.

So I found that I could write a udev rule to change the parameter. The command udevadm info -a -p /sys/fs/led_sysfs give me this :

     KERNEL=="led_sysfs"
     SUBSYSTEM==""
     DRIVER==""
     ATTR{led_value}=="(not readable)"

looking at parent device '/fs': KERNELS=="fs" SUBSYSTEMS=="" DRIVERS==""

So I tried multiple different rules but none of them work... Here's what I tried :

KERNEL=="led_sysfs", ACTION=="add", RUN+="/bin/chmod 777 /sys/fs/led_sysfs/led_value ;/bin/chown root:led /sys/fs/led_sysfs/led_value ;"  
KERNEL=="led_sysfs", ACTION=="change", RUN+="/bin/chmod 777 /sys/fs/led_sysfs/led_value ;/bin/chown root:led /sys/fs/led_sysfs/led_value ;"  
KERNEL=="led_sysfs", RUN+="/bin/chmod 777 /sys/fs/led_sysfs/led_value ;/bin/chown root:led /sys/fs/led_sysfs/led_value ;" KERNEL=="led_sysfs", RUN+="/bin/chmod 777 /sys/fs/led_sysfs/led_value"  
KERNEL=="led_sysfs", RUN+="/bin/chown   root:led /sys/fs/led_sysfs/led_value" KERNEL=="led_sysfs",   RUN+="/bin/chmod 777 /sys/fs/led_sysfs/led_value;"  
KERNEL=="led_sysfs", RUN+="/bin/chown root:led  /sys/fs/led_sysfs/led_value; KERNEL=="led_sysfs", RUN+="/usr/bin/find  /sys/fs/led_sysfs/ -type f -name led_value -exec chown root:led {} ;  -exec chmod 666 {} ;"  

Does the problem come from how I create my module or my rules? Thanks in advance.

Ajay
  • 1,246
  • Above all that, the problem most likely comes from the fact that devices handled by UDEV aren't made available until all rules finish processing ... So, running commands on those devices in the rules that handle them is an impossible mission AFAIK ... See this similar post Different behaviour of bash script in udev – Raffa Dec 18 '23 at 10:35
  • @Raffa I also got the problem if I reload the rules and my module is already set. It does not modify the current sysfs file – Tamanar Dec 18 '23 at 13:25
  • I was describing how UDEV rules work and not encouraging you to change default permissions under /sys/ anyway as files/directories under it are not real but rather a hierarchy of information reflected from the kernel space about subsystems, devices, drivers ... etc. and it might break if you change it ... Instead do something else like adding ALL ALL = (ALL) NOPASSWD: /usr/bin/tee /sys/fs/led_sysfs/led_value to the sudoers file with sudo visudo so that ... | sudo tee /sys/fs/led_sysfs/led_value wouldn't ask for a password for example. – Raffa Dec 18 '23 at 14:08

1 Answers1

0

Solution found! I changed the way I create my module kernel. I use :

sysfs_file_change_owner(kobj_ref, led_attr.attr.name, new_uid, new_gid);

So the file will be in the same group as my user (created only for this use). With this, I can write to the sysfs file without adding a new rule.