如何允许非 root systemd 服务使用 dbus 进行 BLE 操作

Tra*_*ggs 10 d-bus bluetooth systemd bluez

我一直在制作一个 BLE 外设的原型,它在一个像树莓一样的小板上以 root 身份运行。现在我正在强化事情并将 BLE 应用程序分区给非 root 用户。因此,我已将应用程序的 systemd 服务文件更改为如下所示:

[Unit]
Description=BLE Peripheral

[Service]
Type=simple
ExecStart=/usr/bin/python3 -u /opt/myPeripheral/bleMainloop
WorkingDirectory=/opt/myPeripheral
StandardOutput=journal
Restart=on-failure
User=blePeripheral

[Install]
WantedBy=multi-user.target 
Run Code Online (Sandbox Code Playgroud)

添加了UserblePeripheral用户身份运行的字段后,它现在无法启动,原因如下:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.AccessDenied: Rejected send message, 2 matched rules; type="method_call", sender=":1.6797" (uid=107 pid=17300 comm="/usr/bin/python3 -u /opt/pilot/bleMainloop ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination=":1.2" (uid=0 pid=1373 comm="/usr/lib/bluetooth/bluetoothd -d -E --noplugin=* “)
Run Code Online (Sandbox Code Playgroud)

认为我需要做的是以某种方式允许dbus此非 root 用户的某些用途。我看到有一个bluetooth.confin /etc/dbus-1/system.d。我是否需要调整此文件中的某些内容以允许我的应用程序仍然使用 BLE DBus 服务?

Tra*_*ggs 11

虽然从技术上讲@Mark 的回答回答了如何调整 dbus 蓝牙配置文件以实现我想要的问题,但我在查看该文件后注意到了一些我希望在发布之前注意到的内容。该bluetooth组可以访问总线。所以对我来说更简单(也许更正确?)的事情就是将我的非 root 用户添加到蓝牙组。这也可以让事情正常工作。

  • 在将用户“pi”添加到“蓝牙”组后,我必须“sudo systemctl restart dbus”才能使其正常工作。 (2认同)

Mar*_*erg 8

在 中/etc/dbus-1/system.d/bluetooth.conf,尝试添加以下内容:

<policy user="blePeripheral">
  <allow own="org.bluez"/>
  <allow send_destination="org.bluez"/>
  <allow send_interface="org.bluez.GattCharacteristic1"/>
  <allow send_interface="org.bluez.GattDescriptor1"/>
  <allow send_interface="org.freedesktop.DBus.ObjectManager"/>
  <allow send_interface="org.freedesktop.DBus.Properties"/>
</policy>
Run Code Online (Sandbox Code Playgroud)

然后重启dbus服务:

systemctl restart dbus
Run Code Online (Sandbox Code Playgroud)