python-dbus: org.freedesktop.DBus.Error.AccessDenied: Cannot access system DBUS over Python on Linux

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.

New contributor

antonpoll is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật