Plugin dev: elevated privileges for wiringpi


#1

Hi there,
I would like to use wiringPi for my lightslider plugin (dim a led-lightstrip for the printbed via a slider in the control tab).

Since the software pwm in the RPi-GPIO library did not produce a stable pwm signal (resulting in a visible flickering) I took a look into wiringPi, which can use the hardware pwm of the RPi.

A first test python script utilizing wiringPi proved that the hw pwm yields much better results.
But since wiringpi modifys the gpio it needs elevated privileges, thus the script needs to be called with sudo.

I tried to transfer the wiringpi commands into the plugin and see if there would be anything in the log files asking for elevated permissions or something(https://github.com/cutnicace/octoprint_lightintensityslider/tree/master/lightslider). But I did not even get far, since octoprint did not get past a certain point. There is no error message or stacktrace in the log, it seems to just exit. My guess (at the moment my only option since I failed to setup a IDE where I could test the plugin within the octoprint environment) is that the modification of the gpio fails due to missing rights and the program exits.

My question is, what is the preferred way to execute a function call/code from an octoprint plugin which needs elevated permissions?
Do I need to put the wiringpi code into a separate script and call it via a suprocess?
How does octoprint execute the shutdown call to the system (which also needs "sudo")?


#2

To be honest, I have no idea how to get the wiringpi library to run with superuser privileges. Via the command line gpio tool though it should be no problem if installed correctly as far as I understand wiringpi's page

The gpio command is designed to be installed as a setuid program and called by a normal user without using the sudo command (or logging in as root).

At least gpio on OctoPi should also be shipped this way if I remember correctly, but I have no idea if it can be used for PWM.

By having an entry (without password) in /etc/sudoers.d, see here. But that only works for called commands, not library based use. And just running the whole OctoPrint server as user root (which you'd need for the wiringpi lib to run as root) is a bad idea for a multitude of reasons.

If you using the gpio executable with setuid root isn't an option, I'd probably go about building a secondary script running as root and communicating to the plugin via a unix domain socket or some other means of inter process communication.


#3

Thanks for the hint, I took a look at the available gpio params and it looks like all necessary cmds for pwm in mark:space mode are available.
I will verify as soon as I get back home to my testPi.


#4

Your tip put me in the right direction.
Got it working as it should with the gpio command line tool.

Thanks again.