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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
25 #include "gdbusutils.h"
26 #include "gdbusnameowning.h"
27 #include "gdbuserror.h"
28 #include "gdbusprivate.h"
29 #include "gdbusconnection.h"
34 * SECTION:gdbusnameowning
35 * @title: Owning Bus Names
36 * @short_description: Simple API for owning bus names
39 * Convenience API for owning bus names.
41 * A simple example for owning a name can be found in
42 * [gdbus-example-own-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-own-name.c)
45 G_LOCK_DEFINE_STATIC (lock);
47 /* ---------------------------------------------------------------------------------------------------- */
51 PREVIOUS_CALL_NONE = 0,
52 PREVIOUS_CALL_ACQUIRED,
58 volatile gint ref_count;
60 GBusNameOwnerFlags flags;
62 GBusAcquiredCallback bus_acquired_handler;
63 GBusNameAcquiredCallback name_acquired_handler;
64 GBusNameLostCallback name_lost_handler;
66 GDestroyNotify user_data_free_func;
67 GMainContext *main_context;
69 PreviousCall previous_call;
71 GDBusConnection *connection;
72 gulong disconnected_signal_handler_id;
73 guint name_acquired_subscription_id;
74 guint name_lost_subscription_id;
76 volatile gboolean cancelled; /* must hold lock when reading or modifying */
78 gboolean needs_release;
81 static guint next_global_id = 1;
82 static GHashTable *map_id_to_client = NULL;
86 client_ref (Client *client)
88 g_atomic_int_inc (&client->ref_count);
93 client_unref (Client *client)
95 if (g_atomic_int_dec_and_test (&client->ref_count))
97 if (client->connection != NULL)
99 if (client->disconnected_signal_handler_id > 0)
100 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
101 if (client->name_acquired_subscription_id > 0)
102 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
103 if (client->name_lost_subscription_id > 0)
104 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
105 g_object_unref (client->connection);
107 g_main_context_unref (client->main_context);
108 g_free (client->name);
109 if (client->user_data_free_func != NULL)
110 client->user_data_free_func (client->user_data);
115 /* ---------------------------------------------------------------------------------------------------- */
120 CALL_TYPE_NAME_ACQUIRED,
128 /* keep this separate because client->connection may
129 * be set to NULL after scheduling the call
131 GDBusConnection *connection;
133 /* set to TRUE to call acquired */
138 call_handler_data_free (CallHandlerData *data)
140 if (data->connection != NULL)
141 g_object_unref (data->connection);
142 client_unref (data->client);
147 actually_do_call (Client *client, GDBusConnection *connection, CallType call_type)
151 case CALL_TYPE_NAME_ACQUIRED:
152 if (client->name_acquired_handler != NULL)
154 client->name_acquired_handler (connection,
160 case CALL_TYPE_NAME_LOST:
161 if (client->name_lost_handler != NULL)
163 client->name_lost_handler (connection,
170 g_assert_not_reached ();
176 call_in_idle_cb (gpointer _data)
178 CallHandlerData *data = _data;
179 actually_do_call (data->client, data->connection, data->call_type);
184 schedule_call_in_idle (Client *client, CallType call_type)
186 CallHandlerData *data;
187 GSource *idle_source;
189 data = g_new0 (CallHandlerData, 1);
190 data->client = client_ref (client);
191 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
192 data->call_type = call_type;
194 idle_source = g_idle_source_new ();
195 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
196 g_source_set_callback (idle_source,
199 (GDestroyNotify) call_handler_data_free);
200 g_source_set_name (idle_source, "[gio] call_in_idle_cb");
201 g_source_attach (idle_source, client->main_context);
202 g_source_unref (idle_source);
206 do_call (Client *client, CallType call_type)
208 GMainContext *current_context;
210 /* only schedule in idle if we're not in the right thread */
211 current_context = g_main_context_ref_thread_default ();
212 if (current_context != client->main_context)
213 schedule_call_in_idle (client, call_type);
215 actually_do_call (client, client->connection, call_type);
216 g_main_context_unref (current_context);
220 call_acquired_handler (Client *client)
223 if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
225 client->previous_call = PREVIOUS_CALL_ACQUIRED;
226 if (!client->cancelled)
229 do_call (client, CALL_TYPE_NAME_ACQUIRED);
239 call_lost_handler (Client *client)
242 if (client->previous_call != PREVIOUS_CALL_LOST)
244 client->previous_call = PREVIOUS_CALL_LOST;
245 if (!client->cancelled)
248 do_call (client, CALL_TYPE_NAME_LOST);
257 /* ---------------------------------------------------------------------------------------------------- */
260 on_name_lost_or_acquired (GDBusConnection *connection,
261 const gchar *sender_name,
262 const gchar *object_path,
263 const gchar *interface_name,
264 const gchar *signal_name,
265 GVariant *parameters,
268 Client *client = user_data;
271 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
272 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
273 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
276 if (g_strcmp0 (signal_name, "NameLost") == 0)
278 g_variant_get (parameters, "(&s)", &name);
279 if (g_strcmp0 (name, client->name) == 0)
281 call_lost_handler (client);
284 else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
286 g_variant_get (parameters, "(&s)", &name);
287 if (g_strcmp0 (name, client->name) == 0)
289 call_acquired_handler (client);
296 /* ---------------------------------------------------------------------------------------------------- */
299 process_request_name_reply (Client *client,
300 guint32 request_name_reply)
306 switch (request_name_reply)
308 case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
309 /* We got the name - now listen for NameLost and NameAcquired */
310 call_acquired_handler (client);
312 client->needs_release = TRUE;
315 case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
316 /* Waiting in line - listen for NameLost and NameAcquired */
317 call_lost_handler (client);
319 client->needs_release = TRUE;
323 /* assume we couldn't get the name - explicit fallthrough */
324 case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
325 case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
326 /* Some other part of the process is already owning the name */
327 call_lost_handler (client);
334 GDBusConnection *connection = NULL;
336 /* if cancelled, there is no point in subscribing to signals - if not, make sure
337 * we use a known good Connection object since it may be set to NULL at any point
338 * after being cancelled
341 if (!client->cancelled)
342 connection = g_object_ref (client->connection);
345 /* start listening to NameLost and NameAcquired messages */
346 if (connection != NULL)
348 client->name_lost_subscription_id =
349 g_dbus_connection_signal_subscribe (connection,
350 "org.freedesktop.DBus",
351 "org.freedesktop.DBus",
353 "/org/freedesktop/DBus",
355 G_DBUS_SIGNAL_FLAGS_NONE,
356 on_name_lost_or_acquired,
359 client->name_acquired_subscription_id =
360 g_dbus_connection_signal_subscribe (connection,
361 "org.freedesktop.DBus",
362 "org.freedesktop.DBus",
364 "/org/freedesktop/DBus",
366 G_DBUS_SIGNAL_FLAGS_NONE,
367 on_name_lost_or_acquired,
370 g_object_unref (connection);
375 /* ---------------------------------------------------------------------------------------------------- */
378 request_name_cb (GObject *source_object,
382 Client *client = user_data;
384 guint32 request_name_reply;
386 request_name_reply = 0;
389 /* don't use client->connection - it may be NULL already */
390 result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
395 g_variant_get (result, "(u)", &request_name_reply);
396 g_variant_unref (result);
399 process_request_name_reply (client, request_name_reply);
401 client_unref (client);
404 /* ---------------------------------------------------------------------------------------------------- */
407 on_connection_disconnected (GDBusConnection *connection,
408 gboolean remote_peer_vanished,
412 Client *client = user_data;
414 if (client->disconnected_signal_handler_id > 0)
415 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
416 if (client->name_acquired_subscription_id > 0)
417 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
418 if (client->name_lost_subscription_id > 0)
419 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
420 g_object_unref (client->connection);
421 client->disconnected_signal_handler_id = 0;
422 client->name_acquired_subscription_id = 0;
423 client->name_lost_subscription_id = 0;
424 client->connection = NULL;
426 call_lost_handler (client);
429 /* ---------------------------------------------------------------------------------------------------- */
432 has_connection (Client *client)
434 /* listen for disconnection */
435 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
437 G_CALLBACK (on_connection_disconnected),
439 /* attempt to acquire the name */
442 GError *error = NULL;
443 guint32 request_name_reply;
445 request_name_reply = 0;
447 request_name_reply = g_dbus_request_name (client->connection, client->name, client->flags, &error);
448 g_assert_no_error (error);
450 process_request_name_reply (client, request_name_reply);
454 g_dbus_connection_call (client->connection,
455 "org.freedesktop.DBus", /* bus name */
456 "/org/freedesktop/DBus", /* object path */
457 "org.freedesktop.DBus", /* interface name */
458 "RequestName", /* method name */
459 g_variant_new ("(su)",
462 G_VARIANT_TYPE ("(u)"),
463 G_DBUS_CALL_FLAGS_NONE,
466 (GAsyncReadyCallback) request_name_cb,
467 client_ref (client));
473 connection_get_cb (GObject *source_object,
477 Client *client = user_data;
479 /* must not do anything if already cancelled */
481 if (client->cancelled)
488 client->connection = g_bus_get_finish (res, NULL);
489 if (client->connection == NULL)
491 call_lost_handler (client);
495 /* No need to schedule this in idle as we're already in the thread
496 * that the user called g_bus_own_name() from. This is because
497 * g_bus_get() guarantees that.
499 * Also, we need to ensure that the handler is invoked *before*
500 * we call RequestName(). Otherwise there is a race.
502 if (client->bus_acquired_handler != NULL)
504 client->bus_acquired_handler (client->connection,
509 has_connection (client);
512 client_unref (client);
515 /* ---------------------------------------------------------------------------------------------------- */
518 * g_bus_own_name_on_connection:
519 * @connection: a #GDBusConnection
520 * @name: the well-known name to own
521 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
522 * @name_acquired_handler: (allow-none): handler to invoke when @name is acquired or %NULL
523 * @name_lost_handler: (allow-none): handler to invoke when @name is lost or %NULL
524 * @user_data: user data to pass to handlers
525 * @user_data_free_func: (allow-none): function for freeing @user_data or %NULL
527 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
530 * Returns: an identifier (never 0) that an be used with
531 * g_bus_unown_name() to stop owning the name
536 g_bus_own_name_on_connection (GDBusConnection *connection,
538 GBusNameOwnerFlags flags,
539 GBusNameAcquiredCallback name_acquired_handler,
540 GBusNameLostCallback name_lost_handler,
542 GDestroyNotify user_data_free_func)
546 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
547 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
551 client = g_new0 (Client, 1);
552 client->ref_count = 1;
553 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
554 client->name = g_strdup (name);
555 client->flags = flags;
556 client->name_acquired_handler = name_acquired_handler;
557 client->name_lost_handler = name_lost_handler;
558 client->user_data = user_data;
559 client->user_data_free_func = user_data_free_func;
560 client->main_context = g_main_context_ref_thread_default ();
562 client->connection = g_object_ref (connection);
564 if (map_id_to_client == NULL)
566 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
568 g_hash_table_insert (map_id_to_client,
569 GUINT_TO_POINTER (client->id),
574 has_connection (client);
581 * @bus_type: the type of bus to own a name on
582 * @name: the well-known name to own
583 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
584 * @bus_acquired_handler: (allow-none): handler to invoke when connected to the bus of type @bus_type or %NULL
585 * @name_acquired_handler: (allow-none): handler to invoke when @name is acquired or %NULL
586 * @name_lost_handler: (allow-none): handler to invoke when @name is lost or %NULL
587 * @user_data: user data to pass to handlers
588 * @user_data_free_func: (allow-none): function for freeing @user_data or %NULL
590 * Starts acquiring @name on the bus specified by @bus_type and calls
591 * @name_acquired_handler and @name_lost_handler when the name is
592 * acquired respectively lost. Callbacks will be invoked in the
593 * [thread-default main context][g-main-context-push-thread-default]
594 * of the thread you are calling this function from.
596 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
597 * callbacks will be invoked after calling this function - there are three
600 * - @name_lost_handler with a %NULL connection (if a connection to the bus
603 * - @bus_acquired_handler then @name_lost_handler (if the name can't be
606 * - @bus_acquired_handler then @name_acquired_handler (if the name was
609 * When you are done owning the name, just call g_bus_unown_name()
610 * with the owner id this function returns.
612 * If the name is acquired or lost (for example another application
613 * could acquire the name if you allow replacement or the application
614 * currently owning the name exits), the handlers are also invoked.
615 * If the #GDBusConnection that is used for attempting to own the name
616 * closes, then @name_lost_handler is invoked since it is no longer
617 * possible for other processes to access the process.
619 * You cannot use g_bus_own_name() several times for the same name (unless
620 * interleaved with calls to g_bus_unown_name()) - only the first call
623 * Another guarantee is that invocations of @name_acquired_handler
624 * and @name_lost_handler are guaranteed to alternate; that
625 * is, if @name_acquired_handler is invoked then you are
626 * guaranteed that the next time one of the handlers is invoked, it
627 * will be @name_lost_handler. The reverse is also true.
629 * If you plan on exporting objects (using e.g.
630 * g_dbus_connection_register_object()), note that it is generally too late
631 * to export the objects in @name_acquired_handler. Instead, you can do this
632 * in @bus_acquired_handler since you are guaranteed that this will run
633 * before @name is requested from the bus.
635 * This behavior makes it very simple to write applications that wants
636 * to [own names][gdbus-owning-names] and export objects.
637 * Simply register objects to be exported in @bus_acquired_handler and
638 * unregister the objects (if any) in @name_lost_handler.
640 * Returns: an identifier (never 0) that an be used with
641 * g_bus_unown_name() to stop owning the name.
646 g_bus_own_name (GBusType bus_type,
648 GBusNameOwnerFlags flags,
649 GBusAcquiredCallback bus_acquired_handler,
650 GBusNameAcquiredCallback name_acquired_handler,
651 GBusNameLostCallback name_lost_handler,
653 GDestroyNotify user_data_free_func)
657 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
661 client = g_new0 (Client, 1);
662 client->ref_count = 1;
663 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
664 client->name = g_strdup (name);
665 client->flags = flags;
666 client->bus_acquired_handler = bus_acquired_handler;
667 client->name_acquired_handler = name_acquired_handler;
668 client->name_lost_handler = name_lost_handler;
669 client->user_data = user_data;
670 client->user_data_free_func = user_data_free_func;
671 client->main_context = g_main_context_ref_thread_default ();
673 if (map_id_to_client == NULL)
675 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
677 g_hash_table_insert (map_id_to_client,
678 GUINT_TO_POINTER (client->id),
684 client_ref (client));
692 GClosure *bus_acquired_closure;
693 GClosure *name_acquired_closure;
694 GClosure *name_lost_closure;
698 own_name_data_new (GClosure *bus_acquired_closure,
699 GClosure *name_acquired_closure,
700 GClosure *name_lost_closure)
704 data = g_new0 (OwnNameData, 1);
706 if (bus_acquired_closure != NULL)
708 data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
709 g_closure_sink (bus_acquired_closure);
710 if (G_CLOSURE_NEEDS_MARSHAL (bus_acquired_closure))
711 g_closure_set_marshal (bus_acquired_closure, g_cclosure_marshal_generic);
714 if (name_acquired_closure != NULL)
716 data->name_acquired_closure = g_closure_ref (name_acquired_closure);
717 g_closure_sink (name_acquired_closure);
718 if (G_CLOSURE_NEEDS_MARSHAL (name_acquired_closure))
719 g_closure_set_marshal (name_acquired_closure, g_cclosure_marshal_generic);
722 if (name_lost_closure != NULL)
724 data->name_lost_closure = g_closure_ref (name_lost_closure);
725 g_closure_sink (name_lost_closure);
726 if (G_CLOSURE_NEEDS_MARSHAL (name_lost_closure))
727 g_closure_set_marshal (name_lost_closure, g_cclosure_marshal_generic);
734 own_with_closures_on_bus_acquired (GDBusConnection *connection,
738 OwnNameData *data = user_data;
739 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
741 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
742 g_value_set_object (¶ms[0], connection);
744 g_value_init (¶ms[1], G_TYPE_STRING);
745 g_value_set_string (¶ms[1], name);
747 g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
749 g_value_unset (params + 0);
750 g_value_unset (params + 1);
754 own_with_closures_on_name_acquired (GDBusConnection *connection,
758 OwnNameData *data = user_data;
759 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
761 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
762 g_value_set_object (¶ms[0], connection);
764 g_value_init (¶ms[1], G_TYPE_STRING);
765 g_value_set_string (¶ms[1], name);
767 g_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
769 g_value_unset (params + 0);
770 g_value_unset (params + 1);
774 own_with_closures_on_name_lost (GDBusConnection *connection,
778 OwnNameData *data = user_data;
779 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
781 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
782 g_value_set_object (¶ms[0], connection);
784 g_value_init (¶ms[1], G_TYPE_STRING);
785 g_value_set_string (¶ms[1], name);
787 g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
789 g_value_unset (params + 0);
790 g_value_unset (params + 1);
794 bus_own_name_free_func (gpointer user_data)
796 OwnNameData *data = user_data;
798 if (data->bus_acquired_closure != NULL)
799 g_closure_unref (data->bus_acquired_closure);
801 if (data->name_acquired_closure != NULL)
802 g_closure_unref (data->name_acquired_closure);
804 if (data->name_lost_closure != NULL)
805 g_closure_unref (data->name_lost_closure);
811 * g_bus_own_name_with_closures:
812 * @bus_type: the type of bus to own a name on
813 * @name: the well-known name to own
814 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
815 * @bus_acquired_closure: (allow-none): #GClosure to invoke when connected to
816 * the bus of type @bus_type or %NULL
817 * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
819 * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
822 * Version of g_bus_own_name() using closures instead of callbacks for
823 * easier binding in other languages.
825 * Returns: an identifier (never 0) that an be used with
826 * g_bus_unown_name() to stop owning the name.
828 * Rename to: g_bus_own_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:
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: (allow-none): #GClosure to invoke when @name is
859 * @name_lost_closure: (allow-none): #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 an be used with
866 * g_bus_unown_name() to stop owning the name.
868 * Rename to: g_bus_own_name_on_connection
873 g_bus_own_name_on_connection_with_closures (GDBusConnection *connection,
875 GBusNameOwnerFlags flags,
876 GClosure *name_acquired_closure,
877 GClosure *name_lost_closure)
879 return g_bus_own_name_on_connection (connection,
882 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
883 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
884 own_name_data_new (NULL,
885 name_acquired_closure,
887 bus_own_name_free_func);
892 * @owner_id: an identifier obtained from g_bus_own_name()
894 * Stops owning a name.
899 g_bus_unown_name (guint owner_id)
903 g_return_if_fail (owner_id > 0);
908 if (owner_id == 0 || map_id_to_client == NULL ||
909 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
911 g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
915 client->cancelled = TRUE;
916 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
921 /* do callback without holding lock */
924 /* Release the name if needed */
925 if (client->needs_release &&
926 client->connection != NULL &&
927 !g_dbus_connection_is_closed (client->connection))
931 guint32 release_name_reply;
933 /* TODO: it kinda sucks having to do a sync call to release the name - but if
934 * we don't, then a subsequent grab of the name will make the bus daemon return
935 * IN_QUEUE which will trigger name_lost().
937 * I believe this is a bug in the bus daemon.
942 g_dbus_release_name (client->connection, client->name, &error);
943 g_assert_no_error (error);
945 result = g_variant_ref_sink (g_variant_new_uint32 (1));
948 result = g_dbus_connection_call_sync (client->connection,
949 "org.freedesktop.DBus", /* bus name */
950 "/org/freedesktop/DBus", /* object path */
951 "org.freedesktop.DBus", /* interface name */
952 "ReleaseName", /* method name */
953 g_variant_new ("(s)", client->name),
954 G_VARIANT_TYPE ("(u)"),
955 G_DBUS_CALL_FLAGS_NONE,
961 g_warning ("Error releasing name %s: %s", client->name, error->message);
962 g_error_free (error);
966 g_variant_get (result, "(u)", &release_name_reply);
967 if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
969 g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
971 g_variant_unref (result);
975 if (client->disconnected_signal_handler_id > 0)
976 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
977 if (client->name_acquired_subscription_id > 0)
978 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
979 if (client->name_lost_subscription_id > 0)
980 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
981 client->disconnected_signal_handler_id = 0;
982 client->name_acquired_subscription_id = 0;
983 client->name_lost_subscription_id = 0;
984 if (client->connection != NULL)
986 g_object_unref (client->connection);
987 client->connection = NULL;
990 client_unref (client);