D-Bus

From miki
Revision as of 20:10, 26 April 2022 by Mip (talk | contribs) (→‎Reference)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Reference

D-Feet is an easy to use D-Bus debugger.
sudo apt install d-feet

commands

# View bluetooth bus
busctl tree 'org.bluez'
# └─/org
#   └─/org/bluez
#     ├─/org/bluez/hci0
#     │ ├─/org/bluez/hci0/dev_00_1F_20_97_ED_C8
#     │ ├─/org/bluez/hci0/dev_38_18_4C_4B_6A_3A
#     │ │ └─/org/bluez/hci0/dev_38_18_4C_4B_6A_3A/sep1
#     │ │   └─/org/bluez/hci0/dev_38_18_4C_4B_6A_3A/sep1/fd0
#     │ └─/org/bluez/hci0/dev_E8_6B_02_1F_D9_FF
#     └─/org/bluez/test

# Inspect a device
busctl introspect 'org.bluez' '/org/bluez/hci0/dev_38_18_4C_4B_6A_3A'
# NAME                                TYPE      SIGNATURE RESULT/VALUE                             FLAGS
# org.bluez.Battery1                  interface -         -                                        -
# .Percentage                         property  y         20                                       emits-change
# .Source                             property  s         -                                        emits-change
# org.bluez.Device1                   interface -         -                                        -
# .CancelPairing                      method    -         -                                        -
# .Connect                            method    -         -                                        -
# .ConnectProfile                     method    s         -                                        -
# .Disconnect                         method    -         -                                        -
# ...

Using D-Bus

Introspect D-Bus

Most client connected to D-Bus implement introspection [1].


First, we can query the list of services:

# Query session bus
dbus-send           --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames
dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames
# Query system bus
dbus-send --system --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames

For instance, to find dnsmasq service:

dbus-send --system --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames \
    | grep dnsmasq
#       string "org.freedesktop.NetworkManager.dnsmasq"


We can use the org.freedesktop.DBus.Introspectable.Introspect method to get all methods of a given service, but for this we need both the service name and path, which is not listed above. For most services, the path can be obtained by converting . into /:

# Get the DBUS interface:
dbus-send --session           \
  --dest=org.freedesktop.DBus \
  --type=method_call          \
  --print-reply               \
  /org/freedesktop/DBus       \
  org.freedesktop.DBus.Introspectable.Introspect

For dnsmasq (system D-BUS), the path is /uk/org/thekelleys/dnsmasq:

# Get dnsmasq D-Bus interface
sudo dbus-send --system  --dest=org.freedesktop.NetworkManager.dnsmasq --type=method_call --print-reply /uk/org/thekelleys/dnsmasq org.freedesktop.DBus.Introspectable.Introspect
# ...
#     <method name="SetServers">
#       <arg name="servers" direction="in" type="av"/>
#     </method>
#     <method name="SetDomainServers">
#       <arg name="servers" direction="in" type="as"/>

Send command via D-Bus

For instance, to set the DNS server in dnsmasq [2]:

dbus-send --system --print-reply \
    --dest=org.freedesktop.NetworkManager.dnsmasq \
    /uk/org/thekelleys/dnsmasq \
    uk.org.thekelleys.SetDomainServers \
    "array:string:/my.domain.com/128.129.130.131,8.8.8.8"

Use command that requires D-Bus

Some commands require dbus (eg. gcp), or we get an error:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

Use dbus-launch:

dbus-launch gcp -r dir1 dir2