]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-dbus.c
dbus: Do message argument type checking early, centrally.
[pulseaudio] / src / pulsecore / protocol-dbus.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Tanu Kaskinen
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dbus/dbus.h>
27
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/dbus-util.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/idxset.h>
34 #include <pulsecore/shared.h>
35 #include <pulsecore/strbuf.h>
36
37 #include "protocol-dbus.h"
38
39 struct pa_dbus_protocol {
40 PA_REFCNT_DECLARE;
41
42 pa_core *core;
43 pa_hashmap *objects; /* Object path -> struct object_entry */
44 pa_hashmap *connections; /* DBusConnection -> struct connection_entry */
45 pa_idxset *extensions; /* Strings */
46
47 pa_hook hooks[PA_DBUS_PROTOCOL_HOOK_MAX];
48 };
49
50 struct object_entry {
51 char *path;
52 pa_hashmap *interfaces; /* Interface name -> struct interface_entry */
53 char *introspection;
54 };
55
56 struct connection_entry {
57 DBusConnection *connection;
58 pa_client *client;
59
60 pa_bool_t listening_for_all_signals;
61
62 /* Contains object paths. If this is empty, then signals from all objects
63 * are accepted. Only used when listening_for_all_signals == TRUE. */
64 pa_idxset *all_signals_objects;
65
66 /* Signal name -> idxset. The idxsets contain object paths. If an idxset is
67 * empty, then that signal is accepted from all objects. Only used when
68 * listening_for_all_signals == FALSE. */
69 pa_hashmap *listening_signals;
70 };
71
72 struct interface_entry {
73 char *name;
74 pa_hashmap *method_handlers;
75 pa_hashmap *property_handlers;
76 pa_dbus_receive_cb_t get_all_properties_cb;
77 pa_dbus_signal_info *signals;
78 unsigned n_signals;
79 void *userdata;
80 };
81
82 char *pa_get_dbus_address_from_server_type(pa_server_type_t server_type) {
83 char *address = NULL;
84 char *runtime_path = NULL;
85 char *escaped_path = NULL;
86
87 switch (server_type) {
88 case PA_SERVER_TYPE_USER:
89 pa_assert_se((runtime_path = pa_runtime_path(PA_DBUS_SOCKET_NAME)));
90 pa_assert_se((escaped_path = dbus_address_escape_value(runtime_path)));
91 address = pa_sprintf_malloc("unix:path=%s", escaped_path);
92 break;
93
94 case PA_SERVER_TYPE_SYSTEM:
95 pa_assert_se((escaped_path = dbus_address_escape_value(PA_DBUS_SYSTEM_SOCKET_PATH)));
96 address = pa_sprintf_malloc("unix:path=%s", escaped_path);
97 break;
98
99 case PA_SERVER_TYPE_NONE:
100 address = pa_xnew0(char, 1);
101 break;
102
103 default:
104 pa_assert_not_reached();
105 }
106
107 pa_xfree(runtime_path);
108 pa_xfree(escaped_path);
109
110 return address;
111 }
112
113 static pa_dbus_protocol *dbus_protocol_new(pa_core *c) {
114 pa_dbus_protocol *p;
115 unsigned i;
116
117 pa_assert(c);
118
119 p = pa_xnew(pa_dbus_protocol, 1);
120 PA_REFCNT_INIT(p);
121 p->core = pa_core_ref(c);
122 p->objects = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
123 p->connections = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
124 p->extensions = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
125
126 for (i = 0; i < PA_DBUS_PROTOCOL_HOOK_MAX; ++i)
127 pa_hook_init(&p->hooks[i], p);
128
129 pa_assert_se(pa_shared_set(c, "dbus-protocol", p) >= 0);
130
131 return p;
132 }
133
134 pa_dbus_protocol* pa_dbus_protocol_get(pa_core *c) {
135 pa_dbus_protocol *p;
136
137 if ((p = pa_shared_get(c, "dbus-protocol")))
138 return pa_dbus_protocol_ref(p);
139
140 return dbus_protocol_new(c);
141 }
142
143 pa_dbus_protocol* pa_dbus_protocol_ref(pa_dbus_protocol *p) {
144 pa_assert(p);
145 pa_assert(PA_REFCNT_VALUE(p) >= 1);
146
147 PA_REFCNT_INC(p);
148
149 return p;
150 }
151
152 void pa_dbus_protocol_unref(pa_dbus_protocol *p) {
153 unsigned i;
154
155 pa_assert(p);
156 pa_assert(PA_REFCNT_VALUE(p) >= 1);
157
158 if (PA_REFCNT_DEC(p) > 0)
159 return;
160
161 pa_assert(pa_hashmap_isempty(p->objects));
162 pa_assert(pa_hashmap_isempty(p->connections));
163 pa_assert(pa_idxset_isempty(p->extensions));
164
165 pa_hashmap_free(p->objects, NULL, NULL);
166 pa_hashmap_free(p->connections, NULL, NULL);
167 pa_idxset_free(p->extensions, NULL, NULL);
168
169 for (i = 0; i < PA_DBUS_PROTOCOL_HOOK_MAX; ++i)
170 pa_hook_done(&p->hooks[i]);
171
172 pa_assert_se(pa_shared_remove(p->core, "dbus-protocol") >= 0);
173
174 pa_core_unref(p->core);
175
176 pa_xfree(p);
177 }
178
179 static void update_introspection(struct object_entry *oe) {
180 pa_strbuf *buf;
181 void *interfaces_state = NULL;
182 struct interface_entry *iface_entry = NULL;
183
184 pa_assert(oe);
185
186 buf = pa_strbuf_new();
187 pa_strbuf_puts(buf, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
188 pa_strbuf_puts(buf, "<node>\n");
189
190 PA_HASHMAP_FOREACH(iface_entry, oe->interfaces, interfaces_state) {
191 pa_dbus_method_handler *method_handler;
192 pa_dbus_property_handler *property_handler;
193 void *handlers_state = NULL;
194 unsigned i;
195 unsigned j;
196
197 pa_strbuf_printf(buf, " <interface name=\"%s\">\n", iface_entry->name);
198
199 PA_HASHMAP_FOREACH(method_handler, iface_entry->method_handlers, handlers_state) {
200 pa_strbuf_printf(buf, " <method name=\"%s\">\n", method_handler->method_name);
201
202 for (i = 0; i < method_handler->n_arguments; ++i)
203 pa_strbuf_printf(buf, " <arg name=\"%s\" type=\"%s\" direction=\"%s\"/>\n",
204 method_handler->arguments[i].name,
205 method_handler->arguments[i].type,
206 method_handler->arguments[i].direction);
207
208 pa_strbuf_puts(buf, " </method>\n");
209 }
210
211 handlers_state = NULL;
212
213 PA_HASHMAP_FOREACH(property_handler, iface_entry->property_handlers, handlers_state)
214 pa_strbuf_printf(buf, " <property name=\"%s\" type=\"%s\" access=\"%s\"/>\n",
215 property_handler->property_name,
216 property_handler->type,
217 property_handler->get_cb ? (property_handler->set_cb ? "readwrite" : "read") : "write");
218
219 for (i = 0; i < iface_entry->n_signals; ++i) {
220 pa_strbuf_printf(buf, " <signal name=\"%s\">\n", iface_entry->signals[i].name);
221
222 for (j = 0; j < iface_entry->signals[i].n_arguments; ++j)
223 pa_strbuf_printf(buf, " <arg name=\"%s\" type=\"%s\"/>\n", iface_entry->signals[i].arguments[j].name,
224 iface_entry->signals[i].arguments[j].type);
225
226 pa_strbuf_puts(buf, " </signal>\n");
227 }
228
229 pa_strbuf_puts(buf, " </interface>\n");
230 }
231
232 pa_strbuf_puts(buf, " <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE "\">\n"
233 " <method name=\"Introspect\">\n"
234 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
235 " </method>\n"
236 " </interface>\n"
237 " <interface name=\"" DBUS_INTERFACE_PROPERTIES "\">\n"
238 " <method name=\"Get\">\n"
239 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
240 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
241 " <arg name=\"value\" type=\"v\" direction=\"out\"/>\n"
242 " </method>\n"
243 " <method name=\"Set\">\n"
244 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
245 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
246 " <arg name=\"value\" type=\"v\" direction=\"in\"/>\n"
247 " </method>\n"
248 " <method name=\"GetAll\">\n"
249 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
250 " <arg name=\"props\" type=\"a{sv}\" direction=\"out\"/>\n"
251 " </method>\n"
252 " </interface>\n");
253
254 pa_strbuf_puts(buf, "</node>\n");
255
256 pa_xfree(oe->introspection);
257 oe->introspection = pa_strbuf_tostring_free(buf);
258 }
259
260 /* Return value of find_handler() and its subfunctions. */
261 enum find_result_t {
262 /* The received message is a valid .Get call. */
263 FOUND_GET_PROPERTY,
264
265 /* The received message is a valid .Set call. */
266 FOUND_SET_PROPERTY,
267
268 /* The received message is a valid .GetAll call. */
269 FOUND_GET_ALL,
270
271 /* The received message is a valid method call. */
272 FOUND_METHOD,
273
274 /* The interface of the received message hasn't been registered for the
275 * destination object. */
276 NO_SUCH_INTERFACE,
277
278 /* No property handler was found for the received .Get or .Set call. */
279 NO_SUCH_PROPERTY,
280
281 /* The interface argument of a property call didn't match any registered
282 * interface. */
283 NO_SUCH_PROPERTY_INTERFACE,
284
285 /* The received message called .Get or .Set for a property whose access
286 * mode doesn't match the call. */
287 PROPERTY_ACCESS_DENIED,
288
289 /* The new value signature of a .Set call didn't match the expexted
290 * signature. */
291 INVALID_PROPERTY_SIG,
292
293 /* No method handler was found for the received message. */
294 NO_SUCH_METHOD,
295
296 /* The signature of the received message didn't match the expected
297 * signature. Despite the name, this can also be returned for a property
298 * call if its message signature is invalid. */
299 INVALID_METHOD_SIG
300 };
301
302 /* Data for resolving the correct reaction to a received message. */
303 struct call_info {
304 DBusMessage *message; /* The received message. */
305 struct object_entry *obj_entry;
306 const char *interface; /* Destination interface name (extracted from the message). */
307 struct interface_entry *iface_entry;
308
309 const char *property; /* Property name (extracted from the message). */
310 const char *property_interface; /* The interface argument of a property call is stored here. */
311 pa_dbus_property_handler *property_handler;
312 const char *expected_property_sig; /* Property signature from the introspection data. */
313 const char *property_sig; /* The signature of the new value in the received .Set message. */
314 DBusMessageIter variant_iter; /* Iterator pointing to the beginning of the new value variant of a .Set call. */
315
316 const char *method; /* Method name (extracted from the message). */
317 pa_dbus_method_handler *method_handler;
318 const char *expected_method_sig; /* Method signature from the introspection data. */
319 const char *method_sig; /* The signature of the received message. */
320 };
321
322 /* Called when call_info->property has been set and the property interface has
323 * not been given. In case of a Set call, call_info->property_sig is also set,
324 * which is checked against the expected value in this function. */
325 static enum find_result_t find_handler_by_property(struct call_info *call_info) {
326 void *state = NULL;
327
328 pa_assert(call_info);
329
330 PA_HASHMAP_FOREACH(call_info->iface_entry, call_info->obj_entry->interfaces, state) {
331 if ((call_info->property_handler = pa_hashmap_get(call_info->iface_entry->property_handlers, call_info->property))) {
332 if (pa_streq(call_info->method, "Get"))
333 return call_info->property_handler->get_cb ? FOUND_GET_PROPERTY : PROPERTY_ACCESS_DENIED;
334
335 else if (pa_streq(call_info->method, "Set")) {
336 call_info->expected_property_sig = call_info->property_handler->type;
337
338 if (pa_streq(call_info->property_sig, call_info->expected_property_sig))
339 return call_info->property_handler->set_cb ? FOUND_SET_PROPERTY : PROPERTY_ACCESS_DENIED;
340 else
341 return INVALID_PROPERTY_SIG;
342
343 } else
344 pa_assert_not_reached();
345 }
346 }
347
348 return NO_SUCH_PROPERTY;
349 }
350
351 static enum find_result_t find_handler_by_method(struct call_info *call_info) {
352 void *state = NULL;
353
354 pa_assert(call_info);
355
356 PA_HASHMAP_FOREACH(call_info->iface_entry, call_info->obj_entry->interfaces, state) {
357 if ((call_info->method_handler = pa_hashmap_get(call_info->iface_entry->method_handlers, call_info->method)))
358 return FOUND_METHOD;
359 }
360
361 return NO_SUCH_METHOD;
362 }
363
364 static enum find_result_t find_handler_from_properties_call(struct call_info *call_info) {
365 pa_assert(call_info);
366
367 if (pa_streq(call_info->method, "GetAll")) {
368 call_info->expected_method_sig = "s";
369 if (!pa_streq(call_info->method_sig, call_info->expected_method_sig))
370 return INVALID_METHOD_SIG;
371
372 pa_assert_se(dbus_message_get_args(call_info->message, NULL,
373 DBUS_TYPE_STRING, &call_info->property_interface,
374 DBUS_TYPE_INVALID));
375
376 if (*call_info->property_interface) {
377 if ((call_info->iface_entry = pa_hashmap_get(call_info->obj_entry->interfaces, call_info->property_interface)))
378 return FOUND_GET_ALL;
379 else
380 return NO_SUCH_PROPERTY_INTERFACE;
381
382 } else {
383 pa_assert_se(call_info->iface_entry = pa_hashmap_first(call_info->obj_entry->interfaces));
384 return FOUND_GET_ALL;
385 }
386
387 } else if (pa_streq(call_info->method, "Get")) {
388 call_info->expected_method_sig = "ss";
389 if (!pa_streq(call_info->method_sig, call_info->expected_method_sig))
390 return INVALID_METHOD_SIG;
391
392 pa_assert_se(dbus_message_get_args(call_info->message, NULL,
393 DBUS_TYPE_STRING, &call_info->property_interface,
394 DBUS_TYPE_STRING, &call_info->property,
395 DBUS_TYPE_INVALID));
396
397 if (*call_info->property_interface) {
398 if (!(call_info->iface_entry = pa_hashmap_get(call_info->obj_entry->interfaces, call_info->property_interface)))
399 return NO_SUCH_PROPERTY_INTERFACE;
400 else if ((call_info->property_handler =
401 pa_hashmap_get(call_info->iface_entry->property_handlers, call_info->property)))
402 return FOUND_GET_PROPERTY;
403 else
404 return NO_SUCH_PROPERTY;
405
406 } else
407 return find_handler_by_property(call_info);
408
409 } else if (pa_streq(call_info->method, "Set")) {
410 DBusMessageIter msg_iter;
411
412 call_info->expected_method_sig = "ssv";
413 if (!pa_streq(call_info->method_sig, call_info->expected_method_sig))
414 return INVALID_METHOD_SIG;
415
416 pa_assert_se(dbus_message_iter_init(call_info->message, &msg_iter));
417
418 dbus_message_iter_get_basic(&msg_iter, &call_info->property_interface);
419 pa_assert_se(dbus_message_iter_next(&msg_iter));
420 dbus_message_iter_get_basic(&msg_iter, &call_info->property);
421 pa_assert_se(dbus_message_iter_next(&msg_iter));
422
423 dbus_message_iter_recurse(&msg_iter, &call_info->variant_iter);
424
425 call_info->property_sig = dbus_message_iter_get_signature(&call_info->variant_iter);
426
427 if (*call_info->property_interface) {
428 if (!(call_info->iface_entry = pa_hashmap_get(call_info->obj_entry->interfaces, call_info->property_interface)))
429 return NO_SUCH_PROPERTY_INTERFACE;
430
431 else if ((call_info->property_handler =
432 pa_hashmap_get(call_info->iface_entry->property_handlers, call_info->property))) {
433 call_info->expected_property_sig = call_info->property_handler->type;
434
435 if (pa_streq(call_info->property_sig, call_info->expected_property_sig))
436 return FOUND_SET_PROPERTY;
437 else
438 return INVALID_PROPERTY_SIG;
439
440 } else
441 return NO_SUCH_PROPERTY;
442
443 } else
444 return find_handler_by_property(call_info);
445
446 } else
447 pa_assert_not_reached();
448 }
449
450 static enum find_result_t find_handler(struct call_info *call_info) {
451 pa_assert(call_info);
452
453 if (call_info->interface) {
454 if (pa_streq(call_info->interface, DBUS_INTERFACE_PROPERTIES))
455 return find_handler_from_properties_call(call_info);
456
457 else if (!(call_info->iface_entry = pa_hashmap_get(call_info->obj_entry->interfaces, call_info->interface)))
458 return NO_SUCH_INTERFACE;
459
460 else if ((call_info->method_handler = pa_hashmap_get(call_info->iface_entry->method_handlers, call_info->method)))
461 return FOUND_METHOD;
462
463 else
464 return NO_SUCH_METHOD;
465
466 } else { /* The method call doesn't contain an interface. */
467 if (pa_streq(call_info->method, "Get") || pa_streq(call_info->method, "Set") || pa_streq(call_info->method, "GetAll")) {
468 if (find_handler_by_method(call_info) == FOUND_METHOD)
469 /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
470 return FOUND_METHOD;
471 else
472 /* Assume this is a .Properties call. */
473 return find_handler_from_properties_call(call_info);
474
475 } else /* This is not a .Properties call. */
476 return find_handler_by_method(call_info);
477 }
478 }
479
480 static DBusHandlerResult handle_message_cb(DBusConnection *connection, DBusMessage *message, void *user_data) {
481 pa_dbus_protocol *p = user_data;
482 struct call_info call_info;
483
484 pa_assert(connection);
485 pa_assert(message);
486 pa_assert(p);
487 pa_assert(p->objects);
488
489 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
490 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
491
492 pa_log_debug("Received message: destination = %s, interface = %s, member = %s",
493 dbus_message_get_path(message),
494 dbus_message_get_interface(message),
495 dbus_message_get_member(message));
496
497 call_info.message = message;
498 pa_assert_se(call_info.obj_entry = pa_hashmap_get(p->objects, dbus_message_get_path(message)));
499 call_info.interface = dbus_message_get_interface(message);
500 pa_assert_se(call_info.method = dbus_message_get_member(message));
501 pa_assert_se(call_info.method_sig = dbus_message_get_signature(message));
502
503 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") ||
504 (!dbus_message_get_interface(message) && dbus_message_has_member(message, "Introspect"))) {
505 pa_dbus_send_basic_value_reply(connection, message, DBUS_TYPE_STRING, &call_info.obj_entry->introspection);
506 goto finish;
507 }
508
509 switch (find_handler(&call_info)) {
510 case FOUND_GET_PROPERTY:
511 call_info.property_handler->get_cb(connection, message, call_info.iface_entry->userdata);
512 break;
513
514 case FOUND_SET_PROPERTY:
515 call_info.property_handler->set_cb(connection, message, &call_info.variant_iter, call_info.iface_entry->userdata);
516 break;
517
518 case FOUND_METHOD:
519 call_info.method_handler->receive_cb(connection, message, call_info.iface_entry->userdata);
520 break;
521
522 case FOUND_GET_ALL:
523 if (call_info.iface_entry->get_all_properties_cb)
524 call_info.iface_entry->get_all_properties_cb(connection, message, call_info.iface_entry->userdata);
525 else {
526 DBusMessage *dummy_reply = NULL;
527 DBusMessageIter msg_iter;
528 DBusMessageIter dict_iter;
529
530 pa_assert_se(dummy_reply = dbus_message_new_method_return(message));
531 dbus_message_iter_init_append(dummy_reply, &msg_iter);
532 pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
533 pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
534 pa_assert_se(dbus_connection_send(connection, dummy_reply, NULL));
535 dbus_message_unref(dummy_reply);
536 }
537 break;
538
539 case PROPERTY_ACCESS_DENIED:
540 pa_dbus_send_error(connection, message, DBUS_ERROR_ACCESS_DENIED,
541 "%s access denied for property %s", call_info.method, call_info.property);
542 break;
543
544 case NO_SUCH_METHOD:
545 pa_dbus_send_error(connection, message, DBUS_ERROR_UNKNOWN_METHOD, "No such method: %s", call_info.method);
546 break;
547
548 case NO_SUCH_PROPERTY:
549 pa_dbus_send_error(connection, message, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "No such property: %s", call_info.property);
550 break;
551
552 case INVALID_METHOD_SIG:
553 pa_dbus_send_error(connection, message, DBUS_ERROR_INVALID_ARGS,
554 "Invalid signature for method %s: '%s'. Expected '%s'.",
555 call_info.method, call_info.method_sig, call_info.expected_method_sig);
556 break;
557
558 case INVALID_PROPERTY_SIG:
559 pa_dbus_send_error(connection, message, DBUS_ERROR_INVALID_ARGS,
560 "Invalid signature for property %s: '%s'. Expected '%s'.",
561 call_info.property, call_info.property_sig, call_info.expected_property_sig);
562
563 default:
564 pa_assert_not_reached();
565 }
566
567 finish:
568 return DBUS_HANDLER_RESULT_HANDLED;
569 }
570
571 static DBusObjectPathVTable vtable = {
572 .unregister_function = NULL,
573 .message_function = handle_message_cb,
574 .dbus_internal_pad1 = NULL,
575 .dbus_internal_pad2 = NULL,
576 .dbus_internal_pad3 = NULL,
577 .dbus_internal_pad4 = NULL
578 };
579
580 static void register_object(pa_dbus_protocol *p, struct object_entry *obj_entry) {
581 struct connection_entry *conn_entry;
582 void *state = NULL;
583
584 pa_assert(p);
585 pa_assert(obj_entry);
586
587 PA_HASHMAP_FOREACH(conn_entry, p->connections, state)
588 pa_assert_se(dbus_connection_register_object_path(conn_entry->connection, obj_entry->path, &vtable, p));
589 }
590
591 static pa_dbus_arg_info *copy_args(const pa_dbus_arg_info *src, unsigned n) {
592 pa_dbus_arg_info *dst;
593 unsigned i;
594
595 if (n == 0)
596 return NULL;
597
598 pa_assert(src);
599
600 dst = pa_xnew0(pa_dbus_arg_info, n);
601
602 for (i = 0; i < n; ++i) {
603 dst[i].name = pa_xstrdup(src[i].name);
604 dst[i].type = pa_xstrdup(src[i].type);
605 dst[i].direction = pa_xstrdup(src[i].direction);
606 }
607
608 return dst;
609 }
610
611 static pa_hashmap *create_method_handlers(const pa_dbus_interface_info *info) {
612 pa_hashmap *handlers;
613 unsigned i;
614
615 pa_assert(info);
616 pa_assert(info->method_handlers || info->n_method_handlers == 0);
617
618 handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
619
620 for (i = 0; i < info->n_method_handlers; ++i) {
621 pa_dbus_method_handler *h = pa_xnew(pa_dbus_method_handler, 1);
622 h->method_name = pa_xstrdup(info->method_handlers[i].method_name);
623 h->arguments = copy_args(info->method_handlers[i].arguments, info->method_handlers[i].n_arguments);
624 h->n_arguments = info->method_handlers[i].n_arguments;
625 h->receive_cb = info->method_handlers[i].receive_cb;
626
627 pa_hashmap_put(handlers, h->method_name, h);
628 }
629
630 return handlers;
631 }
632
633 static pa_hashmap *create_property_handlers(const pa_dbus_interface_info *info) {
634 pa_hashmap *handlers;
635 unsigned i = 0;
636
637 pa_assert(info);
638 pa_assert(info->property_handlers || info->n_property_handlers == 0);
639
640 handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
641
642 for (i = 0; i < info->n_property_handlers; ++i) {
643 pa_dbus_property_handler *h = pa_xnew(pa_dbus_property_handler, 1);
644 h->property_name = pa_xstrdup(info->property_handlers[i].property_name);
645 h->type = pa_xstrdup(info->property_handlers[i].type);
646 h->get_cb = info->property_handlers[i].get_cb;
647 h->set_cb = info->property_handlers[i].set_cb;
648
649 pa_hashmap_put(handlers, h->property_name, h);
650 }
651
652 return handlers;
653 }
654
655 static pa_dbus_signal_info *copy_signals(const pa_dbus_interface_info *info) {
656 pa_dbus_signal_info *dst;
657 unsigned i;
658
659 pa_assert(info);
660
661 if (info->n_signals == 0)
662 return NULL;
663
664 pa_assert(info->signals);
665
666 dst = pa_xnew(pa_dbus_signal_info, info->n_signals);
667
668 for (i = 0; i < info->n_signals; ++i) {
669 dst[i].name = pa_xstrdup(info->signals[i].name);
670 dst[i].arguments = copy_args(info->signals[i].arguments, info->signals[i].n_arguments);
671 dst[i].n_arguments = info->signals[i].n_arguments;
672 }
673
674 return dst;
675 }
676
677 int pa_dbus_protocol_add_interface(pa_dbus_protocol *p,
678 const char *path,
679 const pa_dbus_interface_info *info,
680 void *userdata) {
681 struct object_entry *obj_entry;
682 struct interface_entry *iface_entry;
683 pa_bool_t obj_entry_created = FALSE;
684
685 pa_assert(p);
686 pa_assert(path);
687 pa_assert(info);
688 pa_assert(info->name);
689 pa_assert(info->method_handlers || info->n_method_handlers == 0);
690 pa_assert(info->property_handlers || info->n_property_handlers == 0);
691 pa_assert(info->get_all_properties_cb || info->n_property_handlers == 0);
692 pa_assert(info->signals || info->n_signals == 0);
693
694 if (!(obj_entry = pa_hashmap_get(p->objects, path))) {
695 obj_entry = pa_xnew(struct object_entry, 1);
696 obj_entry->path = pa_xstrdup(path);
697 obj_entry->interfaces = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
698 obj_entry->introspection = NULL;
699
700 pa_hashmap_put(p->objects, path, obj_entry);
701 obj_entry_created = TRUE;
702 }
703
704 if (pa_hashmap_get(obj_entry->interfaces, info->name) != NULL)
705 goto fail; /* The interface was already registered. */
706
707 iface_entry = pa_xnew(struct interface_entry, 1);
708 iface_entry->name = pa_xstrdup(info->name);
709 iface_entry->method_handlers = create_method_handlers(info);
710 iface_entry->property_handlers = create_property_handlers(info);
711 iface_entry->get_all_properties_cb = info->get_all_properties_cb;
712 iface_entry->signals = copy_signals(info);
713 iface_entry->n_signals = info->n_signals;
714 iface_entry->userdata = userdata;
715 pa_hashmap_put(obj_entry->interfaces, iface_entry->name, iface_entry);
716
717 update_introspection(obj_entry);
718
719 if (obj_entry_created)
720 register_object(p, obj_entry);
721
722 pa_log_debug("Interface %s added for object %s", iface_entry->name, obj_entry->path);
723
724 return 0;
725
726 fail:
727 if (obj_entry_created) {
728 pa_hashmap_remove(p->objects, path);
729 pa_xfree(obj_entry);
730 }
731
732 return -1;
733 }
734
735 static void unregister_object(pa_dbus_protocol *p, struct object_entry *obj_entry) {
736 struct connection_entry *conn_entry;
737 void *state = NULL;
738
739 pa_assert(p);
740 pa_assert(obj_entry);
741
742 PA_HASHMAP_FOREACH(conn_entry, p->connections, state)
743 pa_assert_se(dbus_connection_unregister_object_path(conn_entry->connection, obj_entry->path));
744 }
745
746 static void method_handler_free_cb(void *p, void *userdata) {
747 pa_dbus_method_handler *h = p;
748 unsigned i;
749
750 pa_assert(h);
751
752 pa_xfree((char *) h->method_name);
753
754 for (i = 0; i < h->n_arguments; ++i) {
755 pa_xfree((char *) h->arguments[i].name);
756 pa_xfree((char *) h->arguments[i].type);
757 pa_xfree((char *) h->arguments[i].direction);
758 }
759
760 pa_xfree((pa_dbus_arg_info *) h->arguments);
761 }
762
763 static void property_handler_free_cb(void *p, void *userdata) {
764 pa_dbus_property_handler *h = p;
765
766 pa_assert(h);
767
768 pa_xfree((char *) h->property_name);
769 pa_xfree((char *) h->type);
770
771 pa_xfree(h);
772 }
773
774 int pa_dbus_protocol_remove_interface(pa_dbus_protocol *p, const char* path, const char* interface) {
775 struct object_entry *obj_entry;
776 struct interface_entry *iface_entry;
777 unsigned i;
778
779 pa_assert(p);
780 pa_assert(path);
781 pa_assert(interface);
782
783 if (!(obj_entry = pa_hashmap_get(p->objects, path)))
784 return -1;
785
786 if (!(iface_entry = pa_hashmap_remove(obj_entry->interfaces, interface)))
787 return -1;
788
789 update_introspection(obj_entry);
790
791 pa_log_debug("Interface %s removed from object %s", iface_entry->name, obj_entry->path);
792
793 pa_xfree(iface_entry->name);
794 pa_hashmap_free(iface_entry->method_handlers, method_handler_free_cb, NULL);
795 pa_hashmap_free(iface_entry->property_handlers, property_handler_free_cb, NULL);
796
797 for (i = 0; i < iface_entry->n_signals; ++i) {
798 unsigned j;
799
800 pa_xfree((char *) iface_entry->signals[i].name);
801
802 for (j = 0; j < iface_entry->signals[i].n_arguments; ++j) {
803 pa_xfree((char *) iface_entry->signals[i].arguments[j].name);
804 pa_xfree((char *) iface_entry->signals[i].arguments[j].type);
805 pa_assert(iface_entry->signals[i].arguments[j].direction == NULL);
806 }
807
808 pa_xfree((pa_dbus_arg_info *) iface_entry->signals[i].arguments);
809 }
810
811 pa_xfree(iface_entry->signals);
812 pa_xfree(iface_entry);
813
814 if (pa_hashmap_isempty(obj_entry->interfaces)) {
815 unregister_object(p, obj_entry);
816
817 pa_hashmap_remove(p->objects, path);
818 pa_xfree(obj_entry->path);
819 pa_hashmap_free(obj_entry->interfaces, NULL, NULL);
820 pa_xfree(obj_entry->introspection);
821 pa_xfree(obj_entry);
822 }
823
824 return 0;
825 }
826
827 static void register_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
828 struct object_entry *obj_entry;
829 void *state = NULL;
830
831 pa_assert(p);
832 pa_assert(conn);
833
834 PA_HASHMAP_FOREACH(obj_entry, p->objects, state)
835 pa_assert_se(dbus_connection_register_object_path(conn, obj_entry->path, &vtable, p));
836 }
837
838 int pa_dbus_protocol_register_connection(pa_dbus_protocol *p, DBusConnection *conn, pa_client *client) {
839 struct connection_entry *conn_entry;
840
841 pa_assert(p);
842 pa_assert(conn);
843 pa_assert(client);
844
845 if (pa_hashmap_get(p->connections, conn))
846 return -1; /* The connection was already registered. */
847
848 register_all_objects(p, conn);
849
850 conn_entry = pa_xnew(struct connection_entry, 1);
851 conn_entry->connection = dbus_connection_ref(conn);
852 conn_entry->client = client;
853 conn_entry->listening_for_all_signals = FALSE;
854 conn_entry->all_signals_objects = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
855 conn_entry->listening_signals = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
856
857 pa_hashmap_put(p->connections, conn, conn_entry);
858
859 return 0;
860 }
861
862 static void unregister_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
863 struct object_entry *obj_entry;
864 void *state = NULL;
865
866 pa_assert(p);
867 pa_assert(conn);
868
869 PA_HASHMAP_FOREACH(obj_entry, p->objects, state)
870 pa_assert_se(dbus_connection_unregister_object_path(conn, obj_entry->path));
871 }
872
873 static void free_listened_object_name_cb(void *p, void *userdata) {
874 pa_assert(p);
875
876 pa_xfree(p);
877 }
878
879 static void free_listening_signals_idxset_cb(void *p, void *userdata) {
880 pa_idxset *set = p;
881
882 pa_assert(set);
883
884 pa_idxset_free(set, free_listened_object_name_cb, NULL);
885 }
886
887 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol *p, DBusConnection *conn) {
888 struct connection_entry *conn_entry;
889
890 pa_assert(p);
891 pa_assert(conn);
892
893 if (!(conn_entry = pa_hashmap_remove(p->connections, conn)))
894 return -1;
895
896 unregister_all_objects(p, conn);
897
898 dbus_connection_unref(conn_entry->connection);
899 pa_idxset_free(conn_entry->all_signals_objects, free_listened_object_name_cb, NULL);
900 pa_hashmap_free(conn_entry->listening_signals, free_listening_signals_idxset_cb, NULL);
901 pa_xfree(conn_entry);
902
903 return 0;
904 }
905
906 pa_client *pa_dbus_protocol_get_client(pa_dbus_protocol *p, DBusConnection *conn) {
907 struct connection_entry *conn_entry;
908
909 pa_assert(p);
910 pa_assert(conn);
911
912 if (!(conn_entry = pa_hashmap_get(p->connections, conn)))
913 return NULL;
914
915 return conn_entry->client;
916 }
917
918 void pa_dbus_protocol_add_signal_listener(
919 pa_dbus_protocol *p,
920 DBusConnection *conn,
921 const char *signal,
922 char **objects,
923 unsigned n_objects) {
924 struct connection_entry *conn_entry;
925 pa_idxset *object_set;
926 char *object_path;
927 unsigned i;
928
929 pa_assert(p);
930 pa_assert(conn);
931 pa_assert(objects || n_objects == 0);
932
933 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
934
935 /* all_signals_objects will either be emptied or replaced with new objects,
936 * so we empty it here unconditionally. If listening_for_all_signals is
937 * currently FALSE, the idxset is empty already. */
938 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
939 pa_xfree(object_path);
940
941 if (signal) {
942 conn_entry->listening_for_all_signals = FALSE;
943
944 /* Replace the old object list with a new one. */
945 if ((object_set = pa_hashmap_remove(conn_entry->listening_signals, signal)))
946 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
947 object_set = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
948
949 for (i = 0; i < n_objects; ++i)
950 pa_idxset_put(object_set, pa_xstrdup(objects[i]), NULL);
951
952 pa_hashmap_put(conn_entry->listening_signals, signal, object_set);
953
954 } else {
955 conn_entry->listening_for_all_signals = TRUE;
956
957 /* We're not interested in individual signals anymore, so let's empty
958 * listening_signals. */
959 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
960 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
961
962 for (i = 0; i < n_objects; ++i)
963 pa_idxset_put(conn_entry->all_signals_objects, pa_xstrdup(objects[i]), NULL);
964 }
965 }
966
967 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal) {
968 struct connection_entry *conn_entry;
969 pa_idxset *object_set;
970
971 pa_assert(p);
972 pa_assert(conn);
973
974 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
975
976 if (signal) {
977 if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
978 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
979
980 } else {
981 char *object_path;
982
983 conn_entry->listening_for_all_signals = FALSE;
984
985 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
986 pa_xfree(object_path);
987
988 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
989 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
990 }
991 }
992
993 void pa_dbus_protocol_send_signal(pa_dbus_protocol *p, DBusMessage *signal) {
994 struct connection_entry *conn_entry;
995 void *state = NULL;
996 pa_idxset *object_set;
997 DBusMessage *signal_copy;
998 char *signal_string;
999
1000 pa_assert(p);
1001 pa_assert(signal);
1002 pa_assert(dbus_message_get_type(signal) == DBUS_MESSAGE_TYPE_SIGNAL);
1003 pa_assert_se(dbus_message_get_interface(signal));
1004 pa_assert_se(dbus_message_get_member(signal));
1005
1006 signal_string = pa_sprintf_malloc("%s.%s", dbus_message_get_interface(signal), dbus_message_get_member(signal));
1007
1008 PA_HASHMAP_FOREACH(conn_entry, p->connections, state) {
1009 if ((conn_entry->listening_for_all_signals /* Case 1: listening for all signals */
1010 && (pa_idxset_get_by_data(conn_entry->all_signals_objects, dbus_message_get_path(signal), NULL)
1011 || pa_idxset_isempty(conn_entry->all_signals_objects)))
1012
1013 || (!conn_entry->listening_for_all_signals /* Case 2: not listening for all signals */
1014 && (object_set = pa_hashmap_get(conn_entry->listening_signals, signal_string))
1015 && (pa_idxset_get_by_data(object_set, dbus_message_get_path(signal), NULL)
1016 || pa_idxset_isempty(object_set)))) {
1017
1018 pa_assert_se(signal_copy = dbus_message_copy(signal));
1019 pa_assert_se(dbus_connection_send(conn_entry->connection, signal_copy, NULL));
1020 dbus_message_unref(signal_copy);
1021 }
1022 }
1023
1024 pa_xfree(signal_string);
1025 }
1026
1027 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol *p, unsigned *n) {
1028 const char **extensions;
1029 const char *ext_name;
1030 void *state = NULL;
1031 unsigned i = 0;
1032
1033 pa_assert(p);
1034 pa_assert(n);
1035
1036 *n = pa_idxset_size(p->extensions);
1037
1038 if (*n <= 0)
1039 return NULL;
1040
1041 extensions = pa_xnew(const char *, *n);
1042
1043 while ((ext_name = pa_idxset_iterate(p->extensions, &state, NULL)))
1044 extensions[i++] = ext_name;
1045
1046 return extensions;
1047 }
1048
1049 int pa_dbus_protocol_register_extension(pa_dbus_protocol *p, const char *name) {
1050 char *internal_name;
1051
1052 pa_assert(p);
1053 pa_assert(name);
1054
1055 internal_name = pa_xstrdup(name);
1056
1057 if (pa_idxset_put(p->extensions, internal_name, NULL) < 0) {
1058 pa_xfree(internal_name);
1059 return -1;
1060 }
1061
1062 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED], internal_name);
1063
1064 return 0;
1065 }
1066
1067 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol *p, const char *name) {
1068 char *internal_name;
1069
1070 pa_assert(p);
1071 pa_assert(name);
1072
1073 if (!(internal_name = pa_idxset_remove_by_data(p->extensions, name, NULL)))
1074 return -1;
1075
1076 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED], internal_name);
1077
1078 pa_xfree(internal_name);
1079
1080 return 0;
1081 }
1082
1083 pa_hook_slot *pa_dbus_protocol_hook_connect(
1084 pa_dbus_protocol *p,
1085 pa_dbus_protocol_hook_t hook,
1086 pa_hook_priority_t prio,
1087 pa_hook_cb_t cb,
1088 void *data) {
1089 pa_assert(p);
1090 pa_assert(hook < PA_DBUS_PROTOCOL_HOOK_MAX);
1091 pa_assert(cb);
1092
1093 return pa_hook_connect(&p->hooks[hook], prio, cb, data);
1094 }