/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 | | * 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 "gdbusinterface.h" |
24 | | #include "gdbusinterfaceskeleton.h" |
25 | | #include "gdbusobjectskeleton.h" |
26 | | #include "gioenumtypes.h" |
27 | | #include "gdbusprivate.h" |
28 | | #include "gdbusmethodinvocation.h" |
29 | | #include "gdbusconnection.h" |
30 | | #include "gmarshal-internal.h" |
31 | | #include "gtask.h" |
32 | | #include "gioerror.h" |
33 | | |
34 | | #include "glibintl.h" |
35 | | |
36 | | /** |
37 | | * SECTION:gdbusinterfaceskeleton |
38 | | * @short_description: Service-side D-Bus interface |
39 | | * @include: gio/gio.h |
40 | | * |
41 | | * Abstract base class for D-Bus interfaces on the service side. |
42 | | */ |
43 | | |
44 | | struct _GDBusInterfaceSkeletonPrivate |
45 | | { |
46 | | GMutex lock; |
47 | | |
48 | | GDBusObject *object; |
49 | | GDBusInterfaceSkeletonFlags flags; |
50 | | |
51 | | GSList *connections; /* List of ConnectionData */ |
52 | | gchar *object_path; /* The object path for this skeleton */ |
53 | | GDBusInterfaceVTable *hooked_vtable; |
54 | | }; |
55 | | |
56 | | typedef struct |
57 | | { |
58 | | GDBusConnection *connection; |
59 | | guint registration_id; |
60 | | } ConnectionData; |
61 | | |
62 | | enum |
63 | | { |
64 | | G_AUTHORIZE_METHOD_SIGNAL, |
65 | | LAST_SIGNAL |
66 | | }; |
67 | | |
68 | | enum |
69 | | { |
70 | | PROP_0, |
71 | | PROP_G_FLAGS |
72 | | }; |
73 | | |
74 | | static guint signals[LAST_SIGNAL] = {0}; |
75 | | |
76 | | static void dbus_interface_interface_init (GDBusInterfaceIface *iface); |
77 | | |
78 | | static void set_object_path_locked (GDBusInterfaceSkeleton *interface_, |
79 | | const gchar *object_path); |
80 | | static void remove_connection_locked (GDBusInterfaceSkeleton *interface_, |
81 | | GDBusConnection *connection); |
82 | | static void skeleton_intercept_handle_method_call (GDBusConnection *connection, |
83 | | const gchar *sender, |
84 | | const gchar *object_path, |
85 | | const gchar *interface_name, |
86 | | const gchar *method_name, |
87 | | GVariant *parameters, |
88 | | GDBusMethodInvocation *invocation, |
89 | | gpointer user_data); |
90 | | |
91 | | |
92 | | G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GDBusInterfaceSkeleton, g_dbus_interface_skeleton, G_TYPE_OBJECT, |
93 | | G_ADD_PRIVATE (GDBusInterfaceSkeleton) |
94 | | G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_interface_init)) |
95 | | |
96 | | static void |
97 | | g_dbus_interface_skeleton_finalize (GObject *object) |
98 | 0 | { |
99 | 0 | GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object); |
100 | | |
101 | | /* Hold the lock just in case any code we call verifies that the lock is held */ |
102 | 0 | g_mutex_lock (&interface->priv->lock); |
103 | | |
104 | | /* unexport from all connections if we're exported anywhere */ |
105 | 0 | while (interface->priv->connections != NULL) |
106 | 0 | { |
107 | 0 | ConnectionData *data = interface->priv->connections->data; |
108 | 0 | remove_connection_locked (interface, data->connection); |
109 | 0 | } |
110 | |
|
111 | 0 | set_object_path_locked (interface, NULL); |
112 | |
|
113 | 0 | g_mutex_unlock (&interface->priv->lock); |
114 | |
|
115 | 0 | g_free (interface->priv->hooked_vtable); |
116 | |
|
117 | 0 | if (interface->priv->object != NULL) |
118 | 0 | g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object); |
119 | |
|
120 | 0 | g_mutex_clear (&interface->priv->lock); |
121 | |
|
122 | 0 | G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object); |
123 | 0 | } |
124 | | |
125 | | static void |
126 | | g_dbus_interface_skeleton_get_property (GObject *object, |
127 | | guint prop_id, |
128 | | GValue *value, |
129 | | GParamSpec *pspec) |
130 | 0 | { |
131 | 0 | GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object); |
132 | |
|
133 | 0 | switch (prop_id) |
134 | 0 | { |
135 | 0 | case PROP_G_FLAGS: |
136 | 0 | g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface)); |
137 | 0 | break; |
138 | | |
139 | 0 | default: |
140 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
141 | 0 | break; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | | static void |
146 | | g_dbus_interface_skeleton_set_property (GObject *object, |
147 | | guint prop_id, |
148 | | const GValue *value, |
149 | | GParamSpec *pspec) |
150 | 0 | { |
151 | 0 | GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object); |
152 | |
|
153 | 0 | switch (prop_id) |
154 | 0 | { |
155 | 0 | case PROP_G_FLAGS: |
156 | 0 | g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value)); |
157 | 0 | break; |
158 | | |
159 | 0 | default: |
160 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
161 | 0 | break; |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | | static gboolean |
166 | | g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton *interface, |
167 | | GDBusMethodInvocation *invocation) |
168 | 0 | { |
169 | 0 | return TRUE; |
170 | 0 | } |
171 | | |
172 | | static void |
173 | | g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass) |
174 | 0 | { |
175 | 0 | GObjectClass *gobject_class; |
176 | |
|
177 | 0 | gobject_class = G_OBJECT_CLASS (klass); |
178 | 0 | gobject_class->finalize = g_dbus_interface_skeleton_finalize; |
179 | 0 | gobject_class->set_property = g_dbus_interface_skeleton_set_property; |
180 | 0 | gobject_class->get_property = g_dbus_interface_skeleton_get_property; |
181 | |
|
182 | 0 | klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default; |
183 | | |
184 | | /** |
185 | | * GDBusInterfaceSkeleton:g-flags: |
186 | | * |
187 | | * Flags from the #GDBusInterfaceSkeletonFlags enumeration. |
188 | | * |
189 | | * Since: 2.30 |
190 | | */ |
191 | 0 | g_object_class_install_property (gobject_class, |
192 | 0 | PROP_G_FLAGS, |
193 | 0 | g_param_spec_flags ("g-flags", |
194 | 0 | "g-flags", |
195 | 0 | "Flags for the interface skeleton", |
196 | 0 | G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS, |
197 | 0 | G_DBUS_INTERFACE_SKELETON_FLAGS_NONE, |
198 | 0 | G_PARAM_READABLE | |
199 | 0 | G_PARAM_WRITABLE | |
200 | 0 | G_PARAM_STATIC_STRINGS)); |
201 | | |
202 | | /** |
203 | | * GDBusInterfaceSkeleton::g-authorize-method: |
204 | | * @interface: The #GDBusInterfaceSkeleton emitting the signal. |
205 | | * @invocation: A #GDBusMethodInvocation. |
206 | | * |
207 | | * Emitted when a method is invoked by a remote caller and used to |
208 | | * determine if the method call is authorized. |
209 | | * |
210 | | * Note that this signal is emitted in a thread dedicated to |
211 | | * handling the method call so handlers are allowed to perform |
212 | | * blocking IO. This means that it is appropriate to call e.g. |
213 | | * [polkit_authority_check_authorization_sync()](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#polkit-authority-check-authorization-sync) |
214 | | * with the |
215 | | * [POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#POLKIT-CHECK-AUTHORIZATION-FLAGS-ALLOW-USER-INTERACTION:CAPS) |
216 | | * flag set. |
217 | | * |
218 | | * If %FALSE is returned then no further handlers are run and the |
219 | | * signal handler must take a reference to @invocation and finish |
220 | | * handling the call (e.g. return an error via |
221 | | * g_dbus_method_invocation_return_error()). |
222 | | * |
223 | | * Otherwise, if %TRUE is returned, signal emission continues. If no |
224 | | * handlers return %FALSE, then the method is dispatched. If |
225 | | * @interface has an enclosing #GDBusObjectSkeleton, then the |
226 | | * #GDBusObjectSkeleton::authorize-method signal handlers run before |
227 | | * the handlers for this signal. |
228 | | * |
229 | | * The default class handler just returns %TRUE. |
230 | | * |
231 | | * Please note that the common case is optimized: if no signals |
232 | | * handlers are connected and the default class handler isn't |
233 | | * overridden (for both @interface and the enclosing |
234 | | * #GDBusObjectSkeleton, if any) and #GDBusInterfaceSkeleton:g-flags does |
235 | | * not have the |
236 | | * %G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD |
237 | | * flags set, no dedicated thread is ever used and the call will be |
238 | | * handled in the same thread as the object that @interface belongs |
239 | | * to was exported in. |
240 | | * |
241 | | * Returns: %TRUE if the call is authorized, %FALSE otherwise. |
242 | | * |
243 | | * Since: 2.30 |
244 | | */ |
245 | 0 | signals[G_AUTHORIZE_METHOD_SIGNAL] = |
246 | 0 | g_signal_new (I_("g-authorize-method"), |
247 | 0 | G_TYPE_DBUS_INTERFACE_SKELETON, |
248 | 0 | G_SIGNAL_RUN_LAST, |
249 | 0 | G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method), |
250 | 0 | _g_signal_accumulator_false_handled, |
251 | 0 | NULL, |
252 | 0 | _g_cclosure_marshal_BOOLEAN__OBJECT, |
253 | 0 | G_TYPE_BOOLEAN, |
254 | 0 | 1, |
255 | 0 | G_TYPE_DBUS_METHOD_INVOCATION); |
256 | 0 | g_signal_set_va_marshaller (signals[G_AUTHORIZE_METHOD_SIGNAL], |
257 | 0 | G_TYPE_FROM_CLASS (klass), |
258 | 0 | _g_cclosure_marshal_BOOLEAN__OBJECTv); |
259 | 0 | } |
260 | | |
261 | | static void |
262 | | g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface) |
263 | 0 | { |
264 | 0 | interface->priv = g_dbus_interface_skeleton_get_instance_private (interface); |
265 | 0 | g_mutex_init (&interface->priv->lock); |
266 | 0 | } |
267 | | |
268 | | /* ---------------------------------------------------------------------------------------------------- */ |
269 | | |
270 | | /** |
271 | | * g_dbus_interface_skeleton_get_flags: |
272 | | * @interface_: A #GDBusInterfaceSkeleton. |
273 | | * |
274 | | * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior |
275 | | * of @interface_ |
276 | | * |
277 | | * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration. |
278 | | * |
279 | | * Since: 2.30 |
280 | | */ |
281 | | GDBusInterfaceSkeletonFlags |
282 | | g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton *interface_) |
283 | 0 | { |
284 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE); |
285 | 0 | return interface_->priv->flags; |
286 | 0 | } |
287 | | |
288 | | /** |
289 | | * g_dbus_interface_skeleton_set_flags: |
290 | | * @interface_: A #GDBusInterfaceSkeleton. |
291 | | * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration. |
292 | | * |
293 | | * Sets flags describing what the behavior of @skeleton should be. |
294 | | * |
295 | | * Since: 2.30 |
296 | | */ |
297 | | void |
298 | | g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton *interface_, |
299 | | GDBusInterfaceSkeletonFlags flags) |
300 | 0 | { |
301 | 0 | g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_)); |
302 | 0 | g_mutex_lock (&interface_->priv->lock); |
303 | 0 | if (interface_->priv->flags != flags) |
304 | 0 | { |
305 | 0 | interface_->priv->flags = flags; |
306 | 0 | g_mutex_unlock (&interface_->priv->lock); |
307 | 0 | g_object_notify (G_OBJECT (interface_), "g-flags"); |
308 | 0 | } |
309 | 0 | else |
310 | 0 | { |
311 | 0 | g_mutex_unlock (&interface_->priv->lock); |
312 | 0 | } |
313 | 0 | } |
314 | | |
315 | | /** |
316 | | * g_dbus_interface_skeleton_get_info: |
317 | | * @interface_: A #GDBusInterfaceSkeleton. |
318 | | * |
319 | | * Gets D-Bus introspection information for the D-Bus interface |
320 | | * implemented by @interface_. |
321 | | * |
322 | | * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free. |
323 | | * |
324 | | * Since: 2.30 |
325 | | */ |
326 | | GDBusInterfaceInfo * |
327 | | g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_) |
328 | 0 | { |
329 | 0 | GDBusInterfaceInfo *ret; |
330 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL); |
331 | 0 | ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_info (interface_); |
332 | 0 | g_warn_if_fail (ret != NULL); |
333 | 0 | return ret; |
334 | 0 | } |
335 | | |
336 | | /** |
337 | | * g_dbus_interface_skeleton_get_vtable: (skip) |
338 | | * @interface_: A #GDBusInterfaceSkeleton. |
339 | | * |
340 | | * Gets the interface vtable for the D-Bus interface implemented by |
341 | | * @interface_. The returned function pointers should expect @interface_ |
342 | | * itself to be passed as @user_data. |
343 | | * |
344 | | * Returns: A #GDBusInterfaceVTable (never %NULL). |
345 | | * |
346 | | * Since: 2.30 |
347 | | */ |
348 | | GDBusInterfaceVTable * |
349 | | g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_) |
350 | 0 | { |
351 | 0 | GDBusInterfaceVTable *ret; |
352 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL); |
353 | 0 | ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_vtable (interface_); |
354 | 0 | g_warn_if_fail (ret != NULL); |
355 | 0 | return ret; |
356 | 0 | } |
357 | | |
358 | | /** |
359 | | * g_dbus_interface_skeleton_get_properties: |
360 | | * @interface_: A #GDBusInterfaceSkeleton. |
361 | | * |
362 | | * Gets all D-Bus properties for @interface_. |
363 | | * |
364 | | * Returns: (transfer full): A #GVariant of type |
365 | | * ['a{sv}'][G-VARIANT-TYPE-VARDICT:CAPS]. |
366 | | * Free with g_variant_unref(). |
367 | | * |
368 | | * Since: 2.30 |
369 | | */ |
370 | | GVariant * |
371 | | g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_) |
372 | 0 | { |
373 | 0 | GVariant *ret; |
374 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL); |
375 | 0 | ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_); |
376 | 0 | return g_variant_take_ref (ret); |
377 | 0 | } |
378 | | |
379 | | /** |
380 | | * g_dbus_interface_skeleton_flush: |
381 | | * @interface_: A #GDBusInterfaceSkeleton. |
382 | | * |
383 | | * If @interface_ has outstanding changes, request for these changes to be |
384 | | * emitted immediately. |
385 | | * |
386 | | * For example, an exported D-Bus interface may queue up property |
387 | | * changes and emit the |
388 | | * `org.freedesktop.DBus.Properties.PropertiesChanged` |
389 | | * signal later (e.g. in an idle handler). This technique is useful |
390 | | * for collapsing multiple property changes into one. |
391 | | * |
392 | | * Since: 2.30 |
393 | | */ |
394 | | void |
395 | | g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_) |
396 | 0 | { |
397 | 0 | g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_)); |
398 | 0 | G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_); |
399 | 0 | } |
400 | | |
401 | | /* ---------------------------------------------------------------------------------------------------- */ |
402 | | |
403 | | static GDBusInterfaceInfo * |
404 | | _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_) |
405 | 0 | { |
406 | 0 | GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_); |
407 | 0 | return g_dbus_interface_skeleton_get_info (interface); |
408 | 0 | } |
409 | | |
410 | | static GDBusObject * |
411 | | g_dbus_interface_skeleton_get_object (GDBusInterface *interface_) |
412 | 0 | { |
413 | 0 | GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_); |
414 | 0 | GDBusObject *ret; |
415 | 0 | g_mutex_lock (&interface->priv->lock); |
416 | 0 | ret = interface->priv->object; |
417 | 0 | g_mutex_unlock (&interface->priv->lock); |
418 | 0 | return ret; |
419 | 0 | } |
420 | | |
421 | | static GDBusObject * |
422 | | g_dbus_interface_skeleton_dup_object (GDBusInterface *interface_) |
423 | 0 | { |
424 | 0 | GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_); |
425 | 0 | GDBusObject *ret; |
426 | 0 | g_mutex_lock (&interface->priv->lock); |
427 | 0 | ret = interface->priv->object; |
428 | 0 | if (ret != NULL) |
429 | 0 | g_object_ref (ret); |
430 | 0 | g_mutex_unlock (&interface->priv->lock); |
431 | 0 | return ret; |
432 | 0 | } |
433 | | |
434 | | static void |
435 | | g_dbus_interface_skeleton_set_object (GDBusInterface *interface_, |
436 | | GDBusObject *object) |
437 | 0 | { |
438 | 0 | GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_); |
439 | 0 | g_mutex_lock (&interface->priv->lock); |
440 | 0 | if (interface->priv->object != NULL) |
441 | 0 | g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object); |
442 | 0 | interface->priv->object = object; |
443 | 0 | if (object != NULL) |
444 | 0 | g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object); |
445 | 0 | g_mutex_unlock (&interface->priv->lock); |
446 | 0 | } |
447 | | |
448 | | static void |
449 | | dbus_interface_interface_init (GDBusInterfaceIface *iface) |
450 | 0 | { |
451 | 0 | iface->get_info = _g_dbus_interface_skeleton_get_info; |
452 | 0 | iface->get_object = g_dbus_interface_skeleton_get_object; |
453 | 0 | iface->dup_object = g_dbus_interface_skeleton_dup_object; |
454 | 0 | iface->set_object = g_dbus_interface_skeleton_set_object; |
455 | 0 | } |
456 | | |
457 | | /* ---------------------------------------------------------------------------------------------------- */ |
458 | | |
459 | | typedef struct |
460 | | { |
461 | | gint ref_count; /* (atomic) */ |
462 | | GDBusInterfaceSkeleton *interface; |
463 | | GDBusInterfaceMethodCallFunc method_call_func; |
464 | | GDBusMethodInvocation *invocation; |
465 | | } DispatchData; |
466 | | |
467 | | static void |
468 | | dispatch_data_unref (DispatchData *data) |
469 | 0 | { |
470 | 0 | if (g_atomic_int_dec_and_test (&data->ref_count)) |
471 | 0 | g_slice_free (DispatchData, data); |
472 | 0 | } |
473 | | |
474 | | static DispatchData * |
475 | | dispatch_data_ref (DispatchData *data) |
476 | 0 | { |
477 | 0 | g_atomic_int_inc (&data->ref_count); |
478 | 0 | return data; |
479 | 0 | } |
480 | | |
481 | | static gboolean |
482 | | dispatch_invoke_in_context_func (gpointer user_data) |
483 | 0 | { |
484 | 0 | DispatchData *data = user_data; |
485 | 0 | data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation), |
486 | 0 | g_dbus_method_invocation_get_sender (data->invocation), |
487 | 0 | g_dbus_method_invocation_get_object_path (data->invocation), |
488 | 0 | g_dbus_method_invocation_get_interface_name (data->invocation), |
489 | 0 | g_dbus_method_invocation_get_method_name (data->invocation), |
490 | 0 | g_dbus_method_invocation_get_parameters (data->invocation), |
491 | 0 | data->invocation, |
492 | 0 | g_dbus_method_invocation_get_user_data (data->invocation)); |
493 | 0 | return FALSE; |
494 | 0 | } |
495 | | |
496 | | static void |
497 | | dispatch_in_thread_func (GTask *task, |
498 | | gpointer source_object, |
499 | | gpointer task_data, |
500 | | GCancellable *cancellable) |
501 | 0 | { |
502 | 0 | DispatchData *data = task_data; |
503 | 0 | GDBusInterfaceSkeletonFlags flags; |
504 | 0 | GDBusObject *object; |
505 | 0 | gboolean authorized; |
506 | |
|
507 | 0 | g_mutex_lock (&data->interface->priv->lock); |
508 | 0 | flags = data->interface->priv->flags; |
509 | 0 | object = data->interface->priv->object; |
510 | 0 | if (object != NULL) |
511 | 0 | g_object_ref (object); |
512 | 0 | g_mutex_unlock (&data->interface->priv->lock); |
513 | | |
514 | | /* first check on the enclosing object (if any), then the interface */ |
515 | 0 | authorized = TRUE; |
516 | 0 | if (object != NULL) |
517 | 0 | { |
518 | 0 | g_signal_emit_by_name (object, |
519 | 0 | "authorize-method", |
520 | 0 | data->interface, |
521 | 0 | data->invocation, |
522 | 0 | &authorized); |
523 | 0 | } |
524 | 0 | if (authorized) |
525 | 0 | { |
526 | 0 | g_signal_emit (data->interface, |
527 | 0 | signals[G_AUTHORIZE_METHOD_SIGNAL], |
528 | 0 | 0, |
529 | 0 | data->invocation, |
530 | 0 | &authorized); |
531 | 0 | } |
532 | |
|
533 | 0 | if (authorized) |
534 | 0 | { |
535 | 0 | gboolean run_in_thread; |
536 | 0 | run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD); |
537 | 0 | if (run_in_thread) |
538 | 0 | { |
539 | | /* might as well just re-use the existing thread */ |
540 | 0 | data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation), |
541 | 0 | g_dbus_method_invocation_get_sender (data->invocation), |
542 | 0 | g_dbus_method_invocation_get_object_path (data->invocation), |
543 | 0 | g_dbus_method_invocation_get_interface_name (data->invocation), |
544 | 0 | g_dbus_method_invocation_get_method_name (data->invocation), |
545 | 0 | g_dbus_method_invocation_get_parameters (data->invocation), |
546 | 0 | data->invocation, |
547 | 0 | g_dbus_method_invocation_get_user_data (data->invocation)); |
548 | 0 | } |
549 | 0 | else |
550 | 0 | { |
551 | | /* bah, back to original context */ |
552 | 0 | g_main_context_invoke_full (g_task_get_context (task), |
553 | 0 | g_task_get_priority (task), |
554 | 0 | dispatch_invoke_in_context_func, |
555 | 0 | dispatch_data_ref (data), |
556 | 0 | (GDestroyNotify) dispatch_data_unref); |
557 | 0 | } |
558 | 0 | } |
559 | 0 | else |
560 | 0 | { |
561 | | /* do nothing */ |
562 | 0 | } |
563 | |
|
564 | 0 | if (object != NULL) |
565 | 0 | g_object_unref (object); |
566 | 0 | } |
567 | | |
568 | | static void |
569 | | g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton *interface, |
570 | | GDBusInterfaceMethodCallFunc method_call_func, |
571 | | GDBusMethodInvocation *invocation) |
572 | 0 | { |
573 | 0 | gboolean has_handlers; |
574 | 0 | gboolean has_default_class_handler; |
575 | 0 | gboolean emit_authorized_signal; |
576 | 0 | gboolean run_in_thread; |
577 | 0 | GDBusInterfaceSkeletonFlags flags; |
578 | 0 | GDBusObject *object; |
579 | |
|
580 | 0 | g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface)); |
581 | 0 | g_return_if_fail (method_call_func != NULL); |
582 | 0 | g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation)); |
583 | | |
584 | 0 | g_mutex_lock (&interface->priv->lock); |
585 | 0 | flags = interface->priv->flags; |
586 | 0 | object = interface->priv->object; |
587 | 0 | if (object != NULL) |
588 | 0 | g_object_ref (object); |
589 | 0 | g_mutex_unlock (&interface->priv->lock); |
590 | | |
591 | | /* optimization for the common case where |
592 | | * |
593 | | * a) no handler is connected and class handler is not overridden (both interface and object); and |
594 | | * b) method calls are not dispatched in a thread |
595 | | */ |
596 | 0 | has_handlers = g_signal_has_handler_pending (interface, |
597 | 0 | signals[G_AUTHORIZE_METHOD_SIGNAL], |
598 | 0 | 0, |
599 | 0 | TRUE); |
600 | 0 | has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method == |
601 | 0 | g_dbus_interface_skeleton_g_authorize_method_default); |
602 | |
|
603 | 0 | emit_authorized_signal = (has_handlers || !has_default_class_handler); |
604 | 0 | if (!emit_authorized_signal) |
605 | 0 | { |
606 | 0 | if (object != NULL) |
607 | 0 | emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object)); |
608 | 0 | } |
609 | |
|
610 | 0 | run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD); |
611 | 0 | if (!emit_authorized_signal && !run_in_thread) |
612 | 0 | { |
613 | 0 | method_call_func (g_dbus_method_invocation_get_connection (invocation), |
614 | 0 | g_dbus_method_invocation_get_sender (invocation), |
615 | 0 | g_dbus_method_invocation_get_object_path (invocation), |
616 | 0 | g_dbus_method_invocation_get_interface_name (invocation), |
617 | 0 | g_dbus_method_invocation_get_method_name (invocation), |
618 | 0 | g_dbus_method_invocation_get_parameters (invocation), |
619 | 0 | invocation, |
620 | 0 | g_dbus_method_invocation_get_user_data (invocation)); |
621 | 0 | } |
622 | 0 | else |
623 | 0 | { |
624 | 0 | GTask *task; |
625 | 0 | DispatchData *data; |
626 | |
|
627 | 0 | data = g_slice_new0 (DispatchData); |
628 | 0 | data->interface = interface; |
629 | 0 | data->method_call_func = method_call_func; |
630 | 0 | data->invocation = invocation; |
631 | 0 | data->ref_count = 1; |
632 | |
|
633 | 0 | task = g_task_new (interface, NULL, NULL, NULL); |
634 | 0 | g_task_set_source_tag (task, g_dbus_interface_method_dispatch_helper); |
635 | 0 | g_task_set_name (task, "[gio] D-Bus interface method dispatch"); |
636 | 0 | g_task_set_task_data (task, data, (GDestroyNotify) dispatch_data_unref); |
637 | 0 | g_task_run_in_thread (task, dispatch_in_thread_func); |
638 | 0 | g_object_unref (task); |
639 | 0 | } |
640 | |
|
641 | 0 | if (object != NULL) |
642 | 0 | g_object_unref (object); |
643 | 0 | } |
644 | | |
645 | | static void |
646 | | skeleton_intercept_handle_method_call (GDBusConnection *connection, |
647 | | const gchar *sender, |
648 | | const gchar *object_path, |
649 | | const gchar *interface_name, |
650 | | const gchar *method_name, |
651 | | GVariant *parameters, |
652 | | GDBusMethodInvocation *invocation, |
653 | | gpointer user_data) |
654 | 0 | { |
655 | 0 | GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data); |
656 | 0 | g_dbus_interface_method_dispatch_helper (interface, |
657 | 0 | g_dbus_interface_skeleton_get_vtable (interface)->method_call, |
658 | 0 | invocation); |
659 | 0 | } |
660 | | |
661 | | /* ---------------------------------------------------------------------------------------------------- */ |
662 | | |
663 | | static ConnectionData * |
664 | | new_connection (GDBusConnection *connection, |
665 | | guint registration_id) |
666 | 0 | { |
667 | 0 | ConnectionData *data; |
668 | |
|
669 | 0 | data = g_slice_new0 (ConnectionData); |
670 | 0 | data->connection = g_object_ref (connection); |
671 | 0 | data->registration_id = registration_id; |
672 | |
|
673 | 0 | return data; |
674 | 0 | } |
675 | | |
676 | | static void |
677 | | free_connection (ConnectionData *data) |
678 | 0 | { |
679 | 0 | if (data != NULL) |
680 | 0 | { |
681 | 0 | g_object_unref (data->connection); |
682 | 0 | g_slice_free (ConnectionData, data); |
683 | 0 | } |
684 | 0 | } |
685 | | |
686 | | static gboolean |
687 | | add_connection_locked (GDBusInterfaceSkeleton *interface_, |
688 | | GDBusConnection *connection, |
689 | | GError **error) |
690 | 0 | { |
691 | 0 | ConnectionData *data; |
692 | 0 | guint registration_id; |
693 | 0 | gboolean ret = FALSE; |
694 | |
|
695 | 0 | if (interface_->priv->hooked_vtable == NULL) |
696 | 0 | { |
697 | | /* Hook the vtable since we need to intercept method calls for |
698 | | * ::g-authorize-method and for dispatching in thread vs |
699 | | * context |
700 | | * |
701 | | * We need to wait until subclasses have had time to initialize |
702 | | * properly before building the hooked_vtable, so we create it |
703 | | * once at the last minute. |
704 | | */ |
705 | 0 | interface_->priv->hooked_vtable = g_memdup2 (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable)); |
706 | 0 | interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call; |
707 | 0 | } |
708 | |
|
709 | 0 | registration_id = g_dbus_connection_register_object (connection, |
710 | 0 | interface_->priv->object_path, |
711 | 0 | g_dbus_interface_skeleton_get_info (interface_), |
712 | 0 | interface_->priv->hooked_vtable, |
713 | 0 | interface_, |
714 | 0 | NULL, /* user_data_free_func */ |
715 | 0 | error); |
716 | |
|
717 | 0 | if (registration_id > 0) |
718 | 0 | { |
719 | 0 | data = new_connection (connection, registration_id); |
720 | 0 | interface_->priv->connections = g_slist_append (interface_->priv->connections, data); |
721 | 0 | ret = TRUE; |
722 | 0 | } |
723 | |
|
724 | 0 | return ret; |
725 | 0 | } |
726 | | |
727 | | static void |
728 | | remove_connection_locked (GDBusInterfaceSkeleton *interface_, |
729 | | GDBusConnection *connection) |
730 | 0 | { |
731 | 0 | ConnectionData *data; |
732 | 0 | GSList *l; |
733 | | |
734 | | /* Get the connection in the list and unregister ... */ |
735 | 0 | for (l = interface_->priv->connections; l != NULL; l = l->next) |
736 | 0 | { |
737 | 0 | data = l->data; |
738 | 0 | if (data->connection == connection) |
739 | 0 | { |
740 | 0 | g_warn_if_fail (g_dbus_connection_unregister_object (data->connection, data->registration_id)); |
741 | 0 | free_connection (data); |
742 | 0 | interface_->priv->connections = g_slist_delete_link (interface_->priv->connections, l); |
743 | | /* we are guaranteed that the connection is only added once, so bail out early */ |
744 | 0 | goto out; |
745 | 0 | } |
746 | 0 | } |
747 | 0 | out: |
748 | 0 | ; |
749 | 0 | } |
750 | | |
751 | | static void |
752 | | set_object_path_locked (GDBusInterfaceSkeleton *interface_, |
753 | | const gchar *object_path) |
754 | 0 | { |
755 | 0 | if (g_strcmp0 (interface_->priv->object_path, object_path) != 0) |
756 | 0 | { |
757 | 0 | g_free (interface_->priv->object_path); |
758 | 0 | interface_->priv->object_path = g_strdup (object_path); |
759 | 0 | } |
760 | 0 | } |
761 | | |
762 | | /* ---------------------------------------------------------------------------------------------------- */ |
763 | | |
764 | | /** |
765 | | * g_dbus_interface_skeleton_get_connection: |
766 | | * @interface_: A #GDBusInterfaceSkeleton. |
767 | | * |
768 | | * Gets the first connection that @interface_ is exported on, if any. |
769 | | * |
770 | | * Returns: (nullable) (transfer none): A #GDBusConnection or %NULL if @interface_ is |
771 | | * not exported anywhere. Do not free, the object belongs to @interface_. |
772 | | * |
773 | | * Since: 2.30 |
774 | | */ |
775 | | GDBusConnection * |
776 | | g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_) |
777 | 0 | { |
778 | 0 | ConnectionData *data; |
779 | 0 | GDBusConnection *ret; |
780 | |
|
781 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL); |
782 | 0 | g_mutex_lock (&interface_->priv->lock); |
783 | |
|
784 | 0 | ret = NULL; |
785 | 0 | if (interface_->priv->connections != NULL) |
786 | 0 | { |
787 | 0 | data = interface_->priv->connections->data; |
788 | 0 | if (data != NULL) |
789 | 0 | ret = data->connection; |
790 | 0 | } |
791 | |
|
792 | 0 | g_mutex_unlock (&interface_->priv->lock); |
793 | |
|
794 | 0 | return ret; |
795 | 0 | } |
796 | | |
797 | | /** |
798 | | * g_dbus_interface_skeleton_get_connections: |
799 | | * @interface_: A #GDBusInterfaceSkeleton. |
800 | | * |
801 | | * Gets a list of the connections that @interface_ is exported on. |
802 | | * |
803 | | * Returns: (element-type GDBusConnection) (transfer full): A list of |
804 | | * all the connections that @interface_ is exported on. The returned |
805 | | * list should be freed with g_list_free() after each element has |
806 | | * been freed with g_object_unref(). |
807 | | * |
808 | | * Since: 2.32 |
809 | | */ |
810 | | GList * |
811 | | g_dbus_interface_skeleton_get_connections (GDBusInterfaceSkeleton *interface_) |
812 | 0 | { |
813 | 0 | GList *connections; |
814 | 0 | GSList *l; |
815 | 0 | ConnectionData *data; |
816 | |
|
817 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL); |
818 | | |
819 | 0 | g_mutex_lock (&interface_->priv->lock); |
820 | 0 | connections = NULL; |
821 | |
|
822 | 0 | for (l = interface_->priv->connections; l != NULL; l = l->next) |
823 | 0 | { |
824 | 0 | data = l->data; |
825 | 0 | connections = g_list_prepend (connections, |
826 | | /* Return a reference to each connection */ |
827 | 0 | g_object_ref (data->connection)); |
828 | 0 | } |
829 | |
|
830 | 0 | g_mutex_unlock (&interface_->priv->lock); |
831 | |
|
832 | 0 | return g_list_reverse (connections); |
833 | 0 | } |
834 | | |
835 | | /** |
836 | | * g_dbus_interface_skeleton_has_connection: |
837 | | * @interface_: A #GDBusInterfaceSkeleton. |
838 | | * @connection: A #GDBusConnection. |
839 | | * |
840 | | * Checks if @interface_ is exported on @connection. |
841 | | * |
842 | | * Returns: %TRUE if @interface_ is exported on @connection, %FALSE otherwise. |
843 | | * |
844 | | * Since: 2.32 |
845 | | */ |
846 | | gboolean |
847 | | g_dbus_interface_skeleton_has_connection (GDBusInterfaceSkeleton *interface_, |
848 | | GDBusConnection *connection) |
849 | 0 | { |
850 | 0 | GSList *l; |
851 | 0 | gboolean ret = FALSE; |
852 | |
|
853 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE); |
854 | 0 | g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
855 | | |
856 | 0 | g_mutex_lock (&interface_->priv->lock); |
857 | |
|
858 | 0 | for (l = interface_->priv->connections; l != NULL; l = l->next) |
859 | 0 | { |
860 | 0 | ConnectionData *data = l->data; |
861 | 0 | if (data->connection == connection) |
862 | 0 | { |
863 | 0 | ret = TRUE; |
864 | 0 | goto out; |
865 | 0 | } |
866 | 0 | } |
867 | | |
868 | 0 | out: |
869 | 0 | g_mutex_unlock (&interface_->priv->lock); |
870 | 0 | return ret; |
871 | 0 | } |
872 | | |
873 | | /** |
874 | | * g_dbus_interface_skeleton_get_object_path: |
875 | | * @interface_: A #GDBusInterfaceSkeleton. |
876 | | * |
877 | | * Gets the object path that @interface_ is exported on, if any. |
878 | | * |
879 | | * Returns: (nullable): A string owned by @interface_ or %NULL if @interface_ is not exported |
880 | | * anywhere. Do not free, the string belongs to @interface_. |
881 | | * |
882 | | * Since: 2.30 |
883 | | */ |
884 | | const gchar * |
885 | | g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_) |
886 | 0 | { |
887 | 0 | const gchar *ret; |
888 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL); |
889 | 0 | g_mutex_lock (&interface_->priv->lock); |
890 | 0 | ret = interface_->priv->object_path; |
891 | 0 | g_mutex_unlock (&interface_->priv->lock); |
892 | 0 | return ret; |
893 | 0 | } |
894 | | |
895 | | /** |
896 | | * g_dbus_interface_skeleton_export: |
897 | | * @interface_: The D-Bus interface to export. |
898 | | * @connection: A #GDBusConnection to export @interface_ on. |
899 | | * @object_path: The path to export the interface at. |
900 | | * @error: Return location for error or %NULL. |
901 | | * |
902 | | * Exports @interface_ at @object_path on @connection. |
903 | | * |
904 | | * This can be called multiple times to export the same @interface_ |
905 | | * onto multiple connections however the @object_path provided must be |
906 | | * the same for all connections. |
907 | | * |
908 | | * Use g_dbus_interface_skeleton_unexport() to unexport the object. |
909 | | * |
910 | | * Returns: %TRUE if the interface was exported on @connection, otherwise %FALSE with |
911 | | * @error set. |
912 | | * |
913 | | * Since: 2.30 |
914 | | */ |
915 | | gboolean |
916 | | g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton *interface_, |
917 | | GDBusConnection *connection, |
918 | | const gchar *object_path, |
919 | | GError **error) |
920 | 0 | { |
921 | 0 | gboolean ret = FALSE; |
922 | |
|
923 | 0 | g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE); |
924 | 0 | g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
925 | 0 | g_return_val_if_fail (g_variant_is_object_path (object_path), FALSE); |
926 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
927 | | |
928 | | /* Assert that the object path is the same for multiple connections here */ |
929 | 0 | g_return_val_if_fail (interface_->priv->object_path == NULL || |
930 | 0 | g_strcmp0 (interface_->priv->object_path, object_path) == 0, FALSE); |
931 | | |
932 | 0 | g_mutex_lock (&interface_->priv->lock); |
933 | | |
934 | | /* Set the object path */ |
935 | 0 | set_object_path_locked (interface_, object_path); |
936 | | |
937 | | /* Add the connection */ |
938 | 0 | ret = add_connection_locked (interface_, connection, error); |
939 | |
|
940 | 0 | g_mutex_unlock (&interface_->priv->lock); |
941 | 0 | return ret; |
942 | 0 | } |
943 | | |
944 | | /** |
945 | | * g_dbus_interface_skeleton_unexport: |
946 | | * @interface_: A #GDBusInterfaceSkeleton. |
947 | | * |
948 | | * Stops exporting @interface_ on all connections it is exported on. |
949 | | * |
950 | | * To unexport @interface_ from only a single connection, use |
951 | | * g_dbus_interface_skeleton_unexport_from_connection() |
952 | | * |
953 | | * Since: 2.30 |
954 | | */ |
955 | | void |
956 | | g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_) |
957 | 0 | { |
958 | 0 | g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_)); |
959 | 0 | g_return_if_fail (interface_->priv->connections != NULL); |
960 | | |
961 | 0 | g_mutex_lock (&interface_->priv->lock); |
962 | |
|
963 | 0 | g_assert (interface_->priv->object_path != NULL); |
964 | 0 | g_assert (interface_->priv->hooked_vtable != NULL); |
965 | | |
966 | | /* Remove all connections */ |
967 | 0 | while (interface_->priv->connections != NULL) |
968 | 0 | { |
969 | 0 | ConnectionData *data = interface_->priv->connections->data; |
970 | 0 | remove_connection_locked (interface_, data->connection); |
971 | 0 | } |
972 | | |
973 | | /* Unset the object path since there are no connections left */ |
974 | 0 | set_object_path_locked (interface_, NULL); |
975 | |
|
976 | 0 | g_mutex_unlock (&interface_->priv->lock); |
977 | 0 | } |
978 | | |
979 | | |
980 | | /** |
981 | | * g_dbus_interface_skeleton_unexport_from_connection: |
982 | | * @interface_: A #GDBusInterfaceSkeleton. |
983 | | * @connection: A #GDBusConnection. |
984 | | * |
985 | | * Stops exporting @interface_ on @connection. |
986 | | * |
987 | | * To stop exporting on all connections the interface is exported on, |
988 | | * use g_dbus_interface_skeleton_unexport(). |
989 | | * |
990 | | * Since: 2.32 |
991 | | */ |
992 | | void |
993 | | g_dbus_interface_skeleton_unexport_from_connection (GDBusInterfaceSkeleton *interface_, |
994 | | GDBusConnection *connection) |
995 | 0 | { |
996 | 0 | g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_)); |
997 | 0 | g_return_if_fail (G_IS_DBUS_CONNECTION (connection)); |
998 | 0 | g_return_if_fail (interface_->priv->connections != NULL); |
999 | | |
1000 | 0 | g_mutex_lock (&interface_->priv->lock); |
1001 | |
|
1002 | 0 | g_assert (interface_->priv->object_path != NULL); |
1003 | 0 | g_assert (interface_->priv->hooked_vtable != NULL); |
1004 | | |
1005 | 0 | remove_connection_locked (interface_, connection); |
1006 | | |
1007 | | /* Reset the object path if we removed the last connection */ |
1008 | 0 | if (interface_->priv->connections == NULL) |
1009 | 0 | set_object_path_locked (interface_, NULL); |
1010 | |
|
1011 | 0 | g_mutex_unlock (&interface_->priv->lock); |
1012 | 0 | } |
1013 | | |
1014 | | /* ---------------------------------------------------------------------------------------------------- */ |