understanding the g_bus_own_name_on_connection() method

David Zeuthen zeuthen at gmail.com
Tue Jul 30 11:53:52 PDT 2013


On Tue, Jul 30, 2013 at 11:32 AM, Bogdan Lotko <boguslaw.lotko at chello.at>wrote:

>  Hello David,
> Thank you for the answer and yes, I've read the documentation.
> Though I still don't understand how shall I proceed to achieve my goal
> i.e. *atomic acquirement* of service name on D-Bus.
> as I wrote the
> id = g_bus_own_name_on_connection(...);
> wait_for_handler(s)
> is for me rather strange solution, beside of this it creates a time window
> where other application can register the name.

What's so strange about it? The exact same window exists with libdbus ...
that is, if you're using dbus_bus_request_name() - the only difference is
that g_dbus_own_name() gives is non-blocking and dbus_bus_request_name() is

> Shall I really wait for these callbacks? The documentation states
> "callbacks will be invoked after calling this function" - does it mean
> immediately after calling this function?

No, of course not. They're called from the main loop when the response from
the D-Bus daemon comes in.

>  What I still don't have my main loop started and the decision if to start
> it depends on result of this method?
> For me the simple and clean solution would be:
> returns 0 - failure name already on use
> returns > 0 - OK success name acquired

The way g_bus_own_name() is designed, is that it _explicitly_ requires the
use of a main-loop. Why? Because the philosophy is that sync, blocking APIs
- such as dbus_bus_request_name() - are bad. By only providing an async
API, it's harder for programmers to screw up and write bad programs that
block indefinitely ... or at least, up to 25 seconds which is timeout used
in this case.

It's not a big deal. You really just move the everything but g_main_loop()
out of main() and into on_name_acquired(). That's it. It's cleaner that way.

If you don't like this - but you really should - you can always use
g_dbus_connection_call_sync() and invoke the RequstName() method yourself.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20130730/a4283d7f/attachment.html>

More information about the dbus mailing list