On 03/06/2015 08:11 PM, Lee Crocker wrote:
> All of these suggestions are terrible. The right question to ask is "How do I 
> give a non-root user permission to set GPIOs?" If this can't be done, then 
> this distribution of Linux is broken.
> 
> I can add my user to the "kmem" group, then change the group ownership of 
> /sys/class/gpio/export to kmem, the chmod g+w, and then my user can export 
> the GPIOs. But when they appear, "direction" is still owned root:root perms 
> 644, so I can't do anything with the GPIO I just created, even though, as a 
> member of kmem, I could do the equivalent memory-mapped IO.
> 
> It is not acceptable to require a program to run as root. I not quite 
> Linux-savvy enough to fix this.

The Linux way to fix this is with udev.

1) define a user group that should be able to access gpio, such as "gpio"
2) add a udev rule that runs a script on the necessary udev events. Here's my
   ubuntu rule (your distro might configure udev differently; good luck with 
that):

peter@black:~$ cat /etc/udev/rules.d/50-gpio.rules
ACTION!="add", GOTO="gpio_end"

KERNEL=="gpio[0-9]*", SUBSYSTEM=="gpio", ACTION=="add", 
PROGRAM="/lib/udev/gpio_pin %p"
ENV{OF_NAME}=="gpio", SUBSYSTEM=="platform", ACTION=="add", 
PROGRAM="/lib/udev/gpio_pin enable"

LABEL="gpio_end"

3) udev has the appropriate permissions to run the script (below) which
   changes the group/permissions:

peter@black:~$ cat /lib/udev/gpio_pin
#!/bin/sh

PATH='/sbin:/bin'
basedir="/sys/class/gpio"
devpath=$1

if [ "$1" = "enable" ] ; then
    for f in export unexport ; do
        chmod g+w $basedir/$f
        chown :gpio $basedir/$f
    done
else
    for f in active_low direction edge value ; do
        chmod g+w /sys${devpath}/$f
        chown :gpio /sys${devpath}/$f
    done
fi
exit 0

** NOTE **
Naturally there is a small delay between when the pin sysfs
inode is added and when the permissions have been changed, but
it's relatively simple to use a short timeout/delay loop when
opening a just-exported gpio pin.

** NOTE(2) **
When a user group is added, the user must re-login for the group
to become effective in their session.

** NOTE (3) **
udev can be triggered to re-play events after new rules are added
but it can be difficult to do that right, so it's easier just
to reboot.


Regards,
Peter Hurley



-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to