Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

permission denied #3

Closed
SamDecrock opened this issue May 4, 2013 · 34 comments
Closed

permission denied #3

SamDecrock opened this issue May 4, 2013 · 34 comments

Comments

@SamDecrock
Copy link

I'm getting

Error: EACCES, permission denied '/sys/class/gpio/export'

when I try your first example on my raspberry pi

@SamDecrock
Copy link
Author

Ok, I fixed it by running node as root.. but I'm not so happy about that ;-)

@meyertee
Copy link

meyertee commented May 4, 2013

You need to export the gpios once as superuser, afterwards you can run your script normally.. see "How to handle superuser issues" on the Homepage/Readme

@fivdi
Copy link
Owner

fivdi commented May 5, 2013

Sam, as Thomas mentions and assuming you are using Raspbain wheezy, the gpio files used by onoff which are located in directory /sys/class/gpio can't be written by the default login account (Username: pi Password: raspberry.)

See "How to handle superuser issues" on the Homepage/Readme

@SamDecrock
Copy link
Author

thx, I'll try that out

@fivdi
Copy link
Owner

fivdi commented Sep 9, 2015

The github traffic statistics for onoff show that this issue still gets a lot of hits and I don't know why. Today, user pi on Raspbian should be able to access GPIOs without superuser privileges and without exporting the GPIOs as superuser prior to their usage. In the past with old versions of onoff this was not the case. If someone still having issues here, please report it an the issue can be reopened.

@eduartheinen
Copy link

I had some problems with permissions in my node project after updated raspbian to the latest image, just yesterday. But solved them 'dd'ing the image from april, that was working before the update.

@fivdi
Copy link
Owner

fivdi commented Sep 9, 2015

@eduartheinen Thanks for the feedback.

How did you update to the latest image yesterday? Like this:

sudo apt-get update
sudo apt-get upgrade

@fivdi
Copy link
Owner

fivdi commented Sep 9, 2015

@eduartheinen was the error something like this?

Error: EACCES, permission denied '/sys/class/gpio/export'

@fivdi
Copy link
Owner

fivdi commented Sep 9, 2015

So, after updating as follows:

sudo apt-get update
sudo apt-get upgrade

Errors like this occur:

Error: EACCES: permission denied, open '/sys/class/gpio/gpio14/value'
Error: EACCES: permission denied, open '/sys/class/gpio/gpio14/direction'

@fivdi fivdi reopened this Sep 9, 2015
@fivdi fivdi removed the question label Sep 9, 2015
@fivdi
Copy link
Owner

fivdi commented Sep 9, 2015

@eduartheinen thanks again

@fivdi
Copy link
Owner

fivdi commented Sep 9, 2015

This is an issue with Raspbian and a workaround that can be used until it's fixed is described here.

@eduartheinen
Copy link

Exactly, sorry for taking so long to reply, and thanks for the workaround.

@fivdi
Copy link
Owner

fivdi commented Nov 2, 2015

Note that this known Raspbian issue has been fixed on Raspbain Jessie but is still an issue on Raspbian Wheezy.

@karthikbnd
Copy link

how to access gpio on beaglebone black either on p8 or p9 header

@fivdi
Copy link
Owner

fivdi commented Feb 4, 2016

@karthikbnd what have you tried and what were the issues?

In general, onoff can be used on the BeagleBone Black just like it can be used on the Raspberry Pi, it's just a question of getting the GPIO number passed to the Gpio constructor correct.

For example, if there's an LED on GPIO #15 (P9_24) and a momentary push button on GPIO #14 (P9_26), the following program will turn the LED on when the button is pressed and turn it off when the button is released (http://beagleboard.org/static/images/cape-headers-digital.png).

var Gpio = require('onoff').Gpio,
  led = new Gpio(15, 'out'),
  button = new Gpio(14, 'in', 'both');

button.watch(function(err, value) {
  led.writeSync(value);
});

If there are issues with onoff on the BeagleBone Black please create a new issues for discussing the topic. This issues is for "Permission Denied" problems on the Raspberry Pi.

@Peerawich
Copy link

EPERM: operation not permitted, write
when I try to led.writeSync(1);

@fivdi
Copy link
Owner

fivdi commented Feb 6, 2016

@BloodMore EPERM errors occur when an operation is not permitted, for example, because it doesn't make sense or is illogical. EACCES errors occur when the process doesn't have sufficient privileges to perform the requested operation.

In this case I would imagine that the led object is a Gpio input but should be a Gpio output. It's not possible to call writeSync for a Gpio input.

Was led (mistakenly) created as a Gpio input as follows:

var led = new Gpio(<gpio number>, 'in');

rather than as a Gpio output as follows?

var led = new Gpio(<gpio number>, 'out');

@Peerawich
Copy link

mycode

var express = require('express');
var app = express();
var ejs = require('ejs');
var Gpio = require('onoff').Gpio,
    v1 = new Gpio(14, 'both');
var path = require("path");

app.set('view engine','ejs');

app.use(express.static(path.join(__dirname, 'views')));

app.get('/',function(req,res){
    //res.sendFile(path.join(__dirname+'/view.ejs'));
    if(v1.readSync()=='1') {
        res.render('view.ejs',{v1status: "ON"});
    } 
    if(v1.readSync()=='0') {
        res.render('view.ejs',{v1status: "OFF"});
    }
});
app.post('/V1on', function(req, res) {
    console.log('ON');
    v1.writeSync(1);
});

app.post('/V1off', function(req, res) {
    console.log('OFF');
    v1.writeSync(0);
});

app.listen(1337);
console.log("1337");

I can read it but I can't write
When I try "sudo node app.js" the result still EPERM: operation not permitted, write (I use view.ejs as html button to call /V1on & / V1off) yesterday this code run correctly but today....

I use raspbian jessie

@fivdi
Copy link
Owner

fivdi commented Feb 6, 2016

@BloodMore The Gpio constructor is being used incorrectly. As documented in the readme :), the second parameter passed to the constructor specifies the direction of the Gpio. The valid values are 'in', 'out', 'high', and 'low'. 'both' is not a valid direction.

Please reboot your Raspberry Pi to get it into a consistent state and then change the following line:

    v1 = new Gpio(14, 'both');

to:

    v1 = new Gpio(14, 'out');

Then give it another try to see if it works.

@fivdi
Copy link
Owner

fivdi commented Feb 6, 2016

@BloodMore Note that root privileges shouldn't be needed and it should be possible to run the application as user pi on Raspbian Jessie with node app.js rather than sudo node app.js. If not, something isn't functioning correctly somewhere.

@Peerawich
Copy link

change to
v1 = new Gpio(14, 'out');
still same problem

@fivdi
Copy link
Owner

fivdi commented Feb 6, 2016

Did you reboot the Raspberry Pi?

@fivdi
Copy link
Owner

fivdi commented Feb 6, 2016

@BloodMore your last comment in this thread has somehow disappeared, but if I'm not mistaken you mentioned that the application was now working correctly, is this correct?

@Peerawich
Copy link

now the application is work correctly
I have change to 'v1 = new Gpio(14, 'out');' and reboot my Raspi
I'vetry
'v1 = new Gpio(14, 'in','out');' It has same problem
now I use
v1 = new Gpio(14, 'out'); for this code I can readSync() and writeSync() --> no problem

thanks for your help ^_^

@fivdi
Copy link
Owner

fivdi commented Jun 5, 2016

Closing as the corresponding Raspbian issue has also been close.

@fivdi fivdi closed this as completed Jun 5, 2016
@scargill
Copy link

I am using the node-red-contrib-opi-gpio node which uses onoff... I've tried it on several friendlyArm units - the M3, the NEO, the Air - and by granting permissions as per here...
https://flows.nodered.org/node/node-red-contrib-opi-gpio

Pi user (Node-Red gains access to the ports and it works a treat - this is a life-saver.

However, the NanoPi NEO PLUS2 is not having it - I have checked that the permissions have been given and yet when I start up Node-Red here are some examples

29 Jul 11:01:47 - [info] [opi_out:PA6] Pin: 6
29 Jul 11:01:47 - [error] [opi_out:a9041de2.0d817] Error: EACCES: permission denied, open '/sys/class/gpio/gpio6/value'

29 Jul 00:58:41 - [info] [opi_out:PG11] Pin: 203
29 Jul 00:58:41 - [error] [opi_out:a9041de2.0d817] Error: EACCES: permission denied, open '/sys/class/gpio/export'

I am by no means an expert on permissions but I figured if I grant (recursively) access to /sys/class/gpio for everyone (as root) this would solve the problem - no - it did not. I tried different port bits in case this was some special setting on one of the bits... no - can't access any of them.

Any ideas... So this is the standard FriendlyArm Linux 4.11.2 Arm64 distribution - everything else I've tried works fine - just this port issue. Why would this be different to the other boards?

@fivdi
Copy link
Owner

fivdi commented Jul 29, 2017

I'm by no an expert on permissions either. What I do know is that granting permission recursively to /sys/class/gpio on the Raspberry Pi is not enough to allow user pi to access GPIOs without root privileges. Permissions to access other directories are also needed.

Here are the first few lines of /etc/udev/rules.d/99-com.rule on the latest version of Raspbian.

SUBSYSTEM=="input", GROUP="input", MODE="0660"
SUBSYSTEM=="i2c-dev", GROUP="i2c", MODE="0660"
SUBSYSTEM=="spidev", GROUP="spi", MODE="0660"
SUBSYSTEM=="bcm2835-gpiomem", GROUP="gpio", MODE="0660"

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;\
	chown -R root:gpio /sys$devpath && chmod -R 770 /sys$devpath\
'"

As can be seen not only /sys/class/gpio plays a role. /sys/devices/virtual/gpio and /sys$devpath also play a role. As far as I remember /sys/devices/virtual/gpio only plays a role if the device tree is not used on Raspbian. Raspbian uses the device tree by default. If I'm not mistaken /sys$devpath is probably /sys/devices/gpiochip1 and /sys/devices/gpiochip1 but there may be more involved.

The directories to which permissions need to be granted is system dependent.

@scargill
Copy link

Thanks for that fivdi... as it happens after a reboot I managed to get all but a couple of the gpio pins working - I've taken the liberty of copying that Pi info - thanking you for pointing it out - here...

https://tech.scargill.net/a-new-gpio-in-town/

@scargill
Copy link

I wonder if anyone is able to make such a script that would handle a wide range of machines... might be helpful.

@enigram
Copy link

enigram commented Jan 4, 2019

This issue is closed, but I'm still getting this error 5 years later.

I'm running Raspbian GNU/Linux 9 (stretch) with node v10.15.0. I can't seem to get it working following any old advice. But I was able to get it working by adding my user to the gpio group. Putting this here in case someone is still having issues.

@Mr-Black-Dahlia
Copy link

Mr-Black-Dahlia commented Feb 10, 2019

I fixed this by doing this: https://www.raspberrypi.org/forums/viewtopic.php?t=5185#p161013

@Zebiano
Copy link

Zebiano commented Jul 20, 2021

I'm currently running a Pi 4 with Ubuntu Server 21.04 and Node 14.17.3 installed through nvm. This is problematic, because by default nvm installs Node and npm without root access, which means running sudo npm run will yield command not found. But running onoff without sudo also doesn't work. So far, to get around this issue, I've followed this answer on Stack Overflow. Please use it with caution.

Currently, I do get read values back, though it's always the same output (0). Though I'm probably doing something wrong, so I guess this did fix my permission denied problem! :)

@AshwinDeTaeye
Copy link

This issue resides in Ubuntu 22.04 for raspberry. Even with sudo node app.js, you cannot get around it anymore.
I have read that sys/class/gpio is not supported from linux kernel 5.11 and onwards.

Does this mean this lib is not usable for OS with kernel >5.11?

I looked into your pigpio project, but it seems to also need sudo.
lgpio seems to be an alternative to access gpio in userspace:
https://www.npmjs.com/package/lgpio

@fivdi Do you know an alternative? What do you think lgpio approach?

@fivdi
Copy link
Owner

fivdi commented Sep 5, 2022

This issue resides in Ubuntu 22.04 for raspberry. Even with sudo node app.js, you cannot get around it anymore. I have read that sys/class/gpio is not supported from linux kernel 5.11 and onwards.

Does this mean this lib is not usable for OS with kernel >5.11?

In general, this is not the case as onoff functions with Raspberry Pi OS 11.3 with Linux kernel version 5.15.32-v8+. I'm not familiar with Ubuntu 22.04 on the Raspberry Pi. Perhaps Ubuntu has dropped support for GPIO from user space using the sysfs files at sys/class/gpio. That being said, if you are getting a permission error (EACCES), and not a different error, I would say there are permission issues. I would expect a different error if support for sysfs GPIO was dropped.

I looked into your pigpio project, but it seems to also need sudo. lgpio seems to be an alternative to access gpio in userspace: https://www.npmjs.com/package/lgpio

@fivdi Do you know an alternative? What do you think lgpio approach?

I'm afraid I don't know of an alternative and I'm not familiar with lgpio.

May I ask why you are using Ubuntu rather than Raspberry Pi OS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests