hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
185 stars 73 forks source link

DBus daemon and dbus-send tool #207

Closed tory-kk closed 1 year ago

tory-kk commented 1 year ago

Hello, Currently, we have a setup with Java DBus daemon, Java service that exports object, and Java client that sends messages to the exported object. And it works like a charm.

However, we recently noticed that dbus-send tool does not work properly - it doesn't receive a response from Java daemon.

An example: We tried to send messages to the daemon via command-line and got the following response:

$ dbus-send --bus="tcp:host=localhost,port=36139,guid=fc8572521dcc41b9a927645066366912" --print-reply --reply-timeout=600000 --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.StartUnit string:'test.service' string:'replace'

Error org.freedesktop.DBus.Error.Disconnected: Connection was disconnected before a reply was received

At the same time if the request is sent without asking for response the command-line succeeds:

$ dbus-send --bus="tcp:host=localhost,port=36139,guid=fc8572521dcc41b9a927645066366912" --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.StartUnit string:'test.service' string:'replace' && echo "OK"

OK

If we send command to the system DBus, response from the requested service is received:

$ dbus-send --system --print-reply --reply-timeout=600000 --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.RestartUnit string:'puppet.service' string:'replace'

method return time=1673963470.955449 sender=:1.1 -> destination=:1.276 serial=5660 reply_serial=2
   object path "/org/freedesktop/systemd1/job/6596"

Is there some miss-configuration of the daemon on our side? Are we doing something wrong or missing something? Please help us to solve the issue.

Attaching a gist with simple project with the daemon and exporter: link

hypfvieh commented 1 year ago

As far as I see right now, there seem to be multiple issues with this setup. I'll investigate that further and report back later...

hypfvieh commented 1 year ago

Alright... after a 2 days of investigation and trail & error I finally got it.

This setup has two major problems, one is a bug in dbus-java, the other is the usage of dbus-send.

First the bug: When using dbus-java as DBusDaemon, messages which has to be forwarded from one application to another (app1 exports something app2 wants to call) failed due to broken message headers.

This was a really difficult to find and fix bug. When the DBusDaemon receives the result of the call from app1 it changes the message header and sends the message to the requester (app2). The header is updated when setSource(String) is called on the message. This code is pretty difficult to understand and it seems to be broken since the beginning of this project. In that method, the message is cleared and re-filled after the header has been changed. The changed header was wrong and the resulting message was unreadable by dbus-send.

The second issue is your usage of dbus-send. When using dbus-send with --address option, it does not do proper handshaking. It just sends the method call you want to run but omits sending the "Hello" message. To get this to work, you have to set the DBUS_SESSION_BUS_ADDRESS environment variable. Example: DBUS_SESSION_BUS_ADDRESS="tcp:host=localhost,port=43540,guid=c6d8a3d70fdfcc80ce03510ee263acb8" dbus-send --print-reply --reply-timeout=600000 --type=method_call --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.StartUnit string:'test.service' string:'replace'

tory-kk commented 1 year ago

Thank you very much! Everything works perfectly. :) Are there any estimates about the release of a new version?

hypfvieh commented 1 year ago

Nope, no ETA for any release. Maybe in a few weeks somewhere in february/march depending on how much issues are arsing until then.