GithubHelp home page GithubHelp logo

permission denied about onoff HOT 34 CLOSED

fivdi avatar fivdi commented on May 20, 2024
permission denied

from onoff.

Comments (34)

enigram avatar enigram commented on May 20, 2024 3

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.

from onoff.

SamDecrock avatar SamDecrock commented on May 20, 2024

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

from onoff.

meyertee avatar meyertee commented on May 20, 2024

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

from onoff.

fivdi avatar fivdi commented on May 20, 2024

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

from onoff.

SamDecrock avatar SamDecrock commented on May 20, 2024

thx, I'll try that out

from onoff.

fivdi avatar fivdi commented on May 20, 2024

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.

from onoff.

eduartheinen avatar eduartheinen commented on May 20, 2024

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.

from onoff.

fivdi avatar fivdi commented on May 20, 2024

@eduartheinen Thanks for the feedback.

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

sudo apt-get update
sudo apt-get upgrade

from onoff.

fivdi avatar fivdi commented on May 20, 2024

@eduartheinen was the error something like this?

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

from onoff.

fivdi avatar fivdi commented on May 20, 2024

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'

from onoff.

fivdi avatar fivdi commented on May 20, 2024

@eduartheinen thanks again

from onoff.

fivdi avatar fivdi commented on May 20, 2024

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

from onoff.

eduartheinen avatar eduartheinen commented on May 20, 2024

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

from onoff.

fivdi avatar fivdi commented on May 20, 2024

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

from onoff.

karthikbnd avatar karthikbnd commented on May 20, 2024

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

from onoff.

fivdi avatar fivdi commented on May 20, 2024

@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.

from onoff.

Peerawich avatar Peerawich commented on May 20, 2024

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

from onoff.

fivdi avatar fivdi commented on May 20, 2024

@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');

from onoff.

Peerawich avatar Peerawich commented on May 20, 2024

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

from onoff.

fivdi avatar fivdi commented on May 20, 2024

@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.

from onoff.

fivdi avatar fivdi commented on May 20, 2024

@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.

from onoff.

Peerawich avatar Peerawich commented on May 20, 2024

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

from onoff.

fivdi avatar fivdi commented on May 20, 2024

Did you reboot the Raspberry Pi?

from onoff.

fivdi avatar fivdi commented on May 20, 2024

@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?

from onoff.

Peerawich avatar Peerawich commented on May 20, 2024

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 ^_^

from onoff.

fivdi avatar fivdi commented on May 20, 2024

Closing as the corresponding Raspbian issue has also been close.

from onoff.

scargill avatar scargill commented on May 20, 2024

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?

from onoff.

fivdi avatar fivdi commented on May 20, 2024

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.

from onoff.

scargill avatar scargill commented on May 20, 2024

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/

from onoff.

scargill avatar scargill commented on May 20, 2024

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

from onoff.

Mr-Black-Dahlia avatar Mr-Black-Dahlia commented on May 20, 2024

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

from onoff.

Zebiano avatar Zebiano commented on May 20, 2024

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! :)

from onoff.

AshwinDeTaeye avatar AshwinDeTaeye commented on May 20, 2024

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?

from onoff.

fivdi avatar fivdi commented on May 20, 2024

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?

from onoff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.