Snapctl is-connected command not working

I want to use the snapctl tool’s is-connected command to ask the user to connect required interfaces.

However, it doesn’t look like it’s working (or I’m doing something wrong). It keeps responding that unconnected plugs are connected. It only fails when the plug doesn’t exist.

ubuntu@ip-172-31-60-79:~$ snap connections aws-iot-greengrass
Interface                Plug                                        Slot                 Notes
camera                   aws-iot-greengrass:camera                   -                    -
dvb                      aws-iot-greengrass:dvb                      -                    -
gpio                     aws-iot-greengrass:gpio                     -                    -
gpio-memory-control      aws-iot-greengrass:gpio-memory-control      -                    -
greengrass-support       aws-iot-greengrass:greengrass-support       :greengrass-support  -
hardware-observe         aws-iot-greengrass:hardware-observe         -                    -
hardware-random-control  aws-iot-greengrass:hardware-random-control  -                    -
home                     aws-iot-greengrass:home                     :home                -
home                     aws-iot-greengrass:home-for-hooks           :home                -
hugepages-control        aws-iot-greengrass:hugepages-control        -                    -
i2c                      aws-iot-greengrass:i2c                      -                    -
iio                      aws-iot-greengrass:iio                      -                    -
joystick                 aws-iot-greengrass:joystick                 -                    -
log-observe              aws-iot-greengrass:log-observe              -                    -
mount-observe            aws-iot-greengrass:mount-observe            -                    -
network                  aws-iot-greengrass:network                  :network             -
network-bind             aws-iot-greengrass:network-bind             :network-bind        -
network-control          aws-iot-greengrass:network-control          :network-control     -
opengl                   aws-iot-greengrass:opengl                   :opengl              -
optical-drive            aws-iot-greengrass:optical-drive            :optical-drive       -
process-control          aws-iot-greengrass:process-control          :process-control     -
raw-usb                  aws-iot-greengrass:raw-usb                  -                    -
removable-media          aws-iot-greengrass:removable-media          -                    -
serial-port              aws-iot-greengrass:serial-port              -                    -
spi                      aws-iot-greengrass:spi                      -                    -
system-observe           aws-iot-greengrass:system-observe           :system-observe      -

The snapctl is-connected <plug> | echo $? command comes from https://snapcraft.io/docs/using-snapctl I should be getting a 1 if the plug isn’t connected.

ubuntu@ip-172-31-60-79:~$ sudo snap run --shell aws-iot-greengrass.greengrassd
root@ip-172-31-60-79:/home/ubuntu# snapctl is-connected network | echo $?
0
root@ip-172-31-60-79:/home/ubuntu# snapctl is-connected serial-port | echo $?
0
root@ip-172-31-60-79:/home/ubuntu# snapctl is-connected camera | echo $?
0
root@ip-172-31-60-79:/home/ubuntu# snapctl is-connected bla-bla-bla | echo $?
0
error: error running snapctl: snap "aws-iot-greengrass" has no plug or slot named "bla-bla-bla"

What’s going on?

Also, how does the command differentiate between a plug and a slot? The docs say that

snapctl is-connected <plug|slot>

is the command’s format. When I run:

snapctl is-connected home

am I querying for the home plug or home-for-hooks slot?

Your method of checking the return value of the command is the problem. From a shell:

$ true | echo $?
0
$ false | echo $?
0

As you’re creating a pipeline, the echo $? part would show the exit code of the previous command. And since the last element of each pipeline you ran succeeds, you’re always seeing a zero.

Try using ; echo $? or && echo connected instead.

Hmm you’re right.

I used python to get the actual return code, and the result makes sense:

>>> import subprocess
>>> for plug in ('camera', 'process-control', 'system-observe', 'gpio'):
...     subprocess.run(('snapctl', 'is-connected', plug))
... 
CompletedProcess(args=('snapctl', 'is-connected', 'camera'), returncode=1)
CompletedProcess(args=('snapctl', 'is-connected', 'process-control'), returncode=0)
CompletedProcess(args=('snapctl', 'is-connected', 'system-observe'), returncode=0)
CompletedProcess(args=('snapctl', 'is-connected', 'gpio'), returncode=1)

I got the | echo $? from the docs https://snapcraft.io/docs/using-snapctl
I guess this needs to be corrected.

this is not proper shell (the echo returns the value from the pipe, not from the command)

you want:

snapctl is-connected network; echo $?

like:

$ ls nonexistent >/dev/null 2>&1 | echo $?
0
$ ls nonexistent >/dev/null 2>&1; echo $?
2
$ ls >/dev/null 2>&1; echo $?
0

Looks like the docs have now been updated. Thanks for bringing this to our attention, and sorry it misled you.

2 Likes

No problem. Thanks for helping me out!