1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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"
36 * SECTION:gdbusnameowning
37 * @title: Owning Bus Names
38 * @short_description: Simple API for owning bus names
41 * Convenience API for owning bus names.
43 * A simple example for owning a name can be found in
44 * [gdbus-example-own-name.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-own-name.c)
47 G_LOCK_DEFINE_STATIC (lock);
49 /* ---------------------------------------------------------------------------------------------------- */
53 PREVIOUS_CALL_NONE = 0,
54 PREVIOUS_CALL_ACQUIRED,
60 gint ref_count; /* (atomic) */
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;
78 gboolean cancelled; /* must hold lock when reading or modifying */
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 g_main_context_unref (client->main_context);
110 g_free (client->name);
111 if (client->user_data_free_func != NULL)
112 client->user_data_free_func (client->user_data);
117 /* ---------------------------------------------------------------------------------------------------- */
122 CALL_TYPE_NAME_ACQUIRED,
130 /* keep this separate because client->connection may
131 * be set to NULL after scheduling the call
133 GDBusConnection *connection;
135 /* set to TRUE to call acquired */
140 call_handler_data_free (CallHandlerData *data)
142 if (data->connection != NULL)
143 g_object_unref (data->connection);
144 client_unref (data->client);
149 actually_do_call (Client *client, GDBusConnection *connection, CallType call_type)
153 case CALL_TYPE_NAME_ACQUIRED:
154 if (client->name_acquired_handler != NULL)
156 client->name_acquired_handler (connection,
162 case CALL_TYPE_NAME_LOST:
163 if (client->name_lost_handler != NULL)
165 client->name_lost_handler (connection,
172 g_assert_not_reached ();
178 call_in_idle_cb (gpointer _data)
180 CallHandlerData *data = _data;
181 actually_do_call (data->client, data->connection, data->call_type);
186 schedule_call_in_idle (Client *client, CallType call_type)
188 CallHandlerData *data;
189 GSource *idle_source;
191 data = g_new0 (CallHandlerData, 1);
192 data->client = client_ref (client);
193 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
194 data->call_type = call_type;
196 idle_source = g_idle_source_new ();
197 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
198 g_source_set_callback (idle_source,
201 (GDestroyNotify) call_handler_data_free);
202 g_source_set_static_name (idle_source, "[gio, gdbusnameowning.c] call_in_idle_cb");
203 g_source_attach (idle_source, client->main_context);
204 g_source_unref (idle_source);
208 do_call (Client *client, CallType call_type)
210 GMainContext *current_context;
212 /* only schedule in idle if we're not in the right thread */
213 current_context = g_main_context_ref_thread_default ();
214 if (current_context != client->main_context)
215 schedule_call_in_idle (client, call_type);
217 actually_do_call (client, client->connection, call_type);
218 g_main_context_unref (current_context);
222 call_acquired_handler (Client *client)
225 if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
227 client->previous_call = PREVIOUS_CALL_ACQUIRED;
228 if (!client->cancelled)
231 do_call (client, CALL_TYPE_NAME_ACQUIRED);
241 call_lost_handler (Client *client)
244 if (client->previous_call != PREVIOUS_CALL_LOST)
246 client->previous_call = PREVIOUS_CALL_LOST;
247 if (!client->cancelled)
250 do_call (client, CALL_TYPE_NAME_LOST);
259 /* ---------------------------------------------------------------------------------------------------- */
262 on_name_lost_or_acquired (GDBusConnection *connection,
263 const gchar *sender_name,
264 const gchar *object_path,
265 const gchar *interface_name,
266 const gchar *signal_name,
267 GVariant *parameters,
270 Client *client = user_data;
273 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
274 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
275 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
278 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
280 g_warning ("%s signal had unexpected signature %s", signal_name,
281 g_variant_get_type_string (parameters));
285 if (g_strcmp0 (signal_name, "NameLost") == 0)
287 g_variant_get (parameters, "(&s)", &name);
288 if (g_strcmp0 (name, client->name) == 0)
290 call_lost_handler (client);
293 else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
295 g_variant_get (parameters, "(&s)", &name);
296 if (g_strcmp0 (name, client->name) == 0)
298 call_acquired_handler (client);
305 /* ---------------------------------------------------------------------------------------------------- */
308 request_name_cb (GObject *source_object,
312 Client *client = user_data;
314 guint32 request_name_reply;
315 gboolean unsubscribe;
317 request_name_reply = 0;
320 /* don't use client->connection - it may be NULL already */
321 result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
326 g_variant_get (result, "(u)", &request_name_reply);
327 g_variant_unref (result);
332 switch (request_name_reply)
334 case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
335 /* We got the name - now listen for NameLost and NameAcquired */
336 call_acquired_handler (client);
339 case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
340 /* Waiting in line - listen for NameLost and NameAcquired */
341 call_lost_handler (client);
345 /* assume we couldn't get the name - explicit fallthrough */
346 case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
347 case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
348 /* Some other part of the process is already owning the name */
349 call_lost_handler (client);
351 client->needs_release = FALSE;
355 /* If we’re not the owner and not in the queue, there’s no point in continuing
356 * to listen to NameAcquired or NameLost. */
359 GDBusConnection *connection = NULL;
361 /* make sure we use a known good Connection object since it may be set to
362 * NULL at any point after being cancelled
365 if (!client->cancelled)
366 connection = g_object_ref (client->connection);
369 if (connection != NULL)
371 if (client->name_acquired_subscription_id > 0)
372 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
373 if (client->name_lost_subscription_id > 0)
374 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
375 client->name_acquired_subscription_id = 0;
376 client->name_lost_subscription_id = 0;
378 g_object_unref (connection);
382 client_unref (client);
385 /* ---------------------------------------------------------------------------------------------------- */
388 on_connection_disconnected (GDBusConnection *connection,
389 gboolean remote_peer_vanished,
393 Client *client = user_data;
395 if (client->disconnected_signal_handler_id > 0)
396 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
397 if (client->name_acquired_subscription_id > 0)
398 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
399 if (client->name_lost_subscription_id > 0)
400 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
401 g_object_unref (client->connection);
402 client->disconnected_signal_handler_id = 0;
403 client->name_acquired_subscription_id = 0;
404 client->name_lost_subscription_id = 0;
405 client->connection = NULL;
407 call_lost_handler (client);
410 /* ---------------------------------------------------------------------------------------------------- */
413 has_connection (Client *client)
415 /* listen for disconnection */
416 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
418 G_CALLBACK (on_connection_disconnected),
421 /* Start listening to NameLost and NameAcquired messages. We hold
422 * references to the Client in the signal closures, since it’s possible
423 * for a signal to be in-flight after unsubscribing the signal handler.
424 * This creates a reference count cycle, but that’s explicitly broken by
425 * disconnecting the signal handlers before calling client_unref() in
426 * g_bus_unown_name().
428 * Subscribe to NameLost and NameAcquired before calling RequestName() to
429 * avoid the potential race of losing the name between receiving a reply to
430 * RequestName() and subscribing to NameLost. The #PreviousCall state will
431 * ensure that the user callbacks get called an appropriate number of times. */
432 client->name_lost_subscription_id =
433 g_dbus_connection_signal_subscribe (client->connection,
434 "org.freedesktop.DBus",
435 "org.freedesktop.DBus",
437 "/org/freedesktop/DBus",
439 G_DBUS_SIGNAL_FLAGS_NONE,
440 on_name_lost_or_acquired,
442 (GDestroyNotify) client_unref);
443 client->name_acquired_subscription_id =
444 g_dbus_connection_signal_subscribe (client->connection,
445 "org.freedesktop.DBus",
446 "org.freedesktop.DBus",
448 "/org/freedesktop/DBus",
450 G_DBUS_SIGNAL_FLAGS_NONE,
451 on_name_lost_or_acquired,
453 (GDestroyNotify) client_unref);
455 /* attempt to acquire the name */
456 client->needs_release = TRUE;
457 g_dbus_connection_call (client->connection,
458 "org.freedesktop.DBus", /* bus name */
459 "/org/freedesktop/DBus", /* object path */
460 "org.freedesktop.DBus", /* interface name */
461 "RequestName", /* method name */
462 g_variant_new ("(su)",
465 G_VARIANT_TYPE ("(u)"),
466 G_DBUS_CALL_FLAGS_NONE,
469 (GAsyncReadyCallback) request_name_cb,
470 client_ref (client));
475 connection_get_cb (GObject *source_object,
479 Client *client = user_data;
481 /* must not do anything if already cancelled */
483 if (client->cancelled)
490 client->connection = g_bus_get_finish (res, NULL);
491 if (client->connection == NULL)
493 call_lost_handler (client);
497 /* No need to schedule this in idle as we're already in the thread
498 * that the user called g_bus_own_name() from. This is because
499 * g_bus_get() guarantees that.
501 * Also, we need to ensure that the handler is invoked *before*
502 * we call RequestName(). Otherwise there is a race.
504 if (client->bus_acquired_handler != NULL)
506 client->bus_acquired_handler (client->connection,
511 has_connection (client);
514 client_unref (client);
517 /* ---------------------------------------------------------------------------------------------------- */
520 * g_bus_own_name_on_connection:
521 * @connection: a #GDBusConnection
522 * @name: the well-known name to own
523 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
524 * @name_acquired_handler: (nullable): handler to invoke when @name is acquired or %NULL
525 * @name_lost_handler: (nullable): handler to invoke when @name is lost or %NULL
526 * @user_data: user data to pass to handlers
527 * @user_data_free_func: (nullable): function for freeing @user_data or %NULL
529 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
532 * Returns: an identifier (never 0) that can be used with
533 * g_bus_unown_name() to stop owning the name
538 g_bus_own_name_on_connection (GDBusConnection *connection,
540 GBusNameOwnerFlags flags,
541 GBusNameAcquiredCallback name_acquired_handler,
542 GBusNameLostCallback name_lost_handler,
544 GDestroyNotify user_data_free_func)
548 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
549 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
553 client = g_new0 (Client, 1);
554 client->ref_count = 1;
555 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
556 client->name = g_strdup (name);
557 client->flags = flags;
558 client->name_acquired_handler = name_acquired_handler;
559 client->name_lost_handler = name_lost_handler;
560 client->user_data = user_data;
561 client->user_data_free_func = user_data_free_func;
562 client->main_context = g_main_context_ref_thread_default ();
564 client->connection = g_object_ref (connection);
566 if (map_id_to_client == NULL)
568 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
570 g_hash_table_insert (map_id_to_client,
571 GUINT_TO_POINTER (client->id),
576 has_connection (client);
583 * @bus_type: the type of bus to own a name on
584 * @name: the well-known name to own
585 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
586 * @bus_acquired_handler: (nullable): handler to invoke when connected to the bus of type @bus_type or %NULL
587 * @name_acquired_handler: (nullable): handler to invoke when @name is acquired or %NULL
588 * @name_lost_handler: (nullable): handler to invoke when @name is lost or %NULL
589 * @user_data: user data to pass to handlers
590 * @user_data_free_func: (nullable): function for freeing @user_data or %NULL
592 * Starts acquiring @name on the bus specified by @bus_type and calls
593 * @name_acquired_handler and @name_lost_handler when the name is
594 * acquired respectively lost. Callbacks will be invoked in the
595 * [thread-default main context][g-main-context-push-thread-default]
596 * of the thread you are calling this function from.
598 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
599 * callbacks will be invoked after calling this function - there are three
602 * - @name_lost_handler with a %NULL connection (if a connection to the bus
605 * - @bus_acquired_handler then @name_lost_handler (if the name can't be
608 * - @bus_acquired_handler then @name_acquired_handler (if the name was
611 * When you are done owning the name, just call g_bus_unown_name()
612 * with the owner id this function returns.
614 * If the name is acquired or lost (for example another application
615 * could acquire the name if you allow replacement or the application
616 * currently owning the name exits), the handlers are also invoked.
617 * If the #GDBusConnection that is used for attempting to own the name
618 * closes, then @name_lost_handler is invoked since it is no longer
619 * possible for other processes to access the process.
621 * You cannot use g_bus_own_name() several times for the same name (unless
622 * interleaved with calls to g_bus_unown_name()) - only the first call
625 * Another guarantee is that invocations of @name_acquired_handler
626 * and @name_lost_handler are guaranteed to alternate; that
627 * is, if @name_acquired_handler is invoked then you are
628 * guaranteed that the next time one of the handlers is invoked, it
629 * will be @name_lost_handler. The reverse is also true.
631 * If you plan on exporting objects (using e.g.
632 * g_dbus_connection_register_object()), note that it is generally too late
633 * to export the objects in @name_acquired_handler. Instead, you can do this
634 * in @bus_acquired_handler since you are guaranteed that this will run
635 * before @name is requested from the bus.
637 * This behavior makes it very simple to write applications that wants
638 * to [own names][gdbus-owning-names] and export objects.
639 * Simply register objects to be exported in @bus_acquired_handler and
640 * unregister the objects (if any) in @name_lost_handler.
642 * Returns: an identifier (never 0) that can be used with
643 * g_bus_unown_name() to stop owning the name.
648 g_bus_own_name (GBusType bus_type,
650 GBusNameOwnerFlags flags,
651 GBusAcquiredCallback bus_acquired_handler,
652 GBusNameAcquiredCallback name_acquired_handler,
653 GBusNameLostCallback name_lost_handler,
655 GDestroyNotify user_data_free_func)
659 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
663 client = g_new0 (Client, 1);
664 client->ref_count = 1;
665 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
666 client->name = g_strdup (name);
667 client->flags = flags;
668 client->bus_acquired_handler = bus_acquired_handler;
669 client->name_acquired_handler = name_acquired_handler;
670 client->name_lost_handler = name_lost_handler;
671 client->user_data = user_data;
672 client->user_data_free_func = user_data_free_func;
673 client->main_context = g_main_context_ref_thread_default ();
675 if (map_id_to_client == NULL)
677 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
679 g_hash_table_insert (map_id_to_client,
680 GUINT_TO_POINTER (client->id),
686 client_ref (client));
694 GClosure *bus_acquired_closure;
695 GClosure *name_acquired_closure;
696 GClosure *name_lost_closure;
700 own_name_data_new (GClosure *bus_acquired_closure,
701 GClosure *name_acquired_closure,
702 GClosure *name_lost_closure)
706 data = g_new0 (OwnNameData, 1);
708 if (bus_acquired_closure != NULL)
710 data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
711 g_closure_sink (bus_acquired_closure);
712 if (G_CLOSURE_NEEDS_MARSHAL (bus_acquired_closure))
713 g_closure_set_marshal (bus_acquired_closure, g_cclosure_marshal_generic);
716 if (name_acquired_closure != NULL)
718 data->name_acquired_closure = g_closure_ref (name_acquired_closure);
719 g_closure_sink (name_acquired_closure);
720 if (G_CLOSURE_NEEDS_MARSHAL (name_acquired_closure))
721 g_closure_set_marshal (name_acquired_closure, g_cclosure_marshal_generic);
724 if (name_lost_closure != NULL)
726 data->name_lost_closure = g_closure_ref (name_lost_closure);
727 g_closure_sink (name_lost_closure);
728 if (G_CLOSURE_NEEDS_MARSHAL (name_lost_closure))
729 g_closure_set_marshal (name_lost_closure, g_cclosure_marshal_generic);
736 own_with_closures_on_bus_acquired (GDBusConnection *connection,
740 OwnNameData *data = user_data;
741 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
743 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
744 g_value_set_object (¶ms[0], connection);
746 g_value_init (¶ms[1], G_TYPE_STRING);
747 g_value_set_string (¶ms[1], name);
749 g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
751 g_value_unset (params + 0);
752 g_value_unset (params + 1);
756 own_with_closures_on_name_acquired (GDBusConnection *connection,
760 OwnNameData *data = user_data;
761 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
763 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
764 g_value_set_object (¶ms[0], connection);
766 g_value_init (¶ms[1], G_TYPE_STRING);
767 g_value_set_string (¶ms[1], name);
769 g_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
771 g_value_unset (params + 0);
772 g_value_unset (params + 1);
776 own_with_closures_on_name_lost (GDBusConnection *connection,
780 OwnNameData *data = user_data;
781 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
783 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
784 g_value_set_object (¶ms[0], connection);
786 g_value_init (¶ms[1], G_TYPE_STRING);
787 g_value_set_string (¶ms[1], name);
789 g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
791 g_value_unset (params + 0);
792 g_value_unset (params + 1);
796 bus_own_name_free_func (gpointer user_data)
798 OwnNameData *data = user_data;
800 if (data->bus_acquired_closure != NULL)
801 g_closure_unref (data->bus_acquired_closure);
803 if (data->name_acquired_closure != NULL)
804 g_closure_unref (data->name_acquired_closure);
806 if (data->name_lost_closure != NULL)
807 g_closure_unref (data->name_lost_closure);
813 * g_bus_own_name_with_closures: (rename-to g_bus_own_name)
814 * @bus_type: the type of bus to own a name on
815 * @name: the well-known name to own
816 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
817 * @bus_acquired_closure: (nullable): #GClosure to invoke when connected to
818 * the bus of type @bus_type or %NULL
819 * @name_acquired_closure: (nullable): #GClosure to invoke when @name is
821 * @name_lost_closure: (nullable): #GClosure to invoke when @name is lost or
824 * Version of g_bus_own_name() using closures instead of callbacks for
825 * easier binding in other languages.
827 * Returns: an identifier (never 0) that can be used with
828 * g_bus_unown_name() to stop owning the name.
833 g_bus_own_name_with_closures (GBusType bus_type,
835 GBusNameOwnerFlags flags,
836 GClosure *bus_acquired_closure,
837 GClosure *name_acquired_closure,
838 GClosure *name_lost_closure)
840 return g_bus_own_name (bus_type,
843 bus_acquired_closure != NULL ? own_with_closures_on_bus_acquired : NULL,
844 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
845 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
846 own_name_data_new (bus_acquired_closure,
847 name_acquired_closure,
849 bus_own_name_free_func);
853 * g_bus_own_name_on_connection_with_closures: (rename-to g_bus_own_name_on_connection)
854 * @connection: a #GDBusConnection
855 * @name: the well-known name to own
856 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
857 * @name_acquired_closure: (nullable): #GClosure to invoke when @name is
859 * @name_lost_closure: (nullable): #GClosure to invoke when @name is lost
862 * Version of g_bus_own_name_on_connection() using closures instead of
863 * callbacks for easier binding in other languages.
865 * Returns: an identifier (never 0) that can be used with
866 * g_bus_unown_name() to stop owning the name.
871 g_bus_own_name_on_connection_with_closures (GDBusConnection *connection,
873 GBusNameOwnerFlags flags,
874 GClosure *name_acquired_closure,
875 GClosure *name_lost_closure)
877 return g_bus_own_name_on_connection (connection,
880 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
881 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
882 own_name_data_new (NULL,
883 name_acquired_closure,
885 bus_own_name_free_func);
890 * @owner_id: an identifier obtained from g_bus_own_name()
892 * Stops owning a name.
894 * Note that there may still be D-Bus traffic to process (relating to owning
895 * and unowning the name) in the current thread-default #GMainContext after
896 * this function has returned. You should continue to iterate the #GMainContext
897 * until the #GDestroyNotify function passed to g_bus_own_name() is called, in
898 * order to avoid memory leaks through callbacks queued on the #GMainContext
899 * after it’s stopped being iterated.
904 g_bus_unown_name (guint owner_id)
908 g_return_if_fail (owner_id > 0);
913 if (owner_id == 0 || map_id_to_client == NULL ||
914 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
916 g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
920 client->cancelled = TRUE;
921 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
926 /* do callback without holding lock */
929 /* Release the name if needed */
930 if (client->needs_release &&
931 client->connection != NULL &&
932 !g_dbus_connection_is_closed (client->connection))
936 guint32 release_name_reply;
938 /* TODO: it kinda sucks having to do a sync call to release the name - but if
939 * we don't, then a subsequent grab of the name will make the bus daemon return
940 * IN_QUEUE which will trigger name_lost().
942 * I believe this is a bug in the bus daemon.
945 result = g_dbus_connection_call_sync (client->connection,
946 "org.freedesktop.DBus", /* bus name */
947 "/org/freedesktop/DBus", /* object path */
948 "org.freedesktop.DBus", /* interface name */
949 "ReleaseName", /* method name */
950 g_variant_new ("(s)", client->name),
951 G_VARIANT_TYPE ("(u)"),
952 G_DBUS_CALL_FLAGS_NONE,
958 g_warning ("Error releasing name %s: %s", client->name, error->message);
959 g_error_free (error);
963 g_variant_get (result, "(u)", &release_name_reply);
964 if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
966 g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
970 client->needs_release = FALSE;
972 g_variant_unref (result);
976 if (client->disconnected_signal_handler_id > 0)
977 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
978 if (client->name_acquired_subscription_id > 0)
979 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
980 if (client->name_lost_subscription_id > 0)
981 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
982 client->disconnected_signal_handler_id = 0;
983 client->name_acquired_subscription_id = 0;
984 client->name_lost_subscription_id = 0;
985 if (client->connection != NULL)
987 g_object_unref (client->connection);
988 client->connection = NULL;
991 client_unref (client);