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>
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 "gioscheduler.h"
38 * SECTION:gdbusinterfaceskeleton
39 * @short_description: Service-side D-Bus interface
42 * Abstract base class for D-Bus interfaces on the service side.
45 struct _GDBusInterfaceSkeletonPrivate
50 GDBusInterfaceSkeletonFlags flags;
51 guint registration_id;
53 GDBusConnection *connection;
55 GDBusInterfaceVTable *hooked_vtable;
60 G_AUTHORIZE_METHOD_SIGNAL,
70 static guint signals[LAST_SIGNAL] = {0};
72 static void dbus_interface_interface_init (GDBusInterfaceIface *iface);
74 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GDBusInterfaceSkeleton, g_dbus_interface_skeleton, G_TYPE_OBJECT,
75 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_interface_init));
78 g_dbus_interface_skeleton_finalize (GObject *object)
80 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
81 /* unexport if already exported */
82 if (interface->priv->registration_id > 0)
83 g_dbus_interface_skeleton_unexport (interface);
85 g_assert (interface->priv->connection == NULL);
86 g_assert (interface->priv->object_path == NULL);
87 g_assert (interface->priv->hooked_vtable == NULL);
89 if (interface->priv->object != NULL)
90 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
92 g_mutex_clear (&interface->priv->lock);
94 G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
98 g_dbus_interface_skeleton_get_property (GObject *object,
103 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
108 g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 g_dbus_interface_skeleton_set_property (GObject *object,
123 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
128 g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton *interface,
139 GDBusMethodInvocation *invocation)
145 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
147 GObjectClass *gobject_class;
149 gobject_class = G_OBJECT_CLASS (klass);
150 gobject_class->finalize = g_dbus_interface_skeleton_finalize;
151 gobject_class->set_property = g_dbus_interface_skeleton_set_property;
152 gobject_class->get_property = g_dbus_interface_skeleton_get_property;
154 klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
157 * GDBusInterfaceSkeleton:g-flags:
159 * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
163 g_object_class_install_property (gobject_class,
165 g_param_spec_flags ("g-flags",
167 "Flags for the interface skeleton",
168 G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS,
169 G_DBUS_INTERFACE_SKELETON_FLAGS_NONE,
172 G_PARAM_STATIC_STRINGS));
175 * GDBusInterfaceSkeleton::g-authorize-method:
176 * @interface: The #GDBusInterfaceSkeleton emitting the signal.
177 * @invocation: A #GDBusMethodInvocation.
179 * Emitted when a method is invoked by a remote caller and used to
180 * determine if the method call is authorized.
182 * Note that this signal is emitted in a thread dedicated to
183 * handling the method call so handlers are allowed to perform
184 * blocking IO. This means that it is appropriate to call
186 * url="http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#polkit-authority-check-authorization-sync">polkit_authority_check_authorization_sync()</ulink>
188 * url="http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#POLKIT-CHECK-AUTHORIZATION-FLAGS-ALLOW-USER-INTERACTION:CAPS">POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION</ulink> flag set.
190 * If %FALSE is returned then no further handlers are run and the
191 * signal handler must take a reference to @invocation and finish
192 * handling the call (e.g. return an error via
193 * g_dbus_method_invocation_return_error()).
195 * Otherwise, if %TRUE is returned, signal emission continues. If no
196 * handlers return %FALSE, then the method is dispatched. If
197 * @interface has an enclosing #GDBusObjectSkeleton, then the
198 * #GDBusObjectSkeleton::authorize-method signal handlers run before
199 * the handlers for this signal.
201 * The default class handler just returns %TRUE.
203 * Please note that the common case is optimized: if no signals
204 * handlers are connected and the default class handler isn't
205 * overridden (for both @interface and the enclosing
206 * #GDBusObjectSkeleton, if any) and #GDBusInterfaceSkeleton:g-flags does
208 * %G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD
209 * flags set, no dedicated thread is ever used and the call will be
210 * handled in the same thread as the object that @interface belongs
211 * to was exported in.
213 * Returns: %TRUE if the call is authorized, %FALSE otherwise.
217 signals[G_AUTHORIZE_METHOD_SIGNAL] =
218 g_signal_new ("g-authorize-method",
219 G_TYPE_DBUS_INTERFACE_SKELETON,
221 G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method),
222 _g_signal_accumulator_false_handled,
227 G_TYPE_DBUS_METHOD_INVOCATION);
229 g_type_class_add_private (klass, sizeof (GDBusInterfaceSkeletonPrivate));
233 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
235 interface->priv = G_TYPE_INSTANCE_GET_PRIVATE (interface, G_TYPE_DBUS_INTERFACE_SKELETON, GDBusInterfaceSkeletonPrivate);
236 g_mutex_init (&interface->priv->lock);
239 /* ---------------------------------------------------------------------------------------------------- */
242 * g_dbus_interface_skeleton_get_flags:
243 * @interface_: A #GDBusInterfaceSkeleton.
245 * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
248 * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
252 GDBusInterfaceSkeletonFlags
253 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton *interface_)
255 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
256 return interface_->priv->flags;
260 * g_dbus_interface_skeleton_set_flags:
261 * @interface_: A #GDBusInterfaceSkeleton.
262 * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
264 * Sets flags describing what the behavior of @skeleton should be.
269 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton *interface_,
270 GDBusInterfaceSkeletonFlags flags)
272 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
273 g_mutex_lock (&interface_->priv->lock);
274 if (interface_->priv->flags != flags)
276 interface_->priv->flags = flags;
277 g_mutex_unlock (&interface_->priv->lock);
278 g_object_notify (G_OBJECT (interface_), "g-flags");
282 g_mutex_unlock (&interface_->priv->lock);
287 * g_dbus_interface_skeleton_get_info:
288 * @interface_: A #GDBusInterfaceSkeleton.
290 * Gets D-Bus introspection information for the D-Bus interface
291 * implemented by @interface_.
293 * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
298 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
300 GDBusInterfaceInfo *ret;
301 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
302 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_info (interface_);
303 g_warn_if_fail (ret != NULL);
308 * g_dbus_interface_skeleton_get_vtable: (skip)
309 * @interface_: A #GDBusInterfaceSkeleton.
311 * Gets the interface vtable for the D-Bus interface implemented by
312 * @interface_. The returned function pointers should expect @interface_
313 * itself to be passed as @user_data.
315 * Returns: A #GDBusInterfaceVTable (never %NULL).
319 GDBusInterfaceVTable *
320 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
322 GDBusInterfaceVTable *ret;
323 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
324 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_vtable (interface_);
325 g_warn_if_fail (ret != NULL);
330 * g_dbus_interface_skeleton_get_properties:
331 * @interface_: A #GDBusInterfaceSkeleton.
333 * Gets all D-Bus properties for @interface_.
335 * Returns: (transfer full): A #GVariant of type <link linkend="G-VARIANT-TYPE-VARDICT:CAPS">'a{sv}'</link>. Free with g_variant_unref().
340 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
343 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
344 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_);
345 return g_variant_take_ref (ret);
349 * g_dbus_interface_skeleton_flush:
350 * @interface_: A #GDBusInterfaceSkeleton.
352 * If @interface_ has outstanding changes, request for these changes to be
353 * emitted immediately.
355 * For example, an exported D-Bus interface may queue up property
356 * changes and emit the
357 * <literal>org.freedesktop.DBus.Properties::PropertiesChanged</literal>
358 * signal later (e.g. in an idle handler). This technique is useful
359 * for collapsing multiple property changes into one.
364 g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
366 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
367 G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
370 /* ---------------------------------------------------------------------------------------------------- */
372 static GDBusInterfaceInfo *
373 _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
375 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
376 return g_dbus_interface_skeleton_get_info (interface);
380 g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
382 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
384 g_mutex_lock (&interface->priv->lock);
385 ret = interface->priv->object;
386 g_mutex_unlock (&interface->priv->lock);
391 g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
394 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
395 g_mutex_lock (&interface->priv->lock);
396 if (interface->priv->object != NULL)
397 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
398 interface->priv->object = object;
400 g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
401 g_mutex_unlock (&interface->priv->lock);
405 dbus_interface_interface_init (GDBusInterfaceIface *iface)
407 iface->get_info = _g_dbus_interface_skeleton_get_info;
408 iface->get_object = g_dbus_interface_skeleton_get_object;
409 iface->set_object = g_dbus_interface_skeleton_set_object;
412 /* ---------------------------------------------------------------------------------------------------- */
416 volatile gint ref_count;
417 GDBusInterfaceSkeleton *interface;
418 GDBusInterfaceMethodCallFunc method_call_func;
419 GDBusMethodInvocation *invocation;
420 GMainContext *context;
424 dispatch_data_unref (DispatchData *data)
426 if (g_atomic_int_dec_and_test (&data->ref_count))
428 g_main_context_unref (data->context);
433 static DispatchData *
434 dispatch_data_ref (DispatchData *data)
436 g_atomic_int_inc (&data->ref_count);
441 dispatch_invoke_in_context_func (gpointer user_data)
443 DispatchData *data = user_data;
444 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
445 g_dbus_method_invocation_get_sender (data->invocation),
446 g_dbus_method_invocation_get_object_path (data->invocation),
447 g_dbus_method_invocation_get_interface_name (data->invocation),
448 g_dbus_method_invocation_get_method_name (data->invocation),
449 g_dbus_method_invocation_get_parameters (data->invocation),
451 g_dbus_method_invocation_get_user_data (data->invocation));
456 dispatch_in_thread_func (GIOSchedulerJob *job,
457 GCancellable *cancellable,
460 DispatchData *data = user_data;
461 GDBusInterfaceSkeletonFlags flags;
465 g_mutex_lock (&data->interface->priv->lock);
466 flags = data->interface->priv->flags;
467 object = data->interface->priv->object;
469 g_object_ref (object);
470 g_mutex_unlock (&data->interface->priv->lock);
472 /* first check on the enclosing object (if any), then the interface */
476 g_signal_emit_by_name (object,
484 g_signal_emit (data->interface,
485 signals[G_AUTHORIZE_METHOD_SIGNAL],
493 gboolean run_in_thread;
494 run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
497 /* might as well just re-use the existing thread */
498 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
499 g_dbus_method_invocation_get_sender (data->invocation),
500 g_dbus_method_invocation_get_object_path (data->invocation),
501 g_dbus_method_invocation_get_interface_name (data->invocation),
502 g_dbus_method_invocation_get_method_name (data->invocation),
503 g_dbus_method_invocation_get_parameters (data->invocation),
505 g_dbus_method_invocation_get_user_data (data->invocation));
509 /* bah, back to original context */
510 g_main_context_invoke_full (data->context,
512 dispatch_invoke_in_context_func,
513 dispatch_data_ref (data),
514 (GDestroyNotify) dispatch_data_unref);
523 g_object_unref (object);
529 g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton *interface,
530 GDBusInterfaceMethodCallFunc method_call_func,
531 GDBusMethodInvocation *invocation)
533 gboolean has_handlers;
534 gboolean has_default_class_handler;
535 gboolean emit_authorized_signal;
536 gboolean run_in_thread;
537 GDBusInterfaceSkeletonFlags flags;
540 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
541 g_return_if_fail (method_call_func != NULL);
542 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
544 g_mutex_lock (&interface->priv->lock);
545 flags = interface->priv->flags;
546 object = interface->priv->object;
548 g_object_ref (object);
549 g_mutex_unlock (&interface->priv->lock);
551 /* optimization for the common case where
553 * a) no handler is connected and class handler is not overridden (both interface and object); and
554 * b) method calls are not dispatched in a thread
556 has_handlers = g_signal_has_handler_pending (interface,
557 signals[G_AUTHORIZE_METHOD_SIGNAL],
560 has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
561 g_dbus_interface_skeleton_g_authorize_method_default);
563 emit_authorized_signal = (has_handlers || !has_default_class_handler);
564 if (!emit_authorized_signal)
567 emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object));
570 run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
571 if (!emit_authorized_signal && !run_in_thread)
573 method_call_func (g_dbus_method_invocation_get_connection (invocation),
574 g_dbus_method_invocation_get_sender (invocation),
575 g_dbus_method_invocation_get_object_path (invocation),
576 g_dbus_method_invocation_get_interface_name (invocation),
577 g_dbus_method_invocation_get_method_name (invocation),
578 g_dbus_method_invocation_get_parameters (invocation),
580 g_dbus_method_invocation_get_user_data (invocation));
585 data = g_new0 (DispatchData, 1);
586 data->interface = interface;
587 data->method_call_func = method_call_func;
588 data->invocation = invocation;
589 data->context = g_main_context_ref_thread_default ();
591 g_io_scheduler_push_job (dispatch_in_thread_func,
593 (GDestroyNotify) dispatch_data_unref,
595 NULL); /* GCancellable* */
599 g_object_unref (object);
603 skeleton_intercept_handle_method_call (GDBusConnection *connection,
605 const gchar *object_path,
606 const gchar *interface_name,
607 const gchar *method_name,
608 GVariant *parameters,
609 GDBusMethodInvocation *invocation,
612 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
613 g_dbus_interface_method_dispatch_helper (interface,
614 g_dbus_interface_skeleton_get_vtable (interface)->method_call,
618 /* ---------------------------------------------------------------------------------------------------- */
621 * g_dbus_interface_skeleton_get_connection:
622 * @interface_: A #GDBusInterfaceSkeleton.
624 * Gets the connection that @interface_ is exported on, if any.
626 * Returns: (transfer none): A #GDBusConnection or %NULL if @interface_ is
627 * not exported anywhere. Do not free, the object belongs to @interface_.
632 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
634 GDBusConnection *ret;
635 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
636 g_mutex_lock (&interface_->priv->lock);
637 ret = interface_->priv->connection;
638 g_mutex_unlock (&interface_->priv->lock);
643 * g_dbus_interface_skeleton_get_object_path:
644 * @interface_: A #GDBusInterfaceSkeleton.
646 * Gets the object path that @interface_ is exported on, if any.
648 * Returns: A string owned by @interface_ or %NULL if @interface_ is not exported
649 * anywhere. Do not free, the string belongs to @interface_.
654 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
657 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
658 g_mutex_lock (&interface_->priv->lock);
659 ret = interface_->priv->object_path;
660 g_mutex_unlock (&interface_->priv->lock);
665 * g_dbus_interface_skeleton_export:
666 * @interface_: The D-Bus interface to export.
667 * @connection: A #GDBusConnection to export @interface_ on.
668 * @object_path: The path to export the interface at.
669 * @error: Return location for error or %NULL.
671 * Exports @interface_ at @object_path on @connection.
673 * Use g_dbus_interface_skeleton_unexport() to unexport the object.
675 * Returns: %TRUE if the interface was exported, other %FALSE with
681 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton *interface_,
682 GDBusConnection *connection,
683 const gchar *object_path,
688 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), 0);
689 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
690 g_return_val_if_fail (g_variant_is_object_path (object_path), 0);
691 g_return_val_if_fail (error == NULL || *error == NULL, 0);
693 g_mutex_lock (&interface_->priv->lock);
696 if (interface_->priv->registration_id > 0)
698 g_set_error_literal (error,
700 G_IO_ERROR_FAILED, /* TODO: new error code */
701 "The object is already exported");
705 g_assert (interface_->priv->connection == NULL);
706 g_assert (interface_->priv->object_path == NULL);
707 g_assert (interface_->priv->hooked_vtable == NULL);
709 /* Hook the vtable since we need to intercept method calls for
710 * ::g-authorize-method and for dispatching in thread vs
713 interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
714 interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
716 interface_->priv->connection = g_object_ref (connection);
717 interface_->priv->object_path = g_strdup (object_path);
718 interface_->priv->registration_id = g_dbus_connection_register_object (connection,
720 g_dbus_interface_skeleton_get_info (interface_),
721 interface_->priv->hooked_vtable,
723 NULL, /* user_data_free_func */
725 if (interface_->priv->registration_id == 0)
731 g_mutex_unlock (&interface_->priv->lock);
736 * g_dbus_interface_skeleton_unexport:
737 * @interface_: A #GDBusInterfaceSkeleton.
739 * Stops exporting an interface previously exported with
740 * g_dbus_interface_skeleton_export().
745 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
747 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
748 g_return_if_fail (interface_->priv->registration_id > 0);
750 g_mutex_lock (&interface_->priv->lock);
752 g_assert (interface_->priv->connection != NULL);
753 g_assert (interface_->priv->object_path != NULL);
754 g_assert (interface_->priv->hooked_vtable != NULL);
756 g_warn_if_fail (g_dbus_connection_unregister_object (interface_->priv->connection,
757 interface_->priv->registration_id));
759 g_object_unref (interface_->priv->connection);
760 g_free (interface_->priv->object_path);
761 interface_->priv->connection = NULL;
762 interface_->priv->object_path = NULL;
763 interface_->priv->hooked_vtable = NULL;
764 interface_->priv->registration_id = 0;
766 g_mutex_unlock (&interface_->priv->lock);
769 /* ---------------------------------------------------------------------------------------------------- */