fivdi / pigpio

Fast GPIO, PWM, servo control, state change notification and interrupt handling with Node.js on the Raspberry Pi
MIT License
948 stars 89 forks source link

How to run pigpio with root privilege? #106

Closed nicolasherrbach closed 4 years ago

nicolasherrbach commented 4 years ago

Hello,

Problem: I'm trying to write a plugin for Homebridge using the pigpio library to send waveforms to a 433 MHz emitter plugged on the Pi's GPIO.

I understand the requirement to use root privileges to run pigpio. And my script works correctly if I run it with sudo.

However, Homebridge runs as a service (hb-service) on the Pi, and I don't have the option to put sudo, so I get the error:

+---------------------------------------------------------+
|Sorry, you don't have permission to run this program.    |
|Try running as root, e.g. precede the command with sudo. |
+---------------------------------------------------------+

What I've tried so far (without success):

Is it because the script is ran from a service? Do yo have any idea what I could try?

Environment:

Node.js installed using:

curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
sudo apt install nodejs
fivdi commented 4 years ago

The source of the error message is this block of code in the pigpio C library.

This comment suggests adding the following to /etc/systemd/system/homebridge.service:

[Service]
Type=simple
User=root

Does it work if no user is specified as the default user for services is root?

[Service]
Type=simple

If not, does it work if both User and Group are set to root?

[Service]
Type=simple
User=root
Group=root
nicolasherrbach commented 4 years ago

Thank you for your answer! Unfortunately, none of those options work. When I look at different processes, here's what I get:

We see that all processes are owned by root, but in some cases it doesn't trigger the pigpio error and in other cases it does. My knowledge of linux is quite limited and I'm not sure how this can happen. However the STAT values of those processes are different. Could it be the cause of the problem?

Here's the meaning of those STAT values (from man ps):

S    interruptible sleep (waiting for an event to complete)
L    has pages locked into memory (for real-time and custom IO)
s    is a session leader
l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+    is in the foreground process group
fivdi commented 4 years ago

I'm afraid I'm not familiar enough with Homebridge or using pigpio in services to be able to figure out how to resolve the issue

nicolasherrbach commented 4 years ago

I've got a solution for this on the Raspberry Pi stackexchange.

The problem was not linked with pigpio, but with the service file /etc/systemd/system/homebridge.service where the default settings from the Homebridge installation were preventing somehow to use root privilege.

A working service file is:

[Unit]
Description=Homebridge
Wants=network-online.target
After=syslog.target network-online.target

[Service]
WorkingDirectory=/var/lib/homebridge
EnvironmentFile=/etc/default/homebridge
ExecStartPre=/bin/run-parts /etc/hb-service/homebridge/prestart.d
ExecStartPre=/usr/lib/node_modules/homebridge-config-ui-x/dist/bin/hb-service.js before-start $HOMEBRIDGE_OPTS
ExecStart=/usr/lib/node_modules/homebridge-config-ui-x/dist/bin/hb-service.js run $HOMEBRIDGE_OPTS
KillMode=process

[Install]
WantedBy=multi-user.target

Thank you again for your time and effort on this subject!

fivdi commented 4 years ago

@nicolasherrbach good to hear that it's working now and thank you for providing the feedback,