| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | #include "../common.h" |
|---|
| 31 | #include "helpers.h" |
|---|
| 32 | #include "error.h" |
|---|
| 33 | |
|---|
| 34 | struct dbus_object_path_interface |
|---|
| 35 | { |
|---|
| 36 | const struct dbus_interface_descriptor * iface; |
|---|
| 37 | void * iface_context; |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | struct dbus_object_path |
|---|
| 41 | { |
|---|
| 42 | char * name; |
|---|
| 43 | DBusMessage * introspection; |
|---|
| 44 | struct dbus_object_path_interface * ifaces; |
|---|
| 45 | bool registered; |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | #define write_buf(args...) buf_ptr += sprintf(buf_ptr, ## args) |
|---|
| 49 | |
|---|
| 50 | DBusMessage * |
|---|
| 51 | introspection_new(struct dbus_object_path * opath_ptr) |
|---|
| 52 | { |
|---|
| 53 | char *xml_data, *buf_ptr; |
|---|
| 54 | const struct dbus_object_path_interface * iface_ptr; |
|---|
| 55 | const struct dbus_method_descriptor * method_ptr; |
|---|
| 56 | const struct dbus_method_arg_descriptor * method_arg_ptr; |
|---|
| 57 | const struct dbus_signal_descriptor * signal_ptr; |
|---|
| 58 | const struct dbus_signal_arg_descriptor * signal_arg_ptr; |
|---|
| 59 | DBusMessage * msg; |
|---|
| 60 | DBusMessageIter iter; |
|---|
| 61 | |
|---|
| 62 | log_debug("Creating introspection message"); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | xml_data = malloc(16384); |
|---|
| 72 | if (xml_data == NULL) |
|---|
| 73 | { |
|---|
| 74 | return NULL; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | buf_ptr = xml_data; |
|---|
| 78 | |
|---|
| 79 | write_buf("<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n" |
|---|
| 80 | " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n" |
|---|
| 81 | "<node name=\"%s\">\n", opath_ptr->name); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | for (iface_ptr = opath_ptr->ifaces; iface_ptr->iface != NULL; iface_ptr++) |
|---|
| 85 | { |
|---|
| 86 | write_buf(" <interface name=\"%s\">\n", iface_ptr->iface->name); |
|---|
| 87 | if (iface_ptr->iface->methods != NULL) |
|---|
| 88 | { |
|---|
| 89 | |
|---|
| 90 | for (method_ptr = iface_ptr->iface->methods; method_ptr->name != NULL; method_ptr++) |
|---|
| 91 | { |
|---|
| 92 | write_buf(" <method name=\"%s\">\n", method_ptr->name); |
|---|
| 93 | |
|---|
| 94 | for (method_arg_ptr = method_ptr->args; method_arg_ptr->name != NULL; method_arg_ptr++) |
|---|
| 95 | { |
|---|
| 96 | write_buf( |
|---|
| 97 | " <arg name=\"%s\" type=\"%s\" direction=\"%s\" />\n", |
|---|
| 98 | method_arg_ptr->name, |
|---|
| 99 | method_arg_ptr->type, |
|---|
| 100 | method_arg_ptr->direction_in ? "in" : "out"); |
|---|
| 101 | } |
|---|
| 102 | write_buf(" </method>\n"); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | if (iface_ptr->iface->signals != NULL) |
|---|
| 106 | { |
|---|
| 107 | |
|---|
| 108 | for (signal_ptr = iface_ptr->iface->signals; signal_ptr->name != NULL; signal_ptr++) |
|---|
| 109 | { |
|---|
| 110 | write_buf(" <signal name=\"%s\">\n", signal_ptr->name); |
|---|
| 111 | |
|---|
| 112 | for (signal_arg_ptr = signal_ptr->args; signal_arg_ptr->name != NULL; signal_arg_ptr++) |
|---|
| 113 | { |
|---|
| 114 | write_buf(" <arg name=\"%s\" type=\"%s\" />\n", signal_arg_ptr->name, signal_arg_ptr->type); |
|---|
| 115 | } |
|---|
| 116 | write_buf(" </signal>\n"); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | write_buf(" </interface>\n"); |
|---|
| 120 | } |
|---|
| 121 | write_buf("</node>\n"); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | if ((msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN))) |
|---|
| 128 | { |
|---|
| 129 | dbus_message_iter_init_append(msg, &iter); |
|---|
| 130 | if (dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, (const void *) &xml_data)) |
|---|
| 131 | { |
|---|
| 132 | dbus_message_set_no_reply(msg, TRUE); |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | dbus_message_unref(msg); |
|---|
| 137 | msg = NULL; |
|---|
| 138 | log_error("Failed to append data to introspection message"); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | else |
|---|
| 142 | { |
|---|
| 143 | log_error("Failed to create introspection message"); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | free(xml_data); |
|---|
| 147 | return msg; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | #undef write_buf |
|---|
| 151 | |
|---|
| 152 | void |
|---|
| 153 | introspection_destroy(struct dbus_object_path *path) |
|---|
| 154 | { |
|---|
| 155 | log_debug("Destroying introspection message"); |
|---|
| 156 | |
|---|
| 157 | if (path && path->introspection) { |
|---|
| 158 | dbus_message_unref(path->introspection); |
|---|
| 159 | path->introspection = NULL; |
|---|
| 160 | } |
|---|
| 161 | #ifdef LADISH_DEBUG |
|---|
| 162 | else |
|---|
| 163 | log_debug("Nothing to destroy"); |
|---|
| 164 | #endif |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | static bool introspection_handler(const struct dbus_interface_descriptor * interface, struct dbus_method_call * call_ptr) |
|---|
| 168 | { |
|---|
| 169 | if (strcmp(call_ptr->method_name, "Introspect") != 0) |
|---|
| 170 | { |
|---|
| 171 | |
|---|
| 172 | return false; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | call_ptr->reply = dbus_message_copy(call_ptr->iface_context); |
|---|
| 177 | if (call_ptr->reply == NULL) |
|---|
| 178 | { |
|---|
| 179 | log_error("Ran out of memory trying to copy introspection message"); |
|---|
| 180 | goto fail; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | if (!dbus_message_set_destination(call_ptr->reply, dbus_message_get_sender(call_ptr->message))) |
|---|
| 184 | { |
|---|
| 185 | log_error("dbus_message_set_destination() failed."); |
|---|
| 186 | goto unref_reply; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | if (!dbus_message_set_reply_serial(call_ptr->reply, dbus_message_get_serial(call_ptr->message))) |
|---|
| 190 | { |
|---|
| 191 | log_error("dbus_message_set_reply_serial() failed."); |
|---|
| 192 | goto unref_reply; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | return true; |
|---|
| 196 | |
|---|
| 197 | unref_reply: |
|---|
| 198 | dbus_message_unref(call_ptr->reply); |
|---|
| 199 | call_ptr->reply = NULL; |
|---|
| 200 | |
|---|
| 201 | fail: |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | return true; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | METHOD_ARGS_BEGIN(Introspect, "Get introspection XML") |
|---|
| 209 | METHOD_ARG_DESCRIBE_OUT("xml_data", "s", "XML description of the object") |
|---|
| 210 | METHOD_ARGS_END |
|---|
| 211 | |
|---|
| 212 | METHODS_BEGIN |
|---|
| 213 | METHOD_DESCRIBE(Introspect, NULL) |
|---|
| 214 | METHODS_END |
|---|
| 215 | |
|---|
| 216 | INTERFACE_BEGIN(g_dbus_interface_dtor_introspectable, "org.freedesktop.DBus.Introspectable") |
|---|
| 217 | INTERFACE_HANDLER(introspection_handler) |
|---|
| 218 | INTERFACE_EXPOSE_METHODS |
|---|
| 219 | INTERFACE_END |
|---|
| 220 | |
|---|
| 221 | dbus_object_path dbus_object_path_new(const char *name, const struct dbus_interface_descriptor * iface1_ptr, ...) |
|---|
| 222 | { |
|---|
| 223 | struct dbus_object_path * opath_ptr; |
|---|
| 224 | va_list ap; |
|---|
| 225 | const struct dbus_interface_descriptor * iface_src_ptr; |
|---|
| 226 | struct dbus_object_path_interface * iface_dst_ptr; |
|---|
| 227 | void * iface_context; |
|---|
| 228 | size_t len; |
|---|
| 229 | |
|---|
| 230 | log_debug("Creating object path"); |
|---|
| 231 | |
|---|
| 232 | opath_ptr = malloc(sizeof(struct dbus_object_path)); |
|---|
| 233 | if (opath_ptr == NULL) |
|---|
| 234 | { |
|---|
| 235 | log_error("malloc() failed to allocate struct dbus_object_path."); |
|---|
| 236 | goto fail; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | opath_ptr->name = strdup(name); |
|---|
| 240 | if (opath_ptr->name == NULL) |
|---|
| 241 | { |
|---|
| 242 | log_error("malloc() failed to allocate struct dbus_object_path."); |
|---|
| 243 | goto free; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | va_start(ap, iface1_ptr); |
|---|
| 247 | iface_src_ptr = iface1_ptr; |
|---|
| 248 | len = 0; |
|---|
| 249 | while (iface_src_ptr != NULL) |
|---|
| 250 | { |
|---|
| 251 | iface_context = va_arg(ap, void *); |
|---|
| 252 | iface_src_ptr = va_arg(ap, const struct dbus_interface_descriptor *); |
|---|
| 253 | len++; |
|---|
| 254 | } |
|---|
| 255 | va_end(ap); |
|---|
| 256 | |
|---|
| 257 | opath_ptr->ifaces = malloc((len + 2) * sizeof(struct dbus_object_path_interface)); |
|---|
| 258 | if (opath_ptr->ifaces == NULL) |
|---|
| 259 | { |
|---|
| 260 | log_error("malloc failed to allocate interfaces array"); |
|---|
| 261 | goto free_name; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | va_start(ap, iface1_ptr); |
|---|
| 265 | iface_src_ptr = iface1_ptr; |
|---|
| 266 | iface_dst_ptr = opath_ptr->ifaces; |
|---|
| 267 | while (iface_src_ptr != NULL) |
|---|
| 268 | { |
|---|
| 269 | iface_dst_ptr->iface = iface_src_ptr; |
|---|
| 270 | iface_dst_ptr->iface_context = va_arg(ap, void *); |
|---|
| 271 | iface_src_ptr = va_arg(ap, const struct dbus_interface_descriptor *); |
|---|
| 272 | iface_dst_ptr++; |
|---|
| 273 | len--; |
|---|
| 274 | } |
|---|
| 275 | va_end(ap); |
|---|
| 276 | |
|---|
| 277 | ASSERT(len == 0); |
|---|
| 278 | |
|---|
| 279 | iface_dst_ptr->iface = NULL; |
|---|
| 280 | opath_ptr->introspection = introspection_new(opath_ptr); |
|---|
| 281 | if (opath_ptr->introspection == NULL) |
|---|
| 282 | { |
|---|
| 283 | log_error("introspection_new() failed."); |
|---|
| 284 | goto free_ifaces; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | iface_dst_ptr->iface = &g_dbus_interface_dtor_introspectable; |
|---|
| 288 | iface_dst_ptr->iface_context = opath_ptr->introspection; |
|---|
| 289 | iface_dst_ptr++; |
|---|
| 290 | iface_dst_ptr->iface = NULL; |
|---|
| 291 | |
|---|
| 292 | opath_ptr->registered = false; |
|---|
| 293 | |
|---|
| 294 | return (dbus_object_path)opath_ptr; |
|---|
| 295 | |
|---|
| 296 | free_ifaces: |
|---|
| 297 | free(opath_ptr->ifaces); |
|---|
| 298 | free_name: |
|---|
| 299 | free(opath_ptr->name); |
|---|
| 300 | free: |
|---|
| 301 | free(opath_ptr); |
|---|
| 302 | fail: |
|---|
| 303 | return NULL; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | #define opath_ptr ((struct dbus_object_path *)data) |
|---|
| 307 | |
|---|
| 308 | void dbus_object_path_unregister(DBusConnection * connection_ptr, dbus_object_path data) |
|---|
| 309 | { |
|---|
| 310 | ASSERT(opath_ptr->registered); |
|---|
| 311 | |
|---|
| 312 | if (!dbus_connection_unregister_object_path(connection_ptr, opath_ptr->name)) |
|---|
| 313 | { |
|---|
| 314 | log_error("dbus_connection_unregister_object_path() failed."); |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | void dbus_object_path_destroy(DBusConnection * connection_ptr, dbus_object_path data) |
|---|
| 319 | { |
|---|
| 320 | log_debug("Destroying object path"); |
|---|
| 321 | |
|---|
| 322 | if (opath_ptr->registered && connection_ptr != NULL && !dbus_connection_unregister_object_path(connection_ptr, opath_ptr->name)) |
|---|
| 323 | { |
|---|
| 324 | log_error("dbus_connection_unregister_object_path() failed."); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | introspection_destroy(opath_ptr); |
|---|
| 328 | free(opath_ptr->ifaces); |
|---|
| 329 | free(opath_ptr->name); |
|---|
| 330 | free(opath_ptr); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | static DBusHandlerResult dbus_object_path_handler(DBusConnection * connection, DBusMessage * message, void * data) |
|---|
| 334 | { |
|---|
| 335 | const char * iface_name; |
|---|
| 336 | const struct dbus_object_path_interface * iface_ptr; |
|---|
| 337 | struct dbus_method_call call; |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL) |
|---|
| 341 | { |
|---|
| 342 | goto handled; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | call.method_name = dbus_message_get_member(message); |
|---|
| 347 | if (call.method_name == NULL) |
|---|
| 348 | { |
|---|
| 349 | lash_dbus_error(&call, LASH_DBUS_ERROR_UNKNOWN_METHOD, "Received method call with empty method name"); |
|---|
| 350 | goto send_return; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | call.connection = connection; |
|---|
| 355 | call.message = message; |
|---|
| 356 | call.iface = NULL; |
|---|
| 357 | call.reply = NULL; |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | iface_name = dbus_message_get_interface(message); |
|---|
| 361 | if (iface_name != NULL) |
|---|
| 362 | { |
|---|
| 363 | for (iface_ptr = opath_ptr->ifaces; iface_ptr->iface != NULL; iface_ptr++) |
|---|
| 364 | { |
|---|
| 365 | if (strcmp(iface_name, iface_ptr->iface->name) == 0) |
|---|
| 366 | { |
|---|
| 367 | call.iface_context = iface_ptr->iface_context; |
|---|
| 368 | if (!iface_ptr->iface->handler(iface_ptr->iface, &call)) |
|---|
| 369 | { |
|---|
| 370 | |
|---|
| 371 | break; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | goto send_return; |
|---|
| 375 | } |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | else |
|---|
| 379 | { |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | for (iface_ptr = opath_ptr->ifaces; iface_ptr->iface != NULL; iface_ptr++) |
|---|
| 389 | { |
|---|
| 390 | call.iface_context = iface_ptr->iface_context; |
|---|
| 391 | if (!iface_ptr->iface->handler(iface_ptr->iface, &call)) |
|---|
| 392 | { |
|---|
| 393 | |
|---|
| 394 | goto send_return; |
|---|
| 395 | } |
|---|
| 396 | } |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | lash_dbus_error(&call, LASH_DBUS_ERROR_UNKNOWN_METHOD, "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist", call.method_name, dbus_message_get_signature(message), iface_name); |
|---|
| 400 | |
|---|
| 401 | send_return: |
|---|
| 402 | method_return_send(&call); |
|---|
| 403 | |
|---|
| 404 | handled: |
|---|
| 405 | return DBUS_HANDLER_RESULT_HANDLED; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | static void dbus_object_path_handler_unregister(DBusConnection * connection_ptr, void * data) |
|---|
| 409 | { |
|---|
| 410 | #ifdef LADISH_DEBUG |
|---|
| 411 | log_debug("Message handler of object path %s was unregistered", (opath_ptr && path->name) ? opath_ptr->name : "<unknown>"); |
|---|
| 412 | #endif |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | bool dbus_object_path_register(DBusConnection * connection_ptr, dbus_object_path data) |
|---|
| 416 | { |
|---|
| 417 | log_debug("Registering object path \"%s\"", opath_ptr->name); |
|---|
| 418 | |
|---|
| 419 | ASSERT(!opath_ptr->registered); |
|---|
| 420 | |
|---|
| 421 | DBusObjectPathVTable vtable = |
|---|
| 422 | { |
|---|
| 423 | dbus_object_path_handler_unregister, |
|---|
| 424 | dbus_object_path_handler, |
|---|
| 425 | NULL, NULL, NULL, NULL |
|---|
| 426 | }; |
|---|
| 427 | |
|---|
| 428 | if (!dbus_connection_register_object_path(connection_ptr, opath_ptr->name, &vtable, opath_ptr)) |
|---|
| 429 | { |
|---|
| 430 | return false; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | opath_ptr->registered = true; |
|---|
| 434 | return true; |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | #undef opath_ptr |
|---|