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
48 GDBusInterfaceSkeletonFlags flags;
49 guint registration_id;
51 GDBusConnection *connection;
53 GDBusInterfaceVTable *hooked_vtable;
58 G_AUTHORIZE_METHOD_SIGNAL,
68 static guint signals[LAST_SIGNAL] = {0};
70 static void dbus_interface_interface_init (GDBusInterfaceIface *iface);
72 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GDBusInterfaceSkeleton, g_dbus_interface_skeleton, G_TYPE_OBJECT,
73 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_interface_init));
76 g_dbus_interface_skeleton_finalize (GObject *object)
78 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
79 /* unexport if already exported */
80 if (interface->priv->registration_id > 0)
81 g_dbus_interface_skeleton_unexport (interface);
83 g_assert (interface->priv->connection == NULL);
84 g_assert (interface->priv->object_path == NULL);
85 g_assert (interface->priv->hooked_vtable == NULL);
87 if (interface->priv->object != NULL)
88 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
89 G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
93 g_dbus_interface_skeleton_get_property (GObject *object,
98 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
103 g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 g_dbus_interface_skeleton_set_property (GObject *object,
118 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
123 g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton *interface,
134 GDBusMethodInvocation *invocation)
140 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
142 GObjectClass *gobject_class;
144 gobject_class = G_OBJECT_CLASS (klass);
145 gobject_class->finalize = g_dbus_interface_skeleton_finalize;
146 gobject_class->set_property = g_dbus_interface_skeleton_set_property;
147 gobject_class->get_property = g_dbus_interface_skeleton_get_property;
149 klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
152 * GDBusInterfaceSkeleton:g-flags:
154 * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
158 g_object_class_install_property (gobject_class,
160 g_param_spec_flags ("g-flags",
162 "Flags for the interface skeleton",
163 G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS,
164 G_DBUS_INTERFACE_SKELETON_FLAGS_NONE,
167 G_PARAM_STATIC_STRINGS));
170 * GDBusInterfaceSkeleton::g-authorize-method:
171 * @interface: The #GDBusInterfaceSkeleton emitting the signal.
172 * @invocation: A #GDBusMethodInvocation.
174 * Emitted when a method is invoked by a remote caller and used to
175 * determine if the method call is authorized.
177 * Note that this signal is emitted in a thread dedicated to
178 * handling the method call so handlers are allowed to perform
179 * blocking IO. This means that it is appropriate to call
181 * url="http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#polkit-authority-check-authorization-sync">polkit_authority_check_authorization_sync()</ulink>
183 * 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.
185 * If %FALSE is returned then no further handlers are run and the
186 * signal handler must take a reference to @invocation and finish
187 * handling the call (e.g. return an error via
188 * g_dbus_method_invocation_return_error()).
190 * Otherwise, if %TRUE is returned, signal emission continues. If no
191 * handlers return %FALSE, then the method is dispatched. If
192 * @interface has an enclosing #GDBusObjectSkeleton, then the
193 * #GDBusObjectSkeleton::authorize-method signal handlers run before
194 * the handlers for this signal.
196 * The default class handler just returns %TRUE.
198 * Please note that the common case is optimized: if no signals
199 * handlers are connected and the default class handler isn't
200 * overridden (for both @interface and the enclosing
201 * #GDBusObjectSkeleton, if any) and #GDBusInterfaceSkeleton:g-flags does
203 * %G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD
204 * flags set, no dedicated thread is ever used and the call will be
205 * handled in the same thread as the object that @interface belongs
206 * to was exported in.
208 * Returns: %TRUE if the call is authorized, %FALSE otherwise.
212 signals[G_AUTHORIZE_METHOD_SIGNAL] =
213 g_signal_new ("g-authorize-method",
214 G_TYPE_DBUS_INTERFACE_SKELETON,
216 G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method),
217 _g_signal_accumulator_false_handled,
222 G_TYPE_DBUS_METHOD_INVOCATION);
224 g_type_class_add_private (klass, sizeof (GDBusInterfaceSkeletonPrivate));
228 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
230 interface->priv = G_TYPE_INSTANCE_GET_PRIVATE (interface, G_TYPE_DBUS_INTERFACE_SKELETON, GDBusInterfaceSkeletonPrivate);
233 /* ---------------------------------------------------------------------------------------------------- */
236 * g_dbus_interface_skeleton_get_flags:
237 * @interface_: A #GDBusInterfaceSkeleton.
239 * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
242 * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
246 GDBusInterfaceSkeletonFlags
247 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton *interface_)
249 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
250 return interface_->priv->flags;
254 * g_dbus_interface_skeleton_set_flags:
255 * @interface_: A #GDBusInterfaceSkeleton.
256 * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
258 * Sets flags describing what the behavior of @skeleton should be.
263 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton *interface_,
264 GDBusInterfaceSkeletonFlags flags)
266 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
267 if (interface_->priv->flags != flags)
269 interface_->priv->flags = flags;
270 g_object_notify (G_OBJECT (interface_), "g-flags");
275 * g_dbus_interface_skeleton_get_info:
276 * @interface_: A #GDBusInterfaceSkeleton.
278 * Gets D-Bus introspection information for the D-Bus interface
279 * implemented by @interface_.
281 * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
286 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
288 GDBusInterfaceInfo *ret;
289 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
290 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_info (interface_);
291 g_warn_if_fail (ret != NULL);
296 * g_dbus_interface_skeleton_get_vtable: (skip)
297 * @interface_: A #GDBusInterfaceSkeleton.
299 * Gets the interface vtable for the D-Bus interface implemented by
300 * @interface_. The returned function pointers should expect @interface_
301 * itself to be passed as @user_data.
303 * Returns: A #GDBusInterfaceVTable (never %NULL).
307 GDBusInterfaceVTable *
308 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
310 GDBusInterfaceVTable *ret;
311 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
312 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_vtable (interface_);
313 g_warn_if_fail (ret != NULL);
318 * g_dbus_interface_skeleton_get_properties:
319 * @interface_: A #GDBusInterfaceSkeleton.
321 * Gets all D-Bus properties for @interface_.
323 * Returns: (transfer full): A #GVariant of type <link linkend="G-VARIANT-TYPE-VARDICT:CAPS">'a{sv}'</link>. Free with g_variant_unref().
328 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
331 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
332 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_);
333 return g_variant_take_ref (ret);
337 * g_dbus_interface_skeleton_flush:
338 * @interface_: A #GDBusInterfaceSkeleton.
340 * If @interface_ has outstanding changes, request for these changes to be
341 * emitted immediately.
343 * For example, an exported D-Bus interface may queue up property
344 * changes and emit the
345 * <literal>org.freedesktop.DBus.Properties::PropertiesChanged</literal>
346 * signal later (e.g. in an idle handler). This technique is useful
347 * for collapsing multiple property changes into one.
352 g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
354 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
355 G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
358 /* ---------------------------------------------------------------------------------------------------- */
360 static GDBusInterfaceInfo *
361 _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
363 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
364 return g_dbus_interface_skeleton_get_info (interface);
368 g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
370 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
371 return interface->priv->object;
375 g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
378 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
379 if (interface->priv->object != NULL)
380 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
381 interface->priv->object = object;
383 g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
387 dbus_interface_interface_init (GDBusInterfaceIface *iface)
389 iface->get_info = _g_dbus_interface_skeleton_get_info;
390 iface->get_object = g_dbus_interface_skeleton_get_object;
391 iface->set_object = g_dbus_interface_skeleton_set_object;
394 /* ---------------------------------------------------------------------------------------------------- */
398 volatile gint ref_count;
399 GDBusInterfaceSkeleton *interface;
400 GDBusInterfaceMethodCallFunc method_call_func;
401 GDBusMethodInvocation *invocation;
402 GMainContext *context;
406 dispatch_data_unref (DispatchData *data)
408 if (g_atomic_int_dec_and_test (&data->ref_count))
410 if (data->context != NULL)
411 g_main_context_unref (data->context);
416 static DispatchData *
417 dispatch_data_ref (DispatchData *data)
419 g_atomic_int_inc (&data->ref_count);
424 dispatch_invoke_in_context_func (gpointer user_data)
426 DispatchData *data = user_data;
427 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
428 g_dbus_method_invocation_get_sender (data->invocation),
429 g_dbus_method_invocation_get_object_path (data->invocation),
430 g_dbus_method_invocation_get_interface_name (data->invocation),
431 g_dbus_method_invocation_get_method_name (data->invocation),
432 g_dbus_method_invocation_get_parameters (data->invocation),
434 g_dbus_method_invocation_get_user_data (data->invocation));
439 dispatch_in_thread_func (GIOSchedulerJob *job,
440 GCancellable *cancellable,
443 DispatchData *data = user_data;
446 /* first check on the enclosing object (if any), then the interface */
448 if (data->interface->priv->object != NULL)
450 g_signal_emit_by_name (data->interface->priv->object,
458 g_signal_emit (data->interface,
459 signals[G_AUTHORIZE_METHOD_SIGNAL],
467 gboolean run_in_thread;
468 run_in_thread = (data->interface->priv->flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
471 /* might as well just re-use the existing thread */
472 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
473 g_dbus_method_invocation_get_sender (data->invocation),
474 g_dbus_method_invocation_get_object_path (data->invocation),
475 g_dbus_method_invocation_get_interface_name (data->invocation),
476 g_dbus_method_invocation_get_method_name (data->invocation),
477 g_dbus_method_invocation_get_parameters (data->invocation),
479 g_dbus_method_invocation_get_user_data (data->invocation));
483 /* bah, back to original context */
484 g_main_context_invoke_full (data->context,
486 dispatch_invoke_in_context_func,
487 dispatch_data_ref (data),
488 (GDestroyNotify) dispatch_data_unref);
500 g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton *interface,
501 GDBusInterfaceMethodCallFunc method_call_func,
502 GDBusMethodInvocation *invocation)
504 gboolean has_handlers;
505 gboolean has_default_class_handler;
506 gboolean emit_authorized_signal;
507 gboolean run_in_thread;
509 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
510 g_return_if_fail (method_call_func != NULL);
511 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
513 /* optimization for the common case where
515 * a) no handler is connected and class handler is not overridden (both interface and object); and
516 * b) method calls are not dispatched in a thread
518 has_handlers = g_signal_has_handler_pending (interface,
519 signals[G_AUTHORIZE_METHOD_SIGNAL],
522 has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
523 g_dbus_interface_skeleton_g_authorize_method_default);
525 emit_authorized_signal = (has_handlers || !has_default_class_handler);
526 if (!emit_authorized_signal)
528 if (interface->priv->object != NULL)
529 emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (interface->priv->object));
532 run_in_thread = (interface->priv->flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
533 if (!emit_authorized_signal && !run_in_thread)
535 method_call_func (g_dbus_method_invocation_get_connection (invocation),
536 g_dbus_method_invocation_get_sender (invocation),
537 g_dbus_method_invocation_get_object_path (invocation),
538 g_dbus_method_invocation_get_interface_name (invocation),
539 g_dbus_method_invocation_get_method_name (invocation),
540 g_dbus_method_invocation_get_parameters (invocation),
542 g_dbus_method_invocation_get_user_data (invocation));
547 data = g_new0 (DispatchData, 1);
548 data->interface = interface;
549 data->method_call_func = method_call_func;
550 data->invocation = invocation;
551 data->context = g_main_context_get_thread_default ();
553 if (data->context != NULL)
554 g_main_context_ref (data->context);
555 g_io_scheduler_push_job (dispatch_in_thread_func,
557 (GDestroyNotify) dispatch_data_unref,
559 NULL); /* GCancellable* */
564 skeleton_intercept_handle_method_call (GDBusConnection *connection,
566 const gchar *object_path,
567 const gchar *interface_name,
568 const gchar *method_name,
569 GVariant *parameters,
570 GDBusMethodInvocation *invocation,
573 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
574 g_dbus_interface_method_dispatch_helper (interface,
575 g_dbus_interface_skeleton_get_vtable (interface)->method_call,
579 /* ---------------------------------------------------------------------------------------------------- */
582 * g_dbus_interface_skeleton_get_connection:
583 * @interface_: A #GDBusInterfaceSkeleton.
585 * Gets the connection that @interface_ is exported on, if any.
587 * Returns: (transfer none): A #GDBusConnection or %NULL if @interface_ is
588 * not exported anywhere. Do not free, the object belongs to @interface_.
593 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
595 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
596 return interface_->priv->connection;
600 * g_dbus_interface_skeleton_get_object_path:
601 * @interface_: A #GDBusInterfaceSkeleton.
603 * Gets the object path that @interface_ is exported on, if any.
605 * Returns: A string owned by @interface_ or %NULL if @interface_ is not exported
606 * anywhere. Do not free, the string belongs to @interface_.
611 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
613 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
614 return interface_->priv->object_path;
618 * g_dbus_interface_skeleton_export:
619 * @interface_: The D-Bus interface to export.
620 * @connection: A #GDBusConnection to export @interface_ on.
621 * @object_path: The path to export the interface at.
622 * @error: Return location for error or %NULL.
624 * Exports @interface_ at @object_path on @connection.
626 * Use g_dbus_interface_skeleton_unexport() to unexport the object.
628 * Returns: %TRUE if the interface was exported, other %FALSE with
634 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton *interface_,
635 GDBusConnection *connection,
636 const gchar *object_path,
641 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), 0);
642 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
643 g_return_val_if_fail (g_variant_is_object_path (object_path), 0);
644 g_return_val_if_fail (error == NULL || *error == NULL, 0);
647 if (interface_->priv->registration_id > 0)
649 g_set_error_literal (error,
651 G_IO_ERROR_FAILED, /* TODO: new error code */
652 "The object is already exported");
656 g_assert (interface_->priv->connection == NULL);
657 g_assert (interface_->priv->object_path == NULL);
658 g_assert (interface_->priv->hooked_vtable == NULL);
660 /* Hook the vtable since we need to intercept method calls for
661 * ::g-authorize-method and for dispatching in thread vs
664 interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
665 interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
667 interface_->priv->connection = g_object_ref (connection);
668 interface_->priv->object_path = g_strdup (object_path);
669 interface_->priv->registration_id = g_dbus_connection_register_object (connection,
671 g_dbus_interface_skeleton_get_info (interface_),
672 interface_->priv->hooked_vtable,
674 NULL, /* user_data_free_func */
676 if (interface_->priv->registration_id == 0)
686 * g_dbus_interface_skeleton_unexport:
687 * @interface_: A #GDBusInterfaceSkeleton.
689 * Stops exporting an interface previously exported with
690 * g_dbus_interface_skeleton_export().
695 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
697 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
698 g_return_if_fail (interface_->priv->registration_id > 0);
700 g_assert (interface_->priv->connection != NULL);
701 g_assert (interface_->priv->object_path != NULL);
702 g_assert (interface_->priv->hooked_vtable != NULL);
704 g_warn_if_fail (g_dbus_connection_unregister_object (interface_->priv->connection,
705 interface_->priv->registration_id));
707 g_object_unref (interface_->priv->connection);
708 g_free (interface_->priv->object_path);
709 interface_->priv->connection = NULL;
710 interface_->priv->object_path = NULL;
711 interface_->priv->hooked_vtable = NULL;
712 interface_->priv->registration_id = 0;
715 /* ---------------------------------------------------------------------------------------------------- */