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
40 char *application_name
;
41 char *application_device_name
;
46 DBusConnection
*connection
;
49 unsigned registered
:1;
53 rd_request_cb_t request_cb
;
57 #define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
58 #define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/"
60 static const char introspection
[] =
61 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
63 " <!-- If you are looking for documentation make sure to check out\n"
64 " http://git.0pointer.de/?p=reserve.git;a=blob;f=reserve.txt -->\n"
65 " <interface name=\"org.freedesktop.ReserveDevice1\">"
66 " <method name=\"RequestRelease\">"
67 " <arg name=\"priority\" type=\"i\" direction=\"in\"/>"
68 " <arg name=\"result\" type=\"b\" direction=\"out\"/>"
70 " <property name=\"Priority\" type=\"i\" access=\"read\"/>"
71 " <property name=\"ApplicationName\" type=\"s\" access=\"read\"/>"
72 " <property name=\"ApplicationDeviceName\" type=\"s\" access=\"read\"/>"
74 " <interface name=\"org.freedesktop.DBus.Properties\">"
75 " <method name=\"Get\">"
76 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>"
77 " <arg name=\"property\" direction=\"in\" type=\"s\"/>"
78 " <arg name=\"value\" direction=\"out\" type=\"v\"/>"
81 " <interface name=\"org.freedesktop.DBus.Introspectable\">"
82 " <method name=\"Introspect\">"
83 " <arg name=\"data\" type=\"s\" direction=\"out\"/>"
88 static dbus_bool_t
add_variant(
93 DBusMessageIter iter
, sub
;
99 dbus_message_iter_init_append(m
, &iter
);
101 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_VARIANT
, t
, &sub
))
104 if (!dbus_message_iter_append_basic(&sub
, type
, data
))
107 if (!dbus_message_iter_close_container(&iter
, &sub
))
113 static DBusHandlerResult
object_handler(
120 DBusMessage
*reply
= NULL
;
122 dbus_error_init(&error
);
127 if (dbus_message_is_method_call(
129 "org.freedesktop.ReserveDevice1",
135 if (!dbus_message_get_args(
138 DBUS_TYPE_INT32
, &priority
,
144 if (priority
> d
->priority
&& d
->request_cb
) {
147 if (d
->request_cb(d
, 0) > 0) {
155 if (!(reply
= dbus_message_new_method_return(m
)))
158 if (!dbus_message_append_args(
160 DBUS_TYPE_BOOLEAN
, &ret
,
164 if (!dbus_connection_send(c
, reply
, NULL
))
167 dbus_message_unref(reply
);
169 return DBUS_HANDLER_RESULT_HANDLED
;
171 } else if (dbus_message_is_method_call(
173 "org.freedesktop.DBus.Properties",
176 const char *interface
, *property
;
178 if (!dbus_message_get_args(
181 DBUS_TYPE_STRING
, &interface
,
182 DBUS_TYPE_STRING
, &property
,
186 if (strcmp(interface
, "org.freedesktop.ReserveDevice1") == 0) {
187 const char *empty
= "";
189 if (strcmp(property
, "ApplicationName") == 0 && d
->application_name
) {
190 if (!(reply
= dbus_message_new_method_return(m
)))
196 d
->application_name
? (const char**) &d
->application_name
: &empty
))
199 } else if (strcmp(property
, "ApplicationDeviceName") == 0) {
200 if (!(reply
= dbus_message_new_method_return(m
)))
206 d
->application_device_name
? (const char**) &d
->application_device_name
: &empty
))
209 } else if (strcmp(property
, "Priority") == 0) {
210 if (!(reply
= dbus_message_new_method_return(m
)))
219 if (!(reply
= dbus_message_new_error_printf(
221 DBUS_ERROR_UNKNOWN_METHOD
,
222 "Unknown property %s",
227 if (!dbus_connection_send(c
, reply
, NULL
))
230 dbus_message_unref(reply
);
232 return DBUS_HANDLER_RESULT_HANDLED
;
235 } else if (dbus_message_is_method_call(
237 "org.freedesktop.DBus.Introspectable",
239 const char *i
= introspection
;
241 if (!(reply
= dbus_message_new_method_return(m
)))
244 if (!dbus_message_append_args(
251 if (!dbus_connection_send(c
, reply
, NULL
))
254 dbus_message_unref(reply
);
256 return DBUS_HANDLER_RESULT_HANDLED
;
259 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
263 dbus_message_unref(reply
);
265 if (!(reply
= dbus_message_new_error(
267 DBUS_ERROR_INVALID_ARGS
,
268 "Invalid arguments")))
271 if (!dbus_connection_send(c
, reply
, NULL
))
274 dbus_message_unref(reply
);
276 dbus_error_free(&error
);
278 return DBUS_HANDLER_RESULT_HANDLED
;
282 dbus_message_unref(reply
);
284 dbus_error_free(&error
);
286 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
289 static DBusHandlerResult
filter_handler(
296 char *name_owner
= NULL
;
298 dbus_error_init(&error
);
303 if (dbus_message_is_signal(m
, "org.freedesktop.DBus", "NameLost")) {
306 if (!dbus_message_get_args(
309 DBUS_TYPE_STRING
, &name
,
313 if (strcmp(name
, d
->service_name
) == 0 && d
->owning
) {
314 /* Verify the actual owner of the name to avoid leaked NameLost
315 * signals from previous reservations. The D-Bus daemon will send
316 * all messages asynchronously in the correct order, but we could
317 * potentially process them too late due to the pseudo-blocking
318 * call mechanism used during both acquisition and release. This
319 * can happen if we release the device and immediately after
320 * reacquire it before NameLost is processed. */
324 if ((un
= dbus_bus_get_unique_name(c
)) && rd_dbus_get_name_owner(c
, d
->service_name
, &name_owner
, &error
) == 0)
325 if (strcmp(name_owner
, un
) == 0)
326 goto invalid
; /* Name still owned by us */
346 dbus_error_free(&error
);
348 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
352 static const struct DBusObjectPathVTable vtable
={
353 .message_function
= object_handler
358 DBusConnection
*connection
,
359 const char *device_name
,
360 const char *application_name
,
362 rd_request_cb_t request_cb
,
368 DBusMessage
*m
= NULL
, *reply
= NULL
;
374 dbus_error_init(error
);
385 if (!request_cb
&& priority
!= INT32_MAX
)
388 if (!(d
= calloc(sizeof(rd_device
), 1)))
393 if (!(d
->device_name
= strdup(device_name
))) {
398 if (!(d
->application_name
= strdup(application_name
))) {
403 d
->priority
= priority
;
404 d
->connection
= dbus_connection_ref(connection
);
405 d
->request_cb
= request_cb
;
407 if (!(d
->service_name
= malloc(sizeof(SERVICE_PREFIX
) + strlen(device_name
)))) {
411 sprintf(d
->service_name
, SERVICE_PREFIX
"%s", d
->device_name
);
413 if (!(d
->object_path
= malloc(sizeof(OBJECT_PREFIX
) + strlen(device_name
)))) {
417 sprintf(d
->object_path
, OBJECT_PREFIX
"%s", d
->device_name
);
419 if ((k
= dbus_bus_request_name(
422 DBUS_NAME_FLAG_DO_NOT_QUEUE
|
423 (priority
< INT32_MAX
? DBUS_NAME_FLAG_ALLOW_REPLACEMENT
: 0),
429 if (k
== DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
)
432 if (k
!= DBUS_REQUEST_NAME_REPLY_EXISTS
) {
437 if (priority
<= INT32_MIN
) {
442 if (!(m
= dbus_message_new_method_call(
445 "org.freedesktop.ReserveDevice1",
446 "RequestRelease"))) {
451 if (!dbus_message_append_args(
453 DBUS_TYPE_INT32
, &d
->priority
,
454 DBUS_TYPE_INVALID
)) {
459 if (!(reply
= dbus_connection_send_with_reply_and_block(
465 if (dbus_error_has_name(error
, DBUS_ERROR_TIMED_OUT
) ||
466 dbus_error_has_name(error
, DBUS_ERROR_UNKNOWN_METHOD
) ||
467 dbus_error_has_name(error
, DBUS_ERROR_NO_REPLY
)) {
468 /* This must be treated as denied. */
477 if (!dbus_message_get_args(
480 DBUS_TYPE_BOOLEAN
, &good
,
481 DBUS_TYPE_INVALID
)) {
491 if ((k
= dbus_bus_request_name(
494 DBUS_NAME_FLAG_DO_NOT_QUEUE
|
495 (priority
< INT32_MAX
? DBUS_NAME_FLAG_ALLOW_REPLACEMENT
: 0)|
496 DBUS_NAME_FLAG_REPLACE_EXISTING
,
502 if (k
!= DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
) {
510 if (!(dbus_connection_register_object_path(
521 if (!dbus_connection_add_filter(
537 dbus_message_unref(m
);
540 dbus_message_unref(reply
);
542 if (&_error
== error
)
543 dbus_error_free(&_error
);
564 dbus_connection_remove_filter(
570 dbus_connection_unregister_object_path(
575 dbus_bus_release_name(
580 free(d
->device_name
);
581 free(d
->application_name
);
582 free(d
->application_device_name
);
583 free(d
->service_name
);
584 free(d
->object_path
);
587 dbus_connection_unref(d
->connection
);
592 int rd_set_application_device_name(rd_device
*d
, const char *n
) {
600 if (!(t
= strdup(n
)))
603 free(d
->application_device_name
);
604 d
->application_device_name
= t
;
608 void rd_set_userdata(rd_device
*d
, void *userdata
) {
614 d
->userdata
= userdata
;
617 void* rd_get_userdata(rd_device
*d
) {
627 int rd_dbus_get_name_owner(
628 DBusConnection
*connection
,
633 DBusMessage
*msg
, *reply
;
638 if (!(msg
= dbus_message_new_method_call(DBUS_SERVICE_DBUS
, DBUS_PATH_DBUS
, DBUS_INTERFACE_DBUS
, "GetNameOwner"))) {
643 if (!dbus_message_append_args(msg
, DBUS_TYPE_STRING
, &name
, DBUS_TYPE_INVALID
)) {
648 reply
= dbus_connection_send_with_reply_and_block(connection
, msg
, DBUS_TIMEOUT_USE_DEFAULT
, error
);
649 dbus_message_unref(msg
);
653 if (!dbus_message_get_args(reply
, error
, DBUS_TYPE_STRING
, name_owner
, DBUS_TYPE_INVALID
)) {
654 dbus_message_unref(reply
);
659 *name_owner
= strdup(*name_owner
);
660 dbus_message_unref(reply
);
667 } else if (dbus_error_has_name(error
, "org.freedesktop.DBus.Error.NameHasNoOwner"))
668 dbus_error_free(error
);
678 dbus_message_unref(msg
);