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.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
24 * - would be nice to expose GDBusAuthMechanism and an extension point
26 * - Need to rewrite GDBusAuth and rework GDBusAuthMechanism. In particular
27 * the mechanism VFuncs need to be able to set an error.
29 * - Need to document other mechanisms/sources for determining the D-Bus
30 * address of a well-known bus.
32 * - e.g. on Win32 we need code like from here
34 * http://cgit.freedesktop.org/~david/gdbus-standalone/tree/gdbus/gdbusaddress.c#n900
36 * that was never copied over here because it originally was copy-paste
37 * from the GPLv2 / AFL 2.1 libdbus sources.
39 * - on OS X we need to look in launchd for the address
41 * https://bugs.freedesktop.org/show_bug.cgi?id=14259
43 * - on X11 we need to look in a X11 property on the X server
44 * - (we can also just use dbus-launch(1) from the D-Bus
47 * - (ideally) this requires D-Bus spec work because none of
48 * this has never really been specced out properly (except
51 * - Related to the above, we also need to be able to launch a message bus
52 * instance.... Since we don't want to write our own bus daemon we should
53 * launch dbus-daemon(1) (thus: Win32 and OS X need to bundle it)
55 * - probably want a G_DBUS_NONCE_TCP_TMPDIR environment variable
56 * to specify where the nonce is stored. This will allow people to use
57 * G_DBUS_NONCE_TCP_TMPDIR=/mnt/secure.company.server/dbus-nonce-dir
58 * to easily achieve secure RPC via nonce-tcp.
60 * - need to expose an extension point for resolving D-Bus address and
61 * turning them into GIOStream objects. This will allow us to implement
62 * e.g. X11 D-Bus transports without dlopen()'ing or linking against
64 * - see g_dbus_address_connect() in gdbusaddress.c
66 * - would be cute to use kernel-specific APIs to resolve fds for
67 * debug output when using G_DBUS_DEBUG=message, e.g. in addition to
69 * fd 21: dev=8:1,mode=0100644,ino=1171231,uid=0,gid=0,rdev=0:0,size=234,atime=1273070640,mtime=1267126160,ctime=1267126160
71 * maybe we can show more information about what fd 21 really is.
72 * Ryan suggests looking in /proc/self/fd for clues / symlinks!
73 * Initial experiments on Linux 2.6 suggests that the symlink looks
78 * e.g. not of much use.
80 * - GDBus High-Level docs
81 * - Proxy: properties, signals...
82 * - Connection: IOStream based, ::close, connection setup steps
83 * mainloop integration, threading
84 * - Differences from libdbus (extend "Migrating from")
85 * - the message handling thread
86 * - Using GVariant instead of GValue
87 * - Explain why the high-level API is a good thing and what
88 * kind of pitfalls it avoids
89 * - Export objects before claiming names
90 * - Talk about auto-starting services (cf. GBusNameWatcherFlags)
98 #include "gdbusauth.h"
99 #include "gdbusutils.h"
100 #include "gdbusaddress.h"
101 #include "gdbusmessage.h"
102 #include "gdbusconnection.h"
103 #include "gdbuserror.h"
104 #include "gioenumtypes.h"
105 #include "gdbusintrospection.h"
106 #include "gdbusmethodinvocation.h"
107 #include "gdbusprivate.h"
108 #include "gdbusauthobserver.h"
109 #include "ginitable.h"
110 #include "gasyncinitable.h"
111 #include "giostream.h"
112 #include "gasyncresult.h"
116 #include "gunixconnection.h"
117 #include "gunixfdmessage.h"
120 #include "glibintl.h"
123 * SECTION:gdbusconnection
124 * @short_description: D-Bus Connections
125 * @include: gio/gio.h
127 * The #GDBusConnection type is used for D-Bus connections to remote
128 * peers such as a message buses. It is a low-level API that offers a
129 * lot of flexibility. For instance, it lets you establish a connection
130 * over any transport that can by represented as an #GIOStream.
132 * This class is rarely used directly in D-Bus clients. If you are writing
133 * a D-Bus client, it is often easier to use the g_bus_own_name(),
134 * g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs.
136 * As an exception to the usual GLib rule that a particular object must not
137 * be used by two threads at the same time, #GDBusConnection's methods may be
138 * called from any thread. This is so that g_bus_get() and g_bus_get_sync()
139 * can safely return the same #GDBusConnection when called from any thread.
141 * Most of the ways to obtain a #GDBusConnection automatically initialize it
142 * (i.e. connect to D-Bus): for instance, g_dbus_connection_new() and
143 * g_bus_get(), and the synchronous versions of those methods, give you an
144 * initialized connection. Language bindings for GIO should use
145 * g_initable_new() or g_async_initable_new_async(), which also initialize the
148 * If you construct an uninitialized #GDBusConnection, such as via
149 * g_object_new(), you must initialize it via g_initable_init() or
150 * g_async_initable_init_async() before using its methods or properties.
151 * Calling methods or accessing properties on a #GDBusConnection that has not
152 * completed initialization successfully is considered to be invalid, and leads
153 * to undefined behaviour. In particular, if initialization fails with a
154 * #GError, the only valid thing you can do with that #GDBusConnection is to
155 * free it with g_object_unref().
157 * ## An example D-Bus server # {#gdbus-server}
159 * Here is an example for a D-Bus server:
160 * [gdbus-example-server.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-server.c)
162 * ## An example for exporting a subtree # {#gdbus-subtree-server}
164 * Here is an example for exporting a subtree:
165 * [gdbus-example-subtree.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-subtree.c)
167 * ## An example for file descriptor passing # {#gdbus-unix-fd-client}
169 * Here is an example for passing UNIX file descriptors:
170 * [gdbus-unix-fd-client.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-unix-fd-client.c)
172 * ## An example for exporting a GObject # {#gdbus-export}
174 * Here is an example for exporting a #GObject:
175 * [gdbus-example-export.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-export.c)
178 /* ---------------------------------------------------------------------------------------------------- */
180 typedef struct _GDBusConnectionClass GDBusConnectionClass
;
183 * GDBusConnectionClass:
184 * @closed: Signal class handler for the #GDBusConnection::closed signal.
186 * Class structure for #GDBusConnection.
190 struct _GDBusConnectionClass
193 GObjectClass parent_class
;
197 void (*closed
) (GDBusConnection
*connection
,
198 gboolean remote_peer_vanished
,
202 G_LOCK_DEFINE_STATIC (message_bus_lock
);
204 static GWeakRef the_session_bus
;
205 static GWeakRef the_system_bus
;
207 /* Extra pseudo-member of GDBusSendMessageFlags.
208 * Set by initable_init() to indicate that despite not being initialized yet,
209 * enough of the only-valid-after-init members are set that we can send a
210 * message, and we're being called from its thread, so no memory barrier is
211 * required before accessing them.
213 #define SEND_MESSAGE_FLAGS_INITIALIZING (1u << 31)
215 /* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */
216 #define CALL_FLAGS_INITIALIZING (1u << 31)
218 /* ---------------------------------------------------------------------------------------------------- */
222 GDestroyNotify callback
;
224 GMainContext
*context
;
225 } CallDestroyNotifyData
;
228 call_destroy_notify_data_in_idle (gpointer user_data
)
230 CallDestroyNotifyData
*data
= user_data
;
231 data
->callback (data
->user_data
);
236 call_destroy_notify_data_free (CallDestroyNotifyData
*data
)
238 if (data
->context
!= NULL
)
239 g_main_context_unref (data
->context
);
244 * call_destroy_notify: <internal>
245 * @context: (nullable): A #GMainContext or %NULL.
246 * @callback: (nullable): A #GDestroyNotify or %NULL.
247 * @user_data: Data to pass to @callback.
249 * Schedules @callback to run in @context.
252 call_destroy_notify (GMainContext
*context
,
253 GDestroyNotify callback
,
256 GSource
*idle_source
;
257 CallDestroyNotifyData
*data
;
259 if (callback
== NULL
)
262 data
= g_new0 (CallDestroyNotifyData
, 1);
263 data
->callback
= callback
;
264 data
->user_data
= user_data
;
265 data
->context
= context
;
266 if (data
->context
!= NULL
)
267 g_main_context_ref (data
->context
);
269 idle_source
= g_idle_source_new ();
270 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
271 g_source_set_callback (idle_source
,
272 call_destroy_notify_data_in_idle
,
274 (GDestroyNotify
) call_destroy_notify_data_free
);
275 g_source_set_name (idle_source
, "[gio] call_destroy_notify_data_in_idle");
276 g_source_attach (idle_source
, data
->context
);
277 g_source_unref (idle_source
);
283 /* ---------------------------------------------------------------------------------------------------- */
286 _g_strv_has_string (const gchar
* const *haystack
,
291 for (n
= 0; haystack
!= NULL
&& haystack
[n
] != NULL
; n
++)
293 if (g_strcmp0 (haystack
[n
], needle
) == 0)
299 /* ---------------------------------------------------------------------------------------------------- */
302 #define CONNECTION_ENSURE_LOCK(obj) do { ; } while (FALSE)
304 // TODO: for some reason this doesn't work on Windows
305 #define CONNECTION_ENSURE_LOCK(obj) do { \
306 if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock))) \
308 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
309 "CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \
314 #define CONNECTION_LOCK(obj) do { \
315 g_mutex_lock (&(obj)->lock); \
318 #define CONNECTION_UNLOCK(obj) do { \
319 g_mutex_unlock (&(obj)->lock); \
322 /* Flags in connection->atomic_flags */
324 FLAG_INITIALIZED
= 1 << 0,
325 FLAG_EXIT_ON_CLOSE
= 1 << 1,
332 * The #GDBusConnection structure contains only private data and
333 * should only be accessed using the provided API.
337 struct _GDBusConnection
340 GObject parent_instance
;
342 /* ------------------------------------------------------------------------ */
343 /* -- General object state ------------------------------------------------ */
344 /* ------------------------------------------------------------------------ */
346 /* General-purpose lock for most fields */
349 /* A lock used in the init() method of the GInitable interface - see comments
350 * in initable_init() for why a separate lock is needed.
352 * If you need both @lock and @init_lock, you must take @init_lock first.
356 /* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
357 * someone calls org.freedesktop.DBus.Peer.GetMachineId(). Protected by @lock.
361 /* The underlying stream used for communication
362 * Read-only after initable_init(), so it may be read if you either
363 * hold @init_lock or check for initialization first.
367 /* The object used for authentication (if any).
368 * Read-only after initable_init(), so it may be read if you either
369 * hold @init_lock or check for initialization first.
373 /* Last serial used. Protected by @lock. */
376 /* The object used to send/receive messages.
377 * Read-only after initable_init(), so it may be read if you either
378 * hold @init_lock or check for initialization first.
382 /* If connected to a message bus, this contains the unique name assigned to
383 * us by the bus (e.g. ":1.42").
384 * Read-only after initable_init(), so it may be read if you either
385 * hold @init_lock or check for initialization first.
387 gchar
*bus_unique_name
;
389 /* The GUID returned by the other side if we authenticed as a client or
390 * the GUID to use if authenticating as a server.
391 * Read-only after initable_init(), so it may be read if you either
392 * hold @init_lock or check for initialization first.
396 /* FLAG_INITIALIZED is set exactly when initable_init() has finished running.
397 * Inspect @initialization_error to see whether it succeeded or failed.
399 * FLAG_EXIT_ON_CLOSE is the exit-on-close property.
401 * FLAG_CLOSED is the closed property. It may be read at any time, but
402 * may only be written while holding @lock.
404 volatile gint atomic_flags
;
406 /* If the connection could not be established during initable_init(),
407 * this GError will be set.
408 * Read-only after initable_init(), so it may be read if you either
409 * hold @init_lock or check for initialization first.
411 GError
*initialization_error
;
413 /* The result of g_main_context_ref_thread_default() when the object
414 * was created (the GObject _init() function) - this is used for delivery
415 * of the :closed GObject signal.
417 * Only set in the GObject init function, so no locks are needed.
419 GMainContext
*main_context_at_construction
;
421 /* Read-only construct properties, no locks needed */
423 GDBusConnectionFlags flags
;
425 /* Map used for managing method replies, protected by @lock */
426 GHashTable
*map_method_serial_to_task
; /* guint32 -> GTask* */
428 /* Maps used for managing signal subscription, protected by @lock */
429 GHashTable
*map_rule_to_signal_data
; /* match rule (gchar*) -> SignalData */
430 GHashTable
*map_id_to_signal_data
; /* id (guint) -> SignalData */
431 GHashTable
*map_sender_unique_name_to_signal_data_array
; /* unique sender (gchar*) -> GPtrArray* of SignalData */
433 /* Maps used for managing exported objects and subtrees,
436 GHashTable
*map_object_path_to_eo
; /* gchar* -> ExportedObject* */
437 GHashTable
*map_id_to_ei
; /* guint -> ExportedInterface* */
438 GHashTable
*map_object_path_to_es
; /* gchar* -> ExportedSubtree* */
439 GHashTable
*map_id_to_es
; /* guint -> ExportedSubtree* */
441 /* Map used for storing last used serials for each thread, protected by @lock */
442 GHashTable
*map_thread_to_last_serial
;
444 /* Structure used for message filters, protected by @lock */
447 /* Capabilities negotiated during authentication
448 * Read-only after initable_init(), so it may be read without holding a
449 * lock, if you check for initialization first.
451 GDBusCapabilityFlags capabilities
;
453 /* Protected by @init_lock */
454 GDBusAuthObserver
*authentication_observer
;
456 /* Read-only after initable_init(), so it may be read if you either
457 * hold @init_lock or check for initialization first.
459 GCredentials
*credentials
;
461 /* set to TRUE when finalizing */
465 typedef struct ExportedObject ExportedObject
;
466 static void exported_object_free (ExportedObject
*eo
);
468 typedef struct ExportedSubtree ExportedSubtree
;
469 static void exported_subtree_free (ExportedSubtree
*es
);
487 PROP_CAPABILITY_FLAGS
,
488 PROP_AUTHENTICATION_OBSERVER
,
491 static void distribute_signals (GDBusConnection
*connection
,
492 GDBusMessage
*message
);
494 static void distribute_method_call (GDBusConnection
*connection
,
495 GDBusMessage
*message
);
497 static gboolean
handle_generic_unlocked (GDBusConnection
*connection
,
498 GDBusMessage
*message
);
501 static void purge_all_signal_subscriptions (GDBusConnection
*connection
);
502 static void purge_all_filters (GDBusConnection
*connection
);
504 static void schedule_method_call (GDBusConnection
*connection
,
505 GDBusMessage
*message
,
506 guint registration_id
,
507 guint subtree_registration_id
,
508 const GDBusInterfaceInfo
*interface_info
,
509 const GDBusMethodInfo
*method_info
,
510 const GDBusPropertyInfo
*property_info
,
511 GVariant
*parameters
,
512 const GDBusInterfaceVTable
*vtable
,
513 GMainContext
*main_context
,
516 #define _G_ENSURE_LOCK(name) do { \
517 if (G_UNLIKELY (G_TRYLOCK(name))) \
519 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
520 "_G_ENSURE_LOCK: Lock '" #name "' is not locked"); \
524 static guint signals[LAST_SIGNAL] = { 0 };
526 static void initable_iface_init (GInitableIface
*initable_iface
);
527 static void async_initable_iface_init (GAsyncInitableIface
*async_initable_iface
);
529 G_DEFINE_TYPE_WITH_CODE (GDBusConnection
, g_dbus_connection
, G_TYPE_OBJECT
,
530 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE
, initable_iface_init
)
531 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE
, async_initable_iface_init
)
535 * Check that all members of @connection that can only be accessed after
536 * the connection is initialized can safely be accessed. If not,
537 * log a critical warning. This function is a memory barrier.
539 * Returns: %TRUE if initialized
542 check_initialized (GDBusConnection
*connection
)
544 /* The access to @atomic_flags isn't conditional, so that this function
545 * provides a memory barrier for thread-safety even if checks are disabled.
546 * (If you don't want this stricter guarantee, you can call
547 * g_return_if_fail (check_initialized (c)).)
549 * This isn't strictly necessary now that we've decided use of an
550 * uninitialized GDBusConnection is undefined behaviour, but it seems
551 * better to be as deterministic as is feasible.
553 * (Anything that could suffer a crash from seeing undefined values
554 * must have a race condition - thread A initializes the connection while
555 * thread B calls a method without initialization, hoping that thread A will
556 * win the race - so its behaviour is undefined anyway.)
558 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
560 g_return_val_if_fail (flags
& FLAG_INITIALIZED
, FALSE
);
562 /* We can safely access this, due to the memory barrier above */
563 g_return_val_if_fail (connection
->initialization_error
== NULL
, FALSE
);
569 MAY_BE_UNINITIALIZED
= (1<<1)
570 } CheckUnclosedFlags
;
573 * Check the same thing as check_initialized(), and also that the
574 * connection is not closed. If the connection is uninitialized,
575 * raise a critical warning (it's programmer error); if it's closed,
576 * raise a recoverable GError (it's a runtime error).
578 * This function is a memory barrier.
580 * Returns: %TRUE if initialized and not closed
583 check_unclosed (GDBusConnection
*connection
,
584 CheckUnclosedFlags check
,
587 /* check_initialized() is effectively inlined, so we don't waste time
588 * doing two memory barriers
590 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
592 if (!(check
& MAY_BE_UNINITIALIZED
))
594 g_return_val_if_fail (flags
& FLAG_INITIALIZED
, FALSE
);
595 g_return_val_if_fail (connection
->initialization_error
== NULL
, FALSE
);
598 if (flags
& FLAG_CLOSED
)
600 g_set_error_literal (error
,
603 _("The connection is closed"));
610 static GHashTable
*alive_connections
= NULL
;
613 g_dbus_connection_dispose (GObject
*object
)
615 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
617 G_LOCK (message_bus_lock
);
618 CONNECTION_LOCK (connection
);
619 if (connection
->worker
!= NULL
)
621 _g_dbus_worker_stop (connection
->worker
);
622 connection
->worker
= NULL
;
623 if (alive_connections
!= NULL
)
624 g_warn_if_fail (g_hash_table_remove (alive_connections
, connection
));
628 if (alive_connections
!= NULL
)
629 g_warn_if_fail (!g_hash_table_contains (alive_connections
, connection
));
631 CONNECTION_UNLOCK (connection
);
632 G_UNLOCK (message_bus_lock
);
634 if (G_OBJECT_CLASS (g_dbus_connection_parent_class
)->dispose
!= NULL
)
635 G_OBJECT_CLASS (g_dbus_connection_parent_class
)->dispose (object
);
639 g_dbus_connection_finalize (GObject
*object
)
641 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
643 connection
->finalizing
= TRUE
;
645 purge_all_signal_subscriptions (connection
);
647 purge_all_filters (connection
);
648 g_ptr_array_unref (connection
->filters
);
650 if (connection
->authentication_observer
!= NULL
)
651 g_object_unref (connection
->authentication_observer
);
653 if (connection
->auth
!= NULL
)
654 g_object_unref (connection
->auth
);
656 if (connection
->credentials
)
657 g_object_unref (connection
->credentials
);
659 if (connection
->stream
!= NULL
)
661 g_object_unref (connection
->stream
);
662 connection
->stream
= NULL
;
665 g_free (connection
->address
);
667 g_free (connection
->guid
);
668 g_free (connection
->bus_unique_name
);
670 if (connection
->initialization_error
!= NULL
)
671 g_error_free (connection
->initialization_error
);
673 g_hash_table_unref (connection
->map_method_serial_to_task
);
675 g_hash_table_unref (connection
->map_rule_to_signal_data
);
676 g_hash_table_unref (connection
->map_id_to_signal_data
);
677 g_hash_table_unref (connection
->map_sender_unique_name_to_signal_data_array
);
679 g_hash_table_unref (connection
->map_id_to_ei
);
680 g_hash_table_unref (connection
->map_object_path_to_eo
);
681 g_hash_table_unref (connection
->map_id_to_es
);
682 g_hash_table_unref (connection
->map_object_path_to_es
);
684 g_hash_table_unref (connection
->map_thread_to_last_serial
);
686 g_main_context_unref (connection
->main_context_at_construction
);
688 g_free (connection
->machine_id
);
690 g_mutex_clear (&connection
->init_lock
);
691 g_mutex_clear (&connection
->lock
);
693 G_OBJECT_CLASS (g_dbus_connection_parent_class
)->finalize (object
);
696 /* called in any user thread, with the connection's lock not held */
698 g_dbus_connection_get_property (GObject
*object
,
703 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
708 g_value_set_object (value
, g_dbus_connection_get_stream (connection
));
712 g_value_set_string (value
, g_dbus_connection_get_guid (connection
));
715 case PROP_UNIQUE_NAME
:
716 g_value_set_string (value
, g_dbus_connection_get_unique_name (connection
));
720 g_value_set_boolean (value
, g_dbus_connection_is_closed (connection
));
723 case PROP_EXIT_ON_CLOSE
:
724 g_value_set_boolean (value
, g_dbus_connection_get_exit_on_close (connection
));
727 case PROP_CAPABILITY_FLAGS
:
728 g_value_set_flags (value
, g_dbus_connection_get_capabilities (connection
));
732 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
737 /* called in any user thread, with the connection's lock not held */
739 g_dbus_connection_set_property (GObject
*object
,
744 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
749 connection
->stream
= g_value_dup_object (value
);
753 connection
->guid
= g_value_dup_string (value
);
757 connection
->address
= g_value_dup_string (value
);
761 connection
->flags
= g_value_get_flags (value
);
764 case PROP_EXIT_ON_CLOSE
:
765 g_dbus_connection_set_exit_on_close (connection
, g_value_get_boolean (value
));
768 case PROP_AUTHENTICATION_OBSERVER
:
769 connection
->authentication_observer
= g_value_dup_object (value
);
773 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
778 /* Base-class implementation of GDBusConnection::closed.
780 * Called in a user thread, by the main context that was thread-default when
781 * the object was constructed.
784 g_dbus_connection_real_closed (GDBusConnection
*connection
,
785 gboolean remote_peer_vanished
,
788 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
790 /* Because atomic int access is a memory barrier, we can safely read
791 * initialization_error without a lock, as long as we do it afterwards.
793 if (remote_peer_vanished
&&
794 (flags
& FLAG_EXIT_ON_CLOSE
) != 0 &&
795 (flags
& FLAG_INITIALIZED
) != 0 &&
796 connection
->initialization_error
== NULL
)
803 g_dbus_connection_class_init (GDBusConnectionClass
*klass
)
805 GObjectClass
*gobject_class
;
807 gobject_class
= G_OBJECT_CLASS (klass
);
809 gobject_class
->finalize
= g_dbus_connection_finalize
;
810 gobject_class
->dispose
= g_dbus_connection_dispose
;
811 gobject_class
->set_property
= g_dbus_connection_set_property
;
812 gobject_class
->get_property
= g_dbus_connection_get_property
;
814 klass
->closed
= g_dbus_connection_real_closed
;
817 * GDBusConnection:stream:
819 * The underlying #GIOStream used for I/O.
821 * If this is passed on construction and is a #GSocketConnection,
822 * then the corresponding #GSocket will be put into non-blocking mode.
824 * While the #GDBusConnection is active, it will interact with this
825 * stream from a worker thread, so it is not safe to interact with
826 * the stream directly.
830 g_object_class_install_property (gobject_class
,
832 g_param_spec_object ("stream",
834 P_("The underlying streams used for I/O"),
838 G_PARAM_CONSTRUCT_ONLY
|
839 G_PARAM_STATIC_NAME
|
840 G_PARAM_STATIC_BLURB
|
841 G_PARAM_STATIC_NICK
));
844 * GDBusConnection:address:
846 * A D-Bus address specifying potential endpoints that can be used
847 * when establishing the connection.
851 g_object_class_install_property (gobject_class
,
853 g_param_spec_string ("address",
855 P_("D-Bus address specifying potential socket endpoints"),
858 G_PARAM_CONSTRUCT_ONLY
|
859 G_PARAM_STATIC_NAME
|
860 G_PARAM_STATIC_BLURB
|
861 G_PARAM_STATIC_NICK
));
864 * GDBusConnection:flags:
866 * Flags from the #GDBusConnectionFlags enumeration.
870 g_object_class_install_property (gobject_class
,
872 g_param_spec_flags ("flags",
875 G_TYPE_DBUS_CONNECTION_FLAGS
,
876 G_DBUS_CONNECTION_FLAGS_NONE
,
878 G_PARAM_CONSTRUCT_ONLY
|
879 G_PARAM_STATIC_NAME
|
880 G_PARAM_STATIC_BLURB
|
881 G_PARAM_STATIC_NICK
));
884 * GDBusConnection:guid:
886 * The GUID of the peer performing the role of server when
889 * If you are constructing a #GDBusConnection and pass
890 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER in the
891 * #GDBusConnection:flags property then you MUST also set this
892 * property to a valid guid.
894 * If you are constructing a #GDBusConnection and pass
895 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT in the
896 * #GDBusConnection:flags property you will be able to read the GUID
897 * of the other peer here after the connection has been successfully
902 g_object_class_install_property (gobject_class
,
904 g_param_spec_string ("guid",
906 P_("GUID of the server peer"),
910 G_PARAM_CONSTRUCT_ONLY
|
911 G_PARAM_STATIC_NAME
|
912 G_PARAM_STATIC_BLURB
|
913 G_PARAM_STATIC_NICK
));
916 * GDBusConnection:unique-name:
918 * The unique name as assigned by the message bus or %NULL if the
919 * connection is not open or not a message bus connection.
923 g_object_class_install_property (gobject_class
,
925 g_param_spec_string ("unique-name",
927 P_("Unique name of bus connection"),
930 G_PARAM_STATIC_NAME
|
931 G_PARAM_STATIC_BLURB
|
932 G_PARAM_STATIC_NICK
));
935 * GDBusConnection:closed:
937 * A boolean specifying whether the connection has been closed.
941 g_object_class_install_property (gobject_class
,
943 g_param_spec_boolean ("closed",
945 P_("Whether the connection is closed"),
948 G_PARAM_STATIC_NAME
|
949 G_PARAM_STATIC_BLURB
|
950 G_PARAM_STATIC_NICK
));
953 * GDBusConnection:exit-on-close:
955 * A boolean specifying whether the process will be terminated (by
956 * calling `raise(SIGTERM)`) if the connection is closed by the
959 * Note that #GDBusConnection objects returned by g_bus_get_finish()
960 * and g_bus_get_sync() will (usually) have this property set to %TRUE.
964 g_object_class_install_property (gobject_class
,
966 g_param_spec_boolean ("exit-on-close",
968 P_("Whether the process is terminated when the connection is closed"),
972 G_PARAM_STATIC_NAME
|
973 G_PARAM_STATIC_BLURB
|
974 G_PARAM_STATIC_NICK
));
977 * GDBusConnection:capabilities:
979 * Flags from the #GDBusCapabilityFlags enumeration
980 * representing connection features negotiated with the other peer.
984 g_object_class_install_property (gobject_class
,
985 PROP_CAPABILITY_FLAGS
,
986 g_param_spec_flags ("capabilities",
989 G_TYPE_DBUS_CAPABILITY_FLAGS
,
990 G_DBUS_CAPABILITY_FLAGS_NONE
,
992 G_PARAM_STATIC_NAME
|
993 G_PARAM_STATIC_BLURB
|
994 G_PARAM_STATIC_NICK
));
997 * GDBusConnection:authentication-observer:
999 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
1003 g_object_class_install_property (gobject_class
,
1004 PROP_AUTHENTICATION_OBSERVER
,
1005 g_param_spec_object ("authentication-observer",
1006 P_("Authentication Observer"),
1007 P_("Object used to assist in the authentication process"),
1008 G_TYPE_DBUS_AUTH_OBSERVER
,
1010 G_PARAM_CONSTRUCT_ONLY
|
1011 G_PARAM_STATIC_NAME
|
1012 G_PARAM_STATIC_BLURB
|
1013 G_PARAM_STATIC_NICK
));
1016 * GDBusConnection::closed:
1017 * @connection: the #GDBusConnection emitting the signal
1018 * @remote_peer_vanished: %TRUE if @connection is closed because the
1019 * remote peer closed its end of the connection
1020 * @error: (nullable): a #GError with more details about the event or %NULL
1022 * Emitted when the connection is closed.
1024 * The cause of this event can be
1026 * - If g_dbus_connection_close() is called. In this case
1027 * @remote_peer_vanished is set to %FALSE and @error is %NULL.
1029 * - If the remote peer closes the connection. In this case
1030 * @remote_peer_vanished is set to %TRUE and @error is set.
1032 * - If the remote peer sends invalid or malformed data. In this
1033 * case @remote_peer_vanished is set to %FALSE and @error is set.
1035 * Upon receiving this signal, you should give up your reference to
1036 * @connection. You are guaranteed that this signal is emitted only
1041 signals
[CLOSED_SIGNAL
] = g_signal_new (I_("closed"),
1042 G_TYPE_DBUS_CONNECTION
,
1044 G_STRUCT_OFFSET (GDBusConnectionClass
, closed
),
1055 g_dbus_connection_init (GDBusConnection
*connection
)
1057 g_mutex_init (&connection
->lock
);
1058 g_mutex_init (&connection
->init_lock
);
1060 connection
->map_method_serial_to_task
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
1062 connection
->map_rule_to_signal_data
= g_hash_table_new (g_str_hash
,
1064 connection
->map_id_to_signal_data
= g_hash_table_new (g_direct_hash
,
1066 connection
->map_sender_unique_name_to_signal_data_array
= g_hash_table_new_full (g_str_hash
,
1069 (GDestroyNotify
) g_ptr_array_unref
);
1071 connection
->map_object_path_to_eo
= g_hash_table_new_full (g_str_hash
,
1074 (GDestroyNotify
) exported_object_free
);
1076 connection
->map_id_to_ei
= g_hash_table_new (g_direct_hash
,
1079 connection
->map_object_path_to_es
= g_hash_table_new_full (g_str_hash
,
1082 (GDestroyNotify
) exported_subtree_free
);
1084 connection
->map_id_to_es
= g_hash_table_new (g_direct_hash
,
1087 connection
->map_thread_to_last_serial
= g_hash_table_new (g_direct_hash
,
1090 connection
->main_context_at_construction
= g_main_context_ref_thread_default ();
1092 connection
->filters
= g_ptr_array_new ();
1096 * g_dbus_connection_get_stream:
1097 * @connection: a #GDBusConnection
1099 * Gets the underlying stream used for IO.
1101 * While the #GDBusConnection is active, it will interact with this
1102 * stream from a worker thread, so it is not safe to interact with
1103 * the stream directly.
1105 * Returns: (transfer none): the stream used for IO
1110 g_dbus_connection_get_stream (GDBusConnection
*connection
)
1112 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
1114 /* do not use g_return_val_if_fail(), we want the memory barrier */
1115 if (!check_initialized (connection
))
1118 return connection
->stream
;
1122 * g_dbus_connection_start_message_processing:
1123 * @connection: a #GDBusConnection
1125 * If @connection was created with
1126 * %G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, this method
1127 * starts processing messages. Does nothing on if @connection wasn't
1128 * created with this flag or if the method has already been called.
1133 g_dbus_connection_start_message_processing (GDBusConnection
*connection
)
1135 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1137 /* do not use g_return_val_if_fail(), we want the memory barrier */
1138 if (!check_initialized (connection
))
1141 g_assert (connection
->worker
!= NULL
);
1142 _g_dbus_worker_unfreeze (connection
->worker
);
1146 * g_dbus_connection_is_closed:
1147 * @connection: a #GDBusConnection
1149 * Gets whether @connection is closed.
1151 * Returns: %TRUE if the connection is closed, %FALSE otherwise
1156 g_dbus_connection_is_closed (GDBusConnection
*connection
)
1160 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1162 flags
= g_atomic_int_get (&connection
->atomic_flags
);
1164 return (flags
& FLAG_CLOSED
) ? TRUE
: FALSE
;
1168 * g_dbus_connection_get_capabilities:
1169 * @connection: a #GDBusConnection
1171 * Gets the capabilities negotiated with the remote peer
1173 * Returns: zero or more flags from the #GDBusCapabilityFlags enumeration
1177 GDBusCapabilityFlags
1178 g_dbus_connection_get_capabilities (GDBusConnection
*connection
)
1180 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), G_DBUS_CAPABILITY_FLAGS_NONE
);
1182 /* do not use g_return_val_if_fail(), we want the memory barrier */
1183 if (!check_initialized (connection
))
1184 return G_DBUS_CAPABILITY_FLAGS_NONE
;
1186 return connection
->capabilities
;
1189 /* ---------------------------------------------------------------------------------------------------- */
1191 /* Called in a temporary thread without holding locks. */
1193 flush_in_thread_func (GTask
*task
,
1194 gpointer source_object
,
1196 GCancellable
*cancellable
)
1198 GError
*error
= NULL
;
1200 if (g_dbus_connection_flush_sync (source_object
,
1203 g_task_return_boolean (task
, TRUE
);
1205 g_task_return_error (task
, error
);
1209 * g_dbus_connection_flush:
1210 * @connection: a #GDBusConnection
1211 * @cancellable: (nullable): a #GCancellable or %NULL
1212 * @callback: (nullable): a #GAsyncReadyCallback to call when the
1213 * request is satisfied or %NULL if you don't care about the result
1214 * @user_data: The data to pass to @callback
1216 * Asynchronously flushes @connection, that is, writes all queued
1217 * outgoing message to the transport and then flushes the transport
1218 * (using g_output_stream_flush_async()). This is useful in programs
1219 * that wants to emit a D-Bus signal and then exit immediately. Without
1220 * flushing the connection, there is no guaranteed that the message has
1221 * been sent to the networking buffers in the OS kernel.
1223 * This is an asynchronous method. When the operation is finished,
1224 * @callback will be invoked in the
1225 * [thread-default main context][g-main-context-push-thread-default]
1226 * of the thread you are calling this method from. You can
1227 * then call g_dbus_connection_flush_finish() to get the result of the
1228 * operation. See g_dbus_connection_flush_sync() for the synchronous
1234 g_dbus_connection_flush (GDBusConnection
*connection
,
1235 GCancellable
*cancellable
,
1236 GAsyncReadyCallback callback
,
1241 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1243 task
= g_task_new (connection
, cancellable
, callback
, user_data
);
1244 g_task_set_source_tag (task
, g_dbus_connection_flush
);
1245 g_task_run_in_thread (task
, flush_in_thread_func
);
1246 g_object_unref (task
);
1250 * g_dbus_connection_flush_finish:
1251 * @connection: a #GDBusConnection
1252 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
1253 * to g_dbus_connection_flush()
1254 * @error: return location for error or %NULL
1256 * Finishes an operation started with g_dbus_connection_flush().
1258 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1263 g_dbus_connection_flush_finish (GDBusConnection
*connection
,
1267 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1268 g_return_val_if_fail (g_task_is_valid (res
, connection
), FALSE
);
1269 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1271 return g_task_propagate_boolean (G_TASK (res
), error
);
1275 * g_dbus_connection_flush_sync:
1276 * @connection: a #GDBusConnection
1277 * @cancellable: (nullable): a #GCancellable or %NULL
1278 * @error: return location for error or %NULL
1280 * Synchronously flushes @connection. The calling thread is blocked
1281 * until this is done. See g_dbus_connection_flush() for the
1282 * asynchronous version of this method and more details about what it
1285 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1290 g_dbus_connection_flush_sync (GDBusConnection
*connection
,
1291 GCancellable
*cancellable
,
1296 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1297 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1301 /* This is only a best-effort attempt to see whether the connection is
1302 * closed, so it doesn't need the lock. If the connection closes just
1303 * after this check, but before scheduling the flush operation, the
1304 * result will be more or less the same as if the connection closed while
1305 * the flush operation was pending - it'll fail with either CLOSED or
1308 if (!check_unclosed (connection
, 0, error
))
1311 g_assert (connection
->worker
!= NULL
);
1313 ret
= _g_dbus_worker_flush_sync (connection
->worker
,
1321 /* ---------------------------------------------------------------------------------------------------- */
1325 GDBusConnection
*connection
;
1327 gboolean remote_peer_vanished
;
1331 emit_closed_data_free (EmitClosedData
*data
)
1333 g_object_unref (data
->connection
);
1334 if (data
->error
!= NULL
)
1335 g_error_free (data
->error
);
1339 /* Called in a user thread that has acquired the main context that was
1340 * thread-default when the object was constructed
1343 emit_closed_in_idle (gpointer user_data
)
1345 EmitClosedData
*data
= user_data
;
1348 g_object_notify (G_OBJECT (data
->connection
), "closed");
1349 g_signal_emit (data
->connection
,
1350 signals
[CLOSED_SIGNAL
],
1352 data
->remote_peer_vanished
,
1358 /* Can be called from any thread, must hold lock.
1359 * FLAG_CLOSED must already have been set.
1362 schedule_closed_unlocked (GDBusConnection
*connection
,
1363 gboolean remote_peer_vanished
,
1366 GSource
*idle_source
;
1367 EmitClosedData
*data
;
1369 CONNECTION_ENSURE_LOCK (connection
);
1371 data
= g_new0 (EmitClosedData
, 1);
1372 data
->connection
= g_object_ref (connection
);
1373 data
->remote_peer_vanished
= remote_peer_vanished
;
1374 data
->error
= error
!= NULL
? g_error_copy (error
) : NULL
;
1376 idle_source
= g_idle_source_new ();
1377 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
1378 g_source_set_callback (idle_source
,
1379 emit_closed_in_idle
,
1381 (GDestroyNotify
) emit_closed_data_free
);
1382 g_source_set_name (idle_source
, "[gio] emit_closed_in_idle");
1383 g_source_attach (idle_source
, connection
->main_context_at_construction
);
1384 g_source_unref (idle_source
);
1387 /* ---------------------------------------------------------------------------------------------------- */
1390 * g_dbus_connection_close:
1391 * @connection: a #GDBusConnection
1392 * @cancellable: (nullable): a #GCancellable or %NULL
1393 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is
1394 * satisfied or %NULL if you don't care about the result
1395 * @user_data: The data to pass to @callback
1397 * Closes @connection. Note that this never causes the process to
1398 * exit (this might only happen if the other end of a shared message
1399 * bus connection disconnects, see #GDBusConnection:exit-on-close).
1401 * Once the connection is closed, operations such as sending a message
1402 * will return with the error %G_IO_ERROR_CLOSED. Closing a connection
1403 * will not automatically flush the connection so queued messages may
1404 * be lost. Use g_dbus_connection_flush() if you need such guarantees.
1406 * If @connection is already closed, this method fails with
1407 * %G_IO_ERROR_CLOSED.
1409 * When @connection has been closed, the #GDBusConnection::closed
1410 * signal is emitted in the
1411 * [thread-default main context][g-main-context-push-thread-default]
1412 * of the thread that @connection was constructed in.
1414 * This is an asynchronous method. When the operation is finished,
1415 * @callback will be invoked in the
1416 * [thread-default main context][g-main-context-push-thread-default]
1417 * of the thread you are calling this method from. You can
1418 * then call g_dbus_connection_close_finish() to get the result of the
1419 * operation. See g_dbus_connection_close_sync() for the synchronous
1425 g_dbus_connection_close (GDBusConnection
*connection
,
1426 GCancellable
*cancellable
,
1427 GAsyncReadyCallback callback
,
1432 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1434 /* do not use g_return_val_if_fail(), we want the memory barrier */
1435 if (!check_initialized (connection
))
1438 g_assert (connection
->worker
!= NULL
);
1440 task
= g_task_new (connection
, cancellable
, callback
, user_data
);
1441 g_task_set_source_tag (task
, g_dbus_connection_close
);
1442 _g_dbus_worker_close (connection
->worker
, task
);
1443 g_object_unref (task
);
1447 * g_dbus_connection_close_finish:
1448 * @connection: a #GDBusConnection
1449 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
1450 * to g_dbus_connection_close()
1451 * @error: return location for error or %NULL
1453 * Finishes an operation started with g_dbus_connection_close().
1455 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1460 g_dbus_connection_close_finish (GDBusConnection
*connection
,
1464 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1465 g_return_val_if_fail (g_task_is_valid (res
, connection
), FALSE
);
1466 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1468 return g_task_propagate_boolean (G_TASK (res
), error
);
1473 GAsyncResult
*result
;
1476 /* Can be called by any thread, without the connection lock */
1478 sync_close_cb (GObject
*source_object
,
1482 SyncCloseData
*data
= user_data
;
1484 data
->result
= g_object_ref (res
);
1485 g_main_loop_quit (data
->loop
);
1489 * g_dbus_connection_close_sync:
1490 * @connection: a #GDBusConnection
1491 * @cancellable: (nullable): a #GCancellable or %NULL
1492 * @error: return location for error or %NULL
1494 * Synchronously closes @connection. The calling thread is blocked
1495 * until this is done. See g_dbus_connection_close() for the
1496 * asynchronous version of this method and more details about what it
1499 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1504 g_dbus_connection_close_sync (GDBusConnection
*connection
,
1505 GCancellable
*cancellable
,
1510 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1511 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1515 if (check_unclosed (connection
, 0, error
))
1517 GMainContext
*context
;
1520 context
= g_main_context_new ();
1521 g_main_context_push_thread_default (context
);
1522 data
.loop
= g_main_loop_new (context
, TRUE
);
1525 g_dbus_connection_close (connection
, cancellable
, sync_close_cb
, &data
);
1526 g_main_loop_run (data
.loop
);
1527 ret
= g_dbus_connection_close_finish (connection
, data
.result
, error
);
1529 g_object_unref (data
.result
);
1530 g_main_loop_unref (data
.loop
);
1531 g_main_context_pop_thread_default (context
);
1532 g_main_context_unref (context
);
1538 /* ---------------------------------------------------------------------------------------------------- */
1541 * g_dbus_connection_get_last_serial:
1542 * @connection: a #GDBusConnection
1544 * Retrieves the last serial number assigned to a #GDBusMessage on
1545 * the current thread. This includes messages sent via both low-level
1546 * API such as g_dbus_connection_send_message() as well as
1547 * high-level API such as g_dbus_connection_emit_signal(),
1548 * g_dbus_connection_call() or g_dbus_proxy_call().
1550 * Returns: the last used serial or zero when no message has been sent
1551 * within the current thread
1556 g_dbus_connection_get_last_serial (GDBusConnection
*connection
)
1560 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
1562 CONNECTION_LOCK (connection
);
1563 ret
= GPOINTER_TO_UINT (g_hash_table_lookup (connection
->map_thread_to_last_serial
,
1565 CONNECTION_UNLOCK (connection
);
1570 /* ---------------------------------------------------------------------------------------------------- */
1572 /* Can be called by any thread, with the connection lock held */
1574 g_dbus_connection_send_message_unlocked (GDBusConnection
*connection
,
1575 GDBusMessage
*message
,
1576 GDBusSendMessageFlags flags
,
1577 volatile guint32
*out_serial
,
1582 guint32 serial_to_use
;
1585 CONNECTION_ENSURE_LOCK (connection
);
1587 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1588 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
1590 /* TODO: check all necessary headers are present */
1595 if (out_serial
!= NULL
)
1598 /* If we're in initable_init(), don't check for being initialized, to avoid
1599 * chicken-and-egg problems. initable_init() is responsible for setting up
1600 * our prerequisites (mainly connection->worker), and only calling us
1601 * from its own thread (so no memory barrier is needed).
1603 if (!check_unclosed (connection
,
1604 (flags
& SEND_MESSAGE_FLAGS_INITIALIZING
) ? MAY_BE_UNINITIALIZED
: 0,
1608 blob
= g_dbus_message_to_blob (message
,
1610 connection
->capabilities
,
1615 if (flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
)
1616 serial_to_use
= g_dbus_message_get_serial (message
);
1618 serial_to_use
= ++connection
->last_serial
; /* TODO: handle overflow */
1623 ((guint32
*) blob
)[2] = GUINT32_TO_LE (serial_to_use
);
1626 ((guint32
*) blob
)[2] = GUINT32_TO_BE (serial_to_use
);
1629 g_assert_not_reached ();
1634 g_printerr ("Writing message of %" G_GSIZE_FORMAT
" bytes (serial %d) on %p:\n",
1635 blob_size
, serial_to_use
, connection
);
1636 g_printerr ("----\n");
1637 hexdump (blob
, blob_size
);
1638 g_printerr ("----\n");
1641 /* TODO: use connection->auth to encode the blob */
1643 if (out_serial
!= NULL
)
1644 *out_serial
= serial_to_use
;
1646 /* store used serial for the current thread */
1647 /* TODO: watch the thread disposal and remove associated record
1649 * - see https://bugzilla.gnome.org/show_bug.cgi?id=676825#c7
1651 g_hash_table_replace (connection
->map_thread_to_last_serial
,
1653 GUINT_TO_POINTER (serial_to_use
));
1655 if (!(flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
))
1656 g_dbus_message_set_serial (message
, serial_to_use
);
1658 g_dbus_message_lock (message
);
1659 _g_dbus_worker_send_message (connection
->worker
,
1663 blob
= NULL
; /* since _g_dbus_worker_send_message() steals the blob */
1674 * g_dbus_connection_send_message:
1675 * @connection: a #GDBusConnection
1676 * @message: a #GDBusMessage
1677 * @flags: flags affecting how the message is sent
1678 * @out_serial: (out) (optional): return location for serial number assigned
1679 * to @message when sending it or %NULL
1680 * @error: Return location for error or %NULL
1682 * Asynchronously sends @message to the peer represented by @connection.
1684 * Unless @flags contain the
1685 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1686 * will be assigned by @connection and set on @message via
1687 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1688 * serial number used will be written to this location prior to
1689 * submitting the message to the underlying transport.
1691 * If @connection is closed then the operation will fail with
1692 * %G_IO_ERROR_CLOSED. If @message is not well-formed,
1693 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1695 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
1696 * for an example of how to use this low-level API to send and receive
1697 * UNIX file descriptors.
1699 * Note that @message must be unlocked, unless @flags contain the
1700 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1702 * Returns: %TRUE if the message was well-formed and queued for
1703 * transmission, %FALSE if @error is set
1708 g_dbus_connection_send_message (GDBusConnection
*connection
,
1709 GDBusMessage
*message
,
1710 GDBusSendMessageFlags flags
,
1711 volatile guint32
*out_serial
,
1716 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1717 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
1718 g_return_val_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
), FALSE
);
1719 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1721 CONNECTION_LOCK (connection
);
1722 ret
= g_dbus_connection_send_message_unlocked (connection
, message
, flags
, out_serial
, error
);
1723 CONNECTION_UNLOCK (connection
);
1727 /* ---------------------------------------------------------------------------------------------------- */
1733 gulong cancellable_handler_id
;
1735 GSource
*timeout_source
;
1740 /* Can be called from any thread with or without lock held */
1742 send_message_data_free (SendMessageData
*data
)
1744 g_assert (data
->timeout_source
== NULL
);
1745 g_assert (data
->cancellable_handler_id
== 0);
1747 g_slice_free (SendMessageData
, data
);
1750 /* ---------------------------------------------------------------------------------------------------- */
1752 /* can be called from any thread with lock held; @task is (transfer full) */
1754 send_message_with_reply_cleanup (GTask
*task
, gboolean remove
)
1756 GDBusConnection
*connection
= g_task_get_source_object (task
);
1757 SendMessageData
*data
= g_task_get_task_data (task
);
1759 CONNECTION_ENSURE_LOCK (connection
);
1761 g_assert (!data
->delivered
);
1763 data
->delivered
= TRUE
;
1765 if (data
->timeout_source
!= NULL
)
1767 g_source_destroy (data
->timeout_source
);
1768 data
->timeout_source
= NULL
;
1770 if (data
->cancellable_handler_id
> 0)
1772 g_cancellable_disconnect (g_task_get_cancellable (task
), data
->cancellable_handler_id
);
1773 data
->cancellable_handler_id
= 0;
1778 gboolean removed
= g_hash_table_remove (connection
->map_method_serial_to_task
,
1779 GUINT_TO_POINTER (data
->serial
));
1780 g_warn_if_fail (removed
);
1783 g_object_unref (task
);
1786 /* ---------------------------------------------------------------------------------------------------- */
1788 /* Called from GDBus worker thread with lock held; @task is (transfer full). */
1790 send_message_data_deliver_reply_unlocked (GTask
*task
,
1791 GDBusMessage
*reply
)
1793 SendMessageData
*data
= g_task_get_task_data (task
);
1795 if (data
->delivered
)
1798 g_task_return_pointer (task
, g_object_ref (reply
), g_object_unref
);
1800 send_message_with_reply_cleanup (task
, TRUE
);
1806 /* Called from a user thread, lock is not held */
1808 send_message_data_deliver_error (GTask
*task
,
1811 const char *message
)
1813 GDBusConnection
*connection
= g_task_get_source_object (task
);
1814 SendMessageData
*data
= g_task_get_task_data (task
);
1816 CONNECTION_LOCK (connection
);
1817 if (data
->delivered
)
1819 CONNECTION_UNLOCK (connection
);
1823 g_object_ref (task
);
1824 send_message_with_reply_cleanup (task
, TRUE
);
1825 CONNECTION_UNLOCK (connection
);
1827 g_task_return_new_error (task
, domain
, code
, "%s", message
);
1828 g_object_unref (task
);
1831 /* ---------------------------------------------------------------------------------------------------- */
1833 /* Called from a user thread, lock is not held; @task is (transfer full) */
1835 send_message_with_reply_cancelled_idle_cb (gpointer user_data
)
1837 GTask
*task
= user_data
;
1839 send_message_data_deliver_error (task
, G_IO_ERROR
, G_IO_ERROR_CANCELLED
,
1840 _("Operation was cancelled"));
1844 /* Can be called from any thread with or without lock held */
1846 send_message_with_reply_cancelled_cb (GCancellable
*cancellable
,
1849 GTask
*task
= user_data
;
1850 GSource
*idle_source
;
1852 /* postpone cancellation to idle handler since we may be called directly
1853 * via g_cancellable_connect() (e.g. holding lock)
1855 idle_source
= g_idle_source_new ();
1856 g_source_set_name (idle_source
, "[gio] send_message_with_reply_cancelled_idle_cb");
1857 g_task_attach_source (task
, idle_source
, send_message_with_reply_cancelled_idle_cb
);
1858 g_source_unref (idle_source
);
1861 /* ---------------------------------------------------------------------------------------------------- */
1863 /* Called from a user thread, lock is not held; @task is (transfer full) */
1865 send_message_with_reply_timeout_cb (gpointer user_data
)
1867 GTask
*task
= user_data
;
1869 send_message_data_deliver_error (task
, G_IO_ERROR
, G_IO_ERROR_TIMED_OUT
,
1870 _("Timeout was reached"));
1874 /* ---------------------------------------------------------------------------------------------------- */
1876 /* Called from a user thread, connection's lock is held */
1878 g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection
*connection
,
1879 GDBusMessage
*message
,
1880 GDBusSendMessageFlags flags
,
1882 volatile guint32
*out_serial
,
1883 GCancellable
*cancellable
,
1884 GAsyncReadyCallback callback
,
1888 SendMessageData
*data
;
1889 GError
*error
= NULL
;
1890 volatile guint32 serial
;
1892 if (out_serial
== NULL
)
1893 out_serial
= &serial
;
1895 if (timeout_msec
== -1)
1896 timeout_msec
= 25 * 1000;
1898 data
= g_slice_new0 (SendMessageData
);
1899 task
= g_task_new (connection
, cancellable
, callback
, user_data
);
1900 g_task_set_source_tag (task
,
1901 g_dbus_connection_send_message_with_reply_unlocked
);
1902 g_task_set_task_data (task
, data
, (GDestroyNotify
) send_message_data_free
);
1904 if (g_task_return_error_if_cancelled (task
))
1906 g_object_unref (task
);
1910 if (!g_dbus_connection_send_message_unlocked (connection
, message
, flags
, out_serial
, &error
))
1912 g_task_return_error (task
, error
);
1913 g_object_unref (task
);
1916 data
->serial
= *out_serial
;
1918 if (cancellable
!= NULL
)
1920 data
->cancellable_handler_id
= g_cancellable_connect (cancellable
,
1921 G_CALLBACK (send_message_with_reply_cancelled_cb
),
1922 g_object_ref (task
),
1926 if (timeout_msec
!= G_MAXINT
)
1928 data
->timeout_source
= g_timeout_source_new (timeout_msec
);
1929 g_task_attach_source (task
, data
->timeout_source
,
1930 (GSourceFunc
) send_message_with_reply_timeout_cb
);
1931 g_source_unref (data
->timeout_source
);
1934 g_hash_table_insert (connection
->map_method_serial_to_task
,
1935 GUINT_TO_POINTER (*out_serial
),
1936 g_steal_pointer (&task
));
1940 * g_dbus_connection_send_message_with_reply:
1941 * @connection: a #GDBusConnection
1942 * @message: a #GDBusMessage
1943 * @flags: flags affecting how the message is sent
1944 * @timeout_msec: the timeout in milliseconds, -1 to use the default
1945 * timeout or %G_MAXINT for no timeout
1946 * @out_serial: (out) (optional): return location for serial number assigned
1947 * to @message when sending it or %NULL
1948 * @cancellable: (nullable): a #GCancellable or %NULL
1949 * @callback: (nullable): a #GAsyncReadyCallback to call when the request
1950 * is satisfied or %NULL if you don't care about the result
1951 * @user_data: The data to pass to @callback
1953 * Asynchronously sends @message to the peer represented by @connection.
1955 * Unless @flags contain the
1956 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1957 * will be assigned by @connection and set on @message via
1958 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1959 * serial number used will be written to this location prior to
1960 * submitting the message to the underlying transport.
1962 * If @connection is closed then the operation will fail with
1963 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
1964 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
1965 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1967 * This is an asynchronous method. When the operation is finished, @callback
1968 * will be invoked in the
1969 * [thread-default main context][g-main-context-push-thread-default]
1970 * of the thread you are calling this method from. You can then call
1971 * g_dbus_connection_send_message_with_reply_finish() to get the result of the operation.
1972 * See g_dbus_connection_send_message_with_reply_sync() for the synchronous version.
1974 * Note that @message must be unlocked, unless @flags contain the
1975 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1977 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
1978 * for an example of how to use this low-level API to send and receive
1979 * UNIX file descriptors.
1984 g_dbus_connection_send_message_with_reply (GDBusConnection
*connection
,
1985 GDBusMessage
*message
,
1986 GDBusSendMessageFlags flags
,
1988 volatile guint32
*out_serial
,
1989 GCancellable
*cancellable
,
1990 GAsyncReadyCallback callback
,
1993 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1994 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
1995 g_return_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
));
1996 g_return_if_fail (timeout_msec
>= 0 || timeout_msec
== -1);
1998 CONNECTION_LOCK (connection
);
1999 g_dbus_connection_send_message_with_reply_unlocked (connection
,
2007 CONNECTION_UNLOCK (connection
);
2011 * g_dbus_connection_send_message_with_reply_finish:
2012 * @connection: a #GDBusConnection
2013 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
2014 * g_dbus_connection_send_message_with_reply()
2015 * @error: teturn location for error or %NULL
2017 * Finishes an operation started with g_dbus_connection_send_message_with_reply().
2019 * Note that @error is only set if a local in-process error
2020 * occurred. That is to say that the returned #GDBusMessage object may
2021 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
2022 * g_dbus_message_to_gerror() to transcode this to a #GError.
2024 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
2025 * for an example of how to use this low-level API to send and receive
2026 * UNIX file descriptors.
2028 * Returns: (transfer full): a locked #GDBusMessage or %NULL if @error is set
2033 g_dbus_connection_send_message_with_reply_finish (GDBusConnection
*connection
,
2037 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
2038 g_return_val_if_fail (g_task_is_valid (res
, connection
), NULL
);
2039 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2041 return g_task_propagate_pointer (G_TASK (res
), error
);
2044 /* ---------------------------------------------------------------------------------------------------- */
2049 GMainContext
*context
;
2051 } SendMessageSyncData
;
2053 /* Called from a user thread, lock is not held */
2055 send_message_with_reply_sync_cb (GDBusConnection
*connection
,
2059 SendMessageSyncData
*data
= user_data
;
2060 data
->res
= g_object_ref (res
);
2061 g_main_loop_quit (data
->loop
);
2065 * g_dbus_connection_send_message_with_reply_sync:
2066 * @connection: a #GDBusConnection
2067 * @message: a #GDBusMessage
2068 * @flags: flags affecting how the message is sent.
2069 * @timeout_msec: the timeout in milliseconds, -1 to use the default
2070 * timeout or %G_MAXINT for no timeout
2071 * @out_serial: (out) (optional): return location for serial number
2072 * assigned to @message when sending it or %NULL
2073 * @cancellable: (nullable): a #GCancellable or %NULL
2074 * @error: return location for error or %NULL
2076 * Synchronously sends @message to the peer represented by @connection
2077 * and blocks the calling thread until a reply is received or the
2078 * timeout is reached. See g_dbus_connection_send_message_with_reply()
2079 * for the asynchronous version of this method.
2081 * Unless @flags contain the
2082 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
2083 * will be assigned by @connection and set on @message via
2084 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
2085 * serial number used will be written to this location prior to
2086 * submitting the message to the underlying transport.
2088 * If @connection is closed then the operation will fail with
2089 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
2090 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
2091 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
2093 * Note that @error is only set if a local in-process error
2094 * occurred. That is to say that the returned #GDBusMessage object may
2095 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
2096 * g_dbus_message_to_gerror() to transcode this to a #GError.
2098 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
2099 * for an example of how to use this low-level API to send and receive
2100 * UNIX file descriptors.
2102 * Note that @message must be unlocked, unless @flags contain the
2103 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
2105 * Returns: (transfer full): a locked #GDBusMessage that is the reply
2106 * to @message or %NULL if @error is set
2111 g_dbus_connection_send_message_with_reply_sync (GDBusConnection
*connection
,
2112 GDBusMessage
*message
,
2113 GDBusSendMessageFlags flags
,
2115 volatile guint32
*out_serial
,
2116 GCancellable
*cancellable
,
2119 SendMessageSyncData data
;
2120 GDBusMessage
*reply
;
2122 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
2123 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2124 g_return_val_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
), NULL
);
2125 g_return_val_if_fail (timeout_msec
>= 0 || timeout_msec
== -1, NULL
);
2126 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2129 data
.context
= g_main_context_new ();
2130 data
.loop
= g_main_loop_new (data
.context
, FALSE
);
2132 g_main_context_push_thread_default (data
.context
);
2134 g_dbus_connection_send_message_with_reply (connection
,
2140 (GAsyncReadyCallback
) send_message_with_reply_sync_cb
,
2142 g_main_loop_run (data
.loop
);
2143 reply
= g_dbus_connection_send_message_with_reply_finish (connection
,
2147 g_main_context_pop_thread_default (data
.context
);
2149 g_main_context_unref (data
.context
);
2150 g_main_loop_unref (data
.loop
);
2152 g_object_unref (data
.res
);
2157 /* ---------------------------------------------------------------------------------------------------- */
2163 GDBusMessageFilterFunction filter_function
;
2165 GDestroyNotify user_data_free_func
;
2166 GMainContext
*context
;
2169 /* requires CONNECTION_LOCK */
2170 static FilterData
**
2171 copy_filter_list (GPtrArray
*filters
)
2176 copy
= g_new (FilterData
*, filters
->len
+ 1);
2177 for (n
= 0; n
< filters
->len
; n
++)
2179 copy
[n
] = filters
->pdata
[n
];
2180 copy
[n
]->ref_count
++;
2187 /* requires CONNECTION_LOCK */
2189 free_filter_list (FilterData
**filters
)
2193 for (n
= 0; filters
[n
]; n
++)
2195 filters
[n
]->ref_count
--;
2196 if (filters
[n
]->ref_count
== 0)
2198 call_destroy_notify (filters
[n
]->context
,
2199 filters
[n
]->user_data_free_func
,
2200 filters
[n
]->user_data
);
2201 g_main_context_unref (filters
[n
]->context
);
2202 g_free (filters
[n
]);
2208 /* Called in GDBusWorker's thread - we must not block - with no lock held */
2210 on_worker_message_received (GDBusWorker
*worker
,
2211 GDBusMessage
*message
,
2214 GDBusConnection
*connection
;
2215 FilterData
**filters
;
2219 G_LOCK (message_bus_lock
);
2220 alive
= g_hash_table_contains (alive_connections
, user_data
);
2223 G_UNLOCK (message_bus_lock
);
2226 connection
= G_DBUS_CONNECTION (user_data
);
2227 g_object_ref (connection
);
2228 G_UNLOCK (message_bus_lock
);
2230 //g_debug ("in on_worker_message_received");
2232 g_object_ref (message
);
2233 g_dbus_message_lock (message
);
2235 //g_debug ("boo ref_count = %d %p %p", G_OBJECT (connection)->ref_count, connection, connection->worker);
2237 /* First collect the set of callback functions */
2238 CONNECTION_LOCK (connection
);
2239 filters
= copy_filter_list (connection
->filters
);
2240 CONNECTION_UNLOCK (connection
);
2242 /* then call the filters in order (without holding the lock) */
2243 for (n
= 0; filters
[n
]; n
++)
2245 message
= filters
[n
]->filter_function (connection
,
2248 filters
[n
]->user_data
);
2249 if (message
== NULL
)
2251 g_dbus_message_lock (message
);
2254 CONNECTION_LOCK (connection
);
2255 free_filter_list (filters
);
2256 CONNECTION_UNLOCK (connection
);
2258 /* Standard dispatch unless the filter ate the message - no need to
2259 * do anything if the message was altered
2261 if (message
!= NULL
)
2263 GDBusMessageType message_type
;
2265 message_type
= g_dbus_message_get_message_type (message
);
2266 if (message_type
== G_DBUS_MESSAGE_TYPE_METHOD_RETURN
|| message_type
== G_DBUS_MESSAGE_TYPE_ERROR
)
2268 guint32 reply_serial
;
2271 reply_serial
= g_dbus_message_get_reply_serial (message
);
2272 CONNECTION_LOCK (connection
);
2273 task
= g_hash_table_lookup (connection
->map_method_serial_to_task
,
2274 GUINT_TO_POINTER (reply_serial
));
2277 /* This removes @task from @map_method_serial_to_task. */
2278 //g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection);
2279 send_message_data_deliver_reply_unlocked (task
, message
);
2283 //g_debug ("message reply/error for serial %d but no SendMessageData found for %p", reply_serial, connection);
2285 CONNECTION_UNLOCK (connection
);
2287 else if (message_type
== G_DBUS_MESSAGE_TYPE_SIGNAL
)
2289 CONNECTION_LOCK (connection
);
2290 distribute_signals (connection
, message
);
2291 CONNECTION_UNLOCK (connection
);
2293 else if (message_type
== G_DBUS_MESSAGE_TYPE_METHOD_CALL
)
2295 CONNECTION_LOCK (connection
);
2296 distribute_method_call (connection
, message
);
2297 CONNECTION_UNLOCK (connection
);
2301 if (message
!= NULL
)
2302 g_object_unref (message
);
2303 g_object_unref (connection
);
2306 /* Called in GDBusWorker's thread, lock is not held */
2307 static GDBusMessage
*
2308 on_worker_message_about_to_be_sent (GDBusWorker
*worker
,
2309 GDBusMessage
*message
,
2312 GDBusConnection
*connection
;
2313 FilterData
**filters
;
2317 G_LOCK (message_bus_lock
);
2318 alive
= g_hash_table_contains (alive_connections
, user_data
);
2321 G_UNLOCK (message_bus_lock
);
2324 connection
= G_DBUS_CONNECTION (user_data
);
2325 g_object_ref (connection
);
2326 G_UNLOCK (message_bus_lock
);
2328 //g_debug ("in on_worker_message_about_to_be_sent");
2330 /* First collect the set of callback functions */
2331 CONNECTION_LOCK (connection
);
2332 filters
= copy_filter_list (connection
->filters
);
2333 CONNECTION_UNLOCK (connection
);
2335 /* then call the filters in order (without holding the lock) */
2336 for (n
= 0; filters
[n
]; n
++)
2338 g_dbus_message_lock (message
);
2339 message
= filters
[n
]->filter_function (connection
,
2342 filters
[n
]->user_data
);
2343 if (message
== NULL
)
2347 CONNECTION_LOCK (connection
);
2348 free_filter_list (filters
);
2349 CONNECTION_UNLOCK (connection
);
2351 g_object_unref (connection
);
2356 /* called with connection lock held, in GDBusWorker thread */
2358 cancel_method_on_close (gpointer key
, gpointer value
, gpointer user_data
)
2360 GTask
*task
= value
;
2361 SendMessageData
*data
= g_task_get_task_data (task
);
2363 if (data
->delivered
)
2366 g_task_return_new_error (task
,
2369 _("The connection is closed"));
2371 /* Ask send_message_with_reply_cleanup not to remove the element from the
2372 * hash table - we're in the middle of a foreach; that would be unsafe.
2373 * Instead, return TRUE from this function so that it gets removed safely.
2375 send_message_with_reply_cleanup (task
, FALSE
);
2379 /* Called in GDBusWorker's thread - we must not block - without lock held */
2381 on_worker_closed (GDBusWorker
*worker
,
2382 gboolean remote_peer_vanished
,
2386 GDBusConnection
*connection
;
2388 guint old_atomic_flags
;
2390 G_LOCK (message_bus_lock
);
2391 alive
= g_hash_table_contains (alive_connections
, user_data
);
2394 G_UNLOCK (message_bus_lock
);
2397 connection
= G_DBUS_CONNECTION (user_data
);
2398 g_object_ref (connection
);
2399 G_UNLOCK (message_bus_lock
);
2401 //g_debug ("in on_worker_closed: %s", error->message);
2403 CONNECTION_LOCK (connection
);
2404 /* Even though this is atomic, we do it inside the lock to avoid breaking
2405 * assumptions in remove_match_rule(). We'd need the lock in a moment
2406 * anyway, so, no loss.
2408 old_atomic_flags
= g_atomic_int_or (&connection
->atomic_flags
, FLAG_CLOSED
);
2410 if (!(old_atomic_flags
& FLAG_CLOSED
))
2412 g_hash_table_foreach_remove (connection
->map_method_serial_to_task
, cancel_method_on_close
, NULL
);
2413 schedule_closed_unlocked (connection
, remote_peer_vanished
, error
);
2415 CONNECTION_UNLOCK (connection
);
2417 g_object_unref (connection
);
2420 /* ---------------------------------------------------------------------------------------------------- */
2422 /* Determines the biggest set of capabilities we can support on this
2425 * Called with the init_lock held.
2427 static GDBusCapabilityFlags
2428 get_offered_capabilities_max (GDBusConnection
*connection
)
2430 GDBusCapabilityFlags ret
;
2431 ret
= G_DBUS_CAPABILITY_FLAGS_NONE
;
2433 if (G_IS_UNIX_CONNECTION (connection
->stream
))
2434 ret
|= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
;
2439 /* Called in a user thread, lock is not held */
2441 initable_init (GInitable
*initable
,
2442 GCancellable
*cancellable
,
2445 GDBusConnection
*connection
= G_DBUS_CONNECTION (initable
);
2448 /* This method needs to be idempotent to work with the singleton
2449 * pattern. See the docs for g_initable_init(). We implement this by
2452 * Unfortunately we can't use the main lock since the on_worker_*()
2453 * callbacks above needs the lock during initialization (for message
2454 * bus connections we do a synchronous Hello() call on the bus).
2456 g_mutex_lock (&connection
->init_lock
);
2460 /* Make this a no-op if we're already initialized (successfully or
2463 if ((g_atomic_int_get (&connection
->atomic_flags
) & FLAG_INITIALIZED
))
2465 ret
= (connection
->initialization_error
== NULL
);
2469 /* Because of init_lock, we can't get here twice in different threads */
2470 g_assert (connection
->initialization_error
== NULL
);
2472 /* The user can pass multiple (but mutally exclusive) construct
2475 * - stream (of type GIOStream)
2476 * - address (of type gchar*)
2478 * At the end of the day we end up with a non-NULL GIOStream
2479 * object in connection->stream.
2481 if (connection
->address
!= NULL
)
2483 g_assert (connection
->stream
== NULL
);
2485 if ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
) ||
2486 (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS
))
2488 g_set_error_literal (&connection
->initialization_error
,
2490 G_IO_ERROR_INVALID_ARGUMENT
,
2491 _("Unsupported flags encountered when constructing a client-side connection"));
2495 connection
->stream
= g_dbus_address_get_stream_sync (connection
->address
,
2496 NULL
, /* TODO: out_guid */
2498 &connection
->initialization_error
);
2499 if (connection
->stream
== NULL
)
2502 else if (connection
->stream
!= NULL
)
2508 g_assert_not_reached ();
2511 /* Authenticate the connection */
2512 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
)
2514 g_assert (!(connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
));
2515 g_assert (connection
->guid
!= NULL
);
2516 connection
->auth
= _g_dbus_auth_new (connection
->stream
);
2517 if (!_g_dbus_auth_run_server (connection
->auth
,
2518 connection
->authentication_observer
,
2520 (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS
),
2521 get_offered_capabilities_max (connection
),
2522 &connection
->capabilities
,
2523 &connection
->credentials
,
2525 &connection
->initialization_error
))
2528 else if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
)
2530 g_assert (!(connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
));
2531 g_assert (connection
->guid
== NULL
);
2532 connection
->auth
= _g_dbus_auth_new (connection
->stream
);
2533 connection
->guid
= _g_dbus_auth_run_client (connection
->auth
,
2534 connection
->authentication_observer
,
2535 get_offered_capabilities_max (connection
),
2536 &connection
->capabilities
,
2538 &connection
->initialization_error
);
2539 if (connection
->guid
== NULL
)
2543 if (connection
->authentication_observer
!= NULL
)
2545 g_object_unref (connection
->authentication_observer
);
2546 connection
->authentication_observer
= NULL
;
2549 //g_output_stream_flush (G_SOCKET_CONNECTION (connection->stream)
2551 //g_debug ("haz unix fd passing powers: %d", connection->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
2554 /* We want all IO operations to be non-blocking since they happen in
2555 * the worker thread which is shared by _all_ connections.
2557 if (G_IS_SOCKET_CONNECTION (connection
->stream
))
2559 g_socket_set_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection
->stream
)), FALSE
);
2563 G_LOCK (message_bus_lock
);
2564 if (alive_connections
== NULL
)
2565 alive_connections
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
2566 g_hash_table_add (alive_connections
, connection
);
2567 G_UNLOCK (message_bus_lock
);
2569 connection
->worker
= _g_dbus_worker_new (connection
->stream
,
2570 connection
->capabilities
,
2571 ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING
) != 0),
2572 on_worker_message_received
,
2573 on_worker_message_about_to_be_sent
,
2577 /* if a bus connection, call org.freedesktop.DBus.Hello - this is how we're getting a name */
2578 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)
2580 GVariant
*hello_result
;
2582 /* we could lift this restriction by adding code in gdbusprivate.c */
2583 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING
)
2585 g_set_error_literal (&connection
->initialization_error
,
2588 "Cannot use DELAY_MESSAGE_PROCESSING with MESSAGE_BUS_CONNECTION");
2592 hello_result
= g_dbus_connection_call_sync (connection
,
2593 "org.freedesktop.DBus", /* name */
2594 "/org/freedesktop/DBus", /* path */
2595 "org.freedesktop.DBus", /* interface */
2597 NULL
, /* parameters */
2598 G_VARIANT_TYPE ("(s)"),
2599 CALL_FLAGS_INITIALIZING
,
2601 NULL
, /* TODO: cancellable */
2602 &connection
->initialization_error
);
2603 if (hello_result
== NULL
)
2606 g_variant_get (hello_result
, "(s)", &connection
->bus_unique_name
);
2607 g_variant_unref (hello_result
);
2608 //g_debug ("unique name is '%s'", connection->bus_unique_name);
2615 g_assert (connection
->initialization_error
!= NULL
);
2616 g_propagate_error (error
, g_error_copy (connection
->initialization_error
));
2619 g_atomic_int_or (&connection
->atomic_flags
, FLAG_INITIALIZED
);
2620 g_mutex_unlock (&connection
->init_lock
);
2626 initable_iface_init (GInitableIface
*initable_iface
)
2628 initable_iface
->init
= initable_init
;
2631 /* ---------------------------------------------------------------------------------------------------- */
2634 async_initable_iface_init (GAsyncInitableIface
*async_initable_iface
)
2639 /* ---------------------------------------------------------------------------------------------------- */
2642 * g_dbus_connection_new:
2643 * @stream: a #GIOStream
2644 * @guid: (nullable): the GUID to use if a authenticating as a server or %NULL
2645 * @flags: flags describing how to make the connection
2646 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2647 * @cancellable: (nullable): a #GCancellable or %NULL
2648 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2649 * @user_data: the data to pass to @callback
2651 * Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
2652 * with the end represented by @stream.
2654 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2655 * will be put into non-blocking mode.
2657 * The D-Bus connection will interact with @stream from a worker thread.
2658 * As a result, the caller should not interact with @stream after this
2659 * method has been called, except by calling g_object_unref() on it.
2661 * If @observer is not %NULL it may be used to control the
2662 * authentication process.
2664 * When the operation is finished, @callback will be invoked. You can
2665 * then call g_dbus_connection_new_finish() to get the result of the
2668 * This is a asynchronous failable constructor. See
2669 * g_dbus_connection_new_sync() for the synchronous
2675 g_dbus_connection_new (GIOStream
*stream
,
2677 GDBusConnectionFlags flags
,
2678 GDBusAuthObserver
*observer
,
2679 GCancellable
*cancellable
,
2680 GAsyncReadyCallback callback
,
2683 _g_dbus_initialize ();
2685 g_return_if_fail (G_IS_IO_STREAM (stream
));
2687 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION
,
2695 "authentication-observer", observer
,
2700 * g_dbus_connection_new_finish:
2701 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback
2702 * passed to g_dbus_connection_new().
2703 * @error: return location for error or %NULL
2705 * Finishes an operation started with g_dbus_connection_new().
2707 * Returns: a #GDBusConnection or %NULL if @error is set. Free
2708 * with g_object_unref().
2713 g_dbus_connection_new_finish (GAsyncResult
*res
,
2717 GObject
*source_object
;
2719 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2720 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2722 source_object
= g_async_result_get_source_object (res
);
2723 g_assert (source_object
!= NULL
);
2724 object
= g_async_initable_new_finish (G_ASYNC_INITABLE (source_object
),
2727 g_object_unref (source_object
);
2729 return G_DBUS_CONNECTION (object
);
2735 * g_dbus_connection_new_sync:
2736 * @stream: a #GIOStream
2737 * @guid: (nullable): the GUID to use if a authenticating as a server or %NULL
2738 * @flags: flags describing how to make the connection
2739 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2740 * @cancellable: (nullable): a #GCancellable or %NULL
2741 * @error: return location for error or %NULL
2743 * Synchronously sets up a D-Bus connection for exchanging D-Bus messages
2744 * with the end represented by @stream.
2746 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2747 * will be put into non-blocking mode.
2749 * The D-Bus connection will interact with @stream from a worker thread.
2750 * As a result, the caller should not interact with @stream after this
2751 * method has been called, except by calling g_object_unref() on it.
2753 * If @observer is not %NULL it may be used to control the
2754 * authentication process.
2756 * This is a synchronous failable constructor. See
2757 * g_dbus_connection_new() for the asynchronous version.
2759 * Returns: a #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2764 g_dbus_connection_new_sync (GIOStream
*stream
,
2766 GDBusConnectionFlags flags
,
2767 GDBusAuthObserver
*observer
,
2768 GCancellable
*cancellable
,
2771 _g_dbus_initialize ();
2772 g_return_val_if_fail (G_IS_IO_STREAM (stream
), NULL
);
2773 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2774 return g_initable_new (G_TYPE_DBUS_CONNECTION
,
2780 "authentication-observer", observer
,
2784 /* ---------------------------------------------------------------------------------------------------- */
2787 * g_dbus_connection_new_for_address:
2788 * @address: a D-Bus address
2789 * @flags: flags describing how to make the connection
2790 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2791 * @cancellable: (nullable): a #GCancellable or %NULL
2792 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2793 * @user_data: the data to pass to @callback
2795 * Asynchronously connects and sets up a D-Bus client connection for
2796 * exchanging D-Bus messages with an endpoint specified by @address
2797 * which must be in the
2798 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
2800 * This constructor can only be used to initiate client-side
2801 * connections - use g_dbus_connection_new() if you need to act as the
2802 * server. In particular, @flags cannot contain the
2803 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2804 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2806 * When the operation is finished, @callback will be invoked. You can
2807 * then call g_dbus_connection_new_finish() to get the result of the
2810 * If @observer is not %NULL it may be used to control the
2811 * authentication process.
2813 * This is a asynchronous failable constructor. See
2814 * g_dbus_connection_new_for_address_sync() for the synchronous
2820 g_dbus_connection_new_for_address (const gchar
*address
,
2821 GDBusConnectionFlags flags
,
2822 GDBusAuthObserver
*observer
,
2823 GCancellable
*cancellable
,
2824 GAsyncReadyCallback callback
,
2827 _g_dbus_initialize ();
2829 g_return_if_fail (address
!= NULL
);
2831 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION
,
2838 "authentication-observer", observer
,
2843 * g_dbus_connection_new_for_address_finish:
2844 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
2845 * to g_dbus_connection_new()
2846 * @error: return location for error or %NULL
2848 * Finishes an operation started with g_dbus_connection_new_for_address().
2850 * Returns: a #GDBusConnection or %NULL if @error is set. Free with
2856 g_dbus_connection_new_for_address_finish (GAsyncResult
*res
,
2860 GObject
*source_object
;
2862 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2863 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2865 source_object
= g_async_result_get_source_object (res
);
2866 g_assert (source_object
!= NULL
);
2867 object
= g_async_initable_new_finish (G_ASYNC_INITABLE (source_object
),
2870 g_object_unref (source_object
);
2872 return G_DBUS_CONNECTION (object
);
2878 * g_dbus_connection_new_for_address_sync:
2879 * @address: a D-Bus address
2880 * @flags: flags describing how to make the connection
2881 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2882 * @cancellable: (nullable): a #GCancellable or %NULL
2883 * @error: return location for error or %NULL
2885 * Synchronously connects and sets up a D-Bus client connection for
2886 * exchanging D-Bus messages with an endpoint specified by @address
2887 * which must be in the
2888 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
2890 * This constructor can only be used to initiate client-side
2891 * connections - use g_dbus_connection_new_sync() if you need to act
2892 * as the server. In particular, @flags cannot contain the
2893 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2894 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2896 * This is a synchronous failable constructor. See
2897 * g_dbus_connection_new_for_address() for the asynchronous version.
2899 * If @observer is not %NULL it may be used to control the
2900 * authentication process.
2902 * Returns: a #GDBusConnection or %NULL if @error is set. Free with
2908 g_dbus_connection_new_for_address_sync (const gchar
*address
,
2909 GDBusConnectionFlags flags
,
2910 GDBusAuthObserver
*observer
,
2911 GCancellable
*cancellable
,
2914 _g_dbus_initialize ();
2916 g_return_val_if_fail (address
!= NULL
, NULL
);
2917 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2918 return g_initable_new (G_TYPE_DBUS_CONNECTION
,
2923 "authentication-observer", observer
,
2927 /* ---------------------------------------------------------------------------------------------------- */
2930 * g_dbus_connection_set_exit_on_close:
2931 * @connection: a #GDBusConnection
2932 * @exit_on_close: whether the process should be terminated
2933 * when @connection is closed by the remote peer
2935 * Sets whether the process should be terminated when @connection is
2936 * closed by the remote peer. See #GDBusConnection:exit-on-close for
2939 * Note that this function should be used with care. Most modern UNIX
2940 * desktops tie the notion of a user session the session bus, and expect
2941 * all of a users applications to quit when their bus connection goes away.
2942 * If you are setting @exit_on_close to %FALSE for the shared session
2943 * bus connection, you should make sure that your application exits
2944 * when the user session ends.
2949 g_dbus_connection_set_exit_on_close (GDBusConnection
*connection
,
2950 gboolean exit_on_close
)
2952 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
2955 g_atomic_int_or (&connection
->atomic_flags
, FLAG_EXIT_ON_CLOSE
);
2957 g_atomic_int_and (&connection
->atomic_flags
, ~FLAG_EXIT_ON_CLOSE
);
2962 * g_dbus_connection_get_exit_on_close:
2963 * @connection: a #GDBusConnection
2965 * Gets whether the process is terminated when @connection is
2966 * closed by the remote peer. See
2967 * #GDBusConnection:exit-on-close for more details.
2969 * Returns: whether the process is terminated when @connection is
2970 * closed by the remote peer
2975 g_dbus_connection_get_exit_on_close (GDBusConnection
*connection
)
2977 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
2979 if (g_atomic_int_get (&connection
->atomic_flags
) & FLAG_EXIT_ON_CLOSE
)
2986 * g_dbus_connection_get_guid:
2987 * @connection: a #GDBusConnection
2989 * The GUID of the peer performing the role of server when
2990 * authenticating. See #GDBusConnection:guid for more details.
2992 * Returns: The GUID. Do not free this string, it is owned by
2998 g_dbus_connection_get_guid (GDBusConnection
*connection
)
3000 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3001 return connection
->guid
;
3005 * g_dbus_connection_get_unique_name:
3006 * @connection: a #GDBusConnection
3008 * Gets the unique name of @connection as assigned by the message
3009 * bus. This can also be used to figure out if @connection is a
3010 * message bus connection.
3012 * Returns: the unique name or %NULL if @connection is not a message
3013 * bus connection. Do not free this string, it is owned by
3019 g_dbus_connection_get_unique_name (GDBusConnection
*connection
)
3021 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3023 /* do not use g_return_val_if_fail(), we want the memory barrier */
3024 if (!check_initialized (connection
))
3027 return connection
->bus_unique_name
;
3031 * g_dbus_connection_get_peer_credentials:
3032 * @connection: a #GDBusConnection
3034 * Gets the credentials of the authenticated peer. This will always
3035 * return %NULL unless @connection acted as a server
3036 * (e.g. %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER was passed)
3037 * when set up and the client passed credentials as part of the
3038 * authentication process.
3040 * In a message bus setup, the message bus is always the server and
3041 * each application is a client. So this method will always return
3042 * %NULL for message bus clients.
3044 * Returns: (transfer none) (nullable): a #GCredentials or %NULL if not
3045 * available. Do not free this object, it is owned by @connection.
3050 g_dbus_connection_get_peer_credentials (GDBusConnection
*connection
)
3052 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3054 /* do not use g_return_val_if_fail(), we want the memory barrier */
3055 if (!check_initialized (connection
))
3058 return connection
->credentials
;
3061 /* ---------------------------------------------------------------------------------------------------- */
3063 static volatile guint _global_filter_id
= 1;
3066 * g_dbus_connection_add_filter:
3067 * @connection: a #GDBusConnection
3068 * @filter_function: a filter function
3069 * @user_data: user data to pass to @filter_function
3070 * @user_data_free_func: function to free @user_data with when filter
3071 * is removed or %NULL
3073 * Adds a message filter. Filters are handlers that are run on all
3074 * incoming and outgoing messages, prior to standard dispatch. Filters
3075 * are run in the order that they were added. The same handler can be
3076 * added as a filter more than once, in which case it will be run more
3077 * than once. Filters added during a filter callback won't be run on
3078 * the message being processed. Filter functions are allowed to modify
3079 * and even drop messages.
3081 * Note that filters are run in a dedicated message handling thread so
3082 * they can't block and, generally, can't do anything but signal a
3083 * worker thread. Also note that filters are rarely needed - use API
3084 * such as g_dbus_connection_send_message_with_reply(),
3085 * g_dbus_connection_signal_subscribe() or g_dbus_connection_call() instead.
3087 * If a filter consumes an incoming message the message is not
3088 * dispatched anywhere else - not even the standard dispatch machinery
3089 * (that API such as g_dbus_connection_signal_subscribe() and
3090 * g_dbus_connection_send_message_with_reply() relies on) will see the
3091 * message. Similary, if a filter consumes an outgoing message, the
3092 * message will not be sent to the other peer.
3094 * If @user_data_free_func is non-%NULL, it will be called (in the
3095 * thread-default main context of the thread you are calling this
3096 * method from) at some point after @user_data is no longer
3097 * needed. (It is not guaranteed to be called synchronously when the
3098 * filter is removed, and may be called after @connection has been
3101 * Returns: a filter identifier that can be used with
3102 * g_dbus_connection_remove_filter()
3107 g_dbus_connection_add_filter (GDBusConnection
*connection
,
3108 GDBusMessageFilterFunction filter_function
,
3110 GDestroyNotify user_data_free_func
)
3114 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
3115 g_return_val_if_fail (filter_function
!= NULL
, 0);
3116 g_return_val_if_fail (check_initialized (connection
), 0);
3118 CONNECTION_LOCK (connection
);
3119 data
= g_new0 (FilterData
, 1);
3120 data
->id
= g_atomic_int_add (&_global_filter_id
, 1); /* TODO: overflow etc. */
3121 data
->ref_count
= 1;
3122 data
->filter_function
= filter_function
;
3123 data
->user_data
= user_data
;
3124 data
->user_data_free_func
= user_data_free_func
;
3125 data
->context
= g_main_context_ref_thread_default ();
3126 g_ptr_array_add (connection
->filters
, data
);
3127 CONNECTION_UNLOCK (connection
);
3132 /* only called from finalize(), removes all filters */
3134 purge_all_filters (GDBusConnection
*connection
)
3137 for (n
= 0; n
< connection
->filters
->len
; n
++)
3139 FilterData
*data
= connection
->filters
->pdata
[n
];
3141 call_destroy_notify (data
->context
,
3142 data
->user_data_free_func
,
3144 g_main_context_unref (data
->context
);
3150 * g_dbus_connection_remove_filter:
3151 * @connection: a #GDBusConnection
3152 * @filter_id: an identifier obtained from g_dbus_connection_add_filter()
3156 * Note that since filters run in a different thread, there is a race
3157 * condition where it is possible that the filter will be running even
3158 * after calling g_dbus_connection_remove_filter(), so you cannot just
3159 * free data that the filter might be using. Instead, you should pass
3160 * a #GDestroyNotify to g_dbus_connection_add_filter(), which will be
3161 * called when it is guaranteed that the data is no longer needed.
3166 g_dbus_connection_remove_filter (GDBusConnection
*connection
,
3171 FilterData
*to_destroy
;
3173 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
3174 g_return_if_fail (check_initialized (connection
));
3176 CONNECTION_LOCK (connection
);
3179 for (n
= 0; n
< connection
->filters
->len
; n
++)
3181 FilterData
*data
= connection
->filters
->pdata
[n
];
3182 if (data
->id
== filter_id
)
3185 g_ptr_array_remove_index (connection
->filters
, n
);
3187 if (data
->ref_count
== 0)
3192 CONNECTION_UNLOCK (connection
);
3194 /* do free without holding lock */
3195 if (to_destroy
!= NULL
)
3197 if (to_destroy
->user_data_free_func
!= NULL
)
3198 to_destroy
->user_data_free_func (to_destroy
->user_data
);
3199 g_main_context_unref (to_destroy
->context
);
3200 g_free (to_destroy
);
3204 g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id
);
3208 /* ---------------------------------------------------------------------------------------------------- */
3214 gchar
*sender_unique_name
; /* if sender is unique or org.freedesktop.DBus, then that name... otherwise blank */
3215 gchar
*interface_name
;
3219 GDBusSignalFlags flags
;
3220 GArray
*subscribers
;
3225 GDBusSignalCallback callback
;
3227 GDestroyNotify user_data_free_func
;
3229 GMainContext
*context
;
3233 signal_data_free (SignalData
*signal_data
)
3235 g_free (signal_data
->rule
);
3236 g_free (signal_data
->sender
);
3237 g_free (signal_data
->sender_unique_name
);
3238 g_free (signal_data
->interface_name
);
3239 g_free (signal_data
->member
);
3240 g_free (signal_data
->object_path
);
3241 g_free (signal_data
->arg0
);
3242 g_array_free (signal_data
->subscribers
, TRUE
);
3243 g_free (signal_data
);
3247 args_to_rule (const gchar
*sender
,
3248 const gchar
*interface_name
,
3249 const gchar
*member
,
3250 const gchar
*object_path
,
3252 GDBusSignalFlags flags
)
3256 rule
= g_string_new ("type='signal'");
3257 if (flags
& G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE
)
3258 g_string_prepend_c (rule
, '-');
3260 g_string_append_printf (rule
, ",sender='%s'", sender
);
3261 if (interface_name
!= NULL
)
3262 g_string_append_printf (rule
, ",interface='%s'", interface_name
);
3264 g_string_append_printf (rule
, ",member='%s'", member
);
3265 if (object_path
!= NULL
)
3266 g_string_append_printf (rule
, ",path='%s'", object_path
);
3270 if (flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH
)
3271 g_string_append_printf (rule
, ",arg0path='%s'", arg0
);
3272 else if (flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE
)
3273 g_string_append_printf (rule
, ",arg0namespace='%s'", arg0
);
3275 g_string_append_printf (rule
, ",arg0='%s'", arg0
);
3278 return g_string_free (rule
, FALSE
);
3281 static volatile guint _global_subscriber_id
= 1;
3282 static volatile guint _global_registration_id
= 1;
3283 static volatile guint _global_subtree_registration_id
= 1;
3285 /* ---------------------------------------------------------------------------------------------------- */
3287 /* Called in a user thread, lock is held */
3289 add_match_rule (GDBusConnection
*connection
,
3290 const gchar
*match_rule
)
3293 GDBusMessage
*message
;
3295 if (match_rule
[0] == '-')
3298 message
= g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3299 "/org/freedesktop/DBus", /* path */
3300 "org.freedesktop.DBus", /* interface */
3302 g_dbus_message_set_body (message
, g_variant_new ("(s)", match_rule
));
3304 if (!g_dbus_connection_send_message_unlocked (connection
,
3306 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
3310 g_critical ("Error while sending AddMatch() message: %s", error
->message
);
3311 g_error_free (error
);
3313 g_object_unref (message
);
3316 /* ---------------------------------------------------------------------------------------------------- */
3318 /* Called in a user thread, lock is held */
3320 remove_match_rule (GDBusConnection
*connection
,
3321 const gchar
*match_rule
)
3324 GDBusMessage
*message
;
3326 if (match_rule
[0] == '-')
3329 message
= g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3330 "/org/freedesktop/DBus", /* path */
3331 "org.freedesktop.DBus", /* interface */
3333 g_dbus_message_set_body (message
, g_variant_new ("(s)", match_rule
));
3336 if (!g_dbus_connection_send_message_unlocked (connection
,
3338 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
3342 /* If we could get G_IO_ERROR_CLOSED here, it wouldn't be reasonable to
3343 * critical; but we're holding the lock, and our caller checked whether
3344 * we were already closed, so we can't get that error.
3346 g_critical ("Error while sending RemoveMatch() message: %s", error
->message
);
3347 g_error_free (error
);
3349 g_object_unref (message
);
3352 /* ---------------------------------------------------------------------------------------------------- */
3355 is_signal_data_for_name_lost_or_acquired (SignalData
*signal_data
)
3357 return g_strcmp0 (signal_data
->sender_unique_name
, "org.freedesktop.DBus") == 0 &&
3358 g_strcmp0 (signal_data
->interface_name
, "org.freedesktop.DBus") == 0 &&
3359 g_strcmp0 (signal_data
->object_path
, "/org/freedesktop/DBus") == 0 &&
3360 (g_strcmp0 (signal_data
->member
, "NameLost") == 0 ||
3361 g_strcmp0 (signal_data
->member
, "NameAcquired") == 0);
3364 /* ---------------------------------------------------------------------------------------------------- */
3367 * g_dbus_connection_signal_subscribe:
3368 * @connection: a #GDBusConnection
3369 * @sender: (nullable): sender name to match on (unique or well-known name)
3370 * or %NULL to listen from all senders
3371 * @interface_name: (nullable): D-Bus interface name to match on or %NULL to
3372 * match on all interfaces
3373 * @member: (nullable): D-Bus signal name to match on or %NULL to match on
3375 * @object_path: (nullable): object path to match on or %NULL to match on
3377 * @arg0: (nullable): contents of first string argument to match on or %NULL
3378 * to match on all kinds of arguments
3379 * @flags: #GDBusSignalFlags describing how arg0 is used in subscribing to the
3381 * @callback: callback to invoke when there is a signal matching the requested data
3382 * @user_data: user data to pass to @callback
3383 * @user_data_free_func: (nullable): function to free @user_data with when
3384 * subscription is removed or %NULL
3386 * Subscribes to signals on @connection and invokes @callback with a whenever
3387 * the signal is received. Note that @callback will be invoked in the
3388 * [thread-default main context][g-main-context-push-thread-default]
3389 * of the thread you are calling this method from.
3391 * If @connection is not a message bus connection, @sender must be
3394 * If @sender is a well-known name note that @callback is invoked with
3395 * the unique name for the owner of @sender, not the well-known name
3396 * as one would expect. This is because the message bus rewrites the
3397 * name. As such, to avoid certain race conditions, users should be
3398 * tracking the name owner of the well-known name and use that when
3399 * processing the received signal.
3401 * If one of %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE or
3402 * %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH are given, @arg0 is
3403 * interpreted as part of a namespace or path. The first argument
3404 * of a signal is matched against that part as specified by D-Bus.
3406 * If @user_data_free_func is non-%NULL, it will be called (in the
3407 * thread-default main context of the thread you are calling this
3408 * method from) at some point after @user_data is no longer
3409 * needed. (It is not guaranteed to be called synchronously when the
3410 * signal is unsubscribed from, and may be called after @connection
3411 * has been destroyed.)
3413 * Returns: a subscription identifier that can be used with g_dbus_connection_signal_unsubscribe()
3418 g_dbus_connection_signal_subscribe (GDBusConnection
*connection
,
3419 const gchar
*sender
,
3420 const gchar
*interface_name
,
3421 const gchar
*member
,
3422 const gchar
*object_path
,
3424 GDBusSignalFlags flags
,
3425 GDBusSignalCallback callback
,
3427 GDestroyNotify user_data_free_func
)
3430 SignalData
*signal_data
;
3431 SignalSubscriber subscriber
;
3432 GPtrArray
*signal_data_array
;
3433 const gchar
*sender_unique_name
;
3435 /* Right now we abort if AddMatch() fails since it can only fail with the bus being in
3436 * an OOM condition. We might want to change that but that would involve making
3437 * g_dbus_connection_signal_subscribe() asynchronous and having the call sites
3438 * handle that. And there's really no sensible way of handling this short of retrying
3439 * to add the match rule... and then there's the little thing that, hey, maybe there's
3440 * a reason the bus in an OOM condition.
3442 * Doable, but not really sure it's worth it...
3445 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
3446 g_return_val_if_fail (sender
== NULL
|| (g_dbus_is_name (sender
) && (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)), 0);
3447 g_return_val_if_fail (interface_name
== NULL
|| g_dbus_is_interface_name (interface_name
), 0);
3448 g_return_val_if_fail (member
== NULL
|| g_dbus_is_member_name (member
), 0);
3449 g_return_val_if_fail (object_path
== NULL
|| g_variant_is_object_path (object_path
), 0);
3450 g_return_val_if_fail (callback
!= NULL
, 0);
3451 g_return_val_if_fail (check_initialized (connection
), 0);
3452 g_return_val_if_fail (!((flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH
) && (flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE
)), 0);
3453 g_return_val_if_fail (!(arg0
== NULL
&& (flags
& (G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH
| G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE
))), 0);
3455 CONNECTION_LOCK (connection
);
3457 /* If G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE was specified, we will end up
3458 * with a '-' character to prefix the rule (which will otherwise be
3461 * This allows us to hash the rule and do our lifecycle tracking in
3462 * the usual way, but the '-' prevents the match rule from ever
3463 * actually being send to the bus (either for add or remove).
3465 rule
= args_to_rule (sender
, interface_name
, member
, object_path
, arg0
, flags
);
3467 if (sender
!= NULL
&& (g_dbus_is_unique_name (sender
) || g_strcmp0 (sender
, "org.freedesktop.DBus") == 0))
3468 sender_unique_name
= sender
;
3470 sender_unique_name
= "";
3472 subscriber
.callback
= callback
;
3473 subscriber
.user_data
= user_data
;
3474 subscriber
.user_data_free_func
= user_data_free_func
;
3475 subscriber
.id
= g_atomic_int_add (&_global_subscriber_id
, 1); /* TODO: overflow etc. */
3476 subscriber
.context
= g_main_context_ref_thread_default ();
3478 /* see if we've already have this rule */
3479 signal_data
= g_hash_table_lookup (connection
->map_rule_to_signal_data
, rule
);
3480 if (signal_data
!= NULL
)
3482 g_array_append_val (signal_data
->subscribers
, subscriber
);
3487 signal_data
= g_new0 (SignalData
, 1);
3488 signal_data
->rule
= rule
;
3489 signal_data
->sender
= g_strdup (sender
);
3490 signal_data
->sender_unique_name
= g_strdup (sender_unique_name
);
3491 signal_data
->interface_name
= g_strdup (interface_name
);
3492 signal_data
->member
= g_strdup (member
);
3493 signal_data
->object_path
= g_strdup (object_path
);
3494 signal_data
->arg0
= g_strdup (arg0
);
3495 signal_data
->flags
= flags
;
3496 signal_data
->subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3497 g_array_append_val (signal_data
->subscribers
, subscriber
);
3499 g_hash_table_insert (connection
->map_rule_to_signal_data
,
3503 /* Add the match rule to the bus...
3505 * Avoid adding match rules for NameLost and NameAcquired messages - the bus will
3506 * always send such messages to us.
3508 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)
3510 if (!is_signal_data_for_name_lost_or_acquired (signal_data
))
3511 add_match_rule (connection
, signal_data
->rule
);
3514 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
,
3515 signal_data
->sender_unique_name
);
3516 if (signal_data_array
== NULL
)
3518 signal_data_array
= g_ptr_array_new ();
3519 g_hash_table_insert (connection
->map_sender_unique_name_to_signal_data_array
,
3520 g_strdup (signal_data
->sender_unique_name
),
3523 g_ptr_array_add (signal_data_array
, signal_data
);
3526 g_hash_table_insert (connection
->map_id_to_signal_data
,
3527 GUINT_TO_POINTER (subscriber
.id
),
3530 CONNECTION_UNLOCK (connection
);
3532 return subscriber
.id
;
3535 /* ---------------------------------------------------------------------------------------------------- */
3537 /* called in any thread */
3538 /* must hold lock when calling this (except if connection->finalizing is TRUE) */
3540 unsubscribe_id_internal (GDBusConnection
*connection
,
3541 guint subscription_id
,
3542 GArray
*out_removed_subscribers
)
3544 SignalData
*signal_data
;
3545 GPtrArray
*signal_data_array
;
3548 signal_data
= g_hash_table_lookup (connection
->map_id_to_signal_data
,
3549 GUINT_TO_POINTER (subscription_id
));
3550 if (signal_data
== NULL
)
3552 /* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
3556 for (n
= 0; n
< signal_data
->subscribers
->len
; n
++)
3558 SignalSubscriber
*subscriber
;
3560 subscriber
= &(g_array_index (signal_data
->subscribers
, SignalSubscriber
, n
));
3561 if (subscriber
->id
!= subscription_id
)
3564 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_signal_data
,
3565 GUINT_TO_POINTER (subscription_id
)));
3566 g_array_append_val (out_removed_subscribers
, *subscriber
);
3567 g_array_remove_index (signal_data
->subscribers
, n
);
3569 if (signal_data
->subscribers
->len
== 0)
3571 g_warn_if_fail (g_hash_table_remove (connection
->map_rule_to_signal_data
, signal_data
->rule
));
3573 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
,
3574 signal_data
->sender_unique_name
);
3575 g_warn_if_fail (signal_data_array
!= NULL
);
3576 g_warn_if_fail (g_ptr_array_remove (signal_data_array
, signal_data
));
3578 if (signal_data_array
->len
== 0)
3580 g_warn_if_fail (g_hash_table_remove (connection
->map_sender_unique_name_to_signal_data_array
,
3581 signal_data
->sender_unique_name
));
3584 /* remove the match rule from the bus unless NameLost or NameAcquired (see subscribe()) */
3585 if ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
) &&
3586 !is_signal_data_for_name_lost_or_acquired (signal_data
) &&
3587 !g_dbus_connection_is_closed (connection
) &&
3588 !connection
->finalizing
)
3590 /* The check for g_dbus_connection_is_closed() means that
3591 * sending the RemoveMatch message can't fail with
3592 * G_IO_ERROR_CLOSED, because we're holding the lock,
3593 * so on_worker_closed() can't happen between the check we just
3594 * did, and releasing the lock later.
3596 remove_match_rule (connection
, signal_data
->rule
);
3599 signal_data_free (signal_data
);
3605 g_assert_not_reached ();
3612 * g_dbus_connection_signal_unsubscribe:
3613 * @connection: a #GDBusConnection
3614 * @subscription_id: a subscription id obtained from
3615 * g_dbus_connection_signal_subscribe()
3617 * Unsubscribes from signals.
3622 g_dbus_connection_signal_unsubscribe (GDBusConnection
*connection
,
3623 guint subscription_id
)
3625 GArray
*subscribers
;
3628 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
3629 g_return_if_fail (check_initialized (connection
));
3631 subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3633 CONNECTION_LOCK (connection
);
3634 unsubscribe_id_internal (connection
,
3637 CONNECTION_UNLOCK (connection
);
3640 g_assert (subscribers
->len
== 0 || subscribers
->len
== 1);
3642 /* call GDestroyNotify without lock held */
3643 for (n
= 0; n
< subscribers
->len
; n
++)
3645 SignalSubscriber
*subscriber
;
3646 subscriber
= &(g_array_index (subscribers
, SignalSubscriber
, n
));
3647 call_destroy_notify (subscriber
->context
,
3648 subscriber
->user_data_free_func
,
3649 subscriber
->user_data
);
3650 g_main_context_unref (subscriber
->context
);
3653 g_array_free (subscribers
, TRUE
);
3656 /* ---------------------------------------------------------------------------------------------------- */
3660 guint subscription_id
;
3661 GDBusSignalCallback callback
;
3663 GDBusMessage
*message
;
3664 GDBusConnection
*connection
;
3665 const gchar
*sender
;
3667 const gchar
*interface
;
3668 const gchar
*member
;
3671 /* called on delivery thread (e.g. where g_dbus_connection_signal_subscribe() was called) with
3675 emit_signal_instance_in_idle_cb (gpointer data
)
3677 SignalInstance
*signal_instance
= data
;
3678 GVariant
*parameters
;
3679 gboolean has_subscription
;
3681 parameters
= g_dbus_message_get_body (signal_instance
->message
);
3682 if (parameters
== NULL
)
3684 parameters
= g_variant_new ("()");
3685 g_variant_ref_sink (parameters
);
3689 g_variant_ref_sink (parameters
);
3693 g_print ("in emit_signal_instance_in_idle_cb (id=%d sender=%s path=%s interface=%s member=%s params=%s)\n",
3694 signal_instance
->subscription_id
,
3695 signal_instance
->sender
,
3696 signal_instance
->path
,
3697 signal_instance
->interface
,
3698 signal_instance
->member
,
3699 g_variant_print (parameters
, TRUE
));
3702 /* Careful here, don't do the callback if we no longer has the subscription */
3703 CONNECTION_LOCK (signal_instance
->connection
);
3704 has_subscription
= FALSE
;
3705 if (g_hash_table_lookup (signal_instance
->connection
->map_id_to_signal_data
,
3706 GUINT_TO_POINTER (signal_instance
->subscription_id
)) != NULL
)
3707 has_subscription
= TRUE
;
3708 CONNECTION_UNLOCK (signal_instance
->connection
);
3710 if (has_subscription
)
3711 signal_instance
->callback (signal_instance
->connection
,
3712 signal_instance
->sender
,
3713 signal_instance
->path
,
3714 signal_instance
->interface
,
3715 signal_instance
->member
,
3717 signal_instance
->user_data
);
3719 g_variant_unref (parameters
);
3725 signal_instance_free (SignalInstance
*signal_instance
)
3727 g_object_unref (signal_instance
->message
);
3728 g_object_unref (signal_instance
->connection
);
3729 g_free (signal_instance
);
3733 namespace_rule_matches (const gchar
*namespace,
3739 len_namespace
= strlen (namespace);
3740 len_name
= strlen (name
);
3742 if (len_name
< len_namespace
)
3745 if (memcmp (namespace, name
, len_namespace
) != 0)
3748 return len_namespace
== len_name
|| name
[len_namespace
] == '.';
3752 path_rule_matches (const gchar
*path_a
,
3753 const gchar
*path_b
)
3757 len_a
= strlen (path_a
);
3758 len_b
= strlen (path_b
);
3760 if (len_a
< len_b
&& (len_a
== 0 || path_a
[len_a
- 1] != '/'))
3763 if (len_b
< len_a
&& (len_b
== 0 || path_b
[len_b
- 1] != '/'))
3766 return memcmp (path_a
, path_b
, MIN (len_a
, len_b
)) == 0;
3769 /* called in GDBusWorker thread WITH lock held */
3771 schedule_callbacks (GDBusConnection
*connection
,
3772 GPtrArray
*signal_data_array
,
3773 GDBusMessage
*message
,
3774 const gchar
*sender
)
3777 const gchar
*interface
;
3778 const gchar
*member
;
3787 interface
= g_dbus_message_get_interface (message
);
3788 member
= g_dbus_message_get_member (message
);
3789 path
= g_dbus_message_get_path (message
);
3790 arg0
= g_dbus_message_get_arg0 (message
);
3793 g_print ("In schedule_callbacks:\n"
3795 " interface = '%s'\n"
3806 /* TODO: if this is slow, then we can change signal_data_array into
3807 * map_object_path_to_signal_data_array or something.
3809 for (n
= 0; n
< signal_data_array
->len
; n
++)
3811 SignalData
*signal_data
= signal_data_array
->pdata
[n
];
3813 if (signal_data
->interface_name
!= NULL
&& g_strcmp0 (signal_data
->interface_name
, interface
) != 0)
3816 if (signal_data
->member
!= NULL
&& g_strcmp0 (signal_data
->member
, member
) != 0)
3819 if (signal_data
->object_path
!= NULL
&& g_strcmp0 (signal_data
->object_path
, path
) != 0)
3822 if (signal_data
->arg0
!= NULL
)
3827 if (signal_data
->flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE
)
3829 if (!namespace_rule_matches (signal_data
->arg0
, arg0
))
3832 else if (signal_data
->flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH
)
3834 if (!path_rule_matches (signal_data
->arg0
, arg0
))
3837 else if (!g_str_equal (signal_data
->arg0
, arg0
))
3841 for (m
= 0; m
< signal_data
->subscribers
->len
; m
++)
3843 SignalSubscriber
*subscriber
;
3844 GSource
*idle_source
;
3845 SignalInstance
*signal_instance
;
3847 subscriber
= &(g_array_index (signal_data
->subscribers
, SignalSubscriber
, m
));
3849 signal_instance
= g_new0 (SignalInstance
, 1);
3850 signal_instance
->subscription_id
= subscriber
->id
;
3851 signal_instance
->callback
= subscriber
->callback
;
3852 signal_instance
->user_data
= subscriber
->user_data
;
3853 signal_instance
->message
= g_object_ref (message
);
3854 signal_instance
->connection
= g_object_ref (connection
);
3855 signal_instance
->sender
= sender
;
3856 signal_instance
->path
= path
;
3857 signal_instance
->interface
= interface
;
3858 signal_instance
->member
= member
;
3860 idle_source
= g_idle_source_new ();
3861 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
3862 g_source_set_callback (idle_source
,
3863 emit_signal_instance_in_idle_cb
,
3865 (GDestroyNotify
) signal_instance_free
);
3866 g_source_set_name (idle_source
, "[gio] emit_signal_instance_in_idle_cb");
3867 g_source_attach (idle_source
, subscriber
->context
);
3868 g_source_unref (idle_source
);
3873 /* called in GDBusWorker thread with lock held */
3875 distribute_signals (GDBusConnection
*connection
,
3876 GDBusMessage
*message
)
3878 GPtrArray
*signal_data_array
;
3879 const gchar
*sender
;
3881 sender
= g_dbus_message_get_sender (message
);
3883 if (G_UNLIKELY (_g_dbus_debug_signal ()))
3885 _g_dbus_debug_print_lock ();
3886 g_print ("========================================================================\n"
3887 "GDBus-debug:Signal:\n"
3888 " <<<< RECEIVED SIGNAL %s.%s\n"
3890 " sent by name %s\n",
3891 g_dbus_message_get_interface (message
),
3892 g_dbus_message_get_member (message
),
3893 g_dbus_message_get_path (message
),
3894 sender
!= NULL
? sender
: "(none)");
3895 _g_dbus_debug_print_unlock ();
3898 /* collect subscribers that match on sender */
3901 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
, sender
);
3902 if (signal_data_array
!= NULL
)
3903 schedule_callbacks (connection
, signal_data_array
, message
, sender
);
3906 /* collect subscribers not matching on sender */
3907 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
, "");
3908 if (signal_data_array
!= NULL
)
3909 schedule_callbacks (connection
, signal_data_array
, message
, sender
);
3912 /* ---------------------------------------------------------------------------------------------------- */
3914 /* only called from finalize(), removes all subscriptions */
3916 purge_all_signal_subscriptions (GDBusConnection
*connection
)
3918 GHashTableIter iter
;
3921 GArray
*subscribers
;
3924 ids
= g_array_new (FALSE
, FALSE
, sizeof (guint
));
3925 g_hash_table_iter_init (&iter
, connection
->map_id_to_signal_data
);
3926 while (g_hash_table_iter_next (&iter
, &key
, NULL
))
3928 guint subscription_id
= GPOINTER_TO_UINT (key
);
3929 g_array_append_val (ids
, subscription_id
);
3932 subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3933 for (n
= 0; n
< ids
->len
; n
++)
3935 guint subscription_id
= g_array_index (ids
, guint
, n
);
3936 unsubscribe_id_internal (connection
,
3940 g_array_free (ids
, TRUE
);
3942 /* call GDestroyNotify without lock held */
3943 for (n
= 0; n
< subscribers
->len
; n
++)
3945 SignalSubscriber
*subscriber
;
3946 subscriber
= &(g_array_index (subscribers
, SignalSubscriber
, n
));
3947 call_destroy_notify (subscriber
->context
,
3948 subscriber
->user_data_free_func
,
3949 subscriber
->user_data
);
3950 g_main_context_unref (subscriber
->context
);
3953 g_array_free (subscribers
, TRUE
);
3956 /* ---------------------------------------------------------------------------------------------------- */
3958 static GDBusInterfaceVTable
*
3959 _g_dbus_interface_vtable_copy (const GDBusInterfaceVTable
*vtable
)
3961 /* Don't waste memory by copying padding - remember to update this
3962 * when changing struct _GDBusInterfaceVTable in gdbusconnection.h
3964 return g_memdup ((gconstpointer
) vtable
, 3 * sizeof (gpointer
));
3968 _g_dbus_interface_vtable_free (GDBusInterfaceVTable
*vtable
)
3973 /* ---------------------------------------------------------------------------------------------------- */
3975 static GDBusSubtreeVTable
*
3976 _g_dbus_subtree_vtable_copy (const GDBusSubtreeVTable
*vtable
)
3978 /* Don't waste memory by copying padding - remember to update this
3979 * when changing struct _GDBusSubtreeVTable in gdbusconnection.h
3981 return g_memdup ((gconstpointer
) vtable
, 3 * sizeof (gpointer
));
3985 _g_dbus_subtree_vtable_free (GDBusSubtreeVTable
*vtable
)
3990 /* ---------------------------------------------------------------------------------------------------- */
3992 struct ExportedObject
3995 GDBusConnection
*connection
;
3997 /* maps gchar* -> ExportedInterface* */
3998 GHashTable
*map_if_name_to_ei
;
4001 /* only called with lock held */
4003 exported_object_free (ExportedObject
*eo
)
4005 g_free (eo
->object_path
);
4006 g_hash_table_unref (eo
->map_if_name_to_ei
);
4015 gchar
*interface_name
;
4016 GDBusInterfaceVTable
*vtable
;
4017 GDBusInterfaceInfo
*interface_info
;
4019 GMainContext
*context
;
4021 GDestroyNotify user_data_free_func
;
4022 } ExportedInterface
;
4024 /* called with lock held */
4026 exported_interface_free (ExportedInterface
*ei
)
4028 g_dbus_interface_info_cache_release (ei
->interface_info
);
4029 g_dbus_interface_info_unref ((GDBusInterfaceInfo
*) ei
->interface_info
);
4031 call_destroy_notify (ei
->context
,
4032 ei
->user_data_free_func
,
4035 g_main_context_unref (ei
->context
);
4037 g_free (ei
->interface_name
);
4038 _g_dbus_interface_vtable_free (ei
->vtable
);
4042 /* ---------------------------------------------------------------------------------------------------- */
4044 /* Convenience function to check if @registration_id (if not zero) or
4045 * @subtree_registration_id (if not zero) has been unregistered. If
4046 * so, returns %TRUE.
4048 * May be called by any thread. Caller must *not* hold lock.
4051 has_object_been_unregistered (GDBusConnection
*connection
,
4052 guint registration_id
,
4053 guint subtree_registration_id
)
4057 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
4061 CONNECTION_LOCK (connection
);
4062 if (registration_id
!= 0 && g_hash_table_lookup (connection
->map_id_to_ei
,
4063 GUINT_TO_POINTER (registration_id
)) == NULL
)
4067 else if (subtree_registration_id
!= 0 && g_hash_table_lookup (connection
->map_id_to_es
,
4068 GUINT_TO_POINTER (subtree_registration_id
)) == NULL
)
4072 CONNECTION_UNLOCK (connection
);
4077 /* ---------------------------------------------------------------------------------------------------- */
4081 GDBusConnection
*connection
;
4082 GDBusMessage
*message
;
4084 const gchar
*property_name
;
4085 const GDBusInterfaceVTable
*vtable
;
4086 GDBusInterfaceInfo
*interface_info
;
4087 const GDBusPropertyInfo
*property_info
;
4088 guint registration_id
;
4089 guint subtree_registration_id
;
4093 property_data_free (PropertyData
*data
)
4095 g_object_unref (data
->connection
);
4096 g_object_unref (data
->message
);
4100 /* called in thread where object was registered - no locks held */
4102 invoke_get_property_in_idle_cb (gpointer _data
)
4104 PropertyData
*data
= _data
;
4107 GDBusMessage
*reply
;
4109 if (has_object_been_unregistered (data
->connection
,
4110 data
->registration_id
,
4111 data
->subtree_registration_id
))
4113 reply
= g_dbus_message_new_method_error (data
->message
,
4114 "org.freedesktop.DBus.Error.UnknownMethod",
4115 _("No such interface āorg.freedesktop.DBus.Propertiesā on object at path %s"),
4116 g_dbus_message_get_path (data
->message
));
4117 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4118 g_object_unref (reply
);
4123 value
= data
->vtable
->get_property (data
->connection
,
4124 g_dbus_message_get_sender (data
->message
),
4125 g_dbus_message_get_path (data
->message
),
4126 data
->interface_info
->name
,
4127 data
->property_name
,
4134 g_assert_no_error (error
);
4136 g_variant_take_ref (value
);
4137 reply
= g_dbus_message_new_method_reply (data
->message
);
4138 g_dbus_message_set_body (reply
, g_variant_new ("(v)", value
));
4139 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4140 g_variant_unref (value
);
4141 g_object_unref (reply
);
4145 gchar
*dbus_error_name
;
4146 g_assert (error
!= NULL
);
4147 dbus_error_name
= g_dbus_error_encode_gerror (error
);
4148 reply
= g_dbus_message_new_method_error_literal (data
->message
,
4151 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4152 g_free (dbus_error_name
);
4153 g_error_free (error
);
4154 g_object_unref (reply
);
4161 /* called in thread where object was registered - no locks held */
4163 invoke_set_property_in_idle_cb (gpointer _data
)
4165 PropertyData
*data
= _data
;
4167 GDBusMessage
*reply
;
4173 g_variant_get (g_dbus_message_get_body (data
->message
),
4179 if (!data
->vtable
->set_property (data
->connection
,
4180 g_dbus_message_get_sender (data
->message
),
4181 g_dbus_message_get_path (data
->message
),
4182 data
->interface_info
->name
,
4183 data
->property_name
,
4188 gchar
*dbus_error_name
;
4189 g_assert (error
!= NULL
);
4190 dbus_error_name
= g_dbus_error_encode_gerror (error
);
4191 reply
= g_dbus_message_new_method_error_literal (data
->message
,
4194 g_free (dbus_error_name
);
4195 g_error_free (error
);
4199 reply
= g_dbus_message_new_method_reply (data
->message
);
4202 g_assert (reply
!= NULL
);
4203 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4204 g_object_unref (reply
);
4205 g_variant_unref (value
);
4210 /* called in any thread with connection's lock held */
4212 validate_and_maybe_schedule_property_getset (GDBusConnection
*connection
,
4213 GDBusMessage
*message
,
4214 guint registration_id
,
4215 guint subtree_registration_id
,
4217 GDBusInterfaceInfo
*interface_info
,
4218 const GDBusInterfaceVTable
*vtable
,
4219 GMainContext
*main_context
,
4223 const char *interface_name
;
4224 const char *property_name
;
4225 const GDBusPropertyInfo
*property_info
;
4226 GSource
*idle_source
;
4227 PropertyData
*property_data
;
4228 GDBusMessage
*reply
;
4233 g_variant_get (g_dbus_message_get_body (message
),
4238 g_variant_get (g_dbus_message_get_body (message
),
4247 /* Check that the property exists - if not fail with org.freedesktop.DBus.Error.InvalidArgs
4249 property_info
= NULL
;
4251 /* TODO: the cost of this is O(n) - it might be worth caching the result */
4252 property_info
= g_dbus_interface_info_lookup_property (interface_info
, property_name
);
4253 if (property_info
== NULL
)
4255 reply
= g_dbus_message_new_method_error (message
,
4256 "org.freedesktop.DBus.Error.InvalidArgs",
4257 _("No such property ā%sā"),
4259 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4260 g_object_unref (reply
);
4265 if (is_get
&& !(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
))
4267 reply
= g_dbus_message_new_method_error (message
,
4268 "org.freedesktop.DBus.Error.InvalidArgs",
4269 _("Property ā%sā is not readable"),
4271 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4272 g_object_unref (reply
);
4276 else if (!is_get
&& !(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE
))
4278 reply
= g_dbus_message_new_method_error (message
,
4279 "org.freedesktop.DBus.Error.InvalidArgs",
4280 _("Property ā%sā is not writable"),
4282 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4283 g_object_unref (reply
);
4292 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
4293 * of the given value is wrong
4295 g_variant_get_child (g_dbus_message_get_body (message
), 2, "v", &value
);
4296 if (g_strcmp0 (g_variant_get_type_string (value
), property_info
->signature
) != 0)
4298 reply
= g_dbus_message_new_method_error (message
,
4299 "org.freedesktop.DBus.Error.InvalidArgs",
4300 _("Error setting property ā%sā: Expected type ā%sā but got ā%sā"),
4301 property_name
, property_info
->signature
,
4302 g_variant_get_type_string (value
));
4303 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4304 g_variant_unref (value
);
4305 g_object_unref (reply
);
4310 g_variant_unref (value
);
4313 /* If the vtable pointer for get_property() resp. set_property() is
4314 * NULL then dispatch the call via the method_call() handler.
4318 if (vtable
->get_property
== NULL
)
4320 schedule_method_call (connection
, message
, registration_id
, subtree_registration_id
,
4321 interface_info
, NULL
, property_info
, g_dbus_message_get_body (message
),
4322 vtable
, main_context
, user_data
);
4329 if (vtable
->set_property
== NULL
)
4331 schedule_method_call (connection
, message
, registration_id
, subtree_registration_id
,
4332 interface_info
, NULL
, property_info
, g_dbus_message_get_body (message
),
4333 vtable
, main_context
, user_data
);
4339 /* ok, got the property info - call user code in an idle handler */
4340 property_data
= g_new0 (PropertyData
, 1);
4341 property_data
->connection
= g_object_ref (connection
);
4342 property_data
->message
= g_object_ref (message
);
4343 property_data
->user_data
= user_data
;
4344 property_data
->property_name
= property_name
;
4345 property_data
->vtable
= vtable
;
4346 property_data
->interface_info
= interface_info
;
4347 property_data
->property_info
= property_info
;
4348 property_data
->registration_id
= registration_id
;
4349 property_data
->subtree_registration_id
= subtree_registration_id
;
4351 idle_source
= g_idle_source_new ();
4352 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4353 g_source_set_callback (idle_source
,
4354 is_get
? invoke_get_property_in_idle_cb
: invoke_set_property_in_idle_cb
,
4356 (GDestroyNotify
) property_data_free
);
4358 g_source_set_name (idle_source
, "[gio] invoke_get_property_in_idle_cb");
4360 g_source_set_name (idle_source
, "[gio] invoke_set_property_in_idle_cb");
4361 g_source_attach (idle_source
, main_context
);
4362 g_source_unref (idle_source
);
4370 /* called in GDBusWorker thread with connection's lock held */
4372 handle_getset_property (GDBusConnection
*connection
,
4374 GDBusMessage
*message
,
4377 ExportedInterface
*ei
;
4379 const char *interface_name
;
4380 const char *property_name
;
4385 g_variant_get (g_dbus_message_get_body (message
),
4390 g_variant_get (g_dbus_message_get_body (message
),
4396 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4397 * no such interface registered
4399 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
4402 GDBusMessage
*reply
;
4403 reply
= g_dbus_message_new_method_error (message
,
4404 "org.freedesktop.DBus.Error.InvalidArgs",
4405 _("No such interface ā%sā"),
4407 g_dbus_connection_send_message_unlocked (eo
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4408 g_object_unref (reply
);
4413 handled
= validate_and_maybe_schedule_property_getset (eo
->connection
,
4426 /* ---------------------------------------------------------------------------------------------------- */
4430 GDBusConnection
*connection
;
4431 GDBusMessage
*message
;
4433 const GDBusInterfaceVTable
*vtable
;
4434 GDBusInterfaceInfo
*interface_info
;
4435 guint registration_id
;
4436 guint subtree_registration_id
;
4437 } PropertyGetAllData
;
4440 property_get_all_data_free (PropertyData
*data
)
4442 g_object_unref (data
->connection
);
4443 g_object_unref (data
->message
);
4447 /* called in thread where object was registered - no locks held */
4449 invoke_get_all_properties_in_idle_cb (gpointer _data
)
4451 PropertyGetAllData
*data
= _data
;
4452 GVariantBuilder builder
;
4453 GDBusMessage
*reply
;
4456 if (has_object_been_unregistered (data
->connection
,
4457 data
->registration_id
,
4458 data
->subtree_registration_id
))
4460 reply
= g_dbus_message_new_method_error (data
->message
,
4461 "org.freedesktop.DBus.Error.UnknownMethod",
4462 _("No such interface āorg.freedesktop.DBus.Propertiesā on object at path %s"),
4463 g_dbus_message_get_path (data
->message
));
4464 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4465 g_object_unref (reply
);
4469 /* TODO: Right now we never fail this call - we just omit values if
4470 * a get_property() call is failing.
4472 * We could fail the whole call if just a single get_property() call
4473 * returns an error. We need clarification in the D-Bus spec about this.
4475 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("(a{sv})"));
4476 g_variant_builder_open (&builder
, G_VARIANT_TYPE ("a{sv}"));
4477 for (n
= 0; data
->interface_info
->properties
!= NULL
&& data
->interface_info
->properties
[n
] != NULL
; n
++)
4479 const GDBusPropertyInfo
*property_info
= data
->interface_info
->properties
[n
];
4482 if (!(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
))
4485 value
= data
->vtable
->get_property (data
->connection
,
4486 g_dbus_message_get_sender (data
->message
),
4487 g_dbus_message_get_path (data
->message
),
4488 data
->interface_info
->name
,
4489 property_info
->name
,
4496 g_variant_take_ref (value
);
4497 g_variant_builder_add (&builder
,
4499 property_info
->name
,
4501 g_variant_unref (value
);
4503 g_variant_builder_close (&builder
);
4505 reply
= g_dbus_message_new_method_reply (data
->message
);
4506 g_dbus_message_set_body (reply
, g_variant_builder_end (&builder
));
4507 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4508 g_object_unref (reply
);
4515 interface_has_readable_properties (GDBusInterfaceInfo
*interface_info
)
4519 if (!interface_info
->properties
)
4522 for (i
= 0; interface_info
->properties
[i
]; i
++)
4523 if (interface_info
->properties
[i
]->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
)
4529 /* called in any thread with connection's lock held */
4531 validate_and_maybe_schedule_property_get_all (GDBusConnection
*connection
,
4532 GDBusMessage
*message
,
4533 guint registration_id
,
4534 guint subtree_registration_id
,
4535 GDBusInterfaceInfo
*interface_info
,
4536 const GDBusInterfaceVTable
*vtable
,
4537 GMainContext
*main_context
,
4541 GSource
*idle_source
;
4542 PropertyGetAllData
*property_get_all_data
;
4549 /* If the vtable pointer for get_property() is NULL but we have a
4550 * non-zero number of readable properties, then dispatch the call via
4551 * the method_call() handler.
4553 if (vtable
->get_property
== NULL
&& interface_has_readable_properties (interface_info
))
4555 schedule_method_call (connection
, message
, registration_id
, subtree_registration_id
,
4556 interface_info
, NULL
, NULL
, g_dbus_message_get_body (message
),
4557 vtable
, main_context
, user_data
);
4562 /* ok, got the property info - call user in an idle handler */
4563 property_get_all_data
= g_new0 (PropertyGetAllData
, 1);
4564 property_get_all_data
->connection
= g_object_ref (connection
);
4565 property_get_all_data
->message
= g_object_ref (message
);
4566 property_get_all_data
->user_data
= user_data
;
4567 property_get_all_data
->vtable
= vtable
;
4568 property_get_all_data
->interface_info
= interface_info
;
4569 property_get_all_data
->registration_id
= registration_id
;
4570 property_get_all_data
->subtree_registration_id
= subtree_registration_id
;
4572 idle_source
= g_idle_source_new ();
4573 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4574 g_source_set_callback (idle_source
,
4575 invoke_get_all_properties_in_idle_cb
,
4576 property_get_all_data
,
4577 (GDestroyNotify
) property_get_all_data_free
);
4578 g_source_set_name (idle_source
, "[gio] invoke_get_all_properties_in_idle_cb");
4579 g_source_attach (idle_source
, main_context
);
4580 g_source_unref (idle_source
);
4588 /* called in GDBusWorker thread with connection's lock held */
4590 handle_get_all_properties (GDBusConnection
*connection
,
4592 GDBusMessage
*message
)
4594 ExportedInterface
*ei
;
4596 const char *interface_name
;
4600 g_variant_get (g_dbus_message_get_body (message
),
4604 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4605 * no such interface registered
4607 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
4610 GDBusMessage
*reply
;
4611 reply
= g_dbus_message_new_method_error (message
,
4612 "org.freedesktop.DBus.Error.InvalidArgs",
4613 _("No such interface ā%sā"),
4615 g_dbus_connection_send_message_unlocked (eo
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4616 g_object_unref (reply
);
4621 handled
= validate_and_maybe_schedule_property_get_all (eo
->connection
,
4633 /* ---------------------------------------------------------------------------------------------------- */
4635 static const gchar introspect_header
[] =
4636 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
4637 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
4638 "<!-- GDBus " PACKAGE_VERSION
" -->\n"
4641 static const gchar introspect_tail
[] =
4644 static const gchar introspect_properties_interface
[] =
4645 " <interface name=\"org.freedesktop.DBus.Properties\">\n"
4646 " <method name=\"Get\">\n"
4647 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4648 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4649 " <arg type=\"v\" name=\"value\" direction=\"out\"/>\n"
4651 " <method name=\"GetAll\">\n"
4652 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4653 " <arg type=\"a{sv}\" name=\"properties\" direction=\"out\"/>\n"
4655 " <method name=\"Set\">\n"
4656 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4657 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4658 " <arg type=\"v\" name=\"value\" direction=\"in\"/>\n"
4660 " <signal name=\"PropertiesChanged\">\n"
4661 " <arg type=\"s\" name=\"interface_name\"/>\n"
4662 " <arg type=\"a{sv}\" name=\"changed_properties\"/>\n"
4663 " <arg type=\"as\" name=\"invalidated_properties\"/>\n"
4667 static const gchar introspect_introspectable_interface
[] =
4668 " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
4669 " <method name=\"Introspect\">\n"
4670 " <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>\n"
4673 " <interface name=\"org.freedesktop.DBus.Peer\">\n"
4674 " <method name=\"Ping\"/>\n"
4675 " <method name=\"GetMachineId\">\n"
4676 " <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n"
4681 introspect_append_header (GString
*s
)
4683 g_string_append (s
, introspect_header
);
4687 maybe_add_path (const gchar
*path
, gsize path_len
, const gchar
*object_path
, GHashTable
*set
)
4689 if (g_str_has_prefix (object_path
, path
) && strlen (object_path
) > path_len
&& object_path
[path_len
-1] == '/')
4695 begin
= object_path
+ path_len
;
4696 end
= strchr (begin
, '/');
4698 s
= g_strndup (begin
, end
- begin
);
4700 s
= g_strdup (begin
);
4702 if (!g_hash_table_contains (set
, s
))
4703 g_hash_table_add (set
, s
);
4709 /* TODO: we want a nicer public interface for this */
4710 /* called in any thread with connection's lock held */
4712 g_dbus_connection_list_registered_unlocked (GDBusConnection
*connection
,
4717 GHashTableIter hash_iter
;
4718 const gchar
*object_path
;
4724 CONNECTION_ENSURE_LOCK (connection
);
4726 path_len
= strlen (path
);
4730 set
= g_hash_table_new (g_str_hash
, g_str_equal
);
4732 g_hash_table_iter_init (&hash_iter
, connection
->map_object_path_to_eo
);
4733 while (g_hash_table_iter_next (&hash_iter
, (gpointer
) &object_path
, NULL
))
4734 maybe_add_path (path
, path_len
, object_path
, set
);
4736 g_hash_table_iter_init (&hash_iter
, connection
->map_object_path_to_es
);
4737 while (g_hash_table_iter_next (&hash_iter
, (gpointer
) &object_path
, NULL
))
4738 maybe_add_path (path
, path_len
, object_path
, set
);
4740 p
= g_ptr_array_new ();
4741 keys
= g_hash_table_get_keys (set
);
4742 for (l
= keys
; l
!= NULL
; l
= l
->next
)
4743 g_ptr_array_add (p
, l
->data
);
4744 g_hash_table_unref (set
);
4747 g_ptr_array_add (p
, NULL
);
4748 ret
= (gchar
**) g_ptr_array_free (p
, FALSE
);
4752 /* called in any thread with connection's lock not held */
4754 g_dbus_connection_list_registered (GDBusConnection
*connection
,
4758 CONNECTION_LOCK (connection
);
4759 ret
= g_dbus_connection_list_registered_unlocked (connection
, path
);
4760 CONNECTION_UNLOCK (connection
);
4764 /* called in GDBusWorker thread with connection's lock held */
4766 handle_introspect (GDBusConnection
*connection
,
4768 GDBusMessage
*message
)
4772 GDBusMessage
*reply
;
4773 GHashTableIter hash_iter
;
4774 ExportedInterface
*ei
;
4777 /* first the header with the standard interfaces */
4778 s
= g_string_sized_new (sizeof (introspect_header
) +
4779 sizeof (introspect_properties_interface
) +
4780 sizeof (introspect_introspectable_interface
) +
4781 sizeof (introspect_tail
));
4782 introspect_append_header (s
);
4783 if (!g_hash_table_lookup (eo
->map_if_name_to_ei
,
4784 "org.freedesktop.DBus.Properties"))
4785 g_string_append (s
, introspect_properties_interface
);
4787 if (!g_hash_table_lookup (eo
->map_if_name_to_ei
,
4788 "org.freedesktop.DBus.Introspectable"))
4789 g_string_append (s
, introspect_introspectable_interface
);
4791 /* then include the registered interfaces */
4792 g_hash_table_iter_init (&hash_iter
, eo
->map_if_name_to_ei
);
4793 while (g_hash_table_iter_next (&hash_iter
, NULL
, (gpointer
) &ei
))
4794 g_dbus_interface_info_generate_xml (ei
->interface_info
, 2, s
);
4796 /* finally include nodes registered below us */
4797 registered
= g_dbus_connection_list_registered_unlocked (connection
, eo
->object_path
);
4798 for (n
= 0; registered
!= NULL
&& registered
[n
] != NULL
; n
++)
4799 g_string_append_printf (s
, " <node name=\"%s\"/>\n", registered
[n
]);
4800 g_strfreev (registered
);
4801 g_string_append (s
, introspect_tail
);
4803 reply
= g_dbus_message_new_method_reply (message
);
4804 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
4805 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4806 g_object_unref (reply
);
4807 g_string_free (s
, TRUE
);
4812 /* called in thread where object was registered - no locks held */
4814 call_in_idle_cb (gpointer user_data
)
4816 GDBusMethodInvocation
*invocation
= G_DBUS_METHOD_INVOCATION (user_data
);
4817 GDBusInterfaceVTable
*vtable
;
4818 guint registration_id
;
4819 guint subtree_registration_id
;
4821 registration_id
= GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation
), "g-dbus-registration-id"));
4822 subtree_registration_id
= GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation
), "g-dbus-subtree-registration-id"));
4824 if (has_object_been_unregistered (g_dbus_method_invocation_get_connection (invocation
),
4826 subtree_registration_id
))
4828 GDBusMessage
*reply
;
4829 reply
= g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation
),
4830 "org.freedesktop.DBus.Error.UnknownMethod",
4831 _("No such interface ā%sā on object at path %s"),
4832 g_dbus_method_invocation_get_interface_name (invocation
),
4833 g_dbus_method_invocation_get_object_path (invocation
));
4834 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation
), reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4835 g_object_unref (reply
);
4839 vtable
= g_object_get_data (G_OBJECT (invocation
), "g-dbus-interface-vtable");
4840 g_assert (vtable
!= NULL
&& vtable
->method_call
!= NULL
);
4842 vtable
->method_call (g_dbus_method_invocation_get_connection (invocation
),
4843 g_dbus_method_invocation_get_sender (invocation
),
4844 g_dbus_method_invocation_get_object_path (invocation
),
4845 g_dbus_method_invocation_get_interface_name (invocation
),
4846 g_dbus_method_invocation_get_method_name (invocation
),
4847 g_dbus_method_invocation_get_parameters (invocation
),
4848 g_object_ref (invocation
),
4849 g_dbus_method_invocation_get_user_data (invocation
));
4855 /* called in GDBusWorker thread with connection's lock held */
4857 schedule_method_call (GDBusConnection
*connection
,
4858 GDBusMessage
*message
,
4859 guint registration_id
,
4860 guint subtree_registration_id
,
4861 const GDBusInterfaceInfo
*interface_info
,
4862 const GDBusMethodInfo
*method_info
,
4863 const GDBusPropertyInfo
*property_info
,
4864 GVariant
*parameters
,
4865 const GDBusInterfaceVTable
*vtable
,
4866 GMainContext
*main_context
,
4869 GDBusMethodInvocation
*invocation
;
4870 GSource
*idle_source
;
4872 invocation
= _g_dbus_method_invocation_new (g_dbus_message_get_sender (message
),
4873 g_dbus_message_get_path (message
),
4874 g_dbus_message_get_interface (message
),
4875 g_dbus_message_get_member (message
),
4883 /* TODO: would be nicer with a real MethodData like we already
4884 * have PropertyData and PropertyGetAllData... */
4885 g_object_set_data (G_OBJECT (invocation
), "g-dbus-interface-vtable", (gpointer
) vtable
);
4886 g_object_set_data (G_OBJECT (invocation
), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id
));
4887 g_object_set_data (G_OBJECT (invocation
), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id
));
4889 idle_source
= g_idle_source_new ();
4890 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4891 g_source_set_callback (idle_source
,
4895 g_source_set_name (idle_source
, "[gio, " __FILE__
"] call_in_idle_cb");
4896 g_source_attach (idle_source
, main_context
);
4897 g_source_unref (idle_source
);
4900 /* called in GDBusWorker thread with connection's lock held */
4902 validate_and_maybe_schedule_method_call (GDBusConnection
*connection
,
4903 GDBusMessage
*message
,
4904 guint registration_id
,
4905 guint subtree_registration_id
,
4906 GDBusInterfaceInfo
*interface_info
,
4907 const GDBusInterfaceVTable
*vtable
,
4908 GMainContext
*main_context
,
4911 GDBusMethodInfo
*method_info
;
4912 GDBusMessage
*reply
;
4913 GVariant
*parameters
;
4915 GVariantType
*in_type
;
4919 /* TODO: the cost of this is O(n) - it might be worth caching the result */
4920 method_info
= g_dbus_interface_info_lookup_method (interface_info
, g_dbus_message_get_member (message
));
4922 /* if the method doesn't exist, return the org.freedesktop.DBus.Error.UnknownMethod
4923 * error to the caller
4925 if (method_info
== NULL
)
4927 reply
= g_dbus_message_new_method_error (message
,
4928 "org.freedesktop.DBus.Error.UnknownMethod",
4929 _("No such method ā%sā"),
4930 g_dbus_message_get_member (message
));
4931 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4932 g_object_unref (reply
);
4937 parameters
= g_dbus_message_get_body (message
);
4938 if (parameters
== NULL
)
4940 parameters
= g_variant_new ("()");
4941 g_variant_ref_sink (parameters
);
4945 g_variant_ref (parameters
);
4948 /* Check that the incoming args are of the right type - if they are not, return
4949 * the org.freedesktop.DBus.Error.InvalidArgs error to the caller
4951 in_type
= _g_dbus_compute_complete_signature (method_info
->in_args
);
4952 if (!g_variant_is_of_type (parameters
, in_type
))
4956 type_string
= g_variant_type_dup_string (in_type
);
4958 reply
= g_dbus_message_new_method_error (message
,
4959 "org.freedesktop.DBus.Error.InvalidArgs",
4960 _("Type of message, ā%sā, does not match expected type ā%sā"),
4961 g_variant_get_type_string (parameters
),
4963 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4964 g_variant_type_free (in_type
);
4965 g_variant_unref (parameters
);
4966 g_object_unref (reply
);
4967 g_free (type_string
);
4971 g_variant_type_free (in_type
);
4973 /* schedule the call in idle */
4974 schedule_method_call (connection
, message
, registration_id
, subtree_registration_id
,
4975 interface_info
, method_info
, NULL
, parameters
,
4976 vtable
, main_context
, user_data
);
4977 g_variant_unref (parameters
);
4984 /* ---------------------------------------------------------------------------------------------------- */
4986 /* called in GDBusWorker thread with connection's lock held */
4988 obj_message_func (GDBusConnection
*connection
,
4990 GDBusMessage
*message
)
4992 const gchar
*interface_name
;
4993 const gchar
*member
;
4994 const gchar
*signature
;
4999 interface_name
= g_dbus_message_get_interface (message
);
5000 member
= g_dbus_message_get_member (message
);
5001 signature
= g_dbus_message_get_signature (message
);
5003 /* see if we have an interface for handling this call */
5004 if (interface_name
!= NULL
)
5006 ExportedInterface
*ei
;
5007 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
5010 /* we do - invoke the handler in idle in the right thread */
5012 /* handle no vtable or handler being present */
5013 if (ei
->vtable
== NULL
|| ei
->vtable
->method_call
== NULL
)
5016 handled
= validate_and_maybe_schedule_method_call (connection
,
5028 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Introspectable") == 0 &&
5029 g_strcmp0 (member
, "Introspect") == 0 &&
5030 g_strcmp0 (signature
, "") == 0)
5032 handled
= handle_introspect (connection
, eo
, message
);
5035 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
5036 g_strcmp0 (member
, "Get") == 0 &&
5037 g_strcmp0 (signature
, "ss") == 0)
5039 handled
= handle_getset_property (connection
, eo
, message
, TRUE
);
5042 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
5043 g_strcmp0 (member
, "Set") == 0 &&
5044 g_strcmp0 (signature
, "ssv") == 0)
5046 handled
= handle_getset_property (connection
, eo
, message
, FALSE
);
5049 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
5050 g_strcmp0 (member
, "GetAll") == 0 &&
5051 g_strcmp0 (signature
, "s") == 0)
5053 handled
= handle_get_all_properties (connection
, eo
, message
);
5062 * g_dbus_connection_register_object:
5063 * @connection: a #GDBusConnection
5064 * @object_path: the object path to register at
5065 * @interface_info: introspection data for the interface
5066 * @vtable: (nullable): a #GDBusInterfaceVTable to call into or %NULL
5067 * @user_data: (nullable): data to pass to functions in @vtable
5068 * @user_data_free_func: function to call when the object path is unregistered
5069 * @error: return location for error or %NULL
5071 * Registers callbacks for exported objects at @object_path with the
5072 * D-Bus interface that is described in @interface_info.
5074 * Calls to functions in @vtable (and @user_data_free_func) will happen
5076 * [thread-default main context][g-main-context-push-thread-default]
5077 * of the thread you are calling this method from.
5079 * Note that all #GVariant values passed to functions in @vtable will match
5080 * the signature given in @interface_info - if a remote caller passes
5081 * incorrect values, the `org.freedesktop.DBus.Error.InvalidArgs`
5082 * is returned to the remote caller.
5084 * Additionally, if the remote caller attempts to invoke methods or
5085 * access properties not mentioned in @interface_info the
5086 * `org.freedesktop.DBus.Error.UnknownMethod` resp.
5087 * `org.freedesktop.DBus.Error.InvalidArgs` errors
5088 * are returned to the caller.
5090 * It is considered a programming error if the
5091 * #GDBusInterfaceGetPropertyFunc function in @vtable returns a
5092 * #GVariant of incorrect type.
5094 * If an existing callback is already registered at @object_path and
5095 * @interface_name, then @error is set to #G_IO_ERROR_EXISTS.
5097 * GDBus automatically implements the standard D-Bus interfaces
5098 * org.freedesktop.DBus.Properties, org.freedesktop.DBus.Introspectable
5099 * and org.freedesktop.Peer, so you don't have to implement those for the
5100 * objects you export. You can implement org.freedesktop.DBus.Properties
5101 * yourself, e.g. to handle getting and setting of properties asynchronously.
5103 * Note that the reference count on @interface_info will be
5104 * incremented by 1 (unless allocated statically, e.g. if the
5105 * reference count is -1, see g_dbus_interface_info_ref()) for as long
5106 * as the object is exported. Also note that @vtable will be copied.
5108 * See this [server][gdbus-server] for an example of how to use this method.
5110 * Returns: 0 if @error is set, otherwise a registration id (never 0)
5111 * that can be used with g_dbus_connection_unregister_object()
5116 g_dbus_connection_register_object (GDBusConnection
*connection
,
5117 const gchar
*object_path
,
5118 GDBusInterfaceInfo
*interface_info
,
5119 const GDBusInterfaceVTable
*vtable
,
5121 GDestroyNotify user_data_free_func
,
5125 ExportedInterface
*ei
;
5128 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
5129 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), 0);
5130 g_return_val_if_fail (interface_info
!= NULL
, 0);
5131 g_return_val_if_fail (g_dbus_is_interface_name (interface_info
->name
), 0);
5132 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, 0);
5133 g_return_val_if_fail (check_initialized (connection
), 0);
5137 CONNECTION_LOCK (connection
);
5139 eo
= g_hash_table_lookup (connection
->map_object_path_to_eo
, object_path
);
5142 eo
= g_new0 (ExportedObject
, 1);
5143 eo
->object_path
= g_strdup (object_path
);
5144 eo
->connection
= connection
;
5145 eo
->map_if_name_to_ei
= g_hash_table_new_full (g_str_hash
,
5148 (GDestroyNotify
) exported_interface_free
);
5149 g_hash_table_insert (connection
->map_object_path_to_eo
, eo
->object_path
, eo
);
5152 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_info
->name
);
5158 _("An object is already exported for the interface %s at %s"),
5159 interface_info
->name
,
5164 ei
= g_new0 (ExportedInterface
, 1);
5165 ei
->id
= g_atomic_int_add (&_global_registration_id
, 1); /* TODO: overflow etc. */
5167 ei
->user_data
= user_data
;
5168 ei
->user_data_free_func
= user_data_free_func
;
5169 ei
->vtable
= _g_dbus_interface_vtable_copy (vtable
);
5170 ei
->interface_info
= g_dbus_interface_info_ref (interface_info
);
5171 g_dbus_interface_info_cache_build (ei
->interface_info
);
5172 ei
->interface_name
= g_strdup (interface_info
->name
);
5173 ei
->context
= g_main_context_ref_thread_default ();
5175 g_hash_table_insert (eo
->map_if_name_to_ei
,
5176 (gpointer
) ei
->interface_name
,
5178 g_hash_table_insert (connection
->map_id_to_ei
,
5179 GUINT_TO_POINTER (ei
->id
),
5185 CONNECTION_UNLOCK (connection
);
5191 * g_dbus_connection_unregister_object:
5192 * @connection: a #GDBusConnection
5193 * @registration_id: a registration id obtained from
5194 * g_dbus_connection_register_object()
5196 * Unregisters an object.
5198 * Returns: %TRUE if the object was unregistered, %FALSE otherwise
5203 g_dbus_connection_unregister_object (GDBusConnection
*connection
,
5204 guint registration_id
)
5206 ExportedInterface
*ei
;
5210 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
5211 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5215 CONNECTION_LOCK (connection
);
5217 ei
= g_hash_table_lookup (connection
->map_id_to_ei
,
5218 GUINT_TO_POINTER (registration_id
));
5224 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_ei
, GUINT_TO_POINTER (ei
->id
)));
5225 g_warn_if_fail (g_hash_table_remove (eo
->map_if_name_to_ei
, ei
->interface_name
));
5226 /* unregister object path if we have no more exported interfaces */
5227 if (g_hash_table_size (eo
->map_if_name_to_ei
) == 0)
5228 g_warn_if_fail (g_hash_table_remove (connection
->map_object_path_to_eo
,
5234 CONNECTION_UNLOCK (connection
);
5240 GClosure
*method_call_closure
;
5241 GClosure
*get_property_closure
;
5242 GClosure
*set_property_closure
;
5243 } RegisterObjectData
;
5245 static RegisterObjectData
*
5246 register_object_data_new (GClosure
*method_call_closure
,
5247 GClosure
*get_property_closure
,
5248 GClosure
*set_property_closure
)
5250 RegisterObjectData
*data
;
5252 data
= g_new0 (RegisterObjectData
, 1);
5254 if (method_call_closure
!= NULL
)
5256 data
->method_call_closure
= g_closure_ref (method_call_closure
);
5257 g_closure_sink (method_call_closure
);
5258 if (G_CLOSURE_NEEDS_MARSHAL (method_call_closure
))
5259 g_closure_set_marshal (method_call_closure
, g_cclosure_marshal_generic
);
5262 if (get_property_closure
!= NULL
)
5264 data
->get_property_closure
= g_closure_ref (get_property_closure
);
5265 g_closure_sink (get_property_closure
);
5266 if (G_CLOSURE_NEEDS_MARSHAL (get_property_closure
))
5267 g_closure_set_marshal (get_property_closure
, g_cclosure_marshal_generic
);
5270 if (set_property_closure
!= NULL
)
5272 data
->set_property_closure
= g_closure_ref (set_property_closure
);
5273 g_closure_sink (set_property_closure
);
5274 if (G_CLOSURE_NEEDS_MARSHAL (set_property_closure
))
5275 g_closure_set_marshal (set_property_closure
, g_cclosure_marshal_generic
);
5282 register_object_free_func (gpointer user_data
)
5284 RegisterObjectData
*data
= user_data
;
5286 g_clear_pointer (&data
->method_call_closure
, g_closure_unref
);
5287 g_clear_pointer (&data
->get_property_closure
, g_closure_unref
);
5288 g_clear_pointer (&data
->set_property_closure
, g_closure_unref
);
5294 register_with_closures_on_method_call (GDBusConnection
*connection
,
5295 const gchar
*sender
,
5296 const gchar
*object_path
,
5297 const gchar
*interface_name
,
5298 const gchar
*method_name
,
5299 GVariant
*parameters
,
5300 GDBusMethodInvocation
*invocation
,
5303 RegisterObjectData
*data
= user_data
;
5304 GValue params
[] = { G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
};
5306 g_value_init (¶ms
[0], G_TYPE_DBUS_CONNECTION
);
5307 g_value_set_object (¶ms
[0], connection
);
5309 g_value_init (¶ms
[1], G_TYPE_STRING
);
5310 g_value_set_string (¶ms
[1], sender
);
5312 g_value_init (¶ms
[2], G_TYPE_STRING
);
5313 g_value_set_string (¶ms
[2], object_path
);
5315 g_value_init (¶ms
[3], G_TYPE_STRING
);
5316 g_value_set_string (¶ms
[3], interface_name
);
5318 g_value_init (¶ms
[4], G_TYPE_STRING
);
5319 g_value_set_string (¶ms
[4], method_name
);
5321 g_value_init (¶ms
[5], G_TYPE_VARIANT
);
5322 g_value_set_variant (¶ms
[5], parameters
);
5324 g_value_init (¶ms
[6], G_TYPE_DBUS_METHOD_INVOCATION
);
5325 g_value_set_object (¶ms
[6], invocation
);
5327 g_closure_invoke (data
->method_call_closure
, NULL
, G_N_ELEMENTS (params
), params
, NULL
);
5329 g_value_unset (params
+ 0);
5330 g_value_unset (params
+ 1);
5331 g_value_unset (params
+ 2);
5332 g_value_unset (params
+ 3);
5333 g_value_unset (params
+ 4);
5334 g_value_unset (params
+ 5);
5335 g_value_unset (params
+ 6);
5339 register_with_closures_on_get_property (GDBusConnection
*connection
,
5340 const gchar
*sender
,
5341 const gchar
*object_path
,
5342 const gchar
*interface_name
,
5343 const gchar
*property_name
,
5347 RegisterObjectData
*data
= user_data
;
5348 GValue params
[] = { G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
};
5349 GValue result_value
= G_VALUE_INIT
;
5352 g_value_init (¶ms
[0], G_TYPE_DBUS_CONNECTION
);
5353 g_value_set_object (¶ms
[0], connection
);
5355 g_value_init (¶ms
[1], G_TYPE_STRING
);
5356 g_value_set_string (¶ms
[1], sender
);
5358 g_value_init (¶ms
[2], G_TYPE_STRING
);
5359 g_value_set_string (¶ms
[2], object_path
);
5361 g_value_init (¶ms
[3], G_TYPE_STRING
);
5362 g_value_set_string (¶ms
[3], interface_name
);
5364 g_value_init (¶ms
[4], G_TYPE_STRING
);
5365 g_value_set_string (¶ms
[4], property_name
);
5367 g_value_init (&result_value
, G_TYPE_VARIANT
);
5369 g_closure_invoke (data
->get_property_closure
, &result_value
, G_N_ELEMENTS (params
), params
, NULL
);
5371 result
= g_value_get_variant (&result_value
);
5373 g_variant_ref (result
);
5375 g_value_unset (params
+ 0);
5376 g_value_unset (params
+ 1);
5377 g_value_unset (params
+ 2);
5378 g_value_unset (params
+ 3);
5379 g_value_unset (params
+ 4);
5380 g_value_unset (&result_value
);
5383 g_set_error (error
, G_DBUS_ERROR
, G_DBUS_ERROR_FAILED
,
5384 _("Unable to retrieve property %s.%s"),
5385 interface_name
, property_name
);
5391 register_with_closures_on_set_property (GDBusConnection
*connection
,
5392 const gchar
*sender
,
5393 const gchar
*object_path
,
5394 const gchar
*interface_name
,
5395 const gchar
*property_name
,
5400 RegisterObjectData
*data
= user_data
;
5401 GValue params
[] = { G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
};
5402 GValue result_value
= G_VALUE_INIT
;
5405 g_value_init (¶ms
[0], G_TYPE_DBUS_CONNECTION
);
5406 g_value_set_object (¶ms
[0], connection
);
5408 g_value_init (¶ms
[1], G_TYPE_STRING
);
5409 g_value_set_string (¶ms
[1], sender
);
5411 g_value_init (¶ms
[2], G_TYPE_STRING
);
5412 g_value_set_string (¶ms
[2], object_path
);
5414 g_value_init (¶ms
[3], G_TYPE_STRING
);
5415 g_value_set_string (¶ms
[3], interface_name
);
5417 g_value_init (¶ms
[4], G_TYPE_STRING
);
5418 g_value_set_string (¶ms
[4], property_name
);
5420 g_value_init (¶ms
[5], G_TYPE_VARIANT
);
5421 g_value_set_variant (¶ms
[5], value
);
5423 g_value_init (&result_value
, G_TYPE_BOOLEAN
);
5425 g_closure_invoke (data
->set_property_closure
, &result_value
, G_N_ELEMENTS (params
), params
, NULL
);
5427 result
= g_value_get_boolean (&result_value
);
5429 g_value_unset (params
+ 0);
5430 g_value_unset (params
+ 1);
5431 g_value_unset (params
+ 2);
5432 g_value_unset (params
+ 3);
5433 g_value_unset (params
+ 4);
5434 g_value_unset (params
+ 5);
5435 g_value_unset (&result_value
);
5439 G_DBUS_ERROR
, G_DBUS_ERROR_FAILED
,
5440 _("Unable to set property %s.%s"),
5441 interface_name
, property_name
);
5447 * g_dbus_connection_register_object_with_closures: (rename-to g_dbus_connection_register_object)
5448 * @connection: A #GDBusConnection.
5449 * @object_path: The object path to register at.
5450 * @interface_info: Introspection data for the interface.
5451 * @method_call_closure: (nullable): #GClosure for handling incoming method calls.
5452 * @get_property_closure: (nullable): #GClosure for getting a property.
5453 * @set_property_closure: (nullable): #GClosure for setting a property.
5454 * @error: Return location for error or %NULL.
5456 * Version of g_dbus_connection_register_object() using closures instead of a
5457 * #GDBusInterfaceVTable for easier binding in other languages.
5459 * Returns: 0 if @error is set, otherwise a registration id (never 0)
5460 * that can be used with g_dbus_connection_unregister_object() .
5465 g_dbus_connection_register_object_with_closures (GDBusConnection
*connection
,
5466 const gchar
*object_path
,
5467 GDBusInterfaceInfo
*interface_info
,
5468 GClosure
*method_call_closure
,
5469 GClosure
*get_property_closure
,
5470 GClosure
*set_property_closure
,
5473 RegisterObjectData
*data
;
5474 GDBusInterfaceVTable vtable
=
5476 method_call_closure
!= NULL
? register_with_closures_on_method_call
: NULL
,
5477 get_property_closure
!= NULL
? register_with_closures_on_get_property
: NULL
,
5478 set_property_closure
!= NULL
? register_with_closures_on_set_property
: NULL
5481 data
= register_object_data_new (method_call_closure
, get_property_closure
, set_property_closure
);
5483 return g_dbus_connection_register_object (connection
,
5488 register_object_free_func
,
5492 /* ---------------------------------------------------------------------------------------------------- */
5495 * g_dbus_connection_emit_signal:
5496 * @connection: a #GDBusConnection
5497 * @destination_bus_name: (nullable): the unique bus name for the destination
5498 * for the signal or %NULL to emit to all listeners
5499 * @object_path: path of remote object
5500 * @interface_name: D-Bus interface to emit a signal on
5501 * @signal_name: the name of the signal to emit
5502 * @parameters: (nullable): a #GVariant tuple with parameters for the signal
5503 * or %NULL if not passing parameters
5504 * @error: Return location for error or %NULL
5508 * If the parameters GVariant is floating, it is consumed.
5510 * This can only fail if @parameters is not compatible with the D-Bus protocol
5511 * (%G_IO_ERROR_INVALID_ARGUMENT), or if @connection has been closed
5512 * (%G_IO_ERROR_CLOSED).
5514 * Returns: %TRUE unless @error is set
5519 g_dbus_connection_emit_signal (GDBusConnection
*connection
,
5520 const gchar
*destination_bus_name
,
5521 const gchar
*object_path
,
5522 const gchar
*interface_name
,
5523 const gchar
*signal_name
,
5524 GVariant
*parameters
,
5527 GDBusMessage
*message
;
5533 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
5534 g_return_val_if_fail (destination_bus_name
== NULL
|| g_dbus_is_name (destination_bus_name
), FALSE
);
5535 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), FALSE
);
5536 g_return_val_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
), FALSE
);
5537 g_return_val_if_fail (signal_name
!= NULL
&& g_dbus_is_member_name (signal_name
), FALSE
);
5538 g_return_val_if_fail (parameters
== NULL
|| g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
), FALSE
);
5539 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5541 if (G_UNLIKELY (_g_dbus_debug_emission ()))
5543 _g_dbus_debug_print_lock ();
5544 g_print ("========================================================================\n"
5545 "GDBus-debug:Emission:\n"
5546 " >>>> SIGNAL EMISSION %s.%s()\n"
5548 " destination %s\n",
5549 interface_name
, signal_name
,
5551 destination_bus_name
!= NULL
? destination_bus_name
: "(none)");
5552 _g_dbus_debug_print_unlock ();
5555 message
= g_dbus_message_new_signal (object_path
,
5559 if (destination_bus_name
!= NULL
)
5560 g_dbus_message_set_header (message
,
5561 G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
,
5562 g_variant_new_string (destination_bus_name
));
5564 if (parameters
!= NULL
)
5565 g_dbus_message_set_body (message
, parameters
);
5567 ret
= g_dbus_connection_send_message (connection
, message
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, error
);
5568 g_object_unref (message
);
5574 add_call_flags (GDBusMessage
*message
,
5575 GDBusCallFlags flags
)
5577 GDBusMessageFlags msg_flags
= 0;
5579 if (flags
& G_DBUS_CALL_FLAGS_NO_AUTO_START
)
5580 msg_flags
|= G_DBUS_MESSAGE_FLAGS_NO_AUTO_START
;
5581 if (flags
& G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION
)
5582 msg_flags
|= G_DBUS_MESSAGE_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION
;
5584 g_dbus_message_set_flags (message
, msg_flags
);
5588 decode_method_reply (GDBusMessage
*reply
,
5589 const gchar
*method_name
,
5590 const GVariantType
*reply_type
,
5591 GUnixFDList
**out_fd_list
,
5597 switch (g_dbus_message_get_message_type (reply
))
5599 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN
:
5600 result
= g_dbus_message_get_body (reply
);
5603 result
= g_variant_new ("()");
5604 g_variant_ref_sink (result
);
5608 g_variant_ref (result
);
5611 if (!g_variant_is_of_type (result
, reply_type
))
5613 gchar
*type_string
= g_variant_type_dup_string (reply_type
);
5617 G_IO_ERROR_INVALID_ARGUMENT
,
5618 _("Method ā%sā returned type ā%sā, but expected ā%sā"),
5619 method_name
, g_variant_get_type_string (result
), type_string
);
5621 g_variant_unref (result
);
5622 g_free (type_string
);
5629 if (out_fd_list
!= NULL
)
5631 *out_fd_list
= g_dbus_message_get_unix_fd_list (reply
);
5632 if (*out_fd_list
!= NULL
)
5633 g_object_ref (*out_fd_list
);
5639 case G_DBUS_MESSAGE_TYPE_ERROR
:
5640 g_dbus_message_to_gerror (reply
, error
);
5644 g_assert_not_reached ();
5654 GVariantType
*reply_type
;
5655 gchar
*method_name
; /* for error message */
5657 GUnixFDList
*fd_list
;
5661 call_state_free (CallState
*state
)
5663 g_variant_type_free (state
->reply_type
);
5664 g_free (state
->method_name
);
5666 if (state
->fd_list
!= NULL
)
5667 g_object_unref (state
->fd_list
);
5668 g_slice_free (CallState
, state
);
5671 /* called in any thread, with the connection's lock not held */
5673 g_dbus_connection_call_done (GObject
*source
,
5674 GAsyncResult
*result
,
5677 GDBusConnection
*connection
= G_DBUS_CONNECTION (source
);
5678 GTask
*task
= user_data
;
5679 CallState
*state
= g_task_get_task_data (task
);
5680 GError
*error
= NULL
;
5681 GDBusMessage
*reply
;
5682 GVariant
*value
= NULL
;
5684 reply
= g_dbus_connection_send_message_with_reply_finish (connection
,
5688 if (G_UNLIKELY (_g_dbus_debug_call ()))
5690 _g_dbus_debug_print_lock ();
5691 g_print ("========================================================================\n"
5692 "GDBus-debug:Call:\n"
5693 " <<<< ASYNC COMPLETE %s() (serial %d)\n"
5696 g_dbus_message_get_reply_serial (reply
));
5699 g_print ("SUCCESS\n");
5703 g_print ("FAILED: %s\n",
5706 _g_dbus_debug_print_unlock ();
5710 value
= decode_method_reply (reply
, state
->method_name
, state
->reply_type
, &state
->fd_list
, &error
);
5713 g_task_return_error (task
, error
);
5715 g_task_return_pointer (task
, value
, (GDestroyNotify
) g_variant_unref
);
5717 g_clear_object (&reply
);
5718 g_object_unref (task
);
5721 /* called in any thread, with the connection's lock not held */
5723 g_dbus_connection_call_internal (GDBusConnection
*connection
,
5724 const gchar
*bus_name
,
5725 const gchar
*object_path
,
5726 const gchar
*interface_name
,
5727 const gchar
*method_name
,
5728 GVariant
*parameters
,
5729 const GVariantType
*reply_type
,
5730 GDBusCallFlags flags
,
5732 GUnixFDList
*fd_list
,
5733 GCancellable
*cancellable
,
5734 GAsyncReadyCallback callback
,
5737 GDBusMessage
*message
;
5740 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
5741 g_return_if_fail (bus_name
== NULL
|| g_dbus_is_name (bus_name
));
5742 g_return_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
));
5743 g_return_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
));
5744 g_return_if_fail (method_name
!= NULL
&& g_dbus_is_member_name (method_name
));
5745 g_return_if_fail (timeout_msec
>= 0 || timeout_msec
== -1);
5746 g_return_if_fail ((parameters
== NULL
) || g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
));
5747 g_return_if_fail (check_initialized (connection
));
5749 g_return_if_fail (fd_list
== NULL
|| G_IS_UNIX_FD_LIST (fd_list
));
5751 g_return_if_fail (fd_list
== NULL
);
5754 message
= g_dbus_message_new_method_call (bus_name
,
5758 add_call_flags (message
, flags
);
5759 if (parameters
!= NULL
)
5760 g_dbus_message_set_body (message
, parameters
);
5763 if (fd_list
!= NULL
)
5764 g_dbus_message_set_unix_fd_list (message
, fd_list
);
5767 /* If the user has no callback then we can just send the message with
5768 * the G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set and skip all
5769 * the logic for processing the reply. If the service sends the reply
5770 * anyway then it will just be ignored.
5772 if (callback
!= NULL
)
5777 state
= g_slice_new0 (CallState
);
5778 state
->method_name
= g_strjoin (".", interface_name
, method_name
, NULL
);
5780 if (reply_type
== NULL
)
5781 reply_type
= G_VARIANT_TYPE_ANY
;
5783 state
->reply_type
= g_variant_type_copy (reply_type
);
5785 task
= g_task_new (connection
, cancellable
, callback
, user_data
);
5786 g_task_set_source_tag (task
, g_dbus_connection_call_internal
);
5787 g_task_set_task_data (task
, state
, (GDestroyNotify
) call_state_free
);
5789 g_dbus_connection_send_message_with_reply (connection
,
5791 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
5795 g_dbus_connection_call_done
,
5800 GDBusMessageFlags flags
;
5802 flags
= g_dbus_message_get_flags (message
);
5803 flags
|= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
5804 g_dbus_message_set_flags (message
, flags
);
5806 g_dbus_connection_send_message (connection
,
5808 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
5812 if (G_UNLIKELY (_g_dbus_debug_call ()))
5814 _g_dbus_debug_print_lock ();
5815 g_print ("========================================================================\n"
5816 "GDBus-debug:Call:\n"
5817 " >>>> ASYNC %s.%s()\n"
5819 " owned by name %s (serial %d)\n",
5823 bus_name
!= NULL
? bus_name
: "(none)",
5825 _g_dbus_debug_print_unlock ();
5828 if (message
!= NULL
)
5829 g_object_unref (message
);
5832 /* called in any thread, with the connection's lock not held */
5834 g_dbus_connection_call_finish_internal (GDBusConnection
*connection
,
5835 GUnixFDList
**out_fd_list
,
5843 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
5844 g_return_val_if_fail (g_task_is_valid (res
, connection
), NULL
);
5845 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
5847 task
= G_TASK (res
);
5848 state
= g_task_get_task_data (task
);
5850 ret
= g_task_propagate_pointer (task
, error
);
5854 if (out_fd_list
!= NULL
)
5855 *out_fd_list
= state
->fd_list
!= NULL
? g_object_ref (state
->fd_list
) : NULL
;
5859 /* called in any user thread, with the connection's lock not held */
5861 g_dbus_connection_call_sync_internal (GDBusConnection
*connection
,
5862 const gchar
*bus_name
,
5863 const gchar
*object_path
,
5864 const gchar
*interface_name
,
5865 const gchar
*method_name
,
5866 GVariant
*parameters
,
5867 const GVariantType
*reply_type
,
5868 GDBusCallFlags flags
,
5870 GUnixFDList
*fd_list
,
5871 GUnixFDList
**out_fd_list
,
5872 GCancellable
*cancellable
,
5875 GDBusMessage
*message
;
5876 GDBusMessage
*reply
;
5878 GError
*local_error
;
5879 GDBusSendMessageFlags send_flags
;
5885 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
5886 g_return_val_if_fail (bus_name
== NULL
|| g_dbus_is_name (bus_name
), NULL
);
5887 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), NULL
);
5888 g_return_val_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
), NULL
);
5889 g_return_val_if_fail (method_name
!= NULL
&& g_dbus_is_member_name (method_name
), NULL
);
5890 g_return_val_if_fail (timeout_msec
>= 0 || timeout_msec
== -1, NULL
);
5891 g_return_val_if_fail ((parameters
== NULL
) || g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
), NULL
);
5893 g_return_val_if_fail (fd_list
== NULL
|| G_IS_UNIX_FD_LIST (fd_list
), NULL
);
5895 g_return_val_if_fail (fd_list
== NULL
, NULL
);
5897 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
5899 if (!(flags
& CALL_FLAGS_INITIALIZING
))
5900 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5902 if (reply_type
== NULL
)
5903 reply_type
= G_VARIANT_TYPE_ANY
;
5905 message
= g_dbus_message_new_method_call (bus_name
,
5909 add_call_flags (message
, flags
);
5910 if (parameters
!= NULL
)
5911 g_dbus_message_set_body (message
, parameters
);
5914 if (fd_list
!= NULL
)
5915 g_dbus_message_set_unix_fd_list (message
, fd_list
);
5918 if (G_UNLIKELY (_g_dbus_debug_call ()))
5920 _g_dbus_debug_print_lock ();
5921 g_print ("========================================================================\n"
5922 "GDBus-debug:Call:\n"
5923 " >>>> SYNC %s.%s()\n"
5925 " owned by name %s\n",
5929 bus_name
!= NULL
? bus_name
: "(none)");
5930 _g_dbus_debug_print_unlock ();
5935 send_flags
= G_DBUS_SEND_MESSAGE_FLAGS_NONE
;
5937 /* translate from one flavour of flags to another... */
5938 if (flags
& CALL_FLAGS_INITIALIZING
)
5939 send_flags
|= SEND_MESSAGE_FLAGS_INITIALIZING
;
5941 reply
= g_dbus_connection_send_message_with_reply_sync (connection
,
5945 NULL
, /* volatile guint32 *out_serial */
5949 if (G_UNLIKELY (_g_dbus_debug_call ()))
5951 _g_dbus_debug_print_lock ();
5952 g_print ("========================================================================\n"
5953 "GDBus-debug:Call:\n"
5954 " <<<< SYNC COMPLETE %s.%s()\n"
5960 g_print ("SUCCESS\n");
5964 g_print ("FAILED: %s\n",
5965 local_error
->message
);
5967 _g_dbus_debug_print_unlock ();
5973 *error
= local_error
;
5975 g_error_free (local_error
);
5979 result
= decode_method_reply (reply
, method_name
, reply_type
, out_fd_list
, error
);
5982 if (message
!= NULL
)
5983 g_object_unref (message
);
5985 g_object_unref (reply
);
5990 /* ---------------------------------------------------------------------------------------------------- */
5993 * g_dbus_connection_call:
5994 * @connection: a #GDBusConnection
5995 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
5996 * @connection is not a message bus connection
5997 * @object_path: path of remote object
5998 * @interface_name: D-Bus interface to invoke method on
5999 * @method_name: the name of the method to invoke
6000 * @parameters: (nullable): a #GVariant tuple with parameters for the method
6001 * or %NULL if not passing parameters
6002 * @reply_type: (nullable): the expected type of the reply (which will be a
6004 * @flags: flags from the #GDBusCallFlags enumeration
6005 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6006 * timeout or %G_MAXINT for no timeout
6007 * @cancellable: (nullable): a #GCancellable or %NULL
6008 * @callback: (nullable): a #GAsyncReadyCallback to call when the request
6009 * is satisfied or %NULL if you don't care about the result of the
6011 * @user_data: the data to pass to @callback
6013 * Asynchronously invokes the @method_name method on the
6014 * @interface_name D-Bus interface on the remote object at
6015 * @object_path owned by @bus_name.
6017 * If @connection is closed then the operation will fail with
6018 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
6019 * fail with %G_IO_ERROR_CANCELLED. If @parameters contains a value
6020 * not compatible with the D-Bus protocol, the operation fails with
6021 * %G_IO_ERROR_INVALID_ARGUMENT.
6023 * If @reply_type is non-%NULL then the reply will be checked for having this type and an
6024 * error will be raised if it does not match. Said another way, if you give a @reply_type
6025 * then any non-%NULL return value will be of this type. Unless itās
6026 * %G_VARIANT_TYPE_UNIT, the @reply_type will be a tuple containing one or more
6029 * If the @parameters #GVariant is floating, it is consumed. This allows
6030 * convenient 'inline' use of g_variant_new(), e.g.:
6031 * |[<!-- language="C" -->
6032 * g_dbus_connection_call (connection,
6033 * "org.freedesktop.StringThings",
6034 * "/org/freedesktop/StringThings",
6035 * "org.freedesktop.StringThings",
6037 * g_variant_new ("(ss)",
6041 * G_DBUS_CALL_FLAGS_NONE,
6044 * (GAsyncReadyCallback) two_strings_done,
6048 * This is an asynchronous method. When the operation is finished,
6049 * @callback will be invoked in the
6050 * [thread-default main context][g-main-context-push-thread-default]
6051 * of the thread you are calling this method from. You can then call
6052 * g_dbus_connection_call_finish() to get the result of the operation.
6053 * See g_dbus_connection_call_sync() for the synchronous version of this
6056 * If @callback is %NULL then the D-Bus method call message will be sent with
6057 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
6062 g_dbus_connection_call (GDBusConnection
*connection
,
6063 const gchar
*bus_name
,
6064 const gchar
*object_path
,
6065 const gchar
*interface_name
,
6066 const gchar
*method_name
,
6067 GVariant
*parameters
,
6068 const GVariantType
*reply_type
,
6069 GDBusCallFlags flags
,
6071 GCancellable
*cancellable
,
6072 GAsyncReadyCallback callback
,
6075 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
);
6079 * g_dbus_connection_call_finish:
6080 * @connection: a #GDBusConnection
6081 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call()
6082 * @error: return location for error or %NULL
6084 * Finishes an operation started with g_dbus_connection_call().
6086 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
6087 * return values. Free with g_variant_unref().
6092 g_dbus_connection_call_finish (GDBusConnection
*connection
,
6096 return g_dbus_connection_call_finish_internal (connection
, NULL
, res
, error
);
6100 * g_dbus_connection_call_sync:
6101 * @connection: a #GDBusConnection
6102 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
6103 * @connection is not a message bus connection
6104 * @object_path: path of remote object
6105 * @interface_name: D-Bus interface to invoke method on
6106 * @method_name: the name of the method to invoke
6107 * @parameters: (nullable): a #GVariant tuple with parameters for the method
6108 * or %NULL if not passing parameters
6109 * @reply_type: (nullable): the expected type of the reply, or %NULL
6110 * @flags: flags from the #GDBusCallFlags enumeration
6111 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6112 * timeout or %G_MAXINT for no timeout
6113 * @cancellable: (nullable): a #GCancellable or %NULL
6114 * @error: return location for error or %NULL
6116 * Synchronously invokes the @method_name method on the
6117 * @interface_name D-Bus interface on the remote object at
6118 * @object_path owned by @bus_name.
6120 * If @connection is closed then the operation will fail with
6121 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the
6122 * operation will fail with %G_IO_ERROR_CANCELLED. If @parameters
6123 * contains a value not compatible with the D-Bus protocol, the operation
6124 * fails with %G_IO_ERROR_INVALID_ARGUMENT.
6126 * If @reply_type is non-%NULL then the reply will be checked for having
6127 * this type and an error will be raised if it does not match. Said
6128 * another way, if you give a @reply_type then any non-%NULL return
6129 * value will be of this type.
6131 * If the @parameters #GVariant is floating, it is consumed.
6132 * This allows convenient 'inline' use of g_variant_new(), e.g.:
6133 * |[<!-- language="C" -->
6134 * g_dbus_connection_call_sync (connection,
6135 * "org.freedesktop.StringThings",
6136 * "/org/freedesktop/StringThings",
6137 * "org.freedesktop.StringThings",
6139 * g_variant_new ("(ss)",
6143 * G_DBUS_CALL_FLAGS_NONE,
6149 * The calling thread is blocked until a reply is received. See
6150 * g_dbus_connection_call() for the asynchronous version of
6153 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
6154 * return values. Free with g_variant_unref().
6159 g_dbus_connection_call_sync (GDBusConnection
*connection
,
6160 const gchar
*bus_name
,
6161 const gchar
*object_path
,
6162 const gchar
*interface_name
,
6163 const gchar
*method_name
,
6164 GVariant
*parameters
,
6165 const GVariantType
*reply_type
,
6166 GDBusCallFlags flags
,
6168 GCancellable
*cancellable
,
6171 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
);
6174 /* ---------------------------------------------------------------------------------------------------- */
6179 * g_dbus_connection_call_with_unix_fd_list:
6180 * @connection: a #GDBusConnection
6181 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
6182 * @connection is not a message bus connection
6183 * @object_path: path of remote object
6184 * @interface_name: D-Bus interface to invoke method on
6185 * @method_name: the name of the method to invoke
6186 * @parameters: (nullable): a #GVariant tuple with parameters for the method
6187 * or %NULL if not passing parameters
6188 * @reply_type: (nullable): the expected type of the reply, or %NULL
6189 * @flags: flags from the #GDBusCallFlags enumeration
6190 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6191 * timeout or %G_MAXINT for no timeout
6192 * @fd_list: (nullable): a #GUnixFDList or %NULL
6193 * @cancellable: (nullable): a #GCancellable or %NULL
6194 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is
6195 * satisfied or %NULL if you don't * care about the result of the
6197 * @user_data: The data to pass to @callback.
6199 * Like g_dbus_connection_call() but also takes a #GUnixFDList object.
6201 * This method is only available on UNIX.
6206 g_dbus_connection_call_with_unix_fd_list (GDBusConnection
*connection
,
6207 const gchar
*bus_name
,
6208 const gchar
*object_path
,
6209 const gchar
*interface_name
,
6210 const gchar
*method_name
,
6211 GVariant
*parameters
,
6212 const GVariantType
*reply_type
,
6213 GDBusCallFlags flags
,
6215 GUnixFDList
*fd_list
,
6216 GCancellable
*cancellable
,
6217 GAsyncReadyCallback callback
,
6220 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
);
6224 * g_dbus_connection_call_with_unix_fd_list_finish:
6225 * @connection: a #GDBusConnection
6226 * @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
6227 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
6228 * g_dbus_connection_call_with_unix_fd_list()
6229 * @error: return location for error or %NULL
6231 * Finishes an operation started with g_dbus_connection_call_with_unix_fd_list().
6233 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
6234 * return values. Free with g_variant_unref().
6239 g_dbus_connection_call_with_unix_fd_list_finish (GDBusConnection
*connection
,
6240 GUnixFDList
**out_fd_list
,
6244 return g_dbus_connection_call_finish_internal (connection
, out_fd_list
, res
, error
);
6248 * g_dbus_connection_call_with_unix_fd_list_sync:
6249 * @connection: a #GDBusConnection
6250 * @bus_name: (nullable): a unique or well-known bus name or %NULL
6251 * if @connection is not a message bus connection
6252 * @object_path: path of remote object
6253 * @interface_name: D-Bus interface to invoke method on
6254 * @method_name: the name of the method to invoke
6255 * @parameters: (nullable): a #GVariant tuple with parameters for
6256 * the method or %NULL if not passing parameters
6257 * @reply_type: (nullable): the expected type of the reply, or %NULL
6258 * @flags: flags from the #GDBusCallFlags enumeration
6259 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6260 * timeout or %G_MAXINT for no timeout
6261 * @fd_list: (nullable): a #GUnixFDList or %NULL
6262 * @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
6263 * @cancellable: (nullable): a #GCancellable or %NULL
6264 * @error: return location for error or %NULL
6266 * Like g_dbus_connection_call_sync() but also takes and returns #GUnixFDList objects.
6268 * This method is only available on UNIX.
6270 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
6271 * return values. Free with g_variant_unref().
6276 g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection
*connection
,
6277 const gchar
*bus_name
,
6278 const gchar
*object_path
,
6279 const gchar
*interface_name
,
6280 const gchar
*method_name
,
6281 GVariant
*parameters
,
6282 const GVariantType
*reply_type
,
6283 GDBusCallFlags flags
,
6285 GUnixFDList
*fd_list
,
6286 GUnixFDList
**out_fd_list
,
6287 GCancellable
*cancellable
,
6290 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
);
6293 #endif /* G_OS_UNIX */
6295 /* ---------------------------------------------------------------------------------------------------- */
6297 struct ExportedSubtree
6301 GDBusConnection
*connection
;
6302 GDBusSubtreeVTable
*vtable
;
6303 GDBusSubtreeFlags flags
;
6305 GMainContext
*context
;
6307 GDestroyNotify user_data_free_func
;
6311 exported_subtree_free (ExportedSubtree
*es
)
6313 call_destroy_notify (es
->context
,
6314 es
->user_data_free_func
,
6317 g_main_context_unref (es
->context
);
6319 _g_dbus_subtree_vtable_free (es
->vtable
);
6320 g_free (es
->object_path
);
6324 /* called without lock held in the thread where the caller registered
6328 handle_subtree_introspect (GDBusConnection
*connection
,
6329 ExportedSubtree
*es
,
6330 GDBusMessage
*message
)
6334 GDBusMessage
*reply
;
6337 const gchar
*sender
;
6338 const gchar
*requested_object_path
;
6339 const gchar
*requested_node
;
6340 GDBusInterfaceInfo
**interfaces
;
6342 gchar
**subnode_paths
;
6343 gboolean has_properties_interface
;
6344 gboolean has_introspectable_interface
;
6348 requested_object_path
= g_dbus_message_get_path (message
);
6349 sender
= g_dbus_message_get_sender (message
);
6350 is_root
= (g_strcmp0 (requested_object_path
, es
->object_path
) == 0);
6352 s
= g_string_new (NULL
);
6353 introspect_append_header (s
);
6355 /* Strictly we don't need the children in dynamic mode, but we avoid the
6356 * conditionals to preserve code clarity
6358 children
= es
->vtable
->enumerate (es
->connection
,
6365 requested_node
= strrchr (requested_object_path
, '/') + 1;
6367 /* Assert existence of object if we are not dynamic */
6368 if (!(es
->flags
& G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES
) &&
6369 !_g_strv_has_string ((const gchar
* const *) children
, requested_node
))
6374 requested_node
= NULL
;
6377 interfaces
= es
->vtable
->introspect (es
->connection
,
6382 if (interfaces
!= NULL
)
6384 has_properties_interface
= FALSE
;
6385 has_introspectable_interface
= FALSE
;
6387 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6389 if (strcmp (interfaces
[n
]->name
, "org.freedesktop.DBus.Properties") == 0)
6390 has_properties_interface
= TRUE
;
6391 else if (strcmp (interfaces
[n
]->name
, "org.freedesktop.DBus.Introspectable") == 0)
6392 has_introspectable_interface
= TRUE
;
6394 if (!has_properties_interface
)
6395 g_string_append (s
, introspect_properties_interface
);
6396 if (!has_introspectable_interface
)
6397 g_string_append (s
, introspect_introspectable_interface
);
6399 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6401 g_dbus_interface_info_generate_xml (interfaces
[n
], 2, s
);
6402 g_dbus_interface_info_unref (interfaces
[n
]);
6404 g_free (interfaces
);
6407 /* then include <node> entries from the Subtree for the root */
6410 for (n
= 0; children
!= NULL
&& children
[n
] != NULL
; n
++)
6411 g_string_append_printf (s
, " <node name=\"%s\"/>\n", children
[n
]);
6414 /* finally include nodes registered below us */
6415 subnode_paths
= g_dbus_connection_list_registered (es
->connection
, requested_object_path
);
6416 for (n
= 0; subnode_paths
!= NULL
&& subnode_paths
[n
] != NULL
; n
++)
6417 g_string_append_printf (s
, " <node name=\"%s\"/>\n", subnode_paths
[n
]);
6418 g_strfreev (subnode_paths
);
6420 g_string_append (s
, "</node>\n");
6422 reply
= g_dbus_message_new_method_reply (message
);
6423 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
6424 g_dbus_connection_send_message (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6425 g_object_unref (reply
);
6430 g_string_free (s
, TRUE
);
6431 g_strfreev (children
);
6435 /* called without lock held in the thread where the caller registered
6439 handle_subtree_method_invocation (GDBusConnection
*connection
,
6440 ExportedSubtree
*es
,
6441 GDBusMessage
*message
)
6444 const gchar
*sender
;
6445 const gchar
*interface_name
;
6446 const gchar
*member
;
6447 const gchar
*signature
;
6448 const gchar
*requested_object_path
;
6449 const gchar
*requested_node
;
6451 GDBusInterfaceInfo
*interface_info
;
6452 const GDBusInterfaceVTable
*interface_vtable
;
6453 gpointer interface_user_data
;
6455 GDBusInterfaceInfo
**interfaces
;
6456 gboolean is_property_get
;
6457 gboolean is_property_set
;
6458 gboolean is_property_get_all
;
6463 requested_object_path
= g_dbus_message_get_path (message
);
6464 sender
= g_dbus_message_get_sender (message
);
6465 interface_name
= g_dbus_message_get_interface (message
);
6466 member
= g_dbus_message_get_member (message
);
6467 signature
= g_dbus_message_get_signature (message
);
6468 is_root
= (g_strcmp0 (requested_object_path
, es
->object_path
) == 0);
6470 is_property_get
= FALSE
;
6471 is_property_set
= FALSE
;
6472 is_property_get_all
= FALSE
;
6473 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0)
6475 if (g_strcmp0 (member
, "Get") == 0 && g_strcmp0 (signature
, "ss") == 0)
6476 is_property_get
= TRUE
;
6477 else if (g_strcmp0 (member
, "Set") == 0 && g_strcmp0 (signature
, "ssv") == 0)
6478 is_property_set
= TRUE
;
6479 else if (g_strcmp0 (member
, "GetAll") == 0 && g_strcmp0 (signature
, "s") == 0)
6480 is_property_get_all
= TRUE
;
6485 requested_node
= strrchr (requested_object_path
, '/') + 1;
6487 if (~es
->flags
& G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES
)
6489 /* We don't want to dispatch to unenumerated
6490 * nodes, so ensure that the child exists.
6495 children
= es
->vtable
->enumerate (es
->connection
,
6500 exists
= _g_strv_has_string ((const gchar
* const *) children
, requested_node
);
6501 g_strfreev (children
);
6509 requested_node
= NULL
;
6512 /* get introspection data for the node */
6513 interfaces
= es
->vtable
->introspect (es
->connection
,
6515 requested_object_path
,
6519 if (interfaces
== NULL
)
6522 interface_info
= NULL
;
6523 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6525 if (g_strcmp0 (interfaces
[n
]->name
, interface_name
) == 0)
6526 interface_info
= interfaces
[n
];
6529 /* dispatch the call if the user wants to handle it */
6530 if (interface_info
!= NULL
)
6532 /* figure out where to dispatch the method call */
6533 interface_user_data
= NULL
;
6534 interface_vtable
= es
->vtable
->dispatch (es
->connection
,
6539 &interface_user_data
,
6541 if (interface_vtable
== NULL
)
6544 CONNECTION_LOCK (connection
);
6545 handled
= validate_and_maybe_schedule_method_call (es
->connection
,
6552 interface_user_data
);
6553 CONNECTION_UNLOCK (connection
);
6555 /* handle org.freedesktop.DBus.Properties interface if not explicitly handled */
6556 else if (is_property_get
|| is_property_set
|| is_property_get_all
)
6558 if (is_property_get
)
6559 g_variant_get (g_dbus_message_get_body (message
), "(&s&s)", &interface_name
, NULL
);
6560 else if (is_property_set
)
6561 g_variant_get (g_dbus_message_get_body (message
), "(&s&sv)", &interface_name
, NULL
, NULL
);
6562 else if (is_property_get_all
)
6563 g_variant_get (g_dbus_message_get_body (message
), "(&s)", &interface_name
, NULL
, NULL
);
6565 g_assert_not_reached ();
6567 /* see if the object supports this interface at all */
6568 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6570 if (g_strcmp0 (interfaces
[n
]->name
, interface_name
) == 0)
6571 interface_info
= interfaces
[n
];
6574 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the user-code
6575 * claims it won't support the interface
6577 if (interface_info
== NULL
)
6579 GDBusMessage
*reply
;
6580 reply
= g_dbus_message_new_method_error (message
,
6581 "org.freedesktop.DBus.Error.InvalidArgs",
6582 _("No such interface ā%sā"),
6584 g_dbus_connection_send_message (es
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6585 g_object_unref (reply
);
6590 /* figure out where to dispatch the property get/set/getall calls */
6591 interface_user_data
= NULL
;
6592 interface_vtable
= es
->vtable
->dispatch (es
->connection
,
6597 &interface_user_data
,
6599 if (interface_vtable
== NULL
)
6601 g_warning ("The subtree introspection function indicates that '%s' "
6602 "is a valid interface name, but calling the dispatch "
6603 "function on that interface gave us NULL", interface_name
);
6607 if (is_property_get
|| is_property_set
)
6609 CONNECTION_LOCK (connection
);
6610 handled
= validate_and_maybe_schedule_property_getset (es
->connection
,
6618 interface_user_data
);
6619 CONNECTION_UNLOCK (connection
);
6621 else if (is_property_get_all
)
6623 CONNECTION_LOCK (connection
);
6624 handled
= validate_and_maybe_schedule_property_get_all (es
->connection
,
6631 interface_user_data
);
6632 CONNECTION_UNLOCK (connection
);
6637 if (interfaces
!= NULL
)
6639 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6640 g_dbus_interface_info_unref (interfaces
[n
]);
6641 g_free (interfaces
);
6649 GDBusMessage
*message
;
6650 ExportedSubtree
*es
;
6651 } SubtreeDeferredData
;
6654 subtree_deferred_data_free (SubtreeDeferredData
*data
)
6656 g_object_unref (data
->message
);
6660 /* called without lock held in the thread where the caller registered the subtree */
6662 process_subtree_vtable_message_in_idle_cb (gpointer _data
)
6664 SubtreeDeferredData
*data
= _data
;
6669 if (g_strcmp0 (g_dbus_message_get_interface (data
->message
), "org.freedesktop.DBus.Introspectable") == 0 &&
6670 g_strcmp0 (g_dbus_message_get_member (data
->message
), "Introspect") == 0 &&
6671 g_strcmp0 (g_dbus_message_get_signature (data
->message
), "") == 0)
6672 handled
= handle_subtree_introspect (data
->es
->connection
,
6676 handled
= handle_subtree_method_invocation (data
->es
->connection
,
6682 CONNECTION_LOCK (data
->es
->connection
);
6683 handled
= handle_generic_unlocked (data
->es
->connection
, data
->message
);
6684 CONNECTION_UNLOCK (data
->es
->connection
);
6687 /* if we couldn't handle the request, just bail with the UnknownMethod error */
6690 GDBusMessage
*reply
;
6691 reply
= g_dbus_message_new_method_error (data
->message
,
6692 "org.freedesktop.DBus.Error.UnknownMethod",
6693 _("Method ā%sā on interface ā%sā with signature ā%sā does not exist"),
6694 g_dbus_message_get_member (data
->message
),
6695 g_dbus_message_get_interface (data
->message
),
6696 g_dbus_message_get_signature (data
->message
));
6697 g_dbus_connection_send_message (data
->es
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6698 g_object_unref (reply
);
6704 /* called in GDBusWorker thread with connection's lock held */
6706 subtree_message_func (GDBusConnection
*connection
,
6707 ExportedSubtree
*es
,
6708 GDBusMessage
*message
)
6710 GSource
*idle_source
;
6711 SubtreeDeferredData
*data
;
6713 data
= g_new0 (SubtreeDeferredData
, 1);
6714 data
->message
= g_object_ref (message
);
6717 /* defer this call to an idle handler in the right thread */
6718 idle_source
= g_idle_source_new ();
6719 g_source_set_priority (idle_source
, G_PRIORITY_HIGH
);
6720 g_source_set_callback (idle_source
,
6721 process_subtree_vtable_message_in_idle_cb
,
6723 (GDestroyNotify
) subtree_deferred_data_free
);
6724 g_source_set_name (idle_source
, "[gio] process_subtree_vtable_message_in_idle_cb");
6725 g_source_attach (idle_source
, es
->context
);
6726 g_source_unref (idle_source
);
6728 /* since we own the entire subtree, handlers for objects not in the subtree have been
6729 * tried already by libdbus-1 - so we just need to ensure that we're always going
6730 * to reply to the message
6736 * g_dbus_connection_register_subtree:
6737 * @connection: a #GDBusConnection
6738 * @object_path: the object path to register the subtree at
6739 * @vtable: a #GDBusSubtreeVTable to enumerate, introspect and
6740 * dispatch nodes in the subtree
6741 * @flags: flags used to fine tune the behavior of the subtree
6742 * @user_data: data to pass to functions in @vtable
6743 * @user_data_free_func: function to call when the subtree is unregistered
6744 * @error: return location for error or %NULL
6746 * Registers a whole subtree of dynamic objects.
6748 * The @enumerate and @introspection functions in @vtable are used to
6749 * convey, to remote callers, what nodes exist in the subtree rooted
6752 * When handling remote calls into any node in the subtree, first the
6753 * @enumerate function is used to check if the node exists. If the node exists
6754 * or the #G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is set
6755 * the @introspection function is used to check if the node supports the
6756 * requested method. If so, the @dispatch function is used to determine
6757 * where to dispatch the call. The collected #GDBusInterfaceVTable and
6758 * #gpointer will be used to call into the interface vtable for processing
6761 * All calls into user-provided code will be invoked in the
6762 * [thread-default main context][g-main-context-push-thread-default]
6763 * of the thread you are calling this method from.
6765 * If an existing subtree is already registered at @object_path or
6766 * then @error is set to #G_IO_ERROR_EXISTS.
6768 * Note that it is valid to register regular objects (using
6769 * g_dbus_connection_register_object()) in a subtree registered with
6770 * g_dbus_connection_register_subtree() - if so, the subtree handler
6771 * is tried as the last resort. One way to think about a subtree
6772 * handler is to consider it a fallback handler for object paths not
6773 * registered via g_dbus_connection_register_object() or other bindings.
6775 * Note that @vtable will be copied so you cannot change it after
6778 * See this [server][gdbus-subtree-server] for an example of how to use
6781 * Returns: 0 if @error is set, otherwise a subtree registration id (never 0)
6782 * that can be used with g_dbus_connection_unregister_subtree() .
6787 g_dbus_connection_register_subtree (GDBusConnection
*connection
,
6788 const gchar
*object_path
,
6789 const GDBusSubtreeVTable
*vtable
,
6790 GDBusSubtreeFlags flags
,
6792 GDestroyNotify user_data_free_func
,
6796 ExportedSubtree
*es
;
6798 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
6799 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), 0);
6800 g_return_val_if_fail (vtable
!= NULL
, 0);
6801 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, 0);
6802 g_return_val_if_fail (check_initialized (connection
), 0);
6806 CONNECTION_LOCK (connection
);
6808 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, object_path
);
6814 _("A subtree is already exported for %s"),
6819 es
= g_new0 (ExportedSubtree
, 1);
6820 es
->object_path
= g_strdup (object_path
);
6821 es
->connection
= connection
;
6823 es
->vtable
= _g_dbus_subtree_vtable_copy (vtable
);
6825 es
->id
= g_atomic_int_add (&_global_subtree_registration_id
, 1); /* TODO: overflow etc. */
6826 es
->user_data
= user_data
;
6827 es
->user_data_free_func
= user_data_free_func
;
6828 es
->context
= g_main_context_ref_thread_default ();
6830 g_hash_table_insert (connection
->map_object_path_to_es
, es
->object_path
, es
);
6831 g_hash_table_insert (connection
->map_id_to_es
,
6832 GUINT_TO_POINTER (es
->id
),
6838 CONNECTION_UNLOCK (connection
);
6843 /* ---------------------------------------------------------------------------------------------------- */
6846 * g_dbus_connection_unregister_subtree:
6847 * @connection: a #GDBusConnection
6848 * @registration_id: a subtree registration id obtained from
6849 * g_dbus_connection_register_subtree()
6851 * Unregisters a subtree.
6853 * Returns: %TRUE if the subtree was unregistered, %FALSE otherwise
6858 g_dbus_connection_unregister_subtree (GDBusConnection
*connection
,
6859 guint registration_id
)
6861 ExportedSubtree
*es
;
6864 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
6865 g_return_val_if_fail (check_initialized (connection
), FALSE
);
6869 CONNECTION_LOCK (connection
);
6871 es
= g_hash_table_lookup (connection
->map_id_to_es
,
6872 GUINT_TO_POINTER (registration_id
));
6876 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_es
, GUINT_TO_POINTER (es
->id
)));
6877 g_warn_if_fail (g_hash_table_remove (connection
->map_object_path_to_es
, es
->object_path
));
6882 CONNECTION_UNLOCK (connection
);
6887 /* ---------------------------------------------------------------------------------------------------- */
6889 /* may be called in any thread, with connection's lock held */
6891 handle_generic_ping_unlocked (GDBusConnection
*connection
,
6892 const gchar
*object_path
,
6893 GDBusMessage
*message
)
6895 GDBusMessage
*reply
;
6896 reply
= g_dbus_message_new_method_reply (message
);
6897 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6898 g_object_unref (reply
);
6901 /* may be called in any thread, with connection's lock held */
6903 handle_generic_get_machine_id_unlocked (GDBusConnection
*connection
,
6904 const gchar
*object_path
,
6905 GDBusMessage
*message
)
6907 GDBusMessage
*reply
;
6910 if (connection
->machine_id
== NULL
)
6915 connection
->machine_id
= _g_dbus_get_machine_id (&error
);
6916 if (connection
->machine_id
== NULL
)
6918 reply
= g_dbus_message_new_method_error_literal (message
,
6919 "org.freedesktop.DBus.Error.Failed",
6921 g_error_free (error
);
6927 reply
= g_dbus_message_new_method_reply (message
);
6928 g_dbus_message_set_body (reply
, g_variant_new ("(s)", connection
->machine_id
));
6930 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6931 g_object_unref (reply
);
6934 /* may be called in any thread, with connection's lock held */
6936 handle_generic_introspect_unlocked (GDBusConnection
*connection
,
6937 const gchar
*object_path
,
6938 GDBusMessage
*message
)
6943 GDBusMessage
*reply
;
6945 /* first the header */
6946 s
= g_string_new (NULL
);
6947 introspect_append_header (s
);
6949 registered
= g_dbus_connection_list_registered_unlocked (connection
, object_path
);
6950 for (n
= 0; registered
!= NULL
&& registered
[n
] != NULL
; n
++)
6951 g_string_append_printf (s
, " <node name=\"%s\"/>\n", registered
[n
]);
6952 g_strfreev (registered
);
6953 g_string_append (s
, "</node>\n");
6955 reply
= g_dbus_message_new_method_reply (message
);
6956 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
6957 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6958 g_object_unref (reply
);
6959 g_string_free (s
, TRUE
);
6962 /* may be called in any thread, with connection's lock held */
6964 handle_generic_unlocked (GDBusConnection
*connection
,
6965 GDBusMessage
*message
)
6968 const gchar
*interface_name
;
6969 const gchar
*member
;
6970 const gchar
*signature
;
6973 CONNECTION_ENSURE_LOCK (connection
);
6977 interface_name
= g_dbus_message_get_interface (message
);
6978 member
= g_dbus_message_get_member (message
);
6979 signature
= g_dbus_message_get_signature (message
);
6980 path
= g_dbus_message_get_path (message
);
6982 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Introspectable") == 0 &&
6983 g_strcmp0 (member
, "Introspect") == 0 &&
6984 g_strcmp0 (signature
, "") == 0)
6986 handle_generic_introspect_unlocked (connection
, path
, message
);
6989 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Peer") == 0 &&
6990 g_strcmp0 (member
, "Ping") == 0 &&
6991 g_strcmp0 (signature
, "") == 0)
6993 handle_generic_ping_unlocked (connection
, path
, message
);
6996 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Peer") == 0 &&
6997 g_strcmp0 (member
, "GetMachineId") == 0 &&
6998 g_strcmp0 (signature
, "") == 0)
7000 handle_generic_get_machine_id_unlocked (connection
, path
, message
);
7007 /* ---------------------------------------------------------------------------------------------------- */
7009 /* called in GDBusWorker thread with connection's lock held */
7011 distribute_method_call (GDBusConnection
*connection
,
7012 GDBusMessage
*message
)
7014 GDBusMessage
*reply
;
7016 ExportedSubtree
*es
;
7017 const gchar
*object_path
;
7018 const gchar
*interface_name
;
7019 const gchar
*member
;
7021 gchar
*subtree_path
;
7024 g_assert (g_dbus_message_get_message_type (message
) == G_DBUS_MESSAGE_TYPE_METHOD_CALL
);
7026 interface_name
= g_dbus_message_get_interface (message
);
7027 member
= g_dbus_message_get_member (message
);
7028 path
= g_dbus_message_get_path (message
);
7029 subtree_path
= g_strdup (path
);
7030 needle
= strrchr (subtree_path
, '/');
7031 if (needle
!= NULL
&& needle
!= subtree_path
)
7037 g_free (subtree_path
);
7038 subtree_path
= NULL
;
7042 if (G_UNLIKELY (_g_dbus_debug_incoming ()))
7044 _g_dbus_debug_print_lock ();
7045 g_print ("========================================================================\n"
7046 "GDBus-debug:Incoming:\n"
7047 " <<<< METHOD INVOCATION %s.%s()\n"
7049 " invoked by name %s\n"
7051 interface_name
, member
,
7053 g_dbus_message_get_sender (message
) != NULL
? g_dbus_message_get_sender (message
) : "(none)",
7054 g_dbus_message_get_serial (message
));
7055 _g_dbus_debug_print_unlock ();
7058 object_path
= g_dbus_message_get_path (message
);
7059 g_assert (object_path
!= NULL
);
7061 eo
= g_hash_table_lookup (connection
->map_object_path_to_eo
, object_path
);
7064 if (obj_message_func (connection
, eo
, message
))
7068 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, object_path
);
7071 if (subtree_message_func (connection
, es
, message
))
7075 if (subtree_path
!= NULL
)
7077 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, subtree_path
);
7080 if (subtree_message_func (connection
, es
, message
))
7085 if (handle_generic_unlocked (connection
, message
))
7088 /* if we end up here, the message has not been not handled - so return an error saying this */
7089 reply
= g_dbus_message_new_method_error (message
,
7090 "org.freedesktop.DBus.Error.UnknownMethod",
7091 _("No such interface ā%sā on object at path %s"),
7094 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
7095 g_object_unref (reply
);
7098 g_free (subtree_path
);
7101 /* ---------------------------------------------------------------------------------------------------- */
7103 /* Called in any user thread, with the message_bus_lock held. */
7105 message_bus_get_singleton (GBusType bus_type
,
7109 const gchar
*starter_bus
;
7115 case G_BUS_TYPE_SESSION
:
7116 ret
= &the_session_bus
;
7119 case G_BUS_TYPE_SYSTEM
:
7120 ret
= &the_system_bus
;
7123 case G_BUS_TYPE_STARTER
:
7124 starter_bus
= g_getenv ("DBUS_STARTER_BUS_TYPE");
7125 if (g_strcmp0 (starter_bus
, "session") == 0)
7127 ret
= message_bus_get_singleton (G_BUS_TYPE_SESSION
, error
);
7130 else if (g_strcmp0 (starter_bus
, "system") == 0)
7132 ret
= message_bus_get_singleton (G_BUS_TYPE_SYSTEM
, error
);
7137 if (starter_bus
!= NULL
)
7141 G_IO_ERROR_INVALID_ARGUMENT
,
7142 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
7143 " ā unknown value ā%sā"),
7148 g_set_error_literal (error
,
7150 G_IO_ERROR_INVALID_ARGUMENT
,
7151 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
7152 "variable is not set"));
7158 g_assert_not_reached ();
7166 /* Called in any user thread, without holding locks. */
7167 static GDBusConnection
*
7168 get_uninitialized_connection (GBusType bus_type
,
7169 GCancellable
*cancellable
,
7172 GWeakRef
*singleton
;
7173 GDBusConnection
*ret
;
7177 G_LOCK (message_bus_lock
);
7178 singleton
= message_bus_get_singleton (bus_type
, error
);
7179 if (singleton
== NULL
)
7182 ret
= g_weak_ref_get (singleton
);
7187 address
= g_dbus_address_get_for_bus_sync (bus_type
, cancellable
, error
);
7188 if (address
== NULL
)
7190 ret
= g_object_new (G_TYPE_DBUS_CONNECTION
,
7192 "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
|
7193 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
,
7194 "exit-on-close", TRUE
,
7197 g_weak_ref_set (singleton
, ret
);
7201 g_assert (ret
!= NULL
);
7204 G_UNLOCK (message_bus_lock
);
7208 /* May be called from any thread. Must not hold message_bus_lock. */
7210 _g_bus_get_singleton_if_exists (GBusType bus_type
)
7212 GWeakRef
*singleton
;
7213 GDBusConnection
*ret
= NULL
;
7215 G_LOCK (message_bus_lock
);
7216 singleton
= message_bus_get_singleton (bus_type
, NULL
);
7217 if (singleton
== NULL
)
7220 ret
= g_weak_ref_get (singleton
);
7223 G_UNLOCK (message_bus_lock
);
7227 /* May be called from any thread. Must not hold message_bus_lock. */
7229 _g_bus_forget_singleton (GBusType bus_type
)
7231 GWeakRef
*singleton
;
7233 G_LOCK (message_bus_lock
);
7235 singleton
= message_bus_get_singleton (bus_type
, NULL
);
7237 if (singleton
!= NULL
)
7238 g_weak_ref_set (singleton
, NULL
);
7240 G_UNLOCK (message_bus_lock
);
7245 * @bus_type: a #GBusType
7246 * @cancellable: (nullable): a #GCancellable or %NULL
7247 * @error: return location for error or %NULL
7249 * Synchronously connects to the message bus specified by @bus_type.
7250 * Note that the returned object may shared with other callers,
7251 * e.g. if two separate parts of a process calls this function with
7252 * the same @bus_type, they will share the same object.
7254 * This is a synchronous failable function. See g_bus_get() and
7255 * g_bus_get_finish() for the asynchronous version.
7257 * The returned object is a singleton, that is, shared with other
7258 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
7259 * event that you need a private message bus connection, use
7260 * g_dbus_address_get_for_bus_sync() and
7261 * g_dbus_connection_new_for_address().
7263 * Note that the returned #GDBusConnection object will (usually) have
7264 * the #GDBusConnection:exit-on-close property set to %TRUE.
7266 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
7267 * Free with g_object_unref().
7272 g_bus_get_sync (GBusType bus_type
,
7273 GCancellable
*cancellable
,
7276 GDBusConnection
*connection
;
7278 _g_dbus_initialize ();
7280 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
7282 connection
= get_uninitialized_connection (bus_type
, cancellable
, error
);
7283 if (connection
== NULL
)
7286 if (!g_initable_init (G_INITABLE (connection
), cancellable
, error
))
7288 g_object_unref (connection
);
7297 bus_get_async_initable_cb (GObject
*source_object
,
7301 GTask
*task
= user_data
;
7302 GError
*error
= NULL
;
7304 if (!g_async_initable_init_finish (G_ASYNC_INITABLE (source_object
),
7308 g_assert (error
!= NULL
);
7309 g_task_return_error (task
, error
);
7310 g_object_unref (source_object
);
7314 g_task_return_pointer (task
, source_object
, g_object_unref
);
7316 g_object_unref (task
);
7321 * @bus_type: a #GBusType
7322 * @cancellable: (nullable): a #GCancellable or %NULL
7323 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7324 * @user_data: the data to pass to @callback
7326 * Asynchronously connects to the message bus specified by @bus_type.
7328 * When the operation is finished, @callback will be invoked. You can
7329 * then call g_bus_get_finish() to get the result of the operation.
7331 * This is a asynchronous failable function. See g_bus_get_sync() for
7332 * the synchronous version.
7337 g_bus_get (GBusType bus_type
,
7338 GCancellable
*cancellable
,
7339 GAsyncReadyCallback callback
,
7342 GDBusConnection
*connection
;
7344 GError
*error
= NULL
;
7346 _g_dbus_initialize ();
7348 task
= g_task_new (NULL
, cancellable
, callback
, user_data
);
7349 g_task_set_source_tag (task
, g_bus_get
);
7351 connection
= get_uninitialized_connection (bus_type
, cancellable
, &error
);
7352 if (connection
== NULL
)
7354 g_assert (error
!= NULL
);
7355 g_task_return_error (task
, error
);
7356 g_object_unref (task
);
7360 g_async_initable_init_async (G_ASYNC_INITABLE (connection
),
7363 bus_get_async_initable_cb
,
7370 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
7372 * @error: return location for error or %NULL
7374 * Finishes an operation started with g_bus_get().
7376 * The returned object is a singleton, that is, shared with other
7377 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
7378 * event that you need a private message bus connection, use
7379 * g_dbus_address_get_for_bus_sync() and
7380 * g_dbus_connection_new_for_address().
7382 * Note that the returned #GDBusConnection object will (usually) have
7383 * the #GDBusConnection:exit-on-close property set to %TRUE.
7385 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
7386 * Free with g_object_unref().
7391 g_bus_get_finish (GAsyncResult
*res
,
7394 g_return_val_if_fail (g_task_is_valid (res
, NULL
), NULL
);
7395 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
7397 return g_task_propagate_pointer (G_TASK (res
), error
);
7400 /* ---------------------------------------------------------------------------------------------------- */