1

I am working on setting up an automated deployment of an executable using the PIGPIO C bindings to a Pi Zero. I copy the executable to the pi (into a directory on pi user's desktop) as expected, but when I try to run it, I get a permissions error:

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

It seems this problem can be solved by setting the executable to be owned by root and using a permission setting like 4775. This seems to allow me to run the executable while logged in as pi.

Unfortunately though, when I then copy a new version of the executable in, the ownership permissions stay the same, but the sticky bit is removed and I can no longer run the executable. It seems like the right answer would be "figure out how to run the executable when it's owned by pi". Is this possible? Can I change something so PIGPIO can access the needed hardware without having to do weird things with the permissions?

This question seems to be a similar issue, but with PWM specifically. I see a gpio group but pi is already in that group. Is this question related or completely misleading me?

  • 1
    If you can copy a new file to the Pi, you can also set the permissions on it. – Dmitry Grigoryev Apr 1 '20 at 7:35
  • Have a look at using the C I/F to the pigpio daemon. That will be quite suitable for a lot of usage. abyz.me.uk/rpi/pigpio/pdif2.html – joan Apr 1 '20 at 7:47
  • @joan how does that differ from the standard pigpio library? Will it not run into the permissions issues seen with the other? – RedBassett Apr 1 '20 at 22:18
  • @DmitryGrigoryev In theory, yes, but that is proving difficult with the automated deployment I am using. I can change permissions, but my question is whether there is another way to fix the issue without using special permission bits. – RedBassett Apr 1 '20 at 22:19
  • The pigpio daemon will need to be started with sudo. Programs linked against pigpiod_if2 do not. – joan Apr 2 '20 at 7:07
-1

I am assuming you have the source code of your C program. If so, add these lines:

At the top:

#include <sys/types.h>
#include <unistd.h>

Near the start of main():

if (setuid(getuid()) < 0)
{
        printf("Privilege change failed\n");
        exit(EXIT_FAILURE);
}

You can use any other error notification, instead of the printf.

See man -s 2 setuid for details.

  • This isn't a C program originally, it is an executable from Kotlin Native that is linked to the PIGPIO library. I am favoring a solution that lets the library be run from the pi user without needing special permission bits and sudo, if one exists, in part because I want to avoid fiddling with parts of the C library. – RedBassett Apr 1 '20 at 22:26
  • -1. This will fail unless the process is already privileged (e.g. running as root), at which point there is no purpose in setuid. – Dmitry Grigoryev Apr 2 '20 at 7:57
  • @DmitryGrigoryev I copied these lines (I'm afraid I can't find the reference) for my first ever Pi program; believing it was necessary to access GPIO. It most certainly does not fail, in the sense that I have never experienced the error message. I can't check now, but IIRC, the executable is owned by pi and has default permissions (755). – Peter bill Apr 2 '20 at 9:32
  • Think about it: if any executable with default permissions can simply call setuid(getuid()) to run as root, what is the purpose of having sudo and the sticky bit? – Dmitry Grigoryev Apr 2 '20 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.