Accessing the Gpio (Of a Raspberry Pi) Without ''Sudo''

Accessing the GPIO (of a raspberry pi) without ``sudo``

Rakesh, I've just been trying to figure out exactly the same thing, and I think I've solved it.

You don't need to understand much of the makefile at all. The important lines are the following, which are executed in bash when you run sudo make install

install: install-files
groupadd -f --system gpio
chgrp gpio $(DESTDIR)/bin/gpio-admin
chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admin

groupadd -f --system gpio creates a system group called gpio. chgrp gpio $(DESTDIR)/bin/gpio-admin changes the group of the binary (which the C file gpio-admin.c was compiled to) to gpio. The owner of the binary is still root (since you're running make as root.) chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admin does two important things. Firstly, it lets a member of the gpio group run gpio-admin. Secondly, it sets the setuid bit on gpio-admin.

When you add yourself to the gpio group, you can run gpio-admin, without using sudo, but gpio admin will act like it is being run under sudo. This allows it to write to the /sys/class/gpio/export file. It also allows it to change the owner of the files /sys/class/gpio/gpio[pin number]/direction etc. that get created.

Even if you change the group of /sys/class/gpio/export to gpio, and set permissions to allow you to write to it

sudo chgrp gpio /sys/class/gpio/export /sys/class/gpio/unexport
sudo chmod g+rwx /sys/class/gpio/export /sys/class/gpio/unexport

you can export a pin without superuser powers

echo 22 > /sys/class/gpio/export

but the files /sys/class/gpio/gpio22/direction etc. will still be create with root as the owner and group, and you'll need to use sudo to change them. Also, ownership of the export and unexport files will revert to root after each reboot.

wiringPi non-root access to GPIO

Is your non-root user a member of the gpio group? – Ben Voigt

Yeah, that's the point! It wasn't, just changed (usermod -a -G gpio myuser) and now it's working. – KcFnMi

Odroid GPIO pins in ROS without sudo access

Unfortunately, I could not find a fix to the problem with the exact specifications mentioned. However, when I tested the same code on a Raspberry Pi 3 with Raspbian, it works correctly.

So, I have concluded that the issue lies in the OS used i.e. Ubuntu Mate 16.04.

So, a solution might be to use Raspbian on Odroid itself. I am yet to test whether that works out.

Update : Raspbian doesn't exist as such for Odroid, so some other work around might be required.

Access GPIO (/sys/class/gpio) as non-root

You can do this using udev rules, which can define actions to execute when the kernel instantiates new devices. Current versions of the Raspbian distribution for Raspberry Pi devices contain the following in /etc/udev/rules.d/99-com.rules:

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/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio'"

This ensures that entries under /sys/class/gpio are always available to members of the gpio group:

# ls -lL /sys/class/gpio/
total 0
-rwxrwx--- 1 root gpio 4096 May 6 23:36 export
drwxrwx--- 2 root gpio 0 Jan 1 1970 gpiochip0
-rwxrwx--- 1 root gpio 4096 May 6 23:37 unexport
# echo 11 > /sys/class/gpio/export
# ls -lL /sys/class/gpio/
total 0
-rwxrwx--- 1 root gpio 4096 May 6 23:37 export
drwxrwx--- 2 root gpio 0 May 6 23:37 gpio11
drwxrwx--- 2 root gpio 0 Jan 1 1970 gpiochip0
-rwxrwx--- 1 root gpio 4096 May 6 23:37 unexport

Update

Permissions are correct for individual pins as well:

# ls -Ll /sys/class/gpio/gpio11/
total 0
-rwxrwx--- 1 root gpio 4096 May 6 23:37 active_low
drwxr-xr-x 3 root root 0 May 6 23:36 device
-rwxrwx--- 1 root gpio 4096 May 6 23:37 direction
-rwxrwx--- 1 root gpio 4096 May 6 23:37 edge
drwxrwx--- 2 root gpio 0 May 6 23:37 subsystem
-rwxrwx--- 1 root gpio 4096 May 6 23:37 uevent
-rwxrwx--- 1 root gpio 4096 May 6 23:37 value

A certain program only works if I do NOT use sudo when running

Fix: update wiringPi to newest version :)



Related Topics



Leave a reply



Submit