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>
26 * - would be nice to expose GDBusAuthMechanism and an extension point
28 * - Need to rewrite GDBusAuth and rework GDBusAuthMechanism. In particular
29 * the mechanism VFuncs need to be able to set an error.
31 * - Need to document other mechanisms/sources for determining the D-Bus
32 * address of a well-known bus.
34 * - e.g. on Win32 we need code like from here
36 * http://cgit.freedesktop.org/~david/gdbus-standalone/tree/gdbus/gdbusaddress.c#n900
38 * that was never copied over here because it originally was copy-paste
39 * from the GPLv2 / AFL 2.1 libdbus sources.
41 * - on OS X we need to look in launchd for the address
43 * https://bugs.freedesktop.org/show_bug.cgi?id=14259
45 * - on X11 we need to look in a X11 property on the X server
46 * - (we can also just use dbus-launch(1) from the D-Bus
49 * - (ideally) this requires D-Bus spec work because none of
50 * this has never really been specced out properly (except
53 * - Related to the above, we also need to be able to launch a message bus
54 * instance.... Since we don't want to write our own bus daemon we should
55 * launch dbus-daemon(1) (thus: Win32 and OS X need to bundle it)
57 * - probably want a G_DBUS_NONCE_TCP_TMPDIR environment variable
58 * to specify where the nonce is stored. This will allow people to use
59 * G_DBUS_NONCE_TCP_TMPDIR=/mnt/secure.company.server/dbus-nonce-dir
60 * to easily achieve secure RPC via nonce-tcp.
62 * - need to expose an extension point for resolving D-Bus address and
63 * turning them into GIOStream objects. This will allow us to implement
64 * e.g. X11 D-Bus transports without dlopen()'ing or linking against
66 * - see g_dbus_address_connect() in gdbusaddress.c
68 * - would be cute to use kernel-specific APIs to resolve fds for
69 * debug output when using G_DBUS_DEBUG=message, e.g. in addition to
71 * fd 21: dev=8:1,mode=0100644,ino=1171231,uid=0,gid=0,rdev=0:0,size=234,atime=1273070640,mtime=1267126160,ctime=1267126160
73 * maybe we can show more information about what fd 21 really is.
74 * Ryan suggests looking in /proc/self/fd for clues / symlinks!
75 * Initial experiments on Linux 2.6 suggests that the symlink looks
80 * e.g. not of much use.
82 * - GDBus High-Level docs
83 * - Proxy: properties, signals...
84 * - Connection: IOStream based, ::close, connection setup steps
85 * mainloop integration, threading
86 * - Differences from libdbus (extend "Migrating from")
87 * - the message handling thread
88 * - Using GVariant instead of GValue
89 * - Explain why the high-level API is a good thing and what
90 * kind of pitfalls it avoids
91 * - Export objects before claiming names
92 * - Talk about auto-starting services (cf. GBusNameWatcherFlags)
94 * - use abstract sockets in test code
95 * - right now it doesn't work, dbus-daemon(1) fails with
97 * /gdbus/connection/filter: Failed to start message bus: Failed to bind
98 * socket "/tmp/g-dbus-tests-pid-28531": Address already in use
99 * ** WARNING **: Error reading address from dbus daemon, 0 bytes read
108 #include <sys/types.h>
113 #include "gdbusauth.h"
114 #include "gdbusutils.h"
115 #include "gdbusaddress.h"
116 #include "gdbusmessage.h"
117 #include "gdbusconnection.h"
118 #include "gdbuserror.h"
119 #include "gioenumtypes.h"
120 #include "gdbusintrospection.h"
121 #include "gdbusmethodinvocation.h"
122 #include "gdbusprivate.h"
123 #include "gdbusauthobserver.h"
124 #include "ginitable.h"
125 #include "gasyncinitable.h"
126 #include "giostream.h"
127 #include "gasyncresult.h"
128 #include "gsimpleasyncresult.h"
131 #include "gunixconnection.h"
132 #include "gunixfdmessage.h"
135 #include "glibintl.h"
138 * SECTION:gdbusconnection
139 * @short_description: D-Bus Connections
140 * @include: gio/gio.h
142 * The #GDBusConnection type is used for D-Bus connections to remote
143 * peers such as a message buses. It is a low-level API that offers a
144 * lot of flexibility. For instance, it lets you establish a connection
145 * over any transport that can by represented as an #GIOStream.
147 * This class is rarely used directly in D-Bus clients. If you are writing
148 * an D-Bus client, it is often easier to use the g_bus_own_name(),
149 * g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs.
151 * <example id="gdbus-server"><title>D-Bus server example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-server.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
153 * <example id="gdbus-subtree-server"><title>D-Bus subtree example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-subtree.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
155 * <example id="gdbus-unix-fd-client"><title>D-Bus UNIX File Descriptor example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-unix-fd-client.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
157 * <example id="gdbus-export"><title>Exporting a GObject</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-export.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
160 /* ---------------------------------------------------------------------------------------------------- */
162 typedef struct _GDBusConnectionClass GDBusConnectionClass;
165 * GDBusConnectionClass:
166 * @closed: Signal class handler for the #GDBusConnection::closed signal.
168 * Class structure for #GDBusConnection.
172 struct _GDBusConnectionClass
175 GObjectClass parent_class;
179 void (*closed) (GDBusConnection *connection,
180 gboolean remote_peer_vanished,
184 G_LOCK_DEFINE_STATIC (message_bus_lock);
186 static GDBusConnection *the_session_bus = NULL;
187 static GDBusConnection *the_system_bus = NULL;
189 /* ---------------------------------------------------------------------------------------------------- */
193 GDestroyNotify callback;
195 GMainContext *context;
196 } CallDestroyNotifyData;
199 call_destroy_notify_data_in_idle (gpointer user_data)
201 CallDestroyNotifyData *data = user_data;
202 data->callback (data->user_data);
207 call_destroy_notify_data_free (CallDestroyNotifyData *data)
209 if (data->context != NULL)
210 g_main_context_unref (data->context);
215 * call_destroy_notify: <internal>
216 * @context: A #GMainContext or %NULL.
217 * @callback: A #GDestroyNotify or %NULL.
218 * @user_data: Data to pass to @callback.
220 * Schedules @callback to run in @context.
223 call_destroy_notify (GMainContext *context,
224 GDestroyNotify callback,
227 GMainContext *current_context;
229 if (callback == NULL)
232 current_context = g_main_context_get_thread_default ();
233 if ((context == current_context) ||
234 (current_context == NULL && context == g_main_context_default ()))
236 callback (user_data);
240 GSource *idle_source;
241 CallDestroyNotifyData *data;
243 data = g_new0 (CallDestroyNotifyData, 1);
244 data->callback = callback;
245 data->user_data = user_data;
246 data->context = context;
247 if (data->context != NULL)
248 g_main_context_ref (data->context);
250 idle_source = g_idle_source_new ();
251 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
252 g_source_set_callback (idle_source,
253 call_destroy_notify_data_in_idle,
255 (GDestroyNotify) call_destroy_notify_data_free);
256 g_source_attach (idle_source, data->context);
257 g_source_unref (idle_source);
264 /* ---------------------------------------------------------------------------------------------------- */
267 _g_strv_has_string (const gchar* const *haystack,
272 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
274 if (g_strcmp0 (haystack[n], needle) == 0)
280 /* ---------------------------------------------------------------------------------------------------- */
283 #define CONNECTION_ENSURE_LOCK(obj) do { ; } while (FALSE)
285 // TODO: for some reason this doesn't work on Windows
286 #define CONNECTION_ENSURE_LOCK(obj) do { \
287 if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock))) \
289 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
290 "CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \
295 #define CONNECTION_LOCK(obj) do { \
296 g_mutex_lock (&(obj)->lock); \
299 #define CONNECTION_UNLOCK(obj) do { \
300 g_mutex_unlock (&(obj)->lock); \
306 * The #GDBusConnection structure contains only private data and
307 * should only be accessed using the provided API.
311 struct _GDBusConnection
314 GObject parent_instance;
316 /* ------------------------------------------------------------------------ */
317 /* -- General object state ------------------------------------------------ */
318 /* ------------------------------------------------------------------------ */
320 /* object-wide lock */
323 /* A lock used in the init() method of the GInitable interface - see comments
324 * in initable_init() for why a separate lock is needed
328 /* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
329 * someone calls org.freedesktop.DBus.GetMachineId()
333 /* The underlying stream used for communication */
336 /* The object used for authentication (if any) */
339 /* Set to TRUE if the connection has been closed */
342 /* Last serial used */
345 /* The object used to send/receive message */
348 /* If connected to a message bus, this contains the unique name assigned to
349 * us by the bus (e.g. ":1.42")
351 gchar *bus_unique_name;
353 /* The GUID returned by the other side if we authenticed as a client or
354 * the GUID to use if authenticating as a server
358 /* set to TRUE exactly when initable_init() has finished running */
359 gboolean is_initialized;
361 /* If the connection could not be established during initable_init(), this GError will set */
362 GError *initialization_error;
364 /* The result of g_main_context_ref_thread_default() when the object
365 * was created (the GObject _init() function) - this is used for delivery
366 * of the :closed GObject signal.
368 GMainContext *main_context_at_construction;
370 /* construct properties */
372 GDBusConnectionFlags flags;
374 /* Map used for managing method replies */
375 GHashTable *map_method_serial_to_send_message_data; /* guint32 -> SendMessageData* */
377 /* Maps used for managing signal subscription */
378 GHashTable *map_rule_to_signal_data; /* match rule (gchar*) -> SignalData */
379 GHashTable *map_id_to_signal_data; /* id (guint) -> SignalData */
380 GHashTable *map_sender_unique_name_to_signal_data_array; /* unique sender (gchar*) -> GPtrArray* of SignalData */
382 /* Maps used for managing exported objects and subtrees */
383 GHashTable *map_object_path_to_eo; /* gchar* -> ExportedObject* */
384 GHashTable *map_id_to_ei; /* guint -> ExportedInterface* */
385 GHashTable *map_object_path_to_es; /* gchar* -> ExportedSubtree* */
386 GHashTable *map_id_to_es; /* guint -> ExportedSubtree* */
388 /* Structure used for message filters */
391 /* Whether to exit on close */
392 gboolean exit_on_close;
394 /* Capabilities negotiated during authentication */
395 GDBusCapabilityFlags capabilities;
397 GDBusAuthObserver *authentication_observer;
398 GCredentials *credentials;
400 /* set to TRUE when finalizing */
404 typedef struct ExportedObject ExportedObject;
405 static void exported_object_free (ExportedObject *eo);
407 typedef struct ExportedSubtree ExportedSubtree;
408 static void exported_subtree_free (ExportedSubtree *es);
426 PROP_CAPABILITY_FLAGS,
427 PROP_AUTHENTICATION_OBSERVER,
430 static void distribute_signals (GDBusConnection *connection,
431 GDBusMessage *message);
433 static void distribute_method_call (GDBusConnection *connection,
434 GDBusMessage *message);
436 static gboolean handle_generic_unlocked (GDBusConnection *connection,
437 GDBusMessage *message);
440 static void purge_all_signal_subscriptions (GDBusConnection *connection);
441 static void purge_all_filters (GDBusConnection *connection);
443 #define _G_ENSURE_LOCK(name) do { \
444 if (G_UNLIKELY (G_TRYLOCK(name))) \
446 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
447 "_G_ENSURE_LOCK: Lock `" #name "' is not locked"); \
451 static guint signals[LAST_SIGNAL] = { 0 };
453 static void initable_iface_init (GInitableIface *initable_iface);
454 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
456 G_DEFINE_TYPE_WITH_CODE (GDBusConnection, g_dbus_connection, G_TYPE_OBJECT,
457 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
458 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
461 static GHashTable *alive_connections = NULL;
464 g_dbus_connection_dispose (GObject *object)
466 GDBusConnection *connection = G_DBUS_CONNECTION (object);
468 G_LOCK (message_bus_lock);
469 if (connection == the_session_bus)
471 the_session_bus = NULL;
473 else if (connection == the_system_bus)
475 the_system_bus = NULL;
477 CONNECTION_LOCK (connection);
478 if (connection->worker != NULL)
480 _g_dbus_worker_stop (connection->worker);
481 connection->worker = NULL;
482 if (alive_connections != NULL)
483 g_warn_if_fail (g_hash_table_remove (alive_connections, connection));
487 if (alive_connections != NULL)
488 g_warn_if_fail (g_hash_table_lookup (alive_connections, connection) == NULL);
490 CONNECTION_UNLOCK (connection);
491 G_UNLOCK (message_bus_lock);
493 if (G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose != NULL)
494 G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose (object);
498 g_dbus_connection_finalize (GObject *object)
500 GDBusConnection *connection = G_DBUS_CONNECTION (object);
502 connection->finalizing = TRUE;
504 purge_all_signal_subscriptions (connection);
506 purge_all_filters (connection);
507 g_ptr_array_unref (connection->filters);
509 if (connection->authentication_observer != NULL)
510 g_object_unref (connection->authentication_observer);
512 if (connection->auth != NULL)
513 g_object_unref (connection->auth);
515 if (connection->credentials)
516 g_object_unref (connection->credentials);
518 if (connection->stream != NULL)
520 g_object_unref (connection->stream);
521 connection->stream = NULL;
524 g_free (connection->address);
526 g_free (connection->guid);
527 g_free (connection->bus_unique_name);
529 if (connection->initialization_error != NULL)
530 g_error_free (connection->initialization_error);
532 g_hash_table_unref (connection->map_method_serial_to_send_message_data);
534 g_hash_table_unref (connection->map_rule_to_signal_data);
535 g_hash_table_unref (connection->map_id_to_signal_data);
536 g_hash_table_unref (connection->map_sender_unique_name_to_signal_data_array);
538 g_hash_table_unref (connection->map_id_to_ei);
539 g_hash_table_unref (connection->map_object_path_to_eo);
540 g_hash_table_unref (connection->map_id_to_es);
541 g_hash_table_unref (connection->map_object_path_to_es);
543 g_main_context_unref (connection->main_context_at_construction);
545 g_free (connection->machine_id);
547 g_mutex_clear (&connection->init_lock);
548 g_mutex_clear (&connection->lock);
550 G_OBJECT_CLASS (g_dbus_connection_parent_class)->finalize (object);
554 g_dbus_connection_get_property (GObject *object,
559 GDBusConnection *connection = G_DBUS_CONNECTION (object);
564 g_value_set_object (value, g_dbus_connection_get_stream (connection));
568 g_value_set_string (value, g_dbus_connection_get_guid (connection));
571 case PROP_UNIQUE_NAME:
572 g_value_set_string (value, g_dbus_connection_get_unique_name (connection));
576 g_value_set_boolean (value, g_dbus_connection_is_closed (connection));
579 case PROP_EXIT_ON_CLOSE:
580 g_value_set_boolean (value, g_dbus_connection_get_exit_on_close (connection));
583 case PROP_CAPABILITY_FLAGS:
584 g_value_set_flags (value, g_dbus_connection_get_capabilities (connection));
588 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
594 g_dbus_connection_set_property (GObject *object,
599 GDBusConnection *connection = G_DBUS_CONNECTION (object);
604 connection->stream = g_value_dup_object (value);
608 connection->guid = g_value_dup_string (value);
612 connection->address = g_value_dup_string (value);
616 connection->flags = g_value_get_flags (value);
619 case PROP_EXIT_ON_CLOSE:
620 g_dbus_connection_set_exit_on_close (connection, g_value_get_boolean (value));
623 case PROP_AUTHENTICATION_OBSERVER:
624 connection->authentication_observer = g_value_dup_object (value);
628 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
634 g_dbus_connection_real_closed (GDBusConnection *connection,
635 gboolean remote_peer_vanished,
638 if (remote_peer_vanished && connection->exit_on_close && connection->is_initialized)
642 g_print ("%s: Remote peer vanished with error: %s (%s, %d). Exiting.\n",
645 g_quark_to_string (error->domain), error->code);
649 g_print ("%s: Remote peer vanished. Exiting.\n", G_STRFUNC);
656 g_dbus_connection_class_init (GDBusConnectionClass *klass)
658 GObjectClass *gobject_class;
660 gobject_class = G_OBJECT_CLASS (klass);
662 gobject_class->finalize = g_dbus_connection_finalize;
663 gobject_class->dispose = g_dbus_connection_dispose;
664 gobject_class->set_property = g_dbus_connection_set_property;
665 gobject_class->get_property = g_dbus_connection_get_property;
667 klass->closed = g_dbus_connection_real_closed;
670 * GDBusConnection:stream:
672 * The underlying #GIOStream used for I/O.
674 * If this is passed on construction and is a #GSocketConnection,
675 * then the corresponding #GSocket will be put into non-blocking mode.
677 * While the #GDBusConnection is active, it will interact with this
678 * stream from a worker thread, so it is not safe to interact with
679 * the stream directly.
683 g_object_class_install_property (gobject_class,
685 g_param_spec_object ("stream",
687 P_("The underlying streams used for I/O"),
691 G_PARAM_CONSTRUCT_ONLY |
692 G_PARAM_STATIC_NAME |
693 G_PARAM_STATIC_BLURB |
694 G_PARAM_STATIC_NICK));
697 * GDBusConnection:address:
699 * A D-Bus address specifying potential endpoints that can be used
700 * when establishing the connection.
704 g_object_class_install_property (gobject_class,
706 g_param_spec_string ("address",
708 P_("D-Bus address specifying potential socket endpoints"),
711 G_PARAM_CONSTRUCT_ONLY |
712 G_PARAM_STATIC_NAME |
713 G_PARAM_STATIC_BLURB |
714 G_PARAM_STATIC_NICK));
717 * GDBusConnection:flags:
719 * Flags from the #GDBusConnectionFlags enumeration.
723 g_object_class_install_property (gobject_class,
725 g_param_spec_flags ("flags",
728 G_TYPE_DBUS_CONNECTION_FLAGS,
729 G_DBUS_CONNECTION_FLAGS_NONE,
731 G_PARAM_CONSTRUCT_ONLY |
732 G_PARAM_STATIC_NAME |
733 G_PARAM_STATIC_BLURB |
734 G_PARAM_STATIC_NICK));
737 * GDBusConnection:guid:
739 * The GUID of the peer performing the role of server when
742 * If you are constructing a #GDBusConnection and pass
743 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER in the
744 * #GDBusConnection:flags property then you MUST also set this
745 * property to a valid guid.
747 * If you are constructing a #GDBusConnection and pass
748 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT in the
749 * #GDBusConnection:flags property you will be able to read the GUID
750 * of the other peer here after the connection has been successfully
755 g_object_class_install_property (gobject_class,
757 g_param_spec_string ("guid",
759 P_("GUID of the server peer"),
763 G_PARAM_CONSTRUCT_ONLY |
764 G_PARAM_STATIC_NAME |
765 G_PARAM_STATIC_BLURB |
766 G_PARAM_STATIC_NICK));
769 * GDBusConnection:unique-name:
771 * The unique name as assigned by the message bus or %NULL if the
772 * connection is not open or not a message bus connection.
776 g_object_class_install_property (gobject_class,
778 g_param_spec_string ("unique-name",
780 P_("Unique name of bus connection"),
783 G_PARAM_STATIC_NAME |
784 G_PARAM_STATIC_BLURB |
785 G_PARAM_STATIC_NICK));
788 * GDBusConnection:closed:
790 * A boolean specifying whether the connection has been closed.
794 g_object_class_install_property (gobject_class,
796 g_param_spec_boolean ("closed",
798 P_("Whether the connection is closed"),
801 G_PARAM_STATIC_NAME |
802 G_PARAM_STATIC_BLURB |
803 G_PARAM_STATIC_NICK));
806 * GDBusConnection:exit-on-close:
808 * A boolean specifying whether the process will be terminated (by
809 * calling <literal>raise(SIGTERM)</literal>) if the connection
810 * is closed by the remote peer.
814 g_object_class_install_property (gobject_class,
816 g_param_spec_boolean ("exit-on-close",
818 P_("Whether the process is terminated when the connection is closed"),
822 G_PARAM_STATIC_NAME |
823 G_PARAM_STATIC_BLURB |
824 G_PARAM_STATIC_NICK));
827 * GDBusConnection:capabilities:
829 * Flags from the #GDBusCapabilityFlags enumeration
830 * representing connection features negotiated with the other peer.
834 g_object_class_install_property (gobject_class,
835 PROP_CAPABILITY_FLAGS,
836 g_param_spec_flags ("capabilities",
839 G_TYPE_DBUS_CAPABILITY_FLAGS,
840 G_DBUS_CAPABILITY_FLAGS_NONE,
842 G_PARAM_STATIC_NAME |
843 G_PARAM_STATIC_BLURB |
844 G_PARAM_STATIC_NICK));
847 * GDBusConnection:authentication-observer:
849 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
853 g_object_class_install_property (gobject_class,
854 PROP_AUTHENTICATION_OBSERVER,
855 g_param_spec_object ("authentication-observer",
856 P_("Authentication Observer"),
857 P_("Object used to assist in the authentication process"),
858 G_TYPE_DBUS_AUTH_OBSERVER,
860 G_PARAM_CONSTRUCT_ONLY |
861 G_PARAM_STATIC_NAME |
862 G_PARAM_STATIC_BLURB |
863 G_PARAM_STATIC_NICK));
866 * GDBusConnection::closed:
867 * @connection: The #GDBusConnection emitting the signal.
868 * @remote_peer_vanished: %TRUE if @connection is closed because the
869 * remote peer closed its end of the connection.
870 * @error: A #GError with more details about the event or %NULL.
872 * Emitted when the connection is closed.
874 * The cause of this event can be
877 * If g_dbus_connection_close() is called. In this case
878 * @remote_peer_vanished is set to %FALSE and @error is %NULL.
881 * If the remote peer closes the connection. In this case
882 * @remote_peer_vanished is set to %TRUE and @error is set.
885 * If the remote peer sends invalid or malformed data. In this
886 * case @remote_peer_vanished is set to %FALSE and @error
891 * Upon receiving this signal, you should give up your reference to
892 * @connection. You are guaranteed that this signal is emitted only
897 signals[CLOSED_SIGNAL] = g_signal_new ("closed",
898 G_TYPE_DBUS_CONNECTION,
900 G_STRUCT_OFFSET (GDBusConnectionClass, closed),
911 g_dbus_connection_init (GDBusConnection *connection)
913 g_mutex_init (&connection->lock);
914 g_mutex_init (&connection->init_lock);
916 connection->map_method_serial_to_send_message_data = g_hash_table_new (g_direct_hash, g_direct_equal);
918 connection->map_rule_to_signal_data = g_hash_table_new (g_str_hash,
920 connection->map_id_to_signal_data = g_hash_table_new (g_direct_hash,
922 connection->map_sender_unique_name_to_signal_data_array = g_hash_table_new_full (g_str_hash,
925 (GDestroyNotify) g_ptr_array_unref);
927 connection->map_object_path_to_eo = g_hash_table_new_full (g_str_hash,
930 (GDestroyNotify) exported_object_free);
932 connection->map_id_to_ei = g_hash_table_new (g_direct_hash,
935 connection->map_object_path_to_es = g_hash_table_new_full (g_str_hash,
938 (GDestroyNotify) exported_subtree_free);
940 connection->map_id_to_es = g_hash_table_new (g_direct_hash,
943 connection->main_context_at_construction = g_main_context_ref_thread_default ();
945 connection->filters = g_ptr_array_new ();
949 * g_dbus_connection_get_stream:
950 * @connection: a #GDBusConnection
952 * Gets the underlying stream used for IO.
954 * While the #GDBusConnection is active, it will interact with this
955 * stream from a worker thread, so it is not safe to interact with
956 * the stream directly.
958 * Returns: (transfer none): the stream used for IO
963 g_dbus_connection_get_stream (GDBusConnection *connection)
965 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
966 return connection->stream;
970 * g_dbus_connection_start_message_processing:
971 * @connection: A #GDBusConnection.
973 * If @connection was created with
974 * %G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, this method
975 * starts processing messages. Does nothing on if @connection wasn't
976 * created with this flag or if the method has already been called.
981 g_dbus_connection_start_message_processing (GDBusConnection *connection)
983 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
984 _g_dbus_worker_unfreeze (connection->worker);
988 * g_dbus_connection_is_closed:
989 * @connection: A #GDBusConnection.
991 * Gets whether @connection is closed.
993 * Returns: %TRUE if the connection is closed, %FALSE otherwise.
998 g_dbus_connection_is_closed (GDBusConnection *connection)
1000 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1001 return connection->closed;
1005 * g_dbus_connection_get_capabilities:
1006 * @connection: A #GDBusConnection.
1008 * Gets the capabilities negotiated with the remote peer
1010 * Returns: Zero or more flags from the #GDBusCapabilityFlags enumeration.
1014 GDBusCapabilityFlags
1015 g_dbus_connection_get_capabilities (GDBusConnection *connection)
1017 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CAPABILITY_FLAGS_NONE);
1018 return connection->capabilities;
1021 /* ---------------------------------------------------------------------------------------------------- */
1024 flush_in_thread_func (GSimpleAsyncResult *res,
1026 GCancellable *cancellable)
1031 if (!g_dbus_connection_flush_sync (G_DBUS_CONNECTION (object),
1034 g_simple_async_result_take_error (res, error);
1038 * g_dbus_connection_flush:
1039 * @connection: A #GDBusConnection.
1040 * @cancellable: A #GCancellable or %NULL.
1041 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
1042 * satisfied or %NULL if you don't care about the result.
1043 * @user_data: The data to pass to @callback.
1045 * Asynchronously flushes @connection, that is, writes all queued
1046 * outgoing message to the transport and then flushes the transport
1047 * (using g_output_stream_flush_async()). This is useful in programs
1048 * that wants to emit a D-Bus signal and then exit
1049 * immediately. Without flushing the connection, there is no guarantee
1050 * that the message has been sent to the networking buffers in the OS
1053 * This is an asynchronous method. When the operation is finished,
1054 * @callback will be invoked in the <link
1055 * linkend="g-main-context-push-thread-default">thread-default main
1056 * loop</link> of the thread you are calling this method from. You can
1057 * then call g_dbus_connection_flush_finish() to get the result of the
1058 * operation. See g_dbus_connection_flush_sync() for the synchronous
1064 g_dbus_connection_flush (GDBusConnection *connection,
1065 GCancellable *cancellable,
1066 GAsyncReadyCallback callback,
1069 GSimpleAsyncResult *simple;
1071 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1073 simple = g_simple_async_result_new (G_OBJECT (connection),
1076 g_dbus_connection_flush);
1077 g_simple_async_result_run_in_thread (simple,
1078 flush_in_thread_func,
1081 g_object_unref (simple);
1085 * g_dbus_connection_flush_finish:
1086 * @connection: A #GDBusConnection.
1087 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_flush().
1088 * @error: Return location for error or %NULL.
1090 * Finishes an operation started with g_dbus_connection_flush().
1092 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1097 g_dbus_connection_flush_finish (GDBusConnection *connection,
1101 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1106 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1107 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
1108 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1110 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_flush);
1112 if (g_simple_async_result_propagate_error (simple, error))
1122 * g_dbus_connection_flush_sync:
1123 * @connection: A #GDBusConnection.
1124 * @cancellable: A #GCancellable or %NULL.
1125 * @error: Return location for error or %NULL.
1127 * Synchronously flushes @connection. The calling thread is blocked
1128 * until this is done. See g_dbus_connection_flush() for the
1129 * asynchronous version of this method and more details about what it
1132 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1137 g_dbus_connection_flush_sync (GDBusConnection *connection,
1138 GCancellable *cancellable,
1143 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1144 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1148 if (connection->closed)
1150 g_set_error_literal (error,
1153 _("The connection is closed"));
1157 ret = _g_dbus_worker_flush_sync (connection->worker,
1165 /* ---------------------------------------------------------------------------------------------------- */
1169 GDBusConnection *connection;
1171 gboolean remote_peer_vanished;
1175 emit_closed_data_free (EmitClosedData *data)
1177 g_object_unref (data->connection);
1178 if (data->error != NULL)
1179 g_error_free (data->error);
1184 emit_closed_in_idle (gpointer user_data)
1186 EmitClosedData *data = user_data;
1189 g_object_notify (G_OBJECT (data->connection), "closed");
1190 g_signal_emit (data->connection,
1191 signals[CLOSED_SIGNAL],
1193 data->remote_peer_vanished,
1199 /* Can be called from any thread, must hold lock */
1201 set_closed_unlocked (GDBusConnection *connection,
1202 gboolean remote_peer_vanished,
1205 GSource *idle_source;
1206 EmitClosedData *data;
1208 CONNECTION_ENSURE_LOCK (connection);
1210 g_assert (!connection->closed);
1212 connection->closed = TRUE;
1214 data = g_new0 (EmitClosedData, 1);
1215 data->connection = g_object_ref (connection);
1216 data->remote_peer_vanished = remote_peer_vanished;
1217 data->error = error != NULL ? g_error_copy (error) : NULL;
1219 idle_source = g_idle_source_new ();
1220 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1221 g_source_set_callback (idle_source,
1222 emit_closed_in_idle,
1224 (GDestroyNotify) emit_closed_data_free);
1225 g_source_attach (idle_source, connection->main_context_at_construction);
1226 g_source_unref (idle_source);
1229 /* ---------------------------------------------------------------------------------------------------- */
1232 * g_dbus_connection_close:
1233 * @connection: A #GDBusConnection.
1234 * @cancellable: A #GCancellable or %NULL.
1235 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
1236 * satisfied or %NULL if you don't care about the result.
1237 * @user_data: The data to pass to @callback.
1239 * Closes @connection. Note that this never causes the process to
1240 * exit (this might only happen if the other end of a shared message
1241 * bus connection disconnects, see #GDBusConnection:exit-on-close).
1243 * Once the connection is closed, operations such as sending a message
1244 * will return with the error %G_IO_ERROR_CLOSED. Closing a connection
1245 * will not automatically flush the connection so queued messages may
1246 * be lost. Use g_dbus_connection_flush() if you need such guarantees.
1248 * If @connection is already closed, this method fails with
1249 * %G_IO_ERROR_CLOSED.
1251 * When @connection has been closed, the #GDBusConnection::closed
1252 * signal is emitted in the <link
1253 * linkend="g-main-context-push-thread-default">thread-default main
1254 * loop</link> of the thread that @connection was constructed in.
1256 * This is an asynchronous method. When the operation is finished,
1257 * @callback will be invoked in the <link
1258 * linkend="g-main-context-push-thread-default">thread-default main
1259 * loop</link> of the thread you are calling this method from. You can
1260 * then call g_dbus_connection_close_finish() to get the result of the
1261 * operation. See g_dbus_connection_close_sync() for the synchronous
1267 g_dbus_connection_close (GDBusConnection *connection,
1268 GCancellable *cancellable,
1269 GAsyncReadyCallback callback,
1272 GSimpleAsyncResult *simple;
1274 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1276 simple = g_simple_async_result_new (G_OBJECT (connection),
1279 g_dbus_connection_close);
1280 _g_dbus_worker_close (connection->worker, cancellable, simple);
1281 g_object_unref (simple);
1285 * g_dbus_connection_close_finish:
1286 * @connection: A #GDBusConnection.
1287 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_close().
1288 * @error: Return location for error or %NULL.
1290 * Finishes an operation started with g_dbus_connection_close().
1292 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1297 g_dbus_connection_close_finish (GDBusConnection *connection,
1301 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1306 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1307 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
1308 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1310 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_close);
1312 if (g_simple_async_result_propagate_error (simple, error))
1323 GAsyncResult *result;
1327 sync_close_cb (GObject *source_object,
1331 SyncCloseData *data = user_data;
1333 data->result = g_object_ref (res);
1334 g_main_loop_quit (data->loop);
1338 * g_dbus_connection_close_sync:
1339 * @connection: A #GDBusConnection.
1340 * @cancellable: A #GCancellable or %NULL.
1341 * @error: Return location for error or %NULL.
1343 * Synchronously closees @connection. The calling thread is blocked
1344 * until this is done. See g_dbus_connection_close() for the
1345 * asynchronous version of this method and more details about what it
1348 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1353 g_dbus_connection_close_sync (GDBusConnection *connection,
1354 GCancellable *cancellable,
1359 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1360 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1364 CONNECTION_LOCK (connection);
1365 if (!connection->closed)
1367 GMainContext *context;
1370 context = g_main_context_new ();
1371 g_main_context_push_thread_default (context);
1372 data.loop = g_main_loop_new (context, TRUE);
1375 CONNECTION_UNLOCK (connection);
1376 g_dbus_connection_close (connection, cancellable, sync_close_cb, &data);
1377 g_main_loop_run (data.loop);
1378 ret = g_dbus_connection_close_finish (connection, data.result, error);
1379 CONNECTION_LOCK (connection);
1381 g_object_unref (data.result);
1382 g_main_loop_unref (data.loop);
1383 g_main_context_pop_thread_default (context);
1384 g_main_context_unref (context);
1388 g_set_error_literal (error,
1391 _("The connection is closed"));
1393 CONNECTION_UNLOCK (connection);
1398 /* ---------------------------------------------------------------------------------------------------- */
1401 g_dbus_connection_send_message_unlocked (GDBusConnection *connection,
1402 GDBusMessage *message,
1403 GDBusSendMessageFlags flags,
1404 volatile guint32 *out_serial,
1409 guint32 serial_to_use;
1412 CONNECTION_ENSURE_LOCK (connection);
1414 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1415 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1417 /* TODO: check all necessary headers are present */
1422 if (out_serial != NULL)
1425 if (connection->closed)
1427 g_set_error_literal (error,
1430 _("The connection is closed"));
1434 blob = g_dbus_message_to_blob (message,
1436 connection->capabilities,
1441 if (flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL)
1442 serial_to_use = g_dbus_message_get_serial (message);
1444 serial_to_use = ++connection->last_serial; /* TODO: handle overflow */
1449 ((guint32 *) blob)[2] = GUINT32_TO_LE (serial_to_use);
1452 ((guint32 *) blob)[2] = GUINT32_TO_BE (serial_to_use);
1455 g_assert_not_reached ();
1460 g_printerr ("Writing message of %" G_GSIZE_FORMAT " bytes (serial %d) on %p:\n",
1461 blob_size, serial_to_use, connection);
1462 g_printerr ("----\n");
1463 hexdump (blob, blob_size);
1464 g_printerr ("----\n");
1467 /* TODO: use connection->auth to encode the blob */
1469 if (out_serial != NULL)
1470 *out_serial = serial_to_use;
1472 if (!(flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL))
1473 g_dbus_message_set_serial (message, serial_to_use);
1475 g_dbus_message_lock (message);
1476 _g_dbus_worker_send_message (connection->worker,
1480 blob = NULL; /* since _g_dbus_worker_send_message() steals the blob */
1491 * g_dbus_connection_send_message:
1492 * @connection: A #GDBusConnection.
1493 * @message: A #GDBusMessage
1494 * @flags: Flags affecting how the message is sent.
1495 * @out_serial: (out) (allow-none): Return location for serial number assigned
1496 * to @message when sending it or %NULL.
1497 * @error: Return location for error or %NULL.
1499 * Asynchronously sends @message to the peer represented by @connection.
1501 * Unless @flags contain the
1502 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1503 * will be assigned by @connection and set on @message via
1504 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1505 * serial number used will be written to this location prior to
1506 * submitting the message to the underlying transport.
1508 * If @connection is closed then the operation will fail with
1509 * %G_IO_ERROR_CLOSED. If @message is not well-formed,
1510 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1512 * See <xref linkend="gdbus-server"/> and <xref
1513 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1514 * low-level API to send and receive UNIX file descriptors.
1516 * Note that @message must be unlocked, unless @flags contain the
1517 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1519 * Returns: %TRUE if the message was well-formed and queued for
1520 * transmission, %FALSE if @error is set.
1525 g_dbus_connection_send_message (GDBusConnection *connection,
1526 GDBusMessage *message,
1527 GDBusSendMessageFlags flags,
1528 volatile guint32 *out_serial,
1533 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1534 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1535 g_return_val_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message), FALSE);
1536 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1538 CONNECTION_LOCK (connection);
1539 ret = g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, error);
1540 CONNECTION_UNLOCK (connection);
1544 /* ---------------------------------------------------------------------------------------------------- */
1548 volatile gint ref_count;
1549 GDBusConnection *connection;
1551 GSimpleAsyncResult *simple;
1553 GMainContext *main_context;
1555 GCancellable *cancellable;
1557 gulong cancellable_handler_id;
1559 GSource *timeout_source;
1564 static SendMessageData *
1565 send_message_data_ref (SendMessageData *data)
1567 g_atomic_int_inc (&data->ref_count);
1572 send_message_data_unref (SendMessageData *data)
1574 if (g_atomic_int_dec_and_test (&data->ref_count))
1576 g_assert (data->timeout_source == NULL);
1577 g_assert (data->simple == NULL);
1578 g_assert (data->cancellable_handler_id == 0);
1579 g_object_unref (data->connection);
1580 if (data->cancellable != NULL)
1581 g_object_unref (data->cancellable);
1582 g_main_context_unref (data->main_context);
1587 /* ---------------------------------------------------------------------------------------------------- */
1589 /* can be called from any thread with lock held - caller must have prepared GSimpleAsyncResult already */
1591 send_message_with_reply_deliver (SendMessageData *data, gboolean remove)
1593 CONNECTION_ENSURE_LOCK (data->connection);
1595 g_assert (!data->delivered);
1597 data->delivered = TRUE;
1599 g_simple_async_result_complete_in_idle (data->simple);
1600 g_object_unref (data->simple);
1601 data->simple = NULL;
1603 if (data->timeout_source != NULL)
1605 g_source_destroy (data->timeout_source);
1606 data->timeout_source = NULL;
1608 if (data->cancellable_handler_id > 0)
1610 g_cancellable_disconnect (data->cancellable, data->cancellable_handler_id);
1611 data->cancellable_handler_id = 0;
1616 g_warn_if_fail (g_hash_table_remove (data->connection->map_method_serial_to_send_message_data,
1617 GUINT_TO_POINTER (data->serial)));
1620 send_message_data_unref (data);
1623 /* ---------------------------------------------------------------------------------------------------- */
1625 /* must hold lock */
1627 send_message_data_deliver_reply_unlocked (SendMessageData *data,
1628 GDBusMessage *reply)
1630 if (data->delivered)
1633 g_simple_async_result_set_op_res_gpointer (data->simple,
1634 g_object_ref (reply),
1637 send_message_with_reply_deliver (data, TRUE);
1643 /* ---------------------------------------------------------------------------------------------------- */
1646 send_message_with_reply_cancelled_idle_cb (gpointer user_data)
1648 SendMessageData *data = user_data;
1650 CONNECTION_LOCK (data->connection);
1651 if (data->delivered)
1654 g_simple_async_result_set_error (data->simple,
1656 G_IO_ERROR_CANCELLED,
1657 _("Operation was cancelled"));
1659 send_message_with_reply_deliver (data, TRUE);
1662 CONNECTION_UNLOCK (data->connection);
1666 /* Can be called from any thread with or without lock held */
1668 send_message_with_reply_cancelled_cb (GCancellable *cancellable,
1671 SendMessageData *data = user_data;
1672 GSource *idle_source;
1674 /* postpone cancellation to idle handler since we may be called directly
1675 * via g_cancellable_connect() (e.g. holding lock)
1677 idle_source = g_idle_source_new ();
1678 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1679 g_source_set_callback (idle_source,
1680 send_message_with_reply_cancelled_idle_cb,
1681 send_message_data_ref (data),
1682 (GDestroyNotify) send_message_data_unref);
1683 g_source_attach (idle_source, data->main_context);
1684 g_source_unref (idle_source);
1687 /* ---------------------------------------------------------------------------------------------------- */
1690 send_message_with_reply_timeout_cb (gpointer user_data)
1692 SendMessageData *data = user_data;
1694 CONNECTION_LOCK (data->connection);
1695 if (data->delivered)
1698 g_simple_async_result_set_error (data->simple,
1700 G_IO_ERROR_TIMED_OUT,
1701 _("Timeout was reached"));
1703 send_message_with_reply_deliver (data, TRUE);
1706 CONNECTION_UNLOCK (data->connection);
1711 /* ---------------------------------------------------------------------------------------------------- */
1714 g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection *connection,
1715 GDBusMessage *message,
1716 GDBusSendMessageFlags flags,
1718 volatile guint32 *out_serial,
1719 GCancellable *cancellable,
1720 GAsyncReadyCallback callback,
1723 GSimpleAsyncResult *simple;
1724 SendMessageData *data;
1726 volatile guint32 serial;
1730 if (out_serial == NULL)
1731 out_serial = &serial;
1733 if (timeout_msec == -1)
1734 timeout_msec = 25 * 1000;
1736 simple = g_simple_async_result_new (G_OBJECT (connection),
1739 g_dbus_connection_send_message_with_reply);
1741 if (g_cancellable_is_cancelled (cancellable))
1743 g_simple_async_result_set_error (simple,
1745 G_IO_ERROR_CANCELLED,
1746 _("Operation was cancelled"));
1747 g_simple_async_result_complete_in_idle (simple);
1748 g_object_unref (simple);
1752 if (connection->closed)
1754 g_simple_async_result_set_error (simple,
1757 _("The connection is closed"));
1758 g_simple_async_result_complete_in_idle (simple);
1759 g_object_unref (simple);
1764 if (!g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, &error))
1766 g_simple_async_result_take_error (simple, error);
1767 g_simple_async_result_complete_in_idle (simple);
1768 g_object_unref (simple);
1772 data = g_new0 (SendMessageData, 1);
1773 data->ref_count = 1;
1774 data->connection = g_object_ref (connection);
1775 data->simple = simple;
1776 data->serial = *out_serial;
1777 data->main_context = g_main_context_ref_thread_default ();
1779 if (cancellable != NULL)
1781 data->cancellable = g_object_ref (cancellable);
1782 data->cancellable_handler_id = g_cancellable_connect (cancellable,
1783 G_CALLBACK (send_message_with_reply_cancelled_cb),
1784 send_message_data_ref (data),
1785 (GDestroyNotify) send_message_data_unref);
1786 g_object_set_data_full (G_OBJECT (simple),
1788 g_object_ref (cancellable),
1789 (GDestroyNotify) g_object_unref);
1792 if (timeout_msec != G_MAXINT)
1794 data->timeout_source = g_timeout_source_new (timeout_msec);
1795 g_source_set_priority (data->timeout_source, G_PRIORITY_DEFAULT);
1796 g_source_set_callback (data->timeout_source,
1797 send_message_with_reply_timeout_cb,
1798 send_message_data_ref (data),
1799 (GDestroyNotify) send_message_data_unref);
1800 g_source_attach (data->timeout_source, data->main_context);
1801 g_source_unref (data->timeout_source);
1804 g_hash_table_insert (connection->map_method_serial_to_send_message_data,
1805 GUINT_TO_POINTER (*out_serial),
1813 * g_dbus_connection_send_message_with_reply:
1814 * @connection: A #GDBusConnection.
1815 * @message: A #GDBusMessage.
1816 * @flags: Flags affecting how the message is sent.
1817 * @timeout_msec: The timeout in milliseconds, -1 to use the default
1818 * timeout or %G_MAXINT for no timeout.
1819 * @out_serial: (out) (allow-none): Return location for serial number assigned
1820 * to @message when sending it or %NULL.
1821 * @cancellable: A #GCancellable or %NULL.
1822 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
1823 * satisfied or %NULL if you don't care about the result.
1824 * @user_data: The data to pass to @callback.
1826 * Asynchronously sends @message to the peer represented by @connection.
1828 * Unless @flags contain the
1829 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1830 * will be assigned by @connection and set on @message via
1831 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1832 * serial number used will be written to this location prior to
1833 * submitting the message to the underlying transport.
1835 * If @connection is closed then the operation will fail with
1836 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
1837 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
1838 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1840 * This is an asynchronous method. When the operation is finished, @callback will be invoked
1841 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
1842 * of the thread you are calling this method from. You can then call
1843 * g_dbus_connection_send_message_with_reply_finish() to get the result of the operation.
1844 * See g_dbus_connection_send_message_with_reply_sync() for the synchronous version.
1846 * Note that @message must be unlocked, unless @flags contain the
1847 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1849 * See <xref linkend="gdbus-server"/> and <xref
1850 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1851 * low-level API to send and receive UNIX file descriptors.
1856 g_dbus_connection_send_message_with_reply (GDBusConnection *connection,
1857 GDBusMessage *message,
1858 GDBusSendMessageFlags flags,
1860 volatile guint32 *out_serial,
1861 GCancellable *cancellable,
1862 GAsyncReadyCallback callback,
1865 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1866 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1867 g_return_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message));
1868 g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
1870 CONNECTION_LOCK (connection);
1871 g_dbus_connection_send_message_with_reply_unlocked (connection,
1879 CONNECTION_UNLOCK (connection);
1883 * g_dbus_connection_send_message_with_reply_finish:
1884 * @connection: a #GDBusConnection
1885 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_send_message_with_reply().
1886 * @error: Return location for error or %NULL.
1888 * Finishes an operation started with g_dbus_connection_send_message_with_reply().
1890 * Note that @error is only set if a local in-process error
1891 * occurred. That is to say that the returned #GDBusMessage object may
1892 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
1893 * g_dbus_message_to_gerror() to transcode this to a #GError.
1895 * See <xref linkend="gdbus-server"/> and <xref
1896 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1897 * low-level API to send and receive UNIX file descriptors.
1899 * Returns: (transfer full): A locked #GDBusMessage or %NULL if @error is set.
1904 g_dbus_connection_send_message_with_reply_finish (GDBusConnection *connection,
1908 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1909 GDBusMessage *reply;
1910 GCancellable *cancellable;
1912 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1913 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1917 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_send_message_with_reply);
1919 if (g_simple_async_result_propagate_error (simple, error))
1922 reply = g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1923 cancellable = g_object_get_data (G_OBJECT (simple), "cancellable");
1924 if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
1926 g_object_unref (reply);
1928 g_set_error_literal (error,
1930 G_IO_ERROR_CANCELLED,
1931 _("Operation was cancelled"));
1937 /* ---------------------------------------------------------------------------------------------------- */
1942 GMainContext *context;
1944 } SendMessageSyncData;
1947 send_message_with_reply_sync_cb (GDBusConnection *connection,
1951 SendMessageSyncData *data = user_data;
1952 data->res = g_object_ref (res);
1953 g_main_loop_quit (data->loop);
1957 * g_dbus_connection_send_message_with_reply_sync:
1958 * @connection: A #GDBusConnection.
1959 * @message: A #GDBusMessage.
1960 * @flags: Flags affecting how the message is sent.
1961 * @timeout_msec: The timeout in milliseconds, -1 to use the default
1962 * timeout or %G_MAXINT for no timeout.
1963 * @out_serial: (out) (allow-none): Return location for serial number assigned
1964 * to @message when sending it or %NULL.
1965 * @cancellable: A #GCancellable or %NULL.
1966 * @error: Return location for error or %NULL.
1968 * Synchronously sends @message to the peer represented by @connection
1969 * and blocks the calling thread until a reply is received or the
1970 * timeout is reached. See g_dbus_connection_send_message_with_reply()
1971 * for the asynchronous version of this method.
1973 * Unless @flags contain the
1974 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1975 * will be assigned by @connection and set on @message via
1976 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1977 * serial number used will be written to this location prior to
1978 * submitting the message to the underlying transport.
1980 * If @connection is closed then the operation will fail with
1981 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
1982 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
1983 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1985 * Note that @error is only set if a local in-process error
1986 * occurred. That is to say that the returned #GDBusMessage object may
1987 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
1988 * g_dbus_message_to_gerror() to transcode this to a #GError.
1990 * See <xref linkend="gdbus-server"/> and <xref
1991 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1992 * low-level API to send and receive UNIX file descriptors.
1994 * Note that @message must be unlocked, unless @flags contain the
1995 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1997 * Returns: (transfer full): A locked #GDBusMessage that is the reply to @message or %NULL if @error is set.
2002 g_dbus_connection_send_message_with_reply_sync (GDBusConnection *connection,
2003 GDBusMessage *message,
2004 GDBusSendMessageFlags flags,
2006 volatile guint32 *out_serial,
2007 GCancellable *cancellable,
2010 SendMessageSyncData *data;
2011 GDBusMessage *reply;
2013 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2014 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2015 g_return_val_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message), NULL);
2016 g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
2017 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2019 data = g_new0 (SendMessageSyncData, 1);
2020 data->context = g_main_context_new ();
2021 data->loop = g_main_loop_new (data->context, FALSE);
2023 g_main_context_push_thread_default (data->context);
2025 g_dbus_connection_send_message_with_reply (connection,
2031 (GAsyncReadyCallback) send_message_with_reply_sync_cb,
2033 g_main_loop_run (data->loop);
2034 reply = g_dbus_connection_send_message_with_reply_finish (connection,
2038 g_main_context_pop_thread_default (data->context);
2040 g_main_context_unref (data->context);
2041 g_main_loop_unref (data->loop);
2042 g_object_unref (data->res);
2048 /* ---------------------------------------------------------------------------------------------------- */
2052 GDBusMessageFilterFunction func;
2059 GDBusMessageFilterFunction filter_function;
2061 GDestroyNotify user_data_free_func;
2064 /* Called in worker's thread - we must not block */
2066 on_worker_message_received (GDBusWorker *worker,
2067 GDBusMessage *message,
2070 GDBusConnection *connection;
2071 FilterCallback *filters;
2076 G_LOCK (message_bus_lock);
2077 alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
2080 G_UNLOCK (message_bus_lock);
2083 connection = G_DBUS_CONNECTION (user_data);
2084 g_object_ref (connection);
2085 G_UNLOCK (message_bus_lock);
2087 //g_debug ("in on_worker_message_received");
2089 g_object_ref (message);
2090 g_dbus_message_lock (message);
2092 //g_debug ("boo ref_count = %d %p %p", G_OBJECT (connection)->ref_count, connection, connection->worker);
2094 /* First collect the set of callback functions */
2095 CONNECTION_LOCK (connection);
2096 num_filters = connection->filters->len;
2097 filters = g_new0 (FilterCallback, num_filters);
2098 for (n = 0; n < num_filters; n++)
2100 FilterData *data = connection->filters->pdata[n];
2101 filters[n].func = data->filter_function;
2102 filters[n].user_data = data->user_data;
2104 CONNECTION_UNLOCK (connection);
2106 /* then call the filters in order (without holding the lock) */
2107 for (n = 0; n < num_filters; n++)
2109 message = filters[n].func (connection,
2112 filters[n].user_data);
2113 if (message == NULL)
2115 g_dbus_message_lock (message);
2118 /* Standard dispatch unless the filter ate the message - no need to
2119 * do anything if the message was altered
2121 if (message != NULL)
2123 GDBusMessageType message_type;
2125 message_type = g_dbus_message_get_message_type (message);
2126 if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_RETURN || message_type == G_DBUS_MESSAGE_TYPE_ERROR)
2128 guint32 reply_serial;
2129 SendMessageData *send_message_data;
2131 reply_serial = g_dbus_message_get_reply_serial (message);
2132 CONNECTION_LOCK (connection);
2133 send_message_data = g_hash_table_lookup (connection->map_method_serial_to_send_message_data,
2134 GUINT_TO_POINTER (reply_serial));
2135 if (send_message_data != NULL)
2137 //g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection);
2138 send_message_data_deliver_reply_unlocked (send_message_data, message);
2142 //g_debug ("message reply/error for serial %d but no SendMessageData found for %p", reply_serial, connection);
2144 CONNECTION_UNLOCK (connection);
2146 else if (message_type == G_DBUS_MESSAGE_TYPE_SIGNAL)
2148 CONNECTION_LOCK (connection);
2149 distribute_signals (connection, message);
2150 CONNECTION_UNLOCK (connection);
2152 else if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_CALL)
2154 CONNECTION_LOCK (connection);
2155 distribute_method_call (connection, message);
2156 CONNECTION_UNLOCK (connection);
2160 if (message != NULL)
2161 g_object_unref (message);
2162 g_object_unref (connection);
2166 /* Called in worker's thread */
2167 static GDBusMessage *
2168 on_worker_message_about_to_be_sent (GDBusWorker *worker,
2169 GDBusMessage *message,
2172 GDBusConnection *connection;
2173 FilterCallback *filters;
2178 G_LOCK (message_bus_lock);
2179 alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
2182 G_UNLOCK (message_bus_lock);
2185 connection = G_DBUS_CONNECTION (user_data);
2186 g_object_ref (connection);
2187 G_UNLOCK (message_bus_lock);
2189 //g_debug ("in on_worker_message_about_to_be_sent");
2191 /* First collect the set of callback functions */
2192 CONNECTION_LOCK (connection);
2193 num_filters = connection->filters->len;
2194 filters = g_new0 (FilterCallback, num_filters);
2195 for (n = 0; n < num_filters; n++)
2197 FilterData *data = connection->filters->pdata[n];
2198 filters[n].func = data->filter_function;
2199 filters[n].user_data = data->user_data;
2201 CONNECTION_UNLOCK (connection);
2203 /* then call the filters in order (without holding the lock) */
2204 for (n = 0; n < num_filters; n++)
2206 g_dbus_message_lock (message);
2207 message = filters[n].func (connection,
2210 filters[n].user_data);
2211 if (message == NULL)
2215 g_object_unref (connection);
2221 /* called with connection lock held */
2223 cancel_method_on_close (gpointer key, gpointer value, gpointer user_data)
2225 SendMessageData *data = value;
2227 if (data->delivered)
2230 g_simple_async_result_set_error (data->simple,
2233 _("The connection is closed"));
2235 /* Ask send_message_with_reply_deliver not to remove the element from the
2236 * hash table - we're in the middle of a foreach; that would be unsafe.
2237 * Instead, return TRUE from this function so that it gets removed safely.
2239 send_message_with_reply_deliver (data, FALSE);
2243 /* Called in worker's thread - we must not block */
2245 on_worker_closed (GDBusWorker *worker,
2246 gboolean remote_peer_vanished,
2250 GDBusConnection *connection;
2253 G_LOCK (message_bus_lock);
2254 alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
2257 G_UNLOCK (message_bus_lock);
2260 connection = G_DBUS_CONNECTION (user_data);
2261 g_object_ref (connection);
2262 G_UNLOCK (message_bus_lock);
2264 //g_debug ("in on_worker_closed: %s", error->message);
2266 CONNECTION_LOCK (connection);
2267 if (!connection->closed)
2269 g_hash_table_foreach_remove (connection->map_method_serial_to_send_message_data, cancel_method_on_close, NULL);
2270 set_closed_unlocked (connection, remote_peer_vanished, error);
2272 CONNECTION_UNLOCK (connection);
2274 g_object_unref (connection);
2277 /* ---------------------------------------------------------------------------------------------------- */
2279 /* Determines the biggest set of capabilities we can support on this connection */
2280 static GDBusCapabilityFlags
2281 get_offered_capabilities_max (GDBusConnection *connection)
2283 GDBusCapabilityFlags ret;
2284 ret = G_DBUS_CAPABILITY_FLAGS_NONE;
2286 if (G_IS_UNIX_CONNECTION (connection->stream))
2287 ret |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
2293 initable_init (GInitable *initable,
2294 GCancellable *cancellable,
2297 GDBusConnection *connection = G_DBUS_CONNECTION (initable);
2300 /* This method needs to be idempotent to work with the singleton
2301 * pattern. See the docs for g_initable_init(). We implement this by
2304 * Unfortunately we can't use the main lock since the on_worker_*()
2305 * callbacks above needs the lock during initialization (for message
2306 * bus connections we do a synchronous Hello() call on the bus).
2308 g_mutex_lock (&connection->init_lock);
2312 /* First, handle the case where the connection already has an
2313 * initialization error set.
2315 if (connection->initialization_error != NULL)
2318 /* Also make this a no-op if we're already initialized fine */
2319 if (connection->is_initialized)
2325 g_assert (connection->initialization_error == NULL && !connection->is_initialized);
2327 /* The user can pass multiple (but mutally exclusive) construct
2330 * - stream (of type GIOStream)
2331 * - address (of type gchar*)
2333 * At the end of the day we end up with a non-NULL GIOStream
2334 * object in connection->stream.
2336 if (connection->address != NULL)
2338 g_assert (connection->stream == NULL);
2340 if ((connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER) ||
2341 (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS))
2343 g_set_error_literal (error,
2345 G_IO_ERROR_INVALID_ARGUMENT,
2346 _("Unsupported flags encountered when constructing a client-side connection"));
2350 connection->stream = g_dbus_address_get_stream_sync (connection->address,
2351 NULL, /* TODO: out_guid */
2353 &connection->initialization_error);
2354 if (connection->stream == NULL)
2357 else if (connection->stream != NULL)
2363 g_assert_not_reached ();
2366 /* Authenticate the connection */
2367 if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER)
2369 g_assert (!(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT));
2370 g_assert (connection->guid != NULL);
2371 connection->auth = _g_dbus_auth_new (connection->stream);
2372 if (!_g_dbus_auth_run_server (connection->auth,
2373 connection->authentication_observer,
2375 (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS),
2376 get_offered_capabilities_max (connection),
2377 &connection->capabilities,
2378 &connection->credentials,
2380 &connection->initialization_error))
2383 else if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT)
2385 g_assert (!(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER));
2386 g_assert (connection->guid == NULL);
2387 connection->auth = _g_dbus_auth_new (connection->stream);
2388 connection->guid = _g_dbus_auth_run_client (connection->auth,
2389 get_offered_capabilities_max (connection),
2390 &connection->capabilities,
2392 &connection->initialization_error);
2393 if (connection->guid == NULL)
2397 if (connection->authentication_observer != NULL)
2399 g_object_unref (connection->authentication_observer);
2400 connection->authentication_observer = NULL;
2403 //g_output_stream_flush (G_SOCKET_CONNECTION (connection->stream)
2405 //g_debug ("haz unix fd passing powers: %d", connection->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
2408 /* We want all IO operations to be non-blocking since they happen in
2409 * the worker thread which is shared by _all_ connections.
2411 if (G_IS_SOCKET_CONNECTION (connection->stream))
2413 g_socket_set_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection->stream)), FALSE);
2417 G_LOCK (message_bus_lock);
2418 if (alive_connections == NULL)
2419 alive_connections = g_hash_table_new (g_direct_hash, g_direct_equal);
2420 g_hash_table_insert (alive_connections, connection, connection);
2421 G_UNLOCK (message_bus_lock);
2423 connection->worker = _g_dbus_worker_new (connection->stream,
2424 connection->capabilities,
2425 (connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING),
2426 on_worker_message_received,
2427 on_worker_message_about_to_be_sent,
2431 /* if a bus connection, call org.freedesktop.DBus.Hello - this is how we're getting a name */
2432 if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
2434 GVariant *hello_result;
2436 /* we could lift this restriction by adding code in gdbusprivate.c */
2437 if (connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING)
2439 g_set_error_literal (&connection->initialization_error,
2442 "Cannot use DELAY_MESSAGE_PROCESSING with MESSAGE_BUS_CONNECTION");
2446 hello_result = g_dbus_connection_call_sync (connection,
2447 "org.freedesktop.DBus", /* name */
2448 "/org/freedesktop/DBus", /* path */
2449 "org.freedesktop.DBus", /* interface */
2451 NULL, /* parameters */
2452 G_VARIANT_TYPE ("(s)"),
2453 G_DBUS_CALL_FLAGS_NONE,
2455 NULL, /* TODO: cancellable */
2456 &connection->initialization_error);
2457 if (hello_result == NULL)
2460 g_variant_get (hello_result, "(s)", &connection->bus_unique_name);
2461 g_variant_unref (hello_result);
2462 //g_debug ("unique name is `%s'", connection->bus_unique_name);
2465 connection->is_initialized = TRUE;
2471 g_assert (connection->initialization_error != NULL);
2472 g_propagate_error (error, g_error_copy (connection->initialization_error));
2475 g_mutex_unlock (&connection->init_lock);
2481 initable_iface_init (GInitableIface *initable_iface)
2483 initable_iface->init = initable_init;
2486 /* ---------------------------------------------------------------------------------------------------- */
2489 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
2494 /* ---------------------------------------------------------------------------------------------------- */
2497 * g_dbus_connection_new:
2498 * @stream: A #GIOStream.
2499 * @guid: (allow-none): The GUID to use if a authenticating as a server or %NULL.
2500 * @flags: Flags describing how to make the connection.
2501 * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
2502 * @cancellable: A #GCancellable or %NULL.
2503 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
2504 * @user_data: The data to pass to @callback.
2506 * Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
2507 * with the end represented by @stream.
2509 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2510 * will be put into non-blocking mode.
2512 * The D-Bus connection will interact with @stream from a worker thread.
2513 * As a result, the caller should not interact with @stream after this
2514 * method has been called, except by calling g_object_unref() on it.
2516 * If @observer is not %NULL it may be used to control the
2517 * authentication process.
2519 * When the operation is finished, @callback will be invoked. You can
2520 * then call g_dbus_connection_new_finish() to get the result of the
2523 * This is a asynchronous failable constructor. See
2524 * g_dbus_connection_new_sync() for the synchronous
2530 g_dbus_connection_new (GIOStream *stream,
2532 GDBusConnectionFlags flags,
2533 GDBusAuthObserver *observer,
2534 GCancellable *cancellable,
2535 GAsyncReadyCallback callback,
2538 g_return_if_fail (G_IS_IO_STREAM (stream));
2539 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
2547 "authentication-observer", observer,
2552 * g_dbus_connection_new_finish:
2553 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_new().
2554 * @error: Return location for error or %NULL.
2556 * Finishes an operation started with g_dbus_connection_new().
2558 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2563 g_dbus_connection_new_finish (GAsyncResult *res,
2567 GObject *source_object;
2569 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2570 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2572 source_object = g_async_result_get_source_object (res);
2573 g_assert (source_object != NULL);
2574 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2577 g_object_unref (source_object);
2579 return G_DBUS_CONNECTION (object);
2585 * g_dbus_connection_new_sync:
2586 * @stream: A #GIOStream.
2587 * @guid: (allow-none): The GUID to use if a authenticating as a server or %NULL.
2588 * @flags: Flags describing how to make the connection.
2589 * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
2590 * @cancellable: A #GCancellable or %NULL.
2591 * @error: Return location for error or %NULL.
2593 * Synchronously sets up a D-Bus connection for exchanging D-Bus messages
2594 * with the end represented by @stream.
2596 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2597 * will be put into non-blocking mode.
2599 * The D-Bus connection will interact with @stream from a worker thread.
2600 * As a result, the caller should not interact with @stream after this
2601 * method has been called, except by calling g_object_unref() on it.
2603 * If @observer is not %NULL it may be used to control the
2604 * authentication process.
2606 * This is a synchronous failable constructor. See
2607 * g_dbus_connection_new() for the asynchronous version.
2609 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2614 g_dbus_connection_new_sync (GIOStream *stream,
2616 GDBusConnectionFlags flags,
2617 GDBusAuthObserver *observer,
2618 GCancellable *cancellable,
2621 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
2622 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2623 return g_initable_new (G_TYPE_DBUS_CONNECTION,
2629 "authentication-observer", observer,
2633 /* ---------------------------------------------------------------------------------------------------- */
2636 * g_dbus_connection_new_for_address:
2637 * @address: A D-Bus address.
2638 * @flags: Flags describing how to make the connection.
2639 * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
2640 * @cancellable: A #GCancellable or %NULL.
2641 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
2642 * @user_data: The data to pass to @callback.
2644 * Asynchronously connects and sets up a D-Bus client connection for
2645 * exchanging D-Bus messages with an endpoint specified by @address
2646 * which must be in the D-Bus address format.
2648 * This constructor can only be used to initiate client-side
2649 * connections - use g_dbus_connection_new() if you need to act as the
2650 * server. In particular, @flags cannot contain the
2651 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2652 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2654 * When the operation is finished, @callback will be invoked. You can
2655 * then call g_dbus_connection_new_finish() to get the result of the
2658 * If @observer is not %NULL it may be used to control the
2659 * authentication process.
2661 * This is a asynchronous failable constructor. See
2662 * g_dbus_connection_new_for_address_sync() for the synchronous
2668 g_dbus_connection_new_for_address (const gchar *address,
2669 GDBusConnectionFlags flags,
2670 GDBusAuthObserver *observer,
2671 GCancellable *cancellable,
2672 GAsyncReadyCallback callback,
2675 g_return_if_fail (address != NULL);
2676 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
2683 "authentication-observer", observer,
2688 * g_dbus_connection_new_for_address_finish:
2689 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_new().
2690 * @error: Return location for error or %NULL.
2692 * Finishes an operation started with g_dbus_connection_new_for_address().
2694 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2699 g_dbus_connection_new_for_address_finish (GAsyncResult *res,
2703 GObject *source_object;
2705 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2706 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2708 source_object = g_async_result_get_source_object (res);
2709 g_assert (source_object != NULL);
2710 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2713 g_object_unref (source_object);
2715 return G_DBUS_CONNECTION (object);
2721 * g_dbus_connection_new_for_address_sync:
2722 * @address: A D-Bus address.
2723 * @flags: Flags describing how to make the connection.
2724 * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
2725 * @cancellable: A #GCancellable or %NULL.
2726 * @error: Return location for error or %NULL.
2728 * Synchronously connects and sets up a D-Bus client connection for
2729 * exchanging D-Bus messages with an endpoint specified by @address
2730 * which must be in the D-Bus address format.
2732 * This constructor can only be used to initiate client-side
2733 * connections - use g_dbus_connection_new_sync() if you need to act
2734 * as the server. In particular, @flags cannot contain the
2735 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2736 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2738 * This is a synchronous failable constructor. See
2739 * g_dbus_connection_new_for_address() for the asynchronous version.
2741 * If @observer is not %NULL it may be used to control the
2742 * authentication process.
2744 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2749 g_dbus_connection_new_for_address_sync (const gchar *address,
2750 GDBusConnectionFlags flags,
2751 GDBusAuthObserver *observer,
2752 GCancellable *cancellable,
2755 g_return_val_if_fail (address != NULL, NULL);
2756 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2757 return g_initable_new (G_TYPE_DBUS_CONNECTION,
2762 "authentication-observer", observer,
2766 /* ---------------------------------------------------------------------------------------------------- */
2769 * g_dbus_connection_set_exit_on_close:
2770 * @connection: A #GDBusConnection.
2771 * @exit_on_close: Whether the process should be terminated
2772 * when @connection is closed by the remote peer.
2774 * Sets whether the process should be terminated when @connection is
2775 * closed by the remote peer. See #GDBusConnection:exit-on-close for
2778 * Note that this function should be used with care. Most modern UNIX
2779 * desktops tie the notion of a user session the session bus, and expect
2780 * all of a users applications to quit when their bus connection goes away.
2781 * If you are setting @exit_on_close to %FALSE for the shared session
2782 * bus connection, you should make sure that your application exits
2783 * when the user session ends.
2788 g_dbus_connection_set_exit_on_close (GDBusConnection *connection,
2789 gboolean exit_on_close)
2791 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2792 connection->exit_on_close = exit_on_close;
2796 * g_dbus_connection_get_exit_on_close:
2797 * @connection: A #GDBusConnection.
2799 * Gets whether the process is terminated when @connection is
2800 * closed by the remote peer. See
2801 * #GDBusConnection:exit-on-close for more details.
2803 * Returns: Whether the process is terminated when @connection is
2804 * closed by the remote peer.
2809 g_dbus_connection_get_exit_on_close (GDBusConnection *connection)
2811 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
2812 return connection->exit_on_close;
2816 * g_dbus_connection_get_guid:
2817 * @connection: A #GDBusConnection.
2819 * The GUID of the peer performing the role of server when
2820 * authenticating. See #GDBusConnection:guid for more details.
2822 * Returns: The GUID. Do not free this string, it is owned by
2828 g_dbus_connection_get_guid (GDBusConnection *connection)
2830 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2831 return connection->guid;
2835 * g_dbus_connection_get_unique_name:
2836 * @connection: A #GDBusConnection.
2838 * Gets the unique name of @connection as assigned by the message
2839 * bus. This can also be used to figure out if @connection is a
2840 * message bus connection.
2842 * Returns: The unique name or %NULL if @connection is not a message
2843 * bus connection. Do not free this string, it is owned by
2849 g_dbus_connection_get_unique_name (GDBusConnection *connection)
2851 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2852 return connection->bus_unique_name;
2856 * g_dbus_connection_get_peer_credentials:
2857 * @connection: A #GDBusConnection.
2859 * Gets the credentials of the authenticated peer. This will always
2860 * return %NULL unless @connection acted as a server
2861 * (e.g. %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER was passed)
2862 * when set up and the client passed credentials as part of the
2863 * authentication process.
2865 * In a message bus setup, the message bus is always the server and
2866 * each application is a client. So this method will always return
2867 * %NULL for message bus clients.
2869 * Returns: (transfer none): A #GCredentials or %NULL if not available. Do not free
2870 * this object, it is owned by @connection.
2875 g_dbus_connection_get_peer_credentials (GDBusConnection *connection)
2877 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2878 return connection->credentials;
2881 /* ---------------------------------------------------------------------------------------------------- */
2883 static guint _global_filter_id = 1;
2886 * g_dbus_connection_add_filter:
2887 * @connection: A #GDBusConnection.
2888 * @filter_function: A filter function.
2889 * @user_data: User data to pass to @filter_function.
2890 * @user_data_free_func: Function to free @user_data with when filter
2891 * is removed or %NULL.
2893 * Adds a message filter. Filters are handlers that are run on all
2894 * incoming and outgoing messages, prior to standard dispatch. Filters
2895 * are run in the order that they were added. The same handler can be
2896 * added as a filter more than once, in which case it will be run more
2897 * than once. Filters added during a filter callback won't be run on
2898 * the message being processed. Filter functions are allowed to modify
2899 * and even drop messages.
2901 * Note that filters are run in a dedicated message handling thread so
2902 * they can't block and, generally, can't do anything but signal a
2903 * worker thread. Also note that filters are rarely needed - use API
2904 * such as g_dbus_connection_send_message_with_reply(),
2905 * g_dbus_connection_signal_subscribe() or g_dbus_connection_call() instead.
2907 * If a filter consumes an incoming message the message is not
2908 * dispatched anywhere else - not even the standard dispatch machinery
2909 * (that API such as g_dbus_connection_signal_subscribe() and
2910 * g_dbus_connection_send_message_with_reply() relies on) will see the
2911 * message. Similary, if a filter consumes an outgoing message, the
2912 * message will not be sent to the other peer.
2914 * Returns: A filter identifier that can be used with
2915 * g_dbus_connection_remove_filter().
2920 g_dbus_connection_add_filter (GDBusConnection *connection,
2921 GDBusMessageFilterFunction filter_function,
2923 GDestroyNotify user_data_free_func)
2927 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
2928 g_return_val_if_fail (filter_function != NULL, 0);
2930 CONNECTION_LOCK (connection);
2931 data = g_new0 (FilterData, 1);
2932 data->id = _global_filter_id++; /* TODO: overflow etc. */
2933 data->filter_function = filter_function;
2934 data->user_data = user_data;
2935 data->user_data_free_func = user_data_free_func;
2936 g_ptr_array_add (connection->filters, data);
2937 CONNECTION_UNLOCK (connection);
2942 /* only called from finalize(), removes all filters */
2944 purge_all_filters (GDBusConnection *connection)
2947 for (n = 0; n < connection->filters->len; n++)
2949 FilterData *data = connection->filters->pdata[n];
2950 if (data->user_data_free_func != NULL)
2951 data->user_data_free_func (data->user_data);
2957 * g_dbus_connection_remove_filter:
2958 * @connection: a #GDBusConnection
2959 * @filter_id: an identifier obtained from g_dbus_connection_add_filter()
2966 g_dbus_connection_remove_filter (GDBusConnection *connection,
2970 FilterData *to_destroy;
2972 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2974 CONNECTION_LOCK (connection);
2976 for (n = 0; n < connection->filters->len; n++)
2978 FilterData *data = connection->filters->pdata[n];
2979 if (data->id == filter_id)
2981 g_ptr_array_remove_index (connection->filters, n);
2986 CONNECTION_UNLOCK (connection);
2988 /* do free without holding lock */
2989 if (to_destroy != NULL)
2991 if (to_destroy->user_data_free_func != NULL)
2992 to_destroy->user_data_free_func (to_destroy->user_data);
2993 g_free (to_destroy);
2997 g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id);
3001 /* ---------------------------------------------------------------------------------------------------- */
3007 gchar *sender_unique_name; /* if sender is unique or org.freedesktop.DBus, then that name... otherwise blank */
3008 gchar *interface_name;
3012 GArray *subscribers;
3017 GDBusSignalCallback callback;
3019 GDestroyNotify user_data_free_func;
3021 GMainContext *context;
3025 signal_data_free (SignalData *signal_data)
3027 g_free (signal_data->rule);
3028 g_free (signal_data->sender);
3029 g_free (signal_data->sender_unique_name);
3030 g_free (signal_data->interface_name);
3031 g_free (signal_data->member);
3032 g_free (signal_data->object_path);
3033 g_free (signal_data->arg0);
3034 g_array_free (signal_data->subscribers, TRUE);
3035 g_free (signal_data);
3039 args_to_rule (const gchar *sender,
3040 const gchar *interface_name,
3041 const gchar *member,
3042 const gchar *object_path,
3048 rule = g_string_new ("type='signal'");
3050 g_string_prepend_c (rule, '-');
3052 g_string_append_printf (rule, ",sender='%s'", sender);
3053 if (interface_name != NULL)
3054 g_string_append_printf (rule, ",interface='%s'", interface_name);
3056 g_string_append_printf (rule, ",member='%s'", member);
3057 if (object_path != NULL)
3058 g_string_append_printf (rule, ",path='%s'", object_path);
3060 g_string_append_printf (rule, ",arg0='%s'", arg0);
3062 return g_string_free (rule, FALSE);
3065 static guint _global_subscriber_id = 1;
3066 static guint _global_registration_id = 1;
3067 static guint _global_subtree_registration_id = 1;
3069 /* ---------------------------------------------------------------------------------------------------- */
3071 /* must hold lock when calling */
3073 add_match_rule (GDBusConnection *connection,
3074 const gchar *match_rule)
3077 GDBusMessage *message;
3079 if (match_rule[0] == '-')
3082 message = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3083 "/org/freedesktop/DBus", /* path */
3084 "org.freedesktop.DBus", /* interface */
3086 g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule));
3088 if (!g_dbus_connection_send_message_unlocked (connection,
3090 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
3094 g_critical ("Error while sending AddMatch() message: %s", error->message);
3095 g_error_free (error);
3097 g_object_unref (message);
3100 /* ---------------------------------------------------------------------------------------------------- */
3102 /* must hold lock when calling */
3104 remove_match_rule (GDBusConnection *connection,
3105 const gchar *match_rule)
3108 GDBusMessage *message;
3110 if (match_rule[0] == '-')
3113 message = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3114 "/org/freedesktop/DBus", /* path */
3115 "org.freedesktop.DBus", /* interface */
3117 g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule));
3120 if (!g_dbus_connection_send_message_unlocked (connection,
3122 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
3126 g_critical ("Error while sending RemoveMatch() message: %s", error->message);
3127 g_error_free (error);
3129 g_object_unref (message);
3132 /* ---------------------------------------------------------------------------------------------------- */
3135 is_signal_data_for_name_lost_or_acquired (SignalData *signal_data)
3137 return g_strcmp0 (signal_data->sender_unique_name, "org.freedesktop.DBus") == 0 &&
3138 g_strcmp0 (signal_data->interface_name, "org.freedesktop.DBus") == 0 &&
3139 g_strcmp0 (signal_data->object_path, "/org/freedesktop/DBus") == 0 &&
3140 (g_strcmp0 (signal_data->member, "NameLost") == 0 ||
3141 g_strcmp0 (signal_data->member, "NameAcquired") == 0);
3144 /* ---------------------------------------------------------------------------------------------------- */
3147 * g_dbus_connection_signal_subscribe:
3148 * @connection: A #GDBusConnection.
3149 * @sender: (allow-none): Sender name to match on (unique or well-known name)
3150 * or %NULL to listen from all senders.
3151 * @interface_name: (allow-none): D-Bus interface name to match on or %NULL to
3152 * match on all interfaces.
3153 * @member: (allow-none): D-Bus signal name to match on or %NULL to match on all signals.
3154 * @object_path: (allow-none): Object path to match on or %NULL to match on all object paths.
3155 * @arg0: (allow-none): Contents of first string argument to match on or %NULL
3156 * to match on all kinds of arguments.
3157 * @flags: Flags describing how to subscribe to the signal (currently unused).
3158 * @callback: Callback to invoke when there is a signal matching the requested data.
3159 * @user_data: User data to pass to @callback.
3160 * @user_data_free_func: (allow-none): Function to free @user_data with when
3161 * subscription is removed or %NULL.
3163 * Subscribes to signals on @connection and invokes @callback with a
3164 * whenever the signal is received. Note that @callback
3165 * will be invoked in the <link
3166 * linkend="g-main-context-push-thread-default">thread-default main
3167 * loop</link> of the thread you are calling this method from.
3169 * If @connection is not a message bus connection, @sender must be
3172 * If @sender is a well-known name note that @callback is invoked with
3173 * the unique name for the owner of @sender, not the well-known name
3174 * as one would expect. This is because the message bus rewrites the
3175 * name. As such, to avoid certain race conditions, users should be
3176 * tracking the name owner of the well-known name and use that when
3177 * processing the received signal.
3179 * Returns: A subscription identifier that can be used with g_dbus_connection_signal_unsubscribe().
3184 g_dbus_connection_signal_subscribe (GDBusConnection *connection,
3185 const gchar *sender,
3186 const gchar *interface_name,
3187 const gchar *member,
3188 const gchar *object_path,
3190 GDBusSignalFlags flags,
3191 GDBusSignalCallback callback,
3193 GDestroyNotify user_data_free_func)
3196 SignalData *signal_data;
3197 SignalSubscriber subscriber;
3198 GPtrArray *signal_data_array;
3199 const gchar *sender_unique_name;
3201 /* Right now we abort if AddMatch() fails since it can only fail with the bus being in
3202 * an OOM condition. We might want to change that but that would involve making
3203 * g_dbus_connection_signal_subscribe() asynchronous and having the call sites
3204 * handle that. And there's really no sensible way of handling this short of retrying
3205 * to add the match rule... and then there's the little thing that, hey, maybe there's
3206 * a reason the bus in an OOM condition.
3208 * Doable, but not really sure it's worth it...
3211 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
3212 g_return_val_if_fail (sender == NULL || (g_dbus_is_name (sender) && (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)), 0);
3213 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), 0);
3214 g_return_val_if_fail (member == NULL || g_dbus_is_member_name (member), 0);
3215 g_return_val_if_fail (object_path == NULL || g_variant_is_object_path (object_path), 0);
3216 g_return_val_if_fail (callback != NULL, 0);
3218 CONNECTION_LOCK (connection);
3220 /* If G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE was specified, we will end up
3221 * with a '-' character to prefix the rule (which will otherwise be
3224 * This allows us to hash the rule and do our lifecycle tracking in
3225 * the usual way, but the '-' prevents the match rule from ever
3226 * actually being send to the bus (either for add or remove).
3228 rule = args_to_rule (sender, interface_name, member, object_path, arg0,
3229 (flags & G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE) != 0);
3231 if (sender != NULL && (g_dbus_is_unique_name (sender) || g_strcmp0 (sender, "org.freedesktop.DBus") == 0))
3232 sender_unique_name = sender;
3234 sender_unique_name = "";
3236 subscriber.callback = callback;
3237 subscriber.user_data = user_data;
3238 subscriber.user_data_free_func = user_data_free_func;
3239 subscriber.id = _global_subscriber_id++; /* TODO: overflow etc. */
3240 subscriber.context = g_main_context_ref_thread_default ();
3242 /* see if we've already have this rule */
3243 signal_data = g_hash_table_lookup (connection->map_rule_to_signal_data, rule);
3244 if (signal_data != NULL)
3246 g_array_append_val (signal_data->subscribers, subscriber);
3251 signal_data = g_new0 (SignalData, 1);
3252 signal_data->rule = rule;
3253 signal_data->sender = g_strdup (sender);
3254 signal_data->sender_unique_name = g_strdup (sender_unique_name);
3255 signal_data->interface_name = g_strdup (interface_name);
3256 signal_data->member = g_strdup (member);
3257 signal_data->object_path = g_strdup (object_path);
3258 signal_data->arg0 = g_strdup (arg0);
3259 signal_data->subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber));
3260 g_array_append_val (signal_data->subscribers, subscriber);
3262 g_hash_table_insert (connection->map_rule_to_signal_data,
3266 /* Add the match rule to the bus...
3268 * Avoid adding match rules for NameLost and NameAcquired messages - the bus will
3269 * always send such messages to us.
3271 if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
3273 if (!is_signal_data_for_name_lost_or_acquired (signal_data))
3274 add_match_rule (connection, signal_data->rule);
3277 signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
3278 signal_data->sender_unique_name);
3279 if (signal_data_array == NULL)
3281 signal_data_array = g_ptr_array_new ();
3282 g_hash_table_insert (connection->map_sender_unique_name_to_signal_data_array,
3283 g_strdup (signal_data->sender_unique_name),
3286 g_ptr_array_add (signal_data_array, signal_data);
3289 g_hash_table_insert (connection->map_id_to_signal_data,
3290 GUINT_TO_POINTER (subscriber.id),
3293 CONNECTION_UNLOCK (connection);
3295 return subscriber.id;
3298 /* ---------------------------------------------------------------------------------------------------- */
3300 /* must hold lock when calling this (except if connection->finalizing is TRUE) */
3302 unsubscribe_id_internal (GDBusConnection *connection,
3303 guint subscription_id,
3304 GArray *out_removed_subscribers)
3306 SignalData *signal_data;
3307 GPtrArray *signal_data_array;
3310 signal_data = g_hash_table_lookup (connection->map_id_to_signal_data,
3311 GUINT_TO_POINTER (subscription_id));
3312 if (signal_data == NULL)
3314 /* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
3318 for (n = 0; n < signal_data->subscribers->len; n++)
3320 SignalSubscriber *subscriber;
3322 subscriber = &(g_array_index (signal_data->subscribers, SignalSubscriber, n));
3323 if (subscriber->id != subscription_id)
3326 g_warn_if_fail (g_hash_table_remove (connection->map_id_to_signal_data,
3327 GUINT_TO_POINTER (subscription_id)));
3328 g_array_append_val (out_removed_subscribers, *subscriber);
3329 g_array_remove_index (signal_data->subscribers, n);
3331 if (signal_data->subscribers->len == 0)
3333 g_warn_if_fail (g_hash_table_remove (connection->map_rule_to_signal_data, signal_data->rule));
3335 signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
3336 signal_data->sender_unique_name);
3337 g_warn_if_fail (signal_data_array != NULL);
3338 g_warn_if_fail (g_ptr_array_remove (signal_data_array, signal_data));
3340 if (signal_data_array->len == 0)
3342 g_warn_if_fail (g_hash_table_remove (connection->map_sender_unique_name_to_signal_data_array,
3343 signal_data->sender_unique_name));
3346 /* remove the match rule from the bus unless NameLost or NameAcquired (see subscribe()) */
3347 if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
3349 if (!is_signal_data_for_name_lost_or_acquired (signal_data))
3350 if (!connection->closed && !connection->finalizing)
3351 remove_match_rule (connection, signal_data->rule);
3353 signal_data_free (signal_data);
3359 g_assert_not_reached ();
3366 * g_dbus_connection_signal_unsubscribe:
3367 * @connection: A #GDBusConnection.
3368 * @subscription_id: A subscription id obtained from g_dbus_connection_signal_subscribe().
3370 * Unsubscribes from signals.
3375 g_dbus_connection_signal_unsubscribe (GDBusConnection *connection,
3376 guint subscription_id)
3378 GArray *subscribers;
3381 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
3383 subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber));
3385 CONNECTION_LOCK (connection);
3386 unsubscribe_id_internal (connection,
3389 CONNECTION_UNLOCK (connection);
3392 g_assert (subscribers->len == 0 || subscribers->len == 1);
3394 /* call GDestroyNotify without lock held */
3395 for (n = 0; n < subscribers->len; n++)
3397 SignalSubscriber *subscriber;
3398 subscriber = &(g_array_index (subscribers, SignalSubscriber, n));
3399 call_destroy_notify (subscriber->context,
3400 subscriber->user_data_free_func,
3401 subscriber->user_data);
3402 g_main_context_unref (subscriber->context);
3405 g_array_free (subscribers, TRUE);
3408 /* ---------------------------------------------------------------------------------------------------- */
3412 guint subscription_id;
3413 GDBusSignalCallback callback;
3415 GDBusMessage *message;
3416 GDBusConnection *connection;
3417 const gchar *sender;
3419 const gchar *interface;
3420 const gchar *member;
3423 /* called on delivery thread (e.g. where g_dbus_connection_signal_subscribe() was called) with
3427 emit_signal_instance_in_idle_cb (gpointer data)
3429 SignalInstance *signal_instance = data;
3430 GVariant *parameters;
3431 gboolean has_subscription;
3433 parameters = g_dbus_message_get_body (signal_instance->message);
3434 if (parameters == NULL)
3436 parameters = g_variant_new ("()");
3437 g_variant_ref_sink (parameters);
3441 g_variant_ref_sink (parameters);
3445 g_print ("in emit_signal_instance_in_idle_cb (id=%d sender=%s path=%s interface=%s member=%s params=%s)\n",
3446 signal_instance->subscription_id,
3447 signal_instance->sender,
3448 signal_instance->path,
3449 signal_instance->interface,
3450 signal_instance->member,
3451 g_variant_print (parameters, TRUE));
3454 /* Careful here, don't do the callback if we no longer has the subscription */
3455 CONNECTION_LOCK (signal_instance->connection);
3456 has_subscription = FALSE;
3457 if (g_hash_table_lookup (signal_instance->connection->map_id_to_signal_data,
3458 GUINT_TO_POINTER (signal_instance->subscription_id)) != NULL)
3459 has_subscription = TRUE;
3460 CONNECTION_UNLOCK (signal_instance->connection);
3462 if (has_subscription)
3463 signal_instance->callback (signal_instance->connection,
3464 signal_instance->sender,
3465 signal_instance->path,
3466 signal_instance->interface,
3467 signal_instance->member,
3469 signal_instance->user_data);
3471 g_variant_unref (parameters);
3477 signal_instance_free (SignalInstance *signal_instance)
3479 g_object_unref (signal_instance->message);
3480 g_object_unref (signal_instance->connection);
3481 g_free (signal_instance);
3484 /* called in message handler thread WITH lock held */
3486 schedule_callbacks (GDBusConnection *connection,
3487 GPtrArray *signal_data_array,
3488 GDBusMessage *message,
3489 const gchar *sender)
3492 const gchar *interface;
3493 const gchar *member;
3502 interface = g_dbus_message_get_interface (message);
3503 member = g_dbus_message_get_member (message);
3504 path = g_dbus_message_get_path (message);
3505 arg0 = g_dbus_message_get_arg0 (message);
3508 g_print ("In schedule_callbacks:\n"
3510 " interface = `%s'\n"
3521 /* TODO: if this is slow, then we can change signal_data_array into
3522 * map_object_path_to_signal_data_array or something.
3524 for (n = 0; n < signal_data_array->len; n++)
3526 SignalData *signal_data = signal_data_array->pdata[n];
3528 if (signal_data->interface_name != NULL && g_strcmp0 (signal_data->interface_name, interface) != 0)
3531 if (signal_data->member != NULL && g_strcmp0 (signal_data->member, member) != 0)
3534 if (signal_data->object_path != NULL && g_strcmp0 (signal_data->object_path, path) != 0)
3537 if (signal_data->arg0 != NULL && g_strcmp0 (signal_data->arg0, arg0) != 0)
3540 for (m = 0; m < signal_data->subscribers->len; m++)
3542 SignalSubscriber *subscriber;
3543 GSource *idle_source;
3544 SignalInstance *signal_instance;
3546 subscriber = &(g_array_index (signal_data->subscribers, SignalSubscriber, m));
3548 signal_instance = g_new0 (SignalInstance, 1);
3549 signal_instance->subscription_id = subscriber->id;
3550 signal_instance->callback = subscriber->callback;
3551 signal_instance->user_data = subscriber->user_data;
3552 signal_instance->message = g_object_ref (message);
3553 signal_instance->connection = g_object_ref (connection);
3554 signal_instance->sender = sender;
3555 signal_instance->path = path;
3556 signal_instance->interface = interface;
3557 signal_instance->member = member;
3559 idle_source = g_idle_source_new ();
3560 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
3561 g_source_set_callback (idle_source,
3562 emit_signal_instance_in_idle_cb,
3564 (GDestroyNotify) signal_instance_free);
3565 g_source_attach (idle_source, subscriber->context);
3566 g_source_unref (idle_source);
3571 /* called in message handler thread with lock held */
3573 distribute_signals (GDBusConnection *connection,
3574 GDBusMessage *message)
3576 GPtrArray *signal_data_array;
3577 const gchar *sender;
3579 sender = g_dbus_message_get_sender (message);
3581 if (G_UNLIKELY (_g_dbus_debug_signal ()))
3583 _g_dbus_debug_print_lock ();
3584 g_print ("========================================================================\n"
3585 "GDBus-debug:Signal:\n"
3586 " <<<< RECEIVED SIGNAL %s.%s\n"
3588 " sent by name %s\n",
3589 g_dbus_message_get_interface (message),
3590 g_dbus_message_get_member (message),
3591 g_dbus_message_get_path (message),
3592 sender != NULL ? sender : "(none)");
3593 _g_dbus_debug_print_unlock ();
3596 /* collect subscribers that match on sender */
3599 signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, sender);
3600 if (signal_data_array != NULL)
3601 schedule_callbacks (connection, signal_data_array, message, sender);
3604 /* collect subscribers not matching on sender */
3605 signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, "");
3606 if (signal_data_array != NULL)
3607 schedule_callbacks (connection, signal_data_array, message, sender);
3610 /* ---------------------------------------------------------------------------------------------------- */
3612 /* only called from finalize(), removes all subscriptions */
3614 purge_all_signal_subscriptions (GDBusConnection *connection)
3616 GHashTableIter iter;
3619 GArray *subscribers;
3622 ids = g_array_new (FALSE, FALSE, sizeof (guint));
3623 g_hash_table_iter_init (&iter, connection->map_id_to_signal_data);
3624 while (g_hash_table_iter_next (&iter, &key, NULL))
3626 guint subscription_id = GPOINTER_TO_UINT (key);
3627 g_array_append_val (ids, subscription_id);
3630 subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber));
3631 for (n = 0; n < ids->len; n++)
3633 guint subscription_id = g_array_index (ids, guint, n);
3634 unsubscribe_id_internal (connection,
3638 g_array_free (ids, TRUE);
3640 /* call GDestroyNotify without lock held */
3641 for (n = 0; n < subscribers->len; n++)
3643 SignalSubscriber *subscriber;
3644 subscriber = &(g_array_index (subscribers, SignalSubscriber, n));
3645 call_destroy_notify (subscriber->context,
3646 subscriber->user_data_free_func,
3647 subscriber->user_data);
3648 g_main_context_unref (subscriber->context);
3651 g_array_free (subscribers, TRUE);
3654 /* ---------------------------------------------------------------------------------------------------- */
3656 static GDBusInterfaceVTable *
3657 _g_dbus_interface_vtable_copy (const GDBusInterfaceVTable *vtable)
3659 /* Don't waste memory by copying padding - remember to update this
3660 * when changing struct _GDBusInterfaceVTable in gdbusconnection.h
3662 return g_memdup ((gconstpointer) vtable, 3 * sizeof (gpointer));
3666 _g_dbus_interface_vtable_free (GDBusInterfaceVTable *vtable)
3671 /* ---------------------------------------------------------------------------------------------------- */
3673 static GDBusSubtreeVTable *
3674 _g_dbus_subtree_vtable_copy (const GDBusSubtreeVTable *vtable)
3676 /* Don't waste memory by copying padding - remember to update this
3677 * when changing struct _GDBusSubtreeVTable in gdbusconnection.h
3679 return g_memdup ((gconstpointer) vtable, 3 * sizeof (gpointer));
3683 _g_dbus_subtree_vtable_free (GDBusSubtreeVTable *vtable)
3688 /* ---------------------------------------------------------------------------------------------------- */
3690 struct ExportedObject
3693 GDBusConnection *connection;
3695 /* maps gchar* -> ExportedInterface* */
3696 GHashTable *map_if_name_to_ei;
3699 /* only called with lock held */
3701 exported_object_free (ExportedObject *eo)
3703 g_free (eo->object_path);
3704 g_hash_table_unref (eo->map_if_name_to_ei);
3713 gchar *interface_name;
3714 GDBusInterfaceVTable *vtable;
3715 GDBusInterfaceInfo *interface_info;
3717 GMainContext *context;
3719 GDestroyNotify user_data_free_func;
3720 } ExportedInterface;
3722 /* called with lock held */
3724 exported_interface_free (ExportedInterface *ei)
3726 g_dbus_interface_info_cache_release (ei->interface_info);
3727 g_dbus_interface_info_unref ((GDBusInterfaceInfo *) ei->interface_info);
3729 call_destroy_notify (ei->context,
3730 ei->user_data_free_func,
3733 g_main_context_unref (ei->context);
3735 g_free (ei->interface_name);
3736 _g_dbus_interface_vtable_free (ei->vtable);
3740 /* ---------------------------------------------------------------------------------------------------- */
3742 /* Convenience function to check if @registration_id (if not zero) or
3743 * @subtree_registration_id (if not zero) has been unregistered. If
3744 * so, returns %TRUE.
3746 * Caller must *not* hold lock.
3749 has_object_been_unregistered (GDBusConnection *connection,
3750 guint registration_id,
3751 guint subtree_registration_id)
3755 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
3759 CONNECTION_LOCK (connection);
3760 if (registration_id != 0 && g_hash_table_lookup (connection->map_id_to_ei,
3761 GUINT_TO_POINTER (registration_id)) == NULL)
3765 else if (subtree_registration_id != 0 && g_hash_table_lookup (connection->map_id_to_es,
3766 GUINT_TO_POINTER (subtree_registration_id)) == NULL)
3770 CONNECTION_UNLOCK (connection);
3775 /* ---------------------------------------------------------------------------------------------------- */
3779 GDBusConnection *connection;
3780 GDBusMessage *message;
3782 const gchar *property_name;
3783 const GDBusInterfaceVTable *vtable;
3784 GDBusInterfaceInfo *interface_info;
3785 const GDBusPropertyInfo *property_info;
3786 guint registration_id;
3787 guint subtree_registration_id;
3791 property_data_free (PropertyData *data)
3793 g_object_unref (data->connection);
3794 g_object_unref (data->message);
3798 /* called in thread where object was registered - no locks held */
3800 invoke_get_property_in_idle_cb (gpointer _data)
3802 PropertyData *data = _data;
3805 GDBusMessage *reply;
3807 if (has_object_been_unregistered (data->connection,
3808 data->registration_id,
3809 data->subtree_registration_id))
3811 reply = g_dbus_message_new_method_error (data->message,
3812 "org.freedesktop.DBus.Error.UnknownMethod",
3813 _("No such interface `org.freedesktop.DBus.Properties' on object at path %s"),
3814 g_dbus_message_get_path (data->message));
3815 g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
3816 g_object_unref (reply);
3821 value = data->vtable->get_property (data->connection,
3822 g_dbus_message_get_sender (data->message),
3823 g_dbus_message_get_path (data->message),
3824 data->interface_info->name,
3825 data->property_name,
3832 g_assert_no_error (error);
3834 g_variant_take_ref (value);
3835 reply = g_dbus_message_new_method_reply (data->message);
3836 g_dbus_message_set_body (reply, g_variant_new ("(v)", value));
3837 g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
3838 g_variant_unref (value);
3839 g_object_unref (reply);
3843 gchar *dbus_error_name;
3844 g_assert (error != NULL);
3845 dbus_error_name = g_dbus_error_encode_gerror (error);
3846 reply = g_dbus_message_new_method_error_literal (data->message,
3849 g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
3850 g_free (dbus_error_name);
3851 g_error_free (error);
3852 g_object_unref (reply);
3859 /* called in thread where object was registered - no locks held */
3861 invoke_set_property_in_idle_cb (gpointer _data)
3863 PropertyData *data = _data;
3865 GDBusMessage *reply;
3871 g_variant_get (g_dbus_message_get_body (data->message),
3877 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
3878 * of the given value is wrong
3880 if (g_strcmp0 (g_variant_get_type_string (value), data->property_info->signature) != 0)
3882 reply = g_dbus_message_new_method_error (data->message,
3883 "org.freedesktop.DBus.Error.InvalidArgs",
3884 _("Error setting property `%s': Expected type `%s' but got `%s'"),
3885 data->property_info->name,
3886 data->property_info->signature,
3887 g_variant_get_type_string (value));
3891 if (!data->vtable->set_property (data->connection,
3892 g_dbus_message_get_sender (data->message),
3893 g_dbus_message_get_path (data->message),
3894 data->interface_info->name,
3895 data->property_name,
3900 gchar *dbus_error_name;
3901 g_assert (error != NULL);
3902 dbus_error_name = g_dbus_error_encode_gerror (error);
3903 reply = g_dbus_message_new_method_error_literal (data->message,
3906 g_free (dbus_error_name);
3907 g_error_free (error);
3911 reply = g_dbus_message_new_method_reply (data->message);
3915 g_assert (reply != NULL);
3916 g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
3917 g_object_unref (reply);
3918 g_variant_unref (value);
3923 /* called with lock held */
3925 validate_and_maybe_schedule_property_getset (GDBusConnection *connection,
3926 GDBusMessage *message,
3927 guint registration_id,
3928 guint subtree_registration_id,
3930 GDBusInterfaceInfo *interface_info,
3931 const GDBusInterfaceVTable *vtable,
3932 GMainContext *main_context,
3936 const char *interface_name;
3937 const char *property_name;
3938 const GDBusPropertyInfo *property_info;
3939 GSource *idle_source;
3940 PropertyData *property_data;
3941 GDBusMessage *reply;
3946 g_variant_get (g_dbus_message_get_body (message),
3951 g_variant_get (g_dbus_message_get_body (message),
3960 if (vtable == NULL || vtable->get_property == NULL)
3965 if (vtable == NULL || vtable->set_property == NULL)
3969 /* Check that the property exists - if not fail with org.freedesktop.DBus.Error.InvalidArgs
3971 property_info = NULL;
3973 /* TODO: the cost of this is O(n) - it might be worth caching the result */
3974 property_info = g_dbus_interface_info_lookup_property (interface_info, property_name);
3975 if (property_info == NULL)
3977 reply = g_dbus_message_new_method_error (message,
3978 "org.freedesktop.DBus.Error.InvalidArgs",
3979 _("No such property `%s'"),
3981 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
3982 g_object_unref (reply);
3987 if (is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
3989 reply = g_dbus_message_new_method_error (message,
3990 "org.freedesktop.DBus.Error.InvalidArgs",
3991 _("Property `%s' is not readable"),
3993 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
3994 g_object_unref (reply);
3998 else if (!is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
4000 reply = g_dbus_message_new_method_error (message,
4001 "org.freedesktop.DBus.Error.InvalidArgs",
4002 _("Property `%s' is not writable"),
4004 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4005 g_object_unref (reply);
4010 /* ok, got the property info - call user code in an idle handler */
4011 property_data = g_new0 (PropertyData, 1);
4012 property_data->connection = g_object_ref (connection);
4013 property_data->message = g_object_ref (message);
4014 property_data->user_data = user_data;
4015 property_data->property_name = property_name;
4016 property_data->vtable = vtable;
4017 property_data->interface_info = interface_info;
4018 property_data->property_info = property_info;
4019 property_data->registration_id = registration_id;
4020 property_data->subtree_registration_id = subtree_registration_id;
4022 idle_source = g_idle_source_new ();
4023 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
4024 g_source_set_callback (idle_source,
4025 is_get ? invoke_get_property_in_idle_cb : invoke_set_property_in_idle_cb,
4027 (GDestroyNotify) property_data_free);
4028 g_source_attach (idle_source, main_context);
4029 g_source_unref (idle_source);
4037 /* called with lock held */
4039 handle_getset_property (GDBusConnection *connection,
4041 GDBusMessage *message,
4044 ExportedInterface *ei;
4046 const char *interface_name;
4047 const char *property_name;
4052 g_variant_get (g_dbus_message_get_body (message),
4057 g_variant_get (g_dbus_message_get_body (message),
4063 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4064 * no such interface registered
4066 ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
4069 GDBusMessage *reply;
4070 reply = g_dbus_message_new_method_error (message,
4071 "org.freedesktop.DBus.Error.InvalidArgs",
4072 _("No such interface `%s'"),
4074 g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4075 g_object_unref (reply);
4080 handled = validate_and_maybe_schedule_property_getset (eo->connection,
4093 /* ---------------------------------------------------------------------------------------------------- */
4097 GDBusConnection *connection;
4098 GDBusMessage *message;
4100 const GDBusInterfaceVTable *vtable;
4101 GDBusInterfaceInfo *interface_info;
4102 guint registration_id;
4103 guint subtree_registration_id;
4104 } PropertyGetAllData;
4107 property_get_all_data_free (PropertyData *data)
4109 g_object_unref (data->connection);
4110 g_object_unref (data->message);
4114 /* called in thread where object was registered - no locks held */
4116 invoke_get_all_properties_in_idle_cb (gpointer _data)
4118 PropertyGetAllData *data = _data;
4119 GVariantBuilder builder;
4120 GDBusMessage *reply;
4123 if (has_object_been_unregistered (data->connection,
4124 data->registration_id,
4125 data->subtree_registration_id))
4127 reply = g_dbus_message_new_method_error (data->message,
4128 "org.freedesktop.DBus.Error.UnknownMethod",
4129 _("No such interface `org.freedesktop.DBus.Properties' on object at path %s"),
4130 g_dbus_message_get_path (data->message));
4131 g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4132 g_object_unref (reply);
4136 /* TODO: Right now we never fail this call - we just omit values if
4137 * a get_property() call is failing.
4139 * We could fail the whole call if just a single get_property() call
4140 * returns an error. We need clarification in the D-Bus spec about this.
4142 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a{sv})"));
4143 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
4144 for (n = 0; data->interface_info->properties != NULL && data->interface_info->properties[n] != NULL; n++)
4146 const GDBusPropertyInfo *property_info = data->interface_info->properties[n];
4149 if (!(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
4152 value = data->vtable->get_property (data->connection,
4153 g_dbus_message_get_sender (data->message),
4154 g_dbus_message_get_path (data->message),
4155 data->interface_info->name,
4156 property_info->name,
4163 g_variant_take_ref (value);
4164 g_variant_builder_add (&builder,
4166 property_info->name,
4168 g_variant_unref (value);
4170 g_variant_builder_close (&builder);
4172 reply = g_dbus_message_new_method_reply (data->message);
4173 g_dbus_message_set_body (reply, g_variant_builder_end (&builder));
4174 g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4175 g_object_unref (reply);
4181 /* called with lock held */
4183 validate_and_maybe_schedule_property_get_all (GDBusConnection *connection,
4184 GDBusMessage *message,
4185 guint registration_id,
4186 guint subtree_registration_id,
4187 GDBusInterfaceInfo *interface_info,
4188 const GDBusInterfaceVTable *vtable,
4189 GMainContext *main_context,
4193 const char *interface_name;
4194 GSource *idle_source;
4195 PropertyGetAllData *property_get_all_data;
4199 g_variant_get (g_dbus_message_get_body (message),
4203 if (vtable == NULL || vtable->get_property == NULL)
4206 /* ok, got the property info - call user in an idle handler */
4207 property_get_all_data = g_new0 (PropertyGetAllData, 1);
4208 property_get_all_data->connection = g_object_ref (connection);
4209 property_get_all_data->message = g_object_ref (message);
4210 property_get_all_data->user_data = user_data;
4211 property_get_all_data->vtable = vtable;
4212 property_get_all_data->interface_info = interface_info;
4213 property_get_all_data->registration_id = registration_id;
4214 property_get_all_data->subtree_registration_id = subtree_registration_id;
4216 idle_source = g_idle_source_new ();
4217 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
4218 g_source_set_callback (idle_source,
4219 invoke_get_all_properties_in_idle_cb,
4220 property_get_all_data,
4221 (GDestroyNotify) property_get_all_data_free);
4222 g_source_attach (idle_source, main_context);
4223 g_source_unref (idle_source);
4231 /* called with lock held */
4233 handle_get_all_properties (GDBusConnection *connection,
4235 GDBusMessage *message)
4237 ExportedInterface *ei;
4239 const char *interface_name;
4243 g_variant_get (g_dbus_message_get_body (message),
4247 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4248 * no such interface registered
4250 ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
4253 GDBusMessage *reply;
4254 reply = g_dbus_message_new_method_error (message,
4255 "org.freedesktop.DBus.Error.InvalidArgs",
4256 _("No such interface"),
4258 g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4259 g_object_unref (reply);
4264 handled = validate_and_maybe_schedule_property_get_all (eo->connection,
4276 /* ---------------------------------------------------------------------------------------------------- */
4278 static const gchar introspect_header[] =
4279 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
4280 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
4281 "<!-- GDBus " PACKAGE_VERSION " -->\n"
4284 static const gchar introspect_tail[] =
4287 static const gchar introspect_properties_interface[] =
4288 " <interface name=\"org.freedesktop.DBus.Properties\">\n"
4289 " <method name=\"Get\">\n"
4290 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4291 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4292 " <arg type=\"v\" name=\"value\" direction=\"out\"/>\n"
4294 " <method name=\"GetAll\">\n"
4295 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4296 " <arg type=\"a{sv}\" name=\"properties\" direction=\"out\"/>\n"
4298 " <method name=\"Set\">\n"
4299 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4300 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4301 " <arg type=\"v\" name=\"value\" direction=\"in\"/>\n"
4303 " <signal name=\"PropertiesChanged\">\n"
4304 " <arg type=\"s\" name=\"interface_name\"/>\n"
4305 " <arg type=\"a{sv}\" name=\"changed_properties\"/>\n"
4306 " <arg type=\"as\" name=\"invalidated_properties\"/>\n"
4310 static const gchar introspect_introspectable_interface[] =
4311 " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
4312 " <method name=\"Introspect\">\n"
4313 " <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>\n"
4316 " <interface name=\"org.freedesktop.DBus.Peer\">\n"
4317 " <method name=\"Ping\"/>\n"
4318 " <method name=\"GetMachineId\">\n"
4319 " <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n"
4324 introspect_append_header (GString *s)
4326 g_string_append (s, introspect_header);
4330 maybe_add_path (const gchar *path, gsize path_len, const gchar *object_path, GHashTable *set)
4332 if (g_str_has_prefix (object_path, path) && strlen (object_path) > path_len && object_path[path_len-1] == '/')
4338 begin = object_path + path_len;
4339 end = strchr (begin, '/');
4341 s = g_strndup (begin, end - begin);
4343 s = g_strdup (begin);
4345 if (g_hash_table_lookup (set, s) == NULL)
4346 g_hash_table_insert (set, s, GUINT_TO_POINTER (1));
4352 /* TODO: we want a nicer public interface for this */
4354 g_dbus_connection_list_registered_unlocked (GDBusConnection *connection,
4359 GHashTableIter hash_iter;
4360 const gchar *object_path;
4366 CONNECTION_ENSURE_LOCK (connection);
4368 path_len = strlen (path);
4372 set = g_hash_table_new (g_str_hash, g_str_equal);
4374 g_hash_table_iter_init (&hash_iter, connection->map_object_path_to_eo);
4375 while (g_hash_table_iter_next (&hash_iter, (gpointer) &object_path, NULL))
4376 maybe_add_path (path, path_len, object_path, set);
4378 g_hash_table_iter_init (&hash_iter, connection->map_object_path_to_es);
4379 while (g_hash_table_iter_next (&hash_iter, (gpointer) &object_path, NULL))
4380 maybe_add_path (path, path_len, object_path, set);
4382 p = g_ptr_array_new ();
4383 keys = g_hash_table_get_keys (set);
4384 for (l = keys; l != NULL; l = l->next)
4385 g_ptr_array_add (p, l->data);
4386 g_hash_table_unref (set);
4389 g_ptr_array_add (p, NULL);
4390 ret = (gchar **) g_ptr_array_free (p, FALSE);
4395 g_dbus_connection_list_registered (GDBusConnection *connection,
4399 CONNECTION_LOCK (connection);
4400 ret = g_dbus_connection_list_registered_unlocked (connection, path);
4401 CONNECTION_UNLOCK (connection);
4405 /* called in message handler thread with lock held */
4407 handle_introspect (GDBusConnection *connection,
4409 GDBusMessage *message)
4413 GDBusMessage *reply;
4414 GHashTableIter hash_iter;
4415 ExportedInterface *ei;
4418 /* first the header with the standard interfaces */
4419 s = g_string_sized_new (sizeof (introspect_header) +
4420 sizeof (introspect_properties_interface) +
4421 sizeof (introspect_introspectable_interface) +
4422 sizeof (introspect_tail));
4423 introspect_append_header (s);
4424 if (!g_hash_table_lookup (eo->map_if_name_to_ei,
4425 "org.freedesktop.DBus.Properties"))
4426 g_string_append (s, introspect_properties_interface);
4428 if (!g_hash_table_lookup (eo->map_if_name_to_ei,
4429 "org.freedesktop.DBus.Introspectable"))
4430 g_string_append (s, introspect_introspectable_interface);
4432 /* then include the registered interfaces */
4433 g_hash_table_iter_init (&hash_iter, eo->map_if_name_to_ei);
4434 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &ei))
4435 g_dbus_interface_info_generate_xml (ei->interface_info, 2, s);
4437 /* finally include nodes registered below us */
4438 registered = g_dbus_connection_list_registered_unlocked (connection, eo->object_path);
4439 for (n = 0; registered != NULL && registered[n] != NULL; n++)
4440 g_string_append_printf (s, " <node name=\"%s\"/>\n", registered[n]);
4441 g_strfreev (registered);
4442 g_string_append (s, introspect_tail);
4444 reply = g_dbus_message_new_method_reply (message);
4445 g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
4446 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4447 g_object_unref (reply);
4448 g_string_free (s, TRUE);
4453 /* called in thread where object was registered - no locks held */
4455 call_in_idle_cb (gpointer user_data)
4457 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
4458 GDBusInterfaceVTable *vtable;
4459 guint registration_id;
4460 guint subtree_registration_id;
4462 vtable = g_object_get_data (G_OBJECT (invocation), "g-dbus-interface-vtable");
4463 g_assert (vtable != NULL && vtable->method_call != NULL);
4465 registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-registration-id"));
4466 subtree_registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id"));
4468 if (has_object_been_unregistered (g_dbus_method_invocation_get_connection (invocation),
4470 subtree_registration_id))
4472 GDBusMessage *reply;
4473 reply = g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation),
4474 "org.freedesktop.DBus.Error.UnknownMethod",
4475 _("No such interface `%s' on object at path %s"),
4476 g_dbus_method_invocation_get_interface_name (invocation),
4477 g_dbus_method_invocation_get_object_path (invocation));
4478 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4479 g_object_unref (reply);
4483 vtable->method_call (g_dbus_method_invocation_get_connection (invocation),
4484 g_dbus_method_invocation_get_sender (invocation),
4485 g_dbus_method_invocation_get_object_path (invocation),
4486 g_dbus_method_invocation_get_interface_name (invocation),
4487 g_dbus_method_invocation_get_method_name (invocation),
4488 g_dbus_method_invocation_get_parameters (invocation),
4489 g_object_ref (invocation),
4490 g_dbus_method_invocation_get_user_data (invocation));
4496 /* called in message handler thread with lock held */
4498 validate_and_maybe_schedule_method_call (GDBusConnection *connection,
4499 GDBusMessage *message,
4500 guint registration_id,
4501 guint subtree_registration_id,
4502 GDBusInterfaceInfo *interface_info,
4503 const GDBusInterfaceVTable *vtable,
4504 GMainContext *main_context,
4507 GDBusMethodInvocation *invocation;
4508 const GDBusMethodInfo *method_info;
4509 GDBusMessage *reply;
4510 GVariant *parameters;
4511 GSource *idle_source;
4513 GVariantType *in_type;
4517 /* TODO: the cost of this is O(n) - it might be worth caching the result */
4518 method_info = g_dbus_interface_info_lookup_method (interface_info, g_dbus_message_get_member (message));
4520 /* if the method doesn't exist, return the org.freedesktop.DBus.Error.UnknownMethod
4521 * error to the caller
4523 if (method_info == NULL)
4525 reply = g_dbus_message_new_method_error (message,
4526 "org.freedesktop.DBus.Error.UnknownMethod",
4527 _("No such method `%s'"),
4528 g_dbus_message_get_member (message));
4529 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4530 g_object_unref (reply);
4535 parameters = g_dbus_message_get_body (message);
4536 if (parameters == NULL)
4538 parameters = g_variant_new ("()");
4539 g_variant_ref_sink (parameters);
4543 g_variant_ref (parameters);
4546 /* Check that the incoming args are of the right type - if they are not, return
4547 * the org.freedesktop.DBus.Error.InvalidArgs error to the caller
4549 in_type = _g_dbus_compute_complete_signature (method_info->in_args);
4550 if (!g_variant_is_of_type (parameters, in_type))
4554 type_string = g_variant_type_dup_string (in_type);
4556 reply = g_dbus_message_new_method_error (message,
4557 "org.freedesktop.DBus.Error.InvalidArgs",
4558 _("Type of message, `%s', does not match expected type `%s'"),
4559 g_variant_get_type_string (parameters),
4561 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4562 g_variant_type_free (in_type);
4563 g_variant_unref (parameters);
4564 g_object_unref (reply);
4565 g_free (type_string);
4569 g_variant_type_free (in_type);
4571 /* schedule the call in idle */
4572 invocation = _g_dbus_method_invocation_new (g_dbus_message_get_sender (message),
4573 g_dbus_message_get_path (message),
4574 g_dbus_message_get_interface (message),
4575 g_dbus_message_get_member (message),
4581 g_variant_unref (parameters);
4583 /* TODO: would be nicer with a real MethodData like we already
4584 * have PropertyData and PropertyGetAllData... */
4585 g_object_set_data (G_OBJECT (invocation), "g-dbus-interface-vtable", (gpointer) vtable);
4586 g_object_set_data (G_OBJECT (invocation), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id));
4587 g_object_set_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id));
4589 idle_source = g_idle_source_new ();
4590 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
4591 g_source_set_callback (idle_source,
4595 g_source_attach (idle_source, main_context);
4596 g_source_unref (idle_source);
4604 /* ---------------------------------------------------------------------------------------------------- */
4606 /* called in message handler thread with lock held */
4608 obj_message_func (GDBusConnection *connection,
4610 GDBusMessage *message)
4612 const gchar *interface_name;
4613 const gchar *member;
4614 const gchar *signature;
4619 interface_name = g_dbus_message_get_interface (message);
4620 member = g_dbus_message_get_member (message);
4621 signature = g_dbus_message_get_signature (message);
4623 /* see if we have an interface for handling this call */
4624 if (interface_name != NULL)
4626 ExportedInterface *ei;
4627 ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
4630 /* we do - invoke the handler in idle in the right thread */
4632 /* handle no vtable or handler being present */
4633 if (ei->vtable == NULL || ei->vtable->method_call == NULL)
4636 handled = validate_and_maybe_schedule_method_call (connection,
4648 if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Introspectable") == 0 &&
4649 g_strcmp0 (member, "Introspect") == 0 &&
4650 g_strcmp0 (signature, "") == 0)
4652 handled = handle_introspect (connection, eo, message);
4655 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
4656 g_strcmp0 (member, "Get") == 0 &&
4657 g_strcmp0 (signature, "ss") == 0)
4659 handled = handle_getset_property (connection, eo, message, TRUE);
4662 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
4663 g_strcmp0 (member, "Set") == 0 &&
4664 g_strcmp0 (signature, "ssv") == 0)
4666 handled = handle_getset_property (connection, eo, message, FALSE);
4669 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
4670 g_strcmp0 (member, "GetAll") == 0 &&
4671 g_strcmp0 (signature, "s") == 0)
4673 handled = handle_get_all_properties (connection, eo, message);
4682 * g_dbus_connection_register_object:
4683 * @connection: A #GDBusConnection.
4684 * @object_path: The object path to register at.
4685 * @interface_info: Introspection data for the interface.
4686 * @vtable: (allow-none): A #GDBusInterfaceVTable to call into or %NULL.
4687 * @user_data: (allow-none): Data to pass to functions in @vtable.
4688 * @user_data_free_func: Function to call when the object path is unregistered.
4689 * @error: Return location for error or %NULL.
4691 * Registers callbacks for exported objects at @object_path with the
4692 * D-Bus interface that is described in @interface_info.
4694 * Calls to functions in @vtable (and @user_data_free_func) will
4695 * happen in the <link linkend="g-main-context-push-thread-default">thread-default main
4696 * loop</link> of the thread you are calling this method from.
4698 * Note that all #GVariant values passed to functions in @vtable will match
4699 * the signature given in @interface_info - if a remote caller passes
4700 * incorrect values, the <literal>org.freedesktop.DBus.Error.InvalidArgs</literal>
4701 * is returned to the remote caller.
4703 * Additionally, if the remote caller attempts to invoke methods or
4704 * access properties not mentioned in @interface_info the
4705 * <literal>org.freedesktop.DBus.Error.UnknownMethod</literal> resp.
4706 * <literal>org.freedesktop.DBus.Error.InvalidArgs</literal> errors
4707 * are returned to the caller.
4709 * It is considered a programming error if the
4710 * #GDBusInterfaceGetPropertyFunc function in @vtable returns a
4711 * #GVariant of incorrect type.
4713 * If an existing callback is already registered at @object_path and
4714 * @interface_name, then @error is set to #G_IO_ERROR_EXISTS.
4716 * GDBus automatically implements the standard D-Bus interfaces
4717 * org.freedesktop.DBus.Properties, org.freedesktop.DBus.Introspectable
4718 * and org.freedesktop.Peer, so you don't have to implement those for
4719 * the objects you export. You <emphasis>can</emphasis> implement
4720 * org.freedesktop.DBus.Properties yourself, e.g. to handle getting
4721 * and setting of properties asynchronously.
4723 * Note that the reference count on @interface_info will be
4724 * incremented by 1 (unless allocated statically, e.g. if the
4725 * reference count is -1, see g_dbus_interface_info_ref()) for as long
4726 * as the object is exported. Also note that @vtable will be copied.
4728 * See <xref linkend="gdbus-server"/> for an example of how to use this method.
4730 * Returns: 0 if @error is set, otherwise a registration id (never 0)
4731 * that can be used with g_dbus_connection_unregister_object() .
4736 g_dbus_connection_register_object (GDBusConnection *connection,
4737 const gchar *object_path,
4738 GDBusInterfaceInfo *interface_info,
4739 const GDBusInterfaceVTable *vtable,
4741 GDestroyNotify user_data_free_func,
4745 ExportedInterface *ei;
4748 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
4749 g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
4750 g_return_val_if_fail (interface_info != NULL, 0);
4751 g_return_val_if_fail (g_dbus_is_interface_name (interface_info->name), 0);
4752 g_return_val_if_fail (error == NULL || *error == NULL, 0);
4756 CONNECTION_LOCK (connection);
4758 eo = g_hash_table_lookup (connection->map_object_path_to_eo, object_path);
4761 eo = g_new0 (ExportedObject, 1);
4762 eo->object_path = g_strdup (object_path);
4763 eo->connection = connection;
4764 eo->map_if_name_to_ei = g_hash_table_new_full (g_str_hash,
4767 (GDestroyNotify) exported_interface_free);
4768 g_hash_table_insert (connection->map_object_path_to_eo, eo->object_path, eo);
4771 ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_info->name);
4777 _("An object is already exported for the interface %s at %s"),
4778 interface_info->name,
4783 ei = g_new0 (ExportedInterface, 1);
4784 ei->id = _global_registration_id++; /* TODO: overflow etc. */
4786 ei->user_data = user_data;
4787 ei->user_data_free_func = user_data_free_func;
4788 ei->vtable = _g_dbus_interface_vtable_copy (vtable);
4789 ei->interface_info = g_dbus_interface_info_ref (interface_info);
4790 g_dbus_interface_info_cache_build (ei->interface_info);
4791 ei->interface_name = g_strdup (interface_info->name);
4792 ei->context = g_main_context_ref_thread_default ();
4794 g_hash_table_insert (eo->map_if_name_to_ei,
4795 (gpointer) ei->interface_name,
4797 g_hash_table_insert (connection->map_id_to_ei,
4798 GUINT_TO_POINTER (ei->id),
4804 CONNECTION_UNLOCK (connection);
4810 * g_dbus_connection_unregister_object:
4811 * @connection: A #GDBusConnection.
4812 * @registration_id: A registration id obtained from g_dbus_connection_register_object().
4814 * Unregisters an object.
4816 * Returns: %TRUE if the object was unregistered, %FALSE otherwise.
4821 g_dbus_connection_unregister_object (GDBusConnection *connection,
4822 guint registration_id)
4824 ExportedInterface *ei;
4828 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
4832 CONNECTION_LOCK (connection);
4834 ei = g_hash_table_lookup (connection->map_id_to_ei,
4835 GUINT_TO_POINTER (registration_id));
4841 g_warn_if_fail (g_hash_table_remove (connection->map_id_to_ei, GUINT_TO_POINTER (ei->id)));
4842 g_warn_if_fail (g_hash_table_remove (eo->map_if_name_to_ei, ei->interface_name));
4843 /* unregister object path if we have no more exported interfaces */
4844 if (g_hash_table_size (eo->map_if_name_to_ei) == 0)
4845 g_warn_if_fail (g_hash_table_remove (connection->map_object_path_to_eo,
4851 CONNECTION_UNLOCK (connection);
4856 /* ---------------------------------------------------------------------------------------------------- */
4859 * g_dbus_connection_emit_signal:
4860 * @connection: A #GDBusConnection.
4861 * @destination_bus_name: (allow-none): The unique bus name for the destination
4862 * for the signal or %NULL to emit to all listeners.
4863 * @object_path: Path of remote object.
4864 * @interface_name: D-Bus interface to emit a signal on.
4865 * @signal_name: The name of the signal to emit.
4866 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
4867 * or %NULL if not passing parameters.
4868 * @error: Return location for error or %NULL.
4872 * If the parameters GVariant is floating, it is consumed.
4874 * This can only fail if @parameters is not compatible with the D-Bus protocol.
4876 * Returns: %TRUE unless @error is set.
4881 g_dbus_connection_emit_signal (GDBusConnection *connection,
4882 const gchar *destination_bus_name,
4883 const gchar *object_path,
4884 const gchar *interface_name,
4885 const gchar *signal_name,
4886 GVariant *parameters,
4889 GDBusMessage *message;
4895 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
4896 g_return_val_if_fail (destination_bus_name == NULL || g_dbus_is_name (destination_bus_name), FALSE);
4897 g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), FALSE);
4898 g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), FALSE);
4899 g_return_val_if_fail (signal_name != NULL && g_dbus_is_member_name (signal_name), FALSE);
4900 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), FALSE);
4902 if (G_UNLIKELY (_g_dbus_debug_emission ()))
4904 _g_dbus_debug_print_lock ();
4905 g_print ("========================================================================\n"
4906 "GDBus-debug:Emission:\n"
4907 " >>>> SIGNAL EMISSION %s.%s()\n"
4909 " destination %s\n",
4910 interface_name, signal_name,
4912 destination_bus_name != NULL ? destination_bus_name : "(none)");
4913 _g_dbus_debug_print_unlock ();
4916 message = g_dbus_message_new_signal (object_path,
4920 if (destination_bus_name != NULL)
4921 g_dbus_message_set_header (message,
4922 G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION,
4923 g_variant_new_string (destination_bus_name));
4925 if (parameters != NULL)
4926 g_dbus_message_set_body (message, parameters);
4928 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, error);
4929 g_object_unref (message);
4935 add_call_flags (GDBusMessage *message,
4936 GDBusCallFlags flags)
4938 if (flags & G_DBUS_CALL_FLAGS_NO_AUTO_START)
4939 g_dbus_message_set_flags (message, G_DBUS_MESSAGE_FLAGS_NO_AUTO_START);
4943 decode_method_reply (GDBusMessage *reply,
4944 const gchar *method_name,
4945 const GVariantType *reply_type,
4946 GUnixFDList **out_fd_list,
4952 switch (g_dbus_message_get_message_type (reply))
4954 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
4955 result = g_dbus_message_get_body (reply);
4958 result = g_variant_new ("()");
4959 g_variant_ref_sink (result);
4963 g_variant_ref (result);
4966 if (!g_variant_is_of_type (result, reply_type))
4968 gchar *type_string = g_variant_type_dup_string (reply_type);
4972 G_IO_ERROR_INVALID_ARGUMENT,
4973 _("Method `%s' returned type `%s', but expected `%s'"),
4974 method_name, g_variant_get_type_string (result), type_string);
4976 g_variant_unref (result);
4977 g_free (type_string);
4984 if (out_fd_list != NULL)
4986 *out_fd_list = g_dbus_message_get_unix_fd_list (reply);
4987 if (*out_fd_list != NULL)
4988 g_object_ref (*out_fd_list);
4994 case G_DBUS_MESSAGE_TYPE_ERROR:
4995 g_dbus_message_to_gerror (reply, error);
4999 g_assert_not_reached ();
5009 GSimpleAsyncResult *simple;
5010 GVariantType *reply_type;
5011 gchar *method_name; /* for error message */
5015 GUnixFDList *fd_list;
5019 call_state_free (CallState *state)
5021 g_variant_type_free (state->reply_type);
5022 g_free (state->method_name);
5024 if (state->value != NULL)
5025 g_variant_unref (state->value);
5026 if (state->fd_list != NULL)
5027 g_object_unref (state->fd_list);
5028 g_slice_free (CallState, state);
5032 g_dbus_connection_call_done (GObject *source,
5033 GAsyncResult *result,
5036 GSimpleAsyncResult *simple;
5037 GDBusConnection *connection = G_DBUS_CONNECTION (source);
5038 CallState *state = user_data;
5040 GDBusMessage *reply;
5043 reply = g_dbus_connection_send_message_with_reply_finish (connection,
5047 if (G_UNLIKELY (_g_dbus_debug_call ()))
5049 _g_dbus_debug_print_lock ();
5050 g_print ("========================================================================\n"
5051 "GDBus-debug:Call:\n"
5052 " <<<< ASYNC COMPLETE %s() (serial %d)\n"
5058 g_print ("SUCCESS\n");
5062 g_print ("FAILED: %s\n",
5065 _g_dbus_debug_print_unlock ();
5069 state->value = decode_method_reply (reply, state->method_name, state->reply_type, &state->fd_list, &error);
5071 simple = state->simple; /* why? because state is freed before we unref simple.. */
5074 g_simple_async_result_take_error (state->simple, error);
5075 g_simple_async_result_complete (state->simple);
5076 call_state_free (state);
5080 g_simple_async_result_set_op_res_gpointer (state->simple, state, (GDestroyNotify) call_state_free);
5081 g_simple_async_result_complete (state->simple);
5082 g_object_unref (reply);
5084 g_object_unref (simple);
5088 g_dbus_connection_call_internal (GDBusConnection *connection,
5089 const gchar *bus_name,
5090 const gchar *object_path,
5091 const gchar *interface_name,
5092 const gchar *method_name,
5093 GVariant *parameters,
5094 const GVariantType *reply_type,
5095 GDBusCallFlags flags,
5097 GUnixFDList *fd_list,
5098 GCancellable *cancellable,
5099 GAsyncReadyCallback callback,
5102 GDBusMessage *message;
5105 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
5106 g_return_if_fail (bus_name == NULL || g_dbus_is_name (bus_name));
5107 g_return_if_fail (object_path != NULL && g_variant_is_object_path (object_path));
5108 g_return_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name));
5109 g_return_if_fail (method_name != NULL && g_dbus_is_member_name (method_name));
5110 g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
5111 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
5113 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
5115 g_return_if_fail (fd_list == NULL);
5118 state = g_slice_new0 (CallState);
5119 state->simple = g_simple_async_result_new (G_OBJECT (connection),
5120 callback, user_data,
5121 g_dbus_connection_call_internal);
5122 state->method_name = g_strjoin (".", interface_name, method_name, NULL);
5124 if (reply_type == NULL)
5125 reply_type = G_VARIANT_TYPE_ANY;
5127 state->reply_type = g_variant_type_copy (reply_type);
5129 message = g_dbus_message_new_method_call (bus_name,
5133 add_call_flags (message, flags);
5134 if (parameters != NULL)
5135 g_dbus_message_set_body (message, parameters);
5138 if (fd_list != NULL)
5139 g_dbus_message_set_unix_fd_list (message, fd_list);
5142 g_dbus_connection_send_message_with_reply (connection,
5144 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
5148 g_dbus_connection_call_done,
5151 if (G_UNLIKELY (_g_dbus_debug_call ()))
5153 _g_dbus_debug_print_lock ();
5154 g_print ("========================================================================\n"
5155 "GDBus-debug:Call:\n"
5156 " >>>> ASYNC %s.%s()\n"
5158 " owned by name %s (serial %d)\n",
5162 bus_name != NULL ? bus_name : "(none)",
5164 _g_dbus_debug_print_unlock ();
5167 if (message != NULL)
5168 g_object_unref (message);
5172 g_dbus_connection_call_finish_internal (GDBusConnection *connection,
5173 GUnixFDList **out_fd_list,
5177 GSimpleAsyncResult *simple;
5180 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
5181 g_return_val_if_fail (g_simple_async_result_is_valid (res, G_OBJECT (connection),
5182 g_dbus_connection_call_internal), NULL);
5183 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
5185 simple = G_SIMPLE_ASYNC_RESULT (res);
5187 if (g_simple_async_result_propagate_error (simple, error))
5190 state = g_simple_async_result_get_op_res_gpointer (simple);
5191 if (out_fd_list != NULL)
5192 *out_fd_list = state->fd_list != NULL ? g_object_ref (state->fd_list) : NULL;
5193 return g_variant_ref (state->value);
5197 g_dbus_connection_call_sync_internal (GDBusConnection *connection,
5198 const gchar *bus_name,
5199 const gchar *object_path,
5200 const gchar *interface_name,
5201 const gchar *method_name,
5202 GVariant *parameters,
5203 const GVariantType *reply_type,
5204 GDBusCallFlags flags,
5206 GUnixFDList *fd_list,
5207 GUnixFDList **out_fd_list,
5208 GCancellable *cancellable,
5211 GDBusMessage *message;
5212 GDBusMessage *reply;
5214 GError *local_error;
5220 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
5221 g_return_val_if_fail (bus_name == NULL || g_dbus_is_name (bus_name), NULL);
5222 g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), NULL);
5223 g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), NULL);
5224 g_return_val_if_fail (method_name != NULL && g_dbus_is_member_name (method_name), NULL);
5225 g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
5226 g_return_val_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
5228 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
5230 g_return_val_if_fail (fd_list == NULL, NULL);
5232 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
5234 if (reply_type == NULL)
5235 reply_type = G_VARIANT_TYPE_ANY;
5237 message = g_dbus_message_new_method_call (bus_name,
5241 add_call_flags (message, flags);
5242 if (parameters != NULL)
5243 g_dbus_message_set_body (message, parameters);
5246 if (fd_list != NULL)
5247 g_dbus_message_set_unix_fd_list (message, fd_list);
5250 if (G_UNLIKELY (_g_dbus_debug_call ()))
5252 _g_dbus_debug_print_lock ();
5253 g_print ("========================================================================\n"
5254 "GDBus-debug:Call:\n"
5255 " >>>> SYNC %s.%s()\n"
5257 " owned by name %s\n",
5261 bus_name != NULL ? bus_name : "(none)");
5262 _g_dbus_debug_print_unlock ();
5266 reply = g_dbus_connection_send_message_with_reply_sync (connection,
5268 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
5270 NULL, /* volatile guint32 *out_serial */
5274 if (G_UNLIKELY (_g_dbus_debug_call ()))
5276 _g_dbus_debug_print_lock ();
5277 g_print ("========================================================================\n"
5278 "GDBus-debug:Call:\n"
5279 " <<<< SYNC COMPLETE %s.%s()\n"
5285 g_print ("SUCCESS\n");
5289 g_print ("FAILED: %s\n",
5290 local_error->message);
5292 _g_dbus_debug_print_unlock ();
5298 *error = local_error;
5300 g_error_free (local_error);
5304 result = decode_method_reply (reply, method_name, reply_type, out_fd_list, error);
5307 if (message != NULL)
5308 g_object_unref (message);
5310 g_object_unref (reply);
5315 /* ---------------------------------------------------------------------------------------------------- */
5318 * g_dbus_connection_call:
5319 * @connection: A #GDBusConnection.
5320 * @bus_name: (allow-none): A unique or well-known bus name or %NULL if
5321 * @connection is not a message bus connection.
5322 * @object_path: Path of remote object.
5323 * @interface_name: D-Bus interface to invoke method on.
5324 * @method_name: The name of the method to invoke.
5325 * @parameters: (allow-none): A #GVariant tuple with parameters for the method
5326 * or %NULL if not passing parameters.
5327 * @reply_type: (allow-none): The expected type of the reply, or %NULL.
5328 * @flags: Flags from the #GDBusCallFlags enumeration.
5329 * @timeout_msec: The timeout in milliseconds, -1 to use the default
5330 * timeout or %G_MAXINT for no timeout.
5331 * @cancellable: A #GCancellable or %NULL.
5332 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
5333 * satisfied or %NULL if you don't * care about the result of the
5334 * method invocation.
5335 * @user_data: The data to pass to @callback.
5337 * Asynchronously invokes the @method_name method on the
5338 * @interface_name D-Bus interface on the remote object at
5339 * @object_path owned by @bus_name.
5341 * If @connection is closed then the operation will fail with
5342 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
5343 * fail with %G_IO_ERROR_CANCELLED. If @parameters contains a value
5344 * not compatible with the D-Bus protocol, the operation fails with
5345 * %G_IO_ERROR_INVALID_ARGUMENT.
5347 * If @reply_type is non-%NULL then the reply will be checked for having this type and an
5348 * error will be raised if it does not match. Said another way, if you give a @reply_type
5349 * then any non-%NULL return value will be of this type.
5351 * If the @parameters #GVariant is floating, it is consumed. This allows
5352 * convenient 'inline' use of g_variant_new(), e.g.:
5354 * g_dbus_connection_call (connection,
5355 * "org.freedesktop.StringThings",
5356 * "/org/freedesktop/StringThings",
5357 * "org.freedesktop.StringThings",
5359 * g_variant_new ("(ss)",
5363 * G_DBUS_CALL_FLAGS_NONE,
5366 * (GAsyncReadyCallback) two_strings_done,
5370 * This is an asynchronous method. When the operation is finished, @callback will be invoked
5371 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
5372 * of the thread you are calling this method from. You can then call
5373 * g_dbus_connection_call_finish() to get the result of the operation.
5374 * See g_dbus_connection_call_sync() for the synchronous version of this
5380 g_dbus_connection_call (GDBusConnection *connection,
5381 const gchar *bus_name,
5382 const gchar *object_path,
5383 const gchar *interface_name,
5384 const gchar *method_name,
5385 GVariant *parameters,
5386 const GVariantType *reply_type,
5387 GDBusCallFlags flags,
5389 GCancellable *cancellable,
5390 GAsyncReadyCallback callback,
5393 g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, NULL, cancellable, callback, user_data);
5397 * g_dbus_connection_call_finish:
5398 * @connection: A #GDBusConnection.
5399 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call().
5400 * @error: Return location for error or %NULL.
5402 * Finishes an operation started with g_dbus_connection_call().
5404 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
5405 * return values. Free with g_variant_unref().
5410 g_dbus_connection_call_finish (GDBusConnection *connection,
5414 return g_dbus_connection_call_finish_internal (connection, NULL, res, error);
5418 * g_dbus_connection_call_sync:
5419 * @connection: A #GDBusConnection.
5420 * @bus_name: A unique or well-known bus name.
5421 * @object_path: Path of remote object.
5422 * @interface_name: D-Bus interface to invoke method on.
5423 * @method_name: The name of the method to invoke.
5424 * @parameters: (allow-none): A #GVariant tuple with parameters for the method
5425 * or %NULL if not passing parameters.
5426 * @reply_type: (allow-none): The expected type of the reply, or %NULL.
5427 * @flags: Flags from the #GDBusCallFlags enumeration.
5428 * @timeout_msec: The timeout in milliseconds, -1 to use the default
5429 * timeout or %G_MAXINT for no timeout.
5430 * @cancellable: A #GCancellable or %NULL.
5431 * @error: Return location for error or %NULL.
5433 * Synchronously invokes the @method_name method on the
5434 * @interface_name D-Bus interface on the remote object at
5435 * @object_path owned by @bus_name.
5437 * If @connection is closed then the operation will fail with
5438 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the
5439 * operation will fail with %G_IO_ERROR_CANCELLED. If @parameters
5440 * contains a value not compatible with the D-Bus protocol, the operation
5441 * fails with %G_IO_ERROR_INVALID_ARGUMENT.
5443 * If @reply_type is non-%NULL then the reply will be checked for having
5444 * this type and an error will be raised if it does not match. Said
5445 * another way, if you give a @reply_type then any non-%NULL return
5446 * value will be of this type.
5448 * If the @parameters #GVariant is floating, it is consumed.
5449 * This allows convenient 'inline' use of g_variant_new(), e.g.:
5451 * g_dbus_connection_call_sync (connection,
5452 * "org.freedesktop.StringThings",
5453 * "/org/freedesktop/StringThings",
5454 * "org.freedesktop.StringThings",
5456 * g_variant_new ("(ss)",
5460 * G_DBUS_CALL_FLAGS_NONE,
5466 * The calling thread is blocked until a reply is received. See
5467 * g_dbus_connection_call() for the asynchronous version of
5470 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
5471 * return values. Free with g_variant_unref().
5476 g_dbus_connection_call_sync (GDBusConnection *connection,
5477 const gchar *bus_name,
5478 const gchar *object_path,
5479 const gchar *interface_name,
5480 const gchar *method_name,
5481 GVariant *parameters,
5482 const GVariantType *reply_type,
5483 GDBusCallFlags flags,
5485 GCancellable *cancellable,
5488 return g_dbus_connection_call_sync_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, NULL, NULL, cancellable, error);
5491 /* ---------------------------------------------------------------------------------------------------- */
5496 * g_dbus_connection_call_with_unix_fd_list:
5497 * @connection: A #GDBusConnection.
5498 * @bus_name: (allow-none): A unique or well-known bus name or %NULL if
5499 * @connection is not a message bus connection.
5500 * @object_path: Path of remote object.
5501 * @interface_name: D-Bus interface to invoke method on.
5502 * @method_name: The name of the method to invoke.
5503 * @parameters: (allow-none): A #GVariant tuple with parameters for the method
5504 * or %NULL if not passing parameters.
5505 * @reply_type: (allow-none): The expected type of the reply, or %NULL.
5506 * @flags: Flags from the #GDBusCallFlags enumeration.
5507 * @timeout_msec: The timeout in milliseconds, -1 to use the default
5508 * timeout or %G_MAXINT for no timeout.
5509 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
5510 * @cancellable: A #GCancellable or %NULL.
5511 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
5512 * satisfied or %NULL if you don't * care about the result of the
5513 * method invocation.
5514 * @user_data: The data to pass to @callback.
5516 * Like g_dbus_connection_call() but also takes a #GUnixFDList object.
5518 * This method is only available on UNIX.
5523 g_dbus_connection_call_with_unix_fd_list (GDBusConnection *connection,
5524 const gchar *bus_name,
5525 const gchar *object_path,
5526 const gchar *interface_name,
5527 const gchar *method_name,
5528 GVariant *parameters,
5529 const GVariantType *reply_type,
5530 GDBusCallFlags flags,
5532 GUnixFDList *fd_list,
5533 GCancellable *cancellable,
5534 GAsyncReadyCallback callback,
5537 g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, fd_list, cancellable, callback, user_data);
5541 * g_dbus_connection_call_with_unix_fd_list_finish:
5542 * @connection: A #GDBusConnection.
5543 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
5544 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call_with_unix_fd_list().
5545 * @error: Return location for error or %NULL.
5547 * Finishes an operation started with g_dbus_connection_call_with_unix_fd_list().
5549 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
5550 * return values. Free with g_variant_unref().
5555 g_dbus_connection_call_with_unix_fd_list_finish (GDBusConnection *connection,
5556 GUnixFDList **out_fd_list,
5560 return g_dbus_connection_call_finish_internal (connection, out_fd_list, res, error);
5564 * g_dbus_connection_call_with_unix_fd_list_sync:
5565 * @connection: A #GDBusConnection.
5566 * @bus_name: A unique or well-known bus name.
5567 * @object_path: Path of remote object.
5568 * @interface_name: D-Bus interface to invoke method on.
5569 * @method_name: The name of the method to invoke.
5570 * @parameters: (allow-none): A #GVariant tuple with parameters for the method
5571 * or %NULL if not passing parameters.
5572 * @reply_type: (allow-none): The expected type of the reply, or %NULL.
5573 * @flags: Flags from the #GDBusCallFlags enumeration.
5574 * @timeout_msec: The timeout in milliseconds, -1 to use the default
5575 * timeout or %G_MAXINT for no timeout.
5576 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
5577 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
5578 * @cancellable: A #GCancellable or %NULL.
5579 * @error: Return location for error or %NULL.
5581 * Like g_dbus_connection_call_sync() but also takes and returns #GUnixFDList objects.
5583 * This method is only available on UNIX.
5585 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
5586 * return values. Free with g_variant_unref().
5591 g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection *connection,
5592 const gchar *bus_name,
5593 const gchar *object_path,
5594 const gchar *interface_name,
5595 const gchar *method_name,
5596 GVariant *parameters,
5597 const GVariantType *reply_type,
5598 GDBusCallFlags flags,
5600 GUnixFDList *fd_list,
5601 GUnixFDList **out_fd_list,
5602 GCancellable *cancellable,
5605 return g_dbus_connection_call_sync_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
5608 #endif /* G_OS_UNIX */
5610 /* ---------------------------------------------------------------------------------------------------- */
5612 struct ExportedSubtree
5616 GDBusConnection *connection;
5617 GDBusSubtreeVTable *vtable;
5618 GDBusSubtreeFlags flags;
5620 GMainContext *context;
5622 GDestroyNotify user_data_free_func;
5626 exported_subtree_free (ExportedSubtree *es)
5628 call_destroy_notify (es->context,
5629 es->user_data_free_func,
5632 g_main_context_unref (es->context);
5634 _g_dbus_subtree_vtable_free (es->vtable);
5635 g_free (es->object_path);
5639 /* called without lock held */
5641 handle_subtree_introspect (GDBusConnection *connection,
5642 ExportedSubtree *es,
5643 GDBusMessage *message)
5647 GDBusMessage *reply;
5650 const gchar *sender;
5651 const gchar *requested_object_path;
5652 const gchar *requested_node;
5653 GDBusInterfaceInfo **interfaces;
5655 gchar **subnode_paths;
5656 gboolean has_properties_interface;
5657 gboolean has_introspectable_interface;
5661 requested_object_path = g_dbus_message_get_path (message);
5662 sender = g_dbus_message_get_sender (message);
5663 is_root = (g_strcmp0 (requested_object_path, es->object_path) == 0);
5665 s = g_string_new (NULL);
5666 introspect_append_header (s);
5668 /* Strictly we don't need the children in dynamic mode, but we avoid the
5669 * conditionals to preserve code clarity
5671 children = es->vtable->enumerate (es->connection,
5678 requested_node = strrchr (requested_object_path, '/') + 1;
5680 /* Assert existence of object if we are not dynamic */
5681 if (!(es->flags & G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES) &&
5682 !_g_strv_has_string ((const gchar * const *) children, requested_node))
5687 requested_node = NULL;
5690 interfaces = es->vtable->introspect (es->connection,
5695 if (interfaces != NULL)
5697 has_properties_interface = FALSE;
5698 has_introspectable_interface = FALSE;
5700 for (n = 0; interfaces[n] != NULL; n++)
5702 if (strcmp (interfaces[n]->name, "org.freedesktop.DBus.Properties") == 0)
5703 has_properties_interface = TRUE;
5704 else if (strcmp (interfaces[n]->name, "org.freedesktop.DBus.Introspectable") == 0)
5705 has_introspectable_interface = TRUE;
5707 if (!has_properties_interface)
5708 g_string_append (s, introspect_properties_interface);
5709 if (!has_introspectable_interface)
5710 g_string_append (s, introspect_introspectable_interface);
5712 for (n = 0; interfaces[n] != NULL; n++)
5714 g_dbus_interface_info_generate_xml (interfaces[n], 2, s);
5715 g_dbus_interface_info_unref (interfaces[n]);
5717 g_free (interfaces);
5720 /* then include <node> entries from the Subtree for the root */
5723 for (n = 0; children != NULL && children[n] != NULL; n++)
5724 g_string_append_printf (s, " <node name=\"%s\"/>\n", children[n]);
5727 /* finally include nodes registered below us */
5728 subnode_paths = g_dbus_connection_list_registered (es->connection, requested_object_path);
5729 for (n = 0; subnode_paths != NULL && subnode_paths[n] != NULL; n++)
5730 g_string_append_printf (s, " <node name=\"%s\"/>\n", subnode_paths[n]);
5731 g_strfreev (subnode_paths);
5733 g_string_append (s, "</node>\n");
5735 reply = g_dbus_message_new_method_reply (message);
5736 g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
5737 g_dbus_connection_send_message (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
5738 g_object_unref (reply);
5743 g_string_free (s, TRUE);
5744 g_strfreev (children);
5748 /* called without lock held */
5750 handle_subtree_method_invocation (GDBusConnection *connection,
5751 ExportedSubtree *es,
5752 GDBusMessage *message)
5755 const gchar *sender;
5756 const gchar *interface_name;
5757 const gchar *member;
5758 const gchar *signature;
5759 const gchar *requested_object_path;
5760 const gchar *requested_node;
5762 GDBusInterfaceInfo *interface_info;
5763 const GDBusInterfaceVTable *interface_vtable;
5764 gpointer interface_user_data;
5766 GDBusInterfaceInfo **interfaces;
5767 gboolean is_property_get;
5768 gboolean is_property_set;
5769 gboolean is_property_get_all;
5774 requested_object_path = g_dbus_message_get_path (message);
5775 sender = g_dbus_message_get_sender (message);
5776 interface_name = g_dbus_message_get_interface (message);
5777 member = g_dbus_message_get_member (message);
5778 signature = g_dbus_message_get_signature (message);
5779 is_root = (g_strcmp0 (requested_object_path, es->object_path) == 0);
5781 is_property_get = FALSE;
5782 is_property_set = FALSE;
5783 is_property_get_all = FALSE;
5784 if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0)
5786 if (g_strcmp0 (member, "Get") == 0 && g_strcmp0 (signature, "ss") == 0)
5787 is_property_get = TRUE;
5788 else if (g_strcmp0 (member, "Set") == 0 && g_strcmp0 (signature, "ssv") == 0)
5789 is_property_set = TRUE;
5790 else if (g_strcmp0 (member, "GetAll") == 0 && g_strcmp0 (signature, "s") == 0)
5791 is_property_get_all = TRUE;
5796 requested_node = strrchr (requested_object_path, '/') + 1;
5798 if (~es->flags & G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES)
5800 /* We don't want to dispatch to unenumerated
5801 * nodes, so ensure that the child exists.
5806 children = es->vtable->enumerate (es->connection,
5811 exists = _g_strv_has_string ((const gchar * const *) children, requested_node);
5812 g_strfreev (children);
5820 requested_node = NULL;
5823 /* get introspection data for the node */
5824 interfaces = es->vtable->introspect (es->connection,
5826 requested_object_path,
5830 if (interfaces == NULL)
5833 interface_info = NULL;
5834 for (n = 0; interfaces[n] != NULL; n++)
5836 if (g_strcmp0 (interfaces[n]->name, interface_name) == 0)
5837 interface_info = interfaces[n];
5840 /* dispatch the call if the user wants to handle it */
5841 if (interface_info != NULL)
5843 /* figure out where to dispatch the method call */
5844 interface_user_data = NULL;
5845 interface_vtable = es->vtable->dispatch (es->connection,
5850 &interface_user_data,
5852 if (interface_vtable == NULL)
5855 CONNECTION_LOCK (connection);
5856 handled = validate_and_maybe_schedule_method_call (es->connection,
5863 interface_user_data);
5864 CONNECTION_UNLOCK (connection);
5866 /* handle org.freedesktop.DBus.Properties interface if not explicitly handled */
5867 else if (is_property_get || is_property_set || is_property_get_all)
5869 if (is_property_get)
5870 g_variant_get (g_dbus_message_get_body (message), "(&s&s)", &interface_name, NULL);
5871 else if (is_property_set)
5872 g_variant_get (g_dbus_message_get_body (message), "(&s&sv)", &interface_name, NULL, NULL);
5873 else if (is_property_get_all)
5874 g_variant_get (g_dbus_message_get_body (message), "(&s)", &interface_name, NULL, NULL);
5876 g_assert_not_reached ();
5878 /* see if the object supports this interface at all */
5879 for (n = 0; interfaces[n] != NULL; n++)
5881 if (g_strcmp0 (interfaces[n]->name, interface_name) == 0)
5882 interface_info = interfaces[n];
5885 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the user-code
5886 * claims it won't support the interface
5888 if (interface_info == NULL)
5890 GDBusMessage *reply;
5891 reply = g_dbus_message_new_method_error (message,
5892 "org.freedesktop.DBus.Error.InvalidArgs",
5893 _("No such interface `%s'"),
5895 g_dbus_connection_send_message (es->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
5896 g_object_unref (reply);
5901 /* figure out where to dispatch the property get/set/getall calls */
5902 interface_user_data = NULL;
5903 interface_vtable = es->vtable->dispatch (es->connection,
5908 &interface_user_data,
5910 if (interface_vtable == NULL)
5912 g_warning ("The subtree introspection function indicates that '%s' "
5913 "is a valid interface name, but calling the dispatch "
5914 "function on that interface gave us NULL", interface_name);
5918 if (is_property_get || is_property_set)
5920 CONNECTION_LOCK (connection);
5921 handled = validate_and_maybe_schedule_property_getset (es->connection,
5929 interface_user_data);
5930 CONNECTION_UNLOCK (connection);
5932 else if (is_property_get_all)
5934 CONNECTION_LOCK (connection);
5935 handled = validate_and_maybe_schedule_property_get_all (es->connection,
5942 interface_user_data);
5943 CONNECTION_UNLOCK (connection);
5948 if (interfaces != NULL)
5950 for (n = 0; interfaces[n] != NULL; n++)
5951 g_dbus_interface_info_unref (interfaces[n]);
5952 g_free (interfaces);
5960 GDBusMessage *message;
5961 ExportedSubtree *es;
5962 } SubtreeDeferredData;
5965 subtree_deferred_data_free (SubtreeDeferredData *data)
5967 g_object_unref (data->message);
5971 /* called without lock held in the thread where the caller registered the subtree */
5973 process_subtree_vtable_message_in_idle_cb (gpointer _data)
5975 SubtreeDeferredData *data = _data;
5980 if (g_strcmp0 (g_dbus_message_get_interface (data->message), "org.freedesktop.DBus.Introspectable") == 0 &&
5981 g_strcmp0 (g_dbus_message_get_member (data->message), "Introspect") == 0 &&
5982 g_strcmp0 (g_dbus_message_get_signature (data->message), "") == 0)
5983 handled = handle_subtree_introspect (data->es->connection,
5987 handled = handle_subtree_method_invocation (data->es->connection,
5993 CONNECTION_LOCK (data->es->connection);
5994 handled = handle_generic_unlocked (data->es->connection, data->message);
5995 CONNECTION_UNLOCK (data->es->connection);
5998 /* if we couldn't handle the request, just bail with the UnknownMethod error */
6001 GDBusMessage *reply;
6002 reply = g_dbus_message_new_method_error (data->message,
6003 "org.freedesktop.DBus.Error.UnknownMethod",
6004 _("Method `%s' on interface `%s' with signature `%s' does not exist"),
6005 g_dbus_message_get_member (data->message),
6006 g_dbus_message_get_interface (data->message),
6007 g_dbus_message_get_signature (data->message));
6008 g_dbus_connection_send_message (data->es->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
6009 g_object_unref (reply);
6015 /* called in message handler thread with lock held */
6017 subtree_message_func (GDBusConnection *connection,
6018 ExportedSubtree *es,
6019 GDBusMessage *message)
6021 GSource *idle_source;
6022 SubtreeDeferredData *data;
6024 data = g_new0 (SubtreeDeferredData, 1);
6025 data->message = g_object_ref (message);
6028 /* defer this call to an idle handler in the right thread */
6029 idle_source = g_idle_source_new ();
6030 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
6031 g_source_set_callback (idle_source,
6032 process_subtree_vtable_message_in_idle_cb,
6034 (GDestroyNotify) subtree_deferred_data_free);
6035 g_source_attach (idle_source, es->context);
6036 g_source_unref (idle_source);
6038 /* since we own the entire subtree, handlers for objects not in the subtree have been
6039 * tried already by libdbus-1 - so we just need to ensure that we're always going
6040 * to reply to the message
6046 * g_dbus_connection_register_subtree:
6047 * @connection: A #GDBusConnection.
6048 * @object_path: The object path to register the subtree at.
6049 * @vtable: A #GDBusSubtreeVTable to enumerate, introspect and dispatch nodes in the subtree.
6050 * @flags: Flags used to fine tune the behavior of the subtree.
6051 * @user_data: Data to pass to functions in @vtable.
6052 * @user_data_free_func: Function to call when the subtree is unregistered.
6053 * @error: Return location for error or %NULL.
6055 * Registers a whole subtree of <quote>dynamic</quote> objects.
6057 * The @enumerate and @introspection functions in @vtable are used to
6058 * convey, to remote callers, what nodes exist in the subtree rooted
6061 * When handling remote calls into any node in the subtree, first the
6062 * @enumerate function is used to check if the node exists. If the node exists
6063 * or the #G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is set
6064 * the @introspection function is used to check if the node supports the
6065 * requested method. If so, the @dispatch function is used to determine
6066 * where to dispatch the call. The collected #GDBusInterfaceVTable and
6067 * #gpointer will be used to call into the interface vtable for processing
6070 * All calls into user-provided code will be invoked in the <link
6071 * linkend="g-main-context-push-thread-default">thread-default main
6072 * loop</link> of the thread you are calling this method from.
6074 * If an existing subtree is already registered at @object_path or
6075 * then @error is set to #G_IO_ERROR_EXISTS.
6077 * Note that it is valid to register regular objects (using
6078 * g_dbus_connection_register_object()) in a subtree registered with
6079 * g_dbus_connection_register_subtree() - if so, the subtree handler
6080 * is tried as the last resort. One way to think about a subtree
6081 * handler is to consider it a <quote>fallback handler</quote>
6082 * for object paths not registered via g_dbus_connection_register_object()
6083 * or other bindings.
6085 * Note that @vtable will be copied so you cannot change it after
6088 * See <xref linkend="gdbus-subtree-server"/> for an example of how to use this method.
6090 * Returns: 0 if @error is set, otherwise a subtree registration id (never 0)
6091 * that can be used with g_dbus_connection_unregister_subtree() .
6096 g_dbus_connection_register_subtree (GDBusConnection *connection,
6097 const gchar *object_path,
6098 const GDBusSubtreeVTable *vtable,
6099 GDBusSubtreeFlags flags,
6101 GDestroyNotify user_data_free_func,
6105 ExportedSubtree *es;
6107 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
6108 g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
6109 g_return_val_if_fail (vtable != NULL, 0);
6110 g_return_val_if_fail (error == NULL || *error == NULL, 0);
6114 CONNECTION_LOCK (connection);
6116 es = g_hash_table_lookup (connection->map_object_path_to_es, object_path);
6122 _("A subtree is already exported for %s"),
6127 es = g_new0 (ExportedSubtree, 1);
6128 es->object_path = g_strdup (object_path);
6129 es->connection = connection;
6131 es->vtable = _g_dbus_subtree_vtable_copy (vtable);
6133 es->id = _global_subtree_registration_id++; /* TODO: overflow etc. */
6134 es->user_data = user_data;
6135 es->user_data_free_func = user_data_free_func;
6136 es->context = g_main_context_ref_thread_default ();
6138 g_hash_table_insert (connection->map_object_path_to_es, es->object_path, es);
6139 g_hash_table_insert (connection->map_id_to_es,
6140 GUINT_TO_POINTER (es->id),
6146 CONNECTION_UNLOCK (connection);
6151 /* ---------------------------------------------------------------------------------------------------- */
6154 * g_dbus_connection_unregister_subtree:
6155 * @connection: A #GDBusConnection.
6156 * @registration_id: A subtree registration id obtained from g_dbus_connection_register_subtree().
6158 * Unregisters a subtree.
6160 * Returns: %TRUE if the subtree was unregistered, %FALSE otherwise.
6165 g_dbus_connection_unregister_subtree (GDBusConnection *connection,
6166 guint registration_id)
6168 ExportedSubtree *es;
6171 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
6175 CONNECTION_LOCK (connection);
6177 es = g_hash_table_lookup (connection->map_id_to_es,
6178 GUINT_TO_POINTER (registration_id));
6182 g_warn_if_fail (g_hash_table_remove (connection->map_id_to_es, GUINT_TO_POINTER (es->id)));
6183 g_warn_if_fail (g_hash_table_remove (connection->map_object_path_to_es, es->object_path));
6188 CONNECTION_UNLOCK (connection);
6193 /* ---------------------------------------------------------------------------------------------------- */
6195 /* must be called with lock held */
6197 handle_generic_ping_unlocked (GDBusConnection *connection,
6198 const gchar *object_path,
6199 GDBusMessage *message)
6201 GDBusMessage *reply;
6202 reply = g_dbus_message_new_method_reply (message);
6203 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
6204 g_object_unref (reply);
6207 /* must be called with lock held */
6209 handle_generic_get_machine_id_unlocked (GDBusConnection *connection,
6210 const gchar *object_path,
6211 GDBusMessage *message)
6213 GDBusMessage *reply;
6216 if (connection->machine_id == NULL)
6221 connection->machine_id = _g_dbus_get_machine_id (&error);
6222 if (connection->machine_id == NULL)
6224 reply = g_dbus_message_new_method_error_literal (message,
6225 "org.freedesktop.DBus.Error.Failed",
6227 g_error_free (error);
6233 reply = g_dbus_message_new_method_reply (message);
6234 g_dbus_message_set_body (reply, g_variant_new ("(s)", connection->machine_id));
6236 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
6237 g_object_unref (reply);
6240 /* must be called with lock held */
6242 handle_generic_introspect_unlocked (GDBusConnection *connection,
6243 const gchar *object_path,
6244 GDBusMessage *message)
6249 GDBusMessage *reply;
6251 /* first the header */
6252 s = g_string_new (NULL);
6253 introspect_append_header (s);
6255 registered = g_dbus_connection_list_registered_unlocked (connection, object_path);
6256 for (n = 0; registered != NULL && registered[n] != NULL; n++)
6257 g_string_append_printf (s, " <node name=\"%s\"/>\n", registered[n]);
6258 g_strfreev (registered);
6259 g_string_append (s, "</node>\n");
6261 reply = g_dbus_message_new_method_reply (message);
6262 g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
6263 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
6264 g_object_unref (reply);
6265 g_string_free (s, TRUE);
6268 /* must be called with lock held */
6270 handle_generic_unlocked (GDBusConnection *connection,
6271 GDBusMessage *message)
6274 const gchar *interface_name;
6275 const gchar *member;
6276 const gchar *signature;
6279 CONNECTION_ENSURE_LOCK (connection);
6283 interface_name = g_dbus_message_get_interface (message);
6284 member = g_dbus_message_get_member (message);
6285 signature = g_dbus_message_get_signature (message);
6286 path = g_dbus_message_get_path (message);
6288 if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Introspectable") == 0 &&
6289 g_strcmp0 (member, "Introspect") == 0 &&
6290 g_strcmp0 (signature, "") == 0)
6292 handle_generic_introspect_unlocked (connection, path, message);
6295 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Peer") == 0 &&
6296 g_strcmp0 (member, "Ping") == 0 &&
6297 g_strcmp0 (signature, "") == 0)
6299 handle_generic_ping_unlocked (connection, path, message);
6302 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Peer") == 0 &&
6303 g_strcmp0 (member, "GetMachineId") == 0 &&
6304 g_strcmp0 (signature, "") == 0)
6306 handle_generic_get_machine_id_unlocked (connection, path, message);
6313 /* ---------------------------------------------------------------------------------------------------- */
6315 /* called in message handler thread with lock held */
6317 distribute_method_call (GDBusConnection *connection,
6318 GDBusMessage *message)
6320 GDBusMessage *reply;
6322 ExportedSubtree *es;
6323 const gchar *object_path;
6324 const gchar *interface_name;
6325 const gchar *member;
6327 gchar *subtree_path;
6330 g_assert (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL);
6332 interface_name = g_dbus_message_get_interface (message);
6333 member = g_dbus_message_get_member (message);
6334 path = g_dbus_message_get_path (message);
6335 subtree_path = g_strdup (path);
6336 needle = strrchr (subtree_path, '/');
6337 if (needle != NULL && needle != subtree_path)
6343 g_free (subtree_path);
6344 subtree_path = NULL;
6348 if (G_UNLIKELY (_g_dbus_debug_incoming ()))
6350 _g_dbus_debug_print_lock ();
6351 g_print ("========================================================================\n"
6352 "GDBus-debug:Incoming:\n"
6353 " <<<< METHOD INVOCATION %s.%s()\n"
6355 " invoked by name %s\n"
6357 interface_name, member,
6359 g_dbus_message_get_sender (message) != NULL ? g_dbus_message_get_sender (message) : "(none)",
6360 g_dbus_message_get_serial (message));
6361 _g_dbus_debug_print_unlock ();
6364 object_path = g_dbus_message_get_path (message);
6365 g_assert (object_path != NULL);
6367 eo = g_hash_table_lookup (connection->map_object_path_to_eo, object_path);
6370 if (obj_message_func (connection, eo, message))
6374 es = g_hash_table_lookup (connection->map_object_path_to_es, object_path);
6377 if (subtree_message_func (connection, es, message))
6381 if (subtree_path != NULL)
6383 es = g_hash_table_lookup (connection->map_object_path_to_es, subtree_path);
6386 if (subtree_message_func (connection, es, message))
6391 if (handle_generic_unlocked (connection, message))
6394 /* if we end up here, the message has not been not handled - so return an error saying this */
6395 reply = g_dbus_message_new_method_error (message,
6396 "org.freedesktop.DBus.Error.UnknownMethod",
6397 _("No such interface `%s' on object at path %s"),
6400 g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
6401 g_object_unref (reply);
6404 g_free (subtree_path);
6407 /* ---------------------------------------------------------------------------------------------------- */
6409 static GDBusConnection **
6410 message_bus_get_singleton (GBusType bus_type,
6413 GDBusConnection **ret;
6414 const gchar *starter_bus;
6420 case G_BUS_TYPE_SESSION:
6421 ret = &the_session_bus;
6424 case G_BUS_TYPE_SYSTEM:
6425 ret = &the_system_bus;
6428 case G_BUS_TYPE_STARTER:
6429 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
6430 if (g_strcmp0 (starter_bus, "session") == 0)
6432 ret = message_bus_get_singleton (G_BUS_TYPE_SESSION, error);
6435 else if (g_strcmp0 (starter_bus, "system") == 0)
6437 ret = message_bus_get_singleton (G_BUS_TYPE_SYSTEM, error);
6442 if (starter_bus != NULL)
6446 G_IO_ERROR_INVALID_ARGUMENT,
6447 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
6448 " - unknown value `%s'"),
6453 g_set_error_literal (error,
6455 G_IO_ERROR_INVALID_ARGUMENT,
6456 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
6457 "variable is not set"));
6463 g_assert_not_reached ();
6471 static GDBusConnection *
6472 get_uninitialized_connection (GBusType bus_type,
6473 GCancellable *cancellable,
6476 GDBusConnection **singleton;
6477 GDBusConnection *ret;
6481 G_LOCK (message_bus_lock);
6482 singleton = message_bus_get_singleton (bus_type, error);
6483 if (singleton == NULL)
6486 if (*singleton == NULL)
6489 address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
6490 if (address == NULL)
6492 ret = *singleton = g_object_new (G_TYPE_DBUS_CONNECTION,
6494 "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
6495 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
6496 "exit-on-close", TRUE,
6502 ret = g_object_ref (*singleton);
6505 g_assert (ret != NULL);
6508 G_UNLOCK (message_bus_lock);
6514 * @bus_type: A #GBusType.
6515 * @cancellable: A #GCancellable or %NULL.
6516 * @error: Return location for error or %NULL.
6518 * Synchronously connects to the message bus specified by @bus_type.
6519 * Note that the returned object may shared with other callers,
6520 * e.g. if two separate parts of a process calls this function with
6521 * the same @bus_type, they will share the same object.
6523 * This is a synchronous failable function. See g_bus_get() and
6524 * g_bus_get_finish() for the asynchronous version.
6526 * The returned object is a singleton, that is, shared with other
6527 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
6528 * event that you need a private message bus connection, use
6529 * g_dbus_address_get_for_bus_sync() and
6530 * g_dbus_connection_new_for_address().
6532 * Note that the returned #GDBusConnection object will (usually) have
6533 * the #GDBusConnection:exit-on-close property set to %TRUE.
6535 * Returns: (transfer full): A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
6540 g_bus_get_sync (GBusType bus_type,
6541 GCancellable *cancellable,
6544 GDBusConnection *connection;
6546 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
6548 connection = get_uninitialized_connection (bus_type, cancellable, error);
6549 if (connection == NULL)
6552 if (!g_initable_init (G_INITABLE (connection), cancellable, error))
6554 g_object_unref (connection);
6563 bus_get_async_initable_cb (GObject *source_object,
6567 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
6571 if (!g_async_initable_init_finish (G_ASYNC_INITABLE (source_object),
6575 g_assert (error != NULL);
6576 g_simple_async_result_take_error (simple, error);
6577 g_object_unref (source_object);
6581 g_simple_async_result_set_op_res_gpointer (simple,
6585 g_simple_async_result_complete_in_idle (simple);
6586 g_object_unref (simple);
6591 * @bus_type: A #GBusType.
6592 * @cancellable: A #GCancellable or %NULL.
6593 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
6594 * @user_data: The data to pass to @callback.
6596 * Asynchronously connects to the message bus specified by @bus_type.
6598 * When the operation is finished, @callback will be invoked. You can
6599 * then call g_bus_get_finish() to get the result of the operation.
6601 * This is a asynchronous failable function. See g_bus_get_sync() for
6602 * the synchronous version.
6607 g_bus_get (GBusType bus_type,
6608 GCancellable *cancellable,
6609 GAsyncReadyCallback callback,
6612 GDBusConnection *connection;
6613 GSimpleAsyncResult *simple;
6616 simple = g_simple_async_result_new (NULL,
6622 connection = get_uninitialized_connection (bus_type, cancellable, &error);
6623 if (connection == NULL)
6625 g_assert (error != NULL);
6626 g_simple_async_result_take_error (simple, error);
6627 g_simple_async_result_complete_in_idle (simple);
6628 g_object_unref (simple);
6632 g_async_initable_init_async (G_ASYNC_INITABLE (connection),
6635 bus_get_async_initable_cb,
6642 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_bus_get().
6643 * @error: Return location for error or %NULL.
6645 * Finishes an operation started with g_bus_get().
6647 * The returned object is a singleton, that is, shared with other
6648 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
6649 * event that you need a private message bus connection, use
6650 * g_dbus_address_get_for_bus_sync() and
6651 * g_dbus_connection_new_for_address().
6653 * Note that the returned #GDBusConnection object will (usually) have
6654 * the #GDBusConnection:exit-on-close property set to %TRUE.
6656 * Returns: (transfer full): A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
6661 g_bus_get_finish (GAsyncResult *res,
6664 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
6666 GDBusConnection *ret;
6668 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
6670 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_bus_get);
6674 if (g_simple_async_result_propagate_error (simple, error))
6677 object = g_simple_async_result_get_op_res_gpointer (simple);
6678 g_assert (object != NULL);
6679 ret = g_object_ref (G_DBUS_CONNECTION (object));
6685 /* ---------------------------------------------------------------------------------------------------- */