1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include "gdbusutils.h"
28 #include "gdbusnameowning.h"
29 #include "gdbuserror.h"
30 #include "gdbusprivate.h"
31 #include "gdbusconnection.h"
37 * SECTION:gdbusnameowning
38 * @title: Owning Bus Names
39 * @short_description: Simple API for owning bus names
42 * Convenience API for owning bus names.
44 * <example id="gdbus-owning-names"><title>Simple application owning a name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-own-name.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
47 G_LOCK_DEFINE_STATIC (lock);
49 /* ---------------------------------------------------------------------------------------------------- */
53 PREVIOUS_CALL_NONE = 0,
54 PREVIOUS_CALL_ACQUIRED,
60 volatile gint ref_count;
62 GBusNameOwnerFlags flags;
64 GBusAcquiredCallback bus_acquired_handler;
65 GBusNameAcquiredCallback name_acquired_handler;
66 GBusNameLostCallback name_lost_handler;
68 GDestroyNotify user_data_free_func;
69 GMainContext *main_context;
71 PreviousCall previous_call;
73 GDBusConnection *connection;
74 gulong disconnected_signal_handler_id;
75 guint name_acquired_subscription_id;
76 guint name_lost_subscription_id;
80 gboolean needs_release;
83 static guint next_global_id = 1;
84 static GHashTable *map_id_to_client = NULL;
88 client_ref (Client *client)
90 g_atomic_int_inc (&client->ref_count);
95 client_unref (Client *client)
97 if (g_atomic_int_dec_and_test (&client->ref_count))
99 if (client->connection != NULL)
101 if (client->disconnected_signal_handler_id > 0)
102 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
103 if (client->name_acquired_subscription_id > 0)
104 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
105 if (client->name_lost_subscription_id > 0)
106 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
107 g_object_unref (client->connection);
109 if (client->main_context != NULL)
110 g_main_context_unref (client->main_context);
111 g_free (client->name);
112 if (client->user_data_free_func != NULL)
113 client->user_data_free_func (client->user_data);
118 /* ---------------------------------------------------------------------------------------------------- */
123 CALL_TYPE_NAME_ACQUIRED,
131 /* keep this separate because client->connection may
132 * be set to NULL after scheduling the call
134 GDBusConnection *connection;
136 /* set to TRUE to call acquired */
141 call_handler_data_free (CallHandlerData *data)
143 if (data->connection != NULL)
144 g_object_unref (data->connection);
145 client_unref (data->client);
150 actually_do_call (Client *client, GDBusConnection *connection, CallType call_type)
154 case CALL_TYPE_NAME_ACQUIRED:
155 if (client->name_acquired_handler != NULL)
157 client->name_acquired_handler (connection,
163 case CALL_TYPE_NAME_LOST:
164 if (client->name_lost_handler != NULL)
166 client->name_lost_handler (connection,
173 g_assert_not_reached ();
179 call_in_idle_cb (gpointer _data)
181 CallHandlerData *data = _data;
182 actually_do_call (data->client, data->connection, data->call_type);
187 schedule_call_in_idle (Client *client, CallType call_type)
189 CallHandlerData *data;
190 GSource *idle_source;
192 data = g_new0 (CallHandlerData, 1);
193 data->client = client_ref (client);
194 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
195 data->call_type = call_type;
197 idle_source = g_idle_source_new ();
198 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
199 g_source_set_callback (idle_source,
202 (GDestroyNotify) call_handler_data_free);
203 g_source_attach (idle_source, client->main_context);
204 g_source_unref (idle_source);
208 do_call (Client *client, CallType call_type)
210 /* only schedule in idle if we're not in the right thread */
211 if (g_main_context_get_thread_default () != client->main_context)
212 schedule_call_in_idle (client, call_type);
214 actually_do_call (client, client->connection, call_type);
218 call_acquired_handler (Client *client)
220 if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
222 client->previous_call = PREVIOUS_CALL_ACQUIRED;
223 if (!client->cancelled)
225 do_call (client, CALL_TYPE_NAME_ACQUIRED);
231 call_lost_handler (Client *client)
233 if (client->previous_call != PREVIOUS_CALL_LOST)
235 client->previous_call = PREVIOUS_CALL_LOST;
236 if (!client->cancelled)
238 do_call (client, CALL_TYPE_NAME_LOST);
243 /* ---------------------------------------------------------------------------------------------------- */
246 on_name_lost_or_acquired (GDBusConnection *connection,
247 const gchar *sender_name,
248 const gchar *object_path,
249 const gchar *interface_name,
250 const gchar *signal_name,
251 GVariant *parameters,
254 Client *client = user_data;
257 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
258 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
259 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
262 if (g_strcmp0 (signal_name, "NameLost") == 0)
264 g_variant_get (parameters, "(&s)", &name);
265 if (g_strcmp0 (name, client->name) == 0)
267 call_lost_handler (client);
270 else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
272 g_variant_get (parameters, "(&s)", &name);
273 if (g_strcmp0 (name, client->name) == 0)
275 call_acquired_handler (client);
282 /* ---------------------------------------------------------------------------------------------------- */
285 request_name_cb (GObject *source_object,
289 Client *client = user_data;
291 guint32 request_name_reply;
294 request_name_reply = 0;
297 result = g_dbus_connection_call_finish (client->connection,
302 g_variant_get (result, "(u)", &request_name_reply);
303 g_variant_unref (result);
308 switch (request_name_reply)
310 case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
311 /* We got the name - now listen for NameLost and NameAcquired */
312 call_acquired_handler (client);
314 client->needs_release = TRUE;
317 case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
318 /* Waiting in line - listen for NameLost and NameAcquired */
319 call_lost_handler (client);
321 client->needs_release = TRUE;
325 /* assume we couldn't get the name - explicit fallthrough */
326 case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
327 case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
328 /* Some other part of the process is already owning the name */
329 call_lost_handler (client);
335 /* start listening to NameLost and NameAcquired messages */
336 client->name_lost_subscription_id =
337 g_dbus_connection_signal_subscribe (client->connection,
338 "org.freedesktop.DBus",
339 "org.freedesktop.DBus",
341 "/org/freedesktop/DBus",
343 on_name_lost_or_acquired,
346 client->name_acquired_subscription_id =
347 g_dbus_connection_signal_subscribe (client->connection,
348 "org.freedesktop.DBus",
349 "org.freedesktop.DBus",
351 "/org/freedesktop/DBus",
353 on_name_lost_or_acquired,
358 client_unref (client);
361 /* ---------------------------------------------------------------------------------------------------- */
364 on_connection_disconnected (GDBusConnection *connection,
365 gboolean remote_peer_vanished,
369 Client *client = user_data;
371 if (client->disconnected_signal_handler_id > 0)
372 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
373 if (client->name_acquired_subscription_id > 0)
374 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
375 if (client->name_lost_subscription_id > 0)
376 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
377 g_object_unref (client->connection);
378 client->disconnected_signal_handler_id = 0;
379 client->name_acquired_subscription_id = 0;
380 client->name_lost_subscription_id = 0;
381 client->connection = NULL;
383 call_lost_handler (client);
386 /* ---------------------------------------------------------------------------------------------------- */
389 has_connection (Client *client)
391 /* listen for disconnection */
392 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
394 G_CALLBACK (on_connection_disconnected),
397 /* attempt to acquire the name */
398 g_dbus_connection_call (client->connection,
399 "org.freedesktop.DBus", /* bus name */
400 "/org/freedesktop/DBus", /* object path */
401 "org.freedesktop.DBus", /* interface name */
402 "RequestName", /* method name */
403 g_variant_new ("(su)",
406 G_VARIANT_TYPE ("(u)"),
407 G_DBUS_CALL_FLAGS_NONE,
410 (GAsyncReadyCallback) request_name_cb,
411 client_ref (client));
416 connection_get_cb (GObject *source_object,
420 Client *client = user_data;
422 client->connection = g_bus_get_finish (res, NULL);
423 if (client->connection == NULL)
425 call_lost_handler (client);
429 /* No need to schedule this in idle as we're already in the thread
430 * that the user called g_bus_own_name() from. This is because
431 * g_bus_get() guarantees that.
433 * Also, we need to ensure that the handler is invoked *before*
434 * we call RequestName(). Otherwise there is a race.
436 if (client->bus_acquired_handler != NULL)
438 client->bus_acquired_handler (client->connection,
443 has_connection (client);
446 client_unref (client);
449 /* ---------------------------------------------------------------------------------------------------- */
452 * g_bus_own_name_on_connection:
453 * @connection: A #GDBusConnection.
454 * @name: The well-known name to own.
455 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
456 * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
457 * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
458 * @user_data: User data to pass to handlers.
459 * @user_data_free_func: Function for freeing @user_data or %NULL.
461 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
464 * Returns: An identifier (never 0) that an be used with
465 * g_bus_unown_name() to stop owning the name.
470 g_bus_own_name_on_connection (GDBusConnection *connection,
472 GBusNameOwnerFlags flags,
473 GBusNameAcquiredCallback name_acquired_handler,
474 GBusNameLostCallback name_lost_handler,
476 GDestroyNotify user_data_free_func)
480 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
481 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
485 client = g_new0 (Client, 1);
486 client->ref_count = 1;
487 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
488 client->name = g_strdup (name);
489 client->flags = flags;
490 client->name_acquired_handler = name_acquired_handler;
491 client->name_lost_handler = name_lost_handler;
492 client->user_data = user_data;
493 client->user_data_free_func = user_data_free_func;
494 client->main_context = g_main_context_get_thread_default ();
495 if (client->main_context != NULL)
496 g_main_context_ref (client->main_context);
498 client->connection = g_object_ref (connection);
500 if (map_id_to_client == NULL)
502 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
504 g_hash_table_insert (map_id_to_client,
505 GUINT_TO_POINTER (client->id),
510 has_connection (client);
517 * @bus_type: The type of bus to own a name on.
518 * @name: The well-known name to own.
519 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
520 * @bus_acquired_handler: Handler to invoke when connected to the bus of type @bus_type or %NULL.
521 * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
522 * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
523 * @user_data: User data to pass to handlers.
524 * @user_data_free_func: Function for freeing @user_data or %NULL.
526 * Starts acquiring @name on the bus specified by @bus_type and calls
527 * @name_acquired_handler and @name_lost_handler when the name is
528 * acquired respectively lost. Callbacks will be invoked in the <link
529 * linkend="g-main-context-push-thread-default">thread-default main
530 * loop</link> of the thread you are calling this function from.
532 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
533 * callbacks will be invoked after calling this function - there are three
537 * @name_lost_handler with a %NULL connection (if a connection to the bus can't be made).
540 * @bus_acquired_handler then @name_lost_handler (if the name can't be obtained)
543 * @bus_acquired_handler then @name_acquired_handler (if the name was obtained).
546 * When you are done owning the name, just call g_bus_unown_name()
547 * with the owner id this function returns.
549 * If the name is acquired or lost (for example another application
550 * could acquire the name if you allow replacement or the application
551 * currently owning the name exits), the handlers are also invoked. If the
552 * #GDBusConnection that is used for attempting to own the name
553 * closes, then @name_lost_handler is invoked since it is no
554 * longer possible for other processes to access the process.
556 * You cannot use g_bus_own_name() several times for the same name (unless
557 * interleaved with calls to g_bus_unown_name()) - only the first call
560 * Another guarantee is that invocations of @name_acquired_handler
561 * and @name_lost_handler are guaranteed to alternate; that
562 * is, if @name_acquired_handler is invoked then you are
563 * guaranteed that the next time one of the handlers is invoked, it
564 * will be @name_lost_handler. The reverse is also true.
566 * If you plan on exporting objects (using e.g.
567 * g_dbus_connection_register_object()), note that it is generally too late
568 * to export the objects in @name_acquired_handler. Instead, you can do this
569 * in @bus_acquired_handler since you are guaranteed that this will run
570 * before @name is requested from the bus.
572 * This behavior makes it very simple to write applications that wants
573 * to own names and export objects, see <xref linkend="gdbus-owning-names"/>.
574 * Simply register objects to be exported in @bus_acquired_handler and
575 * unregister the objects (if any) in @name_lost_handler.
577 * Returns: An identifier (never 0) that an be used with
578 * g_bus_unown_name() to stop owning the name.
583 g_bus_own_name (GBusType bus_type,
585 GBusNameOwnerFlags flags,
586 GBusAcquiredCallback bus_acquired_handler,
587 GBusNameAcquiredCallback name_acquired_handler,
588 GBusNameLostCallback name_lost_handler,
590 GDestroyNotify user_data_free_func)
594 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
598 client = g_new0 (Client, 1);
599 client->ref_count = 1;
600 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
601 client->name = g_strdup (name);
602 client->flags = flags;
603 client->bus_acquired_handler = bus_acquired_handler;
604 client->name_acquired_handler = name_acquired_handler;
605 client->name_lost_handler = name_lost_handler;
606 client->user_data = user_data;
607 client->user_data_free_func = user_data_free_func;
608 client->main_context = g_main_context_get_thread_default ();
609 if (client->main_context != NULL)
610 g_main_context_ref (client->main_context);
612 if (map_id_to_client == NULL)
614 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
616 g_hash_table_insert (map_id_to_client,
617 GUINT_TO_POINTER (client->id),
623 client_ref (client));
631 GClosure *bus_acquired_closure;
632 GClosure *name_acquired_closure;
633 GClosure *name_lost_closure;
637 own_with_closures_on_bus_acquired (GDBusConnection *connection,
641 OwnNameData *data = user_data;
642 GValue params[2] = { { 0, }, { 0, } };
644 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
645 g_value_set_object (¶ms[0], connection);
647 g_value_init (¶ms[1], G_TYPE_STRING);
648 g_value_set_string (¶ms[1], name);
650 g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
654 own_with_closures_on_name_acquired (GDBusConnection *connection,
658 OwnNameData *data = user_data;
659 GValue params[2] = { { 0, }, { 0, } };
661 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
662 g_value_set_object (¶ms[0], connection);
664 g_value_init (¶ms[1], G_TYPE_STRING);
665 g_value_set_string (¶ms[1], name);
667 g_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
671 own_with_closures_on_name_lost (GDBusConnection *connection,
675 OwnNameData *data = user_data;
676 GValue params[2] = { { 0, }, { 0, } };
678 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
679 g_value_set_object (¶ms[0], connection);
681 g_value_init (¶ms[1], G_TYPE_STRING);
682 g_value_set_string (¶ms[1], name);
684 g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
688 bus_own_name_free_func (gpointer user_data)
690 OwnNameData *data = user_data;
692 if (data->bus_acquired_closure != NULL)
693 g_closure_unref (data->bus_acquired_closure);
695 if (data->name_acquired_closure != NULL)
696 g_closure_unref (data->name_acquired_closure);
698 if (data->name_lost_closure != NULL)
699 g_closure_unref (data->name_lost_closure);
705 * g_bus_own_name_with_closures:
706 * @bus_type: The type of bus to own a name on.
707 * @name: The well-known name to own.
708 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
709 * @bus_acquired_closure: (allow-none): #GClosure to invoke when connected to
710 * the bus of type @bus_type or %NULL.
711 * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
713 * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
716 * Version of g_bus_own_name() using closures instead of callbacks for
717 * easier binding in other languages.
719 * Returns: An identifier (never 0) that an be used with
720 * g_bus_unown_name() to stop owning the name.
722 * Rename to: g_bus_own_name
727 g_bus_own_name_with_closures (GBusType bus_type,
729 GBusNameOwnerFlags flags,
730 GClosure *bus_acquired_closure,
731 GClosure *name_acquired_closure,
732 GClosure *name_lost_closure)
736 data = g_new0 (OwnNameData, 1);
738 if (bus_acquired_closure != NULL)
740 data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
741 g_closure_sink (bus_acquired_closure);
744 if (name_acquired_closure != NULL)
746 data->name_acquired_closure = g_closure_ref (name_acquired_closure);
747 g_closure_sink (name_acquired_closure);
750 if (name_lost_closure != NULL)
752 data->name_lost_closure = g_closure_ref (name_lost_closure);
753 g_closure_sink (name_lost_closure);
756 return g_bus_own_name (bus_type,
759 bus_acquired_closure != NULL ? own_with_closures_on_bus_acquired : NULL,
760 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
761 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
763 bus_own_name_free_func);
767 * g_bus_own_name_on_connection_with_closures:
768 * @connection: A #GDBusConnection.
769 * @name: The well-known name to own.
770 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
771 * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
773 * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
776 * Version of g_bus_own_name_on_connection() using closures instead of callbacks for
777 * easier binding in other languages.
779 * Returns: An identifier (never 0) that an be used with
780 * g_bus_unown_name() to stop owning the name.
782 * Rename to: g_bus_own_name_on_connection
787 g_bus_own_name_on_connection_with_closures (GDBusConnection *connection,
789 GBusNameOwnerFlags flags,
790 GClosure *name_acquired_closure,
791 GClosure *name_lost_closure)
795 data = g_new0 (OwnNameData, 1);
797 if (name_acquired_closure != NULL)
799 data->name_acquired_closure = g_closure_ref (name_acquired_closure);
800 g_closure_sink (name_acquired_closure);
803 if (name_lost_closure != NULL)
805 data->name_lost_closure = g_closure_ref (name_lost_closure);
806 g_closure_sink (name_lost_closure);
809 return g_bus_own_name_on_connection (connection,
812 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
813 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
815 bus_own_name_free_func);
820 * @owner_id: An identifier obtained from g_bus_own_name()
822 * Stops owning a name.
827 g_bus_unown_name (guint owner_id)
831 g_return_if_fail (owner_id > 0);
836 if (owner_id == 0 || map_id_to_client == NULL ||
837 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
839 g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
843 client->cancelled = TRUE;
844 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
849 /* do callback without holding lock */
852 /* Release the name if needed */
853 if (client->needs_release && client->connection != NULL)
857 guint32 release_name_reply;
859 /* TODO: it kinda sucks having to do a sync call to release the name - but if
860 * we don't, then a subsequent grab of the name will make the bus daemon return
861 * IN_QUEUE which will trigger name_lost().
863 * I believe this is a bug in the bus daemon.
866 result = g_dbus_connection_call_sync (client->connection,
867 "org.freedesktop.DBus", /* bus name */
868 "/org/freedesktop/DBus", /* object path */
869 "org.freedesktop.DBus", /* interface name */
870 "ReleaseName", /* method name */
871 g_variant_new ("(s)", client->name),
872 G_VARIANT_TYPE ("(u)"),
873 G_DBUS_CALL_FLAGS_NONE,
879 g_warning ("Error releasing name %s: %s", client->name, error->message);
880 g_error_free (error);
884 g_variant_get (result, "(u)", &release_name_reply);
885 if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
887 g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
889 g_variant_unref (result);
893 if (client->disconnected_signal_handler_id > 0)
894 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
895 if (client->name_acquired_subscription_id > 0)
896 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
897 if (client->name_lost_subscription_id > 0)
898 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
899 client->disconnected_signal_handler_id = 0;
900 client->name_acquired_subscription_id = 0;
901 client->name_lost_subscription_id = 0;
902 if (client->connection != NULL)
904 g_object_unref (client->connection);
905 client->connection = NULL;
908 client_unref (client);
912 #define __G_DBUS_NAME_OWNING_C__
913 #include "gioaliasdef.c"