Installation not allowed by ".." slot rule of interface "serial-port"

Hello!

I use Ubuntu core 18 on rpi3 B+. I try to access ttyUSB from inside the snap. When i install the snap with --devmode, it works.

When i use strict mode, i cannot access it: “Permission denied”.

I tried to use slot below, but when i try to install the snap it gives “installation not allowed by “…” slot rule of interface “serial-port””.

slots:
  serial-port-device:
    interface: serial-port
    usb-vendor: 1659
    usb-product: 8963
    path: /dev/serial-port-icse012a

Does not work with enabled hoplug feature.I have disabled hotplug.

The interface serial-port output:
name: serial-port
summary: allows accessing a specific serial port
plugs:
- test
slots:
- pi:bt-serial

The snap connections output:
Interface Plug Slot Notes
browser-support test:browser-support :browser-support -
hardware-observe test:hardware-observe - -
home test:home - -
network test:network :network -
network-bind test:network-bind :network-bind -
opengl test:opengl :opengl -
pulseaudio test:pulseaudio - -
raw-usb test:raw-usb - -
serial-port test:serial-port - -
wayland test:wayland mir-kiosk:wayland -

What can i do about: “installation not allowed by “…” slot rule of interface “serial-port”” error?

IIRC serial port slots are only allowed to be owned by the gadget which actually owns the hardware, applications need to use plugs to connect to said slot.

how did hotplug fail for you exactly ? i think it should work for that use-case …

Retryed now like this:

$ sudo snap set system experimental.hotplug=true
$ sudo systemctl restart snapd
$ snap interface serial-port
name:    serial-port
summary: allows accessing a specific serial port
plugs:
  - test
slots:
  - pi:bt-serial
  - snapd:pl2303serialport (allows accessing a specific serial port) <- it appears

I also have a udev rule:

$ cat /etc/udev/rules.d/icse012a.rules 
SUBSYSTEM=="tty", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", ACTION=="add", RUN+="/home/user/icse012a-init"

The devices appeared as snapd:pl2303serialport and it responded ( i heard the relay work ).

Created snap for armfh on lxc: test.snap on RPi3 B+ as explained here: https://ograblog.wordpress.com/2020/07/10/building-snap-packages-on-ubuntu-core/

Then:

snap install --dangerous ./test.snap 

Now the eletron app in the snap test is displayed with the browser console enabled, and with the error:

Error: Command failed: echo '50' | xxd -r p >>/dev/ttyUSB0
/bin/sh: 1: cannot create /dev/ttyUSB0: permission denied.

Then:

$ snap connections test
Interface         Plug                   Slot               Notes
browser-support   test:browser-support   :browser-support   -
hardware-observe  test:hardware-observe  -                  -
home              test:home              -                  -
network           test:network           :network           -
network-bind      test:network-bind      :network-bind      -
opengl            test:opengl            :opengl            -
pulseaudio        test:pulseaudio        -                  -
raw-usb           test:raw-usb           -                  -
serial-port       test:serial-port       -                  -
wayland           test:wayland           mir-kiosk:wayland  -

$ snap connect test:serial-port snapd:pl2303serialport

$ snap connections test
Interface         Plug                   Slot               Notes
browser-support   test:browser-support   :browser-support   -
hardware-observe  test:hardware-observe  -                  -
home              test:home              -                  -
network           test:network           :network           -
network-bind      test:network-bind      :network-bind      -
opengl            test:opengl            :opengl            -
pulseaudio        test:pulseaudio        -                  -
raw-usb           test:raw-usb           -                  -
serial-port       test:serial-port       :pl2303serialport  manual
wayland           test:wayland           mir-kiosk:wayland  -

$ snap restart test
Restarted.

The electron app opens with the same error as before: permission denied.

Any thoughts?

EDIT:

In this version i removed the slot for serial-port interface from snapcraft.yaml !!!

where exactly does that come from ? you should not tinker with devices underneath snapd if snapd owns them already (especially with hotplug where snapd itself dynamically creates udev rules) …

I removed the /etc/udev/rules.d/icse012a.rules file.
I see that the error changed:
" xxd: Permission denied."

Command failed: echo '50' | xxd -r -p >>/dev/ttyUSB0
/bin/sh: 1: xxd: Permission denied.

Still… a permission error. Any ideas?

do you ship xxd in your snap ?

No. What do you mean by ship? List him in stage-packages?

Can you give a syntax example please?

yes, add vim-common to you stage packages … probably it is also needed to use the full path $SNAP/usr/bin/xxd instead of just xxd

note that it is often helpful to have a second terminal open on the device and run snappy-debug (from the snappy-debug snap) in there while your app starts up … that should give you some insight what failed and why …

1 Like

I’m recrafting the snap now, it will take some time.

About the udev rule. I need somehow to run that script to init my device (handshake).
I cannot know the state of the device, which means i cannot know if the handshake has been made, therefore, i need to send those commands once, at start up.

How can i set the udev correctly? If i can’t use a udev, are there alternatives?
I tried to write commands in /etc/rc.local, but it fails with:

touch: cannot touch '/etc/rc.local': Read-only file system

you should be able to have a little daemon script that uses udevadm and watches for add/remove events and runs code according to that … (needs the hardware-observe interface connected)

i do something similar (for block devices though, you’ll need to craft your own udevadm filter for serial devices) here:

here is an example script that i quickly threw together, that should work for ttyUSB0:

#! /bin/sh

stdbuf -oL -- udevadm monitor --udev -s usb-serial | while read -r -- _ _ event devpath _; do 
  if [ "$event" = add ]; then 
    if echo "$devpath" | grep -q ttyUSB0; then 
      echo "ttyUSB0 added" # add your init code here
    fi
  fi
done
1 Like

Thank you very much!

I have added in apps:

  device-observer:
    command: device-observer.sh
    daemon: simple

Will the file be found?
I have in parts source .:

parts:
  electron-helloworld:
    plugin: nil
    source: .

Or i need another path to device-observer.sh?

The snap did not build yet to confirm if the xxd error has been solved.

then it will probably end up in the toplevel i.e. $SNAP

using a separate part (so you dont need to re-build the world when making a small change to the script) and putting it into $SNAP/bin (with the dump plugin or similar) would be a bit cleaner …

Thanks, found it here: https://snapcraft.io/docs/defining-a-command

name: os-release
base: core18
version: '0.1'
summary: Outputs the contents of /etc/os-release
description: Prints the contents of os-release
grade: stable
confinement: strict
apps:
  part-os-release:
    command: bin/os-release.sh
parts:
  part-os-release:
    plugin: dump
    source: .
    organize:
      os-release.sh: bin/

yup, that looks about right … dont forget the app needs the hardware-observe plug connected …

Where should i see the output of the script with udevadm?

daemons log to journalctl …

Many thanks! Everything worked!

Sorry for my noob general knowledge of linux.

we all started somewhere, don’t worry :wink:

Hi again!

The add event is not recognized when i reboot the rpi.

The script runs only when i plug the device.

Is there another event? Or maybe snap does not listen at booting time for udevrules?