Fix getauxval error at qemu
[platform/upstream/glib.git] / gio / gdbusinterfaceskeleton.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
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"
33 #include "gtask.h"
34 #include "gioerror.h"
35
36 #include "glibintl.h"
37
38 /**
39  * SECTION:gdbusinterfaceskeleton
40  * @short_description: Service-side D-Bus interface
41  * @include: gio/gio.h
42  *
43  * Abstract base class for D-Bus interfaces on the service side.
44  */
45
46 struct _GDBusInterfaceSkeletonPrivate
47 {
48   GMutex                      lock;
49
50   GDBusObject                *object;
51   GDBusInterfaceSkeletonFlags flags;
52
53   GSList                     *connections;   /* List of ConnectionData */
54   gchar                      *object_path;   /* The object path for this skeleton */
55   GDBusInterfaceVTable       *hooked_vtable;
56 };
57
58 typedef struct
59 {
60   GDBusConnection *connection;
61   guint            registration_id;
62 } ConnectionData;
63
64 enum
65 {
66   G_AUTHORIZE_METHOD_SIGNAL,
67   LAST_SIGNAL
68 };
69
70 enum
71 {
72   PROP_0,
73   PROP_G_FLAGS
74 };
75
76 static guint signals[LAST_SIGNAL] = {0};
77
78 static void     dbus_interface_interface_init                      (GDBusInterfaceIface    *iface);
79
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,
85                                                                     const gchar            *sender,
86                                                                     const gchar            *object_path,
87                                                                     const gchar            *interface_name,
88                                                                     const gchar            *method_name,
89                                                                     GVariant               *parameters,
90                                                                     GDBusMethodInvocation  *invocation,
91                                                                     gpointer                user_data);
92
93
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))
97
98 static void
99 g_dbus_interface_skeleton_finalize (GObject *object)
100 {
101   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
102
103   /* Hold the lock just in case any code we call verifies that the lock is held */
104   g_mutex_lock (&interface->priv->lock);
105
106   /* unexport from all connections if we're exported anywhere */
107   while (interface->priv->connections != NULL)
108     {
109       ConnectionData *data = interface->priv->connections->data;
110       remove_connection_locked (interface, data->connection);
111     }
112
113   set_object_path_locked (interface, NULL);
114
115   g_mutex_unlock (&interface->priv->lock);
116
117   g_free (interface->priv->hooked_vtable);
118
119   if (interface->priv->object != NULL)
120     g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
121
122   g_mutex_clear (&interface->priv->lock);
123
124   G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
125 }
126
127 static void
128 g_dbus_interface_skeleton_get_property (GObject      *object,
129                                         guint         prop_id,
130                                         GValue       *value,
131                                         GParamSpec   *pspec)
132 {
133   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
134
135   switch (prop_id)
136     {
137     case PROP_G_FLAGS:
138       g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
139       break;
140
141     default:
142       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143       break;
144     }
145 }
146
147 static void
148 g_dbus_interface_skeleton_set_property (GObject      *object,
149                                         guint         prop_id,
150                                         const GValue *value,
151                                         GParamSpec   *pspec)
152 {
153   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
154
155   switch (prop_id)
156     {
157     case PROP_G_FLAGS:
158       g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
159       break;
160
161     default:
162       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163       break;
164     }
165 }
166
167 static gboolean
168 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton    *interface,
169                                                       GDBusMethodInvocation *invocation)
170 {
171   return TRUE;
172 }
173
174 static void
175 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
176 {
177   GObjectClass *gobject_class;
178
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;
183
184   klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
185
186   /**
187    * GDBusInterfaceSkeleton:g-flags:
188    *
189    * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
190    *
191    * Since: 2.30
192    */
193   g_object_class_install_property (gobject_class,
194                                    PROP_G_FLAGS,
195                                    g_param_spec_flags ("g-flags",
196                                                        "g-flags",
197                                                        "Flags for the interface skeleton",
198                                                        G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS,
199                                                        G_DBUS_INTERFACE_SKELETON_FLAGS_NONE,
200                                                        G_PARAM_READABLE |
201                                                        G_PARAM_WRITABLE |
202                                                        G_PARAM_STATIC_STRINGS));
203
204   /**
205    * GDBusInterfaceSkeleton::g-authorize-method:
206    * @interface: The #GDBusInterfaceSkeleton emitting the signal.
207    * @invocation: A #GDBusMethodInvocation.
208    *
209    * Emitted when a method is invoked by a remote caller and used to
210    * determine if the method call is authorized.
211    *
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)
216    * with the
217    * [POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#POLKIT-CHECK-AUTHORIZATION-FLAGS-ALLOW-USER-INTERACTION:CAPS)
218    * flag set.
219    *
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()).
224    *
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.
230    *
231    * The default class handler just returns %TRUE.
232    *
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
237    * not have the
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.
242    *
243    * Returns: %TRUE if the call is authorized, %FALSE otherwise.
244    *
245    * Since: 2.30
246    */
247   signals[G_AUTHORIZE_METHOD_SIGNAL] =
248     g_signal_new (I_("g-authorize-method"),
249                   G_TYPE_DBUS_INTERFACE_SKELETON,
250                   G_SIGNAL_RUN_LAST,
251                   G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method),
252                   _g_signal_accumulator_false_handled,
253                   NULL,
254                   _g_cclosure_marshal_BOOLEAN__OBJECT,
255                   G_TYPE_BOOLEAN,
256                   1,
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);
261 }
262
263 static void
264 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
265 {
266   interface->priv = g_dbus_interface_skeleton_get_instance_private (interface);
267   g_mutex_init (&interface->priv->lock);
268 }
269
270 /* ---------------------------------------------------------------------------------------------------- */
271
272 /**
273  * g_dbus_interface_skeleton_get_flags:
274  * @interface_: A #GDBusInterfaceSkeleton.
275  *
276  * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
277  * of @interface_
278  *
279  * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
280  *
281  * Since: 2.30
282  */
283 GDBusInterfaceSkeletonFlags
284 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton  *interface_)
285 {
286   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
287   return interface_->priv->flags;
288 }
289
290 /**
291  * g_dbus_interface_skeleton_set_flags:
292  * @interface_: A #GDBusInterfaceSkeleton.
293  * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
294  *
295  * Sets flags describing what the behavior of @skeleton should be.
296  *
297  * Since: 2.30
298  */
299 void
300 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton      *interface_,
301                                      GDBusInterfaceSkeletonFlags  flags)
302 {
303   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
304   g_mutex_lock (&interface_->priv->lock);
305   if (interface_->priv->flags != flags)
306     {
307       interface_->priv->flags = flags;
308       g_mutex_unlock (&interface_->priv->lock);
309       g_object_notify (G_OBJECT (interface_), "g-flags");
310     }
311   else
312     {
313       g_mutex_unlock (&interface_->priv->lock);
314     }
315 }
316
317 /**
318  * g_dbus_interface_skeleton_get_info:
319  * @interface_: A #GDBusInterfaceSkeleton.
320  *
321  * Gets D-Bus introspection information for the D-Bus interface
322  * implemented by @interface_.
323  *
324  * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
325  *
326  * Since: 2.30
327  */
328 GDBusInterfaceInfo *
329 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
330 {
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);
335   return ret;
336 }
337
338 /**
339  * g_dbus_interface_skeleton_get_vtable:
340  * @interface_: A #GDBusInterfaceSkeleton.
341  *
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.
345  *
346  * Returns: (not nullable) (transfer none): the vtable of the D-Bus interface implemented by the skeleton
347  *
348  * Since: 2.30
349  */
350 GDBusInterfaceVTable *
351 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
352 {
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);
357   return ret;
358 }
359
360 /**
361  * g_dbus_interface_skeleton_get_properties:
362  * @interface_: A #GDBusInterfaceSkeleton.
363  *
364  * Gets all D-Bus properties for @interface_.
365  *
366  * Returns: (transfer full): A #GVariant of type
367  * ['a{sv}'][G-VARIANT-TYPE-VARDICT:CAPS].
368  * Free with g_variant_unref().
369  *
370  * Since: 2.30
371  */
372 GVariant *
373 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
374 {
375   GVariant *ret;
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);
379 }
380
381 /**
382  * g_dbus_interface_skeleton_flush:
383  * @interface_: A #GDBusInterfaceSkeleton.
384  *
385  * If @interface_ has outstanding changes, request for these changes to be
386  * emitted immediately.
387  *
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.
393  *
394  * Since: 2.30
395  */
396 void
397 g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
398 {
399   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
400   G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
401 }
402
403 /* ---------------------------------------------------------------------------------------------------- */
404
405 static GDBusInterfaceInfo *
406 _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
407 {
408   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
409   return g_dbus_interface_skeleton_get_info (interface);
410 }
411
412 static GDBusObject *
413 g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
414 {
415   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
416   GDBusObject *ret;
417   g_mutex_lock (&interface->priv->lock);
418   ret = interface->priv->object;
419   g_mutex_unlock (&interface->priv->lock);
420   return ret;
421 }
422
423 static GDBusObject *
424 g_dbus_interface_skeleton_dup_object (GDBusInterface *interface_)
425 {
426   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
427   GDBusObject *ret;
428   g_mutex_lock (&interface->priv->lock);
429   ret = interface->priv->object;
430   if (ret != NULL)
431     g_object_ref (ret);
432   g_mutex_unlock (&interface->priv->lock);
433   return ret;
434 }
435
436 static void
437 g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
438                                       GDBusObject    *object)
439 {
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;
445   if (object != NULL)
446     g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
447   g_mutex_unlock (&interface->priv->lock);
448 }
449
450 static void
451 dbus_interface_interface_init (GDBusInterfaceIface *iface)
452 {
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;
457 }
458
459 /* ---------------------------------------------------------------------------------------------------- */
460
461 typedef struct
462 {
463   gint ref_count;  /* (atomic) */
464   GDBusInterfaceMethodCallFunc  method_call_func;
465   GDBusMethodInvocation        *invocation;  /* (owned) */
466 } DispatchData;
467
468 static void
469 dispatch_data_unref (DispatchData *data)
470 {
471   if (g_atomic_int_dec_and_test (&data->ref_count))
472     {
473       g_clear_object (&data->invocation);
474       g_slice_free (DispatchData, data);
475     }
476 }
477
478 static DispatchData *
479 dispatch_data_ref (DispatchData *data)
480 {
481   g_atomic_int_inc (&data->ref_count);
482   return data;
483 }
484
485 static gboolean
486 dispatch_invoke_in_context_func (gpointer user_data)
487 {
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),
495                           data->invocation,
496                           g_dbus_method_invocation_get_user_data (data->invocation));
497   return FALSE;
498 }
499
500 static void
501 dispatch_in_thread_func (GTask        *task,
502                          gpointer      source_object,
503                          gpointer      task_data,
504                          GCancellable *cancellable)
505 {
506   DispatchData *data = task_data;
507   GDBusInterfaceSkeleton *interface = g_task_get_source_object (task);
508   GDBusInterfaceSkeletonFlags flags;
509   GDBusObject *object;
510   gboolean authorized;
511
512   g_mutex_lock (&interface->priv->lock);
513   flags = interface->priv->flags;
514   object = interface->priv->object;
515   if (object != NULL)
516     g_object_ref (object);
517   g_mutex_unlock (&interface->priv->lock);
518
519   /* first check on the enclosing object (if any), then the interface */
520   authorized = TRUE;
521   if (object != NULL)
522     {
523       g_signal_emit_by_name (object,
524                              "authorize-method",
525                              interface,
526                              data->invocation,
527                              &authorized);
528     }
529   if (authorized)
530     {
531       g_signal_emit (interface,
532                      signals[G_AUTHORIZE_METHOD_SIGNAL],
533                      0,
534                      data->invocation,
535                      &authorized);
536     }
537
538   if (authorized)
539     {
540       gboolean run_in_thread;
541       run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
542       if (run_in_thread)
543         {
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),
551                                   data->invocation,
552                                   g_dbus_method_invocation_get_user_data (data->invocation));
553         }
554       else
555         {
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);
562         }
563     }
564   else
565     {
566       /* do nothing */
567     }
568
569   if (object != NULL)
570     g_object_unref (object);
571
572   g_task_return_boolean (task, TRUE);
573 }
574
575 static void
576 g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton       *interface,
577                                          GDBusInterfaceMethodCallFunc  method_call_func,
578                                          GDBusMethodInvocation        *invocation)
579 {
580   gboolean has_handlers;
581   gboolean has_default_class_handler;
582   gboolean emit_authorized_signal;
583   gboolean run_in_thread;
584   GDBusInterfaceSkeletonFlags flags;
585   GDBusObject *object;
586
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));
590
591   g_mutex_lock (&interface->priv->lock);
592   flags = interface->priv->flags;
593   object = interface->priv->object;
594   if (object != NULL)
595     g_object_ref (object);
596   g_mutex_unlock (&interface->priv->lock);
597
598   /* optimization for the common case where
599    *
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
602    */
603   has_handlers = g_signal_has_handler_pending (interface,
604                                                signals[G_AUTHORIZE_METHOD_SIGNAL],
605                                                0,
606                                                TRUE);
607   has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
608                                g_dbus_interface_skeleton_g_authorize_method_default);
609
610   emit_authorized_signal = (has_handlers || !has_default_class_handler);
611   if (!emit_authorized_signal)
612     {
613       if (object != NULL)
614         emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object));
615     }
616
617   run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
618   if (!emit_authorized_signal && !run_in_thread)
619     {
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),
626                         invocation,
627                         g_dbus_method_invocation_get_user_data (invocation));
628     }
629   else
630     {
631       GTask *task;
632       DispatchData *data;
633
634       data = g_slice_new0 (DispatchData);
635       data->method_call_func = method_call_func;
636       data->invocation = g_object_ref (invocation);
637       data->ref_count = 1;
638
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);
645     }
646
647   if (object != NULL)
648     g_object_unref (object);
649 }
650
651 static void
652 skeleton_intercept_handle_method_call (GDBusConnection       *connection,
653                                        const gchar           *sender,
654                                        const gchar           *object_path,
655                                        const gchar           *interface_name,
656                                        const gchar           *method_name,
657                                        GVariant              *parameters,
658                                        GDBusMethodInvocation *invocation,
659                                        gpointer               user_data)
660 {
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,
664                                            invocation);
665 }
666
667 /* ---------------------------------------------------------------------------------------------------- */
668
669 static ConnectionData *
670 new_connection (GDBusConnection *connection,
671                 guint            registration_id)
672 {
673   ConnectionData *data;
674
675   data = g_slice_new0 (ConnectionData);
676   data->connection      = g_object_ref (connection);
677   data->registration_id = registration_id;
678
679   return data;
680 }
681
682 static void
683 free_connection (ConnectionData *data)
684 {
685   if (data != NULL)
686     {
687       g_object_unref (data->connection);
688       g_slice_free (ConnectionData, data);
689     }
690 }
691
692 static gboolean
693 add_connection_locked (GDBusInterfaceSkeleton *interface_,
694                        GDBusConnection        *connection,
695                        GError                **error)
696 {
697   ConnectionData *data;
698   guint registration_id;
699   gboolean ret = FALSE;
700
701   if (interface_->priv->hooked_vtable == NULL)
702     {
703       /* Hook the vtable since we need to intercept method calls for
704        * ::g-authorize-method and for dispatching in thread vs
705        * context
706        *
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.
710        */
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;
713     }
714
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,
719                                                        interface_,
720                                                        NULL, /* user_data_free_func */
721                                                        error);
722
723   if (registration_id > 0)
724     {
725       data = new_connection (connection, registration_id);
726       interface_->priv->connections = g_slist_append (interface_->priv->connections, data);
727       ret = TRUE;
728     }
729
730   return ret;
731 }
732
733 static void
734 remove_connection_locked (GDBusInterfaceSkeleton *interface_,
735                           GDBusConnection        *connection)
736 {
737   ConnectionData *data;
738   GSList *l;
739
740   /* Get the connection in the list and unregister ... */
741   for (l = interface_->priv->connections; l != NULL; l = l->next)
742     {
743       data = l->data;
744       if (data->connection == connection)
745         {
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 */
750           goto out;
751         }
752     }
753  out:
754   ;
755 }
756
757 static void
758 set_object_path_locked (GDBusInterfaceSkeleton *interface_,
759                         const gchar            *object_path)
760 {
761   if (g_strcmp0 (interface_->priv->object_path, object_path) != 0)
762     {
763       g_free (interface_->priv->object_path);
764       interface_->priv->object_path = g_strdup (object_path);
765     }
766 }
767
768 /* ---------------------------------------------------------------------------------------------------- */
769
770 /**
771  * g_dbus_interface_skeleton_get_connection:
772  * @interface_: A #GDBusInterfaceSkeleton.
773  *
774  * Gets the first connection that @interface_ is exported on, if any.
775  *
776  * Returns: (nullable) (transfer none): A #GDBusConnection or %NULL if @interface_ is
777  * not exported anywhere. Do not free, the object belongs to @interface_.
778  *
779  * Since: 2.30
780  */
781 GDBusConnection *
782 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
783 {
784   ConnectionData  *data;
785   GDBusConnection *ret;
786
787   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
788   g_mutex_lock (&interface_->priv->lock);
789
790   ret = NULL;
791   if (interface_->priv->connections != NULL)
792     {
793       data = interface_->priv->connections->data;
794       if (data != NULL)
795         ret = data->connection;
796     }
797
798   g_mutex_unlock (&interface_->priv->lock);
799
800   return ret;
801 }
802
803 /**
804  * g_dbus_interface_skeleton_get_connections:
805  * @interface_: A #GDBusInterfaceSkeleton.
806  *
807  * Gets a list of the connections that @interface_ is exported on.
808  *
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().
813  *
814  * Since: 2.32
815  */
816 GList *
817 g_dbus_interface_skeleton_get_connections (GDBusInterfaceSkeleton *interface_)
818 {
819   GList           *connections;
820   GSList          *l;
821   ConnectionData  *data;
822
823   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
824
825   g_mutex_lock (&interface_->priv->lock);
826   connections = NULL;
827
828   for (l = interface_->priv->connections; l != NULL; l = l->next)
829     {
830       data        = l->data;
831       connections = g_list_prepend (connections,
832                                     /* Return a reference to each connection */
833                                     g_object_ref (data->connection));
834     }
835
836   g_mutex_unlock (&interface_->priv->lock);
837
838   return g_list_reverse (connections);
839 }
840
841 /**
842  * g_dbus_interface_skeleton_has_connection:
843  * @interface_: A #GDBusInterfaceSkeleton.
844  * @connection: A #GDBusConnection.
845  *
846  * Checks if @interface_ is exported on @connection.
847  *
848  * Returns: %TRUE if @interface_ is exported on @connection, %FALSE otherwise.
849  *
850  * Since: 2.32
851  */
852 gboolean
853 g_dbus_interface_skeleton_has_connection (GDBusInterfaceSkeleton     *interface_,
854                                           GDBusConnection            *connection)
855 {
856   GSList *l;
857   gboolean ret = FALSE;
858
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);
861
862   g_mutex_lock (&interface_->priv->lock);
863
864   for (l = interface_->priv->connections; l != NULL; l = l->next)
865     {
866       ConnectionData *data = l->data;
867       if (data->connection == connection)
868         {
869           ret = TRUE;
870           goto out;
871         }
872     }
873
874  out:
875   g_mutex_unlock (&interface_->priv->lock);
876   return ret;
877 }
878
879 /**
880  * g_dbus_interface_skeleton_get_object_path:
881  * @interface_: A #GDBusInterfaceSkeleton.
882  *
883  * Gets the object path that @interface_ is exported on, if any.
884  *
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_.
887  *
888  * Since: 2.30
889  */
890 const gchar *
891 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
892 {
893   const gchar *ret;
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);
898   return ret;
899 }
900
901 /**
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.
907  *
908  * Exports @interface_ at @object_path on @connection.
909  *
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.
913  *
914  * Use g_dbus_interface_skeleton_unexport() to unexport the object.
915  *
916  * Returns: %TRUE if the interface was exported on @connection, otherwise %FALSE with
917  * @error set.
918  *
919  * Since: 2.30
920  */
921 gboolean
922 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton  *interface_,
923                                   GDBusConnection         *connection,
924                                   const gchar             *object_path,
925                                   GError                 **error)
926 {
927   gboolean ret = FALSE;
928
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);
933
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);
937
938   g_mutex_lock (&interface_->priv->lock);
939
940   /* Set the object path */
941   set_object_path_locked (interface_, object_path);
942
943   /* Add the connection */
944   ret = add_connection_locked (interface_, connection, error);
945
946   g_mutex_unlock (&interface_->priv->lock);
947   return ret;
948 }
949
950 /**
951  * g_dbus_interface_skeleton_unexport:
952  * @interface_: A #GDBusInterfaceSkeleton.
953  *
954  * Stops exporting @interface_ on all connections it is exported on.
955  *
956  * To unexport @interface_ from only a single connection, use
957  * g_dbus_interface_skeleton_unexport_from_connection()
958  *
959  * Since: 2.30
960  */
961 void
962 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
963 {
964   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
965   g_return_if_fail (interface_->priv->connections != NULL);
966
967   g_mutex_lock (&interface_->priv->lock);
968
969   g_assert (interface_->priv->object_path != NULL);
970   g_assert (interface_->priv->hooked_vtable != NULL);
971
972   /* Remove all connections */
973   while (interface_->priv->connections != NULL)
974     {
975       ConnectionData *data = interface_->priv->connections->data;
976       remove_connection_locked (interface_, data->connection);
977     }
978
979   /* Unset the object path since there are no connections left */
980   set_object_path_locked (interface_, NULL);
981
982   g_mutex_unlock (&interface_->priv->lock);
983 }
984
985
986 /**
987  * g_dbus_interface_skeleton_unexport_from_connection:
988  * @interface_: A #GDBusInterfaceSkeleton.
989  * @connection: A #GDBusConnection.
990  *
991  * Stops exporting @interface_ on @connection.
992  *
993  * To stop exporting on all connections the interface is exported on,
994  * use g_dbus_interface_skeleton_unexport().
995  *
996  * Since: 2.32
997  */
998 void
999 g_dbus_interface_skeleton_unexport_from_connection (GDBusInterfaceSkeleton *interface_,
1000                                                     GDBusConnection        *connection)
1001 {
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);
1005
1006   g_mutex_lock (&interface_->priv->lock);
1007
1008   g_assert (interface_->priv->object_path != NULL);
1009   g_assert (interface_->priv->hooked_vtable != NULL);
1010
1011   remove_connection_locked (interface_, connection);
1012
1013   /* Reset the object path if we removed the last connection */
1014   if (interface_->priv->connections == NULL)
1015     set_object_path_locked (interface_, NULL);
1016
1017   g_mutex_unlock (&interface_->priv->lock);
1018 }
1019
1020 /* ---------------------------------------------------------------------------------------------------- */