1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 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 <glib/gi18n.h>
29 #include "gdbusutils.h"
30 #include "gdbusnameowning.h"
31 #include "gdbuserror.h"
32 #include "gdbusprivate.h"
33 #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 * <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>
46 G_LOCK_DEFINE_STATIC (lock);
48 /* ---------------------------------------------------------------------------------------------------- */
52 PREVIOUS_CALL_NONE = 0,
53 PREVIOUS_CALL_ACQUIRED,
59 volatile gint ref_count;
61 GBusNameOwnerFlags flags;
63 GBusAcquiredCallback bus_acquired_handler;
64 GBusNameAcquiredCallback name_acquired_handler;
65 GBusNameLostCallback name_lost_handler;
67 GDestroyNotify user_data_free_func;
68 GMainContext *main_context;
70 PreviousCall previous_call;
72 GDBusConnection *connection;
73 gulong disconnected_signal_handler_id;
74 guint name_acquired_subscription_id;
75 guint name_lost_subscription_id;
79 gboolean needs_release;
82 static guint next_global_id = 1;
83 static GHashTable *map_id_to_client = NULL;
87 client_ref (Client *client)
89 g_atomic_int_inc (&client->ref_count);
94 client_unref (Client *client)
96 if (g_atomic_int_dec_and_test (&client->ref_count))
98 if (client->connection != NULL)
100 if (client->disconnected_signal_handler_id > 0)
101 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
102 if (client->name_acquired_subscription_id > 0)
103 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
104 if (client->name_lost_subscription_id > 0)
105 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
106 g_object_unref (client->connection);
108 if (client->main_context != NULL)
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_attach (idle_source, client->main_context);
203 g_source_unref (idle_source);
207 do_call (Client *client, CallType call_type)
209 /* only schedule in idle if we're not in the right thread */
210 if (g_main_context_get_thread_default () != client->main_context)
211 schedule_call_in_idle (client, call_type);
213 actually_do_call (client, client->connection, call_type);
217 call_acquired_handler (Client *client)
219 if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
221 client->previous_call = PREVIOUS_CALL_ACQUIRED;
222 if (!client->cancelled)
224 do_call (client, CALL_TYPE_NAME_ACQUIRED);
230 call_lost_handler (Client *client)
232 if (client->previous_call != PREVIOUS_CALL_LOST)
234 client->previous_call = PREVIOUS_CALL_LOST;
235 if (!client->cancelled)
237 do_call (client, CALL_TYPE_NAME_LOST);
242 /* ---------------------------------------------------------------------------------------------------- */
245 on_name_lost_or_acquired (GDBusConnection *connection,
246 const gchar *sender_name,
247 const gchar *object_path,
248 const gchar *interface_name,
249 const gchar *signal_name,
250 GVariant *parameters,
253 Client *client = user_data;
256 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
257 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
258 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
261 if (g_strcmp0 (signal_name, "NameLost") == 0)
263 g_variant_get (parameters, "(s)", &name);
264 if (g_strcmp0 (name, client->name) == 0)
266 call_lost_handler (client);
269 else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
271 g_variant_get (parameters, "(s)", &name);
272 if (g_strcmp0 (name, client->name) == 0)
274 call_acquired_handler (client);
281 /* ---------------------------------------------------------------------------------------------------- */
284 request_name_cb (GObject *source_object,
288 Client *client = user_data;
290 guint32 request_name_reply;
293 request_name_reply = 0;
296 result = g_dbus_connection_invoke_method_finish (client->connection,
301 g_variant_get (result, "(u)", &request_name_reply);
302 g_variant_unref (result);
307 switch (request_name_reply)
309 case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
310 /* We got the name - now listen for NameLost and NameAcquired */
311 call_acquired_handler (client);
313 client->needs_release = TRUE;
316 case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
317 /* Waiting in line - listen for NameLost and NameAcquired */
318 call_lost_handler (client);
320 client->needs_release = TRUE;
324 /* assume we couldn't get the name - explicit fallthrough */
325 case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
326 case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
327 /* Some other part of the process is already owning the name */
328 call_lost_handler (client);
334 /* start listening to NameLost and NameAcquired messages */
335 client->name_lost_subscription_id =
336 g_dbus_connection_signal_subscribe (client->connection,
337 "org.freedesktop.DBus",
338 "org.freedesktop.DBus",
340 "/org/freedesktop/DBus",
342 on_name_lost_or_acquired,
345 client->name_acquired_subscription_id =
346 g_dbus_connection_signal_subscribe (client->connection,
347 "org.freedesktop.DBus",
348 "org.freedesktop.DBus",
350 "/org/freedesktop/DBus",
352 on_name_lost_or_acquired,
357 client_unref (client);
360 /* ---------------------------------------------------------------------------------------------------- */
363 on_connection_disconnected (GDBusConnection *connection,
364 gboolean remote_peer_vanished,
368 Client *client = user_data;
370 if (client->disconnected_signal_handler_id > 0)
371 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
372 if (client->name_acquired_subscription_id > 0)
373 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
374 if (client->name_lost_subscription_id > 0)
375 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
376 g_object_unref (client->connection);
377 client->disconnected_signal_handler_id = 0;
378 client->name_acquired_subscription_id = 0;
379 client->name_lost_subscription_id = 0;
380 client->connection = NULL;
382 call_lost_handler (client);
385 /* ---------------------------------------------------------------------------------------------------- */
388 has_connection (Client *client)
390 /* listen for disconnection */
391 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
393 G_CALLBACK (on_connection_disconnected),
396 /* attempt to acquire the name */
397 g_dbus_connection_invoke_method (client->connection,
398 "org.freedesktop.DBus", /* bus name */
399 "/org/freedesktop/DBus", /* object path */
400 "org.freedesktop.DBus", /* interface name */
401 "RequestName", /* method name */
402 g_variant_new ("(su)",
405 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
408 (GAsyncReadyCallback) request_name_cb,
409 client_ref (client));
414 connection_get_cb (GObject *source_object,
418 Client *client = user_data;
420 client->connection = g_bus_get_finish (res, NULL);
421 if (client->connection == NULL)
423 call_lost_handler (client);
427 /* No need to schedule this in idle as we're already in the thread
428 * that the user called g_bus_own_name() from. This is because
429 * g_bus_get() guarantees that.
431 * Also, we need to ensure that the handler is invoked *before*
432 * we call RequestName(). Otherwise there is a race.
434 if (client->bus_acquired_handler != NULL)
436 client->bus_acquired_handler (client->connection,
441 has_connection (client);
444 client_unref (client);
447 /* ---------------------------------------------------------------------------------------------------- */
450 * g_bus_own_name_on_connection:
451 * @connection: A #GDBusConnection that is not closed.
452 * @name: The well-known name to own.
453 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
454 * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
455 * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
456 * @user_data: User data to pass to handlers.
457 * @user_data_free_func: Function for freeing @user_data or %NULL.
459 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
462 * Returns: An identifier (never 0) that an be used with
463 * g_bus_unown_name() to stop owning the name.
468 g_bus_own_name_on_connection (GDBusConnection *connection,
470 GBusNameOwnerFlags flags,
471 GBusNameAcquiredCallback name_acquired_handler,
472 GBusNameLostCallback name_lost_handler,
474 GDestroyNotify user_data_free_func)
478 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
479 g_return_val_if_fail (!g_dbus_connection_is_closed (connection), 0);
480 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
484 client = g_new0 (Client, 1);
485 client->ref_count = 1;
486 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
487 client->name = g_strdup (name);
488 client->flags = flags;
489 client->name_acquired_handler = name_acquired_handler;
490 client->name_lost_handler = name_lost_handler;
491 client->user_data = user_data;
492 client->user_data_free_func = user_data_free_func;
493 client->main_context = g_main_context_get_thread_default ();
494 if (client->main_context != NULL)
495 g_main_context_ref (client->main_context);
497 client->connection = g_object_ref (connection);
499 if (map_id_to_client == NULL)
501 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
503 g_hash_table_insert (map_id_to_client,
504 GUINT_TO_POINTER (client->id),
509 has_connection (client);
516 * @bus_type: The type of bus to own a name on.
517 * @name: The well-known name to own.
518 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
519 * @bus_acquired_handler: Handler to invoke when connected to the bus of type @bus_type or %NULL.
520 * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
521 * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
522 * @user_data: User data to pass to handlers.
523 * @user_data_free_func: Function for freeing @user_data or %NULL.
525 * Starts acquiring @name on the bus specified by @bus_type and calls
526 * @name_acquired_handler and @name_lost_handler when the name is
527 * acquired respectively lost. Callbacks will be invoked in the <link
528 * linkend="g-main-context-push-thread-default">thread-default main
529 * loop</link> of the thread you are calling this function from.
531 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
532 * callbacks will be invoked after calling this function - there are three
536 * @name_lost_handler with a %NULL connection (if a connection to the bus can't be made).
539 * @bus_acquired_handler then @name_lost_handler (if the name can't be obtained)
542 * @bus_acquired_handler then @name_acquired_handler (if the name was obtained).
545 * When you are done owning the name, just call g_bus_unown_name()
546 * with the owner id this function returns.
548 * If the name is acquired or lost (for example another application
549 * could acquire the name if you allow replacement or the application
550 * currently owning the name exits), the handlers are also invoked. If the
551 * #GDBusConnection that is used for attempting to own the name
552 * closes, then @name_lost_handler is invoked since it is no
553 * longer possible for other processes to access the process.
555 * You cannot use g_bus_own_name() several times (unless interleaved
556 * with calls to g_bus_unown_name()) - only the first call will work.
558 * Another guarantee is that invocations of @name_acquired_handler
559 * and @name_lost_handler are guaranteed to alternate; that
560 * is, if @name_acquired_handler is invoked then you are
561 * guaranteed that the next time one of the handlers is invoked, it
562 * will be @name_lost_handler. The reverse is also true.
564 * If you plan on exporting objects (using e.g. g_dbus_connection_register_object()), note
565 * that it is generally too late to export the objects in @name_acquired_handler. Instead,
566 * you can do this in @bus_acquired_handler since you are guaranteed that this will
567 * run before @name is requested from the bus.
569 * This behavior makes it very simple to write applications that wants
570 * to own names and export objects, see <xref linkend="gdbus-owning-names"/>. Simply
571 * register objects to be exported in @bus_acquired_handler and
572 * unregister the objects (if any) in @name_lost_handler.
574 * Returns: An identifier (never 0) that an be used with
575 * g_bus_unown_name() to stop owning the name.
580 g_bus_own_name (GBusType bus_type,
582 GBusNameOwnerFlags flags,
583 GBusAcquiredCallback bus_acquired_handler,
584 GBusNameAcquiredCallback name_acquired_handler,
585 GBusNameLostCallback name_lost_handler,
587 GDestroyNotify user_data_free_func)
591 g_return_val_if_fail (bus_type != G_BUS_TYPE_NONE, 0);
592 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
596 client = g_new0 (Client, 1);
597 client->ref_count = 1;
598 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
599 client->name = g_strdup (name);
600 client->flags = flags;
601 client->bus_acquired_handler = bus_acquired_handler;
602 client->name_acquired_handler = name_acquired_handler;
603 client->name_lost_handler = name_lost_handler;
604 client->user_data = user_data;
605 client->user_data_free_func = user_data_free_func;
606 client->main_context = g_main_context_get_thread_default ();
607 if (client->main_context != NULL)
608 g_main_context_ref (client->main_context);
610 if (map_id_to_client == NULL)
612 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
614 g_hash_table_insert (map_id_to_client,
615 GUINT_TO_POINTER (client->id),
621 client_ref (client));
630 * @owner_id: An identifier obtained from g_bus_own_name()
632 * Stops owning a name.
637 g_bus_unown_name (guint owner_id)
641 g_return_if_fail (owner_id > 0);
646 if (owner_id == 0 || map_id_to_client == NULL ||
647 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
649 g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
653 client->cancelled = TRUE;
654 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
659 /* do callback without holding lock */
662 /* Release the name if needed */
663 if (client->needs_release && client->connection != NULL)
667 guint32 release_name_reply;
669 /* TODO: it kinda sucks having to do a sync call to release the name - but if
670 * we don't, then a subsequent grab of the name will make the bus daemon return
671 * IN_QUEUE which will trigger name_lost().
673 * I believe this is a bug in the bus daemon.
676 result = g_dbus_connection_invoke_method_sync (client->connection,
677 "org.freedesktop.DBus", /* bus name */
678 "/org/freedesktop/DBus", /* object path */
679 "org.freedesktop.DBus", /* interface name */
680 "ReleaseName", /* method name */
681 g_variant_new ("(s)", client->name),
682 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
688 g_warning ("Error releasing name %s: %s", client->name, error->message);
689 g_error_free (error);
693 g_variant_get (result, "(u)", &release_name_reply);
694 if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
696 g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
698 g_variant_unref (result);
702 if (client->disconnected_signal_handler_id > 0)
703 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
704 if (client->name_acquired_subscription_id > 0)
705 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
706 if (client->name_lost_subscription_id > 0)
707 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
708 client->disconnected_signal_handler_id = 0;
709 client->name_acquired_subscription_id = 0;
710 client->name_lost_subscription_id = 0;
711 if (client->connection != NULL)
713 g_object_unref (client->connection);
714 client->connection = NULL;
717 client_unref (client);