I am trying to create a Python script that I can run as a user systemd
service on a Raspberry Pi running Raspberry Pi OS Bookworm. The script’s purpose is to listen to system DBUS messages coming from BlueZ in order to notice connection/disconnection events from bluetooth devices.
The script works fine when I run it as sudo
, but when I run it without, I get the following error message:
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.AccessDenied: rejected attempt to call AddMatch by connection :1.117 (uid=1000 pid=1811 comm="python connect_listener.py") with uid 1000
I don’t know if the following might be related, but when I run dbus-monitor --system
in the terminal, I also get this warning, however the monitoring itself works:
dbus-monitor: unable to enable new-style monitoring: org.freedesktop.DBus.Error.AccessDenied: "rejected attempt to call BecomeMonitor by connection :1.118 (uid=1000 pid=1829 comm="dbus-monitor --system") with uid 1000". Falling back to eavesdropping.
The Python 3 script looks like this:
#! /usr/bin/python3
import os
import dbus
from gi.repository import GLib
import dbus.mainloop.glib
def msg_cb(bus, msg):
...
[callback stuff]
...
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
string = "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',eavesdrop='true'"
bus.add_match_string(string)
bus.add_message_filter(msg_cb)
mainloop = GLib.MainLoop()
mainloop.run()
What I tried so far
I assumed it has to do with the rights assigned to the pi
user regarding listening on the system DBUS, so I tried fixing that. I found this thread and added a .conf
file under /etc/dbus-1/system.d/
with this content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<policy user="pi">
<allow eavesdrop="true" />
<allow eavesdrop="true" send_destination="*" />
<allow send_destination="org.freedesktop.DBus"
send_interface="org.freedesktop.DBus.Monitoring"/>
</policy>
</busconfig>
I also wrote this into /etc/dbus-1/system-local.conf
in case the previous was being overwritten by other .conf
files, as I read that theis file is the last one being read in.
Finally, I also found this issue and made sure the pi
user is a member of the bluetooth
group.
I am very thankful for any help or input. It’s probably something minor that I just don’t know about.
antonpoll is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.