1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/
4 Copyright 2009 Lennart Poettering
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include "ardouralsautil/reserve.h"
36 #ifndef DBUS_TIMEOUT_USE_DEFAULT
37 #define DBUS_TIMEOUT_USE_DEFAULT (-1)
44 char *application_name;
45 char *application_device_name;
50 DBusConnection *connection;
53 unsigned registered:1;
57 rd_request_cb_t request_cb;
61 #define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
62 #define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/"
64 static const char introspection[] =
65 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
67 " <!-- If you are looking for documentation make sure to check out\n"
68 " http://git.0pointer.de/?p=reserve.git;a=blob;f=reserve.txt -->\n"
69 " <interface name=\"org.freedesktop.ReserveDevice1\">"
70 " <method name=\"RequestRelease\">"
71 " <arg name=\"priority\" type=\"i\" direction=\"in\"/>"
72 " <arg name=\"result\" type=\"b\" direction=\"out\"/>"
74 " <property name=\"Priority\" type=\"i\" access=\"read\"/>"
75 " <property name=\"ApplicationName\" type=\"s\" access=\"read\"/>"
76 " <property name=\"ApplicationDeviceName\" type=\"s\" access=\"read\"/>"
78 " <interface name=\"org.freedesktop.DBus.Properties\">"
79 " <method name=\"Get\">"
80 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>"
81 " <arg name=\"property\" direction=\"in\" type=\"s\"/>"
82 " <arg name=\"value\" direction=\"out\" type=\"v\"/>"
85 " <interface name=\"org.freedesktop.DBus.Introspectable\">"
86 " <method name=\"Introspect\">"
87 " <arg name=\"data\" type=\"s\" direction=\"out\"/>"
92 static dbus_bool_t add_variant(
97 DBusMessageIter iter, sub;
103 dbus_message_iter_init_append(m, &iter);
105 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, t, &sub))
108 if (!dbus_message_iter_append_basic(&sub, type, data))
111 if (!dbus_message_iter_close_container(&iter, &sub))
117 static DBusHandlerResult object_handler(
124 DBusMessage *reply = NULL;
126 dbus_error_init(&error);
131 if (dbus_message_is_method_call(
133 "org.freedesktop.ReserveDevice1",
139 if (!dbus_message_get_args(
142 DBUS_TYPE_INT32, &priority,
148 if (priority > d->priority && d->request_cb) {
151 if (d->request_cb(d, 0) > 0) {
159 if (!(reply = dbus_message_new_method_return(m)))
162 if (!dbus_message_append_args(
164 DBUS_TYPE_BOOLEAN, &ret,
168 if (!dbus_connection_send(c, reply, NULL))
171 dbus_message_unref(reply);
173 return DBUS_HANDLER_RESULT_HANDLED;
175 } else if (dbus_message_is_method_call(
177 "org.freedesktop.DBus.Properties",
180 const char *interface, *property;
182 if (!dbus_message_get_args(
185 DBUS_TYPE_STRING, &interface,
186 DBUS_TYPE_STRING, &property,
190 if (strcmp(interface, "org.freedesktop.ReserveDevice1") == 0) {
191 const char *empty = "";
193 if (strcmp(property, "ApplicationName") == 0 && d->application_name) {
194 if (!(reply = dbus_message_new_method_return(m)))
200 d->application_name ? (const char * const *) &d->application_name : &empty))
203 } else if (strcmp(property, "ApplicationDeviceName") == 0) {
204 if (!(reply = dbus_message_new_method_return(m)))
210 d->application_device_name ? (const char * const *) &d->application_device_name : &empty))
213 } else if (strcmp(property, "Priority") == 0) {
214 if (!(reply = dbus_message_new_method_return(m)))
223 if (!(reply = dbus_message_new_error_printf(
225 DBUS_ERROR_UNKNOWN_METHOD,
226 "Unknown property %s",
231 if (!dbus_connection_send(c, reply, NULL))
234 dbus_message_unref(reply);
236 return DBUS_HANDLER_RESULT_HANDLED;
239 } else if (dbus_message_is_method_call(
241 "org.freedesktop.DBus.Introspectable",
243 const char *i = introspection;
245 if (!(reply = dbus_message_new_method_return(m)))
248 if (!dbus_message_append_args(
255 if (!dbus_connection_send(c, reply, NULL))
258 dbus_message_unref(reply);
260 return DBUS_HANDLER_RESULT_HANDLED;
263 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
267 dbus_message_unref(reply);
269 if (!(reply = dbus_message_new_error(
271 DBUS_ERROR_INVALID_ARGS,
272 "Invalid arguments")))
275 if (!dbus_connection_send(c, reply, NULL))
278 dbus_message_unref(reply);
280 dbus_error_free(&error);
282 return DBUS_HANDLER_RESULT_HANDLED;
286 dbus_message_unref(reply);
288 dbus_error_free(&error);
290 return DBUS_HANDLER_RESULT_NEED_MEMORY;
293 static DBusHandlerResult filter_handler(
300 char *name_owner = NULL;
302 dbus_error_init(&error);
307 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
310 if (!dbus_message_get_args(
313 DBUS_TYPE_STRING, &name,
317 if (strcmp(name, d->service_name) == 0 && d->owning) {
318 /* Verify the actual owner of the name to avoid leaked NameLost
319 * signals from previous reservations. The D-Bus daemon will send
320 * all messages asynchronously in the correct order, but we could
321 * potentially process them too late due to the pseudo-blocking
322 * call mechanism used during both acquisition and release. This
323 * can happen if we release the device and immediately after
324 * reacquire it before NameLost is processed. */
328 if ((un = dbus_bus_get_unique_name(c)) && rd_dbus_get_name_owner(c, d->service_name, &name_owner, &error) == 0)
329 if (name_owner && strcmp(name_owner, un) == 0)
330 goto invalid; /* Name still owned by us */
350 dbus_error_free(&error);
352 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
356 static const struct DBusObjectPathVTable vtable ={
357 .message_function = object_handler
362 DBusConnection *connection,
363 const char *device_name,
364 const char *application_name,
366 rd_request_cb_t request_cb,
372 DBusMessage *m = NULL, *reply = NULL;
378 dbus_error_init(error);
389 if (!request_cb && priority != INT32_MAX)
392 if (!(d = calloc(sizeof(rd_device), 1)))
397 if (!(d->device_name = strdup(device_name))) {
402 if (!(d->application_name = strdup(application_name))) {
407 d->priority = priority;
408 d->connection = dbus_connection_ref(connection);
409 d->request_cb = request_cb;
411 if (!(d->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
415 sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name);
417 if (!(d->object_path = malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) {
421 sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name);
423 if ((k = dbus_bus_request_name(
426 DBUS_NAME_FLAG_DO_NOT_QUEUE|
427 (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0),
433 if (k == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
436 if (k != DBUS_REQUEST_NAME_REPLY_EXISTS) {
441 if (priority <= INT32_MIN) {
446 if (!(m = dbus_message_new_method_call(
449 "org.freedesktop.ReserveDevice1",
450 "RequestRelease"))) {
455 if (!dbus_message_append_args(
457 DBUS_TYPE_INT32, &d->priority,
458 DBUS_TYPE_INVALID)) {
463 if (!(reply = dbus_connection_send_with_reply_and_block(
469 if (dbus_error_has_name(error, DBUS_ERROR_TIMED_OUT) ||
470 dbus_error_has_name(error, DBUS_ERROR_UNKNOWN_METHOD) ||
471 dbus_error_has_name(error, DBUS_ERROR_NO_REPLY)) {
472 /* This must be treated as denied. */
481 if (!dbus_message_get_args(
484 DBUS_TYPE_BOOLEAN, &good,
485 DBUS_TYPE_INVALID)) {
495 if ((k = dbus_bus_request_name(
498 DBUS_NAME_FLAG_DO_NOT_QUEUE|
499 (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0)|
500 DBUS_NAME_FLAG_REPLACE_EXISTING,
506 if (k != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
514 if (!(dbus_connection_register_object_path(
525 if (!dbus_connection_add_filter(
541 dbus_message_unref(m);
544 dbus_message_unref(reply);
546 if (&_error == error)
547 dbus_error_free(&_error);
568 dbus_connection_remove_filter(
574 dbus_connection_unregister_object_path(
579 dbus_bus_release_name(
584 free(d->device_name);
585 free(d->application_name);
586 free(d->application_device_name);
587 free(d->service_name);
588 free(d->object_path);
591 dbus_connection_unref(d->connection);
596 int rd_set_application_device_name(rd_device *d, const char *n) {
604 if (!(t = strdup(n)))
607 free(d->application_device_name);
608 d->application_device_name = t;
612 void rd_set_userdata(rd_device *d, void *userdata) {
618 d->userdata = userdata;
621 void* rd_get_userdata(rd_device *d) {
631 int rd_dbus_get_name_owner(
632 DBusConnection *connection,
637 DBusMessage *msg, *reply;
642 if (!(msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetNameOwner"))) {
647 if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
652 reply = dbus_connection_send_with_reply_and_block(connection, msg, DBUS_TIMEOUT_USE_DEFAULT, error);
653 dbus_message_unref(msg);
657 if (!dbus_message_get_args(reply, error, DBUS_TYPE_STRING, name_owner, DBUS_TYPE_INVALID)) {
658 dbus_message_unref(reply);
663 *name_owner = strdup(*name_owner);
664 dbus_message_unref(reply);
671 } else if (dbus_error_has_name(error, "org.freedesktop.DBus.Error.NameHasNoOwner"))
672 dbus_error_free(error);
682 dbus_message_unref(msg);