Poor mans GPIO permissions fix [howto]


5 posts
by karl101 » Thu Jun 21, 2012 5:10 pm
using this version of debian http://www.raspberrypi.org/archives/1435 I have come up with a fix to the permission denied GPIO problem. This allows anyone (such as the pi user) who is a member of the dialout group to use them without having to switch to root, or use sudo.

this test fails:
Code: Select all
$ echo "4" > /sys/class/gpio/export
-bash: /sys/class/gpio/export: Permission denied


with your favourate editor, open /etc/rc.local
Code: Select all
$ sudo nano /etc/rc.local


and add these two lines to the end:
Code: Select all
chgrp -R dialout /sys/class/gpio
chmod -R g+rw /sys/class/gpio


and reboot the computer

the test now works:
Code: Select all
$ echo "4" > /sys/class/gpio/export
$


I would prefer a permanent fix for this, probably involving udev. But I could not get that working.

Karl.
Posts: 63
Joined: Wed Jan 11, 2012 10:09 am
by hiveda » Mon Jun 25, 2012 7:15 pm
Hello Karl,

Poor or not your solution solved my problem too > now I can run GPIO c-code as user pi without the annoying "permission denied" problem.

Thanks and a big +1
Posts: 7
Joined: Sat Jan 21, 2012 2:26 pm
by karl101 » Tue Jun 26, 2012 8:50 pm
Well, now that I've had a more detailed play, this does not work as intended. Testing with this python code from here http://www.raspberrypi-spy.co.uk/2012/0 ... utput-pin/:
Code: Select all
#!/usr/bin/python

import RPi.GPIO as GPIO
import time

# Set up header pin 11 as an input
print "Setup Pin 11"
GPIO.setup(11, GPIO.OUT)

print "Start loop"
while 1 :
  print "Set Output False"
  GPIO.output(11, False)
  time.sleep(1)
  print "Set Output True"
  GPIO.output(11, True)
  time.sleep(1)

creates /sys/class/gpio17 with the default root permissions:
Code: Select all
--w-rw----  1 root dialout 4096 Jun 26 20:45 export
lrwxrwxrwx  1 root root       0 Jun 26 20:45 gpio17 -> ../../devices/virtual/gpio/gpio17
lrwxrwxrwx  1 root dialout    0 Jun 26 20:17 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0
--w-rw----  1 root dialout 4096 Jun 26 20:17 unexport

and so you get a permission denied error.

This is very annoying

Karl.
Posts: 63
Joined: Wed Jan 11, 2012 10:09 am
by chrisbitz » Sun Jul 22, 2012 7:17 pm
Are there any more clues about this?

It would be really useful if anyone had any ideas... Is it possible that the new Rasbian Wheezy build fixes it?

thanks for trying to fix it! :-)
Posts: 5
Joined: Wed Jun 27, 2012 3:42 pm
by gordon@drogon.net » Mon Jul 23, 2012 10:26 am
What you could do is run a set-uid program that then does the exports for you, so your python, etc. program can then use the /sys/class/gpio as a normal user rather than running as root - however what you'd need to do is not execute any GPIO.setup() calls in Python as they're already done in the external program.

wiringPi has such a program as part of it, so even if not using the library, then the gpio program might be useful. So e.g. if your program was using GPIOs 17, 18 and 19 for outputs and 0 for input, then this little script would work for you:

Code: Select all
#!/bin/bash
for pin in 17 18 19; do
  gpio write $pin 0
  gpio export $pin out
done
gpio export 0 in


No need to run this with sudo - in-fact you shouldn't run it with sudo, as what the export command is doing is changing the ownership of the control files to that of the calling user. This prevents any other user of your Pi accessing the GPIOs you've exported. (assuming they login with a differnet user, although I suspect that very very few Pi's will actually be run in multi-user mode though...)

Once you do that, then you can run your python, etc. program as needed.

You could even use Pythons ability to call external programs to run the gpio commands from inside your program, if required.

wiringPi is here: https://projects.drogon.net/raspberry-pi/wiringpi/ or fast-forward to the download & install page here: https://projects.drogon.net/raspberry-p ... d-install/

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK