What I am trying to do: Use a chatbot to control the RPi3 gpio.
Ubuntu Core 16.04
rocketchat-server snap - I’m hosting a chat service.
botswana snap - deploys a hubot chatbot and hooks it up to rocketchat.
I have modified the botswana snap by adding a statically compiled c program “gpio” that accesses the GPIO through physical-memory-control and physical-memory-observe. I call “gpio” from the chatbot scripts. This allows me to control the RPi3 gpio remotely with the chatbot.
Problem: If I snap install botswana with confinement “strict”, I can not access /dev/mem even with the botswana plugs connected to physical-memory-control and physical-memory-observe core slots. With --devmode, everything works: I can trigger the chatbot scripts to control the gpio program and see the gpio toggle. This works even though the rocketchat-server is installed with “strict”.
I am controlling the chatbot from within rocketchat. rocketchat-server does not have physical-memory-control and physical-memory-observe plugs.
Everything works if rocketchat-server is “strict” and botswana is “devmode”. I can access /dev/mem by calling gpio from the chatbot inside rocketchat.
snap run botswana.gpio (just calling the gpio program) works on the command line when “strict” as long as I connect the physical memory plugs.
But I am denied access to /dev/mem within rocketchat if botswana is “strict”, even with the botswana plugs hooked up to the core physical memory slots.
I don’t know how to get access to /dev/mem from within rocketchat “strict” when botswana is also “strict”. This is what I would like to do.
physical-memory-control should be enough to get read and write access to /dev/mem. Are there any messages output to the log when you call botswana in strict mode? Use snappy-debug in a separate terminal, while you run your snap, to print the AppArmor messages:
This is after I have connected all botswana plugs. I am using an edge version of ubuntu core 16 and that is why the pi3 slots exists. The stable version does not have them. I do not see the two interfaces you suggest: /dev/gpiomem and the gpio-memory-control interface. What version of ubuntu core has these? My c program access /dev/mem which is mapped to the physical-memory and I’m not having an issue with it. I only have issues when I try to install the snap with “strict” confinement.
The issue is not straight forward because I am calling the gpio program from a coffee script that is linked to a chatbot running in rocketchat. Rocketchat is one snap. The chatbot and gpio are in another snap. I can get gpio to work on the command line, but not when called from the chatbot if the snap is in strict confinement…
/dev/gpiomem is a device only on kernels that provide it. It is available via the bcm2835-gpiomem module which allows non-root processes to access gpio memory. /dev/gpiomem is the subset of /dev/mem set aside for gpio memory. My understanding is that rpi kernels should have this module available. Perhaps use modprobe to load the module?
If that doesn’t work for you, you must necessarily use /dev/mem since, as you say, /dev/gpiomem does not exist. To write to this file, your snap needs physical-memory-control connected, and the process accessing /dev/mem must also be root.
I noticed in your previous comments that you are trying to raise privileges with sudo from inside your botswana snap. This is not allowed. I suggest removing the use of the sudo from the snap and instead use sudo botswana.letloose to have the required privileges to access /dev/mem. Alternatively, you could create a daemon command in your snap to access /dev/mem, since daemons from snaps run as root.
I verified that the pi2-kernel snap (the one used for both rpi2 and rpi3) has the bcm2835_gpiomem module loaded and the /dev/gpiomem device exists on my Ubuntu Core device. I also noticed on this device that /dev/gpiomem only has 0600 permissions. If you want non-root users to access this, you’ll need to adjust the permissions to 0660 and then chgrp /dev/gpiomem to a group your user is in.
while we indeed ship the module and have a /dev/gpiomem device file with our official kernels, a “gpio-memory-control” interface does not exist on any of my Pi’s here … is there a bug or did something not land yet ?
Your snap will not be allowed to use sudo even if you shipped the binary in the package. The command you want to run must be inside the same snap as the caller and you should use direct execution. i.e. do not use snap run. In your coffee script use the full path or relative path to the actual executable inside your snap. You can reference the environment for a variable called SNAP which has the full path to your snap’s filesystem enabling you to just worry about the relative path inside your snap:
@lucyllewy This again is very helpful. I changed the coffee script to point directly to the gpio executable in the /snap/botswana/current/bin directory.
However, the gpio executable uses /dev/mem, which requires sudo access. But, as you point out, I can’t use “sudo” in strict mode. So I think I need to change the gpio program so that a non-root user can access the RPi3 gpio.
@jdstrand suggests using /dev/gpiomem, (which I guess can be accessed by non-root users), but I don’t see a gpio-memory-control interfaces slot when I do “snap interfaces”.
Can I hook it to physical-memory-control instead when accessing /dev/gpiomem?
I have updated my executable to access /dev/gpiomem and verified it works on the command line (not in the snap), but it still requires sudo…
You can’t call sudo from your snap in strict mode, but you can use sudo to call your snap just fine. Note you can also create a daemon in your snap to access /dev/mem and it will run as root and can therefore access the file.
@jdstrand I changed the chatbot launch script to allow sudo when launching the bot. And when launched, I see a process for the bot, owned by root. However, it fails to connect to the chat-service. So I think I am stuck using --devmode for now.
Also, since my edge version has rpi2-bmc-gpio# slots, I could use the “gpio” plug, and change my program to access the gpio through /sys/class/gpio/gpioN, but even ‘sudo echo “4” > /sys/class/gpio/export’ fails…
I also tried to give my username sudo privileges, but even as root I couldn’t make it happen.
Once ubuntu core is available with gpio-memory-control, will my c-program that is memory mapped to /dev/gpiomem work without sudo?
The command above won’t work as you expect, by design.
To correctly echo a value into a file which needs elevated/different privileges you need to sudo the writing step not the echo step. Think of it like this: bash in your command above runs sudo echo '4' which runs echo 4 as root, and you tell bash running as your user to pipe the output of that to /sys/class/gpio/export via your redirection >; the pipe is coordinated by the shell you’re running as yourself so the write will be as you not the user who ran the echo.
This will achieve a write to a root-only file as a non-root user:
echo '4' | sudo tee /sys/class/gpio/export
Here the tee command does the writing and is executed as root via sudo. The echo runs as you, and the pipe redirects the input to sudo which forwards it onto the tee after gaining root privilege, and finally tee running as root then writes to the file.
You could also spawn an entirely new shell and run the whole thing as root: echo, pipe and write. You need to be careful with quotes here though:
Once ubuntu core is available with gpio-memory-control, will it allow read/write access to /dev/gpiomem without sudo?
If not, how can any snap with confinement = strict work without the snap programs being called with sudo? (Because sudo can not be included in a snap, it must be used when doing "snap run snapname.program…)