Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gdbusobjectskeleton.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 "gdbusobject.h"
26
#include "gdbusobjectskeleton.h"
27
#include "gdbusinterfaceskeleton.h"
28
#include "gdbusprivate.h"
29
#include "gdbusmethodinvocation.h"
30
#include "gdbusintrospection.h"
31
#include "gdbusinterface.h"
32
#include "gdbusutils.h"
33
34
#include "glibintl.h"
35
36
/**
37
 * SECTION:gdbusobjectskeleton
38
 * @short_description: Service-side D-Bus object
39
 * @include: gio/gio.h
40
 *
41
 * A #GDBusObjectSkeleton instance is essentially a group of D-Bus
42
 * interfaces. The set of exported interfaces on the object may be
43
 * dynamic and change at runtime.
44
 *
45
 * This type is intended to be used with #GDBusObjectManager.
46
 */
47
48
struct _GDBusObjectSkeletonPrivate
49
{
50
  GMutex lock;
51
  gchar *object_path;
52
  GHashTable *map_name_to_iface;
53
};
54
55
enum
56
{
57
  PROP_0,
58
  PROP_G_OBJECT_PATH
59
};
60
61
enum
62
{
63
  AUTHORIZE_METHOD_SIGNAL,
64
  LAST_SIGNAL,
65
};
66
67
static guint signals[LAST_SIGNAL] = {0};
68
69
static void dbus_object_interface_init (GDBusObjectIface *iface);
70
71
G_DEFINE_TYPE_WITH_CODE (GDBusObjectSkeleton, g_dbus_object_skeleton, G_TYPE_OBJECT,
72
                         G_ADD_PRIVATE (GDBusObjectSkeleton)
73
                         G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT, dbus_object_interface_init))
74
75
76
static void
77
g_dbus_object_skeleton_finalize (GObject *_object)
78
0
{
79
0
  GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
80
81
0
  g_free (object->priv->object_path);
82
0
  g_hash_table_unref (object->priv->map_name_to_iface);
83
84
0
  g_mutex_clear (&object->priv->lock);
85
86
0
  if (G_OBJECT_CLASS (g_dbus_object_skeleton_parent_class)->finalize != NULL)
87
0
    G_OBJECT_CLASS (g_dbus_object_skeleton_parent_class)->finalize (_object);
88
0
}
89
90
static void
91
g_dbus_object_skeleton_get_property (GObject    *_object,
92
                                     guint       prop_id,
93
                                     GValue     *value,
94
                                     GParamSpec *pspec)
95
0
{
96
0
  GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
97
98
0
  switch (prop_id)
99
0
    {
100
0
    case PROP_G_OBJECT_PATH:
101
0
      g_mutex_lock (&object->priv->lock);
102
0
      g_value_set_string (value, object->priv->object_path);
103
0
      g_mutex_unlock (&object->priv->lock);
104
0
      break;
105
106
0
    default:
107
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108
0
      break;
109
0
    }
110
0
}
111
112
static void
113
g_dbus_object_skeleton_set_property (GObject       *_object,
114
                                     guint          prop_id,
115
                                     const GValue  *value,
116
                                     GParamSpec    *pspec)
117
0
{
118
0
  GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
119
120
0
  switch (prop_id)
121
0
    {
122
0
    case PROP_G_OBJECT_PATH:
123
0
      g_dbus_object_skeleton_set_object_path (object, g_value_get_string (value));
124
0
      break;
125
126
0
    default:
127
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128
0
      break;
129
0
    }
130
0
}
131
132
static gboolean
133
g_dbus_object_skeleton_authorize_method_default (GDBusObjectSkeleton    *object,
134
                                                 GDBusInterfaceSkeleton *interface,
135
                                                 GDBusMethodInvocation  *invocation)
136
0
{
137
0
  return TRUE;
138
0
}
139
140
static void
141
g_dbus_object_skeleton_class_init (GDBusObjectSkeletonClass *klass)
142
0
{
143
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144
145
0
  gobject_class->finalize     = g_dbus_object_skeleton_finalize;
146
0
  gobject_class->set_property = g_dbus_object_skeleton_set_property;
147
0
  gobject_class->get_property = g_dbus_object_skeleton_get_property;
148
149
0
  klass->authorize_method = g_dbus_object_skeleton_authorize_method_default;
150
151
  /**
152
   * GDBusObjectSkeleton:g-object-path:
153
   *
154
   * The object path where the object is exported.
155
   *
156
   * Since: 2.30
157
   */
158
0
  g_object_class_install_property (gobject_class,
159
0
                                   PROP_G_OBJECT_PATH,
160
0
                                   g_param_spec_string ("g-object-path",
161
0
                                                        "Object Path",
162
0
                                                        "The object path where the object is exported",
163
0
                                                        NULL,
164
0
                                                        G_PARAM_READABLE |
165
0
                                                        G_PARAM_WRITABLE |
166
0
                                                        G_PARAM_CONSTRUCT |
167
0
                                                        G_PARAM_STATIC_STRINGS));
168
169
  /**
170
   * GDBusObjectSkeleton::authorize-method:
171
   * @object: The #GDBusObjectSkeleton emitting the signal.
172
   * @interface: The #GDBusInterfaceSkeleton that @invocation is for.
173
   * @invocation: A #GDBusMethodInvocation.
174
   *
175
   * Emitted when a method is invoked by a remote caller and used to
176
   * determine if the method call is authorized.
177
   *
178
   * This signal is like #GDBusInterfaceSkeleton's
179
   * #GDBusInterfaceSkeleton::g-authorize-method signal,
180
   * except that it is for the enclosing object.
181
   *
182
   * The default class handler just returns %TRUE.
183
   *
184
   * Returns: %TRUE if the call is authorized, %FALSE otherwise.
185
   *
186
   * Since: 2.30
187
   */
188
0
  signals[AUTHORIZE_METHOD_SIGNAL] =
189
0
    g_signal_new (I_("authorize-method"),
190
0
                  G_TYPE_DBUS_OBJECT_SKELETON,
191
0
                  G_SIGNAL_RUN_LAST,
192
0
                  G_STRUCT_OFFSET (GDBusObjectSkeletonClass, authorize_method),
193
0
                  _g_signal_accumulator_false_handled,
194
0
                  NULL,
195
0
                  NULL,
196
0
                  G_TYPE_BOOLEAN,
197
0
                  2,
198
0
                  G_TYPE_DBUS_INTERFACE_SKELETON,
199
0
                  G_TYPE_DBUS_METHOD_INVOCATION);
200
0
}
201
202
static void
203
g_dbus_object_skeleton_init (GDBusObjectSkeleton *object)
204
0
{
205
0
  object->priv = g_dbus_object_skeleton_get_instance_private (object);
206
0
  g_mutex_init (&object->priv->lock);
207
0
  object->priv->map_name_to_iface = g_hash_table_new_full (g_str_hash,
208
0
                                                           g_str_equal,
209
0
                                                           g_free,
210
0
                                                           (GDestroyNotify) g_object_unref);
211
0
}
212
213
/**
214
 * g_dbus_object_skeleton_new:
215
 * @object_path: An object path.
216
 *
217
 * Creates a new #GDBusObjectSkeleton.
218
 *
219
 * Returns: A #GDBusObjectSkeleton. Free with g_object_unref().
220
 *
221
 * Since: 2.30
222
 */
223
GDBusObjectSkeleton *
224
g_dbus_object_skeleton_new (const gchar *object_path)
225
0
{
226
0
  g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
227
0
  return G_DBUS_OBJECT_SKELETON (g_object_new (G_TYPE_DBUS_OBJECT_SKELETON,
228
0
                                               "g-object-path", object_path,
229
0
                                               NULL));
230
0
}
231
232
/**
233
 * g_dbus_object_skeleton_set_object_path:
234
 * @object: A #GDBusObjectSkeleton.
235
 * @object_path: A valid D-Bus object path.
236
 *
237
 * Sets the object path for @object.
238
 *
239
 * Since: 2.30
240
 */
241
void
242
g_dbus_object_skeleton_set_object_path (GDBusObjectSkeleton *object,
243
                                        const gchar     *object_path)
244
0
{
245
0
  g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
246
0
  g_return_if_fail (object_path == NULL || g_variant_is_object_path (object_path));
247
0
  g_mutex_lock (&object->priv->lock);
248
  /* TODO: fail if object is currently exported */
249
0
  if (g_strcmp0 (object->priv->object_path, object_path) != 0)
250
0
    {
251
0
      g_free (object->priv->object_path);
252
0
      object->priv->object_path = g_strdup (object_path);
253
0
      g_mutex_unlock (&object->priv->lock);
254
0
      g_object_notify (G_OBJECT (object), "g-object-path");
255
0
    }
256
0
  else
257
0
    {
258
0
      g_mutex_unlock (&object->priv->lock);
259
0
    }
260
0
}
261
262
static const gchar *
263
g_dbus_object_skeleton_get_object_path (GDBusObject *_object)
264
0
{
265
0
  GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
266
0
  const gchar *ret;
267
0
  g_mutex_lock (&object->priv->lock);
268
0
  ret = object->priv->object_path;
269
0
  g_mutex_unlock (&object->priv->lock);
270
0
  return ret;
271
0
}
272
273
/**
274
 * g_dbus_object_skeleton_add_interface:
275
 * @object: A #GDBusObjectSkeleton.
276
 * @interface_: A #GDBusInterfaceSkeleton.
277
 *
278
 * Adds @interface_ to @object.
279
 *
280
 * If @object already contains a #GDBusInterfaceSkeleton with the same
281
 * interface name, it is removed before @interface_ is added.
282
 *
283
 * Note that @object takes its own reference on @interface_ and holds
284
 * it until removed.
285
 *
286
 * Since: 2.30
287
 */
288
void
289
g_dbus_object_skeleton_add_interface (GDBusObjectSkeleton     *object,
290
                                      GDBusInterfaceSkeleton  *interface_)
291
0
{
292
0
  GDBusInterfaceInfo *info;
293
0
  GDBusInterface *interface_to_remove;
294
295
0
  g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
296
0
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
297
298
0
  g_mutex_lock (&object->priv->lock);
299
300
0
  info = g_dbus_interface_skeleton_get_info (interface_);
301
0
  g_object_ref (interface_);
302
303
0
  interface_to_remove = g_hash_table_lookup (object->priv->map_name_to_iface, info->name);
304
0
  if (interface_to_remove != NULL)
305
0
    {
306
0
      g_object_ref (interface_to_remove);
307
0
      g_warn_if_fail (g_hash_table_remove (object->priv->map_name_to_iface, info->name));
308
0
    }
309
0
  g_hash_table_insert (object->priv->map_name_to_iface,
310
0
                       g_strdup (info->name),
311
0
                       g_object_ref (interface_));
312
0
  g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_), G_DBUS_OBJECT (object));
313
314
0
  g_mutex_unlock (&object->priv->lock);
315
316
0
  if (interface_to_remove != NULL)
317
0
    {
318
0
      g_dbus_interface_set_object (interface_to_remove, NULL);
319
0
      g_signal_emit_by_name (object,
320
0
                             "interface-removed",
321
0
                             interface_to_remove);
322
0
      g_object_unref (interface_to_remove);
323
0
    }
324
325
0
  g_signal_emit_by_name (object,
326
0
                         "interface-added",
327
0
                         interface_);
328
0
  g_object_unref (interface_);
329
0
}
330
331
/**
332
 * g_dbus_object_skeleton_remove_interface:
333
 * @object: A #GDBusObjectSkeleton.
334
 * @interface_: A #GDBusInterfaceSkeleton.
335
 *
336
 * Removes @interface_ from @object.
337
 *
338
 * Since: 2.30
339
 */
340
void
341
g_dbus_object_skeleton_remove_interface  (GDBusObjectSkeleton    *object,
342
                                          GDBusInterfaceSkeleton *interface_)
343
0
{
344
0
  GDBusInterfaceSkeleton *other_interface;
345
0
  GDBusInterfaceInfo *info;
346
347
0
  g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
348
0
  g_return_if_fail (G_IS_DBUS_INTERFACE (interface_));
349
350
0
  g_mutex_lock (&object->priv->lock);
351
352
0
  info = g_dbus_interface_skeleton_get_info (interface_);
353
354
0
  other_interface = g_hash_table_lookup (object->priv->map_name_to_iface, info->name);
355
0
  if (other_interface == NULL)
356
0
    {
357
0
      g_mutex_unlock (&object->priv->lock);
358
0
      g_warning ("Tried to remove interface with name %s from object "
359
0
                 "at path %s but no such interface exists",
360
0
                 info->name,
361
0
                 object->priv->object_path);
362
0
    }
363
0
  else if (other_interface != interface_)
364
0
    {
365
0
      g_mutex_unlock (&object->priv->lock);
366
0
      g_warning ("Tried to remove interface %p with name %s from object "
367
0
                 "at path %s but the object has the interface %p",
368
0
                 interface_,
369
0
                 info->name,
370
0
                 object->priv->object_path,
371
0
                 other_interface);
372
0
    }
373
0
  else
374
0
    {
375
0
      g_object_ref (interface_);
376
0
      g_warn_if_fail (g_hash_table_remove (object->priv->map_name_to_iface, info->name));
377
0
      g_mutex_unlock (&object->priv->lock);
378
0
      g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_), NULL);
379
0
      g_signal_emit_by_name (object,
380
0
                             "interface-removed",
381
0
                             interface_);
382
0
      g_object_unref (interface_);
383
0
    }
384
0
}
385
386
387
/**
388
 * g_dbus_object_skeleton_remove_interface_by_name:
389
 * @object: A #GDBusObjectSkeleton.
390
 * @interface_name: A D-Bus interface name.
391
 *
392
 * Removes the #GDBusInterface with @interface_name from @object.
393
 *
394
 * If no D-Bus interface of the given interface exists, this function
395
 * does nothing.
396
 *
397
 * Since: 2.30
398
 */
399
void
400
g_dbus_object_skeleton_remove_interface_by_name (GDBusObjectSkeleton *object,
401
                                                 const gchar         *interface_name)
402
0
{
403
0
  GDBusInterface *interface;
404
405
0
  g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
406
0
  g_return_if_fail (g_dbus_is_interface_name (interface_name));
407
408
0
  g_mutex_lock (&object->priv->lock);
409
0
  interface = g_hash_table_lookup (object->priv->map_name_to_iface, interface_name);
410
0
  if (interface != NULL)
411
0
    {
412
0
      g_object_ref (interface);
413
0
      g_warn_if_fail (g_hash_table_remove (object->priv->map_name_to_iface, interface_name));
414
0
      g_mutex_unlock (&object->priv->lock);
415
0
      g_dbus_interface_set_object (interface, NULL);
416
0
      g_signal_emit_by_name (object,
417
0
                             "interface-removed",
418
0
                             interface);
419
0
      g_object_unref (interface);
420
0
    }
421
0
  else
422
0
    {
423
0
      g_mutex_unlock (&object->priv->lock);
424
0
    }
425
0
}
426
427
static GDBusInterface *
428
g_dbus_object_skeleton_get_interface (GDBusObject  *_object,
429
                                      const gchar  *interface_name)
430
0
{
431
0
  GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
432
0
  GDBusInterface *ret;
433
434
0
  g_return_val_if_fail (G_IS_DBUS_OBJECT_SKELETON (object), NULL);
435
0
  g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
436
437
0
  g_mutex_lock (&object->priv->lock);
438
0
  ret = g_hash_table_lookup (object->priv->map_name_to_iface, interface_name);
439
0
  if (ret != NULL)
440
0
    g_object_ref (ret);
441
0
  g_mutex_unlock (&object->priv->lock);
442
0
  return ret;
443
0
}
444
445
static GList *
446
g_dbus_object_skeleton_get_interfaces (GDBusObject *_object)
447
0
{
448
0
  GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
449
0
  GList *ret;
450
451
0
  g_return_val_if_fail (G_IS_DBUS_OBJECT_SKELETON (object), NULL);
452
453
0
  ret = NULL;
454
455
0
  g_mutex_lock (&object->priv->lock);
456
0
  ret = g_hash_table_get_values (object->priv->map_name_to_iface);
457
0
  g_list_foreach (ret, (GFunc) g_object_ref, NULL);
458
0
  g_mutex_unlock (&object->priv->lock);
459
460
0
  return ret;
461
0
}
462
463
/**
464
 * g_dbus_object_skeleton_flush:
465
 * @object: A #GDBusObjectSkeleton.
466
 *
467
 * This method simply calls g_dbus_interface_skeleton_flush() on all
468
 * interfaces belonging to @object. See that method for when flushing
469
 * is useful.
470
 *
471
 * Since: 2.30
472
 */
473
void
474
g_dbus_object_skeleton_flush (GDBusObjectSkeleton *object)
475
0
{
476
0
  GPtrArray *to_flush;
477
478
0
  g_mutex_lock (&object->priv->lock);
479
0
  to_flush = g_hash_table_get_values_as_ptr_array (object->priv->map_name_to_iface);
480
0
  g_ptr_array_foreach (to_flush, (GFunc) g_object_ref, NULL);
481
0
  g_ptr_array_set_free_func (to_flush, g_object_unref);
482
0
  g_mutex_unlock (&object->priv->lock);
483
484
0
  for (guint i = 0; i < to_flush->len; ++i)
485
0
    {
486
0
      g_dbus_interface_skeleton_flush (
487
0
        G_DBUS_INTERFACE_SKELETON (g_ptr_array_index (to_flush, i)));
488
0
    }
489
490
0
  g_clear_pointer (&to_flush, g_ptr_array_unref);
491
0
}
492
493
static void
494
dbus_object_interface_init (GDBusObjectIface *iface)
495
0
{
496
0
  iface->get_object_path = g_dbus_object_skeleton_get_object_path;
497
0
  iface->get_interfaces  = g_dbus_object_skeleton_get_interfaces;
498
0
  iface->get_interface  = g_dbus_object_skeleton_get_interface;
499
0
}
500
501
gboolean
502
_g_dbus_object_skeleton_has_authorize_method_handlers (GDBusObjectSkeleton *object)
503
0
{
504
0
  gboolean has_handlers;
505
0
  gboolean has_default_class_handler;
506
507
0
  has_handlers = g_signal_has_handler_pending (object,
508
0
                                               signals[AUTHORIZE_METHOD_SIGNAL],
509
0
                                               0,
510
0
                                               TRUE);
511
0
  has_default_class_handler = (G_DBUS_OBJECT_SKELETON_GET_CLASS (object)->authorize_method ==
512
0
                               g_dbus_object_skeleton_authorize_method_default);
513
514
0
  return has_handlers || !has_default_class_handler;
515
0
}