Can I reboot OS from my snap package?

My Python application uses os. system(‘reboot’). I connected the shutdown and power-control plugs. I received [Python Controller ERROR]: sh: 1: reboot: Permission denied.

Time: May 10 08:10:30
Log: apparmor="DENIED" operation="exec" profile="snap.aps.daemon" name="/usr/bin/systemctl" pid=5442 comm="sh" requested_mask="x" denied_mask="x" fsuid=0 ouid=0
File: /usr/bin/systemctl (exec)
Suggestions:
* adjust snap to ship 'systemctl'
* adjust program to use relative paths if the snap already ships 'systemctl'

The shutdown interface gives you access to dbus to issue a shutdown or reboot command through a dbus call… there are plenty of posts here in the forum with example commands…

I searched before writing but not found a thing which is work for me.

See

First tried

stage-packages:
  - python3-dbus

and I received the error ModuleNotFoundError: No module named ‘dbus’

Then tried to install dbus-python but I received the error No package 'dbus-1' found I added

build-packages:
 - build-essential 
 - libdbus-glib-1-dev
 - libgirepository1.0-dev
 - libdbus-1-dev

And now I’m trying to fix this

[32/32] Linking target test/test-import-repeatedly
      FAILED: test/test-import-repeatedly
      cc  -o test/test-import-repeatedly test/test-import-repeatedly.p/import-repeatedly.c.o -L/root/parts/python/install/lib -L/root/parts/python/install/usr/lib -L/root/parts/python/install/lib/aarch64-linux-gnu -L/root/parts/python/install/usr/lib/aarch64-linux-gnu -L/root/stage/usr/lib -L/root/stage/usr/lib/aarch64-linux-gnu -Wl,--as-needed -Wl,--no-undefined -Wl,-O1 -isystem/root/parts/python/install/usr/include -isystem/root/parts/python/install/usr/include/aarch64-linux-gnu -Wl,--start-group -lpython3.11 -Wl,--end-group
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `init_condattr':
      /root/parts/python/build/Python/thread_pthread.h:147: undefined reference to `pthread_condattr_setclock'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_tss_create':
      /root/parts/python/build/Python/thread_pthread.h:893: undefined reference to `pthread_key_create'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_start_new_thread':
      /root/parts/python/build/Python/thread_pthread.h:289: undefined reference to `pthread_create'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:306: undefined reference to `pthread_detach'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:270: undefined reference to `pthread_attr_setstacksize'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_tss_delete':
      /root/parts/python/build/Python/thread_pthread.h:910: undefined reference to `pthread_key_delete'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_allocate_lock':
      /root/parts/python/build/Python/thread_pthread.h:388: undefined reference to `sem_init'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:388: undefined reference to `sem_init'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_acquire_lock_timed':
      /root/parts/python/build/Python/thread_pthread.h:497: undefined reference to `sem_wait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:497: undefined reference to `sem_wait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:494: undefined reference to `sem_trywait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:483: undefined reference to `sem_clockwait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:497: undefined reference to `sem_wait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:483: undefined reference to `sem_clockwait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:494: undefined reference to `sem_trywait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:497: undefined reference to `sem_wait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:494: undefined reference to `sem_trywait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:483: undefined reference to `sem_clockwait'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_release_lock':
      /root/parts/python/build/Python/thread_pthread.h:563: undefined reference to `sem_post'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_tss_get':
      /root/parts/python/build/Python/thread_pthread.h:927: undefined reference to `pthread_getspecific'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_tss_set':
      /root/parts/python/build/Python/thread_pthread.h:919: undefined reference to `pthread_setspecific'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_free_lock':
      /root/parts/python/build/Python/thread_pthread.h:413: undefined reference to `sem_destroy'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_create_key':
      /root/parts/python/build/Python/thread_pthread.h:820: undefined reference to `pthread_key_create'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Python/thread_pthread.h:825: undefined reference to `pthread_key_delete'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_delete_key':
      /root/parts/python/build/Python/thread_pthread.h:839: undefined reference to `pthread_key_delete'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_delete_key_value':
      /root/parts/python/build/Python/thread_pthread.h:847: undefined reference to `pthread_setspecific'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_set_key_value':
      /root/parts/python/build/Python/thread_pthread.h:855: undefined reference to `pthread_setspecific'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_get_key_value':
      /root/parts/python/build/Python/thread_pthread.h:866: undefined reference to `pthread_getspecific'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `_pythread_pthread_set_stacksize':
      /root/parts/python/build/Python/thread_pthread.h:783: undefined reference to `pthread_attr_setstacksize'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(thread.o): in function `PyThread_tss_delete':
      /root/parts/python/build/Python/thread_pthread.h:910: undefined reference to `pthread_key_delete'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(faulthandler.o): in function `faulthandler_thread':
      /root/parts/python/build/./Modules/faulthandler.c:609: undefined reference to `pthread_sigmask'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(signalmodule.o): in function `signal_pthread_kill_impl':
      /root/parts/python/build/./Modules/signalmodule.c:1326: undefined reference to `pthread_kill'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(signalmodule.o): in function `signal_pthread_sigmask_impl':
      /root/parts/python/build/./Modules/signalmodule.c:1014: undefined reference to `pthread_sigmask'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(complexobject.o): in function `_Py_c_abs':
      /root/parts/python/build/Objects/complexobject.c:208: undefined reference to `hypot'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(complexobject.o): in function `_Py_c_pow':
      /root/parts/python/build/Objects/complexobject.c:144: undefined reference to `hypot'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/complexobject.c:145: undefined reference to `pow'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/complexobject.c:146: undefined reference to `atan2'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/complexobject.c:149: undefined reference to `exp'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/complexobject.c:150: undefined reference to `log'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/complexobject.c:150: undefined reference to `sincos'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(floatobject.o): in function `float_rem':
      /root/parts/python/build/Objects/floatobject.c:655: undefined reference to `fmod'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(floatobject.o): in function `_float_div_mod':
      /root/parts/python/build/Objects/floatobject.c:675: undefined reference to `fmod'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(floatobject.o): in function `float_pow':
      /root/parts/python/build/Objects/floatobject.c:847: undefined reference to `pow'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/floatobject.c:824: undefined reference to `fmod'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/floatobject.c:847: undefined reference to `pow'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/floatobject.c:788: undefined reference to `fmod'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/floatobject.c:798: undefined reference to `fmod'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(longobject.o): in function `PyLong_FromString':
      /root/parts/python/build/Objects/longobject.c:2441: undefined reference to `log'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/Objects/longobject.c:2441: undefined reference to `log'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(posixmodule.o): in function `os_login_tty_impl':
      /root/parts/python/build/./Modules/posixmodule.c:7425: undefined reference to `login_tty'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(posixmodule.o): in function `os_openpty_impl':
      /root/parts/python/build/./Modules/posixmodule.c:7328: undefined reference to `openpty'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(posixmodule.o): in function `os_forkpty_impl':
      /root/parts/python/build/./Modules/posixmodule.c:7479: undefined reference to `forkpty'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(timemodule.o): in function `time_pthread_getcpuclockid':
      /root/parts/python/build/./Modules/timemodule.c:354: undefined reference to `pthread_getcpuclockid'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/install/usr/lib/libpython3.11.a(dynload_shlib.o): in function `_PyImport_FindSharedFuncptr':
      /root/parts/python/build/./Python/dynload_shlib.c:80: undefined reference to `dlopen'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/./Python/dynload_shlib.c:109: undefined reference to `dlsym'
      /root/parts/python/install/usr/bin/ld: /root/parts/python/build/./Python/dynload_shlib.c:86: undefined reference to `dlerror'
      collect2: error: ld returned 1 exit status
      ninja: build stopped: subcommand failed.
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

When I added and

environment:
PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:$PYTHONPATH

I stopped receiving ModuleNotFoundError: No module named ‘dbus’ but now on import dbus I received

import dbus
File "/snap/aps/x1/usr/lib/python3/dist-packages/dbus/__init__.py", line 73, in <module>
import dbus.types as types
File "/snap/aps/x1/usr/lib/python3/dist-packages/dbus/types.py", line 8, in <module>
from _dbus_bindings import (
ModuleNotFoundError: No module named '_dbus_bindings'

are you using a newer python than in the archive for your app ? or cpython ? this looks like a version mistmatch …

I’m using Python 3.11.

parts:
  aps:
    plugin: dump
    source: ./application.deb
    source-type: deb
    stage-packages:
      - libnspr4
      - libnss3
      - libatk-bridge2.0-0
      - libcups2
      - libgtk-3-0
      - libasound2
      - libpulse0

  python:
    plugin: autotools
    source: https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
    autotools-configure-parameters:
      - --enable-optimizations
      - --prefix=/usr
    build-packages:
      - build-essential
      - gdb
      - lcov
      - pkg-config
      - libbz2-dev
      - libffi-dev
      - libgdbm-dev
      - libgdbm-compat-dev
      - liblzma-dev
      - libncurses5-dev
      - libreadline6-dev
      - libsqlite3-dev
      - libssl-dev
      - lzma
      - lzma-dev
      - tk-dev
      - uuid-dev
      - zlib1g-dev
      - libglib2.0-dev
      - libgirepository1.0-dev
      - gcc
      - libcairo2-dev
      - python3-dev
      - gir1.2-gtk-3.0
      - libnm-dev
      - libusb-1.0-0-dev
      - libdbus-glib-1-dev
      - libdbus-1-dev
      - cmake
    override-stage: |
      # We want the latest pip to be able to install pyproject.toml based projects
      PYTHONUSERBASE="$SNAPCRAFT_STAGE/usr" python3.11 -m pip install --user --upgrade pip wheel

      PYTHONUSERBASE="$SNAPCRAFT_STAGE/usr" python3.11 -m pip install comtypes ConfigUpdater libusb1 osascript psutil pycaw requests sdnotify setuptools SystemdUnitParser winshell pycairo PyGObject dbus-python

      snapcraftctl stage
    stage-packages:
      - libfontconfig1
      - libfreetype6
      - libgdbm-compat4
      - libgdbm6
      - libpng16-16
      - libtcl8.6
      - libtk8.6
      - libx11-6
      - libxau6
      - libxcb1
      - libxdmcp6
      - libxext6
      - libxft2
      - libxrender1
      - libxss1
      - libglib2.0-0
      - libffi7
      - libgirepository1.0-dev
      - gcc
      - libcairo2-dev
      - pkg-config
      - python3-dev
      - gir1.2-gtk-3.0
      - gir1.2-nm-1.0
      - libusb-1.0-0-dev
    prime:
      - -etc
      - -lib/systemd
      - -usr/bin/2to3
      - -usr/bin/2to3-3.11
      - -usr/bin/deb-systemd-helper
      - -usr/bin/deb-systemd-invoke
      - -usr/bin/easy_install-3.11
      - -usr/bin/idle3
      - -usr/bin/idle3.11
      - -usr/bin/pydoc3
      - -usr/bin/pydoc3.11
      - -usr/bin/python3.11-config
      - -usr/bin/python3-config
      - -usr/bin/uuidgen
      - -usr/include
      - -usr/lib/*.a
      - -usr/lib/pkgconfig
      - -usr/lib/python3.11/test
      - -usr/sbin
      - -usr/share
      - -var

When I add

stage-packages:
  - python3-dbus

On Ubuntu Core, I found only this

./snap/aps/x1/usr/lib/python3/dist-packages/_dbus_bindings.cpython-38-aarch64-linux-gnu.so
./snap/aps/x1/usr/lib/python3/dist-packages/_dbus_glib_bindings.cpython-38-aarch64-linux-gnu.so

I think that the problem is cpython-38 but don’t know how do I fix it

well, you are mixing different python versions, that won’t work i guess … what is the reason to use 3.11 instead of the available 3.8 ? from an application POV it should not make much of a difference …

Some things we use don’t work on 3.8.

I resolved my issue with os.system('dbus-send --system --print-reply "--dest=org.freedesktop.login1" /org/freedesktop/login1 org.freedesktop.login1.Manager.Reboot boolean:true'). I found out that there is a problem with the dbus-python 1.3.2 library build on python 3.11. This might be a solution but I haven’t tried it https://pypi.org/project/zymod/

2 Likes