Rootless GPIO interaction

Post Reply
Tpimp
Posts: 355
Joined: Thu Jul 16, 2015 5:57 am
languages_spoken: english
ODROIDs: U3, C1+, XU4
Contact:

Rootless GPIO interaction

Unread post by Tpimp » Mon Jul 20, 2015 5:34 am

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.

User avatar
odroid
Site Admin
Posts: 29380
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Contact:

Re: Rootless GPIO interaction

Unread post by odroid » Mon Jul 20, 2015 10:43 am

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'"

Tpimp
Posts: 355
Joined: Thu Jul 16, 2015 5:57 am
languages_spoken: english
ODROIDs: U3, C1+, XU4
Contact:

Re: Rootless GPIO interaction

Unread post by Tpimp » Tue Jul 21, 2015 2:10 pm

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!

paul_friesen
Posts: 8
Joined: Thu Jan 28, 2016 7:23 am
languages_spoken: english
ODROIDs: XU4
Contact:

Re: Rootless GPIO interaction

Unread post by paul_friesen » Sat Mar 05, 2016 6:47 am

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.

Tpimp
Posts: 355
Joined: Thu Jul 16, 2015 5:57 am
languages_spoken: english
ODROIDs: U3, C1+, XU4
Contact:

Re: Rootless GPIO interaction

Unread post by Tpimp » Sat Mar 05, 2016 8:45 am

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.

dan-cristian
Posts: 1
Joined: Thu Jan 03, 2019 4:18 am
languages_spoken: english
Contact:

Re: Rootless GPIO interaction

Unread post by dan-cristian » Thu Jan 03, 2019 4:21 am

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'"

User avatar
odroid
Site Admin
Posts: 29380
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Contact:

Re: Rootless GPIO interaction

Unread post by odroid » Thu Jan 03, 2019 8:59 am

Thank you for the complete update. :D

PeepTheGeep
Posts: 3
Joined: Fri Mar 30, 2018 1:57 pm
languages_spoken: English, French, German, Russian
ODROIDs: Odroid C1+, Odroid XU4
Contact:

Re: Rootless GPIO interaction

Unread post by PeepTheGeep » Mon Jan 07, 2019 1:48 pm

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

Post Reply

Return to “Hardware and peripherals”

Who is online

Users browsing this forum: No registered users and 1 guest