Coverage Report

Created: 2025-07-01 07:09

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