1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
26 * - Validate all data (e.g. UTF-8) and check all the required D-Bus headers
27 * are present and forbidden ones aren't
28 * - When writing: g_dbus_message_to_blob()
29 * - When reading: g_dbus_message_new_from_blob()
31 * - would be nice to expose GDBusAuthMechanism and an extension point
33 * - Need to rewrite GDBusAuth and rework GDBusAuthMechanism. In particular
34 * the mechanism VFuncs need to be able to set an error.
36 * - Need to document other mechanisms/sources for determining the D-Bus
37 * address of a well-known bus.
39 * - e.g. on Win32 we need code like from here
41 * http://cgit.freedesktop.org/~david/gdbus-standalone/tree/gdbus/gdbusaddress.c#n900
43 * that was never copied over here because it originally was copy-paste
44 * from the GPLv2 / AFL 2.1 libdbus sources.
46 * - on OS X we need to look in launchd for the address
48 * https://bugs.freedesktop.org/show_bug.cgi?id=14259
50 * - on X11 we need to look in a X11 property on the X server
51 * - (we can also just use dbus-launch(1) from the D-Bus
54 * - (ideally) this requires D-Bus spec work because none of
55 * this has never really been specced out properly (excect
58 * - Related to the above, we also need to be able to launch a message bus
59 * instance.... Since we don't want to write our own bus daemon we should
60 * launch dbus-daemon(1) (thus: Win32 and OS X need to bundle it)
62 * - probably want a G_DBUS_NONCE_TCP_TMPDIR environment variable
63 * to specify where the nonce is stored. This will allow people to use
64 * G_DBUS_NONCE_TCP_TMPDIR=/mnt/secure.company.server/dbus-nonce-dir
65 * to easily acheive secure RPC via nonce-tcp.
67 * - need to expose an extension point for resolving D-Bus address and
68 * turning them into GIOStream objects. This will allow us to implement
69 * e.g. X11 D-Bus transports without dlopen()'ing or linking against
71 * - see g_dbus_address_connect() in gdbusaddress.c
73 * - would be cute to use kernel-specific APIs to resolve fds for
74 * debug output when using G_DBUS_DEBUG=message, e.g. in addition to
76 * fd 21: dev=8:1,mode=0100644,ino=1171231,uid=0,gid=0,rdev=0:0,size=234,atime=1273070640,mtime=1267126160,ctime=1267126160
78 * maybe we can show more information about what fd 21 really is.
79 * Ryan suggests looking in /proc/self/fd for clues / symlinks!
80 * Initial experiments on Linux 2.6 suggests that the symlink looks
85 * e.g. not of much use.
87 * - GDBus High-Level docs
88 * - Proxy: properties, signals...
89 * - Connection: IOStream based, ::close, connection setup steps
90 * mainloop integration, threading
91 * - Differences from libdbus (extend "Migrating from")
92 * - the message handling thread
93 * - Using GVariant instead of GValue
94 * - Explain why the high-level API is a good thing and what
95 * kind of pitfalls it avoids
96 * - Export objects before claiming names
97 * - Talk about auto-starting services (cf. GBusNameWatcherFlags)
104 #include <sys/types.h>
109 #include "gdbusauth.h"
110 #include "gdbusutils.h"
111 #include "gdbusaddress.h"
112 #include "gdbusmessage.h"
113 #include "gdbusconnection.h"
114 #include "gdbuserror.h"
115 #include "gioenumtypes.h"
116 #include "gdbusintrospection.h"
117 #include "gdbusmethodinvocation.h"
118 #include "gdbusprivate.h"
119 #include "gdbusauthobserver.h"
120 #include "gio-marshal.h"
121 #include "ginitable.h"
122 #include "gasyncinitable.h"
123 #include "giostream.h"
124 #include "gasyncresult.h"
125 #include "gsimpleasyncresult.h"
128 #include "gunixconnection.h"
129 #include "gunixfdmessage.h"
132 #include "glibintl.h"
135 * SECTION:gdbusconnection
136 * @short_description: D-Bus Connections
137 * @include: gio/gio.h
139 * The #GDBusConnection type is used for D-Bus connections to remote
140 * peers such as a message buses. It is a low-level API that offers a
141 * lot of flexibility. For instance, it lets you establish a connection
142 * over any transport that can by represented as an #GIOStream.
144 * This class is rarely used directly in D-Bus clients. If you are writing
145 * an D-Bus client, it is often easier to use the g_bus_own_name(),
146 * g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs.
148 * <example id="gdbus-server"><title>D-Bus server example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-server.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
150 * <example id="gdbus-subtree-server"><title>D-Bus subtree example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-subtree.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
152 * <example id="gdbus-unix-fd-client"><title>D-Bus UNIX File Descriptor example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-unix-fd-client.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
154 * <example id="gdbus-export"><title>Exporting a GObject</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-export.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
157 /* ---------------------------------------------------------------------------------------------------- */
159 typedef struct _GDBusConnectionClass GDBusConnectionClass;
162 * GDBusConnectionClass:
163 * @closed: Signal class handler for the #GDBusConnection::closed signal.
165 * Class structure for #GDBusConnection.
169 struct _GDBusConnectionClass
172 GObjectClass parent_class;
176 void (*closed) (GDBusConnection *connection,
177 gboolean remote_peer_vanished,
181 G_LOCK_DEFINE_STATIC (message_bus_lock);
183 static GDBusConnection *the_session_bus = NULL;
184 static GDBusConnection *the_system_bus = NULL;
186 /* ---------------------------------------------------------------------------------------------------- */
189 _g_strv_has_string (const gchar* const *haystack,
194 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
196 if (g_strcmp0 (haystack[n], needle) == 0)
202 /* ---------------------------------------------------------------------------------------------------- */
205 #define CONNECTION_ENSURE_LOCK(obj) do { ; } while (FALSE)
207 // TODO: for some reason this doesn't work on Windows
208 #define CONNECTION_ENSURE_LOCK(obj) do { \
209 if (G_UNLIKELY (g_mutex_trylock((obj)->lock))) \
211 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
212 "CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \
217 #define CONNECTION_LOCK(obj) do { \
218 g_mutex_lock ((obj)->lock); \
221 #define CONNECTION_UNLOCK(obj) do { \
222 g_mutex_unlock ((obj)->lock); \
228 * The #GDBusConnection structure contains only private data and
229 * should only be accessed using the provided API.
233 struct _GDBusConnection
236 GObject parent_instance;
238 /* ------------------------------------------------------------------------ */
239 /* -- General object state ------------------------------------------------ */
240 /* ------------------------------------------------------------------------ */
242 /* object-wide lock */
245 /* A lock used in the init() method of the GInitable interface - see comments
246 * in initable_init() for why a separate lock is needed
250 /* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
251 * someone calls org.freedesktop.DBus.GetMachineId()
255 /* The underlying stream used for communication */
258 /* The object used for authentication (if any) */
261 /* Set to TRUE if the connection has been closed */
264 /* Last serial used */
267 /* The object used to send/receive message */
270 /* If connected to a message bus, this contains the unique name assigned to
271 * us by the bus (e.g. ":1.42")
273 gchar *bus_unique_name;
275 /* The GUID returned by the other side if we authenticed as a client or
276 * the GUID to use if authenticating as a server
280 /* set to TRUE exactly when initable_init() has finished running */
281 gboolean is_initialized;
283 /* If the connection could not be established during initable_init(), this GError will set */
284 GError *initialization_error;
286 /* The result of g_main_context_get_thread_default() when the object
287 * was created (the GObject _init() function) - this is used for delivery
288 * of the :closed GObject signal.
290 GMainContext *main_context_at_construction;
292 /* construct properties */
294 GDBusConnectionFlags flags;
296 /* Map used for managing method replies */
297 GHashTable *map_method_serial_to_send_message_data; /* guint32 -> SendMessageData* */
299 /* Maps used for managing signal subscription */
300 GHashTable *map_rule_to_signal_data; /* match rule (gchar*) -> SignalData */
301 GHashTable *map_id_to_signal_data; /* id (guint) -> SignalData */
302 GHashTable *map_sender_unique_name_to_signal_data_array; /* unique sender (gchar*) -> GPtrArray* of SignalData */
304 /* Maps used for managing exported objects and subtrees */
305 GHashTable *map_object_path_to_eo; /* gchar* -> ExportedObject* */
306 GHashTable *map_id_to_ei; /* guint -> ExportedInterface* */
307 GHashTable *map_object_path_to_es; /* gchar* -> ExportedSubtree* */
308 GHashTable *map_id_to_es; /* guint -> ExportedSubtree* */
310 /* Structure used for message filters */
313 /* Whether to exit on close */
314 gboolean exit_on_close;
316 /* Capabilities negotiated during authentication */
317 GDBusCapabilityFlags capabilities;
319 GDBusAuthObserver *authentication_observer;
320 GCredentials *crendentials;
323 typedef struct ExportedObject ExportedObject;
324 static void exported_object_free (ExportedObject *eo);
326 typedef struct ExportedSubtree ExportedSubtree;
327 static void exported_subtree_free (ExportedSubtree *es);
345 PROP_CAPABILITY_FLAGS,
346 PROP_AUTHENTICATION_OBSERVER,
349 static void distribute_signals (GDBusConnection *connection,
350 GDBusMessage *message);
352 static void distribute_method_call (GDBusConnection *connection,
353 GDBusMessage *message);
355 static gboolean handle_generic_unlocked (GDBusConnection *connection,
356 GDBusMessage *message);
359 static void purge_all_signal_subscriptions (GDBusConnection *connection);
360 static void purge_all_filters (GDBusConnection *connection);
362 #define _G_ENSURE_LOCK(name) do { \
363 if (G_UNLIKELY (G_TRYLOCK(name))) \
365 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
366 "_G_ENSURE_LOCK: Lock `" #name "' is not locked"); \
370 static guint signals[LAST_SIGNAL] = { 0 };
372 static void initable_iface_init (GInitableIface *initable_iface);
373 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
375 G_DEFINE_TYPE_WITH_CODE (GDBusConnection, g_dbus_connection, G_TYPE_OBJECT,
376 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
377 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
381 g_dbus_connection_dispose (GObject *object)
383 GDBusConnection *connection = G_DBUS_CONNECTION (object);
385 G_LOCK (message_bus_lock);
386 //g_debug ("disposing %p", connection);
387 if (connection == the_session_bus)
389 the_session_bus = NULL;
391 else if (connection == the_system_bus)
393 the_system_bus = NULL;
395 if (connection->worker != NULL)
397 _g_dbus_worker_stop (connection->worker);
398 connection->worker = NULL;
400 G_UNLOCK (message_bus_lock);
402 if (G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose != NULL)
403 G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose (object);
407 g_dbus_connection_finalize (GObject *object)
409 GDBusConnection *connection = G_DBUS_CONNECTION (object);
411 if (connection->authentication_observer != NULL)
412 g_object_unref (connection->authentication_observer);
414 if (connection->auth != NULL)
415 g_object_unref (connection->auth);
417 //g_debug ("finalizing %p", connection);
418 if (connection->stream != NULL)
420 /* We don't really care if closing the stream succeeds or not */
421 g_io_stream_close_async (connection->stream,
423 NULL, /* GCancellable */
424 NULL, /* GAsyncReadyCallback */
425 NULL); /* userdata */
426 g_object_unref (connection->stream);
427 connection->stream = NULL;
430 g_free (connection->address);
432 g_free (connection->guid);
433 g_free (connection->bus_unique_name);
435 if (connection->initialization_error != NULL)
436 g_error_free (connection->initialization_error);
438 g_hash_table_unref (connection->map_method_serial_to_send_message_data);
440 purge_all_signal_subscriptions (connection);
441 g_hash_table_unref (connection->map_rule_to_signal_data);
442 g_hash_table_unref (connection->map_id_to_signal_data);
443 g_hash_table_unref (connection->map_sender_unique_name_to_signal_data_array);
445 g_hash_table_unref (connection->map_id_to_ei);
446 g_hash_table_unref (connection->map_object_path_to_eo);
447 g_hash_table_unref (connection->map_id_to_es);
448 g_hash_table_unref (connection->map_object_path_to_es);
450 purge_all_filters (connection);
451 g_ptr_array_unref (connection->filters);
453 if (connection->main_context_at_construction != NULL)
454 g_main_context_unref (connection->main_context_at_construction);
456 g_free (connection->machine_id);
458 g_mutex_free (connection->init_lock);
459 g_mutex_free (connection->lock);
461 G_OBJECT_CLASS (g_dbus_connection_parent_class)->finalize (object);
465 g_dbus_connection_get_property (GObject *object,
470 GDBusConnection *connection = G_DBUS_CONNECTION (object);
475 g_value_set_object (value, g_dbus_connection_get_stream (connection));
479 g_value_set_string (value, g_dbus_connection_get_guid (connection));
482 case PROP_UNIQUE_NAME:
483 g_value_set_string (value, g_dbus_connection_get_unique_name (connection));
487 g_value_set_boolean (value, g_dbus_connection_is_closed (connection));
490 case PROP_EXIT_ON_CLOSE:
491 g_value_set_boolean (value, g_dbus_connection_get_exit_on_close (connection));
494 case PROP_CAPABILITY_FLAGS:
495 g_value_set_flags (value, g_dbus_connection_get_capabilities (connection));
499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505 g_dbus_connection_set_property (GObject *object,
510 GDBusConnection *connection = G_DBUS_CONNECTION (object);
515 connection->stream = g_value_dup_object (value);
519 connection->guid = g_value_dup_string (value);
523 connection->address = g_value_dup_string (value);
527 connection->flags = g_value_get_flags (value);
530 case PROP_EXIT_ON_CLOSE:
531 g_dbus_connection_set_exit_on_close (connection, g_value_get_boolean (value));
534 case PROP_AUTHENTICATION_OBSERVER:
535 connection->authentication_observer = g_value_dup_object (value);
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
545 g_dbus_connection_real_closed (GDBusConnection *connection,
546 gboolean remote_peer_vanished,
549 if (remote_peer_vanished && connection->exit_on_close)
551 g_print ("%s: Remote peer vanished. Exiting.\n", G_STRFUNC);
557 g_dbus_connection_class_init (GDBusConnectionClass *klass)
559 GObjectClass *gobject_class;
561 gobject_class = G_OBJECT_CLASS (klass);
563 gobject_class->finalize = g_dbus_connection_finalize;
564 gobject_class->dispose = g_dbus_connection_dispose;
565 gobject_class->set_property = g_dbus_connection_set_property;
566 gobject_class->get_property = g_dbus_connection_get_property;
568 klass->closed = g_dbus_connection_real_closed;
571 * GDBusConnection:stream:
573 * The underlying #GIOStream used for I/O.
577 g_object_class_install_property (gobject_class,
579 g_param_spec_object ("stream",
581 P_("The underlying streams used for I/O"),
585 G_PARAM_CONSTRUCT_ONLY |
586 G_PARAM_STATIC_NAME |
587 G_PARAM_STATIC_BLURB |
588 G_PARAM_STATIC_NICK));
591 * GDBusConnection:address:
593 * A D-Bus address specifying potential endpoints that can be used
594 * when establishing the connection.
598 g_object_class_install_property (gobject_class,
600 g_param_spec_string ("address",
602 P_("D-Bus address specifying potential socket endpoints"),
605 G_PARAM_CONSTRUCT_ONLY |
606 G_PARAM_STATIC_NAME |
607 G_PARAM_STATIC_BLURB |
608 G_PARAM_STATIC_NICK));
611 * GDBusConnection:flags:
613 * Flags from the #GDBusConnectionFlags enumeration.
617 g_object_class_install_property (gobject_class,
619 g_param_spec_flags ("flags",
622 G_TYPE_DBUS_CONNECTION_FLAGS,
623 G_DBUS_CONNECTION_FLAGS_NONE,
625 G_PARAM_CONSTRUCT_ONLY |
626 G_PARAM_STATIC_NAME |
627 G_PARAM_STATIC_BLURB |
628 G_PARAM_STATIC_NICK));
631 * GDBusConnection:guid:
633 * The GUID of the peer performing the role of server when
636 * If you are constructing a #GDBusConnection and pass
637 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER in the
638 * #GDBusConnection:flags property then you MUST also set this
639 * property to a valid guid.
641 * If you are constructing a #GDBusConnection and pass
642 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT in the
643 * #GDBusConnection:flags property you will be able to read the GUID
644 * of the other peer here after the connection has been successfully
649 g_object_class_install_property (gobject_class,
651 g_param_spec_string ("guid",
653 P_("GUID of the server peer"),
657 G_PARAM_CONSTRUCT_ONLY |
658 G_PARAM_STATIC_NAME |
659 G_PARAM_STATIC_BLURB |
660 G_PARAM_STATIC_NICK));
663 * GDBusConnection:unique-name:
665 * The unique name as assigned by the message bus or %NULL if the
666 * connection is not open or not a message bus connection.
670 g_object_class_install_property (gobject_class,
672 g_param_spec_string ("unique-name",
674 P_("Unique name of bus connection"),
677 G_PARAM_STATIC_NAME |
678 G_PARAM_STATIC_BLURB |
679 G_PARAM_STATIC_NICK));
682 * GDBusConnection:closed:
684 * A boolean specifying whether the connection has been closed.
688 g_object_class_install_property (gobject_class,
690 g_param_spec_boolean ("closed",
692 P_("Whether the connection is closed"),
695 G_PARAM_STATIC_NAME |
696 G_PARAM_STATIC_BLURB |
697 G_PARAM_STATIC_NICK));
700 * GDBusConnection:exit-on-close:
702 * A boolean specifying whether the process will be terminated (by
703 * calling <literal>raise(SIGTERM)</literal>) if the connection
704 * is closed by the remote peer.
708 g_object_class_install_property (gobject_class,
710 g_param_spec_boolean ("exit-on-close",
712 P_("Whether the process is terminated when the connection is closed"),
716 G_PARAM_STATIC_NAME |
717 G_PARAM_STATIC_BLURB |
718 G_PARAM_STATIC_NICK));
721 * GDBusConnection:capabilities:
723 * Flags from the #GDBusCapabilityFlags enumeration
724 * representing connection features negotiated with the other peer.
728 g_object_class_install_property (gobject_class,
729 PROP_CAPABILITY_FLAGS,
730 g_param_spec_flags ("capabilities",
733 G_TYPE_DBUS_CAPABILITY_FLAGS,
734 G_DBUS_CAPABILITY_FLAGS_NONE,
736 G_PARAM_STATIC_NAME |
737 G_PARAM_STATIC_BLURB |
738 G_PARAM_STATIC_NICK));
741 * GDBusConnection:authentication-observer:
743 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
747 g_object_class_install_property (gobject_class,
748 PROP_AUTHENTICATION_OBSERVER,
749 g_param_spec_object ("authentication-observer",
750 P_("Authentication Observer"),
751 P_("Object used to assist in the authentication process"),
752 G_TYPE_DBUS_AUTH_OBSERVER,
754 G_PARAM_CONSTRUCT_ONLY |
755 G_PARAM_STATIC_NAME |
756 G_PARAM_STATIC_BLURB |
757 G_PARAM_STATIC_NICK));
760 * GDBusConnection::closed:
761 * @connection: The #GDBusConnection emitting the signal.
762 * @remote_peer_vanished: %TRUE if @connection is closed because the
763 * remote peer closed its end of the connection.
764 * @error: A #GError with more details about the event or %NULL.
766 * Emitted when the connection is closed.
768 * The cause of this event can be
771 * If g_dbus_connection_close() is called. In this case
772 * @remote_peer_vanished is set to %FALSE and @error is %NULL.
775 * If the remote peer closes the connection. In this case
776 * @remote_peer_vanished is set to %TRUE and @error is set.
779 * If the remote peer sends invalid or malformed data. In this
780 * case @remote_peer_vanished is set to %FALSE and @error
785 * Upon receiving this signal, you should give up your reference to
786 * @connection. You are guaranteed that this signal is emitted only
791 signals[CLOSED_SIGNAL] = g_signal_new ("closed",
792 G_TYPE_DBUS_CONNECTION,
794 G_STRUCT_OFFSET (GDBusConnectionClass, closed),
797 _gio_marshal_VOID__BOOLEAN_BOXED,
805 g_dbus_connection_init (GDBusConnection *connection)
807 connection->lock = g_mutex_new ();
808 connection->init_lock = g_mutex_new ();
810 connection->map_method_serial_to_send_message_data = g_hash_table_new (g_direct_hash, g_direct_equal);
812 connection->map_rule_to_signal_data = g_hash_table_new (g_str_hash,
814 connection->map_id_to_signal_data = g_hash_table_new (g_direct_hash,
816 connection->map_sender_unique_name_to_signal_data_array = g_hash_table_new_full (g_str_hash,
821 connection->map_object_path_to_eo = g_hash_table_new_full (g_str_hash,
824 (GDestroyNotify) exported_object_free);
826 connection->map_id_to_ei = g_hash_table_new (g_direct_hash,
829 connection->map_object_path_to_es = g_hash_table_new_full (g_str_hash,
832 (GDestroyNotify) exported_subtree_free);
834 connection->map_id_to_es = g_hash_table_new (g_direct_hash,
837 connection->main_context_at_construction = g_main_context_get_thread_default ();
838 if (connection->main_context_at_construction != NULL)
839 g_main_context_ref (connection->main_context_at_construction);
841 connection->filters = g_ptr_array_new ();
845 * g_dbus_connection_get_stream:
846 * @connection: a #GDBusConnection
848 * Gets the underlying stream used for IO.
850 * Returns: the stream used for IO
855 g_dbus_connection_get_stream (GDBusConnection *connection)
857 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
858 return connection->stream;
862 * g_dbus_connection_start_message_processing:
863 * @connection: A #GDBusConnection.
865 * If @connection was created with
866 * %G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, this method
867 * starts processing messages. Does nothing on if @connection wasn't
868 * created with this flag or if the method has already been called.
873 g_dbus_connection_start_message_processing (GDBusConnection *connection)
875 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
876 _g_dbus_worker_unfreeze (connection->worker);
880 * g_dbus_connection_is_closed:
881 * @connection: A #GDBusConnection.
883 * Gets whether @connection is closed.
885 * Returns: %TRUE if the connection is closed, %FALSE otherwise.
890 g_dbus_connection_is_closed (GDBusConnection *connection)
892 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
893 return connection->closed;
897 * g_dbus_connection_get_capabilities:
898 * @connection: A #GDBusConnection.
900 * Gets the capabilities negotiated with the remote peer
902 * Returns: Zero or more flags from the #GDBusCapabilityFlags enumeration.
907 g_dbus_connection_get_capabilities (GDBusConnection *connection)
909 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CAPABILITY_FLAGS_NONE);
910 return connection->capabilities;
913 /* ---------------------------------------------------------------------------------------------------- */
916 flush_in_thread_func (GSimpleAsyncResult *res,
918 GCancellable *cancellable)
923 if (!g_dbus_connection_flush_sync (G_DBUS_CONNECTION (object),
927 g_simple_async_result_set_from_error (res, error);
928 g_error_free (error);
933 * g_dbus_connection_flush:
934 * @connection: A #GDBusConnection.
935 * @cancellable: A #GCancellable or %NULL.
936 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
937 * care about the result.
938 * @user_data: The data to pass to @callback.
940 * Asynchronously flushes @connection, that is, writes all queued
941 * outgoing message to the transport and then flushes the transport
942 * (using g_output_stream_flush_async()). This is useful in programs
943 * that wants to emit a D-Bus signal and then exit
944 * immediately. Without flushing the connection, there is no guarantee
945 * that the message has been sent to the networking buffers in the OS
948 * This is an asynchronous method. When the operation is finished,
949 * @callback will be invoked in the <link
950 * linkend="g-main-context-push-thread-default">thread-default main
951 * loop</link> of the thread you are calling this method from. You can
952 * then call g_dbus_connection_flush_finish() to get the result of the
953 * operation. See g_dbus_connection_flush_sync() for the synchronous
959 g_dbus_connection_flush (GDBusConnection *connection,
960 GCancellable *cancellable,
961 GAsyncReadyCallback callback,
964 GSimpleAsyncResult *simple;
966 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
968 simple = g_simple_async_result_new (NULL,
971 g_dbus_connection_flush);
972 g_simple_async_result_run_in_thread (simple,
973 flush_in_thread_func,
976 g_object_unref (simple);
980 * g_dbus_connection_flush_finish:
981 * @connection: A #GDBusConnection.
982 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_flush().
983 * @error: Return location for error or %NULL.
985 * Finishes an operation started with g_dbus_connection_flush().
987 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
992 g_dbus_connection_flush_finish (GDBusConnection *connection,
996 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1001 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1002 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
1003 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1005 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_flush);
1007 if (g_simple_async_result_propagate_error (simple, error))
1017 * g_dbus_connection_flush_sync:
1018 * @connection: A #GDBusConnection.
1019 * @cancellable: A #GCancellable or %NULL.
1020 * @error: Return location for error or %NULL.
1022 * Synchronously flushes @connection. The calling thread is blocked
1023 * until this is done. See g_dbus_connection_flush() for the
1024 * asynchronous version of this method and more details about what it
1027 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1032 g_dbus_connection_flush_sync (GDBusConnection *connection,
1033 GCancellable *cancellable,
1038 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1039 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1043 if (connection->closed)
1045 g_set_error_literal (error,
1048 _("The connection is closed"));
1052 ret = _g_dbus_worker_flush_sync (connection->worker,
1060 /* ---------------------------------------------------------------------------------------------------- */
1064 GDBusConnection *connection;
1066 gboolean remote_peer_vanished;
1070 emit_closed_data_free (EmitClosedData *data)
1072 g_object_unref (data->connection);
1073 if (data->error != NULL)
1074 g_error_free (data->error);
1079 emit_closed_in_idle (gpointer user_data)
1081 EmitClosedData *data = user_data;
1084 g_object_notify (G_OBJECT (data->connection), "closed");
1085 g_signal_emit (data->connection,
1086 signals[CLOSED_SIGNAL],
1088 data->remote_peer_vanished,
1094 /* Can be called from any thread, must hold lock */
1096 set_closed_unlocked (GDBusConnection *connection,
1097 gboolean remote_peer_vanished,
1100 GSource *idle_source;
1101 EmitClosedData *data;
1103 CONNECTION_ENSURE_LOCK (connection);
1105 g_assert (!connection->closed);
1107 connection->closed = TRUE;
1109 data = g_new0 (EmitClosedData, 1);
1110 data->connection = g_object_ref (connection);
1111 data->remote_peer_vanished = remote_peer_vanished;
1112 data->error = error != NULL ? g_error_copy (error) : NULL;
1114 idle_source = g_idle_source_new ();
1115 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1116 g_source_set_callback (idle_source,
1117 emit_closed_in_idle,
1119 (GDestroyNotify) emit_closed_data_free);
1120 g_source_attach (idle_source, connection->main_context_at_construction);
1121 g_source_unref (idle_source);
1124 /* ---------------------------------------------------------------------------------------------------- */
1127 close_in_thread_func (GSimpleAsyncResult *res,
1129 GCancellable *cancellable)
1134 if (!g_dbus_connection_close_sync (G_DBUS_CONNECTION (object),
1138 g_simple_async_result_set_from_error (res, error);
1139 g_error_free (error);
1144 * g_dbus_connection_close:
1145 * @connection: A #GDBusConnection.
1146 * @cancellable: A #GCancellable or %NULL.
1147 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
1148 * care about the result.
1149 * @user_data: The data to pass to @callback.
1151 * Closes @connection. Note that this never causes the process to
1152 * exit (this might only happen if the other end of a shared message
1153 * bus connection disconnects, see #GDBusConnection:exit-on-close).
1155 * Once the connection is closed, operations such as sending a message
1156 * will return with the error %G_IO_ERROR_CLOSED. Closing a connection
1157 * will not automatically flush the connection so queued messages may
1158 * be lost. Use g_dbus_connection_flush() if you need such guarantees.
1160 * If @connection is already closed, this method fails with
1161 * %G_IO_ERROR_CLOSED.
1163 * When @connection has been closed, the #GDBusConnection::closed
1164 * signal is emitted in the <link
1165 * linkend="g-main-context-push-thread-default">thread-default main
1166 * loop</link> of the thread that @connection was constructed in.
1168 * This is an asynchronous method. When the operation is finished,
1169 * @callback will be invoked in the <link
1170 * linkend="g-main-context-push-thread-default">thread-default main
1171 * loop</link> of the thread you are calling this method from. You can
1172 * then call g_dbus_connection_close_finish() to get the result of the
1173 * operation. See g_dbus_connection_close_sync() for the synchronous
1179 g_dbus_connection_close (GDBusConnection *connection,
1180 GCancellable *cancellable,
1181 GAsyncReadyCallback callback,
1184 GSimpleAsyncResult *simple;
1186 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1188 simple = g_simple_async_result_new (NULL,
1191 g_dbus_connection_close);
1192 g_simple_async_result_run_in_thread (simple,
1193 close_in_thread_func,
1196 g_object_unref (simple);
1200 * g_dbus_connection_close_finish:
1201 * @connection: A #GDBusConnection.
1202 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_close().
1203 * @error: Return location for error or %NULL.
1205 * Finishes an operation started with g_dbus_connection_close().
1207 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1212 g_dbus_connection_close_finish (GDBusConnection *connection,
1216 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1221 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1222 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
1223 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1225 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_close);
1227 if (g_simple_async_result_propagate_error (simple, error))
1237 * g_dbus_connection_close_sync:
1238 * @connection: A #GDBusConnection.
1239 * @cancellable: A #GCancellable or %NULL.
1240 * @error: Return location for error or %NULL.
1242 * Synchronously closees @connection. The calling thread is blocked
1243 * until this is done. See g_dbus_connection_close() for the
1244 * asynchronous version of this method and more details about what it
1247 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
1252 g_dbus_connection_close_sync (GDBusConnection *connection,
1253 GCancellable *cancellable,
1258 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1259 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1263 CONNECTION_LOCK (connection);
1264 if (!connection->closed)
1266 ret = g_io_stream_close (connection->stream,
1270 set_closed_unlocked (connection, FALSE, NULL);
1274 g_set_error_literal (error,
1277 _("The connection is closed"));
1279 CONNECTION_UNLOCK (connection);
1284 /* ---------------------------------------------------------------------------------------------------- */
1287 g_dbus_connection_send_message_unlocked (GDBusConnection *connection,
1288 GDBusMessage *message,
1289 volatile guint32 *out_serial,
1294 guint32 serial_to_use;
1297 CONNECTION_ENSURE_LOCK (connection);
1299 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1300 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1302 /* TODO: check all necessary headers are present */
1307 if (out_serial != NULL)
1310 if (connection->closed)
1312 g_set_error_literal (error,
1315 _("The connection is closed"));
1319 blob = g_dbus_message_to_blob (message,
1321 connection->capabilities,
1326 serial_to_use = g_dbus_message_get_serial (message);
1327 if (serial_to_use == 0)
1329 serial_to_use = ++connection->last_serial; /* TODO: handle overflow */
1335 ((guint32 *) blob)[2] = GUINT32_TO_LE (serial_to_use);
1338 ((guint32 *) blob)[2] = GUINT32_TO_BE (serial_to_use);
1341 g_assert_not_reached ();
1346 g_printerr ("Writing message of %" G_GSIZE_FORMAT " bytes (serial %d) on %p:\n",
1347 blob_size, serial_to_use, connection);
1348 g_printerr ("----\n");
1349 hexdump (blob, blob_size);
1350 g_printerr ("----\n");
1353 /* TODO: use connection->auth to encode the blob */
1355 if (out_serial != NULL)
1356 *out_serial = serial_to_use;
1358 g_dbus_message_set_serial (message, serial_to_use);
1360 _g_dbus_worker_send_message (connection->worker,
1364 blob = NULL; /* since _g_dbus_worker_send_message() steals the blob */
1375 * g_dbus_connection_send_message:
1376 * @connection: A #GDBusConnection.
1377 * @message: A #GDBusMessage
1378 * @out_serial: Return location for serial number assigned to @message when sending it or %NULL.
1379 * @error: Return location for error or %NULL.
1381 * Asynchronously sends @message to the peer represented by @connection.
1383 * If g_dbus_message_get_serial() returns non-zero for @message, then
1384 * that value is used for the message serial number. Otherwise a
1385 * serial number will be assigned by @connection and set on @message
1386 * via g_dbus_message_set_serial(). If @out_serial is not %NULL, then
1387 * the serial number used will be written to this location prior to
1388 * submitting the message to the underlying transport.
1390 * If @connection is closed then the operation will fail with
1391 * %G_IO_ERROR_CLOSED. If @message is not well-formed,
1392 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1394 * See <xref linkend="gdbus-server"/> and <xref
1395 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1396 * low-level API to send and receive UNIX file descriptors.
1398 * Returns: %TRUE if the message was well-formed and queued for
1399 * transmission, %FALSE if @error is set.
1404 g_dbus_connection_send_message (GDBusConnection *connection,
1405 GDBusMessage *message,
1406 volatile guint32 *out_serial,
1411 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1412 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1413 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1415 CONNECTION_LOCK (connection);
1416 ret = g_dbus_connection_send_message_unlocked (connection, message, out_serial, error);
1417 CONNECTION_UNLOCK (connection);
1421 /* ---------------------------------------------------------------------------------------------------- */
1425 volatile gint ref_count;
1426 GDBusConnection *connection;
1428 GSimpleAsyncResult *simple;
1430 GMainContext *main_context;
1432 GCancellable *cancellable;
1434 gulong cancellable_handler_id;
1436 GSource *timeout_source;
1441 static SendMessageData *
1442 send_message_data_ref (SendMessageData *data)
1444 g_atomic_int_inc (&data->ref_count);
1449 send_message_data_unref (SendMessageData *data)
1451 if (g_atomic_int_dec_and_test (&data->ref_count))
1453 g_assert (data->timeout_source == NULL);
1454 g_assert (data->simple == NULL);
1455 g_assert (data->cancellable_handler_id == 0);
1456 g_object_unref (data->connection);
1457 if (data->cancellable != NULL)
1458 g_object_unref (data->cancellable);
1459 if (data->main_context != NULL)
1460 g_main_context_unref (data->main_context);
1465 /* ---------------------------------------------------------------------------------------------------- */
1467 /* can be called from any thread with lock held - caller must have prepared GSimpleAsyncResult already */
1469 send_message_with_reply_deliver (SendMessageData *data)
1471 CONNECTION_ENSURE_LOCK (data->connection);
1473 g_assert (!data->delivered);
1475 data->delivered = TRUE;
1477 g_simple_async_result_complete_in_idle (data->simple);
1478 g_object_unref (data->simple);
1479 data->simple = NULL;
1481 if (data->timeout_source != NULL)
1483 g_source_destroy (data->timeout_source);
1484 data->timeout_source = NULL;
1486 if (data->cancellable_handler_id > 0)
1488 g_cancellable_disconnect (data->cancellable, data->cancellable_handler_id);
1489 data->cancellable_handler_id = 0;
1492 g_warn_if_fail (g_hash_table_remove (data->connection->map_method_serial_to_send_message_data,
1493 GUINT_TO_POINTER (data->serial)));
1495 send_message_data_unref (data);
1498 /* ---------------------------------------------------------------------------------------------------- */
1500 /* must hold lock */
1502 send_message_data_deliver_reply_unlocked (SendMessageData *data,
1503 GDBusMessage *reply)
1505 if (data->delivered)
1508 g_simple_async_result_set_op_res_gpointer (data->simple,
1509 g_object_ref (reply),
1512 send_message_with_reply_deliver (data);
1518 /* ---------------------------------------------------------------------------------------------------- */
1521 send_message_with_reply_cancelled_idle_cb (gpointer user_data)
1523 SendMessageData *data = user_data;
1525 CONNECTION_LOCK (data->connection);
1526 if (data->delivered)
1529 g_simple_async_result_set_error (data->simple,
1531 G_IO_ERROR_CANCELLED,
1532 _("Operation was cancelled"));
1534 send_message_with_reply_deliver (data);
1537 CONNECTION_UNLOCK (data->connection);
1541 /* Can be called from any thread with or without lock held */
1543 send_message_with_reply_cancelled_cb (GCancellable *cancellable,
1546 SendMessageData *data = user_data;
1547 GSource *idle_source;
1549 /* postpone cancellation to idle handler since we may be called directly
1550 * via g_cancellable_connect() (e.g. holding lock)
1552 idle_source = g_idle_source_new ();
1553 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1554 g_source_set_callback (idle_source,
1555 send_message_with_reply_cancelled_idle_cb,
1556 send_message_data_ref (data),
1557 (GDestroyNotify) send_message_data_unref);
1558 g_source_attach (idle_source, data->main_context);
1559 g_source_unref (idle_source);
1562 /* ---------------------------------------------------------------------------------------------------- */
1565 send_message_with_reply_timeout_cb (gpointer user_data)
1567 SendMessageData *data = user_data;
1569 CONNECTION_LOCK (data->connection);
1570 if (data->delivered)
1573 g_simple_async_result_set_error (data->simple,
1575 G_IO_ERROR_TIMED_OUT,
1576 _("Timeout was reached"));
1578 send_message_with_reply_deliver (data);
1581 CONNECTION_UNLOCK (data->connection);
1586 /* ---------------------------------------------------------------------------------------------------- */
1589 g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection *connection,
1590 GDBusMessage *message,
1592 volatile guint32 *out_serial,
1593 GCancellable *cancellable,
1594 GAsyncReadyCallback callback,
1597 GSimpleAsyncResult *simple;
1598 SendMessageData *data;
1600 volatile guint32 serial;
1604 if (out_serial == NULL)
1605 out_serial = &serial;
1607 if (timeout_msec == -1)
1608 timeout_msec = 25 * 1000;
1610 simple = g_simple_async_result_new (G_OBJECT (connection),
1613 g_dbus_connection_send_message_with_reply);
1615 if (g_cancellable_is_cancelled (cancellable))
1617 g_simple_async_result_set_error (simple,
1619 G_IO_ERROR_CANCELLED,
1620 _("Operation was cancelled"));
1621 g_simple_async_result_complete_in_idle (simple);
1622 g_object_unref (simple);
1626 if (connection->closed)
1628 g_simple_async_result_set_error (simple,
1631 _("The connection is closed"));
1632 g_simple_async_result_complete_in_idle (simple);
1633 g_object_unref (simple);
1638 if (!g_dbus_connection_send_message_unlocked (connection, message, out_serial, &error))
1640 g_simple_async_result_set_from_error (simple, error);
1641 g_simple_async_result_complete_in_idle (simple);
1642 g_object_unref (simple);
1646 data = g_new0 (SendMessageData, 1);
1647 data->ref_count = 1;
1648 data->connection = g_object_ref (connection);
1649 data->simple = simple;
1650 data->serial = *out_serial;
1651 data->main_context = g_main_context_get_thread_default ();
1652 if (data->main_context != NULL)
1653 g_main_context_ref (data->main_context);
1655 if (cancellable != NULL)
1657 data->cancellable = g_object_ref (cancellable);
1658 data->cancellable_handler_id = g_cancellable_connect (cancellable,
1659 G_CALLBACK (send_message_with_reply_cancelled_cb),
1660 send_message_data_ref (data),
1661 (GDestroyNotify) send_message_data_unref);
1662 g_object_set_data_full (G_OBJECT (simple),
1664 g_object_ref (cancellable),
1665 (GDestroyNotify) g_object_unref);
1668 data->timeout_source = g_timeout_source_new (timeout_msec);
1669 g_source_set_priority (data->timeout_source, G_PRIORITY_DEFAULT);
1670 g_source_set_callback (data->timeout_source,
1671 send_message_with_reply_timeout_cb,
1672 send_message_data_ref (data),
1673 (GDestroyNotify) send_message_data_unref);
1674 g_source_attach (data->timeout_source, data->main_context);
1675 g_source_unref (data->timeout_source);
1677 g_hash_table_insert (connection->map_method_serial_to_send_message_data,
1678 GUINT_TO_POINTER (*out_serial),
1686 * g_dbus_connection_send_message_with_reply:
1687 * @connection: A #GDBusConnection.
1688 * @message: A #GDBusMessage.
1689 * @timeout_msec: The timeout in milliseconds or -1 to use the default timeout.
1690 * @out_serial: Return location for serial number assigned to @message when sending it or %NULL.
1691 * @cancellable: A #GCancellable or %NULL.
1692 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
1693 * care about the result.
1694 * @user_data: The data to pass to @callback.
1696 * Asynchronously sends @message to the peer represented by @connection.
1698 * If g_dbus_message_get_serial() returns non-zero for @message, then
1699 * that value is used for the message serial number. Otherwise a
1700 * serial number will be assigned by @connection and set on @message
1701 * via g_dbus_message_set_serial(). If @out_serial is not %NULL, then
1702 * the serial number used will be written to this location prior to
1703 * submitting the message to the underlying transport.
1705 * If @connection is closed then the operation will fail with
1706 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
1707 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
1708 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1710 * This is an asynchronous method. When the operation is finished, @callback will be invoked
1711 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
1712 * of the thread you are calling this method from. You can then call
1713 * g_dbus_connection_send_message_with_reply_finish() to get the result of the operation.
1714 * See g_dbus_connection_send_message_with_reply_sync() for the synchronous version.
1716 * See <xref linkend="gdbus-server"/> and <xref
1717 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1718 * low-level API to send and receive UNIX file descriptors.
1723 g_dbus_connection_send_message_with_reply (GDBusConnection *connection,
1724 GDBusMessage *message,
1726 volatile guint32 *out_serial,
1727 GCancellable *cancellable,
1728 GAsyncReadyCallback callback,
1731 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1732 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1733 g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
1735 CONNECTION_LOCK (connection);
1736 g_dbus_connection_send_message_with_reply_unlocked (connection,
1743 CONNECTION_UNLOCK (connection);
1747 * g_dbus_connection_send_message_with_reply_finish:
1748 * @connection: a #GDBusConnection
1749 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_send_message_with_reply().
1750 * @error: Return location for error or %NULL.
1752 * Finishes an operation started with g_dbus_connection_send_message_with_reply().
1754 * Note that @error is only set if a local in-process error
1755 * occured. That is to say that the returned #GDBusMessage object may
1756 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
1757 * g_dbus_message_to_gerror() to transcode this to a #GError.
1759 * See <xref linkend="gdbus-server"/> and <xref
1760 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1761 * low-level API to send and receive UNIX file descriptors.
1763 * Returns: A #GDBusMessage or %NULL if @error is set.
1768 g_dbus_connection_send_message_with_reply_finish (GDBusConnection *connection,
1772 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1773 GDBusMessage *reply;
1774 GCancellable *cancellable;
1776 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1777 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1781 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_send_message_with_reply);
1783 if (g_simple_async_result_propagate_error (simple, error))
1786 reply = g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1787 cancellable = g_object_get_data (G_OBJECT (simple), "cancellable");
1788 if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
1790 g_object_unref (reply);
1792 g_set_error_literal (error,
1794 G_IO_ERROR_CANCELLED,
1795 _("Operation was cancelled"));
1801 /* ---------------------------------------------------------------------------------------------------- */
1806 GMainContext *context;
1808 } SendMessageSyncData;
1811 send_message_with_reply_sync_cb (GDBusConnection *connection,
1815 SendMessageSyncData *data = user_data;
1816 data->res = g_object_ref (res);
1817 g_main_loop_quit (data->loop);
1821 * g_dbus_connection_send_message_with_reply_sync:
1822 * @connection: A #GDBusConnection.
1823 * @message: A #GDBusMessage.
1824 * @timeout_msec: The timeout in milliseconds or -1 to use the default timeout.
1825 * @out_serial: Return location for serial number assigned to @message when sending it or %NULL.
1826 * @cancellable: A #GCancellable or %NULL.
1827 * @error: Return location for error or %NULL.
1829 * Synchronously sends @message to the peer represented by @connection
1830 * and blocks the calling thread until a reply is received or the
1831 * timeout is reached. See g_dbus_connection_send_message_with_reply()
1832 * for the asynchronous version of this method.
1834 * If g_dbus_message_get_serial() returns non-zero for @message, then
1835 * that value is used for the message serial number. Otherwise a
1836 * serial number will be assigned by @connection and set on @message
1837 * via g_dbus_message_set_serial(). If @out_serial is not %NULL, then
1838 * the serial number used will be written to this location prior to
1839 * submitting the message to the underlying transport.
1841 * If @connection is closed then the operation will fail with
1842 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
1843 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
1844 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1846 * Note that @error is only set if a local in-process error
1847 * occured. That is to say that the returned #GDBusMessage object may
1848 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
1849 * g_dbus_message_to_gerror() to transcode this to a #GError.
1851 * See <xref linkend="gdbus-server"/> and <xref
1852 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
1853 * low-level API to send and receive UNIX file descriptors.
1855 * Returns: A #GDBusMessage that is the reply to @message or %NULL if @error is set.
1860 g_dbus_connection_send_message_with_reply_sync (GDBusConnection *connection,
1861 GDBusMessage *message,
1863 volatile guint32 *out_serial,
1864 GCancellable *cancellable,
1867 SendMessageSyncData *data;
1868 GDBusMessage *reply;
1870 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1871 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1872 g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
1873 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1875 data = g_new0 (SendMessageSyncData, 1);
1876 data->context = g_main_context_new ();
1877 data->loop = g_main_loop_new (data->context, FALSE);
1879 g_main_context_push_thread_default (data->context);
1881 g_dbus_connection_send_message_with_reply (connection,
1886 (GAsyncReadyCallback) send_message_with_reply_sync_cb,
1888 g_main_loop_run (data->loop);
1889 reply = g_dbus_connection_send_message_with_reply_finish (connection,
1893 g_main_context_pop_thread_default (data->context);
1895 g_main_context_unref (data->context);
1896 g_main_loop_unref (data->loop);
1897 g_object_unref (data->res);
1903 /* ---------------------------------------------------------------------------------------------------- */
1907 GDBusMessageFilterFunction func;
1914 GDBusMessageFilterFunction filter_function;
1916 GDestroyNotify user_data_free_func;
1919 /* Called in worker's thread - we must not block */
1921 on_worker_message_received (GDBusWorker *worker,
1922 GDBusMessage *message,
1925 GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
1926 FilterCallback *filters;
1927 gboolean consumed_by_filter;
1931 //g_debug ("in on_worker_message_received");
1933 g_object_ref (connection);
1935 /* First collect the set of callback functions */
1936 CONNECTION_LOCK (connection);
1937 num_filters = connection->filters->len;
1938 filters = g_new0 (FilterCallback, num_filters);
1939 for (n = 0; n < num_filters; n++)
1941 FilterData *data = connection->filters->pdata[n];
1942 filters[n].func = data->filter_function;
1943 filters[n].user_data = data->user_data;
1945 CONNECTION_UNLOCK (connection);
1947 /* the call the filters in order (without holding the lock) */
1948 consumed_by_filter = FALSE;
1949 for (n = 0; n < num_filters; n++)
1951 consumed_by_filter = filters[n].func (connection,
1954 filters[n].user_data);
1955 if (consumed_by_filter)
1959 /* Standard dispatch unless the filter ate the message */
1960 if (!consumed_by_filter)
1962 GDBusMessageType message_type;
1964 message_type = g_dbus_message_get_message_type (message);
1965 if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_RETURN || message_type == G_DBUS_MESSAGE_TYPE_ERROR)
1967 guint32 reply_serial;
1968 SendMessageData *send_message_data;
1970 reply_serial = g_dbus_message_get_reply_serial (message);
1971 CONNECTION_LOCK (connection);
1972 send_message_data = g_hash_table_lookup (connection->map_method_serial_to_send_message_data,
1973 GUINT_TO_POINTER (reply_serial));
1974 if (send_message_data != NULL)
1976 //g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection);
1977 send_message_data_deliver_reply_unlocked (send_message_data, message);
1981 //g_debug ("message reply/error for serial %d but no SendMessageData found for %p", reply_serial, connection);
1983 CONNECTION_UNLOCK (connection);
1985 else if (message_type == G_DBUS_MESSAGE_TYPE_SIGNAL)
1987 CONNECTION_LOCK (connection);
1988 distribute_signals (connection, message);
1989 CONNECTION_UNLOCK (connection);
1991 else if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_CALL)
1993 CONNECTION_LOCK (connection);
1994 distribute_method_call (connection, message);
1995 CONNECTION_UNLOCK (connection);
1999 g_object_unref (connection);
2003 /* Called in worker's thread */
2005 on_worker_message_about_to_be_sent (GDBusWorker *worker,
2006 GDBusMessage *message,
2009 GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
2010 FilterCallback *filters;
2011 gboolean consumed_by_filter;
2015 //g_debug ("in on_worker_message_about_to_be_sent");
2017 g_object_ref (connection);
2019 /* First collect the set of callback functions */
2020 CONNECTION_LOCK (connection);
2021 num_filters = connection->filters->len;
2022 filters = g_new0 (FilterCallback, num_filters);
2023 for (n = 0; n < num_filters; n++)
2025 FilterData *data = connection->filters->pdata[n];
2026 filters[n].func = data->filter_function;
2027 filters[n].user_data = data->user_data;
2029 CONNECTION_UNLOCK (connection);
2031 /* the call the filters in order (without holding the lock) */
2032 consumed_by_filter = FALSE;
2033 for (n = 0; n < num_filters; n++)
2035 consumed_by_filter = filters[n].func (connection,
2038 filters[n].user_data);
2039 if (consumed_by_filter)
2043 g_object_unref (connection);
2046 return consumed_by_filter;
2049 /* Called in worker's thread - we must not block */
2051 on_worker_closed (GDBusWorker *worker,
2052 gboolean remote_peer_vanished,
2056 GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
2058 //g_debug ("in on_worker_closed: %s", error->message);
2060 CONNECTION_LOCK (connection);
2061 if (!connection->closed)
2062 set_closed_unlocked (connection, remote_peer_vanished, error);
2063 CONNECTION_UNLOCK (connection);
2066 /* ---------------------------------------------------------------------------------------------------- */
2068 /* Determines the biggest set of capabilities we can support on this connection */
2069 static GDBusCapabilityFlags
2070 get_offered_capabilities_max (GDBusConnection *connection)
2072 GDBusCapabilityFlags ret;
2073 ret = G_DBUS_CAPABILITY_FLAGS_NONE;
2075 if (G_IS_UNIX_CONNECTION (connection->stream))
2076 ret |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
2082 initable_init (GInitable *initable,
2083 GCancellable *cancellable,
2086 GDBusConnection *connection = G_DBUS_CONNECTION (initable);
2089 /* This method needs to be idempotent to work with the singleton
2090 * pattern. See the docs for g_initable_init(). We implement this by
2093 * Unfortunately we can't use the main lock since the on_worker_*()
2094 * callbacks above needs the lock during initialization (for message
2095 * bus connections we do a synchronous Hello() call on the bus).
2097 g_mutex_lock (connection->init_lock);
2101 if (connection->is_initialized)
2103 if (connection->stream != NULL)
2106 g_assert (connection->initialization_error != NULL);
2109 g_assert (connection->initialization_error == NULL);
2111 /* The user can pass multiple (but mutally exclusive) construct
2114 * - stream (of type GIOStream)
2115 * - address (of type gchar*)
2117 * At the end of the day we end up with a non-NULL GIOStream
2118 * object in connection->stream.
2120 if (connection->address != NULL)
2122 g_assert (connection->stream == NULL);
2124 if ((connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER) ||
2125 (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS))
2127 g_set_error_literal (error,
2129 G_IO_ERROR_INVALID_ARGUMENT,
2130 _("Unsupported flags encountered when constructing a client-side connection"));
2134 connection->stream = g_dbus_address_get_stream_sync (connection->address,
2135 NULL, /* TODO: out_guid */
2137 &connection->initialization_error);
2138 if (connection->stream == NULL)
2141 else if (connection->stream != NULL)
2147 g_assert_not_reached ();
2150 /* Authenticate the connection */
2151 if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER)
2153 g_assert (!(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT));
2154 g_assert (connection->guid != NULL);
2155 connection->auth = _g_dbus_auth_new (connection->stream);
2156 if (!_g_dbus_auth_run_server (connection->auth,
2157 connection->authentication_observer,
2159 (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS),
2160 get_offered_capabilities_max (connection),
2161 &connection->capabilities,
2162 &connection->crendentials,
2164 &connection->initialization_error))
2167 else if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT)
2169 g_assert (!(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER));
2170 g_assert (connection->guid == NULL);
2171 connection->auth = _g_dbus_auth_new (connection->stream);
2172 connection->guid = _g_dbus_auth_run_client (connection->auth,
2173 get_offered_capabilities_max (connection),
2174 &connection->capabilities,
2176 &connection->initialization_error);
2177 if (connection->guid == NULL)
2181 if (connection->authentication_observer != NULL)
2183 g_object_unref (connection->authentication_observer);
2184 connection->authentication_observer = NULL;
2187 //g_output_stream_flush (G_SOCKET_CONNECTION (connection->stream)
2189 //g_debug ("haz unix fd passing powers: %d", connection->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
2194 * https://bugzilla.gnome.org/show_bug.cgi?id=616458
2198 if (G_IS_SOCKET_CONNECTION (connection->stream))
2200 g_socket_set_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection->stream)), FALSE);
2204 connection->worker = _g_dbus_worker_new (connection->stream,
2205 connection->capabilities,
2206 (connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING),
2207 on_worker_message_received,
2208 on_worker_message_about_to_be_sent,
2212 /* if a bus connection, call org.freedesktop.DBus.Hello - this is how we're getting a name */
2213 if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
2215 GVariant *hello_result;
2217 /* we could lift this restriction by adding code in gdbusprivate.c */
2218 if (connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING)
2220 g_set_error_literal (&connection->initialization_error,
2223 "Cannot use DELAY_MESSAGE_PROCESSING with MESSAGE_BUS_CONNECTION");
2227 hello_result = g_dbus_connection_call_sync (connection,
2228 "org.freedesktop.DBus", /* name */
2229 "/org/freedesktop/DBus", /* path */
2230 "org.freedesktop.DBus", /* interface */
2232 NULL, /* parameters */
2233 G_VARIANT_TYPE ("(s)"),
2234 G_DBUS_CALL_FLAGS_NONE,
2236 NULL, /* TODO: cancellable */
2237 &connection->initialization_error);
2238 if (hello_result == NULL)
2241 g_variant_get (hello_result, "(s)", &connection->bus_unique_name);
2242 g_variant_unref (hello_result);
2243 //g_debug ("unique name is `%s'", connection->bus_unique_name);
2246 connection->is_initialized = TRUE;
2252 g_assert (connection->initialization_error != NULL);
2253 g_propagate_error (error, g_error_copy (connection->initialization_error));
2256 g_mutex_unlock (connection->init_lock);
2262 initable_iface_init (GInitableIface *initable_iface)
2264 initable_iface->init = initable_init;
2267 /* ---------------------------------------------------------------------------------------------------- */
2270 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
2275 /* ---------------------------------------------------------------------------------------------------- */
2278 * g_dbus_connection_new:
2279 * @stream: A #GIOStream.
2280 * @guid: The GUID to use if a authenticating as a server or %NULL.
2281 * @flags: Flags describing how to make the connection.
2282 * @observer: A #GDBusAuthObserver or %NULL.
2283 * @cancellable: A #GCancellable or %NULL.
2284 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
2285 * @user_data: The data to pass to @callback.
2287 * Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
2288 * with the end represented by @stream.
2290 * If @observer is not %NULL it may be used to control the
2291 * authentication process.
2293 * When the operation is finished, @callback will be invoked. You can
2294 * then call g_dbus_connection_new_finish() to get the result of the
2297 * This is a asynchronous failable constructor. See
2298 * g_dbus_connection_new_sync() for the synchronous
2304 g_dbus_connection_new (GIOStream *stream,
2306 GDBusConnectionFlags flags,
2307 GDBusAuthObserver *observer,
2308 GCancellable *cancellable,
2309 GAsyncReadyCallback callback,
2312 g_return_if_fail (G_IS_IO_STREAM (stream));
2313 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
2321 "authentication-observer", observer,
2326 * g_dbus_connection_new_finish:
2327 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_new().
2328 * @error: Return location for error or %NULL.
2330 * Finishes an operation started with g_dbus_connection_new().
2332 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2337 g_dbus_connection_new_finish (GAsyncResult *res,
2341 GObject *source_object;
2343 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2344 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2346 source_object = g_async_result_get_source_object (res);
2347 g_assert (source_object != NULL);
2348 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2351 g_object_unref (source_object);
2353 return G_DBUS_CONNECTION (object);
2359 * g_dbus_connection_new_sync:
2360 * @stream: A #GIOStream.
2361 * @guid: The GUID to use if a authenticating as a server or %NULL.
2362 * @flags: Flags describing how to make the connection.
2363 * @observer: A #GDBusAuthObserver or %NULL.
2364 * @cancellable: A #GCancellable or %NULL.
2365 * @error: Return location for error or %NULL.
2367 * Synchronously sets up a D-Bus connection for exchanging D-Bus messages
2368 * with the end represented by @stream.
2370 * If @observer is not %NULL it may be used to control the
2371 * authentication process.
2373 * This is a synchronous failable constructor. See
2374 * g_dbus_connection_new() for the asynchronous version.
2376 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2381 g_dbus_connection_new_sync (GIOStream *stream,
2383 GDBusConnectionFlags flags,
2384 GDBusAuthObserver *observer,
2385 GCancellable *cancellable,
2388 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
2389 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2390 return g_initable_new (G_TYPE_DBUS_CONNECTION,
2396 "authentication-observer", observer,
2400 /* ---------------------------------------------------------------------------------------------------- */
2403 * g_dbus_connection_new_for_address:
2404 * @address: A D-Bus address.
2405 * @flags: Flags describing how to make the connection.
2406 * @observer: A #GDBusAuthObserver or %NULL.
2407 * @cancellable: A #GCancellable or %NULL.
2408 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
2409 * @user_data: The data to pass to @callback.
2411 * Asynchronously connects and sets up a D-Bus client connection for
2412 * exchanging D-Bus messages with an endpoint specified by @address
2413 * which must be in the D-Bus address format.
2415 * This constructor can only be used to initiate client-side
2416 * connections - use g_dbus_connection_new() if you need to act as the
2417 * server. In particular, @flags cannot contain the
2418 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2419 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2421 * When the operation is finished, @callback will be invoked. You can
2422 * then call g_dbus_connection_new_finish() to get the result of the
2425 * If @observer is not %NULL it may be used to control the
2426 * authentication process.
2428 * This is a asynchronous failable constructor. See
2429 * g_dbus_connection_new_for_address_sync() for the synchronous
2435 g_dbus_connection_new_for_address (const gchar *address,
2436 GDBusConnectionFlags flags,
2437 GDBusAuthObserver *observer,
2438 GCancellable *cancellable,
2439 GAsyncReadyCallback callback,
2442 g_return_if_fail (address != NULL);
2443 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
2450 "authentication-observer", observer,
2455 * g_dbus_connection_new_for_address_finish:
2456 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_new().
2457 * @error: Return location for error or %NULL.
2459 * Finishes an operation started with g_dbus_connection_new_for_address().
2461 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2466 g_dbus_connection_new_for_address_finish (GAsyncResult *res,
2470 GObject *source_object;
2472 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2473 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2475 source_object = g_async_result_get_source_object (res);
2476 g_assert (source_object != NULL);
2477 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2480 g_object_unref (source_object);
2482 return G_DBUS_CONNECTION (object);
2488 * g_dbus_connection_new_for_address_sync:
2489 * @address: A D-Bus address.
2490 * @flags: Flags describing how to make the connection.
2491 * @observer: A #GDBusAuthObserver or %NULL.
2492 * @cancellable: A #GCancellable or %NULL.
2493 * @error: Return location for error or %NULL.
2495 * Synchronously connects and sets up a D-Bus client connection for
2496 * exchanging D-Bus messages with an endpoint specified by @address
2497 * which must be in the D-Bus address format.
2499 * This constructor can only be used to initiate client-side
2500 * connections - use g_dbus_connection_new_sync() if you need to act
2501 * as the server. In particular, @flags cannot contain the
2502 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2503 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2505 * This is a synchronous failable constructor. See
2506 * g_dbus_connection_new_for_address() for the asynchronous version.
2508 * If @observer is not %NULL it may be used to control the
2509 * authentication process.
2511 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2516 g_dbus_connection_new_for_address_sync (const gchar *address,
2517 GDBusConnectionFlags flags,
2518 GDBusAuthObserver *observer,
2519 GCancellable *cancellable,
2522 g_return_val_if_fail (address != NULL, NULL);
2523 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2524 return g_initable_new (G_TYPE_DBUS_CONNECTION,
2529 "authentication-observer", observer,
2533 /* ---------------------------------------------------------------------------------------------------- */
2536 * g_dbus_connection_set_exit_on_close:
2537 * @connection: A #GDBusConnection.
2538 * @exit_on_close: Whether the process should be terminated
2539 * when @connection is closed by the remote peer.
2541 * Sets whether the process should be terminated when @connection is
2542 * closed by the remote peer. See #GDBusConnection:exit-on-close for
2548 g_dbus_connection_set_exit_on_close (GDBusConnection *connection,
2549 gboolean exit_on_close)
2551 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2552 connection->exit_on_close = exit_on_close;
2556 * g_dbus_connection_get_exit_on_close:
2557 * @connection: A #GDBusConnection.
2559 * Gets whether the process is terminated when @connection is
2560 * closed by the remote peer. See
2561 * #GDBusConnection:exit-on-close for more details.
2563 * Returns: Whether the process is terminated when @connection is
2564 * closed by the remote peer.
2569 g_dbus_connection_get_exit_on_close (GDBusConnection *connection)
2571 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
2572 return connection->exit_on_close;
2576 * g_dbus_connection_get_guid:
2577 * @connection: A #GDBusConnection.
2579 * The GUID of the peer performing the role of server when
2580 * authenticating. See #GDBusConnection:guid for more details.
2582 * Returns: The GUID. Do not free this string, it is owned by
2588 g_dbus_connection_get_guid (GDBusConnection *connection)
2590 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2591 return connection->guid;
2595 * g_dbus_connection_get_unique_name:
2596 * @connection: A #GDBusConnection.
2598 * Gets the unique name of @connection as assigned by the message
2599 * bus. This can also be used to figure out if @connection is a
2600 * message bus connection.
2602 * Returns: The unique name or %NULL if @connection is not a message
2603 * bus connection. Do not free this string, it is owned by
2609 g_dbus_connection_get_unique_name (GDBusConnection *connection)
2611 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2612 return connection->bus_unique_name;
2616 * g_dbus_connection_get_peer_credentials:
2617 * @connection: A #GDBusConnection.
2619 * Gets the credentials of the authenticated peer. This will always
2620 * return %NULL unless @connection acted as a server
2621 * (e.g. %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER was passed)
2622 * when set up and the client passed credentials as part of the
2623 * authentication process.
2625 * In a message bus setup, the message bus is always the server and
2626 * each application is a client. So this method will always return
2627 * %NULL for message bus clients.
2629 * Returns: A #GCredentials or %NULL if not available. Do not free
2630 * this object, it is owned by @connection.
2635 g_dbus_connection_get_peer_credentials (GDBusConnection *connection)
2637 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2638 return connection->crendentials;
2641 /* ---------------------------------------------------------------------------------------------------- */
2643 static guint _global_filter_id = 1;
2646 * g_dbus_connection_add_filter:
2647 * @connection: A #GDBusConnection.
2648 * @filter_function: A filter function.
2649 * @user_data: User data to pass to @filter_function.
2650 * @user_data_free_func: Function to free @user_data with when filter
2651 * is removed or %NULL.
2653 * Adds a message filter. Filters are handlers that are run on all
2654 * incoming and outgoing messages, prior to standard dispatch. Filters
2655 * are run in the order that they were added. The same handler can be
2656 * added as a filter more than once, in which case it will be run more
2657 * than once. Filters added during a filter callback won't be run on
2658 * the message being processed.
2660 * Note that filters are run in a dedicated message handling thread so
2661 * they can't block and, generally, can't do anything but signal a
2662 * worker thread. Also note that filters are rarely needed - use API
2663 * such as g_dbus_connection_send_message_with_reply(),
2664 * g_dbus_connection_signal_subscribe() or
2665 * g_dbus_connection_call() instead.
2667 * If a filter consumes an incoming message (by returning %TRUE), the
2668 * message is not dispatched anywhere else - not even the standard
2669 * dispatch machinery (that API such as
2670 * g_dbus_connection_signal_subscribe() and
2671 * g_dbus_connection_send_message_with_reply() relies on) will see the
2672 * message. Similary, if a filter consumes an outgoing message, the
2673 * message will not be sent to the other peer.
2675 * Returns: A filter identifier that can be used with
2676 * g_dbus_connection_remove_filter().
2681 g_dbus_connection_add_filter (GDBusConnection *connection,
2682 GDBusMessageFilterFunction filter_function,
2684 GDestroyNotify user_data_free_func)
2688 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
2689 g_return_val_if_fail (filter_function != NULL, 0);
2691 CONNECTION_LOCK (connection);
2692 data = g_new0 (FilterData, 1);
2693 data->id = _global_filter_id++; /* TODO: overflow etc. */
2694 data->filter_function = filter_function;
2695 data->user_data = user_data;
2696 data->user_data_free_func = user_data_free_func;
2697 g_ptr_array_add (connection->filters, data);
2698 CONNECTION_UNLOCK (connection);
2703 /* only called from finalize(), removes all filters */
2705 purge_all_filters (GDBusConnection *connection)
2708 for (n = 0; n < connection->filters->len; n++)
2710 FilterData *data = connection->filters->pdata[n];
2711 if (data->user_data_free_func != NULL)
2712 data->user_data_free_func (data->user_data);
2718 * g_dbus_connection_remove_filter:
2719 * @connection: a #GDBusConnection
2720 * @filter_id: an identifier obtained from g_dbus_connection_add_filter()
2727 g_dbus_connection_remove_filter (GDBusConnection *connection,
2731 FilterData *to_destroy;
2733 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2735 CONNECTION_LOCK (connection);
2737 for (n = 0; n < connection->filters->len; n++)
2739 FilterData *data = connection->filters->pdata[n];
2740 if (data->id == filter_id)
2742 g_ptr_array_remove_index (connection->filters, n);
2747 CONNECTION_UNLOCK (connection);
2749 /* do free without holding lock */
2750 if (to_destroy != NULL)
2752 if (to_destroy->user_data_free_func != NULL)
2753 to_destroy->user_data_free_func (to_destroy->user_data);
2754 g_free (to_destroy);
2758 g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id);
2762 /* ---------------------------------------------------------------------------------------------------- */
2768 gchar *sender_unique_name; /* if sender is unique or org.freedesktop.DBus, then that name... otherwise blank */
2769 gchar *interface_name;
2773 GArray *subscribers;
2778 GDBusSignalCallback callback;
2780 GDestroyNotify user_data_free_func;
2782 GMainContext *context;
2786 signal_data_free (SignalData *signal_data)
2788 g_free (signal_data->rule);
2789 g_free (signal_data->sender);
2790 g_free (signal_data->sender_unique_name);
2791 g_free (signal_data->interface_name);
2792 g_free (signal_data->member);
2793 g_free (signal_data->object_path);
2794 g_free (signal_data->arg0);
2795 g_array_free (signal_data->subscribers, TRUE);
2796 g_free (signal_data);
2800 args_to_rule (const gchar *sender,
2801 const gchar *interface_name,
2802 const gchar *member,
2803 const gchar *object_path,
2808 rule = g_string_new ("type='signal'");
2810 g_string_append_printf (rule, ",sender='%s'", sender);
2811 if (interface_name != NULL)
2812 g_string_append_printf (rule, ",interface='%s'", interface_name);
2814 g_string_append_printf (rule, ",member='%s'", member);
2815 if (object_path != NULL)
2816 g_string_append_printf (rule, ",path='%s'", object_path);
2818 g_string_append_printf (rule, ",arg0='%s'", arg0);
2820 return g_string_free (rule, FALSE);
2823 static guint _global_subscriber_id = 1;
2824 static guint _global_registration_id = 1;
2825 static guint _global_subtree_registration_id = 1;
2827 /* ---------------------------------------------------------------------------------------------------- */
2829 /* must hold lock when calling */
2831 add_match_rule (GDBusConnection *connection,
2832 const gchar *match_rule)
2835 GDBusMessage *message;
2837 message = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
2838 "/org/freedesktop/DBus", /* path */
2839 "org.freedesktop.DBus", /* interface */
2841 g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule));
2843 if (!g_dbus_connection_send_message_unlocked (connection,
2848 g_critical ("Error while sending AddMatch() message: %s", error->message);
2849 g_error_free (error);
2851 g_object_unref (message);
2854 /* ---------------------------------------------------------------------------------------------------- */
2856 /* must hold lock when calling */
2858 remove_match_rule (GDBusConnection *connection,
2859 const gchar *match_rule)
2862 GDBusMessage *message;
2864 message = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
2865 "/org/freedesktop/DBus", /* path */
2866 "org.freedesktop.DBus", /* interface */
2868 g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule));
2871 if (!g_dbus_connection_send_message_unlocked (connection,
2876 g_critical ("Error while sending RemoveMatch() message: %s", error->message);
2877 g_error_free (error);
2879 g_object_unref (message);
2882 /* ---------------------------------------------------------------------------------------------------- */
2885 is_signal_data_for_name_lost_or_acquired (SignalData *signal_data)
2887 return g_strcmp0 (signal_data->sender_unique_name, "org.freedesktop.DBus") == 0 &&
2888 g_strcmp0 (signal_data->interface_name, "org.freedesktop.DBus") == 0 &&
2889 g_strcmp0 (signal_data->object_path, "/org/freedesktop/DBus") == 0 &&
2890 (g_strcmp0 (signal_data->member, "NameLost") == 0 ||
2891 g_strcmp0 (signal_data->member, "NameAcquired") == 0);
2894 /* ---------------------------------------------------------------------------------------------------- */
2897 * g_dbus_connection_signal_subscribe:
2898 * @connection: A #GDBusConnection.
2899 * @sender: Sender name to match on (unique or well-known name) or %NULL to listen from all senders.
2900 * @interface_name: D-Bus interface name to match on or %NULL to match on all interfaces.
2901 * @member: D-Bus signal name to match on or %NULL to match on all signals.
2902 * @object_path: Object path to match on or %NULL to match on all object paths.
2903 * @arg0: Contents of first string argument to match on or %NULL to match on all kinds of arguments.
2904 * @callback: Callback to invoke when there is a signal matching the requested data.
2905 * @user_data: User data to pass to @callback.
2906 * @user_data_free_func: Function to free @user_data with when subscription is removed or %NULL.
2908 * Subscribes to signals on @connection and invokes @callback with a
2909 * whenever the signal is received. Note that @callback
2910 * will be invoked in the <link
2911 * linkend="g-main-context-push-thread-default">thread-default main
2912 * loop</link> of the thread you are calling this method from.
2914 * If @connection is not a message bus connection, @sender must be
2917 * If @sender is a well-known name note that @callback is invoked with
2918 * the unique name for the owner of @sender, not the well-known name
2919 * as one would expect. This is because the message bus rewrites the
2920 * name. As such, to avoid certain race conditions, users should be
2921 * tracking the name owner of the well-known name and use that when
2922 * processing the received signal.
2924 * Returns: A subscription identifier that can be used with g_dbus_connection_signal_unsubscribe().
2929 g_dbus_connection_signal_subscribe (GDBusConnection *connection,
2930 const gchar *sender,
2931 const gchar *interface_name,
2932 const gchar *member,
2933 const gchar *object_path,
2935 GDBusSignalCallback callback,
2937 GDestroyNotify user_data_free_func)
2940 SignalData *signal_data;
2941 SignalSubscriber subscriber;
2942 GPtrArray *signal_data_array;
2943 const gchar *sender_unique_name;
2945 /* Right now we abort if AddMatch() fails since it can only fail with the bus being in
2946 * an OOM condition. We might want to change that but that would involve making
2947 * g_dbus_connection_signal_subscribe() asynchronous and having the call sites
2948 * handle that. And there's really no sensible way of handling this short of retrying
2949 * to add the match rule... and then there's the little thing that, hey, maybe there's
2950 * a reason the bus in an OOM condition.
2952 * Doable, but not really sure it's worth it...
2955 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
2956 g_return_val_if_fail (sender == NULL || (g_dbus_is_name (sender) && (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)), 0);
2957 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), 0);
2958 g_return_val_if_fail (member == NULL || g_dbus_is_member_name (member), 0);
2959 g_return_val_if_fail (object_path == NULL || g_variant_is_object_path (object_path), 0);
2960 g_return_val_if_fail (callback != NULL, 0);
2962 CONNECTION_LOCK (connection);
2964 rule = args_to_rule (sender, interface_name, member, object_path, arg0);
2966 if (sender != NULL && (g_dbus_is_unique_name (sender) || g_strcmp0 (sender, "org.freedesktop.DBus") == 0))
2967 sender_unique_name = sender;
2969 sender_unique_name = "";
2971 subscriber.callback = callback;
2972 subscriber.user_data = user_data;
2973 subscriber.user_data_free_func = user_data_free_func;
2974 subscriber.id = _global_subscriber_id++; /* TODO: overflow etc. */
2975 subscriber.context = g_main_context_get_thread_default ();
2976 if (subscriber.context != NULL)
2977 g_main_context_ref (subscriber.context);
2979 /* see if we've already have this rule */
2980 signal_data = g_hash_table_lookup (connection->map_rule_to_signal_data, rule);
2981 if (signal_data != NULL)
2983 g_array_append_val (signal_data->subscribers, subscriber);
2988 signal_data = g_new0 (SignalData, 1);
2989 signal_data->rule = rule;
2990 signal_data->sender = g_strdup (sender);
2991 signal_data->sender_unique_name = g_strdup (sender_unique_name);
2992 signal_data->interface_name = g_strdup (interface_name);
2993 signal_data->member = g_strdup (member);
2994 signal_data->object_path = g_strdup (object_path);
2995 signal_data->arg0 = g_strdup (arg0);
2996 signal_data->subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber));
2997 g_array_append_val (signal_data->subscribers, subscriber);
2999 g_hash_table_insert (connection->map_rule_to_signal_data,
3003 /* Add the match rule to the bus...
3005 * Avoid adding match rules for NameLost and NameAcquired messages - the bus will
3006 * always send such messages to us.
3008 if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
3010 if (!is_signal_data_for_name_lost_or_acquired (signal_data))
3011 add_match_rule (connection, signal_data->rule);
3014 signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
3015 signal_data->sender_unique_name);
3016 if (signal_data_array == NULL)
3018 signal_data_array = g_ptr_array_new ();
3019 g_hash_table_insert (connection->map_sender_unique_name_to_signal_data_array,
3020 g_strdup (signal_data->sender_unique_name),
3023 g_ptr_array_add (signal_data_array, signal_data);
3026 g_hash_table_insert (connection->map_id_to_signal_data,
3027 GUINT_TO_POINTER (subscriber.id),
3030 CONNECTION_UNLOCK (connection);
3032 return subscriber.id;
3035 /* ---------------------------------------------------------------------------------------------------- */
3037 /* must hold lock when calling this */
3039 unsubscribe_id_internal (GDBusConnection *connection,
3040 guint subscription_id,
3041 GArray *out_removed_subscribers)
3043 SignalData *signal_data;
3044 GPtrArray *signal_data_array;
3047 signal_data = g_hash_table_lookup (connection->map_id_to_signal_data,
3048 GUINT_TO_POINTER (subscription_id));
3049 if (signal_data == NULL)
3051 /* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
3055 for (n = 0; n < signal_data->subscribers->len; n++)
3057 SignalSubscriber *subscriber;
3059 subscriber = &(g_array_index (signal_data->subscribers, SignalSubscriber, n));
3060 if (subscriber->id != subscription_id)
3063 g_warn_if_fail (g_hash_table_remove (connection->map_id_to_signal_data,
3064 GUINT_TO_POINTER (subscription_id)));
3065 g_array_append_val (out_removed_subscribers, *subscriber);
3066 g_array_remove_index (signal_data->subscribers, n);
3068 if (signal_data->subscribers->len == 0)
3070 g_warn_if_fail (g_hash_table_remove (connection->map_rule_to_signal_data, signal_data->rule));
3072 signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
3073 signal_data->sender_unique_name);
3074 g_warn_if_fail (signal_data_array != NULL);
3075 g_warn_if_fail (g_ptr_array_remove (signal_data_array, signal_data));
3077 if (signal_data_array->len == 0)
3079 g_warn_if_fail (g_hash_table_remove (connection->map_sender_unique_name_to_signal_data_array,
3080 signal_data->sender_unique_name));
3083 /* remove the match rule from the bus unless NameLost or NameAcquired (see subscribe()) */
3084 if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
3086 if (!is_signal_data_for_name_lost_or_acquired (signal_data))
3087 if (!connection->closed)
3088 remove_match_rule (connection, signal_data->rule);
3090 signal_data_free (signal_data);
3096 g_assert_not_reached ();
3103 * g_dbus_connection_signal_unsubscribe:
3104 * @connection: A #GDBusConnection.
3105 * @subscription_id: A subscription id obtained from g_dbus_connection_signal_subscribe().
3107 * Unsubscribes from signals.
3112 g_dbus_connection_signal_unsubscribe (GDBusConnection *connection,
3113 guint subscription_id)
3115 GArray *subscribers;
3118 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
3120 subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber));
3122 CONNECTION_LOCK (connection);
3123 unsubscribe_id_internal (connection,
3126 CONNECTION_UNLOCK (connection);
3129 g_assert (subscribers->len == 0 || subscribers->len == 1);
3131 /* call GDestroyNotify without lock held */
3132 for (n = 0; n < subscribers->len; n++)
3134 SignalSubscriber *subscriber;
3135 subscriber = &(g_array_index (subscribers, SignalSubscriber, n));
3136 if (subscriber->user_data_free_func != NULL)
3137 subscriber->user_data_free_func (subscriber->user_data);
3138 if (subscriber->context != NULL)
3139 g_main_context_unref (subscriber->context);
3142 g_array_free (subscribers, TRUE);
3145 /* ---------------------------------------------------------------------------------------------------- */
3149 guint subscription_id;
3150 GDBusSignalCallback callback;
3152 GDBusMessage *message;
3153 GDBusConnection *connection;
3154 const gchar *sender;
3156 const gchar *interface;
3157 const gchar *member;
3160 /* called on delivery thread (e.g. where g_dbus_connection_signal_subscribe() was called) with
3164 emit_signal_instance_in_idle_cb (gpointer data)
3166 SignalInstance *signal_instance = data;
3167 GVariant *parameters;
3168 gboolean has_subscription;
3170 parameters = g_dbus_message_get_body (signal_instance->message);
3171 if (parameters == NULL)
3173 parameters = g_variant_new ("()");
3174 g_variant_ref_sink (parameters);
3178 g_variant_ref_sink (parameters);
3182 g_print ("in emit_signal_instance_in_idle_cb (id=%d sender=%s path=%s interface=%s member=%s params=%s)\n",
3183 signal_instance->subscription_id,
3184 signal_instance->sender,
3185 signal_instance->path,
3186 signal_instance->interface,
3187 signal_instance->member,
3188 g_variant_print (parameters, TRUE));
3191 /* Careful here, don't do the callback if we no longer has the subscription */
3192 CONNECTION_LOCK (signal_instance->connection);
3193 has_subscription = FALSE;
3194 if (g_hash_table_lookup (signal_instance->connection->map_id_to_signal_data,
3195 GUINT_TO_POINTER (signal_instance->subscription_id)) != NULL)
3196 has_subscription = TRUE;
3197 CONNECTION_UNLOCK (signal_instance->connection);
3199 if (has_subscription)
3200 signal_instance->callback (signal_instance->connection,
3201 signal_instance->sender,
3202 signal_instance->path,
3203 signal_instance->interface,
3204 signal_instance->member,
3206 signal_instance->user_data);
3208 if (parameters != NULL)
3209 g_variant_unref (parameters);
3215 signal_instance_free (SignalInstance *signal_instance)
3217 g_object_unref (signal_instance->message);
3218 g_object_unref (signal_instance->connection);
3219 g_free (signal_instance);
3222 /* called in message handler thread WITH lock held */
3224 schedule_callbacks (GDBusConnection *connection,
3225 GPtrArray *signal_data_array,
3226 GDBusMessage *message,
3227 const gchar *sender)
3230 const gchar *interface;
3231 const gchar *member;
3240 interface = g_dbus_message_get_interface (message);
3241 member = g_dbus_message_get_member (message);
3242 path = g_dbus_message_get_path (message);
3243 arg0 = g_dbus_message_get_arg0 (message);
3246 g_print ("In schedule_callbacks:\n"
3248 " interface = `%s'\n"
3259 /* TODO: if this is slow, then we can change signal_data_array into
3260 * map_object_path_to_signal_data_array or something.
3262 for (n = 0; n < signal_data_array->len; n++)
3264 SignalData *signal_data = signal_data_array->pdata[n];
3266 if (signal_data->interface_name != NULL && g_strcmp0 (signal_data->interface_name, interface) != 0)
3269 if (signal_data->member != NULL && g_strcmp0 (signal_data->member, member) != 0)
3272 if (signal_data->object_path != NULL && g_strcmp0 (signal_data->object_path, path) != 0)
3275 if (signal_data->arg0 != NULL && g_strcmp0 (signal_data->arg0, arg0) != 0)
3278 for (m = 0; m < signal_data->subscribers->len; m++)
3280 SignalSubscriber *subscriber;
3281 GSource *idle_source;
3282 SignalInstance *signal_instance;
3284 subscriber = &(g_array_index (signal_data->subscribers, SignalSubscriber, m));
3286 signal_instance = g_new0 (SignalInstance, 1);
3287 signal_instance->subscription_id = subscriber->id;
3288 signal_instance->callback = subscriber->callback;
3289 signal_instance->user_data = subscriber->user_data;
3290 signal_instance->message = g_object_ref (message);
3291 signal_instance->connection = g_object_ref (connection);
3292 signal_instance->sender = sender;
3293 signal_instance->path = path;
3294 signal_instance->interface = interface;
3295 signal_instance->member = member;
3297 idle_source = g_idle_source_new ();
3298 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
3299 g_source_set_callback (idle_source,
3300 emit_signal_instance_in_idle_cb,
3302 (GDestroyNotify) signal_instance_free);
3303 g_source_attach (idle_source, subscriber->context);
3304 g_source_unref (idle_source);
3309 /* called in message handler thread with lock held */
3311 distribute_signals (GDBusConnection *connection,
3312 GDBusMessage *message)
3314 GPtrArray *signal_data_array;
3315 const gchar *sender;
3317 sender = g_dbus_message_get_sender (message);
3319 if (G_UNLIKELY (_g_dbus_debug_signal ()))
3321 _g_dbus_debug_print_lock ();
3322 g_print ("========================================================================\n"
3323 "GDBus-debug:Signal:\n"
3324 " >>>> SIGNAL %s.%s\n"
3326 " sent by name %s\n",
3327 g_dbus_message_get_interface (message),
3328 g_dbus_message_get_member (message),
3329 g_dbus_message_get_path (message),
3330 sender != NULL ? sender : "(none)");
3331 _g_dbus_debug_print_unlock ();
3334 /* collect subscribers that match on sender */
3337 signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, sender);
3338 if (signal_data_array != NULL)
3339 schedule_callbacks (connection, signal_data_array, message, sender);
3342 /* collect subscribers not matching on sender */
3343 signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, "");
3344 if (signal_data_array != NULL)
3345 schedule_callbacks (connection, signal_data_array, message, sender);
3348 /* ---------------------------------------------------------------------------------------------------- */
3350 /* only called from finalize(), removes all subscriptions */
3352 purge_all_signal_subscriptions (GDBusConnection *connection)
3354 GHashTableIter iter;
3357 GArray *subscribers;
3360 ids = g_array_new (FALSE, FALSE, sizeof (guint));
3361 g_hash_table_iter_init (&iter, connection->map_id_to_signal_data);
3362 while (g_hash_table_iter_next (&iter, &key, NULL))
3364 guint subscription_id = GPOINTER_TO_UINT (key);
3365 g_array_append_val (ids, subscription_id);
3368 subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber));
3369 for (n = 0; n < ids->len; n++)
3371 guint subscription_id = g_array_index (ids, guint, n);
3372 unsubscribe_id_internal (connection,
3376 g_array_free (ids, TRUE);
3378 /* call GDestroyNotify without lock held */
3379 for (n = 0; n < subscribers->len; n++)
3381 SignalSubscriber *subscriber;
3382 subscriber = &(g_array_index (subscribers, SignalSubscriber, n));
3383 if (subscriber->user_data_free_func != NULL)
3384 subscriber->user_data_free_func (subscriber->user_data);
3385 if (subscriber->context != NULL)
3386 g_main_context_unref (subscriber->context);
3389 g_array_free (subscribers, TRUE);
3392 /* ---------------------------------------------------------------------------------------------------- */
3394 struct ExportedObject
3397 GDBusConnection *connection;
3399 /* maps gchar* -> ExportedInterface* */
3400 GHashTable *map_if_name_to_ei;
3403 /* only called with lock held */
3405 exported_object_free (ExportedObject *eo)
3407 g_free (eo->object_path);
3408 g_hash_table_unref (eo->map_if_name_to_ei);
3417 gchar *interface_name;
3418 const GDBusInterfaceVTable *vtable;
3419 const GDBusInterfaceInfo *interface_info;
3421 GMainContext *context;
3423 GDestroyNotify user_data_free_func;
3424 } ExportedInterface;
3426 /* called with lock held */
3428 exported_interface_free (ExportedInterface *ei)
3430 g_dbus_interface_info_unref ((GDBusInterfaceInfo *) ei->interface_info);
3432 if (ei->user_data_free_func != NULL)
3433 /* TODO: push to thread-default mainloop */
3434 ei->user_data_free_func (ei->user_data);
3436 if (ei->context != NULL)
3437 g_main_context_unref (ei->context);
3439 g_free (ei->interface_name);
3443 /* ---------------------------------------------------------------------------------------------------- */
3445 /* Convenience function to check if @registration_id (if not zero) or
3446 * @subtree_registration_id (if not zero) has been unregistered. If
3447 * so, returns %TRUE.
3449 * Caller must *not* hold lock.
3452 has_object_been_unregistered (GDBusConnection *connection,
3453 guint registration_id,
3454 guint subtree_registration_id)
3458 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
3462 CONNECTION_LOCK (connection);
3463 if (registration_id != 0 && g_hash_table_lookup (connection->map_id_to_ei,
3464 GUINT_TO_POINTER (registration_id)) == NULL)
3468 else if (subtree_registration_id != 0 && g_hash_table_lookup (connection->map_id_to_es,
3469 GUINT_TO_POINTER (subtree_registration_id)) == NULL)
3473 CONNECTION_UNLOCK (connection);
3478 /* ---------------------------------------------------------------------------------------------------- */
3482 GDBusConnection *connection;
3483 GDBusMessage *message;
3485 const gchar *property_name;
3486 const GDBusInterfaceVTable *vtable;
3487 const GDBusInterfaceInfo *interface_info;
3488 const GDBusPropertyInfo *property_info;
3489 guint registration_id;
3490 guint subtree_registration_id;
3494 property_data_free (PropertyData *data)
3496 g_object_unref (data->connection);
3497 g_object_unref (data->message);
3501 /* called in thread where object was registered - no locks held */
3503 invoke_get_property_in_idle_cb (gpointer _data)
3505 PropertyData *data = _data;
3508 GDBusMessage *reply;
3510 if (has_object_been_unregistered (data->connection,
3511 data->registration_id,
3512 data->subtree_registration_id))
3514 reply = g_dbus_message_new_method_error (data->message,
3515 "org.freedesktop.DBus.Error.UnknownMethod",
3516 _("No such interface `org.freedesktop.DBus.Properties' on object at path %s"),
3517 g_dbus_message_get_path (data->message));
3518 g_dbus_connection_send_message (data->connection, reply, NULL, NULL);
3519 g_object_unref (reply);
3524 value = data->vtable->get_property (data->connection,
3525 g_dbus_message_get_sender (data->message),
3526 g_dbus_message_get_path (data->message),
3527 data->interface_info->name,
3528 data->property_name,
3535 g_assert_no_error (error);
3537 g_variant_ref_sink (value);
3538 reply = g_dbus_message_new_method_reply (data->message);
3539 g_dbus_message_set_body (reply, g_variant_new ("(v)", value));
3540 g_dbus_connection_send_message (data->connection, reply, NULL, NULL);
3541 g_variant_unref (value);
3542 g_object_unref (reply);
3546 gchar *dbus_error_name;
3547 g_assert (error != NULL);
3548 dbus_error_name = g_dbus_error_encode_gerror (error);
3549 reply = g_dbus_message_new_method_error_literal (data->message,
3552 g_dbus_connection_send_message (data->connection, reply, NULL, NULL);
3553 g_free (dbus_error_name);
3554 g_error_free (error);
3555 g_object_unref (reply);
3562 /* called in thread where object was registered - no locks held */
3564 invoke_set_property_in_idle_cb (gpointer _data)
3566 PropertyData *data = _data;
3568 GDBusMessage *reply;
3574 g_variant_get (g_dbus_message_get_body (data->message),
3580 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
3581 * of the given value is wrong
3583 if (g_strcmp0 (g_variant_get_type_string (value), data->property_info->signature) != 0)
3585 reply = g_dbus_message_new_method_error (data->message,
3586 "org.freedesktop.DBus.Error.InvalidArgs",
3587 _("Error setting property `%s': Expected type `%s' but got `%s'"),
3588 data->property_info->name,
3589 data->property_info->signature,
3590 g_variant_get_type_string (value));
3594 if (!data->vtable->set_property (data->connection,
3595 g_dbus_message_get_sender (data->message),
3596 g_dbus_message_get_path (data->message),
3597 data->interface_info->name,
3598 data->property_name,
3603 gchar *dbus_error_name;
3604 g_assert (error != NULL);
3605 dbus_error_name = g_dbus_error_encode_gerror (error);
3606 reply = g_dbus_message_new_method_error_literal (data->message,
3609 g_free (dbus_error_name);
3610 g_error_free (error);
3614 reply = g_dbus_message_new_method_reply (data->message);
3618 g_assert (reply != NULL);
3619 g_dbus_connection_send_message (data->connection, reply, NULL, NULL);
3620 g_object_unref (reply);
3625 /* called with lock held */
3627 validate_and_maybe_schedule_property_getset (GDBusConnection *connection,
3628 GDBusMessage *message,
3629 guint registration_id,
3630 guint subtree_registration_id,
3632 const GDBusInterfaceInfo *interface_info,
3633 const GDBusInterfaceVTable *vtable,
3634 GMainContext *main_context,
3638 const char *interface_name;
3639 const char *property_name;
3640 const GDBusPropertyInfo *property_info;
3641 GSource *idle_source;
3642 PropertyData *property_data;
3643 GDBusMessage *reply;
3648 g_variant_get (g_dbus_message_get_body (message),
3653 g_variant_get (g_dbus_message_get_body (message),
3662 if (vtable == NULL || vtable->get_property == NULL)
3667 if (vtable == NULL || vtable->set_property == NULL)
3671 /* Check that the property exists - if not fail with org.freedesktop.DBus.Error.InvalidArgs
3673 property_info = NULL;
3675 /* TODO: the cost of this is O(n) - it might be worth caching the result */
3676 property_info = g_dbus_interface_info_lookup_property (interface_info, property_name);
3677 if (property_info == NULL)
3679 reply = g_dbus_message_new_method_error (message,
3680 "org.freedesktop.DBus.Error.InvalidArgs",
3681 _("No such property `%s'"),
3683 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
3684 g_object_unref (reply);
3689 if (is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
3691 reply = g_dbus_message_new_method_error (message,
3692 "org.freedesktop.DBus.Error.InvalidArgs",
3693 _("Property `%s' is not readable"),
3695 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
3696 g_object_unref (reply);
3700 else if (!is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
3702 reply = g_dbus_message_new_method_error (message,
3703 "org.freedesktop.DBus.Error.InvalidArgs",
3704 _("Property `%s' is not writable"),
3706 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
3707 g_object_unref (reply);
3712 /* ok, got the property info - call user code in an idle handler */
3713 property_data = g_new0 (PropertyData, 1);
3714 property_data->connection = g_object_ref (connection);
3715 property_data->message = g_object_ref (message);
3716 property_data->user_data = user_data;
3717 property_data->property_name = property_name;
3718 property_data->vtable = vtable;
3719 property_data->interface_info = interface_info;
3720 property_data->property_info = property_info;
3721 property_data->registration_id = registration_id;
3722 property_data->subtree_registration_id = subtree_registration_id;
3724 idle_source = g_idle_source_new ();
3725 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
3726 g_source_set_callback (idle_source,
3727 is_get ? invoke_get_property_in_idle_cb : invoke_set_property_in_idle_cb,
3729 (GDestroyNotify) property_data_free);
3730 g_source_attach (idle_source, main_context);
3731 g_source_unref (idle_source);
3739 /* called with lock held */
3741 handle_getset_property (GDBusConnection *connection,
3743 GDBusMessage *message,
3746 ExportedInterface *ei;
3748 const char *interface_name;
3749 const char *property_name;
3754 g_variant_get (g_dbus_message_get_body (message),
3759 g_variant_get (g_dbus_message_get_body (message),
3765 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
3766 * no such interface registered
3768 ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
3771 GDBusMessage *reply;
3772 reply = g_dbus_message_new_method_error (message,
3773 "org.freedesktop.DBus.Error.InvalidArgs",
3774 _("No such interface `%s'"),
3776 g_dbus_connection_send_message_unlocked (eo->connection, reply, NULL, NULL);
3777 g_object_unref (reply);
3782 handled = validate_and_maybe_schedule_property_getset (eo->connection,
3795 /* ---------------------------------------------------------------------------------------------------- */
3799 GDBusConnection *connection;
3800 GDBusMessage *message;
3802 const GDBusInterfaceVTable *vtable;
3803 const GDBusInterfaceInfo *interface_info;
3804 guint registration_id;
3805 guint subtree_registration_id;
3806 } PropertyGetAllData;
3809 property_get_all_data_free (PropertyData *data)
3811 g_object_unref (data->connection);
3812 g_object_unref (data->message);
3816 /* called in thread where object was registered - no locks held */
3818 invoke_get_all_properties_in_idle_cb (gpointer _data)
3820 PropertyGetAllData *data = _data;
3821 GVariantBuilder builder;
3823 GDBusMessage *reply;
3826 if (has_object_been_unregistered (data->connection,
3827 data->registration_id,
3828 data->subtree_registration_id))
3830 reply = g_dbus_message_new_method_error (data->message,
3831 "org.freedesktop.DBus.Error.UnknownMethod",
3832 _("No such interface `org.freedesktop.DBus.Properties' on object at path %s"),
3833 g_dbus_message_get_path (data->message));
3834 g_dbus_connection_send_message (data->connection, reply, NULL, NULL);
3835 g_object_unref (reply);
3841 /* TODO: Right now we never fail this call - we just omit values if
3842 * a get_property() call is failing.
3844 * We could fail the whole call if just a single get_property() call
3845 * returns an error. We need clarification in the D-Bus spec about this.
3847 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a{sv})"));
3848 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
3849 for (n = 0; data->interface_info->properties != NULL && data->interface_info->properties[n] != NULL; n++)
3851 const GDBusPropertyInfo *property_info = data->interface_info->properties[n];
3854 if (!(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
3857 value = data->vtable->get_property (data->connection,
3858 g_dbus_message_get_sender (data->message),
3859 g_dbus_message_get_path (data->message),
3860 data->interface_info->name,
3861 property_info->name,
3868 g_variant_builder_add (&builder,
3870 property_info->name,
3873 g_variant_builder_close (&builder);
3875 reply = g_dbus_message_new_method_reply (data->message);
3876 g_dbus_message_set_body (reply, g_variant_builder_end (&builder));
3877 g_dbus_connection_send_message (data->connection, reply, NULL, NULL);
3878 g_object_unref (reply);
3884 /* called with lock held */
3886 validate_and_maybe_schedule_property_get_all (GDBusConnection *connection,
3887 GDBusMessage *message,
3888 guint registration_id,
3889 guint subtree_registration_id,
3890 const GDBusInterfaceInfo *interface_info,
3891 const GDBusInterfaceVTable *vtable,
3892 GMainContext *main_context,
3896 const char *interface_name;
3897 GSource *idle_source;
3898 PropertyGetAllData *property_get_all_data;
3902 g_variant_get (g_dbus_message_get_body (message),
3906 if (vtable == NULL || vtable->get_property == NULL)
3909 /* ok, got the property info - call user in an idle handler */
3910 property_get_all_data = g_new0 (PropertyGetAllData, 1);
3911 property_get_all_data->connection = g_object_ref (connection);
3912 property_get_all_data->message = g_object_ref (message);
3913 property_get_all_data->user_data = user_data;
3914 property_get_all_data->vtable = vtable;
3915 property_get_all_data->interface_info = interface_info;
3916 property_get_all_data->registration_id = registration_id;
3917 property_get_all_data->subtree_registration_id = subtree_registration_id;
3919 idle_source = g_idle_source_new ();
3920 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
3921 g_source_set_callback (idle_source,
3922 invoke_get_all_properties_in_idle_cb,
3923 property_get_all_data,
3924 (GDestroyNotify) property_get_all_data_free);
3925 g_source_attach (idle_source, main_context);
3926 g_source_unref (idle_source);
3934 /* called with lock held */
3936 handle_get_all_properties (GDBusConnection *connection,
3938 GDBusMessage *message)
3940 ExportedInterface *ei;
3942 const char *interface_name;
3946 g_variant_get (g_dbus_message_get_body (message),
3950 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
3951 * no such interface registered
3953 ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
3956 GDBusMessage *reply;
3957 reply = g_dbus_message_new_method_error (message,
3958 "org.freedesktop.DBus.Error.InvalidArgs",
3959 _("No such interface"),
3961 g_dbus_connection_send_message_unlocked (eo->connection, reply, NULL, NULL);
3962 g_object_unref (reply);
3967 handled = validate_and_maybe_schedule_property_get_all (eo->connection,
3979 /* ---------------------------------------------------------------------------------------------------- */
3981 static const gchar introspect_header[] =
3982 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
3983 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
3984 "<!-- GDBus " PACKAGE_VERSION " -->\n"
3987 static const gchar introspect_tail[] =
3990 static const gchar introspect_standard_interfaces[] =
3991 " <interface name=\"org.freedesktop.DBus.Properties\">\n"
3992 " <method name=\"Get\">\n"
3993 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
3994 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
3995 " <arg type=\"v\" name=\"value\" direction=\"out\"/>\n"
3997 " <method name=\"GetAll\">\n"
3998 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
3999 " <arg type=\"a{sv}\" name=\"properties\" direction=\"out\"/>\n"
4001 " <method name=\"Set\">\n"
4002 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4003 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4004 " <arg type=\"v\" name=\"value\" direction=\"in\"/>\n"
4006 " <signal name=\"PropertiesChanged\">\n"
4007 " <arg type=\"s\" name=\"interface_name\"/>\n"
4008 " <arg type=\"a{sv}\" name=\"changed_properties\"/>\n"
4009 " <arg type=\"as\" name=\"invalidated_properties\"/>\n"
4012 " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
4013 " <method name=\"Introspect\">\n"
4014 " <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>\n"
4017 " <interface name=\"org.freedesktop.DBus.Peer\">\n"
4018 " <method name=\"Ping\"/>\n"
4019 " <method name=\"GetMachineId\">\n"
4020 " <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n"
4025 introspect_append_header (GString *s)
4027 g_string_append (s, introspect_header);
4031 introspect_append_standard_interfaces (GString *s)
4033 g_string_append (s, introspect_standard_interfaces);
4037 maybe_add_path (const gchar *path, gsize path_len, const gchar *object_path, GHashTable *set)
4039 if (g_str_has_prefix (object_path, path) && strlen (object_path) > path_len && object_path[path_len-1] == '/')
4045 begin = object_path + path_len;
4046 end = strchr (begin, '/');
4048 s = g_strndup (begin, end - begin);
4050 s = g_strdup (begin);
4052 if (g_hash_table_lookup (set, s) == NULL)
4053 g_hash_table_insert (set, s, GUINT_TO_POINTER (1));
4059 /* TODO: we want a nicer public interface for this */
4061 g_dbus_connection_list_registered_unlocked (GDBusConnection *connection,
4066 GHashTableIter hash_iter;
4067 const gchar *object_path;
4073 CONNECTION_ENSURE_LOCK (connection);
4075 path_len = strlen (path);
4079 set = g_hash_table_new (g_str_hash, g_str_equal);
4081 g_hash_table_iter_init (&hash_iter, connection->map_object_path_to_eo);
4082 while (g_hash_table_iter_next (&hash_iter, (gpointer) &object_path, NULL))
4083 maybe_add_path (path, path_len, object_path, set);
4085 g_hash_table_iter_init (&hash_iter, connection->map_object_path_to_es);
4086 while (g_hash_table_iter_next (&hash_iter, (gpointer) &object_path, NULL))
4087 maybe_add_path (path, path_len, object_path, set);
4089 p = g_ptr_array_new ();
4090 keys = g_hash_table_get_keys (set);
4091 for (l = keys; l != NULL; l = l->next)
4092 g_ptr_array_add (p, l->data);
4093 g_hash_table_unref (set);
4096 g_ptr_array_add (p, NULL);
4097 ret = (gchar **) g_ptr_array_free (p, FALSE);
4102 g_dbus_connection_list_registered (GDBusConnection *connection,
4106 CONNECTION_LOCK (connection);
4107 ret = g_dbus_connection_list_registered_unlocked (connection, path);
4108 CONNECTION_UNLOCK (connection);
4112 /* called in message handler thread with lock held */
4114 handle_introspect (GDBusConnection *connection,
4116 GDBusMessage *message)
4120 GDBusMessage *reply;
4121 GHashTableIter hash_iter;
4122 ExportedInterface *ei;
4125 /* first the header with the standard interfaces */
4126 s = g_string_sized_new (sizeof (introspect_header) +
4127 sizeof (introspect_standard_interfaces) +
4128 sizeof (introspect_tail));
4129 introspect_append_header (s);
4130 introspect_append_standard_interfaces (s);
4132 /* then include the registered interfaces */
4133 g_hash_table_iter_init (&hash_iter, eo->map_if_name_to_ei);
4134 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &ei))
4135 g_dbus_interface_info_generate_xml (ei->interface_info, 2, s);
4137 /* finally include nodes registered below us */
4138 registered = g_dbus_connection_list_registered_unlocked (connection, eo->object_path);
4139 for (n = 0; registered != NULL && registered[n] != NULL; n++)
4140 g_string_append_printf (s, " <node name=\"%s\"/>\n", registered[n]);
4141 g_strfreev (registered);
4142 g_string_append (s, introspect_tail);
4144 reply = g_dbus_message_new_method_reply (message);
4145 g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
4146 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
4147 g_object_unref (reply);
4148 g_string_free (s, TRUE);
4153 /* called in thread where object was registered - no locks held */
4155 call_in_idle_cb (gpointer user_data)
4157 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
4158 GDBusInterfaceVTable *vtable;
4159 guint registration_id;
4160 guint subtree_registration_id;
4162 vtable = g_object_get_data (G_OBJECT (invocation), "g-dbus-interface-vtable");
4163 g_assert (vtable != NULL && vtable->method_call != NULL);
4165 registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-registration-id"));
4166 subtree_registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id"));
4168 if (has_object_been_unregistered (g_dbus_method_invocation_get_connection (invocation),
4170 subtree_registration_id))
4172 GDBusMessage *reply;
4173 reply = g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation),
4174 "org.freedesktop.DBus.Error.UnknownMethod",
4175 _("No such interface `%s' on object at path %s"),
4176 g_dbus_method_invocation_get_interface_name (invocation),
4177 g_dbus_method_invocation_get_object_path (invocation));
4178 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, NULL);
4179 g_object_unref (reply);
4183 vtable->method_call (g_dbus_method_invocation_get_connection (invocation),
4184 g_dbus_method_invocation_get_sender (invocation),
4185 g_dbus_method_invocation_get_object_path (invocation),
4186 g_dbus_method_invocation_get_interface_name (invocation),
4187 g_dbus_method_invocation_get_method_name (invocation),
4188 g_dbus_method_invocation_get_parameters (invocation),
4189 g_object_ref (invocation),
4190 g_dbus_method_invocation_get_user_data (invocation));
4196 /* called in message handler thread with lock held */
4198 validate_and_maybe_schedule_method_call (GDBusConnection *connection,
4199 GDBusMessage *message,
4200 guint registration_id,
4201 guint subtree_registration_id,
4202 const GDBusInterfaceInfo *interface_info,
4203 const GDBusInterfaceVTable *vtable,
4204 GMainContext *main_context,
4207 GDBusMethodInvocation *invocation;
4208 const GDBusMethodInfo *method_info;
4209 GDBusMessage *reply;
4210 GVariant *parameters;
4211 GSource *idle_source;
4213 GVariantType *in_type;
4217 /* TODO: the cost of this is O(n) - it might be worth caching the result */
4218 method_info = g_dbus_interface_info_lookup_method (interface_info, g_dbus_message_get_member (message));
4220 /* if the method doesn't exist, return the org.freedesktop.DBus.Error.UnknownMethod
4221 * error to the caller
4223 if (method_info == NULL)
4225 reply = g_dbus_message_new_method_error (message,
4226 "org.freedesktop.DBus.Error.UnknownMethod",
4227 _("No such method `%s'"),
4228 g_dbus_message_get_member (message));
4229 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
4230 g_object_unref (reply);
4235 parameters = g_dbus_message_get_body (message);
4236 if (parameters == NULL)
4238 parameters = g_variant_new ("()");
4239 g_variant_ref_sink (parameters);
4243 g_variant_ref (parameters);
4246 /* Check that the incoming args are of the right type - if they are not, return
4247 * the org.freedesktop.DBus.Error.InvalidArgs error to the caller
4249 in_type = _g_dbus_compute_complete_signature (method_info->in_args);
4250 if (!g_variant_is_of_type (parameters, in_type))
4254 type_string = g_variant_type_dup_string (in_type);
4256 reply = g_dbus_message_new_method_error (message,
4257 "org.freedesktop.DBus.Error.InvalidArgs",
4258 _("Type of message, `%s', does not match expected type `%s'"),
4259 g_variant_get_type_string (parameters),
4261 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
4262 g_variant_type_free (in_type);
4263 g_variant_unref (parameters);
4264 g_object_unref (reply);
4265 g_free (type_string);
4269 g_variant_type_free (in_type);
4271 /* schedule the call in idle */
4272 invocation = g_dbus_method_invocation_new (g_dbus_message_get_sender (message),
4273 g_dbus_message_get_path (message),
4274 g_dbus_message_get_interface (message),
4275 g_dbus_message_get_member (message),
4281 g_variant_unref (parameters);
4283 /* TODO: would be nicer with a real MethodData like we already
4284 * have PropertyData and PropertyGetAllData... */
4285 g_object_set_data (G_OBJECT (invocation), "g-dbus-interface-vtable", (gpointer) vtable);
4286 g_object_set_data (G_OBJECT (invocation), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id));
4287 g_object_set_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id));
4289 idle_source = g_idle_source_new ();
4290 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
4291 g_source_set_callback (idle_source,
4295 g_source_attach (idle_source, main_context);
4296 g_source_unref (idle_source);
4304 /* ---------------------------------------------------------------------------------------------------- */
4306 /* called in message handler thread with lock held */
4308 obj_message_func (GDBusConnection *connection,
4310 GDBusMessage *message)
4312 const gchar *interface_name;
4313 const gchar *member;
4314 const gchar *signature;
4319 interface_name = g_dbus_message_get_interface (message);
4320 member = g_dbus_message_get_member (message);
4321 signature = g_dbus_message_get_signature (message);
4323 /* see if we have an interface for handling this call */
4324 if (interface_name != NULL)
4326 ExportedInterface *ei;
4327 ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
4330 /* we do - invoke the handler in idle in the right thread */
4332 /* handle no vtable or handler being present */
4333 if (ei->vtable == NULL || ei->vtable->method_call == NULL)
4336 handled = validate_and_maybe_schedule_method_call (connection,
4348 if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Introspectable") == 0 &&
4349 g_strcmp0 (member, "Introspect") == 0 &&
4350 g_strcmp0 (signature, "") == 0)
4352 handled = handle_introspect (connection, eo, message);
4355 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
4356 g_strcmp0 (member, "Get") == 0 &&
4357 g_strcmp0 (signature, "ss") == 0)
4359 handled = handle_getset_property (connection, eo, message, TRUE);
4362 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
4363 g_strcmp0 (member, "Set") == 0 &&
4364 g_strcmp0 (signature, "ssv") == 0)
4366 handled = handle_getset_property (connection, eo, message, FALSE);
4369 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
4370 g_strcmp0 (member, "GetAll") == 0 &&
4371 g_strcmp0 (signature, "s") == 0)
4373 handled = handle_get_all_properties (connection, eo, message);
4382 * g_dbus_connection_register_object:
4383 * @connection: A #GDBusConnection.
4384 * @object_path: The object path to register at.
4385 * @interface_info: Introspection data for the interface.
4386 * @vtable: A #GDBusInterfaceVTable to call into or %NULL.
4387 * @user_data: Data to pass to functions in @vtable.
4388 * @user_data_free_func: Function to call when the object path is unregistered.
4389 * @error: Return location for error or %NULL.
4391 * Registers callbacks for exported objects at @object_path with the
4392 * D-Bus interface that is described in @interface_info.
4394 * Calls to functions in @vtable (and @user_data_free_func) will
4395 * happen in the <link linkend="g-main-context-push-thread-default">thread-default main
4396 * loop</link> of the thread you are calling this method from.
4398 * Note that all #GVariant values passed to functions in @vtable will match
4399 * the signature given in @interface_info - if a remote caller passes
4400 * incorrect values, the <literal>org.freedesktop.DBus.Error.InvalidArgs</literal>
4401 * is returned to the remote caller.
4403 * Additionally, if the remote caller attempts to invoke methods or
4404 * access properties not mentioned in @interface_info the
4405 * <literal>org.freedesktop.DBus.Error.UnknownMethod</literal> resp.
4406 * <literal>org.freedesktop.DBus.Error.InvalidArgs</literal> errors
4407 * are returned to the caller.
4409 * It is considered a programming error if the
4410 * #GDBusInterfaceGetPropertyFunc function in @vtable returns a
4411 * #GVariant of incorrect type.
4413 * If an existing callback is already registered at @object_path and
4414 * @interface_name, then @error is set to #G_IO_ERROR_EXISTS.
4416 * Note that @vtable is not copied, so the struct you pass must exist until
4417 * the path is unregistered. One possibility is to free @vtable at the
4418 * same time as @user_data when @user_data_free_func is called.
4420 * GDBus automatically implements the standard D-Bus interfaces
4421 * org.freedesktop.DBus.Properties, org.freedesktop.DBus.Introspectable
4422 * and org.freedesktop.Peer, so you don't have to implement those for
4423 * the objects you export. You <emphasis>can</emphasis> implement
4424 * org.freedesktop.DBus.Properties yourself, e.g. to handle getting
4425 * and setting of properties asynchronously.
4427 * See <xref linkend="gdbus-server"/> for an example of how to use this method.
4429 * Returns: 0 if @error is set, otherwise a registration id (never 0)
4430 * that can be used with g_dbus_connection_unregister_object() .
4435 g_dbus_connection_register_object (GDBusConnection *connection,
4436 const gchar *object_path,
4437 const GDBusInterfaceInfo *interface_info,
4438 const GDBusInterfaceVTable *vtable,
4440 GDestroyNotify user_data_free_func,
4444 ExportedInterface *ei;
4447 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
4448 g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
4449 g_return_val_if_fail (interface_info != NULL, 0);
4450 g_return_val_if_fail (g_dbus_is_interface_name (interface_info->name), 0);
4451 g_return_val_if_fail (error == NULL || *error == NULL, 0);
4455 CONNECTION_LOCK (connection);
4457 eo = g_hash_table_lookup (connection->map_object_path_to_eo, object_path);
4460 eo = g_new0 (ExportedObject, 1);
4461 eo->object_path = g_strdup (object_path);
4462 eo->connection = connection;
4463 eo->map_if_name_to_ei = g_hash_table_new_full (g_str_hash,
4466 (GDestroyNotify) exported_interface_free);
4467 g_hash_table_insert (connection->map_object_path_to_eo, eo->object_path, eo);
4470 ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_info->name);
4476 _("An object is already exported for the interface %s at %s"),
4477 interface_info->name,
4482 ei = g_new0 (ExportedInterface, 1);
4483 ei->id = _global_registration_id++; /* TODO: overflow etc. */
4485 ei->user_data = user_data;
4486 ei->user_data_free_func = user_data_free_func;
4487 ei->vtable = vtable;
4488 ei->interface_info = g_dbus_interface_info_ref ((GDBusInterfaceInfo *) interface_info);
4489 ei->interface_name = g_strdup (interface_info->name);
4490 ei->context = g_main_context_get_thread_default ();
4491 if (ei->context != NULL)
4492 g_main_context_ref (ei->context);
4494 g_hash_table_insert (eo->map_if_name_to_ei,
4495 (gpointer) ei->interface_name,
4497 g_hash_table_insert (connection->map_id_to_ei,
4498 GUINT_TO_POINTER (ei->id),
4504 CONNECTION_UNLOCK (connection);
4510 * g_dbus_connection_unregister_object:
4511 * @connection: A #GDBusConnection.
4512 * @registration_id: A registration id obtained from g_dbus_connection_register_object().
4514 * Unregisters an object.
4516 * Returns: %TRUE if the object was unregistered, %FALSE otherwise.
4521 g_dbus_connection_unregister_object (GDBusConnection *connection,
4522 guint registration_id)
4524 ExportedInterface *ei;
4528 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
4532 CONNECTION_LOCK (connection);
4534 ei = g_hash_table_lookup (connection->map_id_to_ei,
4535 GUINT_TO_POINTER (registration_id));
4541 g_warn_if_fail (g_hash_table_remove (connection->map_id_to_ei, GUINT_TO_POINTER (ei->id)));
4542 g_warn_if_fail (g_hash_table_remove (eo->map_if_name_to_ei, ei->interface_name));
4543 /* unregister object path if we have no more exported interfaces */
4544 if (g_hash_table_size (eo->map_if_name_to_ei) == 0)
4545 g_warn_if_fail (g_hash_table_remove (connection->map_object_path_to_eo,
4551 CONNECTION_UNLOCK (connection);
4556 /* ---------------------------------------------------------------------------------------------------- */
4559 * g_dbus_connection_emit_signal:
4560 * @connection: A #GDBusConnection.
4561 * @destination_bus_name: The unique bus name for the destination for the signal or %NULL to emit to all listeners.
4562 * @object_path: Path of remote object.
4563 * @interface_name: D-Bus interface to emit a signal on.
4564 * @signal_name: The name of the signal to emit.
4565 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
4566 * @error: Return location for error or %NULL.
4570 * If the parameters GVariant is floating, it is consumed.
4572 * This can only fail if @parameters is not compatible with the D-Bus protocol.
4574 * Returns: %TRUE unless @error is set.
4579 g_dbus_connection_emit_signal (GDBusConnection *connection,
4580 const gchar *destination_bus_name,
4581 const gchar *object_path,
4582 const gchar *interface_name,
4583 const gchar *signal_name,
4584 GVariant *parameters,
4587 GDBusMessage *message;
4593 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
4594 g_return_val_if_fail (destination_bus_name == NULL || g_dbus_is_name (destination_bus_name), FALSE);
4595 g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), FALSE);
4596 g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), FALSE);
4597 g_return_val_if_fail (signal_name != NULL && g_dbus_is_member_name (signal_name), FALSE);
4598 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), FALSE);
4600 if (G_UNLIKELY (_g_dbus_debug_emission ()))
4602 _g_dbus_debug_print_lock ();
4603 g_print ("========================================================================\n"
4604 "GDBus-debug:Emission:\n"
4605 " >>>> SIGNAL EMISSION %s.%s()\n"
4607 " destination %s\n",
4608 interface_name, signal_name,
4610 destination_bus_name != NULL ? destination_bus_name : "(none)");
4611 _g_dbus_debug_print_unlock ();
4614 message = g_dbus_message_new_signal (object_path,
4618 if (destination_bus_name != NULL)
4619 g_dbus_message_set_header (message,
4620 G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION,
4621 g_variant_new_string (destination_bus_name));
4623 if (parameters != NULL)
4624 g_dbus_message_set_body (message, parameters);
4626 ret = g_dbus_connection_send_message (connection, message, NULL, error);
4627 g_object_unref (message);
4633 add_call_flags (GDBusMessage *message,
4634 GDBusCallFlags flags)
4636 if (flags & G_DBUS_CALL_FLAGS_NO_AUTO_START)
4637 g_dbus_message_set_flags (message, G_DBUS_MESSAGE_FLAGS_NO_AUTO_START);
4641 decode_method_reply (GDBusMessage *reply,
4642 const gchar *method_name,
4643 const GVariantType *reply_type,
4649 switch (g_dbus_message_get_message_type (reply))
4651 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
4652 result = g_dbus_message_get_body (reply);
4655 result = g_variant_new ("()");
4656 g_variant_ref_sink (result);
4660 g_variant_ref (result);
4663 if (!g_variant_is_of_type (result, reply_type))
4665 gchar *type_string = g_variant_type_dup_string (reply_type);
4669 G_IO_ERROR_INVALID_ARGUMENT,
4670 _("Method `%s' returned type `%s', but expected `%s'"),
4671 method_name, g_variant_get_type_string (result), type_string);
4673 g_variant_unref (result);
4674 g_free (type_string);
4679 case G_DBUS_MESSAGE_TYPE_ERROR:
4680 g_dbus_message_to_gerror (reply, error);
4684 g_assert_not_reached ();
4694 GSimpleAsyncResult *simple;
4695 GVariantType *reply_type;
4696 gchar *method_name; /* for error message */
4701 g_dbus_connection_call_done (GObject *source,
4702 GAsyncResult *result,
4705 GDBusConnection *connection = G_DBUS_CONNECTION (source);
4706 CallState *state = user_data;
4708 GDBusMessage *reply;
4712 reply = g_dbus_connection_send_message_with_reply_finish (connection,
4716 if (G_UNLIKELY (_g_dbus_debug_call ()))
4718 _g_dbus_debug_print_lock ();
4719 g_print ("========================================================================\n"
4720 "GDBus-debug:Call:\n"
4721 " >>>> ASYNC COMPLETE %s() (serial %d)\n"
4727 g_print ("SUCCESS\n");
4731 g_print ("FAILED: %s\n",
4734 _g_dbus_debug_print_unlock ();
4740 value = decode_method_reply (reply, state->method_name,
4741 state->reply_type, &error);
4742 g_object_unref (reply);
4749 g_simple_async_result_set_from_error (state->simple, error);
4750 g_error_free (error);
4753 g_simple_async_result_set_op_res_gpointer (state->simple, value,
4754 (GDestroyNotify) g_variant_unref);
4756 g_simple_async_result_complete (state->simple);
4757 g_variant_type_free (state->reply_type);
4758 g_object_unref (state->simple);
4759 g_free (state->method_name);
4761 g_slice_free (CallState, state);
4765 * g_dbus_connection_call:
4766 * @connection: A #GDBusConnection.
4767 * @bus_name: A unique or well-known bus name or %NULL if @connection is not a message bus connection.
4768 * @object_path: Path of remote object.
4769 * @interface_name: D-Bus interface to invoke method on.
4770 * @method_name: The name of the method to invoke.
4771 * @parameters: A #GVariant tuple with parameters for the method or %NULL if not passing parameters.
4772 * @reply_type: The expected type of the reply, or %NULL.
4773 * @flags: Flags from the #GDBusCallFlags enumeration.
4774 * @timeout_msec: The timeout in milliseconds or -1 to use the default timeout.
4775 * @cancellable: A #GCancellable or %NULL.
4776 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
4777 * care about the result of the method invocation.
4778 * @user_data: The data to pass to @callback.
4780 * Asynchronously invokes the @method_name method on the
4781 * @interface_name D-Bus interface on the remote object at
4782 * @object_path owned by @bus_name.
4784 * If @connection is closed then the operation will fail with
4785 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
4786 * fail with %G_IO_ERROR_CANCELLED. If @parameters contains a value
4787 * not compatible with the D-Bus protocol, the operation fails with
4788 * %G_IO_ERROR_INVALID_ARGUMENT.
4790 * If @reply_type is non-%NULL then the reply will be checked for having this type and an
4791 * error will be raised if it does not match. Said another way, if you give a @reply_type
4792 * then any non-%NULL return value will be of this type.
4794 * If the @parameters #GVariant is floating, it is consumed. This allows
4795 * convenient 'inline' use of g_variant_new(), e.g.:
4797 * g_dbus_connection_call (connection,
4798 * "org.freedesktop.StringThings",
4799 * "/org/freedesktop/StringThings",
4800 * "org.freedesktop.StringThings",
4802 * g_variant_new ("(ss)",
4806 * G_DBUS_CALL_FLAGS_NONE,
4809 * (GAsyncReadyCallback) two_strings_done,
4813 * This is an asynchronous method. When the operation is finished, @callback will be invoked
4814 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
4815 * of the thread you are calling this method from. You can then call
4816 * g_dbus_connection_call_finish() to get the result of the operation.
4817 * See g_dbus_connection_call_sync() for the synchronous version of this
4823 g_dbus_connection_call (GDBusConnection *connection,
4824 const gchar *bus_name,
4825 const gchar *object_path,
4826 const gchar *interface_name,
4827 const gchar *method_name,
4828 GVariant *parameters,
4829 const GVariantType *reply_type,
4830 GDBusCallFlags flags,
4832 GCancellable *cancellable,
4833 GAsyncReadyCallback callback,
4836 GDBusMessage *message;
4839 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
4840 g_return_if_fail (bus_name == NULL || g_dbus_is_name (bus_name));
4841 g_return_if_fail (object_path != NULL && g_variant_is_object_path (object_path));
4842 g_return_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name));
4843 g_return_if_fail (method_name != NULL && g_dbus_is_member_name (method_name));
4844 g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
4845 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
4847 state = g_slice_new (CallState);
4848 state->simple = g_simple_async_result_new (G_OBJECT (connection),
4849 callback, user_data,
4850 g_dbus_connection_call);
4851 state->method_name = g_strjoin (".", interface_name, method_name, NULL);
4853 if (reply_type == NULL)
4854 reply_type = G_VARIANT_TYPE_ANY;
4856 state->reply_type = g_variant_type_copy (reply_type);
4858 message = g_dbus_message_new_method_call (bus_name,
4862 add_call_flags (message, flags);
4863 if (parameters != NULL)
4864 g_dbus_message_set_body (message, parameters);
4866 g_dbus_connection_send_message_with_reply (connection,
4871 g_dbus_connection_call_done,
4874 if (G_UNLIKELY (_g_dbus_debug_call ()))
4876 _g_dbus_debug_print_lock ();
4877 g_print ("========================================================================\n"
4878 "GDBus-debug:Call:\n"
4879 " >>>> ASYNC %s.%s()\n"
4881 " owned by name %s (serial %d)\n",
4885 bus_name != NULL ? bus_name : "(none)",
4887 _g_dbus_debug_print_unlock ();
4890 if (message != NULL)
4891 g_object_unref (message);
4895 * g_dbus_connection_call_finish:
4896 * @connection: A #GDBusConnection.
4897 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call().
4898 * @error: Return location for error or %NULL.
4900 * Finishes an operation started with g_dbus_connection_call().
4902 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
4903 * return values. Free with g_variant_unref().
4908 g_dbus_connection_call_finish (GDBusConnection *connection,
4912 GSimpleAsyncResult *simple;
4914 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
4915 g_return_val_if_fail (g_simple_async_result_is_valid (res, G_OBJECT (connection),
4916 g_dbus_connection_call), NULL);
4917 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
4919 simple = G_SIMPLE_ASYNC_RESULT (res);
4921 if (g_simple_async_result_propagate_error (simple, error))
4924 return g_variant_ref (g_simple_async_result_get_op_res_gpointer (simple));
4927 /* ---------------------------------------------------------------------------------------------------- */
4930 * g_dbus_connection_call_sync:
4931 * @connection: A #GDBusConnection.
4932 * @bus_name: A unique or well-known bus name.
4933 * @object_path: Path of remote object.
4934 * @interface_name: D-Bus interface to invoke method on.
4935 * @method_name: The name of the method to invoke.
4936 * @parameters: A #GVariant tuple with parameters for the method or %NULL if not passing parameters.
4937 * @reply_type: The expected type of the reply, or %NULL.
4938 * @flags: Flags from the #GDBusCallFlags enumeration.
4939 * @timeout_msec: The timeout in milliseconds or -1 to use the default timeout.
4940 * @cancellable: A #GCancellable or %NULL.
4941 * @error: Return location for error or %NULL.
4943 * Synchronously invokes the @method_name method on the
4944 * @interface_name D-Bus interface on the remote object at
4945 * @object_path owned by @bus_name.
4947 * If @connection is closed then the operation will fail with
4948 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the
4949 * operation will fail with %G_IO_ERROR_CANCELLED. If @parameters
4950 * contains a value not compatible with the D-Bus protocol, the operation
4951 * fails with %G_IO_ERROR_INVALID_ARGUMENT.
4953 * If @reply_type is non-%NULL then the reply will be checked for having
4954 * this type and an error will be raised if it does not match. Said
4955 * another way, if you give a @reply_type then any non-%NULL return
4956 * value will be of this type.
4958 * If the @parameters #GVariant is floating, it is consumed.
4959 * This allows convenient 'inline' use of g_variant_new(), e.g.:
4961 * g_dbus_connection_call_sync (connection,
4962 * "org.freedesktop.StringThings",
4963 * "/org/freedesktop/StringThings",
4964 * "org.freedesktop.StringThings",
4966 * g_variant_new ("(ss)",
4970 * G_DBUS_CALL_FLAGS_NONE,
4976 * The calling thread is blocked until a reply is received. See
4977 * g_dbus_connection_call() for the asynchronous version of
4980 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
4981 * return values. Free with g_variant_unref().
4986 g_dbus_connection_call_sync (GDBusConnection *connection,
4987 const gchar *bus_name,
4988 const gchar *object_path,
4989 const gchar *interface_name,
4990 const gchar *method_name,
4991 GVariant *parameters,
4992 const GVariantType *reply_type,
4993 GDBusCallFlags flags,
4995 GCancellable *cancellable,
4998 GDBusMessage *message;
4999 GDBusMessage *reply;
5001 GError *local_error;
5007 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
5008 g_return_val_if_fail (bus_name == NULL || g_dbus_is_name (bus_name), NULL);
5009 g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), NULL);
5010 g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), NULL);
5011 g_return_val_if_fail (method_name != NULL && g_dbus_is_member_name (method_name), NULL);
5012 g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
5013 g_return_val_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
5015 if (reply_type == NULL)
5016 reply_type = G_VARIANT_TYPE_ANY;
5018 message = g_dbus_message_new_method_call (bus_name,
5022 add_call_flags (message, flags);
5023 if (parameters != NULL)
5024 g_dbus_message_set_body (message, parameters);
5026 if (G_UNLIKELY (_g_dbus_debug_call ()))
5028 _g_dbus_debug_print_lock ();
5029 g_print ("========================================================================\n"
5030 "GDBus-debug:Call:\n"
5031 " >>>> SYNC %s.%s()\n"
5033 " owned by name %s\n",
5037 bus_name != NULL ? bus_name : "(none)");
5038 _g_dbus_debug_print_unlock ();
5042 reply = g_dbus_connection_send_message_with_reply_sync (connection,
5045 NULL, /* volatile guint32 *out_serial */
5049 if (G_UNLIKELY (_g_dbus_debug_call ()))
5051 _g_dbus_debug_print_lock ();
5052 g_print ("========================================================================\n"
5053 "GDBus-debug:Call:\n"
5054 " <<<< SYNC COMPLETE %s.%s()\n"
5060 g_print ("SUCCESS\n");
5064 g_print ("FAILED: %s\n",
5065 local_error->message);
5067 _g_dbus_debug_print_unlock ();
5073 *error = local_error;
5075 g_error_free (local_error);
5079 result = decode_method_reply (reply, method_name, reply_type, error);
5082 if (message != NULL)
5083 g_object_unref (message);
5085 g_object_unref (reply);
5090 /* ---------------------------------------------------------------------------------------------------- */
5092 struct ExportedSubtree
5096 GDBusConnection *connection;
5097 const GDBusSubtreeVTable *vtable;
5098 GDBusSubtreeFlags flags;
5100 GMainContext *context;
5102 GDestroyNotify user_data_free_func;
5106 exported_subtree_free (ExportedSubtree *es)
5108 if (es->user_data_free_func != NULL)
5109 /* TODO: push to thread-default mainloop */
5110 es->user_data_free_func (es->user_data);
5112 if (es->context != NULL)
5113 g_main_context_unref (es->context);
5115 g_free (es->object_path);
5119 /* called without lock held */
5121 handle_subtree_introspect (GDBusConnection *connection,
5122 ExportedSubtree *es,
5123 GDBusMessage *message)
5127 GDBusMessage *reply;
5130 const gchar *sender;
5131 const gchar *requested_object_path;
5132 const gchar *requested_node;
5133 GPtrArray *interfaces;
5135 gchar **subnode_paths;
5139 requested_object_path = g_dbus_message_get_path (message);
5140 sender = g_dbus_message_get_sender (message);
5141 is_root = (g_strcmp0 (requested_object_path, es->object_path) == 0);
5143 s = g_string_new (NULL);
5144 introspect_append_header (s);
5146 /* Strictly we don't need the children in dynamic mode, but we avoid the
5147 * conditionals to preserve code clarity
5149 children = es->vtable->enumerate (es->connection,
5156 requested_node = strrchr (requested_object_path, '/') + 1;
5158 /* Assert existence of object if we are not dynamic */
5159 if (!(es->flags & G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES) &&
5160 !_g_strv_has_string ((const gchar * const *) children, requested_node))
5165 requested_node = "/";
5168 interfaces = es->vtable->introspect (es->connection,
5173 if (interfaces != NULL)
5175 if (interfaces->len > 0)
5177 /* we're in business */
5178 introspect_append_standard_interfaces (s);
5180 for (n = 0; n < interfaces->len; n++)
5182 const GDBusInterfaceInfo *interface_info = interfaces->pdata[n];
5183 g_dbus_interface_info_generate_xml (interface_info, 2, s);
5186 g_ptr_array_unref (interfaces);
5189 /* then include <node> entries from the Subtree for the root */
5192 for (n = 0; children != NULL && children[n] != NULL; n++)
5193 g_string_append_printf (s, " <node name=\"%s\"/>\n", children[n]);
5196 /* finally include nodes registered below us */
5197 subnode_paths = g_dbus_connection_list_registered (es->connection, requested_object_path);
5198 for (n = 0; subnode_paths != NULL && subnode_paths[n] != NULL; n++)
5199 g_string_append_printf (s, " <node name=\"%s\"/>\n", subnode_paths[n]);
5200 g_strfreev (subnode_paths);
5202 g_string_append (s, "</node>\n");
5204 reply = g_dbus_message_new_method_reply (message);
5205 g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
5206 g_dbus_connection_send_message (connection, reply, NULL, NULL);
5207 g_object_unref (reply);
5212 g_string_free (s, TRUE);
5213 g_strfreev (children);
5217 /* called without lock held */
5219 handle_subtree_method_invocation (GDBusConnection *connection,
5220 ExportedSubtree *es,
5221 GDBusMessage *message)
5224 const gchar *sender;
5225 const gchar *interface_name;
5226 const gchar *member;
5227 const gchar *signature;
5228 const gchar *requested_object_path;
5229 const gchar *requested_node;
5232 const GDBusInterfaceInfo *interface_info;
5233 const GDBusInterfaceVTable *interface_vtable;
5234 gpointer interface_user_data;
5236 GPtrArray *interfaces;
5237 gboolean is_property_get;
5238 gboolean is_property_set;
5239 gboolean is_property_get_all;
5244 requested_object_path = g_dbus_message_get_path (message);
5245 sender = g_dbus_message_get_sender (message);
5246 interface_name = g_dbus_message_get_interface (message);
5247 member = g_dbus_message_get_member (message);
5248 signature = g_dbus_message_get_signature (message);
5249 is_root = (g_strcmp0 (requested_object_path, es->object_path) == 0);
5251 is_property_get = FALSE;
5252 is_property_set = FALSE;
5253 is_property_get_all = FALSE;
5254 if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0)
5256 if (g_strcmp0 (member, "Get") == 0 && g_strcmp0 (signature, "ss") == 0)
5257 is_property_get = TRUE;
5258 else if (g_strcmp0 (member, "Set") == 0 && g_strcmp0 (signature, "ssv") == 0)
5259 is_property_set = TRUE;
5260 else if (g_strcmp0 (member, "GetAll") == 0 && g_strcmp0 (signature, "s") == 0)
5261 is_property_get_all = TRUE;
5264 children = es->vtable->enumerate (es->connection,
5271 requested_node = strrchr (requested_object_path, '/') + 1;
5273 /* If not dynamic, skip if requested node is not part of children */
5274 if (!(es->flags & G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES) &&
5275 !_g_strv_has_string ((const gchar * const *) children, requested_node))
5280 requested_node = "/";
5283 /* get introspection data for the node */
5284 interfaces = es->vtable->introspect (es->connection,
5286 requested_object_path,
5289 g_assert (interfaces != NULL);
5290 interface_info = NULL;
5291 for (n = 0; n < interfaces->len; n++)
5293 const GDBusInterfaceInfo *id_n = (const GDBusInterfaceInfo *) interfaces->pdata[n];
5294 if (g_strcmp0 (id_n->name, interface_name) == 0)
5295 interface_info = id_n;
5298 /* dispatch the call if the user wants to handle it */
5299 if (interface_info != NULL)
5301 /* figure out where to dispatch the method call */
5302 interface_user_data = NULL;
5303 interface_vtable = es->vtable->dispatch (es->connection,
5308 &interface_user_data,
5310 if (interface_vtable == NULL)
5313 CONNECTION_LOCK (connection);
5314 handled = validate_and_maybe_schedule_method_call (es->connection,
5321 interface_user_data);
5322 CONNECTION_UNLOCK (connection);
5324 /* handle org.freedesktop.DBus.Properties interface if not explicitly handled */
5325 else if (is_property_get || is_property_set || is_property_get_all)
5327 if (is_property_get)
5328 g_variant_get (g_dbus_message_get_body (message), "(&s&s)", &interface_name, NULL);
5329 else if (is_property_set)
5330 g_variant_get (g_dbus_message_get_body (message), "(&s&sv)", &interface_name, NULL, NULL);
5331 else if (is_property_get_all)
5332 g_variant_get (g_dbus_message_get_body (message), "(&s)", &interface_name, NULL, NULL);
5334 g_assert_not_reached ();
5336 /* see if the object supports this interface at all */
5337 for (n = 0; n < interfaces->len; n++)
5339 const GDBusInterfaceInfo *id_n = (const GDBusInterfaceInfo *) interfaces->pdata[n];
5340 if (g_strcmp0 (id_n->name, interface_name) == 0)
5341 interface_info = id_n;
5344 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the user-code
5345 * claims it won't support the interface
5347 if (interface_info == NULL)
5349 GDBusMessage *reply;
5350 reply = g_dbus_message_new_method_error (message,
5351 "org.freedesktop.DBus.Error.InvalidArgs",
5352 _("No such interface `%s'"),
5354 g_dbus_connection_send_message (es->connection, reply, NULL, NULL);
5355 g_object_unref (reply);
5360 /* figure out where to dispatch the property get/set/getall calls */
5361 interface_user_data = NULL;
5362 interface_vtable = es->vtable->dispatch (es->connection,
5367 &interface_user_data,
5369 if (interface_vtable == NULL)
5372 if (is_property_get || is_property_set)
5374 CONNECTION_LOCK (connection);
5375 handled = validate_and_maybe_schedule_property_getset (es->connection,
5383 interface_user_data);
5384 CONNECTION_UNLOCK (connection);
5386 else if (is_property_get_all)
5388 CONNECTION_LOCK (connection);
5389 handled = validate_and_maybe_schedule_property_get_all (es->connection,
5396 interface_user_data);
5397 CONNECTION_UNLOCK (connection);
5402 if (interfaces != NULL)
5403 g_ptr_array_unref (interfaces);
5404 g_strfreev (children);
5410 GDBusMessage *message;
5411 ExportedSubtree *es;
5412 } SubtreeDeferredData;
5415 subtree_deferred_data_free (SubtreeDeferredData *data)
5417 g_object_unref (data->message);
5421 /* called without lock held in the thread where the caller registered the subtree */
5423 process_subtree_vtable_message_in_idle_cb (gpointer _data)
5425 SubtreeDeferredData *data = _data;
5430 if (g_strcmp0 (g_dbus_message_get_interface (data->message), "org.freedesktop.DBus.Introspectable") == 0 &&
5431 g_strcmp0 (g_dbus_message_get_member (data->message), "Introspect") == 0 &&
5432 g_strcmp0 (g_dbus_message_get_signature (data->message), "") == 0)
5433 handled = handle_subtree_introspect (data->es->connection,
5437 handled = handle_subtree_method_invocation (data->es->connection,
5443 CONNECTION_LOCK (data->es->connection);
5444 handled = handle_generic_unlocked (data->es->connection, data->message);
5445 CONNECTION_UNLOCK (data->es->connection);
5448 /* if we couldn't handle the request, just bail with the UnknownMethod error */
5451 GDBusMessage *reply;
5452 reply = g_dbus_message_new_method_error (data->message,
5453 "org.freedesktop.DBus.Error.UnknownMethod",
5454 _("Method `%s' on interface `%s' with signature `%s' does not exist"),
5455 g_dbus_message_get_member (data->message),
5456 g_dbus_message_get_interface (data->message),
5457 g_dbus_message_get_signature (data->message));
5458 g_dbus_connection_send_message (data->es->connection, reply, NULL, NULL);
5459 g_object_unref (reply);
5465 /* called in message handler thread with lock held */
5467 subtree_message_func (GDBusConnection *connection,
5468 ExportedSubtree *es,
5469 GDBusMessage *message)
5471 GSource *idle_source;
5472 SubtreeDeferredData *data;
5474 data = g_new0 (SubtreeDeferredData, 1);
5475 data->message = g_object_ref (message);
5478 /* defer this call to an idle handler in the right thread */
5479 idle_source = g_idle_source_new ();
5480 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
5481 g_source_set_callback (idle_source,
5482 process_subtree_vtable_message_in_idle_cb,
5484 (GDestroyNotify) subtree_deferred_data_free);
5485 g_source_attach (idle_source, es->context);
5486 g_source_unref (idle_source);
5488 /* since we own the entire subtree, handlers for objects not in the subtree have been
5489 * tried already by libdbus-1 - so we just need to ensure that we're always going
5490 * to reply to the message
5496 * g_dbus_connection_register_subtree:
5497 * @connection: A #GDBusConnection.
5498 * @object_path: The object path to register the subtree at.
5499 * @vtable: A #GDBusSubtreeVTable to enumerate, introspect and dispatch nodes in the subtree.
5500 * @flags: Flags used to fine tune the behavior of the subtree.
5501 * @user_data: Data to pass to functions in @vtable.
5502 * @user_data_free_func: Function to call when the subtree is unregistered.
5503 * @error: Return location for error or %NULL.
5505 * Registers a whole subtree of <quote>dynamic</quote> objects.
5507 * The @enumerate and @introspection functions in @vtable are used to
5508 * convey, to remote callers, what nodes exist in the subtree rooted
5511 * When handling remote calls into any node in the subtree, first the
5512 * @enumerate function is used to check if the node exists. If the node exists
5513 * or the #G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is set
5514 * the @introspection function is used to check if the node supports the
5515 * requested method. If so, the @dispatch function is used to determine
5516 * where to dispatch the call. The collected #GDBusInterfaceVTable and
5517 * #gpointer will be used to call into the interface vtable for processing
5520 * All calls into user-provided code will be invoked in the <link
5521 * linkend="g-main-context-push-thread-default">thread-default main
5522 * loop</link> of the thread you are calling this method from.
5524 * If an existing subtree is already registered at @object_path or
5525 * then @error is set to #G_IO_ERROR_EXISTS.
5527 * Note that it is valid to register regular objects (using
5528 * g_dbus_connection_register_object()) in a subtree registered with
5529 * g_dbus_connection_register_subtree() - if so, the subtree handler
5530 * is tried as the last resort. One way to think about a subtree
5531 * handler is to consider it a <quote>fallback handler</quote>
5532 * for object paths not registered via g_dbus_connection_register_object()
5533 * or other bindings.
5535 * See <xref linkend="gdbus-subtree-server"/> for an example of how to use this method.
5537 * Returns: 0 if @error is set, otherwise a subtree registration id (never 0)
5538 * that can be used with g_dbus_connection_unregister_subtree() .
5543 g_dbus_connection_register_subtree (GDBusConnection *connection,
5544 const gchar *object_path,
5545 const GDBusSubtreeVTable *vtable,
5546 GDBusSubtreeFlags flags,
5548 GDestroyNotify user_data_free_func,
5552 ExportedSubtree *es;
5554 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
5555 g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
5556 g_return_val_if_fail (vtable != NULL, 0);
5557 g_return_val_if_fail (error == NULL || *error == NULL, 0);
5561 CONNECTION_LOCK (connection);
5563 es = g_hash_table_lookup (connection->map_object_path_to_es, object_path);
5569 _("A subtree is already exported for %s"),
5574 es = g_new0 (ExportedSubtree, 1);
5575 es->object_path = g_strdup (object_path);
5576 es->connection = connection;
5578 es->vtable = vtable;
5580 es->id = _global_subtree_registration_id++; /* TODO: overflow etc. */
5581 es->user_data = user_data;
5582 es->user_data_free_func = user_data_free_func;
5583 es->context = g_main_context_get_thread_default ();
5584 if (es->context != NULL)
5585 g_main_context_ref (es->context);
5587 g_hash_table_insert (connection->map_object_path_to_es, es->object_path, es);
5588 g_hash_table_insert (connection->map_id_to_es,
5589 GUINT_TO_POINTER (es->id),
5595 CONNECTION_UNLOCK (connection);
5600 /* ---------------------------------------------------------------------------------------------------- */
5603 * g_dbus_connection_unregister_subtree:
5604 * @connection: A #GDBusConnection.
5605 * @registration_id: A subtree registration id obtained from g_dbus_connection_register_subtree().
5607 * Unregisters a subtree.
5609 * Returns: %TRUE if the subtree was unregistered, %FALSE otherwise.
5614 g_dbus_connection_unregister_subtree (GDBusConnection *connection,
5615 guint registration_id)
5617 ExportedSubtree *es;
5620 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
5624 CONNECTION_LOCK (connection);
5626 es = g_hash_table_lookup (connection->map_id_to_es,
5627 GUINT_TO_POINTER (registration_id));
5631 g_warn_if_fail (g_hash_table_remove (connection->map_id_to_es, GUINT_TO_POINTER (es->id)));
5632 g_warn_if_fail (g_hash_table_remove (connection->map_object_path_to_es, es->object_path));
5637 CONNECTION_UNLOCK (connection);
5642 /* ---------------------------------------------------------------------------------------------------- */
5644 /* must be called with lock held */
5646 handle_generic_ping_unlocked (GDBusConnection *connection,
5647 const gchar *object_path,
5648 GDBusMessage *message)
5650 GDBusMessage *reply;
5651 reply = g_dbus_message_new_method_reply (message);
5652 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
5653 g_object_unref (reply);
5656 /* must be called with lock held */
5658 handle_generic_get_machine_id_unlocked (GDBusConnection *connection,
5659 const gchar *object_path,
5660 GDBusMessage *message)
5662 GDBusMessage *reply;
5665 if (connection->machine_id == NULL)
5670 connection->machine_id = _g_dbus_get_machine_id (&error);
5671 if (connection->machine_id == NULL)
5673 reply = g_dbus_message_new_method_error_literal (message,
5674 "org.freedesktop.DBus.Error.Failed",
5676 g_error_free (error);
5682 reply = g_dbus_message_new_method_reply (message);
5683 g_dbus_message_set_body (reply, g_variant_new ("(s)", connection->machine_id));
5685 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
5686 g_object_unref (reply);
5689 /* must be called with lock held */
5691 handle_generic_introspect_unlocked (GDBusConnection *connection,
5692 const gchar *object_path,
5693 GDBusMessage *message)
5698 GDBusMessage *reply;
5700 /* first the header */
5701 s = g_string_new (NULL);
5702 introspect_append_header (s);
5704 registered = g_dbus_connection_list_registered_unlocked (connection, object_path);
5705 for (n = 0; registered != NULL && registered[n] != NULL; n++)
5706 g_string_append_printf (s, " <node name=\"%s\"/>\n", registered[n]);
5707 g_strfreev (registered);
5708 g_string_append (s, "</node>\n");
5710 reply = g_dbus_message_new_method_reply (message);
5711 g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
5712 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
5713 g_object_unref (reply);
5714 g_string_free (s, TRUE);
5717 /* must be called with lock held */
5719 handle_generic_unlocked (GDBusConnection *connection,
5720 GDBusMessage *message)
5723 const gchar *interface_name;
5724 const gchar *member;
5725 const gchar *signature;
5728 CONNECTION_ENSURE_LOCK (connection);
5732 interface_name = g_dbus_message_get_interface (message);
5733 member = g_dbus_message_get_member (message);
5734 signature = g_dbus_message_get_signature (message);
5735 path = g_dbus_message_get_path (message);
5737 if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Introspectable") == 0 &&
5738 g_strcmp0 (member, "Introspect") == 0 &&
5739 g_strcmp0 (signature, "") == 0)
5741 handle_generic_introspect_unlocked (connection, path, message);
5744 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Peer") == 0 &&
5745 g_strcmp0 (member, "Ping") == 0 &&
5746 g_strcmp0 (signature, "") == 0)
5748 handle_generic_ping_unlocked (connection, path, message);
5751 else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Peer") == 0 &&
5752 g_strcmp0 (member, "GetMachineId") == 0 &&
5753 g_strcmp0 (signature, "") == 0)
5755 handle_generic_get_machine_id_unlocked (connection, path, message);
5762 /* ---------------------------------------------------------------------------------------------------- */
5764 /* called in message handler thread with lock held */
5766 distribute_method_call (GDBusConnection *connection,
5767 GDBusMessage *message)
5769 GDBusMessage *reply;
5771 ExportedSubtree *es;
5772 const gchar *object_path;
5773 const gchar *interface_name;
5774 const gchar *member;
5775 const gchar *signature;
5777 gchar *subtree_path;
5780 g_assert (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL);
5782 interface_name = g_dbus_message_get_interface (message);
5783 member = g_dbus_message_get_member (message);
5784 signature = g_dbus_message_get_signature (message);
5785 path = g_dbus_message_get_path (message);
5786 subtree_path = g_strdup (path);
5787 needle = strrchr (subtree_path, '/');
5788 if (needle != NULL && needle != subtree_path)
5794 g_free (subtree_path);
5795 subtree_path = NULL;
5799 if (G_UNLIKELY (_g_dbus_debug_incoming ()))
5801 _g_dbus_debug_print_lock ();
5802 g_print ("========================================================================\n"
5803 "GDBus-debug:Incoming:\n"
5804 " >>>> METHOD INVOCATION %s.%s()\n"
5806 " invoked by name %s\n",
5807 interface_name, member,
5809 g_dbus_message_get_sender (message) != NULL ? g_dbus_message_get_sender (message) : "(none)");
5810 _g_dbus_debug_print_unlock ();
5814 g_debug ("interface = `%s'", interface_name);
5815 g_debug ("member = `%s'", member);
5816 g_debug ("signature = `%s'", signature);
5817 g_debug ("path = `%s'", path);
5818 g_debug ("subtree_path = `%s'", subtree_path != NULL ? subtree_path : "N/A");
5821 object_path = g_dbus_message_get_path (message);
5822 g_assert (object_path != NULL);
5824 eo = g_hash_table_lookup (connection->map_object_path_to_eo, object_path);
5827 if (obj_message_func (connection, eo, message))
5831 es = g_hash_table_lookup (connection->map_object_path_to_es, object_path);
5834 if (subtree_message_func (connection, es, message))
5838 if (subtree_path != NULL)
5840 es = g_hash_table_lookup (connection->map_object_path_to_es, subtree_path);
5843 if (subtree_message_func (connection, es, message))
5848 if (handle_generic_unlocked (connection, message))
5851 /* if we end up here, the message has not been not handled - so return an error saying this */
5852 reply = g_dbus_message_new_method_error (message,
5853 "org.freedesktop.DBus.Error.UnknownMethod",
5854 _("No such interface `%s' on object at path %s"),
5857 g_dbus_connection_send_message_unlocked (connection, reply, NULL, NULL);
5858 g_object_unref (reply);
5861 g_free (subtree_path);
5864 /* ---------------------------------------------------------------------------------------------------- */
5866 static GDBusConnection **
5867 message_bus_get_singleton (GBusType bus_type,
5870 GDBusConnection **ret;
5871 const gchar *starter_bus;
5877 case G_BUS_TYPE_SESSION:
5878 ret = &the_session_bus;
5881 case G_BUS_TYPE_SYSTEM:
5882 ret = &the_system_bus;
5885 case G_BUS_TYPE_STARTER:
5886 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
5887 if (g_strcmp0 (starter_bus, "session") == 0)
5889 ret = message_bus_get_singleton (G_BUS_TYPE_SESSION, error);
5892 else if (g_strcmp0 (starter_bus, "system") == 0)
5894 ret = message_bus_get_singleton (G_BUS_TYPE_SYSTEM, error);
5899 if (starter_bus != NULL)
5903 G_IO_ERROR_INVALID_ARGUMENT,
5904 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
5905 " - unknown value `%s'"),
5910 g_set_error_literal (error,
5912 G_IO_ERROR_INVALID_ARGUMENT,
5913 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
5914 "variable is not set"));
5920 g_assert_not_reached ();
5928 static GDBusConnection *
5929 get_uninitialized_connection (GBusType bus_type,
5930 GCancellable *cancellable,
5933 GDBusConnection **singleton;
5934 GDBusConnection *ret;
5938 G_LOCK (message_bus_lock);
5939 singleton = message_bus_get_singleton (bus_type, error);
5940 if (singleton == NULL)
5943 if (*singleton == NULL)
5946 address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
5947 if (address == NULL)
5949 ret = *singleton = g_object_new (G_TYPE_DBUS_CONNECTION,
5951 "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
5952 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
5953 "exit-on-close", TRUE,
5959 ret = g_object_ref (*singleton);
5962 g_assert (ret != NULL);
5965 G_UNLOCK (message_bus_lock);
5971 * @bus_type: A #GBusType.
5972 * @cancellable: A #GCancellable or %NULL.
5973 * @error: Return location for error or %NULL.
5975 * Synchronously connects to the message bus specified by @bus_type.
5976 * Note that the returned object may shared with other callers,
5977 * e.g. if two separate parts of a process calls this function with
5978 * the same @bus_type, they will share the same object.
5980 * This is a synchronous failable function. See g_bus_get() and
5981 * g_bus_get_finish() for the asynchronous version.
5983 * The returned object is a singleton, that is, shared with other
5984 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
5985 * event that you need a private message bus connection, use
5986 * g_dbus_address_get_for_bus_sync() and
5987 * g_dbus_connection_new_for_address().
5989 * Note that the returned #GDBusConnection object will (usually) have
5990 * the #GDBusConnection:exit-on-close property set to %TRUE.
5992 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
5997 g_bus_get_sync (GBusType bus_type,
5998 GCancellable *cancellable,
6001 GDBusConnection *connection;
6003 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
6005 connection = get_uninitialized_connection (bus_type, cancellable, error);
6006 if (connection == NULL)
6009 if (!g_initable_init (G_INITABLE (connection), cancellable, error))
6011 g_object_unref (connection);
6020 bus_get_async_initable_cb (GObject *source_object,
6024 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
6028 if (!g_async_initable_init_finish (G_ASYNC_INITABLE (source_object),
6032 g_assert (error != NULL);
6033 g_simple_async_result_set_from_error (simple, error);
6034 g_error_free (error);
6035 g_object_unref (source_object);
6039 g_simple_async_result_set_op_res_gpointer (simple,
6043 g_simple_async_result_complete_in_idle (simple);
6044 g_object_unref (simple);
6049 * @bus_type: A #GBusType.
6050 * @cancellable: A #GCancellable or %NULL.
6051 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
6052 * @user_data: The data to pass to @callback.
6054 * Asynchronously connects to the message bus specified by @bus_type.
6056 * When the operation is finished, @callback will be invoked. You can
6057 * then call g_bus_get_finish() to get the result of the operation.
6059 * This is a asynchronous failable function. See g_bus_get_sync() for
6060 * the synchronous version.
6065 g_bus_get (GBusType bus_type,
6066 GCancellable *cancellable,
6067 GAsyncReadyCallback callback,
6070 GDBusConnection *connection;
6071 GSimpleAsyncResult *simple;
6074 simple = g_simple_async_result_new (NULL,
6080 connection = get_uninitialized_connection (bus_type, cancellable, &error);
6081 if (connection == NULL)
6083 g_assert (error != NULL);
6084 g_simple_async_result_set_from_error (simple, error);
6085 g_error_free (error);
6086 g_simple_async_result_complete_in_idle (simple);
6087 g_object_unref (simple);
6091 g_async_initable_init_async (G_ASYNC_INITABLE (connection),
6094 bus_get_async_initable_cb,
6101 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_bus_get().
6102 * @error: Return location for error or %NULL.
6104 * Finishes an operation started with g_bus_get().
6106 * The returned object is a singleton, that is, shared with other
6107 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
6108 * event that you need a private message bus connection, use
6109 * g_dbus_address_get_for_bus() and
6110 * g_dbus_connection_new_for_address().
6112 * Note that the returned #GDBusConnection object will (usually) have
6113 * the #GDBusConnection:exit-on-close property set to %TRUE.
6115 * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
6120 g_bus_get_finish (GAsyncResult *res,
6123 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
6125 GDBusConnection *ret;
6127 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
6129 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_bus_get);
6133 if (g_simple_async_result_propagate_error (simple, error))
6136 object = g_simple_async_result_get_op_res_gpointer (simple);
6137 g_assert (object != NULL);
6138 ret = g_object_ref (G_DBUS_CONNECTION (object));
6144 /* ---------------------------------------------------------------------------------------------------- */