Page 1 of 1

Rootless GPIO interaction

Posted: Mon Jul 20, 2015 5:34 am
by Tpimp
From my experience GPIO from sysfs requires sudo access. i.e. to export a pin or work with it either the permissions must be changed or applications must be ran as sudo. To combat this, I have worked on two udev rules to always make GPIO available to user on start.

append this to your udev rules (or create it's own) /etc/udev/rules.d/10-odroid.rules
i.e. the shield udev rule would also be a good place

Code: Select all

SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"
Another huge benefit is all the GPIOs will also be exported on start! After exporting them, the second udev rule then sets permissions.

This might work on other odroids as well but I do not have anything but the U3 to test on at the moment.

Re: Rootless GPIO interaction

Posted: Mon Jul 20, 2015 10:43 am
by odroid
Thank you for the nice tip. :)

We've slightly modified your udev rules file to work with XU3/XU4.

Code: Select all

SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:odroid /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:odroid /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"

Re: Rootless GPIO interaction

Posted: Tue Jul 21, 2015 2:10 pm
by Tpimp
odroid wrote:Thank you for the nice tip. :)

We've slightly modified your udev rules file to work with XU3/XU4.

Code: Select all

SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:odroid /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:odroid /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"
Oh sorry I forgot too mention in my version, it gives permission for anyone in the gpio group. So you need to explicitly add odroid to the group. But it also allows you to add others if they just get added to the group by root once.

Awesome! Glad I could help!

Re: Rootless GPIO interaction

Posted: Sat Mar 05, 2016 6:47 am
by paul_friesen
I found a simple way to get access to GPIO without being root.

Example:
To get access to pin 4 on the 30-pin header, add this line to the file /etc/rc.local, just before the line "exit 0" (you need to be root to edit it):

sudo -u odroid /usr/local/bin/gpio export 173 out

Now, every time the computer reboots, it will export the gpio line, just as if you had used echo 173 /sys/class/gpio/export, except that the owner of the "value" file that is created will be "odroid" and not "root". You can write to it without being root.

Re: Rootless GPIO interaction

Posted: Sat Mar 05, 2016 8:45 am
by Tpimp
That's what this udev rule is doing but for all gpio available and AFAIK is the preferred method. If you only wanted a specific one you can also alter udev rule for only one pin.

Re: Rootless GPIO interaction

Posted: Thu Jan 03, 2019 4:21 am
by dan-cristian
The above UDEV rules are not providing interrupts access. The complete rules with interrupts enables are below, access to uevent is also needed:

Code: Select all

SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value /sys%p/uevent; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value /sys%p/uevent'"

Re: Rootless GPIO interaction

Posted: Thu Jan 03, 2019 8:59 am
by odroid
Thank you for the complete update. :D

Re: Rootless GPIO interaction

Posted: Mon Jan 07, 2019 1:48 pm
by PeepTheGeep
Hello, I was working with the XU4 to get rootless GPIO access/interaction.

I tried out all these solutions and more that I found elsewhere. Most didn't work, some half worked (was able to export GPIO pins as odroid user but nothing else).

What did end up working for me was creating the file /etc/udev/rules.d/99-com.rules and adding the following line to it:

Code: Select all

SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio; chown -R root:gpio /sys/devices && chmod -R 770 /sys/devices'"
It was important to add the second half because otherwise only the folders in the gpio folder belonged to the group gpio, but not their descendant files (which confused me since I thought I did a recursive chown/chmod combo). With

Code: Select all

ls -la
I saw that the files in the gpio directory all had symlinks pointing to /sys/devices so I added the same chown/chmod combo for /sys/devices and then everything worked!

Here's my commit which also has a README that explains the purpose/setup:
https://github.com/space-concordia-robo ... e11feb45f9