8

This is on a Raspberry Pi.

Here's the output of sudo ls -lL /sys/class/gpio/gpio18:

-rwxrwx--- 1 root     gpio     4096 Mar  8 10:50 active_low
-rwxrwx--- 1 root     gpio     4096 Mar  8 10:52 direction
-rwxrwx--- 1 cameron  cameron  4096 Mar  8 10:50 edge
drwxrwx--- 2 root     gpio        0 Mar  8 10:50 power
drwxrwx--- 2 root     gpio        0 Mar  8 10:50 subsystem
-rwxrwx--- 1 root     gpio     4096 Mar  8 10:50 uevent
-rwxrwx--- 1 cameron  cameron  4096 Mar  8 10:50 value

So looks like I should now have access to value, great. However:

cameron@raspberrypi~ $ echo 1 > /sys/class/gpio/gpio18/value
-bash: /sys/class/gpio/gpio18/value: Permission denied

What's going on? If I chmod 777 everything, then it works, but I shouldn't have to do that when I own the file.

  • 2
    /sys is a pseudo-filesystem and in fact an interface of communication with the kernel. Writing in a file in /sys is like making a request to the kernel so that may require permissions which are not expressed by the file attributes. – lgeorget Mar 08 '14 at 11:24
  • 3
    And before writing a "1" to value, isn't there another file you would have to modify, such as direction for example? – lgeorget Mar 08 '14 at 11:37
  • Cross-posted: http://raspberrypi.stackexchange.com/questions/14287/cant-read-write-to-gpio-as-non-root-after-export – goldilocks Mar 08 '14 at 13:31

4 Answers4

8

I solved the problem by adding cameron to the gpio group:

sudo usermod -aG gpio cameron
gpio export 18 out
echo 1 > /sys/class/gpio/gpio18/value

Now everything works.

5

I have not played with the GPIO pins this way but based on lgeorgets second comment and this article, you must first set the direction of the pin to "out". The direction node is owned by root, so:

sudo sh -c 'echo out > /sys/class/gpio/gpio18/direction'

sh -c is needed here to execute that command in a root subshell. This is because sudo echo out > direction would execute echo as root, but the redirection (> direction) would be done by your current (non-root) shell. You could also just do this su root.

After that you should, hopefully, be able to set value as cameron.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • @StephaneChazelas Do you have any suggestions here that will work? TBH I never use sudo (I su root) so this was a deduction from the man page and elsewhere. We probably should have a canonical question about "sudo and redirection", but I am not really the one to ask it since I do not want to bother configuring something I have no use for. – goldilocks Mar 08 '14 at 18:57
  • there is such a question somewhere. Solution would be sudo sh -c '....', sudo -s calls the user's shell which we don't know anything about anyway. – Stéphane Chazelas Mar 08 '14 at 18:58
  • @StephaneChazelas Thx. Eek at this tho: http://unix.stackexchange.com/questions/1416/redirecting-stdout-to-a-file-you-dont-have-write-permission-on Which seems to be our canonical one... – goldilocks Mar 08 '14 at 19:01
1

As lgeorget mentions as a comment above,

/sys is a pseudo file system provided by the kernel, if the kernel requires the writer to be root then this is a fixed requirement by the kernel and can not be changed by fiddling with attributes and owners.

This is explained in more detail with this answer on question : How to set permissions in /sys/ permanent which provides a solution to access such a file using the sudo command, and provides a recipe to modify /etc/sudoers to avoid having to type a password in each time.

It really wouldn't make sense to rename any of these files would it ?

X Tian
  • 10,463
0

Is this file a symbolic link to some other location? Because in that case, the original file may have different ownership than the link.

What are the permissions of the directory (check ls -l one directory up).

Did you change the ownership of this file? Because sys is a special virtual filesystem that reflects the internal state of the kernel, so most of these variables are immutable by nature, no matter what the filesystem says.

orion
  • 12,502
  • Note that the OP used option -L in ls to dereference the links and show the information on pointed files rather than on symbolic links. – lgeorget Mar 08 '14 at 11:30
  • Who owns the link then? – orion Mar 08 '14 at 12:54
  • There is no link. If you look the output of ls, /sys/class/gpio/gpio18/value is a regular file. (well... more or less regular, as it's a /sys file) – lgeorget Mar 08 '14 at 14:03
  • Then it must be that kernel simply doesn't allow this operation by a regular user. It's convenient to have configuration presented as filesystem (you can use standard tools to manipulate the system on the fly), but never forget that it's not actually a real filesystem. – orion Mar 08 '14 at 14:10
  • I agree. I think glodilocks has pinpointed the problem accurately in his answer. – lgeorget Mar 08 '14 at 14:14