Search code examples
cglibdbusgio

Check if D-Bus object exists


I am currently talking to ConsoleKit with GDBus. I used the ConsoleKit2 XML files and gdbus-codegen to generate the code. Everything is working fine. But how can I check if an object exists? For example I want to see if there is a /org/freedesktop/ConsoleKit/Session2 (just an example, I know I could enumerate all Sessions in the Seat object).

I tried using the org.freedesktop.DBus.Peer.Ping function, but that will return

dbus-send --system --print-reply --reply-timeout=2000 --type=method_call --dest=org.freedesktop.DBus /org/freedesktop/ConsoleKit/Seat1  org.freedesktop.DBus.Peer.Ping

Error org.freedesktop.DBus.Error.AccessDenied: Rejected send message, 1 matched rules; type="method_call", sender=":1.168" (uid=1000 pid=18279 comm="dbus-send --system --print-reply --reply-timeout=2") interface="org.freedesktop.DBus.Peer" member="Ping" error name="(unset)" requested_reply="0" destination="org.freedesktop.DBus" (bus)

Solution

  • You have several options, listed in order from most preferable to least preferable:

    1. Enumerate all the sessions in the seat object using GetSessions().
    2. Try and call the method you want on that session’s object path and see if it fails with an error from org.freedesktop.DBus.Error.
    3. Call the Introspect() method on /org/freedesktop/ConsoleKit and parse the <node> elements from the resulting XML blob to see the current object path hierarchy.

    The first option is probably the easiest to implement, and is how you’re intended to use the ConsoleKit API. Note that seat and session numbering is not deterministic, so you shouldn’t just hard-code a session object path in your code, since that path might change on future boots.

    Also note that, as the ConsoleKit website says, ConsoleKit is deprecated in favour of systemd-logind, which you should consider using instead.