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 * As an exception to the usual GLib rule that a particular object must not be
152 * used by two threads at the same time, #GDBusConnection's methods may be
153 * called from any thread<footnote>
155 * This is so that g_bus_get() and g_bus_get_sync() can safely return the
156 * same #GDBusConnection when called from any thread.
160 * Most of the ways to obtain a #GDBusConnection automatically initialize it
161 * (i.e. connect to D-Bus): for instance, g_dbus_connection_new() and
162 * g_bus_get(), and the synchronous versions of those methods, give you an
163 * initialized connection. Language bindings for GIO should use
164 * g_initable_new() or g_async_initable_new_async(), which also initialize the
167 * If you construct an uninitialized #GDBusConnection, such as via
168 * g_object_new(), you must initialize it via g_initable_init() or
169 * g_async_initable_init_async() before using its methods or properties.
170 * Calling methods or accessing properties on a #GDBusConnection that has not
171 * completed initialization successfully is considered to be invalid, and leads
172 * to undefined behaviour. In particular, if initialization fails with a
173 * #GError, the only valid thing you can do with that #GDBusConnection is to
174 * free it with g_object_unref().
176 * <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>
178 * <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>
180 * <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>
182 * <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>
185 /* ---------------------------------------------------------------------------------------------------- */
187 typedef struct _GDBusConnectionClass GDBusConnectionClass
;
190 * GDBusConnectionClass:
191 * @closed: Signal class handler for the #GDBusConnection::closed signal.
193 * Class structure for #GDBusConnection.
197 struct _GDBusConnectionClass
200 GObjectClass parent_class
;
204 void (*closed
) (GDBusConnection
*connection
,
205 gboolean remote_peer_vanished
,
209 G_LOCK_DEFINE_STATIC (message_bus_lock
);
211 static GWeakRef the_session_bus
;
212 static GWeakRef the_system_bus
;
214 /* Extra pseudo-member of GDBusSendMessageFlags.
215 * Set by initable_init() to indicate that despite not being initialized yet,
216 * enough of the only-valid-after-init members are set that we can send a
217 * message, and we're being called from its thread, so no memory barrier is
218 * required before accessing them.
220 #define SEND_MESSAGE_FLAGS_INITIALIZING (1<<31)
222 /* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */
223 #define CALL_FLAGS_INITIALIZING (1<<31)
225 /* ---------------------------------------------------------------------------------------------------- */
229 GDestroyNotify callback
;
231 GMainContext
*context
;
232 } CallDestroyNotifyData
;
235 call_destroy_notify_data_in_idle (gpointer user_data
)
237 CallDestroyNotifyData
*data
= user_data
;
238 data
->callback (data
->user_data
);
243 call_destroy_notify_data_free (CallDestroyNotifyData
*data
)
245 if (data
->context
!= NULL
)
246 g_main_context_unref (data
->context
);
251 * call_destroy_notify: <internal>
252 * @context: (allow-none): A #GMainContext or %NULL.
253 * @callback: (allow-none): A #GDestroyNotify or %NULL.
254 * @user_data: Data to pass to @callback.
256 * Schedules @callback to run in @context.
259 call_destroy_notify (GMainContext
*context
,
260 GDestroyNotify callback
,
263 GSource
*idle_source
;
264 CallDestroyNotifyData
*data
;
266 if (callback
== NULL
)
269 data
= g_new0 (CallDestroyNotifyData
, 1);
270 data
->callback
= callback
;
271 data
->user_data
= user_data
;
272 data
->context
= context
;
273 if (data
->context
!= NULL
)
274 g_main_context_ref (data
->context
);
276 idle_source
= g_idle_source_new ();
277 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
278 g_source_set_callback (idle_source
,
279 call_destroy_notify_data_in_idle
,
281 (GDestroyNotify
) call_destroy_notify_data_free
);
282 g_source_attach (idle_source
, data
->context
);
283 g_source_unref (idle_source
);
289 /* ---------------------------------------------------------------------------------------------------- */
292 _g_strv_has_string (const gchar
* const *haystack
,
297 for (n
= 0; haystack
!= NULL
&& haystack
[n
] != NULL
; n
++)
299 if (g_strcmp0 (haystack
[n
], needle
) == 0)
305 /* ---------------------------------------------------------------------------------------------------- */
308 #define CONNECTION_ENSURE_LOCK(obj) do { ; } while (FALSE)
310 // TODO: for some reason this doesn't work on Windows
311 #define CONNECTION_ENSURE_LOCK(obj) do { \
312 if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock))) \
314 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
315 "CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \
320 #define CONNECTION_LOCK(obj) do { \
321 g_mutex_lock (&(obj)->lock); \
324 #define CONNECTION_UNLOCK(obj) do { \
325 g_mutex_unlock (&(obj)->lock); \
328 /* Flags in connection->atomic_flags */
330 FLAG_INITIALIZED
= 1 << 0,
331 FLAG_EXIT_ON_CLOSE
= 1 << 1,
338 * The #GDBusConnection structure contains only private data and
339 * should only be accessed using the provided API.
343 struct _GDBusConnection
346 GObject parent_instance
;
348 /* ------------------------------------------------------------------------ */
349 /* -- General object state ------------------------------------------------ */
350 /* ------------------------------------------------------------------------ */
352 /* General-purpose lock for most fields */
355 /* A lock used in the init() method of the GInitable interface - see comments
356 * in initable_init() for why a separate lock is needed.
358 * If you need both @lock and @init_lock, you must take @init_lock first.
362 /* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
363 * someone calls org.freedesktop.DBus.GetMachineId(). Protected by @lock.
367 /* The underlying stream used for communication
368 * Read-only after initable_init(), so it may be read if you either
369 * hold @init_lock or check for initialization first.
373 /* The object used for authentication (if any).
374 * Read-only after initable_init(), so it may be read if you either
375 * hold @init_lock or check for initialization first.
379 /* Last serial used. Protected by @lock. */
382 /* The object used to send/receive messages.
383 * Read-only after initable_init(), so it may be read if you either
384 * hold @init_lock or check for initialization first.
388 /* If connected to a message bus, this contains the unique name assigned to
389 * us by the bus (e.g. ":1.42").
390 * Read-only after initable_init(), so it may be read if you either
391 * hold @init_lock or check for initialization first.
393 gchar
*bus_unique_name
;
395 /* The GUID returned by the other side if we authenticed as a client or
396 * the GUID to use if authenticating as a server.
397 * Read-only after initable_init(), so it may be read if you either
398 * hold @init_lock or check for initialization first.
402 /* FLAG_INITIALIZED is set exactly when initable_init() has finished running.
403 * Inspect @initialization_error to see whether it succeeded or failed.
405 * FLAG_EXIT_ON_CLOSE is the exit-on-close property.
407 * FLAG_CLOSED is the closed property. It may be read at any time, but
408 * may only be written while holding @lock.
410 volatile gint atomic_flags
;
412 /* If the connection could not be established during initable_init(),
413 * this GError will be set.
414 * Read-only after initable_init(), so it may be read if you either
415 * hold @init_lock or check for initialization first.
417 GError
*initialization_error
;
419 /* The result of g_main_context_ref_thread_default() when the object
420 * was created (the GObject _init() function) - this is used for delivery
421 * of the :closed GObject signal.
423 * Only set in the GObject init function, so no locks are needed.
425 GMainContext
*main_context_at_construction
;
427 /* Read-only construct properties, no locks needed */
429 GDBusConnectionFlags flags
;
431 /* Map used for managing method replies, protected by @lock */
432 GHashTable
*map_method_serial_to_send_message_data
; /* guint32 -> SendMessageData* */
434 /* Maps used for managing signal subscription, protected by @lock */
435 GHashTable
*map_rule_to_signal_data
; /* match rule (gchar*) -> SignalData */
436 GHashTable
*map_id_to_signal_data
; /* id (guint) -> SignalData */
437 GHashTable
*map_sender_unique_name_to_signal_data_array
; /* unique sender (gchar*) -> GPtrArray* of SignalData */
439 /* Maps used for managing exported objects and subtrees,
442 GHashTable
*map_object_path_to_eo
; /* gchar* -> ExportedObject* */
443 GHashTable
*map_id_to_ei
; /* guint -> ExportedInterface* */
444 GHashTable
*map_object_path_to_es
; /* gchar* -> ExportedSubtree* */
445 GHashTable
*map_id_to_es
; /* guint -> ExportedSubtree* */
447 /* Map used for storing last used serials for each thread, protected by @lock */
448 GHashTable
*map_thread_to_last_serial
;
450 /* Structure used for message filters, protected by @lock */
453 /* Capabilities negotiated during authentication
454 * Read-only after initable_init(), so it may be read without holding a
455 * lock, if you check for initialization first.
457 GDBusCapabilityFlags capabilities
;
459 /* Protected by @init_lock */
460 GDBusAuthObserver
*authentication_observer
;
462 /* Read-only after initable_init(), so it may be read if you either
463 * hold @init_lock or check for initialization first.
465 GCredentials
*credentials
;
467 /* set to TRUE when finalizing */
471 typedef struct ExportedObject ExportedObject
;
472 static void exported_object_free (ExportedObject
*eo
);
474 typedef struct ExportedSubtree ExportedSubtree
;
475 static void exported_subtree_free (ExportedSubtree
*es
);
493 PROP_CAPABILITY_FLAGS
,
494 PROP_AUTHENTICATION_OBSERVER
,
497 static void distribute_signals (GDBusConnection
*connection
,
498 GDBusMessage
*message
);
500 static void distribute_method_call (GDBusConnection
*connection
,
501 GDBusMessage
*message
);
503 static gboolean
handle_generic_unlocked (GDBusConnection
*connection
,
504 GDBusMessage
*message
);
507 static void purge_all_signal_subscriptions (GDBusConnection
*connection
);
508 static void purge_all_filters (GDBusConnection
*connection
);
510 #define _G_ENSURE_LOCK(name) do { \
511 if (G_UNLIKELY (G_TRYLOCK(name))) \
513 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
514 "_G_ENSURE_LOCK: Lock `" #name "' is not locked"); \
518 static guint signals[LAST_SIGNAL] = { 0 };
520 static void initable_iface_init (GInitableIface
*initable_iface
);
521 static void async_initable_iface_init (GAsyncInitableIface
*async_initable_iface
);
523 G_DEFINE_TYPE_WITH_CODE (GDBusConnection
, g_dbus_connection
, G_TYPE_OBJECT
,
524 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE
, initable_iface_init
)
525 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE
, async_initable_iface_init
)
529 * Check that all members of @connection that can only be accessed after
530 * the connection is initialized can safely be accessed. If not,
531 * log a critical warning. This function is a memory barrier.
533 * Returns: %TRUE if initialized
536 check_initialized (GDBusConnection
*connection
)
538 /* The access to @atomic_flags isn't conditional, so that this function
539 * provides a memory barrier for thread-safety even if checks are disabled.
540 * (If you don't want this stricter guarantee, you can call
541 * g_return_if_fail (check_initialized (c)).)
543 * This isn't strictly necessary now that we've decided use of an
544 * uninitialized GDBusConnection is undefined behaviour, but it seems
545 * better to be as deterministic as is feasible.
547 * (Anything that could suffer a crash from seeing undefined values
548 * must have a race condition - thread A initializes the connection while
549 * thread B calls a method without initialization, hoping that thread A will
550 * win the race - so its behaviour is undefined anyway.)
552 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
554 g_return_val_if_fail (flags
& FLAG_INITIALIZED
, FALSE
);
556 /* We can safely access this, due to the memory barrier above */
557 g_return_val_if_fail (connection
->initialization_error
== NULL
, FALSE
);
563 MAY_BE_UNINITIALIZED
= (1<<1)
564 } CheckUnclosedFlags
;
567 * Check the same thing as check_initialized(), and also that the
568 * connection is not closed. If the connection is uninitialized,
569 * raise a critical warning (it's programmer error); if it's closed,
570 * raise a recoverable GError (it's a runtime error).
572 * This function is a memory barrier.
574 * Returns: %TRUE if initialized and not closed
577 check_unclosed (GDBusConnection
*connection
,
578 CheckUnclosedFlags check
,
581 /* check_initialized() is effectively inlined, so we don't waste time
582 * doing two memory barriers
584 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
586 if (!(check
& MAY_BE_UNINITIALIZED
))
588 g_return_val_if_fail (flags
& FLAG_INITIALIZED
, FALSE
);
589 g_return_val_if_fail (connection
->initialization_error
== NULL
, FALSE
);
592 if (flags
& FLAG_CLOSED
)
594 g_set_error_literal (error
,
597 _("The connection is closed"));
604 static GHashTable
*alive_connections
= NULL
;
607 g_dbus_connection_dispose (GObject
*object
)
609 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
611 G_LOCK (message_bus_lock
);
612 CONNECTION_LOCK (connection
);
613 if (connection
->worker
!= NULL
)
615 _g_dbus_worker_stop (connection
->worker
);
616 connection
->worker
= NULL
;
617 if (alive_connections
!= NULL
)
618 g_warn_if_fail (g_hash_table_remove (alive_connections
, connection
));
622 if (alive_connections
!= NULL
)
623 g_warn_if_fail (g_hash_table_lookup (alive_connections
, connection
) == NULL
);
625 CONNECTION_UNLOCK (connection
);
626 G_UNLOCK (message_bus_lock
);
628 if (G_OBJECT_CLASS (g_dbus_connection_parent_class
)->dispose
!= NULL
)
629 G_OBJECT_CLASS (g_dbus_connection_parent_class
)->dispose (object
);
633 g_dbus_connection_finalize (GObject
*object
)
635 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
637 connection
->finalizing
= TRUE
;
639 purge_all_signal_subscriptions (connection
);
641 purge_all_filters (connection
);
642 g_ptr_array_unref (connection
->filters
);
644 if (connection
->authentication_observer
!= NULL
)
645 g_object_unref (connection
->authentication_observer
);
647 if (connection
->auth
!= NULL
)
648 g_object_unref (connection
->auth
);
650 if (connection
->credentials
)
651 g_object_unref (connection
->credentials
);
653 if (connection
->stream
!= NULL
)
655 g_object_unref (connection
->stream
);
656 connection
->stream
= NULL
;
659 g_free (connection
->address
);
661 g_free (connection
->guid
);
662 g_free (connection
->bus_unique_name
);
664 if (connection
->initialization_error
!= NULL
)
665 g_error_free (connection
->initialization_error
);
667 g_hash_table_unref (connection
->map_method_serial_to_send_message_data
);
669 g_hash_table_unref (connection
->map_rule_to_signal_data
);
670 g_hash_table_unref (connection
->map_id_to_signal_data
);
671 g_hash_table_unref (connection
->map_sender_unique_name_to_signal_data_array
);
673 g_hash_table_unref (connection
->map_id_to_ei
);
674 g_hash_table_unref (connection
->map_object_path_to_eo
);
675 g_hash_table_unref (connection
->map_id_to_es
);
676 g_hash_table_unref (connection
->map_object_path_to_es
);
678 g_hash_table_unref (connection
->map_thread_to_last_serial
);
680 g_main_context_unref (connection
->main_context_at_construction
);
682 g_free (connection
->machine_id
);
684 g_mutex_clear (&connection
->init_lock
);
685 g_mutex_clear (&connection
->lock
);
687 G_OBJECT_CLASS (g_dbus_connection_parent_class
)->finalize (object
);
690 /* called in any user thread, with the connection's lock not held */
692 g_dbus_connection_get_property (GObject
*object
,
697 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
702 g_value_set_object (value
, g_dbus_connection_get_stream (connection
));
706 g_value_set_string (value
, g_dbus_connection_get_guid (connection
));
709 case PROP_UNIQUE_NAME
:
710 g_value_set_string (value
, g_dbus_connection_get_unique_name (connection
));
714 g_value_set_boolean (value
, g_dbus_connection_is_closed (connection
));
717 case PROP_EXIT_ON_CLOSE
:
718 g_value_set_boolean (value
, g_dbus_connection_get_exit_on_close (connection
));
721 case PROP_CAPABILITY_FLAGS
:
722 g_value_set_flags (value
, g_dbus_connection_get_capabilities (connection
));
726 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
731 /* called in any user thread, with the connection's lock not held */
733 g_dbus_connection_set_property (GObject
*object
,
738 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
743 connection
->stream
= g_value_dup_object (value
);
747 connection
->guid
= g_value_dup_string (value
);
751 connection
->address
= g_value_dup_string (value
);
755 connection
->flags
= g_value_get_flags (value
);
758 case PROP_EXIT_ON_CLOSE
:
759 g_dbus_connection_set_exit_on_close (connection
, g_value_get_boolean (value
));
762 case PROP_AUTHENTICATION_OBSERVER
:
763 connection
->authentication_observer
= g_value_dup_object (value
);
767 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
772 /* Base-class implementation of GDBusConnection::closed.
774 * Called in a user thread, by the main context that was thread-default when
775 * the object was constructed.
778 g_dbus_connection_real_closed (GDBusConnection
*connection
,
779 gboolean remote_peer_vanished
,
782 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
784 /* Because atomic int access is a memory barrier, we can safely read
785 * initialization_error without a lock, as long as we do it afterwards.
787 if (remote_peer_vanished
&&
788 (flags
& FLAG_EXIT_ON_CLOSE
) != 0 &&
789 (flags
& FLAG_INITIALIZED
) != 0 &&
790 connection
->initialization_error
== NULL
)
794 g_print ("%s: Remote peer vanished with error: %s (%s, %d). Exiting.\n",
797 g_quark_to_string (error
->domain
), error
->code
);
801 g_print ("%s: Remote peer vanished. Exiting.\n", G_STRFUNC
);
808 g_dbus_connection_class_init (GDBusConnectionClass
*klass
)
810 GObjectClass
*gobject_class
;
812 gobject_class
= G_OBJECT_CLASS (klass
);
814 gobject_class
->finalize
= g_dbus_connection_finalize
;
815 gobject_class
->dispose
= g_dbus_connection_dispose
;
816 gobject_class
->set_property
= g_dbus_connection_set_property
;
817 gobject_class
->get_property
= g_dbus_connection_get_property
;
819 klass
->closed
= g_dbus_connection_real_closed
;
822 * GDBusConnection:stream:
824 * The underlying #GIOStream used for I/O.
826 * If this is passed on construction and is a #GSocketConnection,
827 * then the corresponding #GSocket will be put into non-blocking mode.
829 * While the #GDBusConnection is active, it will interact with this
830 * stream from a worker thread, so it is not safe to interact with
831 * the stream directly.
835 g_object_class_install_property (gobject_class
,
837 g_param_spec_object ("stream",
839 P_("The underlying streams used for I/O"),
843 G_PARAM_CONSTRUCT_ONLY
|
844 G_PARAM_STATIC_NAME
|
845 G_PARAM_STATIC_BLURB
|
846 G_PARAM_STATIC_NICK
));
849 * GDBusConnection:address:
851 * A D-Bus address specifying potential endpoints that can be used
852 * when establishing the connection.
856 g_object_class_install_property (gobject_class
,
858 g_param_spec_string ("address",
860 P_("D-Bus address specifying potential socket endpoints"),
863 G_PARAM_CONSTRUCT_ONLY
|
864 G_PARAM_STATIC_NAME
|
865 G_PARAM_STATIC_BLURB
|
866 G_PARAM_STATIC_NICK
));
869 * GDBusConnection:flags:
871 * Flags from the #GDBusConnectionFlags enumeration.
875 g_object_class_install_property (gobject_class
,
877 g_param_spec_flags ("flags",
880 G_TYPE_DBUS_CONNECTION_FLAGS
,
881 G_DBUS_CONNECTION_FLAGS_NONE
,
883 G_PARAM_CONSTRUCT_ONLY
|
884 G_PARAM_STATIC_NAME
|
885 G_PARAM_STATIC_BLURB
|
886 G_PARAM_STATIC_NICK
));
889 * GDBusConnection:guid:
891 * The GUID of the peer performing the role of server when
894 * If you are constructing a #GDBusConnection and pass
895 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER in the
896 * #GDBusConnection:flags property then you MUST also set this
897 * property to a valid guid.
899 * If you are constructing a #GDBusConnection and pass
900 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT in the
901 * #GDBusConnection:flags property you will be able to read the GUID
902 * of the other peer here after the connection has been successfully
907 g_object_class_install_property (gobject_class
,
909 g_param_spec_string ("guid",
911 P_("GUID of the server peer"),
915 G_PARAM_CONSTRUCT_ONLY
|
916 G_PARAM_STATIC_NAME
|
917 G_PARAM_STATIC_BLURB
|
918 G_PARAM_STATIC_NICK
));
921 * GDBusConnection:unique-name:
923 * The unique name as assigned by the message bus or %NULL if the
924 * connection is not open or not a message bus connection.
928 g_object_class_install_property (gobject_class
,
930 g_param_spec_string ("unique-name",
932 P_("Unique name of bus connection"),
935 G_PARAM_STATIC_NAME
|
936 G_PARAM_STATIC_BLURB
|
937 G_PARAM_STATIC_NICK
));
940 * GDBusConnection:closed:
942 * A boolean specifying whether the connection has been closed.
946 g_object_class_install_property (gobject_class
,
948 g_param_spec_boolean ("closed",
950 P_("Whether the connection is closed"),
953 G_PARAM_STATIC_NAME
|
954 G_PARAM_STATIC_BLURB
|
955 G_PARAM_STATIC_NICK
));
958 * GDBusConnection:exit-on-close:
960 * A boolean specifying whether the process will be terminated (by
961 * calling <literal>raise(SIGTERM)</literal>) if the connection
962 * is closed by the remote peer.
964 * Note that #GDBusConnection objects returned by g_bus_get_finish() and
965 * g_bus_get_sync() will (usually) have this property set to %TRUE.
969 g_object_class_install_property (gobject_class
,
971 g_param_spec_boolean ("exit-on-close",
973 P_("Whether the process is terminated when the connection is closed"),
977 G_PARAM_STATIC_NAME
|
978 G_PARAM_STATIC_BLURB
|
979 G_PARAM_STATIC_NICK
));
982 * GDBusConnection:capabilities:
984 * Flags from the #GDBusCapabilityFlags enumeration
985 * representing connection features negotiated with the other peer.
989 g_object_class_install_property (gobject_class
,
990 PROP_CAPABILITY_FLAGS
,
991 g_param_spec_flags ("capabilities",
994 G_TYPE_DBUS_CAPABILITY_FLAGS
,
995 G_DBUS_CAPABILITY_FLAGS_NONE
,
997 G_PARAM_STATIC_NAME
|
998 G_PARAM_STATIC_BLURB
|
999 G_PARAM_STATIC_NICK
));
1002 * GDBusConnection:authentication-observer:
1004 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
1008 g_object_class_install_property (gobject_class
,
1009 PROP_AUTHENTICATION_OBSERVER
,
1010 g_param_spec_object ("authentication-observer",
1011 P_("Authentication Observer"),
1012 P_("Object used to assist in the authentication process"),
1013 G_TYPE_DBUS_AUTH_OBSERVER
,
1015 G_PARAM_CONSTRUCT_ONLY
|
1016 G_PARAM_STATIC_NAME
|
1017 G_PARAM_STATIC_BLURB
|
1018 G_PARAM_STATIC_NICK
));
1021 * GDBusConnection::closed:
1022 * @connection: The #GDBusConnection emitting the signal.
1023 * @remote_peer_vanished: %TRUE if @connection is closed because the
1024 * remote peer closed its end of the connection.
1025 * @error: (allow-none): A #GError with more details about the event or %NULL.
1027 * Emitted when the connection is closed.
1029 * The cause of this event can be
1032 * If g_dbus_connection_close() is called. In this case
1033 * @remote_peer_vanished is set to %FALSE and @error is %NULL.
1034 * </para></listitem>
1036 * If the remote peer closes the connection. In this case
1037 * @remote_peer_vanished is set to %TRUE and @error is set.
1038 * </para></listitem>
1040 * If the remote peer sends invalid or malformed data. In this
1041 * case @remote_peer_vanished is set to %FALSE and @error
1043 * </para></listitem>
1046 * Upon receiving this signal, you should give up your reference to
1047 * @connection. You are guaranteed that this signal is emitted only
1052 signals
[CLOSED_SIGNAL
] = g_signal_new ("closed",
1053 G_TYPE_DBUS_CONNECTION
,
1055 G_STRUCT_OFFSET (GDBusConnectionClass
, closed
),
1066 g_dbus_connection_init (GDBusConnection
*connection
)
1068 g_mutex_init (&connection
->lock
);
1069 g_mutex_init (&connection
->init_lock
);
1071 connection
->map_method_serial_to_send_message_data
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
1073 connection
->map_rule_to_signal_data
= g_hash_table_new (g_str_hash
,
1075 connection
->map_id_to_signal_data
= g_hash_table_new (g_direct_hash
,
1077 connection
->map_sender_unique_name_to_signal_data_array
= g_hash_table_new_full (g_str_hash
,
1080 (GDestroyNotify
) g_ptr_array_unref
);
1082 connection
->map_object_path_to_eo
= g_hash_table_new_full (g_str_hash
,
1085 (GDestroyNotify
) exported_object_free
);
1087 connection
->map_id_to_ei
= g_hash_table_new (g_direct_hash
,
1090 connection
->map_object_path_to_es
= g_hash_table_new_full (g_str_hash
,
1093 (GDestroyNotify
) exported_subtree_free
);
1095 connection
->map_id_to_es
= g_hash_table_new (g_direct_hash
,
1098 connection
->map_thread_to_last_serial
= g_hash_table_new (g_direct_hash
,
1101 connection
->main_context_at_construction
= g_main_context_ref_thread_default ();
1103 connection
->filters
= g_ptr_array_new ();
1107 * g_dbus_connection_get_stream:
1108 * @connection: a #GDBusConnection
1110 * Gets the underlying stream used for IO.
1112 * While the #GDBusConnection is active, it will interact with this
1113 * stream from a worker thread, so it is not safe to interact with
1114 * the stream directly.
1116 * Returns: (transfer none): the stream used for IO
1121 g_dbus_connection_get_stream (GDBusConnection
*connection
)
1123 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
1125 /* do not use g_return_val_if_fail(), we want the memory barrier */
1126 if (!check_initialized (connection
))
1129 return connection
->stream
;
1133 * g_dbus_connection_start_message_processing:
1134 * @connection: A #GDBusConnection.
1136 * If @connection was created with
1137 * %G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, this method
1138 * starts processing messages. Does nothing on if @connection wasn't
1139 * created with this flag or if the method has already been called.
1144 g_dbus_connection_start_message_processing (GDBusConnection
*connection
)
1146 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1148 /* do not use g_return_val_if_fail(), we want the memory barrier */
1149 if (!check_initialized (connection
))
1152 g_assert (connection
->worker
!= NULL
);
1153 _g_dbus_worker_unfreeze (connection
->worker
);
1157 * g_dbus_connection_is_closed:
1158 * @connection: A #GDBusConnection.
1160 * Gets whether @connection is closed.
1162 * Returns: %TRUE if the connection is closed, %FALSE otherwise.
1167 g_dbus_connection_is_closed (GDBusConnection
*connection
)
1171 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1173 flags
= g_atomic_int_get (&connection
->atomic_flags
);
1175 return (flags
& FLAG_CLOSED
) ? TRUE
: FALSE
;
1179 * g_dbus_connection_get_capabilities:
1180 * @connection: A #GDBusConnection.
1182 * Gets the capabilities negotiated with the remote peer
1184 * Returns: Zero or more flags from the #GDBusCapabilityFlags enumeration.
1188 GDBusCapabilityFlags
1189 g_dbus_connection_get_capabilities (GDBusConnection
*connection
)
1191 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), G_DBUS_CAPABILITY_FLAGS_NONE
);
1193 /* do not use g_return_val_if_fail(), we want the memory barrier */
1194 if (!check_initialized (connection
))
1195 return G_DBUS_CAPABILITY_FLAGS_NONE
;
1197 return connection
->capabilities
;
1200 /* ---------------------------------------------------------------------------------------------------- */
1202 /* Called in a temporary thread without holding locks. */
1204 flush_in_thread_func (GSimpleAsyncResult
*res
,
1206 GCancellable
*cancellable
)
1211 if (!g_dbus_connection_flush_sync (G_DBUS_CONNECTION (object
),
1214 g_simple_async_result_take_error (res
, error
);
1218 * g_dbus_connection_flush:
1219 * @connection: A #GDBusConnection.
1220 * @cancellable: (allow-none): A #GCancellable or %NULL.
1221 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
1222 * satisfied or %NULL if you don't care about the result.
1223 * @user_data: The data to pass to @callback.
1225 * Asynchronously flushes @connection, that is, writes all queued
1226 * outgoing message to the transport and then flushes the transport
1227 * (using g_output_stream_flush_async()). This is useful in programs
1228 * that wants to emit a D-Bus signal and then exit
1229 * immediately. Without flushing the connection, there is no guarantee
1230 * that the message has been sent to the networking buffers in the OS
1233 * This is an asynchronous method. When the operation is finished,
1234 * @callback will be invoked in the <link
1235 * linkend="g-main-context-push-thread-default">thread-default main
1236 * loop</link> of the thread you are calling this method from. You can
1237 * then call g_dbus_connection_flush_finish() to get the result of the
1238 * operation. See g_dbus_connection_flush_sync() for the synchronous
1244 g_dbus_connection_flush (GDBusConnection
*connection
,
1245 GCancellable
*cancellable
,
1246 GAsyncReadyCallback callback
,
1249 GSimpleAsyncResult
*simple
;
1251 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1253 simple
= g_simple_async_result_new (G_OBJECT (connection
),
1256 g_dbus_connection_flush
);
1257 g_simple_async_result_set_check_cancellable (simple
, cancellable
);
1258 g_simple_async_result_run_in_thread (simple
,
1259 flush_in_thread_func
,
1262 g_object_unref (simple
);
1266 * g_dbus_connection_flush_finish:
1267 * @connection: A #GDBusConnection.
1268 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_flush().
1269 * @error: Return location for error or %NULL.
1271 * Finishes an operation started with g_dbus_connection_flush().
1273 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1278 g_dbus_connection_flush_finish (GDBusConnection
*connection
,
1282 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (res
);
1287 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1288 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), FALSE
);
1289 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1291 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_dbus_connection_flush
);
1293 if (g_simple_async_result_propagate_error (simple
, error
))
1303 * g_dbus_connection_flush_sync:
1304 * @connection: A #GDBusConnection.
1305 * @cancellable: (allow-none): A #GCancellable or %NULL.
1306 * @error: Return location for error or %NULL.
1308 * Synchronously flushes @connection. The calling thread is blocked
1309 * until this is done. See g_dbus_connection_flush() for the
1310 * asynchronous version of this method and more details about what it
1313 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1318 g_dbus_connection_flush_sync (GDBusConnection
*connection
,
1319 GCancellable
*cancellable
,
1324 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1325 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1329 /* This is only a best-effort attempt to see whether the connection is
1330 * closed, so it doesn't need the lock. If the connection closes just
1331 * after this check, but before scheduling the flush operation, the
1332 * result will be more or less the same as if the connection closed while
1333 * the flush operation was pending - it'll fail with either CLOSED or
1336 if (!check_unclosed (connection
, 0, error
))
1339 g_assert (connection
->worker
!= NULL
);
1341 ret
= _g_dbus_worker_flush_sync (connection
->worker
,
1349 /* ---------------------------------------------------------------------------------------------------- */
1353 GDBusConnection
*connection
;
1355 gboolean remote_peer_vanished
;
1359 emit_closed_data_free (EmitClosedData
*data
)
1361 g_object_unref (data
->connection
);
1362 if (data
->error
!= NULL
)
1363 g_error_free (data
->error
);
1367 /* Called in a user thread that has acquired the main context that was
1368 * thread-default when the object was constructed
1371 emit_closed_in_idle (gpointer user_data
)
1373 EmitClosedData
*data
= user_data
;
1376 g_object_notify (G_OBJECT (data
->connection
), "closed");
1377 g_signal_emit (data
->connection
,
1378 signals
[CLOSED_SIGNAL
],
1380 data
->remote_peer_vanished
,
1386 /* Can be called from any thread, must hold lock.
1387 * FLAG_CLOSED must already have been set.
1390 schedule_closed_unlocked (GDBusConnection
*connection
,
1391 gboolean remote_peer_vanished
,
1394 GSource
*idle_source
;
1395 EmitClosedData
*data
;
1397 CONNECTION_ENSURE_LOCK (connection
);
1399 data
= g_new0 (EmitClosedData
, 1);
1400 data
->connection
= g_object_ref (connection
);
1401 data
->remote_peer_vanished
= remote_peer_vanished
;
1402 data
->error
= error
!= NULL
? g_error_copy (error
) : NULL
;
1404 idle_source
= g_idle_source_new ();
1405 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
1406 g_source_set_callback (idle_source
,
1407 emit_closed_in_idle
,
1409 (GDestroyNotify
) emit_closed_data_free
);
1410 g_source_attach (idle_source
, connection
->main_context_at_construction
);
1411 g_source_unref (idle_source
);
1414 /* ---------------------------------------------------------------------------------------------------- */
1417 * g_dbus_connection_close:
1418 * @connection: A #GDBusConnection.
1419 * @cancellable: (allow-none): A #GCancellable or %NULL.
1420 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
1421 * satisfied or %NULL if you don't care about the result.
1422 * @user_data: The data to pass to @callback.
1424 * Closes @connection. Note that this never causes the process to
1425 * exit (this might only happen if the other end of a shared message
1426 * bus connection disconnects, see #GDBusConnection:exit-on-close).
1428 * Once the connection is closed, operations such as sending a message
1429 * will return with the error %G_IO_ERROR_CLOSED. Closing a connection
1430 * will not automatically flush the connection so queued messages may
1431 * be lost. Use g_dbus_connection_flush() if you need such guarantees.
1433 * If @connection is already closed, this method fails with
1434 * %G_IO_ERROR_CLOSED.
1436 * When @connection has been closed, the #GDBusConnection::closed
1437 * signal is emitted in the <link
1438 * linkend="g-main-context-push-thread-default">thread-default main
1439 * loop</link> of the thread that @connection was constructed in.
1441 * This is an asynchronous method. When the operation is finished,
1442 * @callback will be invoked in the <link
1443 * linkend="g-main-context-push-thread-default">thread-default main
1444 * loop</link> of the thread you are calling this method from. You can
1445 * then call g_dbus_connection_close_finish() to get the result of the
1446 * operation. See g_dbus_connection_close_sync() for the synchronous
1452 g_dbus_connection_close (GDBusConnection
*connection
,
1453 GCancellable
*cancellable
,
1454 GAsyncReadyCallback callback
,
1457 GSimpleAsyncResult
*simple
;
1459 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1461 /* do not use g_return_val_if_fail(), we want the memory barrier */
1462 if (!check_initialized (connection
))
1465 g_assert (connection
->worker
!= NULL
);
1467 simple
= g_simple_async_result_new (G_OBJECT (connection
),
1470 g_dbus_connection_close
);
1471 g_simple_async_result_set_check_cancellable (simple
, cancellable
);
1472 _g_dbus_worker_close (connection
->worker
, cancellable
, simple
);
1473 g_object_unref (simple
);
1477 * g_dbus_connection_close_finish:
1478 * @connection: A #GDBusConnection.
1479 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_close().
1480 * @error: Return location for error or %NULL.
1482 * Finishes an operation started with g_dbus_connection_close().
1484 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1489 g_dbus_connection_close_finish (GDBusConnection
*connection
,
1493 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (res
);
1498 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1499 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), FALSE
);
1500 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1502 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_dbus_connection_close
);
1504 if (g_simple_async_result_propagate_error (simple
, error
))
1515 GAsyncResult
*result
;
1518 /* Can be called by any thread, without the connection lock */
1520 sync_close_cb (GObject
*source_object
,
1524 SyncCloseData
*data
= user_data
;
1526 data
->result
= g_object_ref (res
);
1527 g_main_loop_quit (data
->loop
);
1531 * g_dbus_connection_close_sync:
1532 * @connection: A #GDBusConnection.
1533 * @cancellable: (allow-none): A #GCancellable or %NULL.
1534 * @error: Return location for error or %NULL.
1536 * Synchronously closees @connection. The calling thread is blocked
1537 * until this is done. See g_dbus_connection_close() for the
1538 * asynchronous version of this method and more details about what it
1541 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1546 g_dbus_connection_close_sync (GDBusConnection
*connection
,
1547 GCancellable
*cancellable
,
1552 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1553 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1557 if (check_unclosed (connection
, 0, error
))
1559 GMainContext
*context
;
1562 context
= g_main_context_new ();
1563 g_main_context_push_thread_default (context
);
1564 data
.loop
= g_main_loop_new (context
, TRUE
);
1567 g_dbus_connection_close (connection
, cancellable
, sync_close_cb
, &data
);
1568 g_main_loop_run (data
.loop
);
1569 ret
= g_dbus_connection_close_finish (connection
, data
.result
, error
);
1571 g_object_unref (data
.result
);
1572 g_main_loop_unref (data
.loop
);
1573 g_main_context_pop_thread_default (context
);
1574 g_main_context_unref (context
);
1580 /* ---------------------------------------------------------------------------------------------------- */
1583 * g_dbus_connection_get_last_serial:
1584 * @connection: A #GDBusConnection.
1586 * Retrieves the last serial number assigned to a #GDBusMessage on
1587 * the current thread. This includes messages sent via both low-level
1588 * API such as g_dbus_connection_send_message() as well as
1589 * high-level API such as g_dbus_connection_emit_signal(),
1590 * g_dbus_connection_call() or g_dbus_proxy_call().
1592 * Returns: the last used serial or zero when no message has been sent
1593 * within the current thread.
1598 g_dbus_connection_get_last_serial (GDBusConnection
*connection
)
1602 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
1604 CONNECTION_LOCK (connection
);
1605 ret
= GPOINTER_TO_UINT (g_hash_table_lookup (connection
->map_thread_to_last_serial
,
1607 CONNECTION_UNLOCK (connection
);
1612 /* ---------------------------------------------------------------------------------------------------- */
1614 /* Can be called by any thread, with the connection lock held */
1616 g_dbus_connection_send_message_unlocked (GDBusConnection
*connection
,
1617 GDBusMessage
*message
,
1618 GDBusSendMessageFlags flags
,
1619 volatile guint32
*out_serial
,
1624 guint32 serial_to_use
;
1627 CONNECTION_ENSURE_LOCK (connection
);
1629 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1630 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
1632 /* TODO: check all necessary headers are present */
1637 if (out_serial
!= NULL
)
1640 /* If we're in initable_init(), don't check for being initialized, to avoid
1641 * chicken-and-egg problems. initable_init() is responsible for setting up
1642 * our prerequisites (mainly connection->worker), and only calling us
1643 * from its own thread (so no memory barrier is needed).
1645 if (!check_unclosed (connection
,
1646 (flags
& SEND_MESSAGE_FLAGS_INITIALIZING
) ? MAY_BE_UNINITIALIZED
: 0,
1650 blob
= g_dbus_message_to_blob (message
,
1652 connection
->capabilities
,
1657 if (flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
)
1658 serial_to_use
= g_dbus_message_get_serial (message
);
1660 serial_to_use
= ++connection
->last_serial
; /* TODO: handle overflow */
1665 ((guint32
*) blob
)[2] = GUINT32_TO_LE (serial_to_use
);
1668 ((guint32
*) blob
)[2] = GUINT32_TO_BE (serial_to_use
);
1671 g_assert_not_reached ();
1676 g_printerr ("Writing message of %" G_GSIZE_FORMAT
" bytes (serial %d) on %p:\n",
1677 blob_size
, serial_to_use
, connection
);
1678 g_printerr ("----\n");
1679 hexdump (blob
, blob_size
);
1680 g_printerr ("----\n");
1683 /* TODO: use connection->auth to encode the blob */
1685 if (out_serial
!= NULL
)
1686 *out_serial
= serial_to_use
;
1688 /* store used serial for the current thread */
1689 /* TODO: watch the thread disposal and remove associated record
1691 * - see https://bugzilla.gnome.org/show_bug.cgi?id=676825#c7
1693 g_hash_table_replace (connection
->map_thread_to_last_serial
,
1695 GUINT_TO_POINTER (serial_to_use
));
1697 if (!(flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
))
1698 g_dbus_message_set_serial (message
, serial_to_use
);
1700 g_dbus_message_lock (message
);
1701 _g_dbus_worker_send_message (connection
->worker
,
1705 blob
= NULL
; /* since _g_dbus_worker_send_message() steals the blob */
1716 * g_dbus_connection_send_message:
1717 * @connection: A #GDBusConnection.
1718 * @message: A #GDBusMessage
1719 * @flags: Flags affecting how the message is sent.
1720 * @out_serial: (out) (allow-none): Return location for serial number assigned
1721 * to @message when sending it or %NULL.
1722 * @error: Return location for error or %NULL.
1724 * Asynchronously sends @message to the peer represented by @connection.
1726 * Unless @flags contain the
1727 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1728 * will be assigned by @connection and set on @message via
1729 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1730 * serial number used will be written to this location prior to
1731 * submitting the message to the underlying transport.
1733 * If @connection is closed then the operation will fail with
1734 * %G_IO_ERROR_CLOSED. If @message is not well-formed,
1735 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1737 * See <xref linkend="gdbus-server"/> and <xref
1738 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1739 * low-level API to send and receive UNIX file descriptors.
1741 * Note that @message must be unlocked, unless @flags contain the
1742 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1744 * Returns: %TRUE if the message was well-formed and queued for
1745 * transmission, %FALSE if @error is set.
1750 g_dbus_connection_send_message (GDBusConnection
*connection
,
1751 GDBusMessage
*message
,
1752 GDBusSendMessageFlags flags
,
1753 volatile guint32
*out_serial
,
1758 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1759 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
1760 g_return_val_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
), FALSE
);
1761 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1763 CONNECTION_LOCK (connection
);
1764 ret
= g_dbus_connection_send_message_unlocked (connection
, message
, flags
, out_serial
, error
);
1765 CONNECTION_UNLOCK (connection
);
1769 /* ---------------------------------------------------------------------------------------------------- */
1773 volatile gint ref_count
;
1774 GDBusConnection
*connection
;
1776 GSimpleAsyncResult
*simple
;
1778 GMainContext
*main_context
;
1780 GCancellable
*cancellable
;
1782 gulong cancellable_handler_id
;
1784 GSource
*timeout_source
;
1789 /* Can be called from any thread with or without lock held */
1790 static SendMessageData
*
1791 send_message_data_ref (SendMessageData
*data
)
1793 g_atomic_int_inc (&data
->ref_count
);
1797 /* Can be called from any thread with or without lock held */
1799 send_message_data_unref (SendMessageData
*data
)
1801 if (g_atomic_int_dec_and_test (&data
->ref_count
))
1803 g_assert (data
->timeout_source
== NULL
);
1804 g_assert (data
->simple
== NULL
);
1805 g_assert (data
->cancellable_handler_id
== 0);
1806 g_object_unref (data
->connection
);
1807 if (data
->cancellable
!= NULL
)
1808 g_object_unref (data
->cancellable
);
1809 g_main_context_unref (data
->main_context
);
1814 /* ---------------------------------------------------------------------------------------------------- */
1816 /* can be called from any thread with lock held - caller must have prepared GSimpleAsyncResult already */
1818 send_message_with_reply_deliver (SendMessageData
*data
, gboolean remove
)
1820 CONNECTION_ENSURE_LOCK (data
->connection
);
1822 g_assert (!data
->delivered
);
1824 data
->delivered
= TRUE
;
1826 g_simple_async_result_complete_in_idle (data
->simple
);
1827 g_object_unref (data
->simple
);
1828 data
->simple
= NULL
;
1830 if (data
->timeout_source
!= NULL
)
1832 g_source_destroy (data
->timeout_source
);
1833 data
->timeout_source
= NULL
;
1835 if (data
->cancellable_handler_id
> 0)
1837 g_cancellable_disconnect (data
->cancellable
, data
->cancellable_handler_id
);
1838 data
->cancellable_handler_id
= 0;
1843 g_warn_if_fail (g_hash_table_remove (data
->connection
->map_method_serial_to_send_message_data
,
1844 GUINT_TO_POINTER (data
->serial
)));
1847 send_message_data_unref (data
);
1850 /* ---------------------------------------------------------------------------------------------------- */
1852 /* Can be called from any thread with lock held */
1854 send_message_data_deliver_reply_unlocked (SendMessageData
*data
,
1855 GDBusMessage
*reply
)
1857 if (data
->delivered
)
1860 g_simple_async_result_set_op_res_gpointer (data
->simple
,
1861 g_object_ref (reply
),
1864 send_message_with_reply_deliver (data
, TRUE
);
1870 /* ---------------------------------------------------------------------------------------------------- */
1872 /* Called from a user thread, lock is not held */
1874 send_message_with_reply_cancelled_idle_cb (gpointer user_data
)
1876 SendMessageData
*data
= user_data
;
1878 CONNECTION_LOCK (data
->connection
);
1879 if (data
->delivered
)
1882 g_simple_async_result_set_error (data
->simple
,
1884 G_IO_ERROR_CANCELLED
,
1885 _("Operation was cancelled"));
1887 send_message_with_reply_deliver (data
, TRUE
);
1890 CONNECTION_UNLOCK (data
->connection
);
1894 /* Can be called from any thread with or without lock held */
1896 send_message_with_reply_cancelled_cb (GCancellable
*cancellable
,
1899 SendMessageData
*data
= user_data
;
1900 GSource
*idle_source
;
1902 /* postpone cancellation to idle handler since we may be called directly
1903 * via g_cancellable_connect() (e.g. holding lock)
1905 idle_source
= g_idle_source_new ();
1906 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
1907 g_source_set_callback (idle_source
,
1908 send_message_with_reply_cancelled_idle_cb
,
1909 send_message_data_ref (data
),
1910 (GDestroyNotify
) send_message_data_unref
);
1911 g_source_attach (idle_source
, data
->main_context
);
1912 g_source_unref (idle_source
);
1915 /* ---------------------------------------------------------------------------------------------------- */
1917 /* Called from a user thread, lock is not held */
1919 send_message_with_reply_timeout_cb (gpointer user_data
)
1921 SendMessageData
*data
= user_data
;
1923 CONNECTION_LOCK (data
->connection
);
1924 if (data
->delivered
)
1927 g_simple_async_result_set_error (data
->simple
,
1929 G_IO_ERROR_TIMED_OUT
,
1930 _("Timeout was reached"));
1932 send_message_with_reply_deliver (data
, TRUE
);
1935 CONNECTION_UNLOCK (data
->connection
);
1940 /* ---------------------------------------------------------------------------------------------------- */
1942 /* Called from a user thread, connection's lock is held */
1944 g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection
*connection
,
1945 GDBusMessage
*message
,
1946 GDBusSendMessageFlags flags
,
1948 volatile guint32
*out_serial
,
1949 GCancellable
*cancellable
,
1950 GAsyncReadyCallback callback
,
1953 GSimpleAsyncResult
*simple
;
1954 SendMessageData
*data
;
1956 volatile guint32 serial
;
1960 if (out_serial
== NULL
)
1961 out_serial
= &serial
;
1963 if (timeout_msec
== -1)
1964 timeout_msec
= 25 * 1000;
1966 simple
= g_simple_async_result_new (G_OBJECT (connection
),
1969 g_dbus_connection_send_message_with_reply
);
1970 g_simple_async_result_set_check_cancellable (simple
, cancellable
);
1972 if (g_cancellable_is_cancelled (cancellable
))
1974 g_simple_async_result_set_error (simple
,
1976 G_IO_ERROR_CANCELLED
,
1977 _("Operation was cancelled"));
1978 g_simple_async_result_complete_in_idle (simple
);
1979 g_object_unref (simple
);
1984 if (!g_dbus_connection_send_message_unlocked (connection
, message
, flags
, out_serial
, &error
))
1986 g_simple_async_result_take_error (simple
, error
);
1987 g_simple_async_result_complete_in_idle (simple
);
1988 g_object_unref (simple
);
1992 data
= g_new0 (SendMessageData
, 1);
1993 data
->ref_count
= 1;
1994 data
->connection
= g_object_ref (connection
);
1995 data
->simple
= simple
;
1996 data
->serial
= *out_serial
;
1997 data
->main_context
= g_main_context_ref_thread_default ();
1999 if (cancellable
!= NULL
)
2001 data
->cancellable
= g_object_ref (cancellable
);
2002 data
->cancellable_handler_id
= g_cancellable_connect (cancellable
,
2003 G_CALLBACK (send_message_with_reply_cancelled_cb
),
2004 send_message_data_ref (data
),
2005 (GDestroyNotify
) send_message_data_unref
);
2008 if (timeout_msec
!= G_MAXINT
)
2010 data
->timeout_source
= g_timeout_source_new (timeout_msec
);
2011 g_source_set_priority (data
->timeout_source
, G_PRIORITY_DEFAULT
);
2012 g_source_set_callback (data
->timeout_source
,
2013 send_message_with_reply_timeout_cb
,
2014 send_message_data_ref (data
),
2015 (GDestroyNotify
) send_message_data_unref
);
2016 g_source_attach (data
->timeout_source
, data
->main_context
);
2017 g_source_unref (data
->timeout_source
);
2020 g_hash_table_insert (connection
->map_method_serial_to_send_message_data
,
2021 GUINT_TO_POINTER (*out_serial
),
2029 * g_dbus_connection_send_message_with_reply:
2030 * @connection: A #GDBusConnection.
2031 * @message: A #GDBusMessage.
2032 * @flags: Flags affecting how the message is sent.
2033 * @timeout_msec: The timeout in milliseconds, -1 to use the default
2034 * timeout or %G_MAXINT for no timeout.
2035 * @out_serial: (out) (allow-none): Return location for serial number assigned
2036 * to @message when sending it or %NULL.
2037 * @cancellable: (allow-none): A #GCancellable or %NULL.
2038 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
2039 * satisfied or %NULL if you don't care about the result.
2040 * @user_data: The data to pass to @callback.
2042 * Asynchronously sends @message to the peer represented by @connection.
2044 * Unless @flags contain the
2045 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
2046 * will be assigned by @connection and set on @message via
2047 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
2048 * serial number used will be written to this location prior to
2049 * submitting the message to the underlying transport.
2051 * If @connection is closed then the operation will fail with
2052 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
2053 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
2054 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
2056 * This is an asynchronous method. When the operation is finished, @callback will be invoked
2057 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2058 * of the thread you are calling this method from. You can then call
2059 * g_dbus_connection_send_message_with_reply_finish() to get the result of the operation.
2060 * See g_dbus_connection_send_message_with_reply_sync() for the synchronous version.
2062 * Note that @message must be unlocked, unless @flags contain the
2063 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
2065 * See <xref linkend="gdbus-server"/> and <xref
2066 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
2067 * low-level API to send and receive UNIX file descriptors.
2072 g_dbus_connection_send_message_with_reply (GDBusConnection
*connection
,
2073 GDBusMessage
*message
,
2074 GDBusSendMessageFlags flags
,
2076 volatile guint32
*out_serial
,
2077 GCancellable
*cancellable
,
2078 GAsyncReadyCallback callback
,
2081 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
2082 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2083 g_return_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
));
2084 g_return_if_fail (timeout_msec
>= 0 || timeout_msec
== -1);
2086 CONNECTION_LOCK (connection
);
2087 g_dbus_connection_send_message_with_reply_unlocked (connection
,
2095 CONNECTION_UNLOCK (connection
);
2099 * g_dbus_connection_send_message_with_reply_finish:
2100 * @connection: a #GDBusConnection
2101 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_send_message_with_reply().
2102 * @error: Return location for error or %NULL.
2104 * Finishes an operation started with g_dbus_connection_send_message_with_reply().
2106 * Note that @error is only set if a local in-process error
2107 * occurred. That is to say that the returned #GDBusMessage object may
2108 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
2109 * g_dbus_message_to_gerror() to transcode this to a #GError.
2111 * See <xref linkend="gdbus-server"/> and <xref
2112 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
2113 * low-level API to send and receive UNIX file descriptors.
2115 * Returns: (transfer full): A locked #GDBusMessage or %NULL if @error is set.
2120 g_dbus_connection_send_message_with_reply_finish (GDBusConnection
*connection
,
2124 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (res
);
2125 GDBusMessage
*reply
;
2127 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
2128 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2132 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_dbus_connection_send_message_with_reply
);
2134 if (g_simple_async_result_propagate_error (simple
, error
))
2137 reply
= g_object_ref (g_simple_async_result_get_op_res_gpointer (simple
));
2143 /* ---------------------------------------------------------------------------------------------------- */
2148 GMainContext
*context
;
2150 } SendMessageSyncData
;
2152 /* Called from a user thread, lock is not held */
2154 send_message_with_reply_sync_cb (GDBusConnection
*connection
,
2158 SendMessageSyncData
*data
= user_data
;
2159 data
->res
= g_object_ref (res
);
2160 g_main_loop_quit (data
->loop
);
2164 * g_dbus_connection_send_message_with_reply_sync:
2165 * @connection: A #GDBusConnection.
2166 * @message: A #GDBusMessage.
2167 * @flags: Flags affecting how the message is sent.
2168 * @timeout_msec: The timeout in milliseconds, -1 to use the default
2169 * timeout or %G_MAXINT for no timeout.
2170 * @out_serial: (out) (allow-none): Return location for serial number assigned
2171 * to @message when sending it or %NULL.
2172 * @cancellable: (allow-none): A #GCancellable or %NULL.
2173 * @error: Return location for error or %NULL.
2175 * Synchronously sends @message to the peer represented by @connection
2176 * and blocks the calling thread until a reply is received or the
2177 * timeout is reached. See g_dbus_connection_send_message_with_reply()
2178 * for the asynchronous version of this method.
2180 * Unless @flags contain the
2181 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
2182 * will be assigned by @connection and set on @message via
2183 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
2184 * serial number used will be written to this location prior to
2185 * submitting the message to the underlying transport.
2187 * If @connection is closed then the operation will fail with
2188 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
2189 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
2190 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
2192 * Note that @error is only set if a local in-process error
2193 * occurred. That is to say that the returned #GDBusMessage object may
2194 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
2195 * g_dbus_message_to_gerror() to transcode this to a #GError.
2197 * See <xref linkend="gdbus-server"/> and <xref
2198 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
2199 * low-level API to send and receive UNIX file descriptors.
2201 * Note that @message must be unlocked, unless @flags contain the
2202 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
2204 * Returns: (transfer full): A locked #GDBusMessage that is the reply to @message or %NULL if @error is set.
2209 g_dbus_connection_send_message_with_reply_sync (GDBusConnection
*connection
,
2210 GDBusMessage
*message
,
2211 GDBusSendMessageFlags flags
,
2213 volatile guint32
*out_serial
,
2214 GCancellable
*cancellable
,
2217 SendMessageSyncData
*data
;
2218 GDBusMessage
*reply
;
2220 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
2221 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2222 g_return_val_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
), NULL
);
2223 g_return_val_if_fail (timeout_msec
>= 0 || timeout_msec
== -1, NULL
);
2224 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2226 data
= g_new0 (SendMessageSyncData
, 1);
2227 data
->context
= g_main_context_new ();
2228 data
->loop
= g_main_loop_new (data
->context
, FALSE
);
2230 g_main_context_push_thread_default (data
->context
);
2232 g_dbus_connection_send_message_with_reply (connection
,
2238 (GAsyncReadyCallback
) send_message_with_reply_sync_cb
,
2240 g_main_loop_run (data
->loop
);
2241 reply
= g_dbus_connection_send_message_with_reply_finish (connection
,
2245 g_main_context_pop_thread_default (data
->context
);
2247 g_main_context_unref (data
->context
);
2248 g_main_loop_unref (data
->loop
);
2249 g_object_unref (data
->res
);
2255 /* ---------------------------------------------------------------------------------------------------- */
2259 GDBusMessageFilterFunction func
;
2266 GDBusMessageFilterFunction filter_function
;
2268 GDestroyNotify user_data_free_func
;
2271 /* Called in GDBusWorker's thread - we must not block - with no lock held */
2273 on_worker_message_received (GDBusWorker
*worker
,
2274 GDBusMessage
*message
,
2277 GDBusConnection
*connection
;
2278 FilterCallback
*filters
;
2283 G_LOCK (message_bus_lock
);
2284 alive
= (g_hash_table_lookup (alive_connections
, user_data
) != NULL
);
2287 G_UNLOCK (message_bus_lock
);
2290 connection
= G_DBUS_CONNECTION (user_data
);
2291 g_object_ref (connection
);
2292 G_UNLOCK (message_bus_lock
);
2294 //g_debug ("in on_worker_message_received");
2296 g_object_ref (message
);
2297 g_dbus_message_lock (message
);
2299 //g_debug ("boo ref_count = %d %p %p", G_OBJECT (connection)->ref_count, connection, connection->worker);
2301 /* First collect the set of callback functions */
2302 CONNECTION_LOCK (connection
);
2303 num_filters
= connection
->filters
->len
;
2304 filters
= g_new0 (FilterCallback
, num_filters
);
2305 for (n
= 0; n
< num_filters
; n
++)
2307 FilterData
*data
= connection
->filters
->pdata
[n
];
2308 filters
[n
].func
= data
->filter_function
;
2309 filters
[n
].user_data
= data
->user_data
;
2311 CONNECTION_UNLOCK (connection
);
2313 /* then call the filters in order (without holding the lock) */
2314 for (n
= 0; n
< num_filters
; n
++)
2316 message
= filters
[n
].func (connection
,
2319 filters
[n
].user_data
);
2320 if (message
== NULL
)
2322 g_dbus_message_lock (message
);
2325 /* Standard dispatch unless the filter ate the message - no need to
2326 * do anything if the message was altered
2328 if (message
!= NULL
)
2330 GDBusMessageType message_type
;
2332 message_type
= g_dbus_message_get_message_type (message
);
2333 if (message_type
== G_DBUS_MESSAGE_TYPE_METHOD_RETURN
|| message_type
== G_DBUS_MESSAGE_TYPE_ERROR
)
2335 guint32 reply_serial
;
2336 SendMessageData
*send_message_data
;
2338 reply_serial
= g_dbus_message_get_reply_serial (message
);
2339 CONNECTION_LOCK (connection
);
2340 send_message_data
= g_hash_table_lookup (connection
->map_method_serial_to_send_message_data
,
2341 GUINT_TO_POINTER (reply_serial
));
2342 if (send_message_data
!= NULL
)
2344 //g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection);
2345 send_message_data_deliver_reply_unlocked (send_message_data
, message
);
2349 //g_debug ("message reply/error for serial %d but no SendMessageData found for %p", reply_serial, connection);
2351 CONNECTION_UNLOCK (connection
);
2353 else if (message_type
== G_DBUS_MESSAGE_TYPE_SIGNAL
)
2355 CONNECTION_LOCK (connection
);
2356 distribute_signals (connection
, message
);
2357 CONNECTION_UNLOCK (connection
);
2359 else if (message_type
== G_DBUS_MESSAGE_TYPE_METHOD_CALL
)
2361 CONNECTION_LOCK (connection
);
2362 distribute_method_call (connection
, message
);
2363 CONNECTION_UNLOCK (connection
);
2367 if (message
!= NULL
)
2368 g_object_unref (message
);
2369 g_object_unref (connection
);
2373 /* Called in GDBusWorker's thread, lock is not held */
2374 static GDBusMessage
*
2375 on_worker_message_about_to_be_sent (GDBusWorker
*worker
,
2376 GDBusMessage
*message
,
2379 GDBusConnection
*connection
;
2380 FilterCallback
*filters
;
2385 G_LOCK (message_bus_lock
);
2386 alive
= (g_hash_table_lookup (alive_connections
, user_data
) != NULL
);
2389 G_UNLOCK (message_bus_lock
);
2392 connection
= G_DBUS_CONNECTION (user_data
);
2393 g_object_ref (connection
);
2394 G_UNLOCK (message_bus_lock
);
2396 //g_debug ("in on_worker_message_about_to_be_sent");
2398 /* First collect the set of callback functions */
2399 CONNECTION_LOCK (connection
);
2400 num_filters
= connection
->filters
->len
;
2401 filters
= g_new0 (FilterCallback
, num_filters
);
2402 for (n
= 0; n
< num_filters
; n
++)
2404 FilterData
*data
= connection
->filters
->pdata
[n
];
2405 filters
[n
].func
= data
->filter_function
;
2406 filters
[n
].user_data
= data
->user_data
;
2408 CONNECTION_UNLOCK (connection
);
2410 /* then call the filters in order (without holding the lock) */
2411 for (n
= 0; n
< num_filters
; n
++)
2413 g_dbus_message_lock (message
);
2414 message
= filters
[n
].func (connection
,
2417 filters
[n
].user_data
);
2418 if (message
== NULL
)
2422 g_object_unref (connection
);
2428 /* called with connection lock held, in GDBusWorker thread */
2430 cancel_method_on_close (gpointer key
, gpointer value
, gpointer user_data
)
2432 SendMessageData
*data
= value
;
2434 if (data
->delivered
)
2437 g_simple_async_result_set_error (data
->simple
,
2440 _("The connection is closed"));
2442 /* Ask send_message_with_reply_deliver not to remove the element from the
2443 * hash table - we're in the middle of a foreach; that would be unsafe.
2444 * Instead, return TRUE from this function so that it gets removed safely.
2446 send_message_with_reply_deliver (data
, FALSE
);
2450 /* Called in GDBusWorker's thread - we must not block - without lock held */
2452 on_worker_closed (GDBusWorker
*worker
,
2453 gboolean remote_peer_vanished
,
2457 GDBusConnection
*connection
;
2459 guint old_atomic_flags
;
2461 G_LOCK (message_bus_lock
);
2462 alive
= (g_hash_table_lookup (alive_connections
, user_data
) != NULL
);
2465 G_UNLOCK (message_bus_lock
);
2468 connection
= G_DBUS_CONNECTION (user_data
);
2469 g_object_ref (connection
);
2470 G_UNLOCK (message_bus_lock
);
2472 //g_debug ("in on_worker_closed: %s", error->message);
2474 CONNECTION_LOCK (connection
);
2475 /* Even though this is atomic, we do it inside the lock to avoid breaking
2476 * assumptions in remove_match_rule(). We'd need the lock in a moment
2477 * anyway, so, no loss.
2479 old_atomic_flags
= g_atomic_int_or (&connection
->atomic_flags
, FLAG_CLOSED
);
2481 if (!(old_atomic_flags
& FLAG_CLOSED
))
2483 g_hash_table_foreach_remove (connection
->map_method_serial_to_send_message_data
, cancel_method_on_close
, NULL
);
2484 schedule_closed_unlocked (connection
, remote_peer_vanished
, error
);
2486 CONNECTION_UNLOCK (connection
);
2488 g_object_unref (connection
);
2491 /* ---------------------------------------------------------------------------------------------------- */
2493 /* Determines the biggest set of capabilities we can support on this
2496 * Called with the init_lock held.
2498 static GDBusCapabilityFlags
2499 get_offered_capabilities_max (GDBusConnection
*connection
)
2501 GDBusCapabilityFlags ret
;
2502 ret
= G_DBUS_CAPABILITY_FLAGS_NONE
;
2504 if (G_IS_UNIX_CONNECTION (connection
->stream
))
2505 ret
|= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
;
2510 /* Called in a user thread, lock is not held */
2512 initable_init (GInitable
*initable
,
2513 GCancellable
*cancellable
,
2516 GDBusConnection
*connection
= G_DBUS_CONNECTION (initable
);
2519 /* This method needs to be idempotent to work with the singleton
2520 * pattern. See the docs for g_initable_init(). We implement this by
2523 * Unfortunately we can't use the main lock since the on_worker_*()
2524 * callbacks above needs the lock during initialization (for message
2525 * bus connections we do a synchronous Hello() call on the bus).
2527 g_mutex_lock (&connection
->init_lock
);
2531 /* Make this a no-op if we're already initialized (successfully or
2534 if ((g_atomic_int_get (&connection
->atomic_flags
) & FLAG_INITIALIZED
))
2536 ret
= (connection
->initialization_error
== NULL
);
2540 /* Because of init_lock, we can't get here twice in different threads */
2541 g_assert (connection
->initialization_error
== NULL
);
2543 /* The user can pass multiple (but mutally exclusive) construct
2546 * - stream (of type GIOStream)
2547 * - address (of type gchar*)
2549 * At the end of the day we end up with a non-NULL GIOStream
2550 * object in connection->stream.
2552 if (connection
->address
!= NULL
)
2554 g_assert (connection
->stream
== NULL
);
2556 if ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
) ||
2557 (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS
))
2559 g_set_error_literal (&connection
->initialization_error
,
2561 G_IO_ERROR_INVALID_ARGUMENT
,
2562 _("Unsupported flags encountered when constructing a client-side connection"));
2566 connection
->stream
= g_dbus_address_get_stream_sync (connection
->address
,
2567 NULL
, /* TODO: out_guid */
2569 &connection
->initialization_error
);
2570 if (connection
->stream
== NULL
)
2573 else if (connection
->stream
!= NULL
)
2579 g_assert_not_reached ();
2582 /* Authenticate the connection */
2583 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
)
2585 g_assert (!(connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
));
2586 g_assert (connection
->guid
!= NULL
);
2587 connection
->auth
= _g_dbus_auth_new (connection
->stream
);
2588 if (!_g_dbus_auth_run_server (connection
->auth
,
2589 connection
->authentication_observer
,
2591 (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS
),
2592 get_offered_capabilities_max (connection
),
2593 &connection
->capabilities
,
2594 &connection
->credentials
,
2596 &connection
->initialization_error
))
2599 else if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
)
2601 g_assert (!(connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
));
2602 g_assert (connection
->guid
== NULL
);
2603 connection
->auth
= _g_dbus_auth_new (connection
->stream
);
2604 connection
->guid
= _g_dbus_auth_run_client (connection
->auth
,
2605 connection
->authentication_observer
,
2606 get_offered_capabilities_max (connection
),
2607 &connection
->capabilities
,
2609 &connection
->initialization_error
);
2610 if (connection
->guid
== NULL
)
2614 if (connection
->authentication_observer
!= NULL
)
2616 g_object_unref (connection
->authentication_observer
);
2617 connection
->authentication_observer
= NULL
;
2620 //g_output_stream_flush (G_SOCKET_CONNECTION (connection->stream)
2622 //g_debug ("haz unix fd passing powers: %d", connection->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
2625 /* We want all IO operations to be non-blocking since they happen in
2626 * the worker thread which is shared by _all_ connections.
2628 if (G_IS_SOCKET_CONNECTION (connection
->stream
))
2630 g_socket_set_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection
->stream
)), FALSE
);
2634 G_LOCK (message_bus_lock
);
2635 if (alive_connections
== NULL
)
2636 alive_connections
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
2637 g_hash_table_insert (alive_connections
, connection
, connection
);
2638 G_UNLOCK (message_bus_lock
);
2640 connection
->worker
= _g_dbus_worker_new (connection
->stream
,
2641 connection
->capabilities
,
2642 ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING
) != 0),
2643 on_worker_message_received
,
2644 on_worker_message_about_to_be_sent
,
2648 /* if a bus connection, call org.freedesktop.DBus.Hello - this is how we're getting a name */
2649 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)
2651 GVariant
*hello_result
;
2653 /* we could lift this restriction by adding code in gdbusprivate.c */
2654 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING
)
2656 g_set_error_literal (&connection
->initialization_error
,
2659 "Cannot use DELAY_MESSAGE_PROCESSING with MESSAGE_BUS_CONNECTION");
2663 hello_result
= g_dbus_connection_call_sync (connection
,
2664 "org.freedesktop.DBus", /* name */
2665 "/org/freedesktop/DBus", /* path */
2666 "org.freedesktop.DBus", /* interface */
2668 NULL
, /* parameters */
2669 G_VARIANT_TYPE ("(s)"),
2670 CALL_FLAGS_INITIALIZING
,
2672 NULL
, /* TODO: cancellable */
2673 &connection
->initialization_error
);
2674 if (hello_result
== NULL
)
2677 g_variant_get (hello_result
, "(s)", &connection
->bus_unique_name
);
2678 g_variant_unref (hello_result
);
2679 //g_debug ("unique name is `%s'", connection->bus_unique_name);
2686 g_assert (connection
->initialization_error
!= NULL
);
2687 g_propagate_error (error
, g_error_copy (connection
->initialization_error
));
2690 g_atomic_int_or (&connection
->atomic_flags
, FLAG_INITIALIZED
);
2691 g_mutex_unlock (&connection
->init_lock
);
2697 initable_iface_init (GInitableIface
*initable_iface
)
2699 initable_iface
->init
= initable_init
;
2702 /* ---------------------------------------------------------------------------------------------------- */
2705 async_initable_iface_init (GAsyncInitableIface
*async_initable_iface
)
2710 /* ---------------------------------------------------------------------------------------------------- */
2713 * g_dbus_connection_new:
2714 * @stream: A #GIOStream.
2715 * @guid: (allow-none): The GUID to use if a authenticating as a server or %NULL.
2716 * @flags: Flags describing how to make the connection.
2717 * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
2718 * @cancellable: (allow-none): A #GCancellable or %NULL.
2719 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
2720 * @user_data: The data to pass to @callback.
2722 * Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
2723 * with the end represented by @stream.
2725 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2726 * will be put into non-blocking mode.
2728 * The D-Bus connection will interact with @stream from a worker thread.
2729 * As a result, the caller should not interact with @stream after this
2730 * method has been called, except by calling g_object_unref() on it.
2732 * If @observer is not %NULL it may be used to control the
2733 * authentication process.
2735 * When the operation is finished, @callback will be invoked. You can
2736 * then call g_dbus_connection_new_finish() to get the result of the
2739 * This is a asynchronous failable constructor. See
2740 * g_dbus_connection_new_sync() for the synchronous
2746 g_dbus_connection_new (GIOStream
*stream
,
2748 GDBusConnectionFlags flags
,
2749 GDBusAuthObserver
*observer
,
2750 GCancellable
*cancellable
,
2751 GAsyncReadyCallback callback
,
2754 g_return_if_fail (G_IS_IO_STREAM (stream
));
2755 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION
,
2763 "authentication-observer", observer
,
2768 * g_dbus_connection_new_finish:
2769 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_new().
2770 * @error: Return location for error or %NULL.
2772 * Finishes an operation started with g_dbus_connection_new().
2774 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2779 g_dbus_connection_new_finish (GAsyncResult
*res
,
2783 GObject
*source_object
;
2785 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2786 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2788 source_object
= g_async_result_get_source_object (res
);
2789 g_assert (source_object
!= NULL
);
2790 object
= g_async_initable_new_finish (G_ASYNC_INITABLE (source_object
),
2793 g_object_unref (source_object
);
2795 return G_DBUS_CONNECTION (object
);
2801 * g_dbus_connection_new_sync:
2802 * @stream: A #GIOStream.
2803 * @guid: (allow-none): The GUID to use if a authenticating as a server or %NULL.
2804 * @flags: Flags describing how to make the connection.
2805 * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
2806 * @cancellable: (allow-none): A #GCancellable or %NULL.
2807 * @error: Return location for error or %NULL.
2809 * Synchronously sets up a D-Bus connection for exchanging D-Bus messages
2810 * with the end represented by @stream.
2812 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2813 * will be put into non-blocking mode.
2815 * The D-Bus connection will interact with @stream from a worker thread.
2816 * As a result, the caller should not interact with @stream after this
2817 * method has been called, except by calling g_object_unref() on it.
2819 * If @observer is not %NULL it may be used to control the
2820 * authentication process.
2822 * This is a synchronous failable constructor. See
2823 * g_dbus_connection_new() for the asynchronous version.
2825 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2830 g_dbus_connection_new_sync (GIOStream
*stream
,
2832 GDBusConnectionFlags flags
,
2833 GDBusAuthObserver
*observer
,
2834 GCancellable
*cancellable
,
2837 g_return_val_if_fail (G_IS_IO_STREAM (stream
), NULL
);
2838 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2839 return g_initable_new (G_TYPE_DBUS_CONNECTION
,
2845 "authentication-observer", observer
,
2849 /* ---------------------------------------------------------------------------------------------------- */
2852 * g_dbus_connection_new_for_address:
2853 * @address: A D-Bus address.
2854 * @flags: Flags describing how to make the connection.
2855 * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
2856 * @cancellable: (allow-none): A #GCancellable or %NULL.
2857 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
2858 * @user_data: The data to pass to @callback.
2860 * Asynchronously connects and sets up a D-Bus client connection for
2861 * exchanging D-Bus messages with an endpoint specified by @address
2862 * which must be in the D-Bus address format.
2864 * This constructor can only be used to initiate client-side
2865 * connections - use g_dbus_connection_new() if you need to act as the
2866 * server. In particular, @flags cannot contain the
2867 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2868 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2870 * When the operation is finished, @callback will be invoked. You can
2871 * then call g_dbus_connection_new_finish() to get the result of the
2874 * If @observer is not %NULL it may be used to control the
2875 * authentication process.
2877 * This is a asynchronous failable constructor. See
2878 * g_dbus_connection_new_for_address_sync() for the synchronous
2884 g_dbus_connection_new_for_address (const gchar
*address
,
2885 GDBusConnectionFlags flags
,
2886 GDBusAuthObserver
*observer
,
2887 GCancellable
*cancellable
,
2888 GAsyncReadyCallback callback
,
2891 g_return_if_fail (address
!= NULL
);
2892 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION
,
2899 "authentication-observer", observer
,
2904 * g_dbus_connection_new_for_address_finish:
2905 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_new().
2906 * @error: Return location for error or %NULL.
2908 * Finishes an operation started with g_dbus_connection_new_for_address().
2910 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2915 g_dbus_connection_new_for_address_finish (GAsyncResult
*res
,
2919 GObject
*source_object
;
2921 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2922 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2924 source_object
= g_async_result_get_source_object (res
);
2925 g_assert (source_object
!= NULL
);
2926 object
= g_async_initable_new_finish (G_ASYNC_INITABLE (source_object
),
2929 g_object_unref (source_object
);
2931 return G_DBUS_CONNECTION (object
);
2937 * g_dbus_connection_new_for_address_sync:
2938 * @address: A D-Bus address.
2939 * @flags: Flags describing how to make the connection.
2940 * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
2941 * @cancellable: (allow-none): A #GCancellable or %NULL.
2942 * @error: Return location for error or %NULL.
2944 * Synchronously connects and sets up a D-Bus client connection for
2945 * exchanging D-Bus messages with an endpoint specified by @address
2946 * which must be in the D-Bus address format.
2948 * This constructor can only be used to initiate client-side
2949 * connections - use g_dbus_connection_new_sync() if you need to act
2950 * as the server. In particular, @flags cannot contain the
2951 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2952 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2954 * This is a synchronous failable constructor. See
2955 * g_dbus_connection_new_for_address() for the asynchronous version.
2957 * If @observer is not %NULL it may be used to control the
2958 * authentication process.
2960 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2965 g_dbus_connection_new_for_address_sync (const gchar
*address
,
2966 GDBusConnectionFlags flags
,
2967 GDBusAuthObserver
*observer
,
2968 GCancellable
*cancellable
,
2971 g_return_val_if_fail (address
!= NULL
, NULL
);
2972 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2973 return g_initable_new (G_TYPE_DBUS_CONNECTION
,
2978 "authentication-observer", observer
,
2982 /* ---------------------------------------------------------------------------------------------------- */
2985 * g_dbus_connection_set_exit_on_close:
2986 * @connection: A #GDBusConnection.
2987 * @exit_on_close: Whether the process should be terminated
2988 * when @connection is closed by the remote peer.
2990 * Sets whether the process should be terminated when @connection is
2991 * closed by the remote peer. See #GDBusConnection:exit-on-close for
2994 * Note that this function should be used with care. Most modern UNIX
2995 * desktops tie the notion of a user session the session bus, and expect
2996 * all of a users applications to quit when their bus connection goes away.
2997 * If you are setting @exit_on_close to %FALSE for the shared session
2998 * bus connection, you should make sure that your application exits
2999 * when the user session ends.
3004 g_dbus_connection_set_exit_on_close (GDBusConnection
*connection
,
3005 gboolean exit_on_close
)
3007 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
3010 g_atomic_int_or (&connection
->atomic_flags
, FLAG_EXIT_ON_CLOSE
);
3012 g_atomic_int_and (&connection
->atomic_flags
, ~FLAG_EXIT_ON_CLOSE
);
3017 * g_dbus_connection_get_exit_on_close:
3018 * @connection: A #GDBusConnection.
3020 * Gets whether the process is terminated when @connection is
3021 * closed by the remote peer. See
3022 * #GDBusConnection:exit-on-close for more details.
3024 * Returns: Whether the process is terminated when @connection is
3025 * closed by the remote peer.
3030 g_dbus_connection_get_exit_on_close (GDBusConnection
*connection
)
3032 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
3034 if (g_atomic_int_get (&connection
->atomic_flags
) & FLAG_EXIT_ON_CLOSE
)
3041 * g_dbus_connection_get_guid:
3042 * @connection: A #GDBusConnection.
3044 * The GUID of the peer performing the role of server when
3045 * authenticating. See #GDBusConnection:guid for more details.
3047 * Returns: The GUID. Do not free this string, it is owned by
3053 g_dbus_connection_get_guid (GDBusConnection
*connection
)
3055 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3056 return connection
->guid
;
3060 * g_dbus_connection_get_unique_name:
3061 * @connection: A #GDBusConnection.
3063 * Gets the unique name of @connection as assigned by the message
3064 * bus. This can also be used to figure out if @connection is a
3065 * message bus connection.
3067 * Returns: The unique name or %NULL if @connection is not a message
3068 * bus connection. Do not free this string, it is owned by
3074 g_dbus_connection_get_unique_name (GDBusConnection
*connection
)
3076 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3078 /* do not use g_return_val_if_fail(), we want the memory barrier */
3079 if (!check_initialized (connection
))
3082 return connection
->bus_unique_name
;
3086 * g_dbus_connection_get_peer_credentials:
3087 * @connection: A #GDBusConnection.
3089 * Gets the credentials of the authenticated peer. This will always
3090 * return %NULL unless @connection acted as a server
3091 * (e.g. %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER was passed)
3092 * when set up and the client passed credentials as part of the
3093 * authentication process.
3095 * In a message bus setup, the message bus is always the server and
3096 * each application is a client. So this method will always return
3097 * %NULL for message bus clients.
3099 * Returns: (transfer none): A #GCredentials or %NULL if not available. Do not free
3100 * this object, it is owned by @connection.
3105 g_dbus_connection_get_peer_credentials (GDBusConnection
*connection
)
3107 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3109 /* do not use g_return_val_if_fail(), we want the memory barrier */
3110 if (!check_initialized (connection
))
3113 return connection
->credentials
;
3116 /* ---------------------------------------------------------------------------------------------------- */
3118 static guint _global_filter_id
= 1;
3121 * g_dbus_connection_add_filter:
3122 * @connection: A #GDBusConnection.
3123 * @filter_function: A filter function.
3124 * @user_data: User data to pass to @filter_function.
3125 * @user_data_free_func: Function to free @user_data with when filter
3126 * is removed or %NULL.
3128 * Adds a message filter. Filters are handlers that are run on all
3129 * incoming and outgoing messages, prior to standard dispatch. Filters
3130 * are run in the order that they were added. The same handler can be
3131 * added as a filter more than once, in which case it will be run more
3132 * than once. Filters added during a filter callback won't be run on
3133 * the message being processed. Filter functions are allowed to modify
3134 * and even drop messages.
3136 * Note that filters are run in a dedicated message handling thread so
3137 * they can't block and, generally, can't do anything but signal a
3138 * worker thread. Also note that filters are rarely needed - use API
3139 * such as g_dbus_connection_send_message_with_reply(),
3140 * g_dbus_connection_signal_subscribe() or g_dbus_connection_call() instead.
3142 * If a filter consumes an incoming message the message is not
3143 * dispatched anywhere else - not even the standard dispatch machinery
3144 * (that API such as g_dbus_connection_signal_subscribe() and
3145 * g_dbus_connection_send_message_with_reply() relies on) will see the
3146 * message. Similary, if a filter consumes an outgoing message, the
3147 * message will not be sent to the other peer.
3149 * Returns: A filter identifier that can be used with
3150 * g_dbus_connection_remove_filter().
3155 g_dbus_connection_add_filter (GDBusConnection
*connection
,
3156 GDBusMessageFilterFunction filter_function
,
3158 GDestroyNotify user_data_free_func
)
3162 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
3163 g_return_val_if_fail (filter_function
!= NULL
, 0);
3164 g_return_val_if_fail (check_initialized (connection
), 0);
3166 CONNECTION_LOCK (connection
);
3167 data
= g_new0 (FilterData
, 1);
3168 data
->id
= _global_filter_id
++; /* TODO: overflow etc. */
3169 data
->filter_function
= filter_function
;
3170 data
->user_data
= user_data
;
3171 data
->user_data_free_func
= user_data_free_func
;
3172 g_ptr_array_add (connection
->filters
, data
);
3173 CONNECTION_UNLOCK (connection
);
3178 /* only called from finalize(), removes all filters */
3180 purge_all_filters (GDBusConnection
*connection
)
3183 for (n
= 0; n
< connection
->filters
->len
; n
++)
3185 FilterData
*data
= connection
->filters
->pdata
[n
];
3186 if (data
->user_data_free_func
!= NULL
)
3187 data
->user_data_free_func (data
->user_data
);
3193 * g_dbus_connection_remove_filter:
3194 * @connection: a #GDBusConnection
3195 * @filter_id: an identifier obtained from g_dbus_connection_add_filter()
3202 g_dbus_connection_remove_filter (GDBusConnection
*connection
,
3206 FilterData
*to_destroy
;
3208 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
3209 g_return_if_fail (check_initialized (connection
));
3211 CONNECTION_LOCK (connection
);
3213 for (n
= 0; n
< connection
->filters
->len
; n
++)
3215 FilterData
*data
= connection
->filters
->pdata
[n
];
3216 if (data
->id
== filter_id
)
3218 g_ptr_array_remove_index (connection
->filters
, n
);
3223 CONNECTION_UNLOCK (connection
);
3225 /* do free without holding lock */
3226 if (to_destroy
!= NULL
)
3228 if (to_destroy
->user_data_free_func
!= NULL
)
3229 to_destroy
->user_data_free_func (to_destroy
->user_data
);
3230 g_free (to_destroy
);
3234 g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id
);
3238 /* ---------------------------------------------------------------------------------------------------- */
3244 gchar
*sender_unique_name
; /* if sender is unique or org.freedesktop.DBus, then that name... otherwise blank */
3245 gchar
*interface_name
;
3249 GArray
*subscribers
;
3254 GDBusSignalCallback callback
;
3256 GDestroyNotify user_data_free_func
;
3258 GMainContext
*context
;
3262 signal_data_free (SignalData
*signal_data
)
3264 g_free (signal_data
->rule
);
3265 g_free (signal_data
->sender
);
3266 g_free (signal_data
->sender_unique_name
);
3267 g_free (signal_data
->interface_name
);
3268 g_free (signal_data
->member
);
3269 g_free (signal_data
->object_path
);
3270 g_free (signal_data
->arg0
);
3271 g_array_free (signal_data
->subscribers
, TRUE
);
3272 g_free (signal_data
);
3276 args_to_rule (const gchar
*sender
,
3277 const gchar
*interface_name
,
3278 const gchar
*member
,
3279 const gchar
*object_path
,
3285 rule
= g_string_new ("type='signal'");
3287 g_string_prepend_c (rule
, '-');
3289 g_string_append_printf (rule
, ",sender='%s'", sender
);
3290 if (interface_name
!= NULL
)
3291 g_string_append_printf (rule
, ",interface='%s'", interface_name
);
3293 g_string_append_printf (rule
, ",member='%s'", member
);
3294 if (object_path
!= NULL
)
3295 g_string_append_printf (rule
, ",path='%s'", object_path
);
3297 g_string_append_printf (rule
, ",arg0='%s'", arg0
);
3299 return g_string_free (rule
, FALSE
);
3302 static guint _global_subscriber_id
= 1;
3303 static guint _global_registration_id
= 1;
3304 static guint _global_subtree_registration_id
= 1;
3306 /* ---------------------------------------------------------------------------------------------------- */
3308 /* Called in a user thread, lock is held */
3310 add_match_rule (GDBusConnection
*connection
,
3311 const gchar
*match_rule
)
3314 GDBusMessage
*message
;
3316 if (match_rule
[0] == '-')
3319 message
= g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3320 "/org/freedesktop/DBus", /* path */
3321 "org.freedesktop.DBus", /* interface */
3323 g_dbus_message_set_body (message
, g_variant_new ("(s)", match_rule
));
3325 if (!g_dbus_connection_send_message_unlocked (connection
,
3327 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
3331 g_critical ("Error while sending AddMatch() message: %s", error
->message
);
3332 g_error_free (error
);
3334 g_object_unref (message
);
3337 /* ---------------------------------------------------------------------------------------------------- */
3339 /* Called in a user thread, lock is held */
3341 remove_match_rule (GDBusConnection
*connection
,
3342 const gchar
*match_rule
)
3345 GDBusMessage
*message
;
3347 if (match_rule
[0] == '-')
3350 message
= g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3351 "/org/freedesktop/DBus", /* path */
3352 "org.freedesktop.DBus", /* interface */
3354 g_dbus_message_set_body (message
, g_variant_new ("(s)", match_rule
));
3357 if (!g_dbus_connection_send_message_unlocked (connection
,
3359 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
3363 /* If we could get G_IO_ERROR_CLOSED here, it wouldn't be reasonable to
3364 * critical; but we're holding the lock, and our caller checked whether
3365 * we were already closed, so we can't get that error.
3367 g_critical ("Error while sending RemoveMatch() message: %s", error
->message
);
3368 g_error_free (error
);
3370 g_object_unref (message
);
3373 /* ---------------------------------------------------------------------------------------------------- */
3376 is_signal_data_for_name_lost_or_acquired (SignalData
*signal_data
)
3378 return g_strcmp0 (signal_data
->sender_unique_name
, "org.freedesktop.DBus") == 0 &&
3379 g_strcmp0 (signal_data
->interface_name
, "org.freedesktop.DBus") == 0 &&
3380 g_strcmp0 (signal_data
->object_path
, "/org/freedesktop/DBus") == 0 &&
3381 (g_strcmp0 (signal_data
->member
, "NameLost") == 0 ||
3382 g_strcmp0 (signal_data
->member
, "NameAcquired") == 0);
3385 /* ---------------------------------------------------------------------------------------------------- */
3388 * g_dbus_connection_signal_subscribe:
3389 * @connection: A #GDBusConnection.
3390 * @sender: (allow-none): Sender name to match on (unique or well-known name)
3391 * or %NULL to listen from all senders.
3392 * @interface_name: (allow-none): D-Bus interface name to match on or %NULL to
3393 * match on all interfaces.
3394 * @member: (allow-none): D-Bus signal name to match on or %NULL to match on all signals.
3395 * @object_path: (allow-none): Object path to match on or %NULL to match on all object paths.
3396 * @arg0: (allow-none): Contents of first string argument to match on or %NULL
3397 * to match on all kinds of arguments.
3398 * @flags: Flags describing how to subscribe to the signal (currently unused).
3399 * @callback: Callback to invoke when there is a signal matching the requested data.
3400 * @user_data: User data to pass to @callback.
3401 * @user_data_free_func: (allow-none): Function to free @user_data with when
3402 * subscription is removed or %NULL.
3404 * Subscribes to signals on @connection and invokes @callback with a
3405 * whenever the signal is received. Note that @callback
3406 * will be invoked in the <link
3407 * linkend="g-main-context-push-thread-default">thread-default main
3408 * loop</link> of the thread you are calling this method from.
3410 * If @connection is not a message bus connection, @sender must be
3413 * If @sender is a well-known name note that @callback is invoked with
3414 * the unique name for the owner of @sender, not the well-known name
3415 * as one would expect. This is because the message bus rewrites the
3416 * name. As such, to avoid certain race conditions, users should be
3417 * tracking the name owner of the well-known name and use that when
3418 * processing the received signal.
3420 * Returns: A subscription identifier that can be used with g_dbus_connection_signal_unsubscribe().
3425 g_dbus_connection_signal_subscribe (GDBusConnection
*connection
,
3426 const gchar
*sender
,
3427 const gchar
*interface_name
,
3428 const gchar
*member
,
3429 const gchar
*object_path
,
3431 GDBusSignalFlags flags
,
3432 GDBusSignalCallback callback
,
3434 GDestroyNotify user_data_free_func
)
3437 SignalData
*signal_data
;
3438 SignalSubscriber subscriber
;
3439 GPtrArray
*signal_data_array
;
3440 const gchar
*sender_unique_name
;
3442 /* Right now we abort if AddMatch() fails since it can only fail with the bus being in
3443 * an OOM condition. We might want to change that but that would involve making
3444 * g_dbus_connection_signal_subscribe() asynchronous and having the call sites
3445 * handle that. And there's really no sensible way of handling this short of retrying
3446 * to add the match rule... and then there's the little thing that, hey, maybe there's
3447 * a reason the bus in an OOM condition.
3449 * Doable, but not really sure it's worth it...
3452 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
3453 g_return_val_if_fail (sender
== NULL
|| (g_dbus_is_name (sender
) && (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)), 0);
3454 g_return_val_if_fail (interface_name
== NULL
|| g_dbus_is_interface_name (interface_name
), 0);
3455 g_return_val_if_fail (member
== NULL
|| g_dbus_is_member_name (member
), 0);
3456 g_return_val_if_fail (object_path
== NULL
|| g_variant_is_object_path (object_path
), 0);
3457 g_return_val_if_fail (callback
!= NULL
, 0);
3458 g_return_val_if_fail (check_initialized (connection
), 0);
3460 CONNECTION_LOCK (connection
);
3462 /* If G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE was specified, we will end up
3463 * with a '-' character to prefix the rule (which will otherwise be
3466 * This allows us to hash the rule and do our lifecycle tracking in
3467 * the usual way, but the '-' prevents the match rule from ever
3468 * actually being send to the bus (either for add or remove).
3470 rule
= args_to_rule (sender
, interface_name
, member
, object_path
, arg0
,
3471 (flags
& G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE
) != 0);
3473 if (sender
!= NULL
&& (g_dbus_is_unique_name (sender
) || g_strcmp0 (sender
, "org.freedesktop.DBus") == 0))
3474 sender_unique_name
= sender
;
3476 sender_unique_name
= "";
3478 subscriber
.callback
= callback
;
3479 subscriber
.user_data
= user_data
;
3480 subscriber
.user_data_free_func
= user_data_free_func
;
3481 subscriber
.id
= _global_subscriber_id
++; /* TODO: overflow etc. */
3482 subscriber
.context
= g_main_context_ref_thread_default ();
3484 /* see if we've already have this rule */
3485 signal_data
= g_hash_table_lookup (connection
->map_rule_to_signal_data
, rule
);
3486 if (signal_data
!= NULL
)
3488 g_array_append_val (signal_data
->subscribers
, subscriber
);
3493 signal_data
= g_new0 (SignalData
, 1);
3494 signal_data
->rule
= rule
;
3495 signal_data
->sender
= g_strdup (sender
);
3496 signal_data
->sender_unique_name
= g_strdup (sender_unique_name
);
3497 signal_data
->interface_name
= g_strdup (interface_name
);
3498 signal_data
->member
= g_strdup (member
);
3499 signal_data
->object_path
= g_strdup (object_path
);
3500 signal_data
->arg0
= g_strdup (arg0
);
3501 signal_data
->subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3502 g_array_append_val (signal_data
->subscribers
, subscriber
);
3504 g_hash_table_insert (connection
->map_rule_to_signal_data
,
3508 /* Add the match rule to the bus...
3510 * Avoid adding match rules for NameLost and NameAcquired messages - the bus will
3511 * always send such messages to us.
3513 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)
3515 if (!is_signal_data_for_name_lost_or_acquired (signal_data
))
3516 add_match_rule (connection
, signal_data
->rule
);
3519 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
,
3520 signal_data
->sender_unique_name
);
3521 if (signal_data_array
== NULL
)
3523 signal_data_array
= g_ptr_array_new ();
3524 g_hash_table_insert (connection
->map_sender_unique_name_to_signal_data_array
,
3525 g_strdup (signal_data
->sender_unique_name
),
3528 g_ptr_array_add (signal_data_array
, signal_data
);
3531 g_hash_table_insert (connection
->map_id_to_signal_data
,
3532 GUINT_TO_POINTER (subscriber
.id
),
3535 CONNECTION_UNLOCK (connection
);
3537 return subscriber
.id
;
3540 /* ---------------------------------------------------------------------------------------------------- */
3542 /* called in any thread */
3543 /* must hold lock when calling this (except if connection->finalizing is TRUE) */
3545 unsubscribe_id_internal (GDBusConnection
*connection
,
3546 guint subscription_id
,
3547 GArray
*out_removed_subscribers
)
3549 SignalData
*signal_data
;
3550 GPtrArray
*signal_data_array
;
3553 signal_data
= g_hash_table_lookup (connection
->map_id_to_signal_data
,
3554 GUINT_TO_POINTER (subscription_id
));
3555 if (signal_data
== NULL
)
3557 /* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
3561 for (n
= 0; n
< signal_data
->subscribers
->len
; n
++)
3563 SignalSubscriber
*subscriber
;
3565 subscriber
= &(g_array_index (signal_data
->subscribers
, SignalSubscriber
, n
));
3566 if (subscriber
->id
!= subscription_id
)
3569 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_signal_data
,
3570 GUINT_TO_POINTER (subscription_id
)));
3571 g_array_append_val (out_removed_subscribers
, *subscriber
);
3572 g_array_remove_index (signal_data
->subscribers
, n
);
3574 if (signal_data
->subscribers
->len
== 0)
3576 g_warn_if_fail (g_hash_table_remove (connection
->map_rule_to_signal_data
, signal_data
->rule
));
3578 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
,
3579 signal_data
->sender_unique_name
);
3580 g_warn_if_fail (signal_data_array
!= NULL
);
3581 g_warn_if_fail (g_ptr_array_remove (signal_data_array
, signal_data
));
3583 if (signal_data_array
->len
== 0)
3585 g_warn_if_fail (g_hash_table_remove (connection
->map_sender_unique_name_to_signal_data_array
,
3586 signal_data
->sender_unique_name
));
3589 /* remove the match rule from the bus unless NameLost or NameAcquired (see subscribe()) */
3590 if ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
) &&
3591 !is_signal_data_for_name_lost_or_acquired (signal_data
) &&
3592 !g_dbus_connection_is_closed (connection
) &&
3593 !connection
->finalizing
)
3595 /* The check for g_dbus_connection_is_closed() means that
3596 * sending the RemoveMatch message can't fail with
3597 * G_IO_ERROR_CLOSED, because we're holding the lock,
3598 * so on_worker_closed() can't happen between the check we just
3599 * did, and releasing the lock later.
3601 remove_match_rule (connection
, signal_data
->rule
);
3604 signal_data_free (signal_data
);
3610 g_assert_not_reached ();
3617 * g_dbus_connection_signal_unsubscribe:
3618 * @connection: A #GDBusConnection.
3619 * @subscription_id: A subscription id obtained from g_dbus_connection_signal_subscribe().
3621 * Unsubscribes from signals.
3626 g_dbus_connection_signal_unsubscribe (GDBusConnection
*connection
,
3627 guint subscription_id
)
3629 GArray
*subscribers
;
3632 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
3633 g_return_if_fail (check_initialized (connection
));
3635 subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3637 CONNECTION_LOCK (connection
);
3638 unsubscribe_id_internal (connection
,
3641 CONNECTION_UNLOCK (connection
);
3644 g_assert (subscribers
->len
== 0 || subscribers
->len
== 1);
3646 /* call GDestroyNotify without lock held */
3647 for (n
= 0; n
< subscribers
->len
; n
++)
3649 SignalSubscriber
*subscriber
;
3650 subscriber
= &(g_array_index (subscribers
, SignalSubscriber
, n
));
3651 call_destroy_notify (subscriber
->context
,
3652 subscriber
->user_data_free_func
,
3653 subscriber
->user_data
);
3654 g_main_context_unref (subscriber
->context
);
3657 g_array_free (subscribers
, TRUE
);
3660 /* ---------------------------------------------------------------------------------------------------- */
3664 guint subscription_id
;
3665 GDBusSignalCallback callback
;
3667 GDBusMessage
*message
;
3668 GDBusConnection
*connection
;
3669 const gchar
*sender
;
3671 const gchar
*interface
;
3672 const gchar
*member
;
3675 /* called on delivery thread (e.g. where g_dbus_connection_signal_subscribe() was called) with
3679 emit_signal_instance_in_idle_cb (gpointer data
)
3681 SignalInstance
*signal_instance
= data
;
3682 GVariant
*parameters
;
3683 gboolean has_subscription
;
3685 parameters
= g_dbus_message_get_body (signal_instance
->message
);
3686 if (parameters
== NULL
)
3688 parameters
= g_variant_new ("()");
3689 g_variant_ref_sink (parameters
);
3693 g_variant_ref_sink (parameters
);
3697 g_print ("in emit_signal_instance_in_idle_cb (id=%d sender=%s path=%s interface=%s member=%s params=%s)\n",
3698 signal_instance
->subscription_id
,
3699 signal_instance
->sender
,
3700 signal_instance
->path
,
3701 signal_instance
->interface
,
3702 signal_instance
->member
,
3703 g_variant_print (parameters
, TRUE
));
3706 /* Careful here, don't do the callback if we no longer has the subscription */
3707 CONNECTION_LOCK (signal_instance
->connection
);
3708 has_subscription
= FALSE
;
3709 if (g_hash_table_lookup (signal_instance
->connection
->map_id_to_signal_data
,
3710 GUINT_TO_POINTER (signal_instance
->subscription_id
)) != NULL
)
3711 has_subscription
= TRUE
;
3712 CONNECTION_UNLOCK (signal_instance
->connection
);
3714 if (has_subscription
)
3715 signal_instance
->callback (signal_instance
->connection
,
3716 signal_instance
->sender
,
3717 signal_instance
->path
,
3718 signal_instance
->interface
,
3719 signal_instance
->member
,
3721 signal_instance
->user_data
);
3723 g_variant_unref (parameters
);
3729 signal_instance_free (SignalInstance
*signal_instance
)
3731 g_object_unref (signal_instance
->message
);
3732 g_object_unref (signal_instance
->connection
);
3733 g_free (signal_instance
);
3736 /* called in GDBusWorker thread WITH lock held */
3738 schedule_callbacks (GDBusConnection
*connection
,
3739 GPtrArray
*signal_data_array
,
3740 GDBusMessage
*message
,
3741 const gchar
*sender
)
3744 const gchar
*interface
;
3745 const gchar
*member
;
3754 interface
= g_dbus_message_get_interface (message
);
3755 member
= g_dbus_message_get_member (message
);
3756 path
= g_dbus_message_get_path (message
);
3757 arg0
= g_dbus_message_get_arg0 (message
);
3760 g_print ("In schedule_callbacks:\n"
3762 " interface = `%s'\n"
3773 /* TODO: if this is slow, then we can change signal_data_array into
3774 * map_object_path_to_signal_data_array or something.
3776 for (n
= 0; n
< signal_data_array
->len
; n
++)
3778 SignalData
*signal_data
= signal_data_array
->pdata
[n
];
3780 if (signal_data
->interface_name
!= NULL
&& g_strcmp0 (signal_data
->interface_name
, interface
) != 0)
3783 if (signal_data
->member
!= NULL
&& g_strcmp0 (signal_data
->member
, member
) != 0)
3786 if (signal_data
->object_path
!= NULL
&& g_strcmp0 (signal_data
->object_path
, path
) != 0)
3789 if (signal_data
->arg0
!= NULL
&& g_strcmp0 (signal_data
->arg0
, arg0
) != 0)
3792 for (m
= 0; m
< signal_data
->subscribers
->len
; m
++)
3794 SignalSubscriber
*subscriber
;
3795 GSource
*idle_source
;
3796 SignalInstance
*signal_instance
;
3798 subscriber
= &(g_array_index (signal_data
->subscribers
, SignalSubscriber
, m
));
3800 signal_instance
= g_new0 (SignalInstance
, 1);
3801 signal_instance
->subscription_id
= subscriber
->id
;
3802 signal_instance
->callback
= subscriber
->callback
;
3803 signal_instance
->user_data
= subscriber
->user_data
;
3804 signal_instance
->message
= g_object_ref (message
);
3805 signal_instance
->connection
= g_object_ref (connection
);
3806 signal_instance
->sender
= sender
;
3807 signal_instance
->path
= path
;
3808 signal_instance
->interface
= interface
;
3809 signal_instance
->member
= member
;
3811 idle_source
= g_idle_source_new ();
3812 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
3813 g_source_set_callback (idle_source
,
3814 emit_signal_instance_in_idle_cb
,
3816 (GDestroyNotify
) signal_instance_free
);
3817 g_source_attach (idle_source
, subscriber
->context
);
3818 g_source_unref (idle_source
);
3823 /* called in GDBusWorker thread with lock held */
3825 distribute_signals (GDBusConnection
*connection
,
3826 GDBusMessage
*message
)
3828 GPtrArray
*signal_data_array
;
3829 const gchar
*sender
;
3831 sender
= g_dbus_message_get_sender (message
);
3833 if (G_UNLIKELY (_g_dbus_debug_signal ()))
3835 _g_dbus_debug_print_lock ();
3836 g_print ("========================================================================\n"
3837 "GDBus-debug:Signal:\n"
3838 " <<<< RECEIVED SIGNAL %s.%s\n"
3840 " sent by name %s\n",
3841 g_dbus_message_get_interface (message
),
3842 g_dbus_message_get_member (message
),
3843 g_dbus_message_get_path (message
),
3844 sender
!= NULL
? sender
: "(none)");
3845 _g_dbus_debug_print_unlock ();
3848 /* collect subscribers that match on sender */
3851 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
, sender
);
3852 if (signal_data_array
!= NULL
)
3853 schedule_callbacks (connection
, signal_data_array
, message
, sender
);
3856 /* collect subscribers not matching on sender */
3857 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
, "");
3858 if (signal_data_array
!= NULL
)
3859 schedule_callbacks (connection
, signal_data_array
, message
, sender
);
3862 /* ---------------------------------------------------------------------------------------------------- */
3864 /* only called from finalize(), removes all subscriptions */
3866 purge_all_signal_subscriptions (GDBusConnection
*connection
)
3868 GHashTableIter iter
;
3871 GArray
*subscribers
;
3874 ids
= g_array_new (FALSE
, FALSE
, sizeof (guint
));
3875 g_hash_table_iter_init (&iter
, connection
->map_id_to_signal_data
);
3876 while (g_hash_table_iter_next (&iter
, &key
, NULL
))
3878 guint subscription_id
= GPOINTER_TO_UINT (key
);
3879 g_array_append_val (ids
, subscription_id
);
3882 subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3883 for (n
= 0; n
< ids
->len
; n
++)
3885 guint subscription_id
= g_array_index (ids
, guint
, n
);
3886 unsubscribe_id_internal (connection
,
3890 g_array_free (ids
, TRUE
);
3892 /* call GDestroyNotify without lock held */
3893 for (n
= 0; n
< subscribers
->len
; n
++)
3895 SignalSubscriber
*subscriber
;
3896 subscriber
= &(g_array_index (subscribers
, SignalSubscriber
, n
));
3897 call_destroy_notify (subscriber
->context
,
3898 subscriber
->user_data_free_func
,
3899 subscriber
->user_data
);
3900 g_main_context_unref (subscriber
->context
);
3903 g_array_free (subscribers
, TRUE
);
3906 /* ---------------------------------------------------------------------------------------------------- */
3908 static GDBusInterfaceVTable
*
3909 _g_dbus_interface_vtable_copy (const GDBusInterfaceVTable
*vtable
)
3911 /* Don't waste memory by copying padding - remember to update this
3912 * when changing struct _GDBusInterfaceVTable in gdbusconnection.h
3914 return g_memdup ((gconstpointer
) vtable
, 3 * sizeof (gpointer
));
3918 _g_dbus_interface_vtable_free (GDBusInterfaceVTable
*vtable
)
3923 /* ---------------------------------------------------------------------------------------------------- */
3925 static GDBusSubtreeVTable
*
3926 _g_dbus_subtree_vtable_copy (const GDBusSubtreeVTable
*vtable
)
3928 /* Don't waste memory by copying padding - remember to update this
3929 * when changing struct _GDBusSubtreeVTable in gdbusconnection.h
3931 return g_memdup ((gconstpointer
) vtable
, 3 * sizeof (gpointer
));
3935 _g_dbus_subtree_vtable_free (GDBusSubtreeVTable
*vtable
)
3940 /* ---------------------------------------------------------------------------------------------------- */
3942 struct ExportedObject
3945 GDBusConnection
*connection
;
3947 /* maps gchar* -> ExportedInterface* */
3948 GHashTable
*map_if_name_to_ei
;
3951 /* only called with lock held */
3953 exported_object_free (ExportedObject
*eo
)
3955 g_free (eo
->object_path
);
3956 g_hash_table_unref (eo
->map_if_name_to_ei
);
3965 gchar
*interface_name
;
3966 GDBusInterfaceVTable
*vtable
;
3967 GDBusInterfaceInfo
*interface_info
;
3969 GMainContext
*context
;
3971 GDestroyNotify user_data_free_func
;
3972 } ExportedInterface
;
3974 /* called with lock held */
3976 exported_interface_free (ExportedInterface
*ei
)
3978 g_dbus_interface_info_cache_release (ei
->interface_info
);
3979 g_dbus_interface_info_unref ((GDBusInterfaceInfo
*) ei
->interface_info
);
3981 call_destroy_notify (ei
->context
,
3982 ei
->user_data_free_func
,
3985 g_main_context_unref (ei
->context
);
3987 g_free (ei
->interface_name
);
3988 _g_dbus_interface_vtable_free (ei
->vtable
);
3992 /* ---------------------------------------------------------------------------------------------------- */
3994 /* Convenience function to check if @registration_id (if not zero) or
3995 * @subtree_registration_id (if not zero) has been unregistered. If
3996 * so, returns %TRUE.
3998 * May be called by any thread. Caller must *not* hold lock.
4001 has_object_been_unregistered (GDBusConnection
*connection
,
4002 guint registration_id
,
4003 guint subtree_registration_id
)
4007 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
4011 CONNECTION_LOCK (connection
);
4012 if (registration_id
!= 0 && g_hash_table_lookup (connection
->map_id_to_ei
,
4013 GUINT_TO_POINTER (registration_id
)) == NULL
)
4017 else if (subtree_registration_id
!= 0 && g_hash_table_lookup (connection
->map_id_to_es
,
4018 GUINT_TO_POINTER (subtree_registration_id
)) == NULL
)
4022 CONNECTION_UNLOCK (connection
);
4027 /* ---------------------------------------------------------------------------------------------------- */
4031 GDBusConnection
*connection
;
4032 GDBusMessage
*message
;
4034 const gchar
*property_name
;
4035 const GDBusInterfaceVTable
*vtable
;
4036 GDBusInterfaceInfo
*interface_info
;
4037 const GDBusPropertyInfo
*property_info
;
4038 guint registration_id
;
4039 guint subtree_registration_id
;
4043 property_data_free (PropertyData
*data
)
4045 g_object_unref (data
->connection
);
4046 g_object_unref (data
->message
);
4050 /* called in thread where object was registered - no locks held */
4052 invoke_get_property_in_idle_cb (gpointer _data
)
4054 PropertyData
*data
= _data
;
4057 GDBusMessage
*reply
;
4059 if (has_object_been_unregistered (data
->connection
,
4060 data
->registration_id
,
4061 data
->subtree_registration_id
))
4063 reply
= g_dbus_message_new_method_error (data
->message
,
4064 "org.freedesktop.DBus.Error.UnknownMethod",
4065 _("No such interface `org.freedesktop.DBus.Properties' on object at path %s"),
4066 g_dbus_message_get_path (data
->message
));
4067 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4068 g_object_unref (reply
);
4073 value
= data
->vtable
->get_property (data
->connection
,
4074 g_dbus_message_get_sender (data
->message
),
4075 g_dbus_message_get_path (data
->message
),
4076 data
->interface_info
->name
,
4077 data
->property_name
,
4084 g_assert_no_error (error
);
4086 g_variant_take_ref (value
);
4087 reply
= g_dbus_message_new_method_reply (data
->message
);
4088 g_dbus_message_set_body (reply
, g_variant_new ("(v)", value
));
4089 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4090 g_variant_unref (value
);
4091 g_object_unref (reply
);
4095 gchar
*dbus_error_name
;
4096 g_assert (error
!= NULL
);
4097 dbus_error_name
= g_dbus_error_encode_gerror (error
);
4098 reply
= g_dbus_message_new_method_error_literal (data
->message
,
4101 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4102 g_free (dbus_error_name
);
4103 g_error_free (error
);
4104 g_object_unref (reply
);
4111 /* called in thread where object was registered - no locks held */
4113 invoke_set_property_in_idle_cb (gpointer _data
)
4115 PropertyData
*data
= _data
;
4117 GDBusMessage
*reply
;
4123 g_variant_get (g_dbus_message_get_body (data
->message
),
4129 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
4130 * of the given value is wrong
4132 if (g_strcmp0 (g_variant_get_type_string (value
), data
->property_info
->signature
) != 0)
4134 reply
= g_dbus_message_new_method_error (data
->message
,
4135 "org.freedesktop.DBus.Error.InvalidArgs",
4136 _("Error setting property `%s': Expected type `%s' but got `%s'"),
4137 data
->property_info
->name
,
4138 data
->property_info
->signature
,
4139 g_variant_get_type_string (value
));
4143 if (!data
->vtable
->set_property (data
->connection
,
4144 g_dbus_message_get_sender (data
->message
),
4145 g_dbus_message_get_path (data
->message
),
4146 data
->interface_info
->name
,
4147 data
->property_name
,
4152 gchar
*dbus_error_name
;
4153 g_assert (error
!= NULL
);
4154 dbus_error_name
= g_dbus_error_encode_gerror (error
);
4155 reply
= g_dbus_message_new_method_error_literal (data
->message
,
4158 g_free (dbus_error_name
);
4159 g_error_free (error
);
4163 reply
= g_dbus_message_new_method_reply (data
->message
);
4167 g_assert (reply
!= NULL
);
4168 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4169 g_object_unref (reply
);
4170 g_variant_unref (value
);
4175 /* called in any thread with connection's lock held */
4177 validate_and_maybe_schedule_property_getset (GDBusConnection
*connection
,
4178 GDBusMessage
*message
,
4179 guint registration_id
,
4180 guint subtree_registration_id
,
4182 GDBusInterfaceInfo
*interface_info
,
4183 const GDBusInterfaceVTable
*vtable
,
4184 GMainContext
*main_context
,
4188 const char *interface_name
;
4189 const char *property_name
;
4190 const GDBusPropertyInfo
*property_info
;
4191 GSource
*idle_source
;
4192 PropertyData
*property_data
;
4193 GDBusMessage
*reply
;
4198 g_variant_get (g_dbus_message_get_body (message
),
4203 g_variant_get (g_dbus_message_get_body (message
),
4212 if (vtable
== NULL
|| vtable
->get_property
== NULL
)
4217 if (vtable
== NULL
|| vtable
->set_property
== NULL
)
4221 /* Check that the property exists - if not fail with org.freedesktop.DBus.Error.InvalidArgs
4223 property_info
= NULL
;
4225 /* TODO: the cost of this is O(n) - it might be worth caching the result */
4226 property_info
= g_dbus_interface_info_lookup_property (interface_info
, property_name
);
4227 if (property_info
== NULL
)
4229 reply
= g_dbus_message_new_method_error (message
,
4230 "org.freedesktop.DBus.Error.InvalidArgs",
4231 _("No such property `%s'"),
4233 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4234 g_object_unref (reply
);
4239 if (is_get
&& !(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
))
4241 reply
= g_dbus_message_new_method_error (message
,
4242 "org.freedesktop.DBus.Error.InvalidArgs",
4243 _("Property `%s' is not readable"),
4245 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4246 g_object_unref (reply
);
4250 else if (!is_get
&& !(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE
))
4252 reply
= g_dbus_message_new_method_error (message
,
4253 "org.freedesktop.DBus.Error.InvalidArgs",
4254 _("Property `%s' is not writable"),
4256 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4257 g_object_unref (reply
);
4262 /* ok, got the property info - call user code in an idle handler */
4263 property_data
= g_new0 (PropertyData
, 1);
4264 property_data
->connection
= g_object_ref (connection
);
4265 property_data
->message
= g_object_ref (message
);
4266 property_data
->user_data
= user_data
;
4267 property_data
->property_name
= property_name
;
4268 property_data
->vtable
= vtable
;
4269 property_data
->interface_info
= interface_info
;
4270 property_data
->property_info
= property_info
;
4271 property_data
->registration_id
= registration_id
;
4272 property_data
->subtree_registration_id
= subtree_registration_id
;
4274 idle_source
= g_idle_source_new ();
4275 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4276 g_source_set_callback (idle_source
,
4277 is_get
? invoke_get_property_in_idle_cb
: invoke_set_property_in_idle_cb
,
4279 (GDestroyNotify
) property_data_free
);
4280 g_source_attach (idle_source
, main_context
);
4281 g_source_unref (idle_source
);
4289 /* called in GDBusWorker thread with connection's lock held */
4291 handle_getset_property (GDBusConnection
*connection
,
4293 GDBusMessage
*message
,
4296 ExportedInterface
*ei
;
4298 const char *interface_name
;
4299 const char *property_name
;
4304 g_variant_get (g_dbus_message_get_body (message
),
4309 g_variant_get (g_dbus_message_get_body (message
),
4315 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4316 * no such interface registered
4318 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
4321 GDBusMessage
*reply
;
4322 reply
= g_dbus_message_new_method_error (message
,
4323 "org.freedesktop.DBus.Error.InvalidArgs",
4324 _("No such interface `%s'"),
4326 g_dbus_connection_send_message_unlocked (eo
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4327 g_object_unref (reply
);
4332 handled
= validate_and_maybe_schedule_property_getset (eo
->connection
,
4345 /* ---------------------------------------------------------------------------------------------------- */
4349 GDBusConnection
*connection
;
4350 GDBusMessage
*message
;
4352 const GDBusInterfaceVTable
*vtable
;
4353 GDBusInterfaceInfo
*interface_info
;
4354 guint registration_id
;
4355 guint subtree_registration_id
;
4356 } PropertyGetAllData
;
4359 property_get_all_data_free (PropertyData
*data
)
4361 g_object_unref (data
->connection
);
4362 g_object_unref (data
->message
);
4366 /* called in thread where object was registered - no locks held */
4368 invoke_get_all_properties_in_idle_cb (gpointer _data
)
4370 PropertyGetAllData
*data
= _data
;
4371 GVariantBuilder builder
;
4372 GDBusMessage
*reply
;
4375 if (has_object_been_unregistered (data
->connection
,
4376 data
->registration_id
,
4377 data
->subtree_registration_id
))
4379 reply
= g_dbus_message_new_method_error (data
->message
,
4380 "org.freedesktop.DBus.Error.UnknownMethod",
4381 _("No such interface `org.freedesktop.DBus.Properties' on object at path %s"),
4382 g_dbus_message_get_path (data
->message
));
4383 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4384 g_object_unref (reply
);
4388 /* TODO: Right now we never fail this call - we just omit values if
4389 * a get_property() call is failing.
4391 * We could fail the whole call if just a single get_property() call
4392 * returns an error. We need clarification in the D-Bus spec about this.
4394 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("(a{sv})"));
4395 g_variant_builder_open (&builder
, G_VARIANT_TYPE ("a{sv}"));
4396 for (n
= 0; data
->interface_info
->properties
!= NULL
&& data
->interface_info
->properties
[n
] != NULL
; n
++)
4398 const GDBusPropertyInfo
*property_info
= data
->interface_info
->properties
[n
];
4401 if (!(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
))
4404 value
= data
->vtable
->get_property (data
->connection
,
4405 g_dbus_message_get_sender (data
->message
),
4406 g_dbus_message_get_path (data
->message
),
4407 data
->interface_info
->name
,
4408 property_info
->name
,
4415 g_variant_take_ref (value
);
4416 g_variant_builder_add (&builder
,
4418 property_info
->name
,
4420 g_variant_unref (value
);
4422 g_variant_builder_close (&builder
);
4424 reply
= g_dbus_message_new_method_reply (data
->message
);
4425 g_dbus_message_set_body (reply
, g_variant_builder_end (&builder
));
4426 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4427 g_object_unref (reply
);
4433 /* called in any thread with connection's lock held */
4435 validate_and_maybe_schedule_property_get_all (GDBusConnection
*connection
,
4436 GDBusMessage
*message
,
4437 guint registration_id
,
4438 guint subtree_registration_id
,
4439 GDBusInterfaceInfo
*interface_info
,
4440 const GDBusInterfaceVTable
*vtable
,
4441 GMainContext
*main_context
,
4445 const char *interface_name
;
4446 GSource
*idle_source
;
4447 PropertyGetAllData
*property_get_all_data
;
4451 g_variant_get (g_dbus_message_get_body (message
),
4455 if (vtable
== NULL
|| vtable
->get_property
== NULL
)
4458 /* ok, got the property info - call user in an idle handler */
4459 property_get_all_data
= g_new0 (PropertyGetAllData
, 1);
4460 property_get_all_data
->connection
= g_object_ref (connection
);
4461 property_get_all_data
->message
= g_object_ref (message
);
4462 property_get_all_data
->user_data
= user_data
;
4463 property_get_all_data
->vtable
= vtable
;
4464 property_get_all_data
->interface_info
= interface_info
;
4465 property_get_all_data
->registration_id
= registration_id
;
4466 property_get_all_data
->subtree_registration_id
= subtree_registration_id
;
4468 idle_source
= g_idle_source_new ();
4469 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4470 g_source_set_callback (idle_source
,
4471 invoke_get_all_properties_in_idle_cb
,
4472 property_get_all_data
,
4473 (GDestroyNotify
) property_get_all_data_free
);
4474 g_source_attach (idle_source
, main_context
);
4475 g_source_unref (idle_source
);
4483 /* called in GDBusWorker thread with connection's lock held */
4485 handle_get_all_properties (GDBusConnection
*connection
,
4487 GDBusMessage
*message
)
4489 ExportedInterface
*ei
;
4491 const char *interface_name
;
4495 g_variant_get (g_dbus_message_get_body (message
),
4499 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4500 * no such interface registered
4502 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
4505 GDBusMessage
*reply
;
4506 reply
= g_dbus_message_new_method_error (message
,
4507 "org.freedesktop.DBus.Error.InvalidArgs",
4508 _("No such interface"),
4510 g_dbus_connection_send_message_unlocked (eo
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4511 g_object_unref (reply
);
4516 handled
= validate_and_maybe_schedule_property_get_all (eo
->connection
,
4528 /* ---------------------------------------------------------------------------------------------------- */
4530 static const gchar introspect_header
[] =
4531 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
4532 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
4533 "<!-- GDBus " PACKAGE_VERSION
" -->\n"
4536 static const gchar introspect_tail
[] =
4539 static const gchar introspect_properties_interface
[] =
4540 " <interface name=\"org.freedesktop.DBus.Properties\">\n"
4541 " <method name=\"Get\">\n"
4542 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4543 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4544 " <arg type=\"v\" name=\"value\" direction=\"out\"/>\n"
4546 " <method name=\"GetAll\">\n"
4547 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4548 " <arg type=\"a{sv}\" name=\"properties\" direction=\"out\"/>\n"
4550 " <method name=\"Set\">\n"
4551 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4552 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4553 " <arg type=\"v\" name=\"value\" direction=\"in\"/>\n"
4555 " <signal name=\"PropertiesChanged\">\n"
4556 " <arg type=\"s\" name=\"interface_name\"/>\n"
4557 " <arg type=\"a{sv}\" name=\"changed_properties\"/>\n"
4558 " <arg type=\"as\" name=\"invalidated_properties\"/>\n"
4562 static const gchar introspect_introspectable_interface
[] =
4563 " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
4564 " <method name=\"Introspect\">\n"
4565 " <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>\n"
4568 " <interface name=\"org.freedesktop.DBus.Peer\">\n"
4569 " <method name=\"Ping\"/>\n"
4570 " <method name=\"GetMachineId\">\n"
4571 " <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n"
4576 introspect_append_header (GString
*s
)
4578 g_string_append (s
, introspect_header
);
4582 maybe_add_path (const gchar
*path
, gsize path_len
, const gchar
*object_path
, GHashTable
*set
)
4584 if (g_str_has_prefix (object_path
, path
) && strlen (object_path
) > path_len
&& object_path
[path_len
-1] == '/')
4590 begin
= object_path
+ path_len
;
4591 end
= strchr (begin
, '/');
4593 s
= g_strndup (begin
, end
- begin
);
4595 s
= g_strdup (begin
);
4597 if (g_hash_table_lookup (set
, s
) == NULL
)
4598 g_hash_table_insert (set
, s
, GUINT_TO_POINTER (1));
4604 /* TODO: we want a nicer public interface for this */
4605 /* called in any thread with connection's lock held */
4607 g_dbus_connection_list_registered_unlocked (GDBusConnection
*connection
,
4612 GHashTableIter hash_iter
;
4613 const gchar
*object_path
;
4619 CONNECTION_ENSURE_LOCK (connection
);
4621 path_len
= strlen (path
);
4625 set
= g_hash_table_new (g_str_hash
, g_str_equal
);
4627 g_hash_table_iter_init (&hash_iter
, connection
->map_object_path_to_eo
);
4628 while (g_hash_table_iter_next (&hash_iter
, (gpointer
) &object_path
, NULL
))
4629 maybe_add_path (path
, path_len
, object_path
, set
);
4631 g_hash_table_iter_init (&hash_iter
, connection
->map_object_path_to_es
);
4632 while (g_hash_table_iter_next (&hash_iter
, (gpointer
) &object_path
, NULL
))
4633 maybe_add_path (path
, path_len
, object_path
, set
);
4635 p
= g_ptr_array_new ();
4636 keys
= g_hash_table_get_keys (set
);
4637 for (l
= keys
; l
!= NULL
; l
= l
->next
)
4638 g_ptr_array_add (p
, l
->data
);
4639 g_hash_table_unref (set
);
4642 g_ptr_array_add (p
, NULL
);
4643 ret
= (gchar
**) g_ptr_array_free (p
, FALSE
);
4647 /* called in any thread with connection's lock not held */
4649 g_dbus_connection_list_registered (GDBusConnection
*connection
,
4653 CONNECTION_LOCK (connection
);
4654 ret
= g_dbus_connection_list_registered_unlocked (connection
, path
);
4655 CONNECTION_UNLOCK (connection
);
4659 /* called in GDBusWorker thread with connection's lock held */
4661 handle_introspect (GDBusConnection
*connection
,
4663 GDBusMessage
*message
)
4667 GDBusMessage
*reply
;
4668 GHashTableIter hash_iter
;
4669 ExportedInterface
*ei
;
4672 /* first the header with the standard interfaces */
4673 s
= g_string_sized_new (sizeof (introspect_header
) +
4674 sizeof (introspect_properties_interface
) +
4675 sizeof (introspect_introspectable_interface
) +
4676 sizeof (introspect_tail
));
4677 introspect_append_header (s
);
4678 if (!g_hash_table_lookup (eo
->map_if_name_to_ei
,
4679 "org.freedesktop.DBus.Properties"))
4680 g_string_append (s
, introspect_properties_interface
);
4682 if (!g_hash_table_lookup (eo
->map_if_name_to_ei
,
4683 "org.freedesktop.DBus.Introspectable"))
4684 g_string_append (s
, introspect_introspectable_interface
);
4686 /* then include the registered interfaces */
4687 g_hash_table_iter_init (&hash_iter
, eo
->map_if_name_to_ei
);
4688 while (g_hash_table_iter_next (&hash_iter
, NULL
, (gpointer
) &ei
))
4689 g_dbus_interface_info_generate_xml (ei
->interface_info
, 2, s
);
4691 /* finally include nodes registered below us */
4692 registered
= g_dbus_connection_list_registered_unlocked (connection
, eo
->object_path
);
4693 for (n
= 0; registered
!= NULL
&& registered
[n
] != NULL
; n
++)
4694 g_string_append_printf (s
, " <node name=\"%s\"/>\n", registered
[n
]);
4695 g_strfreev (registered
);
4696 g_string_append (s
, introspect_tail
);
4698 reply
= g_dbus_message_new_method_reply (message
);
4699 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
4700 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4701 g_object_unref (reply
);
4702 g_string_free (s
, TRUE
);
4707 /* called in thread where object was registered - no locks held */
4709 call_in_idle_cb (gpointer user_data
)
4711 GDBusMethodInvocation
*invocation
= G_DBUS_METHOD_INVOCATION (user_data
);
4712 GDBusInterfaceVTable
*vtable
;
4713 guint registration_id
;
4714 guint subtree_registration_id
;
4716 registration_id
= GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation
), "g-dbus-registration-id"));
4717 subtree_registration_id
= GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation
), "g-dbus-subtree-registration-id"));
4719 if (has_object_been_unregistered (g_dbus_method_invocation_get_connection (invocation
),
4721 subtree_registration_id
))
4723 GDBusMessage
*reply
;
4724 reply
= g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation
),
4725 "org.freedesktop.DBus.Error.UnknownMethod",
4726 _("No such interface `%s' on object at path %s"),
4727 g_dbus_method_invocation_get_interface_name (invocation
),
4728 g_dbus_method_invocation_get_object_path (invocation
));
4729 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation
), reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4730 g_object_unref (reply
);
4734 vtable
= g_object_get_data (G_OBJECT (invocation
), "g-dbus-interface-vtable");
4735 g_assert (vtable
!= NULL
&& vtable
->method_call
!= NULL
);
4737 vtable
->method_call (g_dbus_method_invocation_get_connection (invocation
),
4738 g_dbus_method_invocation_get_sender (invocation
),
4739 g_dbus_method_invocation_get_object_path (invocation
),
4740 g_dbus_method_invocation_get_interface_name (invocation
),
4741 g_dbus_method_invocation_get_method_name (invocation
),
4742 g_dbus_method_invocation_get_parameters (invocation
),
4743 g_object_ref (invocation
),
4744 g_dbus_method_invocation_get_user_data (invocation
));
4750 /* called in GDBusWorker thread with connection's lock held */
4752 validate_and_maybe_schedule_method_call (GDBusConnection
*connection
,
4753 GDBusMessage
*message
,
4754 guint registration_id
,
4755 guint subtree_registration_id
,
4756 GDBusInterfaceInfo
*interface_info
,
4757 const GDBusInterfaceVTable
*vtable
,
4758 GMainContext
*main_context
,
4761 GDBusMethodInvocation
*invocation
;
4762 const GDBusMethodInfo
*method_info
;
4763 GDBusMessage
*reply
;
4764 GVariant
*parameters
;
4765 GSource
*idle_source
;
4767 GVariantType
*in_type
;
4771 /* TODO: the cost of this is O(n) - it might be worth caching the result */
4772 method_info
= g_dbus_interface_info_lookup_method (interface_info
, g_dbus_message_get_member (message
));
4774 /* if the method doesn't exist, return the org.freedesktop.DBus.Error.UnknownMethod
4775 * error to the caller
4777 if (method_info
== NULL
)
4779 reply
= g_dbus_message_new_method_error (message
,
4780 "org.freedesktop.DBus.Error.UnknownMethod",
4781 _("No such method `%s'"),
4782 g_dbus_message_get_member (message
));
4783 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4784 g_object_unref (reply
);
4789 parameters
= g_dbus_message_get_body (message
);
4790 if (parameters
== NULL
)
4792 parameters
= g_variant_new ("()");
4793 g_variant_ref_sink (parameters
);
4797 g_variant_ref (parameters
);
4800 /* Check that the incoming args are of the right type - if they are not, return
4801 * the org.freedesktop.DBus.Error.InvalidArgs error to the caller
4803 in_type
= _g_dbus_compute_complete_signature (method_info
->in_args
);
4804 if (!g_variant_is_of_type (parameters
, in_type
))
4808 type_string
= g_variant_type_dup_string (in_type
);
4810 reply
= g_dbus_message_new_method_error (message
,
4811 "org.freedesktop.DBus.Error.InvalidArgs",
4812 _("Type of message, `%s', does not match expected type `%s'"),
4813 g_variant_get_type_string (parameters
),
4815 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4816 g_variant_type_free (in_type
);
4817 g_variant_unref (parameters
);
4818 g_object_unref (reply
);
4819 g_free (type_string
);
4823 g_variant_type_free (in_type
);
4825 /* schedule the call in idle */
4826 invocation
= _g_dbus_method_invocation_new (g_dbus_message_get_sender (message
),
4827 g_dbus_message_get_path (message
),
4828 g_dbus_message_get_interface (message
),
4829 g_dbus_message_get_member (message
),
4835 g_variant_unref (parameters
);
4837 /* TODO: would be nicer with a real MethodData like we already
4838 * have PropertyData and PropertyGetAllData... */
4839 g_object_set_data (G_OBJECT (invocation
), "g-dbus-interface-vtable", (gpointer
) vtable
);
4840 g_object_set_data (G_OBJECT (invocation
), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id
));
4841 g_object_set_data (G_OBJECT (invocation
), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id
));
4843 idle_source
= g_idle_source_new ();
4844 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4845 g_source_set_callback (idle_source
,
4849 g_source_attach (idle_source
, main_context
);
4850 g_source_unref (idle_source
);
4858 /* ---------------------------------------------------------------------------------------------------- */
4860 /* called in GDBusWorker thread with connection's lock held */
4862 obj_message_func (GDBusConnection
*connection
,
4864 GDBusMessage
*message
)
4866 const gchar
*interface_name
;
4867 const gchar
*member
;
4868 const gchar
*signature
;
4873 interface_name
= g_dbus_message_get_interface (message
);
4874 member
= g_dbus_message_get_member (message
);
4875 signature
= g_dbus_message_get_signature (message
);
4877 /* see if we have an interface for handling this call */
4878 if (interface_name
!= NULL
)
4880 ExportedInterface
*ei
;
4881 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
4884 /* we do - invoke the handler in idle in the right thread */
4886 /* handle no vtable or handler being present */
4887 if (ei
->vtable
== NULL
|| ei
->vtable
->method_call
== NULL
)
4890 handled
= validate_and_maybe_schedule_method_call (connection
,
4902 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Introspectable") == 0 &&
4903 g_strcmp0 (member
, "Introspect") == 0 &&
4904 g_strcmp0 (signature
, "") == 0)
4906 handled
= handle_introspect (connection
, eo
, message
);
4909 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
4910 g_strcmp0 (member
, "Get") == 0 &&
4911 g_strcmp0 (signature
, "ss") == 0)
4913 handled
= handle_getset_property (connection
, eo
, message
, TRUE
);
4916 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
4917 g_strcmp0 (member
, "Set") == 0 &&
4918 g_strcmp0 (signature
, "ssv") == 0)
4920 handled
= handle_getset_property (connection
, eo
, message
, FALSE
);
4923 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
4924 g_strcmp0 (member
, "GetAll") == 0 &&
4925 g_strcmp0 (signature
, "s") == 0)
4927 handled
= handle_get_all_properties (connection
, eo
, message
);
4936 * g_dbus_connection_register_object:
4937 * @connection: A #GDBusConnection.
4938 * @object_path: The object path to register at.
4939 * @interface_info: Introspection data for the interface.
4940 * @vtable: (allow-none): A #GDBusInterfaceVTable to call into or %NULL.
4941 * @user_data: (allow-none): Data to pass to functions in @vtable.
4942 * @user_data_free_func: Function to call when the object path is unregistered.
4943 * @error: Return location for error or %NULL.
4945 * Registers callbacks for exported objects at @object_path with the
4946 * D-Bus interface that is described in @interface_info.
4948 * Calls to functions in @vtable (and @user_data_free_func) will
4949 * happen in the <link linkend="g-main-context-push-thread-default">thread-default main
4950 * loop</link> of the thread you are calling this method from.
4952 * Note that all #GVariant values passed to functions in @vtable will match
4953 * the signature given in @interface_info - if a remote caller passes
4954 * incorrect values, the <literal>org.freedesktop.DBus.Error.InvalidArgs</literal>
4955 * is returned to the remote caller.
4957 * Additionally, if the remote caller attempts to invoke methods or
4958 * access properties not mentioned in @interface_info the
4959 * <literal>org.freedesktop.DBus.Error.UnknownMethod</literal> resp.
4960 * <literal>org.freedesktop.DBus.Error.InvalidArgs</literal> errors
4961 * are returned to the caller.
4963 * It is considered a programming error if the
4964 * #GDBusInterfaceGetPropertyFunc function in @vtable returns a
4965 * #GVariant of incorrect type.
4967 * If an existing callback is already registered at @object_path and
4968 * @interface_name, then @error is set to #G_IO_ERROR_EXISTS.
4970 * GDBus automatically implements the standard D-Bus interfaces
4971 * org.freedesktop.DBus.Properties, org.freedesktop.DBus.Introspectable
4972 * and org.freedesktop.Peer, so you don't have to implement those for
4973 * the objects you export. You <emphasis>can</emphasis> implement
4974 * org.freedesktop.DBus.Properties yourself, e.g. to handle getting
4975 * and setting of properties asynchronously.
4977 * Note that the reference count on @interface_info will be
4978 * incremented by 1 (unless allocated statically, e.g. if the
4979 * reference count is -1, see g_dbus_interface_info_ref()) for as long
4980 * as the object is exported. Also note that @vtable will be copied.
4982 * See <xref linkend="gdbus-server"/> for an example of how to use this method.
4984 * Returns: 0 if @error is set, otherwise a registration id (never 0)
4985 * that can be used with g_dbus_connection_unregister_object() .
4990 g_dbus_connection_register_object (GDBusConnection
*connection
,
4991 const gchar
*object_path
,
4992 GDBusInterfaceInfo
*interface_info
,
4993 const GDBusInterfaceVTable
*vtable
,
4995 GDestroyNotify user_data_free_func
,
4999 ExportedInterface
*ei
;
5002 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
5003 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), 0);
5004 g_return_val_if_fail (interface_info
!= NULL
, 0);
5005 g_return_val_if_fail (g_dbus_is_interface_name (interface_info
->name
), 0);
5006 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, 0);
5007 g_return_val_if_fail (check_initialized (connection
), 0);
5011 CONNECTION_LOCK (connection
);
5013 eo
= g_hash_table_lookup (connection
->map_object_path_to_eo
, object_path
);
5016 eo
= g_new0 (ExportedObject
, 1);
5017 eo
->object_path
= g_strdup (object_path
);
5018 eo
->connection
= connection
;
5019 eo
->map_if_name_to_ei
= g_hash_table_new_full (g_str_hash
,
5022 (GDestroyNotify
) exported_interface_free
);
5023 g_hash_table_insert (connection
->map_object_path_to_eo
, eo
->object_path
, eo
);
5026 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_info
->name
);
5032 _("An object is already exported for the interface %s at %s"),
5033 interface_info
->name
,
5038 ei
= g_new0 (ExportedInterface
, 1);
5039 ei
->id
= _global_registration_id
++; /* TODO: overflow etc. */
5041 ei
->user_data
= user_data
;
5042 ei
->user_data_free_func
= user_data_free_func
;
5043 ei
->vtable
= _g_dbus_interface_vtable_copy (vtable
);
5044 ei
->interface_info
= g_dbus_interface_info_ref (interface_info
);
5045 g_dbus_interface_info_cache_build (ei
->interface_info
);
5046 ei
->interface_name
= g_strdup (interface_info
->name
);
5047 ei
->context
= g_main_context_ref_thread_default ();
5049 g_hash_table_insert (eo
->map_if_name_to_ei
,
5050 (gpointer
) ei
->interface_name
,
5052 g_hash_table_insert (connection
->map_id_to_ei
,
5053 GUINT_TO_POINTER (ei
->id
),
5059 CONNECTION_UNLOCK (connection
);
5065 * g_dbus_connection_unregister_object:
5066 * @connection: A #GDBusConnection.
5067 * @registration_id: A registration id obtained from g_dbus_connection_register_object().
5069 * Unregisters an object.
5071 * Returns: %TRUE if the object was unregistered, %FALSE otherwise.
5076 g_dbus_connection_unregister_object (GDBusConnection
*connection
,
5077 guint registration_id
)
5079 ExportedInterface
*ei
;
5083 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
5084 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5088 CONNECTION_LOCK (connection
);
5090 ei
= g_hash_table_lookup (connection
->map_id_to_ei
,
5091 GUINT_TO_POINTER (registration_id
));
5097 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_ei
, GUINT_TO_POINTER (ei
->id
)));
5098 g_warn_if_fail (g_hash_table_remove (eo
->map_if_name_to_ei
, ei
->interface_name
));
5099 /* unregister object path if we have no more exported interfaces */
5100 if (g_hash_table_size (eo
->map_if_name_to_ei
) == 0)
5101 g_warn_if_fail (g_hash_table_remove (connection
->map_object_path_to_eo
,
5107 CONNECTION_UNLOCK (connection
);
5112 /* ---------------------------------------------------------------------------------------------------- */
5115 * g_dbus_connection_emit_signal:
5116 * @connection: A #GDBusConnection.
5117 * @destination_bus_name: (allow-none): The unique bus name for the destination
5118 * for the signal or %NULL to emit to all listeners.
5119 * @object_path: Path of remote object.
5120 * @interface_name: D-Bus interface to emit a signal on.
5121 * @signal_name: The name of the signal to emit.
5122 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
5123 * or %NULL if not passing parameters.
5124 * @error: Return location for error or %NULL.
5128 * If the parameters GVariant is floating, it is consumed.
5130 * This can only fail if @parameters is not compatible with the D-Bus protocol.
5132 * Returns: %TRUE unless @error is set.
5137 g_dbus_connection_emit_signal (GDBusConnection
*connection
,
5138 const gchar
*destination_bus_name
,
5139 const gchar
*object_path
,
5140 const gchar
*interface_name
,
5141 const gchar
*signal_name
,
5142 GVariant
*parameters
,
5145 GDBusMessage
*message
;
5151 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
5152 g_return_val_if_fail (destination_bus_name
== NULL
|| g_dbus_is_name (destination_bus_name
), FALSE
);
5153 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), FALSE
);
5154 g_return_val_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
), FALSE
);
5155 g_return_val_if_fail (signal_name
!= NULL
&& g_dbus_is_member_name (signal_name
), FALSE
);
5156 g_return_val_if_fail (parameters
== NULL
|| g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
), FALSE
);
5157 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5159 if (G_UNLIKELY (_g_dbus_debug_emission ()))
5161 _g_dbus_debug_print_lock ();
5162 g_print ("========================================================================\n"
5163 "GDBus-debug:Emission:\n"
5164 " >>>> SIGNAL EMISSION %s.%s()\n"
5166 " destination %s\n",
5167 interface_name
, signal_name
,
5169 destination_bus_name
!= NULL
? destination_bus_name
: "(none)");
5170 _g_dbus_debug_print_unlock ();
5173 message
= g_dbus_message_new_signal (object_path
,
5177 if (destination_bus_name
!= NULL
)
5178 g_dbus_message_set_header (message
,
5179 G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
,
5180 g_variant_new_string (destination_bus_name
));
5182 if (parameters
!= NULL
)
5183 g_dbus_message_set_body (message
, parameters
);
5185 ret
= g_dbus_connection_send_message (connection
, message
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, error
);
5186 g_object_unref (message
);
5192 add_call_flags (GDBusMessage
*message
,
5193 GDBusCallFlags flags
)
5195 if (flags
& G_DBUS_CALL_FLAGS_NO_AUTO_START
)
5196 g_dbus_message_set_flags (message
, G_DBUS_MESSAGE_FLAGS_NO_AUTO_START
);
5200 decode_method_reply (GDBusMessage
*reply
,
5201 const gchar
*method_name
,
5202 const GVariantType
*reply_type
,
5203 GUnixFDList
**out_fd_list
,
5209 switch (g_dbus_message_get_message_type (reply
))
5211 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN
:
5212 result
= g_dbus_message_get_body (reply
);
5215 result
= g_variant_new ("()");
5216 g_variant_ref_sink (result
);
5220 g_variant_ref (result
);
5223 if (!g_variant_is_of_type (result
, reply_type
))
5225 gchar
*type_string
= g_variant_type_dup_string (reply_type
);
5229 G_IO_ERROR_INVALID_ARGUMENT
,
5230 _("Method `%s' returned type `%s', but expected `%s'"),
5231 method_name
, g_variant_get_type_string (result
), type_string
);
5233 g_variant_unref (result
);
5234 g_free (type_string
);
5241 if (out_fd_list
!= NULL
)
5243 *out_fd_list
= g_dbus_message_get_unix_fd_list (reply
);
5244 if (*out_fd_list
!= NULL
)
5245 g_object_ref (*out_fd_list
);
5251 case G_DBUS_MESSAGE_TYPE_ERROR
:
5252 g_dbus_message_to_gerror (reply
, error
);
5256 g_assert_not_reached ();
5266 GSimpleAsyncResult
*simple
;
5267 GVariantType
*reply_type
;
5268 gchar
*method_name
; /* for error message */
5272 GUnixFDList
*fd_list
;
5276 call_state_free (CallState
*state
)
5278 g_variant_type_free (state
->reply_type
);
5279 g_free (state
->method_name
);
5281 if (state
->value
!= NULL
)
5282 g_variant_unref (state
->value
);
5283 if (state
->fd_list
!= NULL
)
5284 g_object_unref (state
->fd_list
);
5285 g_slice_free (CallState
, state
);
5288 /* called in any thread, with the connection's lock not held */
5290 g_dbus_connection_call_done (GObject
*source
,
5291 GAsyncResult
*result
,
5294 GSimpleAsyncResult
*simple
;
5295 GDBusConnection
*connection
= G_DBUS_CONNECTION (source
);
5296 CallState
*state
= user_data
;
5298 GDBusMessage
*reply
;
5301 reply
= g_dbus_connection_send_message_with_reply_finish (connection
,
5305 if (G_UNLIKELY (_g_dbus_debug_call ()))
5307 _g_dbus_debug_print_lock ();
5308 g_print ("========================================================================\n"
5309 "GDBus-debug:Call:\n"
5310 " <<<< ASYNC COMPLETE %s() (serial %d)\n"
5316 g_print ("SUCCESS\n");
5320 g_print ("FAILED: %s\n",
5323 _g_dbus_debug_print_unlock ();
5327 state
->value
= decode_method_reply (reply
, state
->method_name
, state
->reply_type
, &state
->fd_list
, &error
);
5329 simple
= state
->simple
; /* why? because state is freed before we unref simple.. */
5332 g_simple_async_result_take_error (state
->simple
, error
);
5333 g_simple_async_result_complete (state
->simple
);
5334 call_state_free (state
);
5338 g_simple_async_result_set_op_res_gpointer (state
->simple
, state
, (GDestroyNotify
) call_state_free
);
5339 g_simple_async_result_complete (state
->simple
);
5341 g_clear_object (&reply
);
5342 g_object_unref (simple
);
5345 /* called in any thread, with the connection's lock not held */
5347 g_dbus_connection_call_internal (GDBusConnection
*connection
,
5348 const gchar
*bus_name
,
5349 const gchar
*object_path
,
5350 const gchar
*interface_name
,
5351 const gchar
*method_name
,
5352 GVariant
*parameters
,
5353 const GVariantType
*reply_type
,
5354 GDBusCallFlags flags
,
5356 GUnixFDList
*fd_list
,
5357 GCancellable
*cancellable
,
5358 GAsyncReadyCallback callback
,
5361 GDBusMessage
*message
;
5364 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
5365 g_return_if_fail (bus_name
== NULL
|| g_dbus_is_name (bus_name
));
5366 g_return_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
));
5367 g_return_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
));
5368 g_return_if_fail (method_name
!= NULL
&& g_dbus_is_member_name (method_name
));
5369 g_return_if_fail (timeout_msec
>= 0 || timeout_msec
== -1);
5370 g_return_if_fail ((parameters
== NULL
) || g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
));
5371 g_return_if_fail (check_initialized (connection
));
5373 g_return_if_fail (fd_list
== NULL
|| G_IS_UNIX_FD_LIST (fd_list
));
5375 g_return_if_fail (fd_list
== NULL
);
5378 message
= g_dbus_message_new_method_call (bus_name
,
5382 add_call_flags (message
, flags
);
5383 if (parameters
!= NULL
)
5384 g_dbus_message_set_body (message
, parameters
);
5387 if (fd_list
!= NULL
)
5388 g_dbus_message_set_unix_fd_list (message
, fd_list
);
5391 /* If the user has no callback then we can just send the message with
5392 * the G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set and skip all
5393 * the logic for processing the reply. If the service sends the reply
5394 * anyway then it will just be ignored.
5396 if (callback
!= NULL
)
5400 state
= g_slice_new0 (CallState
);
5401 state
->simple
= g_simple_async_result_new (G_OBJECT (connection
),
5402 callback
, user_data
,
5403 g_dbus_connection_call_internal
);
5404 g_simple_async_result_set_check_cancellable (state
->simple
, cancellable
);
5405 state
->method_name
= g_strjoin (".", interface_name
, method_name
, NULL
);
5407 if (reply_type
== NULL
)
5408 reply_type
= G_VARIANT_TYPE_ANY
;
5410 state
->reply_type
= g_variant_type_copy (reply_type
);
5412 g_dbus_connection_send_message_with_reply (connection
,
5414 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
5418 g_dbus_connection_call_done
,
5420 serial
= state
->serial
;
5424 GDBusMessageFlags flags
;
5426 flags
= g_dbus_message_get_flags (message
);
5427 flags
|= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
5428 g_dbus_message_set_flags (message
, flags
);
5430 g_dbus_connection_send_message (connection
,
5432 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
5436 if (G_UNLIKELY (_g_dbus_debug_call ()))
5438 _g_dbus_debug_print_lock ();
5439 g_print ("========================================================================\n"
5440 "GDBus-debug:Call:\n"
5441 " >>>> ASYNC %s.%s()\n"
5443 " owned by name %s (serial %d)\n",
5447 bus_name
!= NULL
? bus_name
: "(none)",
5449 _g_dbus_debug_print_unlock ();
5452 if (message
!= NULL
)
5453 g_object_unref (message
);
5456 /* called in any thread, with the connection's lock not held */
5458 g_dbus_connection_call_finish_internal (GDBusConnection
*connection
,
5459 GUnixFDList
**out_fd_list
,
5463 GSimpleAsyncResult
*simple
;
5466 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
5467 g_return_val_if_fail (g_simple_async_result_is_valid (res
, G_OBJECT (connection
),
5468 g_dbus_connection_call_internal
), NULL
);
5469 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
5471 simple
= G_SIMPLE_ASYNC_RESULT (res
);
5473 if (g_simple_async_result_propagate_error (simple
, error
))
5476 state
= g_simple_async_result_get_op_res_gpointer (simple
);
5477 if (out_fd_list
!= NULL
)
5478 *out_fd_list
= state
->fd_list
!= NULL
? g_object_ref (state
->fd_list
) : NULL
;
5479 return g_variant_ref (state
->value
);
5482 /* called in any user thread, with the connection's lock not held */
5484 g_dbus_connection_call_sync_internal (GDBusConnection
*connection
,
5485 const gchar
*bus_name
,
5486 const gchar
*object_path
,
5487 const gchar
*interface_name
,
5488 const gchar
*method_name
,
5489 GVariant
*parameters
,
5490 const GVariantType
*reply_type
,
5491 GDBusCallFlags flags
,
5493 GUnixFDList
*fd_list
,
5494 GUnixFDList
**out_fd_list
,
5495 GCancellable
*cancellable
,
5498 GDBusMessage
*message
;
5499 GDBusMessage
*reply
;
5501 GError
*local_error
;
5502 GDBusSendMessageFlags send_flags
;
5508 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
5509 g_return_val_if_fail (bus_name
== NULL
|| g_dbus_is_name (bus_name
), NULL
);
5510 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), NULL
);
5511 g_return_val_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
), NULL
);
5512 g_return_val_if_fail (method_name
!= NULL
&& g_dbus_is_member_name (method_name
), NULL
);
5513 g_return_val_if_fail (timeout_msec
>= 0 || timeout_msec
== -1, NULL
);
5514 g_return_val_if_fail ((parameters
== NULL
) || g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
), NULL
);
5516 g_return_val_if_fail (fd_list
== NULL
|| G_IS_UNIX_FD_LIST (fd_list
), NULL
);
5518 g_return_val_if_fail (fd_list
== NULL
, NULL
);
5520 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
5522 if (!(flags
& CALL_FLAGS_INITIALIZING
))
5523 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5525 if (reply_type
== NULL
)
5526 reply_type
= G_VARIANT_TYPE_ANY
;
5528 message
= g_dbus_message_new_method_call (bus_name
,
5532 add_call_flags (message
, flags
);
5533 if (parameters
!= NULL
)
5534 g_dbus_message_set_body (message
, parameters
);
5537 if (fd_list
!= NULL
)
5538 g_dbus_message_set_unix_fd_list (message
, fd_list
);
5541 if (G_UNLIKELY (_g_dbus_debug_call ()))
5543 _g_dbus_debug_print_lock ();
5544 g_print ("========================================================================\n"
5545 "GDBus-debug:Call:\n"
5546 " >>>> SYNC %s.%s()\n"
5548 " owned by name %s\n",
5552 bus_name
!= NULL
? bus_name
: "(none)");
5553 _g_dbus_debug_print_unlock ();
5558 send_flags
= G_DBUS_SEND_MESSAGE_FLAGS_NONE
;
5560 /* translate from one flavour of flags to another... */
5561 if (flags
& CALL_FLAGS_INITIALIZING
)
5562 send_flags
|= SEND_MESSAGE_FLAGS_INITIALIZING
;
5564 reply
= g_dbus_connection_send_message_with_reply_sync (connection
,
5568 NULL
, /* volatile guint32 *out_serial */
5572 if (G_UNLIKELY (_g_dbus_debug_call ()))
5574 _g_dbus_debug_print_lock ();
5575 g_print ("========================================================================\n"
5576 "GDBus-debug:Call:\n"
5577 " <<<< SYNC COMPLETE %s.%s()\n"
5583 g_print ("SUCCESS\n");
5587 g_print ("FAILED: %s\n",
5588 local_error
->message
);
5590 _g_dbus_debug_print_unlock ();
5596 *error
= local_error
;
5598 g_error_free (local_error
);
5602 result
= decode_method_reply (reply
, method_name
, reply_type
, out_fd_list
, error
);
5605 if (message
!= NULL
)
5606 g_object_unref (message
);
5608 g_object_unref (reply
);
5613 /* ---------------------------------------------------------------------------------------------------- */
5616 * g_dbus_connection_call:
5617 * @connection: A #GDBusConnection.
5618 * @bus_name: (allow-none): A unique or well-known bus name or %NULL if
5619 * @connection is not a message bus connection.
5620 * @object_path: Path of remote object.
5621 * @interface_name: D-Bus interface to invoke method on.
5622 * @method_name: The name of the method to invoke.
5623 * @parameters: (allow-none): A #GVariant tuple with parameters for the method
5624 * or %NULL if not passing parameters.
5625 * @reply_type: (allow-none): The expected type of the reply, or %NULL.
5626 * @flags: Flags from the #GDBusCallFlags enumeration.
5627 * @timeout_msec: The timeout in milliseconds, -1 to use the default
5628 * timeout or %G_MAXINT for no timeout.
5629 * @cancellable: (allow-none): A #GCancellable or %NULL.
5630 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
5631 * satisfied or %NULL if you don't care about the result of the
5632 * method invocation.
5633 * @user_data: The data to pass to @callback.
5635 * Asynchronously invokes the @method_name method on the
5636 * @interface_name D-Bus interface on the remote object at
5637 * @object_path owned by @bus_name.
5639 * If @connection is closed then the operation will fail with
5640 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
5641 * fail with %G_IO_ERROR_CANCELLED. If @parameters contains a value
5642 * not compatible with the D-Bus protocol, the operation fails with
5643 * %G_IO_ERROR_INVALID_ARGUMENT.
5645 * If @reply_type is non-%NULL then the reply will be checked for having this type and an
5646 * error will be raised if it does not match. Said another way, if you give a @reply_type
5647 * then any non-%NULL return value will be of this type.
5649 * If the @parameters #GVariant is floating, it is consumed. This allows
5650 * convenient 'inline' use of g_variant_new(), e.g.:
5652 * g_dbus_connection_call (connection,
5653 * "org.freedesktop.StringThings",
5654 * "/org/freedesktop/StringThings",
5655 * "org.freedesktop.StringThings",
5657 * g_variant_new ("(ss)",
5661 * G_DBUS_CALL_FLAGS_NONE,
5664 * (GAsyncReadyCallback) two_strings_done,
5668 * This is an asynchronous method. When the operation is finished, @callback will be invoked
5669 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
5670 * of the thread you are calling this method from. You can then call
5671 * g_dbus_connection_call_finish() to get the result of the operation.
5672 * See g_dbus_connection_call_sync() for the synchronous version of this
5675 * If @callback is %NULL then the D-Bus method call message will be sent with
5676 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
5681 g_dbus_connection_call (GDBusConnection
*connection
,
5682 const gchar
*bus_name
,
5683 const gchar
*object_path
,
5684 const gchar
*interface_name
,
5685 const gchar
*method_name
,
5686 GVariant
*parameters
,
5687 const GVariantType
*reply_type
,
5688 GDBusCallFlags flags
,
5690 GCancellable
*cancellable
,
5691 GAsyncReadyCallback callback
,
5694 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
);
5698 * g_dbus_connection_call_finish:
5699 * @connection: A #GDBusConnection.
5700 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call().
5701 * @error: Return location for error or %NULL.
5703 * Finishes an operation started with g_dbus_connection_call().
5705 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
5706 * return values. Free with g_variant_unref().
5711 g_dbus_connection_call_finish (GDBusConnection
*connection
,
5715 return g_dbus_connection_call_finish_internal (connection
, NULL
, res
, error
);
5719 * g_dbus_connection_call_sync:
5720 * @connection: A #GDBusConnection.
5721 * @bus_name: (allow-none): A unique or well-known bus name or %NULL if
5722 * @connection is not a message bus connection.
5723 * @object_path: Path of remote object.
5724 * @interface_name: D-Bus interface to invoke method on.
5725 * @method_name: The name of the method to invoke.
5726 * @parameters: (allow-none): A #GVariant tuple with parameters for the method
5727 * or %NULL if not passing parameters.
5728 * @reply_type: (allow-none): The expected type of the reply, or %NULL.
5729 * @flags: Flags from the #GDBusCallFlags enumeration.
5730 * @timeout_msec: The timeout in milliseconds, -1 to use the default
5731 * timeout or %G_MAXINT for no timeout.
5732 * @cancellable: (allow-none): A #GCancellable or %NULL.
5733 * @error: Return location for error or %NULL.
5735 * Synchronously invokes the @method_name method on the
5736 * @interface_name D-Bus interface on the remote object at
5737 * @object_path owned by @bus_name.
5739 * If @connection is closed then the operation will fail with
5740 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the
5741 * operation will fail with %G_IO_ERROR_CANCELLED. If @parameters
5742 * contains a value not compatible with the D-Bus protocol, the operation
5743 * fails with %G_IO_ERROR_INVALID_ARGUMENT.
5745 * If @reply_type is non-%NULL then the reply will be checked for having
5746 * this type and an error will be raised if it does not match. Said
5747 * another way, if you give a @reply_type then any non-%NULL return
5748 * value will be of this type.
5750 * If the @parameters #GVariant is floating, it is consumed.
5751 * This allows convenient 'inline' use of g_variant_new(), e.g.:
5753 * g_dbus_connection_call_sync (connection,
5754 * "org.freedesktop.StringThings",
5755 * "/org/freedesktop/StringThings",
5756 * "org.freedesktop.StringThings",
5758 * g_variant_new ("(ss)",
5762 * G_DBUS_CALL_FLAGS_NONE,
5768 * The calling thread is blocked until a reply is received. See
5769 * g_dbus_connection_call() for the asynchronous version of
5772 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
5773 * return values. Free with g_variant_unref().
5778 g_dbus_connection_call_sync (GDBusConnection
*connection
,
5779 const gchar
*bus_name
,
5780 const gchar
*object_path
,
5781 const gchar
*interface_name
,
5782 const gchar
*method_name
,
5783 GVariant
*parameters
,
5784 const GVariantType
*reply_type
,
5785 GDBusCallFlags flags
,
5787 GCancellable
*cancellable
,
5790 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
);
5793 /* ---------------------------------------------------------------------------------------------------- */
5798 * g_dbus_connection_call_with_unix_fd_list:
5799 * @connection: A #GDBusConnection.
5800 * @bus_name: (allow-none): A unique or well-known bus name or %NULL if
5801 * @connection is not a message bus connection.
5802 * @object_path: Path of remote object.
5803 * @interface_name: D-Bus interface to invoke method on.
5804 * @method_name: The name of the method to invoke.
5805 * @parameters: (allow-none): A #GVariant tuple with parameters for the method
5806 * or %NULL if not passing parameters.
5807 * @reply_type: (allow-none): The expected type of the reply, or %NULL.
5808 * @flags: Flags from the #GDBusCallFlags enumeration.
5809 * @timeout_msec: The timeout in milliseconds, -1 to use the default
5810 * timeout or %G_MAXINT for no timeout.
5811 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
5812 * @cancellable: (allow-none): A #GCancellable or %NULL.
5813 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
5814 * satisfied or %NULL if you don't * care about the result of the
5815 * method invocation.
5816 * @user_data: The data to pass to @callback.
5818 * Like g_dbus_connection_call() but also takes a #GUnixFDList object.
5820 * This method is only available on UNIX.
5825 g_dbus_connection_call_with_unix_fd_list (GDBusConnection
*connection
,
5826 const gchar
*bus_name
,
5827 const gchar
*object_path
,
5828 const gchar
*interface_name
,
5829 const gchar
*method_name
,
5830 GVariant
*parameters
,
5831 const GVariantType
*reply_type
,
5832 GDBusCallFlags flags
,
5834 GUnixFDList
*fd_list
,
5835 GCancellable
*cancellable
,
5836 GAsyncReadyCallback callback
,
5839 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
);
5843 * g_dbus_connection_call_with_unix_fd_list_finish:
5844 * @connection: A #GDBusConnection.
5845 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
5846 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call_with_unix_fd_list().
5847 * @error: Return location for error or %NULL.
5849 * Finishes an operation started with g_dbus_connection_call_with_unix_fd_list().
5851 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
5852 * return values. Free with g_variant_unref().
5857 g_dbus_connection_call_with_unix_fd_list_finish (GDBusConnection
*connection
,
5858 GUnixFDList
**out_fd_list
,
5862 return g_dbus_connection_call_finish_internal (connection
, out_fd_list
, res
, error
);
5866 * g_dbus_connection_call_with_unix_fd_list_sync:
5867 * @connection: A #GDBusConnection.
5868 * @bus_name: (allow-none): A unique or well-known bus name or %NULL if
5869 * @connection is not a message bus connection.
5870 * @object_path: Path of remote object.
5871 * @interface_name: D-Bus interface to invoke method on.
5872 * @method_name: The name of the method to invoke.
5873 * @parameters: (allow-none): A #GVariant tuple with parameters for the method
5874 * or %NULL if not passing parameters.
5875 * @reply_type: (allow-none): The expected type of the reply, or %NULL.
5876 * @flags: Flags from the #GDBusCallFlags enumeration.
5877 * @timeout_msec: The timeout in milliseconds, -1 to use the default
5878 * timeout or %G_MAXINT for no timeout.
5879 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
5880 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
5881 * @cancellable: (allow-none): A #GCancellable or %NULL.
5882 * @error: Return location for error or %NULL.
5884 * Like g_dbus_connection_call_sync() but also takes and returns #GUnixFDList objects.
5886 * This method is only available on UNIX.
5888 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
5889 * return values. Free with g_variant_unref().
5894 g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection
*connection
,
5895 const gchar
*bus_name
,
5896 const gchar
*object_path
,
5897 const gchar
*interface_name
,
5898 const gchar
*method_name
,
5899 GVariant
*parameters
,
5900 const GVariantType
*reply_type
,
5901 GDBusCallFlags flags
,
5903 GUnixFDList
*fd_list
,
5904 GUnixFDList
**out_fd_list
,
5905 GCancellable
*cancellable
,
5908 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
);
5911 #endif /* G_OS_UNIX */
5913 /* ---------------------------------------------------------------------------------------------------- */
5915 struct ExportedSubtree
5919 GDBusConnection
*connection
;
5920 GDBusSubtreeVTable
*vtable
;
5921 GDBusSubtreeFlags flags
;
5923 GMainContext
*context
;
5925 GDestroyNotify user_data_free_func
;
5929 exported_subtree_free (ExportedSubtree
*es
)
5931 call_destroy_notify (es
->context
,
5932 es
->user_data_free_func
,
5935 g_main_context_unref (es
->context
);
5937 _g_dbus_subtree_vtable_free (es
->vtable
);
5938 g_free (es
->object_path
);
5942 /* called without lock held in the thread where the caller registered
5946 handle_subtree_introspect (GDBusConnection
*connection
,
5947 ExportedSubtree
*es
,
5948 GDBusMessage
*message
)
5952 GDBusMessage
*reply
;
5955 const gchar
*sender
;
5956 const gchar
*requested_object_path
;
5957 const gchar
*requested_node
;
5958 GDBusInterfaceInfo
**interfaces
;
5960 gchar
**subnode_paths
;
5961 gboolean has_properties_interface
;
5962 gboolean has_introspectable_interface
;
5966 requested_object_path
= g_dbus_message_get_path (message
);
5967 sender
= g_dbus_message_get_sender (message
);
5968 is_root
= (g_strcmp0 (requested_object_path
, es
->object_path
) == 0);
5970 s
= g_string_new (NULL
);
5971 introspect_append_header (s
);
5973 /* Strictly we don't need the children in dynamic mode, but we avoid the
5974 * conditionals to preserve code clarity
5976 children
= es
->vtable
->enumerate (es
->connection
,
5983 requested_node
= strrchr (requested_object_path
, '/') + 1;
5985 /* Assert existence of object if we are not dynamic */
5986 if (!(es
->flags
& G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES
) &&
5987 !_g_strv_has_string ((const gchar
* const *) children
, requested_node
))
5992 requested_node
= NULL
;
5995 interfaces
= es
->vtable
->introspect (es
->connection
,
6000 if (interfaces
!= NULL
)
6002 has_properties_interface
= FALSE
;
6003 has_introspectable_interface
= FALSE
;
6005 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6007 if (strcmp (interfaces
[n
]->name
, "org.freedesktop.DBus.Properties") == 0)
6008 has_properties_interface
= TRUE
;
6009 else if (strcmp (interfaces
[n
]->name
, "org.freedesktop.DBus.Introspectable") == 0)
6010 has_introspectable_interface
= TRUE
;
6012 if (!has_properties_interface
)
6013 g_string_append (s
, introspect_properties_interface
);
6014 if (!has_introspectable_interface
)
6015 g_string_append (s
, introspect_introspectable_interface
);
6017 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6019 g_dbus_interface_info_generate_xml (interfaces
[n
], 2, s
);
6020 g_dbus_interface_info_unref (interfaces
[n
]);
6022 g_free (interfaces
);
6025 /* then include <node> entries from the Subtree for the root */
6028 for (n
= 0; children
!= NULL
&& children
[n
] != NULL
; n
++)
6029 g_string_append_printf (s
, " <node name=\"%s\"/>\n", children
[n
]);
6032 /* finally include nodes registered below us */
6033 subnode_paths
= g_dbus_connection_list_registered (es
->connection
, requested_object_path
);
6034 for (n
= 0; subnode_paths
!= NULL
&& subnode_paths
[n
] != NULL
; n
++)
6035 g_string_append_printf (s
, " <node name=\"%s\"/>\n", subnode_paths
[n
]);
6036 g_strfreev (subnode_paths
);
6038 g_string_append (s
, "</node>\n");
6040 reply
= g_dbus_message_new_method_reply (message
);
6041 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
6042 g_dbus_connection_send_message (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6043 g_object_unref (reply
);
6048 g_string_free (s
, TRUE
);
6049 g_strfreev (children
);
6053 /* called without lock held in the thread where the caller registered
6057 handle_subtree_method_invocation (GDBusConnection
*connection
,
6058 ExportedSubtree
*es
,
6059 GDBusMessage
*message
)
6062 const gchar
*sender
;
6063 const gchar
*interface_name
;
6064 const gchar
*member
;
6065 const gchar
*signature
;
6066 const gchar
*requested_object_path
;
6067 const gchar
*requested_node
;
6069 GDBusInterfaceInfo
*interface_info
;
6070 const GDBusInterfaceVTable
*interface_vtable
;
6071 gpointer interface_user_data
;
6073 GDBusInterfaceInfo
**interfaces
;
6074 gboolean is_property_get
;
6075 gboolean is_property_set
;
6076 gboolean is_property_get_all
;
6081 requested_object_path
= g_dbus_message_get_path (message
);
6082 sender
= g_dbus_message_get_sender (message
);
6083 interface_name
= g_dbus_message_get_interface (message
);
6084 member
= g_dbus_message_get_member (message
);
6085 signature
= g_dbus_message_get_signature (message
);
6086 is_root
= (g_strcmp0 (requested_object_path
, es
->object_path
) == 0);
6088 is_property_get
= FALSE
;
6089 is_property_set
= FALSE
;
6090 is_property_get_all
= FALSE
;
6091 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0)
6093 if (g_strcmp0 (member
, "Get") == 0 && g_strcmp0 (signature
, "ss") == 0)
6094 is_property_get
= TRUE
;
6095 else if (g_strcmp0 (member
, "Set") == 0 && g_strcmp0 (signature
, "ssv") == 0)
6096 is_property_set
= TRUE
;
6097 else if (g_strcmp0 (member
, "GetAll") == 0 && g_strcmp0 (signature
, "s") == 0)
6098 is_property_get_all
= TRUE
;
6103 requested_node
= strrchr (requested_object_path
, '/') + 1;
6105 if (~es
->flags
& G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES
)
6107 /* We don't want to dispatch to unenumerated
6108 * nodes, so ensure that the child exists.
6113 children
= es
->vtable
->enumerate (es
->connection
,
6118 exists
= _g_strv_has_string ((const gchar
* const *) children
, requested_node
);
6119 g_strfreev (children
);
6127 requested_node
= NULL
;
6130 /* get introspection data for the node */
6131 interfaces
= es
->vtable
->introspect (es
->connection
,
6133 requested_object_path
,
6137 if (interfaces
== NULL
)
6140 interface_info
= NULL
;
6141 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6143 if (g_strcmp0 (interfaces
[n
]->name
, interface_name
) == 0)
6144 interface_info
= interfaces
[n
];
6147 /* dispatch the call if the user wants to handle it */
6148 if (interface_info
!= NULL
)
6150 /* figure out where to dispatch the method call */
6151 interface_user_data
= NULL
;
6152 interface_vtable
= es
->vtable
->dispatch (es
->connection
,
6157 &interface_user_data
,
6159 if (interface_vtable
== NULL
)
6162 CONNECTION_LOCK (connection
);
6163 handled
= validate_and_maybe_schedule_method_call (es
->connection
,
6170 interface_user_data
);
6171 CONNECTION_UNLOCK (connection
);
6173 /* handle org.freedesktop.DBus.Properties interface if not explicitly handled */
6174 else if (is_property_get
|| is_property_set
|| is_property_get_all
)
6176 if (is_property_get
)
6177 g_variant_get (g_dbus_message_get_body (message
), "(&s&s)", &interface_name
, NULL
);
6178 else if (is_property_set
)
6179 g_variant_get (g_dbus_message_get_body (message
), "(&s&sv)", &interface_name
, NULL
, NULL
);
6180 else if (is_property_get_all
)
6181 g_variant_get (g_dbus_message_get_body (message
), "(&s)", &interface_name
, NULL
, NULL
);
6183 g_assert_not_reached ();
6185 /* see if the object supports this interface at all */
6186 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6188 if (g_strcmp0 (interfaces
[n
]->name
, interface_name
) == 0)
6189 interface_info
= interfaces
[n
];
6192 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the user-code
6193 * claims it won't support the interface
6195 if (interface_info
== NULL
)
6197 GDBusMessage
*reply
;
6198 reply
= g_dbus_message_new_method_error (message
,
6199 "org.freedesktop.DBus.Error.InvalidArgs",
6200 _("No such interface `%s'"),
6202 g_dbus_connection_send_message (es
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6203 g_object_unref (reply
);
6208 /* figure out where to dispatch the property get/set/getall calls */
6209 interface_user_data
= NULL
;
6210 interface_vtable
= es
->vtable
->dispatch (es
->connection
,
6215 &interface_user_data
,
6217 if (interface_vtable
== NULL
)
6219 g_warning ("The subtree introspection function indicates that '%s' "
6220 "is a valid interface name, but calling the dispatch "
6221 "function on that interface gave us NULL", interface_name
);
6225 if (is_property_get
|| is_property_set
)
6227 CONNECTION_LOCK (connection
);
6228 handled
= validate_and_maybe_schedule_property_getset (es
->connection
,
6236 interface_user_data
);
6237 CONNECTION_UNLOCK (connection
);
6239 else if (is_property_get_all
)
6241 CONNECTION_LOCK (connection
);
6242 handled
= validate_and_maybe_schedule_property_get_all (es
->connection
,
6249 interface_user_data
);
6250 CONNECTION_UNLOCK (connection
);
6255 if (interfaces
!= NULL
)
6257 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6258 g_dbus_interface_info_unref (interfaces
[n
]);
6259 g_free (interfaces
);
6267 GDBusMessage
*message
;
6268 ExportedSubtree
*es
;
6269 } SubtreeDeferredData
;
6272 subtree_deferred_data_free (SubtreeDeferredData
*data
)
6274 g_object_unref (data
->message
);
6278 /* called without lock held in the thread where the caller registered the subtree */
6280 process_subtree_vtable_message_in_idle_cb (gpointer _data
)
6282 SubtreeDeferredData
*data
= _data
;
6287 if (g_strcmp0 (g_dbus_message_get_interface (data
->message
), "org.freedesktop.DBus.Introspectable") == 0 &&
6288 g_strcmp0 (g_dbus_message_get_member (data
->message
), "Introspect") == 0 &&
6289 g_strcmp0 (g_dbus_message_get_signature (data
->message
), "") == 0)
6290 handled
= handle_subtree_introspect (data
->es
->connection
,
6294 handled
= handle_subtree_method_invocation (data
->es
->connection
,
6300 CONNECTION_LOCK (data
->es
->connection
);
6301 handled
= handle_generic_unlocked (data
->es
->connection
, data
->message
);
6302 CONNECTION_UNLOCK (data
->es
->connection
);
6305 /* if we couldn't handle the request, just bail with the UnknownMethod error */
6308 GDBusMessage
*reply
;
6309 reply
= g_dbus_message_new_method_error (data
->message
,
6310 "org.freedesktop.DBus.Error.UnknownMethod",
6311 _("Method `%s' on interface `%s' with signature `%s' does not exist"),
6312 g_dbus_message_get_member (data
->message
),
6313 g_dbus_message_get_interface (data
->message
),
6314 g_dbus_message_get_signature (data
->message
));
6315 g_dbus_connection_send_message (data
->es
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6316 g_object_unref (reply
);
6322 /* called in GDBusWorker thread with connection's lock held */
6324 subtree_message_func (GDBusConnection
*connection
,
6325 ExportedSubtree
*es
,
6326 GDBusMessage
*message
)
6328 GSource
*idle_source
;
6329 SubtreeDeferredData
*data
;
6331 data
= g_new0 (SubtreeDeferredData
, 1);
6332 data
->message
= g_object_ref (message
);
6335 /* defer this call to an idle handler in the right thread */
6336 idle_source
= g_idle_source_new ();
6337 g_source_set_priority (idle_source
, G_PRIORITY_HIGH
);
6338 g_source_set_callback (idle_source
,
6339 process_subtree_vtable_message_in_idle_cb
,
6341 (GDestroyNotify
) subtree_deferred_data_free
);
6342 g_source_attach (idle_source
, es
->context
);
6343 g_source_unref (idle_source
);
6345 /* since we own the entire subtree, handlers for objects not in the subtree have been
6346 * tried already by libdbus-1 - so we just need to ensure that we're always going
6347 * to reply to the message
6353 * g_dbus_connection_register_subtree:
6354 * @connection: A #GDBusConnection.
6355 * @object_path: The object path to register the subtree at.
6356 * @vtable: A #GDBusSubtreeVTable to enumerate, introspect and dispatch nodes in the subtree.
6357 * @flags: Flags used to fine tune the behavior of the subtree.
6358 * @user_data: Data to pass to functions in @vtable.
6359 * @user_data_free_func: Function to call when the subtree is unregistered.
6360 * @error: Return location for error or %NULL.
6362 * Registers a whole subtree of <quote>dynamic</quote> objects.
6364 * The @enumerate and @introspection functions in @vtable are used to
6365 * convey, to remote callers, what nodes exist in the subtree rooted
6368 * When handling remote calls into any node in the subtree, first the
6369 * @enumerate function is used to check if the node exists. If the node exists
6370 * or the #G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is set
6371 * the @introspection function is used to check if the node supports the
6372 * requested method. If so, the @dispatch function is used to determine
6373 * where to dispatch the call. The collected #GDBusInterfaceVTable and
6374 * #gpointer will be used to call into the interface vtable for processing
6377 * All calls into user-provided code will be invoked in the <link
6378 * linkend="g-main-context-push-thread-default">thread-default main
6379 * loop</link> of the thread you are calling this method from.
6381 * If an existing subtree is already registered at @object_path or
6382 * then @error is set to #G_IO_ERROR_EXISTS.
6384 * Note that it is valid to register regular objects (using
6385 * g_dbus_connection_register_object()) in a subtree registered with
6386 * g_dbus_connection_register_subtree() - if so, the subtree handler
6387 * is tried as the last resort. One way to think about a subtree
6388 * handler is to consider it a <quote>fallback handler</quote>
6389 * for object paths not registered via g_dbus_connection_register_object()
6390 * or other bindings.
6392 * Note that @vtable will be copied so you cannot change it after
6395 * See <xref linkend="gdbus-subtree-server"/> for an example of how to use this method.
6397 * Returns: 0 if @error is set, otherwise a subtree registration id (never 0)
6398 * that can be used with g_dbus_connection_unregister_subtree() .
6403 g_dbus_connection_register_subtree (GDBusConnection
*connection
,
6404 const gchar
*object_path
,
6405 const GDBusSubtreeVTable
*vtable
,
6406 GDBusSubtreeFlags flags
,
6408 GDestroyNotify user_data_free_func
,
6412 ExportedSubtree
*es
;
6414 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
6415 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), 0);
6416 g_return_val_if_fail (vtable
!= NULL
, 0);
6417 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, 0);
6418 g_return_val_if_fail (check_initialized (connection
), 0);
6422 CONNECTION_LOCK (connection
);
6424 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, object_path
);
6430 _("A subtree is already exported for %s"),
6435 es
= g_new0 (ExportedSubtree
, 1);
6436 es
->object_path
= g_strdup (object_path
);
6437 es
->connection
= connection
;
6439 es
->vtable
= _g_dbus_subtree_vtable_copy (vtable
);
6441 es
->id
= _global_subtree_registration_id
++; /* TODO: overflow etc. */
6442 es
->user_data
= user_data
;
6443 es
->user_data_free_func
= user_data_free_func
;
6444 es
->context
= g_main_context_ref_thread_default ();
6446 g_hash_table_insert (connection
->map_object_path_to_es
, es
->object_path
, es
);
6447 g_hash_table_insert (connection
->map_id_to_es
,
6448 GUINT_TO_POINTER (es
->id
),
6454 CONNECTION_UNLOCK (connection
);
6459 /* ---------------------------------------------------------------------------------------------------- */
6462 * g_dbus_connection_unregister_subtree:
6463 * @connection: A #GDBusConnection.
6464 * @registration_id: A subtree registration id obtained from g_dbus_connection_register_subtree().
6466 * Unregisters a subtree.
6468 * Returns: %TRUE if the subtree was unregistered, %FALSE otherwise.
6473 g_dbus_connection_unregister_subtree (GDBusConnection
*connection
,
6474 guint registration_id
)
6476 ExportedSubtree
*es
;
6479 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
6480 g_return_val_if_fail (check_initialized (connection
), FALSE
);
6484 CONNECTION_LOCK (connection
);
6486 es
= g_hash_table_lookup (connection
->map_id_to_es
,
6487 GUINT_TO_POINTER (registration_id
));
6491 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_es
, GUINT_TO_POINTER (es
->id
)));
6492 g_warn_if_fail (g_hash_table_remove (connection
->map_object_path_to_es
, es
->object_path
));
6497 CONNECTION_UNLOCK (connection
);
6502 /* ---------------------------------------------------------------------------------------------------- */
6504 /* may be called in any thread, with connection's lock held */
6506 handle_generic_ping_unlocked (GDBusConnection
*connection
,
6507 const gchar
*object_path
,
6508 GDBusMessage
*message
)
6510 GDBusMessage
*reply
;
6511 reply
= g_dbus_message_new_method_reply (message
);
6512 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6513 g_object_unref (reply
);
6516 /* may be called in any thread, with connection's lock held */
6518 handle_generic_get_machine_id_unlocked (GDBusConnection
*connection
,
6519 const gchar
*object_path
,
6520 GDBusMessage
*message
)
6522 GDBusMessage
*reply
;
6525 if (connection
->machine_id
== NULL
)
6530 connection
->machine_id
= _g_dbus_get_machine_id (&error
);
6531 if (connection
->machine_id
== NULL
)
6533 reply
= g_dbus_message_new_method_error_literal (message
,
6534 "org.freedesktop.DBus.Error.Failed",
6536 g_error_free (error
);
6542 reply
= g_dbus_message_new_method_reply (message
);
6543 g_dbus_message_set_body (reply
, g_variant_new ("(s)", connection
->machine_id
));
6545 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6546 g_object_unref (reply
);
6549 /* may be called in any thread, with connection's lock held */
6551 handle_generic_introspect_unlocked (GDBusConnection
*connection
,
6552 const gchar
*object_path
,
6553 GDBusMessage
*message
)
6558 GDBusMessage
*reply
;
6560 /* first the header */
6561 s
= g_string_new (NULL
);
6562 introspect_append_header (s
);
6564 registered
= g_dbus_connection_list_registered_unlocked (connection
, object_path
);
6565 for (n
= 0; registered
!= NULL
&& registered
[n
] != NULL
; n
++)
6566 g_string_append_printf (s
, " <node name=\"%s\"/>\n", registered
[n
]);
6567 g_strfreev (registered
);
6568 g_string_append (s
, "</node>\n");
6570 reply
= g_dbus_message_new_method_reply (message
);
6571 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
6572 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6573 g_object_unref (reply
);
6574 g_string_free (s
, TRUE
);
6577 /* may be called in any thread, with connection's lock held */
6579 handle_generic_unlocked (GDBusConnection
*connection
,
6580 GDBusMessage
*message
)
6583 const gchar
*interface_name
;
6584 const gchar
*member
;
6585 const gchar
*signature
;
6588 CONNECTION_ENSURE_LOCK (connection
);
6592 interface_name
= g_dbus_message_get_interface (message
);
6593 member
= g_dbus_message_get_member (message
);
6594 signature
= g_dbus_message_get_signature (message
);
6595 path
= g_dbus_message_get_path (message
);
6597 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Introspectable") == 0 &&
6598 g_strcmp0 (member
, "Introspect") == 0 &&
6599 g_strcmp0 (signature
, "") == 0)
6601 handle_generic_introspect_unlocked (connection
, path
, message
);
6604 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Peer") == 0 &&
6605 g_strcmp0 (member
, "Ping") == 0 &&
6606 g_strcmp0 (signature
, "") == 0)
6608 handle_generic_ping_unlocked (connection
, path
, message
);
6611 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Peer") == 0 &&
6612 g_strcmp0 (member
, "GetMachineId") == 0 &&
6613 g_strcmp0 (signature
, "") == 0)
6615 handle_generic_get_machine_id_unlocked (connection
, path
, message
);
6622 /* ---------------------------------------------------------------------------------------------------- */
6624 /* called in GDBusWorker thread with connection's lock held */
6626 distribute_method_call (GDBusConnection
*connection
,
6627 GDBusMessage
*message
)
6629 GDBusMessage
*reply
;
6631 ExportedSubtree
*es
;
6632 const gchar
*object_path
;
6633 const gchar
*interface_name
;
6634 const gchar
*member
;
6636 gchar
*subtree_path
;
6639 g_assert (g_dbus_message_get_message_type (message
) == G_DBUS_MESSAGE_TYPE_METHOD_CALL
);
6641 interface_name
= g_dbus_message_get_interface (message
);
6642 member
= g_dbus_message_get_member (message
);
6643 path
= g_dbus_message_get_path (message
);
6644 subtree_path
= g_strdup (path
);
6645 needle
= strrchr (subtree_path
, '/');
6646 if (needle
!= NULL
&& needle
!= subtree_path
)
6652 g_free (subtree_path
);
6653 subtree_path
= NULL
;
6657 if (G_UNLIKELY (_g_dbus_debug_incoming ()))
6659 _g_dbus_debug_print_lock ();
6660 g_print ("========================================================================\n"
6661 "GDBus-debug:Incoming:\n"
6662 " <<<< METHOD INVOCATION %s.%s()\n"
6664 " invoked by name %s\n"
6666 interface_name
, member
,
6668 g_dbus_message_get_sender (message
) != NULL
? g_dbus_message_get_sender (message
) : "(none)",
6669 g_dbus_message_get_serial (message
));
6670 _g_dbus_debug_print_unlock ();
6673 object_path
= g_dbus_message_get_path (message
);
6674 g_assert (object_path
!= NULL
);
6676 eo
= g_hash_table_lookup (connection
->map_object_path_to_eo
, object_path
);
6679 if (obj_message_func (connection
, eo
, message
))
6683 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, object_path
);
6686 if (subtree_message_func (connection
, es
, message
))
6690 if (subtree_path
!= NULL
)
6692 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, subtree_path
);
6695 if (subtree_message_func (connection
, es
, message
))
6700 if (handle_generic_unlocked (connection
, message
))
6703 /* if we end up here, the message has not been not handled - so return an error saying this */
6704 reply
= g_dbus_message_new_method_error (message
,
6705 "org.freedesktop.DBus.Error.UnknownMethod",
6706 _("No such interface `%s' on object at path %s"),
6709 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6710 g_object_unref (reply
);
6713 g_free (subtree_path
);
6716 /* ---------------------------------------------------------------------------------------------------- */
6718 /* Called in any user thread, with the message_bus_lock held. */
6720 message_bus_get_singleton (GBusType bus_type
,
6724 const gchar
*starter_bus
;
6730 case G_BUS_TYPE_SESSION
:
6731 ret
= &the_session_bus
;
6734 case G_BUS_TYPE_SYSTEM
:
6735 ret
= &the_system_bus
;
6738 case G_BUS_TYPE_STARTER
:
6739 starter_bus
= g_getenv ("DBUS_STARTER_BUS_TYPE");
6740 if (g_strcmp0 (starter_bus
, "session") == 0)
6742 ret
= message_bus_get_singleton (G_BUS_TYPE_SESSION
, error
);
6745 else if (g_strcmp0 (starter_bus
, "system") == 0)
6747 ret
= message_bus_get_singleton (G_BUS_TYPE_SYSTEM
, error
);
6752 if (starter_bus
!= NULL
)
6756 G_IO_ERROR_INVALID_ARGUMENT
,
6757 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
6758 " - unknown value `%s'"),
6763 g_set_error_literal (error
,
6765 G_IO_ERROR_INVALID_ARGUMENT
,
6766 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
6767 "variable is not set"));
6773 g_assert_not_reached ();
6781 /* Called in any user thread, without holding locks. */
6782 static GDBusConnection
*
6783 get_uninitialized_connection (GBusType bus_type
,
6784 GCancellable
*cancellable
,
6787 GWeakRef
*singleton
;
6788 GDBusConnection
*ret
;
6792 G_LOCK (message_bus_lock
);
6793 singleton
= message_bus_get_singleton (bus_type
, error
);
6794 if (singleton
== NULL
)
6797 ret
= g_weak_ref_get (singleton
);
6802 address
= g_dbus_address_get_for_bus_sync (bus_type
, cancellable
, error
);
6803 if (address
== NULL
)
6805 ret
= g_object_new (G_TYPE_DBUS_CONNECTION
,
6807 "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
|
6808 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
,
6809 "exit-on-close", TRUE
,
6812 g_weak_ref_set (singleton
, ret
);
6816 g_assert (ret
!= NULL
);
6819 G_UNLOCK (message_bus_lock
);
6823 /* May be called from any thread. Must not hold message_bus_lock. */
6825 _g_bus_get_singleton_if_exists (GBusType bus_type
)
6827 GWeakRef
*singleton
;
6828 GDBusConnection
*ret
= NULL
;
6830 G_LOCK (message_bus_lock
);
6831 singleton
= message_bus_get_singleton (bus_type
, NULL
);
6832 if (singleton
== NULL
)
6835 ret
= g_weak_ref_get (singleton
);
6838 G_UNLOCK (message_bus_lock
);
6844 * @bus_type: A #GBusType.
6845 * @cancellable: (allow-none): A #GCancellable or %NULL.
6846 * @error: Return location for error or %NULL.
6848 * Synchronously connects to the message bus specified by @bus_type.
6849 * Note that the returned object may shared with other callers,
6850 * e.g. if two separate parts of a process calls this function with
6851 * the same @bus_type, they will share the same object.
6853 * This is a synchronous failable function. See g_bus_get() and
6854 * g_bus_get_finish() for the asynchronous version.
6856 * The returned object is a singleton, that is, shared with other
6857 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
6858 * event that you need a private message bus connection, use
6859 * g_dbus_address_get_for_bus_sync() and
6860 * g_dbus_connection_new_for_address().
6862 * Note that the returned #GDBusConnection object will (usually) have
6863 * the #GDBusConnection:exit-on-close property set to %TRUE.
6865 * Returns: (transfer full): A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
6870 g_bus_get_sync (GBusType bus_type
,
6871 GCancellable
*cancellable
,
6874 GDBusConnection
*connection
;
6876 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
6878 connection
= get_uninitialized_connection (bus_type
, cancellable
, error
);
6879 if (connection
== NULL
)
6882 if (!g_initable_init (G_INITABLE (connection
), cancellable
, error
))
6884 g_object_unref (connection
);
6893 bus_get_async_initable_cb (GObject
*source_object
,
6897 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (user_data
);
6901 if (!g_async_initable_init_finish (G_ASYNC_INITABLE (source_object
),
6905 g_assert (error
!= NULL
);
6906 g_simple_async_result_take_error (simple
, error
);
6907 g_object_unref (source_object
);
6911 g_simple_async_result_set_op_res_gpointer (simple
,
6915 g_simple_async_result_complete_in_idle (simple
);
6916 g_object_unref (simple
);
6921 * @bus_type: A #GBusType.
6922 * @cancellable: (allow-none): A #GCancellable or %NULL.
6923 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
6924 * @user_data: The data to pass to @callback.
6926 * Asynchronously connects to the message bus specified by @bus_type.
6928 * When the operation is finished, @callback will be invoked. You can
6929 * then call g_bus_get_finish() to get the result of the operation.
6931 * This is a asynchronous failable function. See g_bus_get_sync() for
6932 * the synchronous version.
6937 g_bus_get (GBusType bus_type
,
6938 GCancellable
*cancellable
,
6939 GAsyncReadyCallback callback
,
6942 GDBusConnection
*connection
;
6943 GSimpleAsyncResult
*simple
;
6946 simple
= g_simple_async_result_new (NULL
,
6950 g_simple_async_result_set_check_cancellable (simple
, cancellable
);
6953 connection
= get_uninitialized_connection (bus_type
, cancellable
, &error
);
6954 if (connection
== NULL
)
6956 g_assert (error
!= NULL
);
6957 g_simple_async_result_take_error (simple
, error
);
6958 g_simple_async_result_complete_in_idle (simple
);
6959 g_object_unref (simple
);
6963 g_async_initable_init_async (G_ASYNC_INITABLE (connection
),
6966 bus_get_async_initable_cb
,
6973 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_bus_get().
6974 * @error: Return location for error or %NULL.
6976 * Finishes an operation started with g_bus_get().
6978 * The returned object is a singleton, that is, shared with other
6979 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
6980 * event that you need a private message bus connection, use
6981 * g_dbus_address_get_for_bus_sync() and
6982 * g_dbus_connection_new_for_address().
6984 * Note that the returned #GDBusConnection object will (usually) have
6985 * the #GDBusConnection:exit-on-close property set to %TRUE.
6987 * Returns: (transfer full): A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
6992 g_bus_get_finish (GAsyncResult
*res
,
6995 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (res
);
6997 GDBusConnection
*ret
;
6999 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
7001 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_bus_get
);
7005 if (g_simple_async_result_propagate_error (simple
, error
))
7008 object
= g_simple_async_result_get_op_res_gpointer (simple
);
7009 g_assert (object
!= NULL
);
7010 ret
= g_object_ref (G_DBUS_CONNECTION (object
));
7016 /* ---------------------------------------------------------------------------------------------------- */