Skip to content

Instantly share code, notes, and snippets.

@metasoarous
Last active February 22, 2018 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metasoarous/a7308779837f9dcba662 to your computer and use it in GitHub Desktop.
Save metasoarous/a7308779837f9dcba662 to your computer and use it in GitHub Desktop.
Script and udev rule for setting group permissions on pin files for beaglebone black development
# Install in: /etc/udev/rules.d/
KERNEL=="gpio*", SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/usr/local/bin/set-pinuesrs-permissions.sh"
#!/bin/bash
#
# Place in /usr/local/bin/set-pinusers-permissions.sh
#
# This file will change the user, group and permissions in both the
# /sys/devices/virtual/gpio and /sys/class/gpio path directories. In particular,
# it will add a `pinusers` group to these files. By adding your user to this
# group, you will have access to those pin files.
#
# DO NOT change the order of the commands below, they are optimized so that
# commonly created files and directories are changed first.
#
chown -R :pinusers /sys/devices/virtual/gpio
chown -R :pinusers /sys/class/gpio
find /sys/devices/virtual/gpio -type d -exec chmod 2775 {} \;
find /sys/devices/virtual/gpio -name "direction" -exec chmod 0660 {} \;
find /sys/devices/virtual/gpio -name "edge" -exec chmod 0660 {} \;
find /sys/devices/virtual/gpio -name "value" -exec chmod 0660 {} \;
find /sys/devices/virtual/gpio -name "active_low" -exec chmod 0660 {} \;
chmod 0220 /sys/class/gpio/export
chmod 0220 /sys/class/gpio/unexport
find /sys/devices/virtual/gpio -name "uevent" -exec chmod 0660 {} \;
find /sys/devices/virtual/gpio -name "autosuspend_delay_ms" -exec chmod 0660 {} \;
find /sys/devices/virtual/gpio -name "control" -exec chmod 0660 {} \;
# Additional code for getting AIN pins set up
ain_activator=/sys/devices/bone_capemgr.*
chown -R :pinusers $ain_activator/
chmod -R 2775 $ain_activator/
# Uncomment to have the AIN pins activated by default on boot
# echo cape-bone-iio > $ain_activator/slots
@nardev
Copy link

nardev commented Jan 10, 2017

You have small typo in script name:

KERNEL=="gpio*", SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/usr/local/bin/set-pinuesrs-permissions.sh"

.... pinuesrs .... <<

i believe it should be >> pinusers <<

@sayanee
Copy link

sayanee commented Jan 31, 2017

Thanks @metasoarous! I used this udev script and it has changed the ownership from root:root to another non-root user and group. However, the individual files like direction are not changed despite doing chown -R. E.g.

$ ls -al /sys/class/gpio/gpio23/

total 0
drwxr-xr-x  3 root root    0 Jan 31 15:12 .
drwxr-xr-x 17 root root    0 Jan 31 15:12 ..
-rw-r--r--  1 root root 4096 Jan 31 15:12 active_low
lrwxrwxrwx  1 root root    0 Jan 31 15:12 device -> ../../../44e07000.gpio
-rw-r--r--  1 root root 4096 Jan 31 15:12 direction
-rw-r--r--  1 root root 4096 Jan 31 15:12 edge
drwxr-xr-x  2 root root    0 Jan 31 15:12 power
lrwxrwxrwx  1 root root    0 Jan 31 15:12 subsystem -> ../../../../../../class/gpio
-rw-r--r--  1 root root 4096 Jan 31 15:12 uevent
-rw-r--r--  1 root root 4096 Jan 31 15:12 value

Did you run into the same challenge?

@rtennill
Copy link

the find command does not automatically follow symbolic links so it will not touch those files. Unfortunately just using the '-L' option to follow links also probably won't work due to filesystem reference cycles/loops.

I ended up with a for loop in a bash script to touch the direction, value, and edge files under each gpio during the 'add' event.

#!/bin/bash 

GPIODIR=/sys/class/gpio
FILES="direction value edge"

for d in $(ls $GPIODIR | grep gpio[000-999])
do
  d=$GPIODIR/$d #make an absolute path
  for f in $FILES
  do
    [ -f $d/$f ] && 
      chown :gpio $d/$f
      chmod ug+rw $d/$f
  done
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment