diwic / dbus-rs

D-Bus binding for the Rust language
Other
589 stars 133 forks source link

How to ensure that libdbus-sys compiles with X11 for dbus-daemon autolaunch on build #448

Closed zachfeldman closed 1 year ago

zachfeldman commented 1 year ago

When I try to run this example from this package https://github.com/diwic/dbus-rs/blob/366a6dca3d20745f5dcfa006b1b1311c376d420e/dbus/examples/monitor.rs with cargo run everything works great!

When I cargo build though and try to run as a systemd service, I get this error: Using X11 for dbus-daemon autolaunch was disabled at compile time, set your DBUS_SESSION_BUS_ADDRESS instead (org.freedesktop.DBus.Error.NotSupported)'

Ok, fair enough. But how can I compile libdbus-sys with this feature enabled? I tried turning on the vendored feature in both this crate and the one explicitly in Cargo.toml but it gave me the same error. My Cargo.toml:

<redacted>
[dependencies]
dbus = { version = "0.9.7", features = ["vendored"] }

[dependencies.libdbus-sys]
default-features = false
features = ["vendored"]
version = "0.2.5"

If that's impossible for some reason, I've also tried setting DBUS_SESSION_BUS_ADDRESS to unix:path=/run/user/1000/bus after running export DBUS_SESSION_BUS_ADDRESS=$(grep -P -o -a "(?<=DBUS_SESSION_BUS_ADDRESS=).+?(?=\x00)" /proc/"$(pgrep gnome-session | head -1)"/environ) following some advice from this SO thread https://stackoverflow.com/questions/41242460/how-to-export-dbus-session-bus-address. I got this error:

D-Bus connection failed: D-Bus error: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken. (org.freedesktop.DBus.Error.NoReply)'

I also tried setting DBUS_SESSION_BUS_ADDRESS to unix:path=/run/dbus/system_bus_socket, which the program seemed to accept, but which didn't seem to accept any messages or show them as my experiments with cargo run did.

If anyone knows how I can just ensure that libdbus-sys just compiles with this option on, that would be great, as all of my use cases are in X11 environments. Thanks for your help!

diwic commented 1 year ago

Could you show your systemd unit file? Not sure exactly how systemd does things but I suspect x11 is not the way here.

zachfeldman commented 1 year ago

Sure @diwic , good point, maybe I don't really want to access DBus through X11 in a systemd service, that might be more appropriate for something run under my windowed session...heres my unit file:

[Unit]
Description=This program monitors dbus for messages and makes a connected USB device react to them

[Service]
ExecStart=/usr/bin/my-outputted-bin

[Install]
WantedBy=multi-user.target

it is pretty simple.

diwic commented 1 year ago

So; this looks like a system service rather than a session service, right? Can you double check that you're connecting to the system bus and not the session bus? You will not have a session bus in multi-user.target.

zachfeldman commented 1 year ago

Thanks for pointing me in the right direction @diwic ! I actually did mean to make a session service but I wasn't as familiar with systemds options for setting one up. I've familiarized myself and all is working as expected now. Super appreciate you!