DBus - hpaluch/hpaluch.github.io GitHub Wiki
D-bus
Is important communication bus used by systemd and many other programs.
[!WARNING] Just started - very quick peek into d-bus...
There are 2 main scopes:
- System (single instance of system)
- Session (logged user session)
To test D-Bus - install in openSUSE LEAP 15.6
sudo zypper in d-feet python3-dasbus
Where:
d-feet
is GUI d-bus browser (and can also invoke Methods)python3-dasbus
is favorite python library.
Examples
Let's start with getting current hostname
Related system files are in:
$ find /usr/share/dbus-1/ -name '*hostname*'
/usr/share/dbus-1/system-services/org.freedesktop.hostname1.service
/usr/share/dbus-1/interfaces/org.freedesktop.hostname1.xml
/usr/share/dbus-1/system.d/org.freedesktop.hostname1.conf
And polkit permissions are in /usr/share/polkit-1/actions/org.freedesktop.hostname1.policy
Here is source called dbus-hostname.py
:
#!/usr/bin/env python3
# Get hostname via dbus, see https://dasbus.readthedocs.io/en/latest/examples.html
from dasbus.connection import SystemMessageBus
bus = SystemMessageBus()
proxy = bus.get_proxy(
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1"
)
print(proxy.Hostname)
You can run it simply by invoking with path: ./dbus-hostname.py
But wait!
- there is also Method
Describe
that returns detailed system description. - I found that interface using GUI tool
d-feet
- example file
dbus-hostname-describe.py
#!/usr/bin/env python3
# Calls hostname1.Describe
import json
from dasbus.connection import SystemMessageBus
bus = SystemMessageBus()
proxy = bus.get_proxy(
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1"
)
json_str = proxy.Describe()
json_obj = json.loads(json_str)
print( json.dumps(json_obj,indent=2) )
I got surprisingly detailed output (available to all applications!):
{
"Hostname": "cubi",
"StaticHostname": "cubi",
"PrettyHostname": null,
"DefaultHostname": "localhost",
"HostnameSource": "static",
"IconName": "computer-desktop",
"Chassis": "desktop",
"Deployment": null,
"Location": null,
"KernelName": "Linux",
"KernelRelease": "6.4.0-150600.23.30-default",
"KernelVersion": "#1 SMP PREEMPT_DYNAMIC Sat Dec 7 08:37:53 UTC 2024 (8c25a0a)",
"OperatingSystemPrettyName": "openSUSE Leap 15.6",
"OperatingSystemCPEName": "cpe:/o:opensuse:leap:15.6",
"OperatingSystemHomeURL": "https://www.opensuse.org/",
"HardwareVendor": "Micro-Star International Co., Ltd.",
"HardwareModel": "PRO ADL-U Cubi 5 _MS-XXXX_",
"HardwareSerial": null,
"FirmwareVersion": "X.XX",
"FirmwareVendor": "American Megatrends International, LLC.",
"FirmwareDate": 9999999999999999,
"ProductUUID": null
}
I redacted some fields...