3

I am attempting to get a c++ program to access the gpio as non-root. I am on a raspi 2 running Jessie. I followed the instructions on issue: Access GPIO pins without root. No access to /dev/mem. Try running as root! For some reason, it still doesn't work after adding user to the gpio group and ensuring the correct permissions/owners. It says

can't open /dev/mem: : Permission denied

Am i supposed to change permissions to /dev/mem? I've read that it is a security concern so I would like to avoid it. The only think i didn't do was update the kernel to 4.4 (at 4.1.19-v7+) since it looks like it can cause issues with some drivers. Maybe i should risk that?

Any insight would be helpful.

Cole
  • 133
  • 1
  • 1
  • 3

1 Answers1

4

You need root privileges to access /dev/mem.

To allow access to the GPIO a new device was created called /dev/gpiomem.

/dev/gpiomem can be accessed by members of the gpio group and grants access to the memory used by the GPIO.

I give example code at http://abyz.me.uk/rpi/pigpio/examples.html#Misc_tiny_gpio

joan
  • 71,024
  • 5
  • 73
  • 106
  • I have been using the library here: https://github.com/hzeller/rpi-rgb-led-matrix . It turns out this library maps to the hardware directly, so this won't fix my issue. But i think it answers the question i posted. It turns out I need to use setresgid() and setresuid() to make my code work without root access. – Cole May 08 '16 at 04:18
  • @Cole You can't make your program access /dev/mem without root privileges. That's just another way of granting your program root privileges. It may be simpler just to set the program suid. I.e. have the program owned by root and set the s bit, e.g. chown root.root prog; sudo chmod +s prog. – joan May 08 '16 at 07:43
  • @Cole I have the same issue. How do you use setresgid() and setresuid()? – 0__ Jun 18 '21 at 14:48