Restart ubuntu 20.04 LTS from a strictly confined snap

Hi. I have a snap that is strictly confined. I am trying to restart the system using a shell command suggested by @ogra in one of the threads.

dbus-send --system --print-reply \
                    --dest=org.freedesktop.login1 /org/freedesktop/login1 \
                    "org.freedesktop.login1.Manager.Reboot" boolean:true

But I am getting this error, every time this command is executed.

The error


dbus[3533]: arguments to dbus_message_new_method_call() were incorrect, assertion "iface == NULL || _dbus_check_is_valid_interface (iface)" failed in file ../../../dbus/dbus-message.c line 1367.
This is normally a bug in some application using the D-Bus library.

  D-Bus not built with -rdynamic so unable to print a backtrace

Any help would be really appreciated :slightly_smiling_face: Thanks in advance

is the shutdown interface connected ?

@ogra Thanks for the reply. Yes, it is connected. After installing the snap, I used the command

sudo snap connect snap_name:shutdown core:shutdown

And also I listed shutdown in the plugs section

plugs:
  - shutdown

Well, i only have used this command on Ubuntu Core devices yet (i.e. from kiosk snaps running as daemons), so i don’t know if on a classic install something like policykit gets in the way and it might also only work when the app is run as root (i.e. as a service or via sudo) … perhaps @jamesh has an idea …

Thanks. I will wait for @jamesh to reply in this thread. I hope we will find a solution to the error.

That doesn’t look like something that polkit could cause: it looks like a client-side error rather than an error from logind. The error in question says that the interface name passed to dbus-send (which would be everything before the final dot in the first positional argument) is not a valid D-Bus interface name.

What you’ve pasted looks correct though, so it might help if you could provide a bit more context to how you’re calling this.

Hi @jamesh. I have created a shell script named restart, converted it into an executable, and then calling the executable when certain conditions are met.

Here is how I call the script

import os
os.system("$SNAP/restart")

The shell script

#!/bin/sh

dbus-send --system --print-reply  --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.Reboot" boolean:true

And yes, the app from which I am calling the shell script is strictly confined.

A few things to try then:

  1. does the script work when run outside of confinement?
  2. can you run the script directly within confinement? This could be done by running snap run --shell myapp, which will drop you to a shell running within the same sandbox your app would. From there you could try executing $SNAP/restart.

For the first part, yes it works outside confinement. I can restart the system by using

./restart

Give me sometime. Let me try the second part and I will get back to you. Thanks

I am getting permission denied error when trying to run the script within the confinement

user@user-Mini-PC:/snap/snap_name/x1$ $SNAP/restart
bash: /snap/snap_name/x1/restart: Permission denied

TBH this looks more like your script does not end up being executable inside the snap … also make sure to use snap run --shell <snapname>.<app> when wanting to test inside confinement

Hi @ogra. Sorry for the late reply. If it does end up as an executable, then I think it should at least work if I use the command

bash restart

Well, I tried the command above and I got the same error as mentioned above.

dbus[3533]: arguments to dbus_message_new_method_call() were incorrect, assertion "iface == NULL || _dbus_check_is_valid_interface (iface)" failed in file ../../../dbus/dbus-message.c line 1367.
This is normally a bug in some application using the D-Bus library.

  D-Bus not built with -rdynamic so unable to print a backtrace

have you tried just calling the dbus-send command directly from within a snap run --shell ... environment ?

yes, it works. I tested it just now. At first I used the command

sudo snap run --shell snap_name.app

And then directly ran the dbus-send command from the terminal

dbus-send --system --print-reply  --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.Reboot" boolean:true

And my pc restarted.

so it has definitely something to do with the wrapping script …

I will test some more ways to call the shell script and try out other approaches. I will get back to you If I find something. Thanks for looking into my issue.

Link to thread: Strict Confined Snap System Reboot? How To w/Python

ogra your amazing!!! You solved our reboot/shutdown issues that we have been fighting snap with for weeks. It’s working great!

1 Like