changing group ownership with udev rules?

Kernels & Hardware, configuring network, installing services

changing group ownership with udev rules?

Postby karl101 » 2012-06-21 15:03

Hello,

on my new computer
Code: Select all
$ uname -a
Linux raspberrypi 3.1.9+ #125 PREEMPT Sun Jun 17 16:09:36 BST 2012 armv6l GNU/Linux


I have this directory: /sys/class/gpio and I can not change the group ownership from root to dialout, The directory contains:
Code: Select all
/sys/class/gpio $ ls -la
total 0
drwxr-xr-x  2 root root    0 Jun 21 13:54 .
drwxr-xr-x 25 root root    0 Jun 21 13:47 ..
--w-------  1 root root 4096 Jun 21 13:54 export
lrwxrwxrwx  1 root root    0 Jun 21 13:54 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0
--w-------  1 root root 4096 Jun 21 13:54 unexport

I can temporarily change the permissions
Code: Select all
# chgrp -R dialout /sys/class/gpio
# chmod -R g+rw /sys/class/gpio

But those get lost when the computer is rebooted. I *believe* the directory is created at boot from the kernel using udev. So in /lib/udev/rules.d i have created a file: 99-gpio.rules, this contains:
Code: Select all
KERNEL=="gpio", DEVPATH=="/class/gpio", GROUP="dialout", MODE="0660"

I have also tried variations, eg:
Code: Select all
SUBSYSTEM=="subsystem", DEVPATH=="/class/gpio", GROUP="dialout", MODE="0660"
KERNEL=="gpio", DEVPATH=="/sys/class/gpio", GROUP="dialout", MODE="0660"
etc...


. I have also tried 11-gpio.rules, and placing it in 91-permissions.rules

this is the info udevdamin gives me:
Code: Select all
 $ udevadm info --path=/sys/class/gpio --attribute-walk

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/class/gpio':
    KERNEL=="gpio"
    SUBSYSTEM=="subsystem"
    DRIVER==""


and this is the results of the test:
Code: Select all
 $ udevadm test /sys/class/gpio
run_command: calling: test
adm_test: version 175
This program is for debugging only, it does not run any program,
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

parse_file: reading '/lib/udev/rules.d/10-local-rpi.rules' as rules file
parse_file: reading '/lib/udev/rules.d/42-qemu-usb.rules' as rules file
parse_file: reading '/lib/udev/rules.d/50-udev-default.rules' as rules file
parse_file: reading '/lib/udev/rules.d/55-dm.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-cdrom_id.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-fuse.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-gnupg.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-ifplugd.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-persistent-alsa.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-persistent-input.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-persistent-serial.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-persistent-storage-dm.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-persistent-storage-tape.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-persistent-storage.rules' as rules file
parse_file: reading '/lib/udev/rules.d/60-persistent-v4l.rules' as rules file
parse_file: reading '/lib/udev/rules.d/61-accelerometer.rules' as rules file
parse_file: reading '/run/udev/rules.d/61-dev-root-link.rules' as rules file
parse_file: reading '/lib/udev/rules.d/64-xorg-xkb.rules' as rules file
parse_file: reading '/lib/udev/rules.d/70-udev-acl.rules' as rules file
parse_file: reading '/lib/udev/rules.d/75-cd-aliases-generator.rules' as rules file
parse_file: reading '/lib/udev/rules.d/75-net-description.rules' as rules file
parse_file: reading '/lib/udev/rules.d/75-persistent-net-generator.rules' as rules file
parse_file: reading '/lib/udev/rules.d/75-probe_mtd.rules' as rules file
parse_file: reading '/lib/udev/rules.d/75-tty-description.rules' as rules file
parse_file: reading '/lib/udev/rules.d/78-sound-card.rules' as rules file
parse_file: reading '/lib/udev/rules.d/80-drivers.rules' as rules file
parse_file: reading '/lib/udev/rules.d/80-networking.rules' as rules file
parse_file: reading '/lib/udev/rules.d/80-udisks.rules' as rules file
parse_file: reading '/lib/udev/rules.d/85-hdparm.rules' as rules file
parse_file: reading '/lib/udev/rules.d/85-hwclock.rules' as rules file
parse_file: reading '/lib/udev/rules.d/91-permissions.rules' as rules file
parse_file: reading '/lib/udev/rules.d/95-keyboard-force-release.rules' as rules file
parse_file: reading '/lib/udev/rules.d/95-keymap.rules' as rules file
parse_file: reading '/lib/udev/rules.d/95-udev-late.rules' as rules file
parse_file: reading '/lib/udev/rules.d/99-gpio.rules' as rules file
parse_file: reading '/etc/udev/rules.d/99-input.rules' as rules file
udev_rules_new: rules use 26868 bytes tokens (2239 * 12 bytes), 16792 bytes buffer
udev_rules_new: temporary index used 17440 bytes (872 * 20 bytes)
udev_device_new_from_syspath: device 0x143d748 has devpath '/class/gpio'
udev_device_new_from_syspath: device 0x143da48 has devpath '/class/gpio'
udev_device_read_db: no db file to read /run/udev/data/+subsystem:gpio: No such file or directory
ACTION=add
DEVPATH=/class/gpio
SUBSYSTEM=subsystem
UDEV_LOG=6
USEC_INITIALIZED=4890292434


Sorry about the big lists of things, but I've been at this most of the day and the permissions stubbornly refuse to change.

Help!
Karl.
karl101
 
Posts: 3
Joined: 2012-06-21 14:39

Re: changing group ownership with udev rules?

Postby subhuman » 2012-06-22 06:35

you know how to read manpages, don't you? why not try a look at
Code: Select all
man udev
first? especially the paragraph
OWNER, GROUP, MODE
The permissions for the device node. Every specified value overwrites the compiled-in default value.

should result in some immediate action on your side of the viewscreen.
User avatar
subhuman
 
Posts: 353
Joined: 2011-08-20 14:54

Re: changing group ownership with udev rules?

Postby karl101 » 2012-06-26 20:54

subhuman wrote:you know how to read manpages, don't you? why not try a look at
Code: Select all
man udev
first? especially the paragraph
OWNER, GROUP, MODE
The permissions for the device node. Every specified value overwrites the compiled-in default value.

should result in some immediate action on your side of the viewscreen.


Such as? for example? I thought I was doing that by adding a rule to 91-permissions.rules. Isn't that what this does?
Code: Select all
KERNEL=="gpio", DEVPATH=="/class/gpio", GROUP="dialout", MODE="0660"


Karl.
karl101
 
Posts: 3
Joined: 2012-06-21 14:39

Re: changing group ownership with udev rules?

Postby katalyst » 2012-10-19 00:57

I was following a similar path to karl101.

I started experimenting by changing ownership to the /sys/class/gpio tree by placing this in my rc.local:

Code: Select all
chown -R pi:pi /sys/class/gpio


Which obviously allows you to export a gpio pin.
I ran into the same issue of root ownership of the gpioX nodes that get created when a gpio pin is exported, preventing me from changing it's direction, or value.

After a while, I figured out the right udev incantation, and this is what I have in my udev rules file (99-gpio.rules):


Code: Select all
SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown -R pi:pi /sys%p'"

This works!

The nodes are accessed by:
/sys/devices/virtual/gpio/gpioX/direction
/sys/devices/virtual/gpio/gpioX/value
etc...

It has udev change the permissions to the dynamically generated gpioX files, allowing access without sudo, or similar techniques.
You can easily substitute other commands to change the permissions, instead of changing the ownership.

Providing access to www-data, for example, allows direct reading/writing with PHP file commands to manipulate the gpio states, which was my goal.

Just another way to have fun with the Raspberry Pi.

I hope that someone finds this helpful.

Dave
katalyst
 
Posts: 1
Joined: 2012-10-18 07:06

Re: changing group ownership with udev rules?

Postby JoergSH » 2013-07-31 17:04

Hello Dave,
that works for GPIOs build at startup.

But export with /sys/class/gpio/export don't work.

Joerg
JoergSH
 
Posts: 1
Joined: 2013-07-31 16:58


Return to System configuration

Who is online

Users browsing this forum: mael and 8 guests

fashionable