Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gdbusinterfaceskeleton.c
Line
Count
Source (jump to first uncovered line)
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
0
{
101
0
  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
0
  g_mutex_lock (&interface->priv->lock);
105
106
  /* unexport from all connections if we're exported anywhere */
107
0
  while (interface->priv->connections != NULL)
108
0
    {
109
0
      ConnectionData *data = interface->priv->connections->data;
110
0
      remove_connection_locked (interface, data->connection);
111
0
    }
112
113
0
  set_object_path_locked (interface, NULL);
114
115
0
  g_mutex_unlock (&interface->priv->lock);
116
117
0
  g_free (interface->priv->hooked_vtable);
118
119
0
  if (interface->priv->object != NULL)
120
0
    g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
121
122
0
  g_mutex_clear (&interface->priv->lock);
123
124
0
  G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
125
0
}
126
127
static void
128
g_dbus_interface_skeleton_get_property (GObject      *object,
129
                                        guint         prop_id,
130
                                        GValue       *value,
131
                                        GParamSpec   *pspec)
132
0
{
133
0
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
134
135
0
  switch (prop_id)
136
0
    {
137
0
    case PROP_G_FLAGS:
138
0
      g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
139
0
      break;
140
141
0
    default:
142
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143
0
      break;
144
0
    }
145
0
}
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
0
{
153
0
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
154
155
0
  switch (prop_id)
156
0
    {
157
0
    case PROP_G_FLAGS:
158
0
      g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
159
0
      break;
160
161
0
    default:
162
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163
0
      break;
164
0
    }
165
0
}
166
167
static gboolean
168
g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton    *interface,
169
                                                      GDBusMethodInvocation *invocation)
170
0
{
171
0
  return TRUE;
172
0
}
173
174
static void
175
g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
176
0
{
177
0
  GObjectClass *gobject_class;
178
179
0
  gobject_class = G_OBJECT_CLASS (klass);
180
0
  gobject_class->finalize     = g_dbus_interface_skeleton_finalize;
181
0
  gobject_class->set_property = g_dbus_interface_skeleton_set_property;
182
0
  gobject_class->get_property = g_dbus_interface_skeleton_get_property;
183
184
0
  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
0
  g_object_class_install_property (gobject_class,
194
0
                                   PROP_G_FLAGS,
195
0
                                   g_param_spec_flags ("g-flags",
196
0
                                                       "g-flags",
197
0
                                                       "Flags for the interface skeleton",
198
0
                                                       G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS,
199
0
                                                       G_DBUS_INTERFACE_SKELETON_FLAGS_NONE,
200
0
                                                       G_PARAM_READABLE |
201
0
                                                       G_PARAM_WRITABLE |
202
0
                                                       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
0
  signals[G_AUTHORIZE_METHOD_SIGNAL] =
248
0
    g_signal_new (I_("g-authorize-method"),
249
0
                  G_TYPE_DBUS_INTERFACE_SKELETON,
250
0
                  G_SIGNAL_RUN_LAST,
251
0
                  G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method),
252
0
                  _g_signal_accumulator_false_handled,
253
0
                  NULL,
254
0
                  _g_cclosure_marshal_BOOLEAN__OBJECT,
255
0
                  G_TYPE_BOOLEAN,
256
0
                  1,
257
0
                  G_TYPE_DBUS_METHOD_INVOCATION);
258
0
  g_signal_set_va_marshaller (signals[G_AUTHORIZE_METHOD_SIGNAL],
259
0
                              G_TYPE_FROM_CLASS (klass),
260
0
                              _g_cclosure_marshal_BOOLEAN__OBJECTv);
261
0
}
262
263
static void
264
g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
265
0
{
266
0
  interface->priv = g_dbus_interface_skeleton_get_instance_private (interface);
267
0
  g_mutex_init (&interface->priv->lock);
268
0
}
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
0
{
286
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
287
0
  return interface_->priv->flags;
288
0
}
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
0
{
303
0
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
304
0
  g_mutex_lock (&interface_->priv->lock);
305
0
  if (interface_->priv->flags != flags)
306
0
    {
307
0
      interface_->priv->flags = flags;
308
0
      g_mutex_unlock (&interface_->priv->lock);
309
0
      g_object_notify (G_OBJECT (interface_), "g-flags");
310
0
    }
311
0
  else
312
0
    {
313
0
      g_mutex_unlock (&interface_->priv->lock);
314
0
    }
315
0
}
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
0
{
331
0
  GDBusInterfaceInfo *ret;
332
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
333
0
  ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_info (interface_);
334
0
  g_warn_if_fail (ret != NULL);
335
0
  return ret;
336
0
}
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
0
{
353
0
  GDBusInterfaceVTable *ret;
354
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
355
0
  ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_vtable (interface_);
356
0
  g_warn_if_fail (ret != NULL);
357
0
  return ret;
358
0
}
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
0
{
375
0
  GVariant *ret;
376
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
377
0
  ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_);
378
0
  return g_variant_take_ref (ret);
379
0
}
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
0
{
399
0
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
400
0
  G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
401
0
}
402
403
/* ---------------------------------------------------------------------------------------------------- */
404
405
static GDBusInterfaceInfo *
406
_g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
407
0
{
408
0
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
409
0
  return g_dbus_interface_skeleton_get_info (interface);
410
0
}
411
412
static GDBusObject *
413
g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
414
0
{
415
0
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
416
0
  GDBusObject *ret;
417
0
  g_mutex_lock (&interface->priv->lock);
418
0
  ret = interface->priv->object;
419
0
  g_mutex_unlock (&interface->priv->lock);
420
0
  return ret;
421
0
}
422
423
static GDBusObject *
424
g_dbus_interface_skeleton_dup_object (GDBusInterface *interface_)
425
0
{
426
0
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
427
0
  GDBusObject *ret;
428
0
  g_mutex_lock (&interface->priv->lock);
429
0
  ret = interface->priv->object;
430
0
  if (ret != NULL)
431
0
    g_object_ref (ret);
432
0
  g_mutex_unlock (&interface->priv->lock);
433
0
  return ret;
434
0
}
435
436
static void
437
g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
438
                                      GDBusObject    *object)
439
0
{
440
0
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
441
0
  g_mutex_lock (&interface->priv->lock);
442
0
  if (interface->priv->object != NULL)
443
0
    g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
444
0
  interface->priv->object = object;
445
0
  if (object != NULL)
446
0
    g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
447
0
  g_mutex_unlock (&interface->priv->lock);
448
0
}
449
450
static void
451
dbus_interface_interface_init (GDBusInterfaceIface *iface)
452
0
{
453
0
  iface->get_info    = _g_dbus_interface_skeleton_get_info;
454
0
  iface->get_object  = g_dbus_interface_skeleton_get_object;
455
0
  iface->dup_object  = g_dbus_interface_skeleton_dup_object;
456
0
  iface->set_object  = g_dbus_interface_skeleton_set_object;
457
0
}
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
0
{
471
0
  if (g_atomic_int_dec_and_test (&data->ref_count))
472
0
    {
473
0
      g_clear_object (&data->invocation);
474
0
      g_slice_free (DispatchData, data);
475
0
    }
476
0
}
477
478
static DispatchData *
479
dispatch_data_ref (DispatchData *data)
480
0
{
481
0
  g_atomic_int_inc (&data->ref_count);
482
0
  return data;
483
0
}
484
485
static gboolean
486
dispatch_invoke_in_context_func (gpointer user_data)
487
0
{
488
0
  DispatchData *data = user_data;
489
0
  data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
490
0
                          g_dbus_method_invocation_get_sender (data->invocation),
491
0
                          g_dbus_method_invocation_get_object_path (data->invocation),
492
0
                          g_dbus_method_invocation_get_interface_name (data->invocation),
493
0
                          g_dbus_method_invocation_get_method_name (data->invocation),
494
0
                          g_dbus_method_invocation_get_parameters (data->invocation),
495
0
                          data->invocation,
496
0
                          g_dbus_method_invocation_get_user_data (data->invocation));
497
0
  return FALSE;
498
0
}
499
500
static void
501
dispatch_in_thread_func (GTask        *task,
502
                         gpointer      source_object,
503
                         gpointer      task_data,
504
                         GCancellable *cancellable)
505
0
{
506
0
  DispatchData *data = task_data;
507
0
  GDBusInterfaceSkeleton *interface = g_task_get_source_object (task);
508
0
  GDBusInterfaceSkeletonFlags flags;
509
0
  GDBusObject *object;
510
0
  gboolean authorized;
511
512
0
  g_mutex_lock (&interface->priv->lock);
513
0
  flags = interface->priv->flags;
514
0
  object = interface->priv->object;
515
0
  if (object != NULL)
516
0
    g_object_ref (object);
517
0
  g_mutex_unlock (&interface->priv->lock);
518
519
  /* first check on the enclosing object (if any), then the interface */
520
0
  authorized = TRUE;
521
0
  if (object != NULL)
522
0
    {
523
0
      g_signal_emit_by_name (object,
524
0
                             "authorize-method",
525
0
                             interface,
526
0
                             data->invocation,
527
0
                             &authorized);
528
0
    }
529
0
  if (authorized)
530
0
    {
531
0
      g_signal_emit (interface,
532
0
                     signals[G_AUTHORIZE_METHOD_SIGNAL],
533
0
                     0,
534
0
                     data->invocation,
535
0
                     &authorized);
536
0
    }
537
538
0
  if (authorized)
539
0
    {
540
0
      gboolean run_in_thread;
541
0
      run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
542
0
      if (run_in_thread)
543
0
        {
544
          /* might as well just re-use the existing thread */
545
0
          data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
546
0
                                  g_dbus_method_invocation_get_sender (data->invocation),
547
0
                                  g_dbus_method_invocation_get_object_path (data->invocation),
548
0
                                  g_dbus_method_invocation_get_interface_name (data->invocation),
549
0
                                  g_dbus_method_invocation_get_method_name (data->invocation),
550
0
                                  g_dbus_method_invocation_get_parameters (data->invocation),
551
0
                                  data->invocation,
552
0
                                  g_dbus_method_invocation_get_user_data (data->invocation));
553
0
        }
554
0
      else
555
0
        {
556
          /* bah, back to original context */
557
0
          g_main_context_invoke_full (g_task_get_context (task),
558
0
                                      g_task_get_priority (task),
559
0
                                      dispatch_invoke_in_context_func,
560
0
                                      dispatch_data_ref (data),
561
0
                                      (GDestroyNotify) dispatch_data_unref);
562
0
        }
563
0
    }
564
0
  else
565
0
    {
566
      /* do nothing */
567
0
    }
568
569
0
  if (object != NULL)
570
0
    g_object_unref (object);
571
572
0
  g_task_return_boolean (task, TRUE);
573
0
}
574
575
static void
576
g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton       *interface,
577
                                         GDBusInterfaceMethodCallFunc  method_call_func,
578
                                         GDBusMethodInvocation        *invocation)
579
0
{
580
0
  gboolean has_handlers;
581
0
  gboolean has_default_class_handler;
582
0
  gboolean emit_authorized_signal;
583
0
  gboolean run_in_thread;
584
0
  GDBusInterfaceSkeletonFlags flags;
585
0
  GDBusObject *object;
586
587
0
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
588
0
  g_return_if_fail (method_call_func != NULL);
589
0
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
590
591
0
  g_mutex_lock (&interface->priv->lock);
592
0
  flags = interface->priv->flags;
593
0
  object = interface->priv->object;
594
0
  if (object != NULL)
595
0
    g_object_ref (object);
596
0
  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
0
  has_handlers = g_signal_has_handler_pending (interface,
604
0
                                               signals[G_AUTHORIZE_METHOD_SIGNAL],
605
0
                                               0,
606
0
                                               TRUE);
607
0
  has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
608
0
                               g_dbus_interface_skeleton_g_authorize_method_default);
609
610
0
  emit_authorized_signal = (has_handlers || !has_default_class_handler);
611
0
  if (!emit_authorized_signal)
612
0
    {
613
0
      if (object != NULL)
614
0
        emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object));
615
0
    }
616
617
0
  run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
618
0
  if (!emit_authorized_signal && !run_in_thread)
619
0
    {
620
0
      method_call_func (g_dbus_method_invocation_get_connection (invocation),
621
0
                        g_dbus_method_invocation_get_sender (invocation),
622
0
                        g_dbus_method_invocation_get_object_path (invocation),
623
0
                        g_dbus_method_invocation_get_interface_name (invocation),
624
0
                        g_dbus_method_invocation_get_method_name (invocation),
625
0
                        g_dbus_method_invocation_get_parameters (invocation),
626
0
                        invocation,
627
0
                        g_dbus_method_invocation_get_user_data (invocation));
628
0
    }
629
0
  else
630
0
    {
631
0
      GTask *task;
632
0
      DispatchData *data;
633
634
0
      data = g_slice_new0 (DispatchData);
635
0
      data->method_call_func = method_call_func;
636
0
      data->invocation = g_object_ref (invocation);
637
0
      data->ref_count = 1;
638
639
0
      task = g_task_new (interface, NULL, NULL, NULL);
640
0
      g_task_set_source_tag (task, g_dbus_interface_method_dispatch_helper);
641
0
      g_task_set_name (task, "[gio] D-Bus interface method dispatch");
642
0
      g_task_set_task_data (task, data, (GDestroyNotify) dispatch_data_unref);
643
0
      g_task_run_in_thread (task, dispatch_in_thread_func);
644
0
      g_object_unref (task);
645
0
    }
646
647
0
  if (object != NULL)
648
0
    g_object_unref (object);
649
0
}
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
0
{
661
0
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
662
0
  g_dbus_interface_method_dispatch_helper (interface,
663
0
                                           g_dbus_interface_skeleton_get_vtable (interface)->method_call,
664
0
                                           invocation);
665
0
}
666
667
/* ---------------------------------------------------------------------------------------------------- */
668
669
static ConnectionData *
670
new_connection (GDBusConnection *connection,
671
                guint            registration_id)
672
0
{
673
0
  ConnectionData *data;
674
675
0
  data = g_slice_new0 (ConnectionData);
676
0
  data->connection      = g_object_ref (connection);
677
0
  data->registration_id = registration_id;
678
679
0
  return data;
680
0
}
681
682
static void
683
free_connection (ConnectionData *data)
684
0
{
685
0
  if (data != NULL)
686
0
    {
687
0
      g_object_unref (data->connection);
688
0
      g_slice_free (ConnectionData, data);
689
0
    }
690
0
}
691
692
static gboolean
693
add_connection_locked (GDBusInterfaceSkeleton *interface_,
694
                       GDBusConnection        *connection,
695
                       GError                **error)
696
0
{
697
0
  ConnectionData *data;
698
0
  guint registration_id;
699
0
  gboolean ret = FALSE;
700
701
0
  if (interface_->priv->hooked_vtable == NULL)
702
0
    {
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
0
      interface_->priv->hooked_vtable = g_memdup2 (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
712
0
      interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
713
0
    }
714
715
0
  registration_id = g_dbus_connection_register_object (connection,
716
0
                                                       interface_->priv->object_path,
717
0
                                                       g_dbus_interface_skeleton_get_info (interface_),
718
0
                                                       interface_->priv->hooked_vtable,
719
0
                                                       interface_,
720
0
                                                       NULL, /* user_data_free_func */
721
0
                                                       error);
722
723
0
  if (registration_id > 0)
724
0
    {
725
0
      data = new_connection (connection, registration_id);
726
0
      interface_->priv->connections = g_slist_append (interface_->priv->connections, data);
727
0
      ret = TRUE;
728
0
    }
729
730
0
  return ret;
731
0
}
732
733
static void
734
remove_connection_locked (GDBusInterfaceSkeleton *interface_,
735
                          GDBusConnection        *connection)
736
0
{
737
0
  ConnectionData *data;
738
0
  GSList *l;
739
740
  /* Get the connection in the list and unregister ... */
741
0
  for (l = interface_->priv->connections; l != NULL; l = l->next)
742
0
    {
743
0
      data = l->data;
744
0
      if (data->connection == connection)
745
0
        {
746
0
          g_warn_if_fail (g_dbus_connection_unregister_object (data->connection, data->registration_id));
747
0
          free_connection (data);
748
0
          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
0
          goto out;
751
0
        }
752
0
    }
753
0
 out:
754
0
  ;
755
0
}
756
757
static void
758
set_object_path_locked (GDBusInterfaceSkeleton *interface_,
759
                        const gchar            *object_path)
760
0
{
761
0
  if (g_strcmp0 (interface_->priv->object_path, object_path) != 0)
762
0
    {
763
0
      g_free (interface_->priv->object_path);
764
0
      interface_->priv->object_path = g_strdup (object_path);
765
0
    }
766
0
}
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
0
{
784
0
  ConnectionData  *data;
785
0
  GDBusConnection *ret;
786
787
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
788
0
  g_mutex_lock (&interface_->priv->lock);
789
790
0
  ret = NULL;
791
0
  if (interface_->priv->connections != NULL)
792
0
    {
793
0
      data = interface_->priv->connections->data;
794
0
      if (data != NULL)
795
0
        ret = data->connection;
796
0
    }
797
798
0
  g_mutex_unlock (&interface_->priv->lock);
799
800
0
  return ret;
801
0
}
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
0
{
819
0
  GList           *connections;
820
0
  GSList          *l;
821
0
  ConnectionData  *data;
822
823
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
824
825
0
  g_mutex_lock (&interface_->priv->lock);
826
0
  connections = NULL;
827
828
0
  for (l = interface_->priv->connections; l != NULL; l = l->next)
829
0
    {
830
0
      data        = l->data;
831
0
      connections = g_list_prepend (connections,
832
                                    /* Return a reference to each connection */
833
0
                                    g_object_ref (data->connection));
834
0
    }
835
836
0
  g_mutex_unlock (&interface_->priv->lock);
837
838
0
  return g_list_reverse (connections);
839
0
}
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
0
{
856
0
  GSList *l;
857
0
  gboolean ret = FALSE;
858
859
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
860
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
861
862
0
  g_mutex_lock (&interface_->priv->lock);
863
864
0
  for (l = interface_->priv->connections; l != NULL; l = l->next)
865
0
    {
866
0
      ConnectionData *data = l->data;
867
0
      if (data->connection == connection)
868
0
        {
869
0
          ret = TRUE;
870
0
          goto out;
871
0
        }
872
0
    }
873
874
0
 out:
875
0
  g_mutex_unlock (&interface_->priv->lock);
876
0
  return ret;
877
0
}
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
0
{
893
0
  const gchar *ret;
894
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
895
0
  g_mutex_lock (&interface_->priv->lock);
896
0
  ret = interface_->priv->object_path;
897
0
  g_mutex_unlock (&interface_->priv->lock);
898
0
  return ret;
899
0
}
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
0
{
927
0
  gboolean ret = FALSE;
928
929
0
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
930
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
931
0
  g_return_val_if_fail (g_variant_is_object_path (object_path), FALSE);
932
0
  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
0
  g_return_val_if_fail (interface_->priv->object_path == NULL ||
936
0
                        g_strcmp0 (interface_->priv->object_path, object_path) == 0, FALSE);
937
938
0
  g_mutex_lock (&interface_->priv->lock);
939
940
  /* Set the object path */
941
0
  set_object_path_locked (interface_, object_path);
942
943
  /* Add the connection */
944
0
  ret = add_connection_locked (interface_, connection, error);
945
946
0
  g_mutex_unlock (&interface_->priv->lock);
947
0
  return ret;
948
0
}
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
0
{
964
0
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
965
0
  g_return_if_fail (interface_->priv->connections != NULL);
966
967
0
  g_mutex_lock (&interface_->priv->lock);
968
969
0
  g_assert (interface_->priv->object_path != NULL);
970
0
  g_assert (interface_->priv->hooked_vtable != NULL);
971
972
  /* Remove all connections */
973
0
  while (interface_->priv->connections != NULL)
974
0
    {
975
0
      ConnectionData *data = interface_->priv->connections->data;
976
0
      remove_connection_locked (interface_, data->connection);
977
0
    }
978
979
  /* Unset the object path since there are no connections left */
980
0
  set_object_path_locked (interface_, NULL);
981
982
0
  g_mutex_unlock (&interface_->priv->lock);
983
0
}
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
0
{
1002
0
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
1003
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1004
0
  g_return_if_fail (interface_->priv->connections != NULL);
1005
1006
0
  g_mutex_lock (&interface_->priv->lock);
1007
1008
0
  g_assert (interface_->priv->object_path != NULL);
1009
0
  g_assert (interface_->priv->hooked_vtable != NULL);
1010
1011
0
  remove_connection_locked (interface_, connection);
1012
1013
  /* Reset the object path if we removed the last connection */
1014
0
  if (interface_->priv->connections == NULL)
1015
0
    set_object_path_locked (interface_, NULL);
1016
1017
0
  g_mutex_unlock (&interface_->priv->lock);
1018
0
}
1019
1020
/* ---------------------------------------------------------------------------------------------------- */