0

Access GPIO on beaglebone as non root user?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Access GPIO on beaglebone as non root user?

by nardev on Wed Jan 11, 2017 9:01 am

I asked all around and any udev rules i tried simply didn't work.

What i need is to be able to access gpio pins as some other use than root.

For example, executing python script from Adafruit which is supposed to read temperature from sensor, and i got this:

user@host$ python Adafruit_Python_DHT/examples/simpletest.py
Traceback (most recent call last):
File "Adafruit_Python_DHT/examples/simpletest.py", line 39, in <module>
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 90, in read_retry
File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 77, in read
File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Beaglebone_Black.py", line 213, in read
RuntimeError: Error accessing GPIO. Make sure program is run as root with sudo!
I tried many different tips to resolve this but it works only if i use it as root I tried this: https://gist.github.com/metasoarous/a73 ... 7f9dcba662 and this https://github.com/metasoarous/strange- ... master/etc

And some other udev/rules.d but nothing worked so far...

Any idea how to change this?

nardev
 
Posts: 44
Joined: Wed Jan 19, 2011 5:03 am

Re: Access GPIO on beaglebone as non root user?

by drewfustini on Wed Jan 11, 2017 9:36 pm

curious, I'm guessing vedran is nardev spelled backwards? :)

[beaglebone] Permission to GPIO and other pins for other than root user? Even after reboot?
https://groups.google.com/forum/#!categ ... oQmdxTXsOs

I think the issue is the that permissions and/or ownership are not set on the correct files and directories. /sys contains many sym links. The permissions and/or ownership need to be set on the target of the sym links, not the sym links themselves.

I've just tried this:
root@beaglebone:~# chmod -R go+rwX /sys/devices/platform/ocp/*gpio/
root@beaglebone:~# su - debian

debian@beaglebone:~$ ls -la
/sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/direction
-rw-rw-rw- 1 root root 4096 Jan 12 01:29
/sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/direction

debian@beaglebone:~$ ls -la
/sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/value
-rw-rw-rw- 1 root root 4096 Jan 12 01:29
/sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/value

debian@beaglebone:~$ echo out >
/sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/direction
debian@beaglebone:~$ echo 1 >
/sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/value

debian@beaglebone:~$ cat
/sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/direction
out
debian@beaglebone:~$ cat
/sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/value
1
Last edited by drewfustini on Wed Jan 11, 2017 9:46 pm, edited 1 time in total.

drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Access GPIO on beaglebone as non root user?

by drewfustini on Wed Jan 11, 2017 9:46 pm

World writable directories and files are usually a bad idea. Here's an example of changing the ownership, instead of the permissions:

root@beaglebone:~# chown -R debian:debian /sys/devices/platform/ocp/*gpio/
root@beaglebone:~# su - debian
debian@beaglebone:~$ git commit -a^C
debian@beaglebone:~$ ls -la /sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/value
-rw-r--r-- 1 debian debian 4096 Jan 12 01:43 /sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/value
debian@beaglebone:~$ ls -la /sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/direction
-rw-r--r-- 1 debian debian 4096 Jan 12 01:43 /sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/direction
debian@beaglebone:~$ echo out > /sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/direction
debian@beaglebone:~$ echo 1 > /sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/value
debian@beaglebone:~$ cat /sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/direction
out
debian@beaglebone:~$ cat /sys/devices/platform/ocp/4804c000.gpio/gpio/gpio61/value
1

drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Access GPIO on beaglebone as non root user?

by nardev on Thu Jan 12, 2017 3:15 am

Hey, thnx for help, unfortunately it didn't help.

I think that they should think about developing good pin settings tool for BeagleBone

p.s. yes youy are rigt about nardev, just wondering what triggered you about that? You have some relations to Balkan?

nardev
 
Posts: 44
Joined: Wed Jan 19, 2011 5:03 am

Re: Access GPIO on beaglebone as non root user?

by drewfustini on Thu Jan 12, 2017 4:15 am

nardev wrote:Hey, thnx for help, unfortunately it didn't help.

I'll give Adafruit_Python_DHT a try and see exactly what paths in /sys that it is trying to access.

nardev wrote:I think that they should think about developing good pin settings tool for BeagleBone

The config-pin utilty leverages capeuniversal and is currently the simplest way to configure pins:
https://github.com/cdsteinkuehler/beagl ... config-pin

nardev wrote:p.s. yes youy are rigt about nardev, just wondering what triggered you about that? You have some relations to Balkan?

Oh, just that one of the replies in "[beaglebone] Permission to GPIO and other pins for other than root user" yesterday was from "vedran" and then I saw your post here on the Adafruit forum.

drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Access GPIO on beaglebone as non root user?

by drewfustini on Thu Jan 12, 2017 5:39 am

Ah, so the issue is that Adafruit_Python_DHT uses memory mapped I/O to access the pins:
debian@beaglebone:~/Adafruit_Python_DHT/examples$ strace -f python ./simpletest.py 2>&1 |grep 'Permission denied'
open("/dev/mem", O_RDWR|O_SYNC) = -1 EACCES (Permission denied)


Only root is allowed to read or write to /dev/mem as this device provides direct access to physical memory.

drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Access GPIO on beaglebone as non root user?

by nardev on Thu Jan 12, 2017 5:41 am

ah, yes, palindrome :D

nardev
 
Posts: 44
Joined: Wed Jan 19, 2011 5:03 am

Please be positive and constructive with your questions and comments.