1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: David Zeuthen <davidz@redhat.com>
25 #include "gdbusinterface.h"
26 #include "gdbusinterfaceskeleton.h"
27 #include "gdbusobjectskeleton.h"
28 #include "gioenumtypes.h"
29 #include "gdbusprivate.h"
30 #include "gdbusmethodinvocation.h"
31 #include "gdbusconnection.h"
32 #include "gmarshal-internal.h"
39 * SECTION:gdbusinterfaceskeleton
40 * @short_description: Service-side D-Bus interface
43 * Abstract base class for D-Bus interfaces on the service side.
46 struct _GDBusInterfaceSkeletonPrivate
51 GDBusInterfaceSkeletonFlags flags;
53 GSList *connections; /* List of ConnectionData */
54 gchar *object_path; /* The object path for this skeleton */
55 GDBusInterfaceVTable *hooked_vtable;
60 GDBusConnection *connection;
61 guint registration_id;
66 G_AUTHORIZE_METHOD_SIGNAL,
76 static guint signals[LAST_SIGNAL] = {0};
78 static void dbus_interface_interface_init (GDBusInterfaceIface *iface);
80 static void set_object_path_locked (GDBusInterfaceSkeleton *interface_,
81 const gchar *object_path);
82 static void remove_connection_locked (GDBusInterfaceSkeleton *interface_,
83 GDBusConnection *connection);
84 static void skeleton_intercept_handle_method_call (GDBusConnection *connection,
86 const gchar *object_path,
87 const gchar *interface_name,
88 const gchar *method_name,
90 GDBusMethodInvocation *invocation,
94 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GDBusInterfaceSkeleton, g_dbus_interface_skeleton, G_TYPE_OBJECT,
95 G_ADD_PRIVATE (GDBusInterfaceSkeleton)
96 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_interface_init))
99 g_dbus_interface_skeleton_finalize (GObject *object)
101 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
103 /* Hold the lock just in case any code we call verifies that the lock is held */
104 g_mutex_lock (&interface->priv->lock);
106 /* unexport from all connections if we're exported anywhere */
107 while (interface->priv->connections != NULL)
109 ConnectionData *data = interface->priv->connections->data;
110 remove_connection_locked (interface, data->connection);
113 set_object_path_locked (interface, NULL);
115 g_mutex_unlock (&interface->priv->lock);
117 g_free (interface->priv->hooked_vtable);
119 if (interface->priv->object != NULL)
120 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
122 g_mutex_clear (&interface->priv->lock);
124 G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
128 g_dbus_interface_skeleton_get_property (GObject *object,
133 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
138 g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148 g_dbus_interface_skeleton_set_property (GObject *object,
153 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
158 g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton *interface,
169 GDBusMethodInvocation *invocation)
175 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
177 GObjectClass *gobject_class;
179 gobject_class = G_OBJECT_CLASS (klass);
180 gobject_class->finalize = g_dbus_interface_skeleton_finalize;
181 gobject_class->set_property = g_dbus_interface_skeleton_set_property;
182 gobject_class->get_property = g_dbus_interface_skeleton_get_property;
184 klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
187 * GDBusInterfaceSkeleton:g-flags:
189 * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
193 g_object_class_install_property (gobject_class,
195 g_param_spec_flags ("g-flags",
197 "Flags for the interface skeleton",
198 G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS,
199 G_DBUS_INTERFACE_SKELETON_FLAGS_NONE,
202 G_PARAM_STATIC_STRINGS));
205 * GDBusInterfaceSkeleton::g-authorize-method:
206 * @interface: The #GDBusInterfaceSkeleton emitting the signal.
207 * @invocation: A #GDBusMethodInvocation.
209 * Emitted when a method is invoked by a remote caller and used to
210 * determine if the method call is authorized.
212 * Note that this signal is emitted in a thread dedicated to
213 * handling the method call so handlers are allowed to perform
214 * blocking IO. This means that it is appropriate to call e.g.
215 * [polkit_authority_check_authorization_sync()](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#polkit-authority-check-authorization-sync)
217 * [POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#POLKIT-CHECK-AUTHORIZATION-FLAGS-ALLOW-USER-INTERACTION:CAPS)
220 * If %FALSE is returned then no further handlers are run and the
221 * signal handler must take a reference to @invocation and finish
222 * handling the call (e.g. return an error via
223 * g_dbus_method_invocation_return_error()).
225 * Otherwise, if %TRUE is returned, signal emission continues. If no
226 * handlers return %FALSE, then the method is dispatched. If
227 * @interface has an enclosing #GDBusObjectSkeleton, then the
228 * #GDBusObjectSkeleton::authorize-method signal handlers run before
229 * the handlers for this signal.
231 * The default class handler just returns %TRUE.
233 * Please note that the common case is optimized: if no signals
234 * handlers are connected and the default class handler isn't
235 * overridden (for both @interface and the enclosing
236 * #GDBusObjectSkeleton, if any) and #GDBusInterfaceSkeleton:g-flags does
238 * %G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD
239 * flags set, no dedicated thread is ever used and the call will be
240 * handled in the same thread as the object that @interface belongs
241 * to was exported in.
243 * Returns: %TRUE if the call is authorized, %FALSE otherwise.
247 signals[G_AUTHORIZE_METHOD_SIGNAL] =
248 g_signal_new (I_("g-authorize-method"),
249 G_TYPE_DBUS_INTERFACE_SKELETON,
251 G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method),
252 _g_signal_accumulator_false_handled,
254 _g_cclosure_marshal_BOOLEAN__OBJECT,
257 G_TYPE_DBUS_METHOD_INVOCATION);
258 g_signal_set_va_marshaller (signals[G_AUTHORIZE_METHOD_SIGNAL],
259 G_TYPE_FROM_CLASS (klass),
260 _g_cclosure_marshal_BOOLEAN__OBJECTv);
264 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
266 interface->priv = g_dbus_interface_skeleton_get_instance_private (interface);
267 g_mutex_init (&interface->priv->lock);
270 /* ---------------------------------------------------------------------------------------------------- */
273 * g_dbus_interface_skeleton_get_flags:
274 * @interface_: A #GDBusInterfaceSkeleton.
276 * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
279 * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
283 GDBusInterfaceSkeletonFlags
284 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton *interface_)
286 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
287 return interface_->priv->flags;
291 * g_dbus_interface_skeleton_set_flags:
292 * @interface_: A #GDBusInterfaceSkeleton.
293 * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
295 * Sets flags describing what the behavior of @skeleton should be.
300 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton *interface_,
301 GDBusInterfaceSkeletonFlags flags)
303 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
304 g_mutex_lock (&interface_->priv->lock);
305 if (interface_->priv->flags != flags)
307 interface_->priv->flags = flags;
308 g_mutex_unlock (&interface_->priv->lock);
309 g_object_notify (G_OBJECT (interface_), "g-flags");
313 g_mutex_unlock (&interface_->priv->lock);
318 * g_dbus_interface_skeleton_get_info:
319 * @interface_: A #GDBusInterfaceSkeleton.
321 * Gets D-Bus introspection information for the D-Bus interface
322 * implemented by @interface_.
324 * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
329 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
331 GDBusInterfaceInfo *ret;
332 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
333 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_info (interface_);
334 g_warn_if_fail (ret != NULL);
339 * g_dbus_interface_skeleton_get_vtable:
340 * @interface_: A #GDBusInterfaceSkeleton.
342 * Gets the interface vtable for the D-Bus interface implemented by
343 * @interface_. The returned function pointers should expect @interface_
344 * itself to be passed as @user_data.
346 * Returns: (not nullable) (transfer none): the vtable of the D-Bus interface implemented by the skeleton
350 GDBusInterfaceVTable *
351 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
353 GDBusInterfaceVTable *ret;
354 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
355 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_vtable (interface_);
356 g_warn_if_fail (ret != NULL);
361 * g_dbus_interface_skeleton_get_properties:
362 * @interface_: A #GDBusInterfaceSkeleton.
364 * Gets all D-Bus properties for @interface_.
366 * Returns: (transfer full): A #GVariant of type
367 * ['a{sv}'][G-VARIANT-TYPE-VARDICT:CAPS].
368 * Free with g_variant_unref().
373 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
376 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
377 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_);
378 return g_variant_take_ref (ret);
382 * g_dbus_interface_skeleton_flush:
383 * @interface_: A #GDBusInterfaceSkeleton.
385 * If @interface_ has outstanding changes, request for these changes to be
386 * emitted immediately.
388 * For example, an exported D-Bus interface may queue up property
389 * changes and emit the
390 * `org.freedesktop.DBus.Properties.PropertiesChanged`
391 * signal later (e.g. in an idle handler). This technique is useful
392 * for collapsing multiple property changes into one.
397 g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
399 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
400 G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
403 /* ---------------------------------------------------------------------------------------------------- */
405 static GDBusInterfaceInfo *
406 _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
408 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
409 return g_dbus_interface_skeleton_get_info (interface);
413 g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
415 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
417 g_mutex_lock (&interface->priv->lock);
418 ret = interface->priv->object;
419 g_mutex_unlock (&interface->priv->lock);
424 g_dbus_interface_skeleton_dup_object (GDBusInterface *interface_)
426 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
428 g_mutex_lock (&interface->priv->lock);
429 ret = interface->priv->object;
432 g_mutex_unlock (&interface->priv->lock);
437 g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
440 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
441 g_mutex_lock (&interface->priv->lock);
442 if (interface->priv->object != NULL)
443 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
444 interface->priv->object = object;
446 g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
447 g_mutex_unlock (&interface->priv->lock);
451 dbus_interface_interface_init (GDBusInterfaceIface *iface)
453 iface->get_info = _g_dbus_interface_skeleton_get_info;
454 iface->get_object = g_dbus_interface_skeleton_get_object;
455 iface->dup_object = g_dbus_interface_skeleton_dup_object;
456 iface->set_object = g_dbus_interface_skeleton_set_object;
459 /* ---------------------------------------------------------------------------------------------------- */
463 gint ref_count; /* (atomic) */
464 GDBusInterfaceMethodCallFunc method_call_func;
465 GDBusMethodInvocation *invocation; /* (owned) */
469 dispatch_data_unref (DispatchData *data)
471 if (g_atomic_int_dec_and_test (&data->ref_count))
473 g_clear_object (&data->invocation);
474 g_slice_free (DispatchData, data);
478 static DispatchData *
479 dispatch_data_ref (DispatchData *data)
481 g_atomic_int_inc (&data->ref_count);
486 dispatch_invoke_in_context_func (gpointer user_data)
488 DispatchData *data = user_data;
489 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
490 g_dbus_method_invocation_get_sender (data->invocation),
491 g_dbus_method_invocation_get_object_path (data->invocation),
492 g_dbus_method_invocation_get_interface_name (data->invocation),
493 g_dbus_method_invocation_get_method_name (data->invocation),
494 g_dbus_method_invocation_get_parameters (data->invocation),
496 g_dbus_method_invocation_get_user_data (data->invocation));
501 dispatch_in_thread_func (GTask *task,
502 gpointer source_object,
504 GCancellable *cancellable)
506 DispatchData *data = task_data;
507 GDBusInterfaceSkeleton *interface = g_task_get_source_object (task);
508 GDBusInterfaceSkeletonFlags flags;
512 g_mutex_lock (&interface->priv->lock);
513 flags = interface->priv->flags;
514 object = interface->priv->object;
516 g_object_ref (object);
517 g_mutex_unlock (&interface->priv->lock);
519 /* first check on the enclosing object (if any), then the interface */
523 g_signal_emit_by_name (object,
531 g_signal_emit (interface,
532 signals[G_AUTHORIZE_METHOD_SIGNAL],
540 gboolean run_in_thread;
541 run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
544 /* might as well just re-use the existing thread */
545 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
546 g_dbus_method_invocation_get_sender (data->invocation),
547 g_dbus_method_invocation_get_object_path (data->invocation),
548 g_dbus_method_invocation_get_interface_name (data->invocation),
549 g_dbus_method_invocation_get_method_name (data->invocation),
550 g_dbus_method_invocation_get_parameters (data->invocation),
552 g_dbus_method_invocation_get_user_data (data->invocation));
556 /* bah, back to original context */
557 g_main_context_invoke_full (g_task_get_context (task),
558 g_task_get_priority (task),
559 dispatch_invoke_in_context_func,
560 dispatch_data_ref (data),
561 (GDestroyNotify) dispatch_data_unref);
570 g_object_unref (object);
572 g_task_return_boolean (task, TRUE);
576 g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton *interface,
577 GDBusInterfaceMethodCallFunc method_call_func,
578 GDBusMethodInvocation *invocation)
580 gboolean has_handlers;
581 gboolean has_default_class_handler;
582 gboolean emit_authorized_signal;
583 gboolean run_in_thread;
584 GDBusInterfaceSkeletonFlags flags;
587 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
588 g_return_if_fail (method_call_func != NULL);
589 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
591 g_mutex_lock (&interface->priv->lock);
592 flags = interface->priv->flags;
593 object = interface->priv->object;
595 g_object_ref (object);
596 g_mutex_unlock (&interface->priv->lock);
598 /* optimization for the common case where
600 * a) no handler is connected and class handler is not overridden (both interface and object); and
601 * b) method calls are not dispatched in a thread
603 has_handlers = g_signal_has_handler_pending (interface,
604 signals[G_AUTHORIZE_METHOD_SIGNAL],
607 has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
608 g_dbus_interface_skeleton_g_authorize_method_default);
610 emit_authorized_signal = (has_handlers || !has_default_class_handler);
611 if (!emit_authorized_signal)
614 emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object));
617 run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
618 if (!emit_authorized_signal && !run_in_thread)
620 method_call_func (g_dbus_method_invocation_get_connection (invocation),
621 g_dbus_method_invocation_get_sender (invocation),
622 g_dbus_method_invocation_get_object_path (invocation),
623 g_dbus_method_invocation_get_interface_name (invocation),
624 g_dbus_method_invocation_get_method_name (invocation),
625 g_dbus_method_invocation_get_parameters (invocation),
627 g_dbus_method_invocation_get_user_data (invocation));
634 data = g_slice_new0 (DispatchData);
635 data->method_call_func = method_call_func;
636 data->invocation = g_object_ref (invocation);
639 task = g_task_new (interface, NULL, NULL, NULL);
640 g_task_set_source_tag (task, g_dbus_interface_method_dispatch_helper);
641 g_task_set_name (task, "[gio] D-Bus interface method dispatch");
642 g_task_set_task_data (task, data, (GDestroyNotify) dispatch_data_unref);
643 g_task_run_in_thread (task, dispatch_in_thread_func);
644 g_object_unref (task);
648 g_object_unref (object);
652 skeleton_intercept_handle_method_call (GDBusConnection *connection,
654 const gchar *object_path,
655 const gchar *interface_name,
656 const gchar *method_name,
657 GVariant *parameters,
658 GDBusMethodInvocation *invocation,
661 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
662 g_dbus_interface_method_dispatch_helper (interface,
663 g_dbus_interface_skeleton_get_vtable (interface)->method_call,
667 /* ---------------------------------------------------------------------------------------------------- */
669 static ConnectionData *
670 new_connection (GDBusConnection *connection,
671 guint registration_id)
673 ConnectionData *data;
675 data = g_slice_new0 (ConnectionData);
676 data->connection = g_object_ref (connection);
677 data->registration_id = registration_id;
683 free_connection (ConnectionData *data)
687 g_object_unref (data->connection);
688 g_slice_free (ConnectionData, data);
693 add_connection_locked (GDBusInterfaceSkeleton *interface_,
694 GDBusConnection *connection,
697 ConnectionData *data;
698 guint registration_id;
699 gboolean ret = FALSE;
701 if (interface_->priv->hooked_vtable == NULL)
703 /* Hook the vtable since we need to intercept method calls for
704 * ::g-authorize-method and for dispatching in thread vs
707 * We need to wait until subclasses have had time to initialize
708 * properly before building the hooked_vtable, so we create it
709 * once at the last minute.
711 interface_->priv->hooked_vtable = g_memdup2 (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
712 interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
715 registration_id = g_dbus_connection_register_object (connection,
716 interface_->priv->object_path,
717 g_dbus_interface_skeleton_get_info (interface_),
718 interface_->priv->hooked_vtable,
720 NULL, /* user_data_free_func */
723 if (registration_id > 0)
725 data = new_connection (connection, registration_id);
726 interface_->priv->connections = g_slist_append (interface_->priv->connections, data);
734 remove_connection_locked (GDBusInterfaceSkeleton *interface_,
735 GDBusConnection *connection)
737 ConnectionData *data;
740 /* Get the connection in the list and unregister ... */
741 for (l = interface_->priv->connections; l != NULL; l = l->next)
744 if (data->connection == connection)
746 g_warn_if_fail (g_dbus_connection_unregister_object (data->connection, data->registration_id));
747 free_connection (data);
748 interface_->priv->connections = g_slist_delete_link (interface_->priv->connections, l);
749 /* we are guaranteed that the connection is only added once, so bail out early */
758 set_object_path_locked (GDBusInterfaceSkeleton *interface_,
759 const gchar *object_path)
761 if (g_strcmp0 (interface_->priv->object_path, object_path) != 0)
763 g_free (interface_->priv->object_path);
764 interface_->priv->object_path = g_strdup (object_path);
768 /* ---------------------------------------------------------------------------------------------------- */
771 * g_dbus_interface_skeleton_get_connection:
772 * @interface_: A #GDBusInterfaceSkeleton.
774 * Gets the first connection that @interface_ is exported on, if any.
776 * Returns: (nullable) (transfer none): A #GDBusConnection or %NULL if @interface_ is
777 * not exported anywhere. Do not free, the object belongs to @interface_.
782 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
784 ConnectionData *data;
785 GDBusConnection *ret;
787 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
788 g_mutex_lock (&interface_->priv->lock);
791 if (interface_->priv->connections != NULL)
793 data = interface_->priv->connections->data;
795 ret = data->connection;
798 g_mutex_unlock (&interface_->priv->lock);
804 * g_dbus_interface_skeleton_get_connections:
805 * @interface_: A #GDBusInterfaceSkeleton.
807 * Gets a list of the connections that @interface_ is exported on.
809 * Returns: (element-type GDBusConnection) (transfer full): A list of
810 * all the connections that @interface_ is exported on. The returned
811 * list should be freed with g_list_free() after each element has
812 * been freed with g_object_unref().
817 g_dbus_interface_skeleton_get_connections (GDBusInterfaceSkeleton *interface_)
821 ConnectionData *data;
823 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
825 g_mutex_lock (&interface_->priv->lock);
828 for (l = interface_->priv->connections; l != NULL; l = l->next)
831 connections = g_list_prepend (connections,
832 /* Return a reference to each connection */
833 g_object_ref (data->connection));
836 g_mutex_unlock (&interface_->priv->lock);
838 return g_list_reverse (connections);
842 * g_dbus_interface_skeleton_has_connection:
843 * @interface_: A #GDBusInterfaceSkeleton.
844 * @connection: A #GDBusConnection.
846 * Checks if @interface_ is exported on @connection.
848 * Returns: %TRUE if @interface_ is exported on @connection, %FALSE otherwise.
853 g_dbus_interface_skeleton_has_connection (GDBusInterfaceSkeleton *interface_,
854 GDBusConnection *connection)
857 gboolean ret = FALSE;
859 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
860 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
862 g_mutex_lock (&interface_->priv->lock);
864 for (l = interface_->priv->connections; l != NULL; l = l->next)
866 ConnectionData *data = l->data;
867 if (data->connection == connection)
875 g_mutex_unlock (&interface_->priv->lock);
880 * g_dbus_interface_skeleton_get_object_path:
881 * @interface_: A #GDBusInterfaceSkeleton.
883 * Gets the object path that @interface_ is exported on, if any.
885 * Returns: (nullable): A string owned by @interface_ or %NULL if @interface_ is not exported
886 * anywhere. Do not free, the string belongs to @interface_.
891 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
894 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
895 g_mutex_lock (&interface_->priv->lock);
896 ret = interface_->priv->object_path;
897 g_mutex_unlock (&interface_->priv->lock);
902 * g_dbus_interface_skeleton_export:
903 * @interface_: The D-Bus interface to export.
904 * @connection: A #GDBusConnection to export @interface_ on.
905 * @object_path: The path to export the interface at.
906 * @error: Return location for error or %NULL.
908 * Exports @interface_ at @object_path on @connection.
910 * This can be called multiple times to export the same @interface_
911 * onto multiple connections however the @object_path provided must be
912 * the same for all connections.
914 * Use g_dbus_interface_skeleton_unexport() to unexport the object.
916 * Returns: %TRUE if the interface was exported on @connection, otherwise %FALSE with
922 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton *interface_,
923 GDBusConnection *connection,
924 const gchar *object_path,
927 gboolean ret = FALSE;
929 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
930 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
931 g_return_val_if_fail (g_variant_is_object_path (object_path), FALSE);
932 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
934 /* Assert that the object path is the same for multiple connections here */
935 g_return_val_if_fail (interface_->priv->object_path == NULL ||
936 g_strcmp0 (interface_->priv->object_path, object_path) == 0, FALSE);
938 g_mutex_lock (&interface_->priv->lock);
940 /* Set the object path */
941 set_object_path_locked (interface_, object_path);
943 /* Add the connection */
944 ret = add_connection_locked (interface_, connection, error);
946 g_mutex_unlock (&interface_->priv->lock);
951 * g_dbus_interface_skeleton_unexport:
952 * @interface_: A #GDBusInterfaceSkeleton.
954 * Stops exporting @interface_ on all connections it is exported on.
956 * To unexport @interface_ from only a single connection, use
957 * g_dbus_interface_skeleton_unexport_from_connection()
962 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
964 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
965 g_return_if_fail (interface_->priv->connections != NULL);
967 g_mutex_lock (&interface_->priv->lock);
969 g_assert (interface_->priv->object_path != NULL);
970 g_assert (interface_->priv->hooked_vtable != NULL);
972 /* Remove all connections */
973 while (interface_->priv->connections != NULL)
975 ConnectionData *data = interface_->priv->connections->data;
976 remove_connection_locked (interface_, data->connection);
979 /* Unset the object path since there are no connections left */
980 set_object_path_locked (interface_, NULL);
982 g_mutex_unlock (&interface_->priv->lock);
987 * g_dbus_interface_skeleton_unexport_from_connection:
988 * @interface_: A #GDBusInterfaceSkeleton.
989 * @connection: A #GDBusConnection.
991 * Stops exporting @interface_ on @connection.
993 * To stop exporting on all connections the interface is exported on,
994 * use g_dbus_interface_skeleton_unexport().
999 g_dbus_interface_skeleton_unexport_from_connection (GDBusInterfaceSkeleton *interface_,
1000 GDBusConnection *connection)
1002 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
1003 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1004 g_return_if_fail (interface_->priv->connections != NULL);
1006 g_mutex_lock (&interface_->priv->lock);
1008 g_assert (interface_->priv->object_path != NULL);
1009 g_assert (interface_->priv->hooked_vtable != NULL);
1011 remove_connection_locked (interface_, connection);
1013 /* Reset the object path if we removed the last connection */
1014 if (interface_->priv->connections == NULL)
1015 set_object_path_locked (interface_, NULL);
1017 g_mutex_unlock (&interface_->priv->lock);
1020 /* ---------------------------------------------------------------------------------------------------- */