From 72deb8c17675fe99a1c82ff9810f75e0dfed29a3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 2 Feb 2009 23:18:24 +0100 Subject: initial commit --- .gitignore | 2 + Makefile | 30 +++ reserve-test.c | 110 ++++++++++ reserve.c | 644 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ reserve.h | 70 +++++++ 5 files changed, 856 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 reserve-test.c create mode 100644 reserve.c create mode 100644 reserve.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..014a764 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +reserve-test diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c32c57c --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +# Copyright 2009 Lennart Poettering +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation files +# (the "Software"), to deal in the Software without restriction, +# including without limitation the rights to use, copy, modify, merge, +# publish, distribute, sublicense, and/or sell copies of the Software, +# and to permit persons to whom the Software is furnished to do so, +# subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +CFLAGS=-Wall -Wextra -O0 -g -pipe `pkg-config --cflags dbus-1` +LIBS=`pkg-config --libs dbus-1` + +reserve-test: reserve.h reserve.o reserve-test.o + $(CC) $(CFLAGS) $^ -o $@ $(LIBS) + +clean: + rm -f *.o reserve-test diff --git a/reserve-test.c b/reserve-test.c new file mode 100644 index 0000000..d90f420 --- /dev/null +++ b/reserve-test.c @@ -0,0 +1,110 @@ +/*** + Copyright 2009 Lennart Poettering + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +***/ + +#include +#include +#include +#include +#include +#include + +#include "reserve.h" + +#define RUN_FOR_SECS 10 + +static int quit = 0; +rd_device *device = NULL; + +static int request_cb(rd_device *d, int forced, int show_ui) { + printf("Giving up device. (forced=%i, show_ui=%i)\n", forced, show_ui); + + rd_release(d); + device = NULL; + + quit = 1; + return 1; +} + +int main(int argc, char *argv[]) { + DBusError error; + DBusConnection *c; + int r = 1, e; + time_t started_at; + int32_t priority = 0; + + dbus_error_init(&error); + + if (argc >= 2) + priority = atoi(argv[1]); + + printf("Using priority %i.\n", priority); + + if (!(c = dbus_bus_get(DBUS_BUS_SESSION, &error))) { + fprintf(stderr, "Failed to connect to session bus: %s\n", error.message); + goto finish; + } + + if ((e = rd_acquire( + &device, + c, + "Audio0", + "ReserveTest", + priority, + 1, + request_cb, + &error)) < 0) { + + fprintf(stderr, "Failed to acquire device: %s\n", + error.message ? error.message : strerror(-e)); + goto finish; + } + + printf("Successfully acquired device.\n"); + + started_at = time(NULL); + + while (!quit) { + time_t t; + + t = time(NULL); + + if (t >= started_at + RUN_FOR_SECS) + break; + + if (!dbus_connection_read_write_dispatch(c, (started_at + RUN_FOR_SECS - t) * 1000)) + break; + } + + r = 0; + +finish: + + if (device) + rd_release(device); + + if (c) + dbus_connection_unref(c); + + return r; +} diff --git a/reserve.c b/reserve.c new file mode 100644 index 0000000..0b76379 --- /dev/null +++ b/reserve.c @@ -0,0 +1,644 @@ +/*** + Copyright 2009 Lennart Poettering + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +***/ + +#include +#include +#include +#include +#include +#include + +#include "reserve.h" + +struct rd_device { + int ref; + + char *device_name; + char *application_name; + char *application_device_name; + char *service_name; + char *object_path; + int32_t priority; + + DBusConnection *connection; + + int owning:1; + int registered:1; + int filtering:1; + int gave_up:1; + + rd_request_cb_t request_cb; + void *userdata; +}; + + +#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1." +#define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/" + +static const char introspection[] = + DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + +static dbus_bool_t add_variant( + DBusMessage *m, + int type, + const void *data) { + + DBusMessageIter iter, sub; + char t[2]; + + t[0] = (char) type; + t[1] = 0; + + dbus_message_iter_init_append(m, &iter); + + if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, t, &sub)) + return FALSE; + + if (!dbus_message_iter_append_basic(&sub, type, data)) + return FALSE; + + if (!dbus_message_iter_close_container(&iter, &sub)) + return FALSE; + + return TRUE; +} + +static DBusHandlerResult object_handler( + DBusConnection *c, + DBusMessage *m, + void *userdata) { + + rd_device *d; + DBusError error; + DBusMessage *reply = NULL; + + dbus_error_init(&error); + + d = userdata; + assert(d->ref >= 1); + + if (dbus_message_is_method_call( + m, + "org.freedesktop.ReserveDevice1", + "RequestRelease")) { + + int32_t priority; + dbus_bool_t show_ui, ret; + + if (!dbus_message_get_args( + m, + &error, + DBUS_TYPE_INT32, &priority, + DBUS_TYPE_BOOLEAN, &show_ui, + DBUS_TYPE_INVALID)) + goto invalid; + + ret = FALSE; + + if (priority > d->priority && d->request_cb) { + d->ref++; + + if (d->request_cb(d, 0, show_ui) > 0) { + ret = TRUE; + d->gave_up = 1; + } + + rd_release(d); + } + + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!dbus_message_append_args( + reply, + DBUS_TYPE_BOOLEAN, &ret, + DBUS_TYPE_INVALID)) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; + + } else if (dbus_message_is_method_call( + m, + "org.freedesktop.DBus.Properties", + "Get")) { + + const char *interface, *property; + + if (!dbus_message_get_args( + m, + &error, + DBUS_TYPE_STRING, &interface, + DBUS_TYPE_STRING, &property, + DBUS_TYPE_INVALID)) + goto invalid; + + if (strcmp(interface, "org.freedesktop.ReserveDevice1") == 0) { + const char *empty = ""; + + if (strcmp(property, "ApplicationName") == 0 && d->application_name) { + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!add_variant( + reply, + DBUS_TYPE_STRING, + d->application_name ? (const char**) &d->application_name : &empty)) + goto oom; + + } else if (strcmp(property, "ApplicationDeviceName") == 0) { + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!add_variant( + reply, + DBUS_TYPE_STRING, + d->application_device_name ? (const char**) &d->application_device_name : &empty)) + goto oom; + + } else if (strcmp(property, "Priority") == 0) { + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!add_variant( + reply, + DBUS_TYPE_INT32, + &d->priority)) + goto oom; + } else if (strcmp(property, "ApplicationProcessID") == 0) { + uint64_t u; + + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + u = (uint64_t) getpid(); + + if (!add_variant( + reply, + DBUS_TYPE_UINT64, + &u)) + goto oom; + } else { + if (!(reply = dbus_message_new_error_printf( + m, + DBUS_ERROR_UNKNOWN_METHOD, + "Unknown property %s", + property))) + goto oom; + } + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + } else if (dbus_message_is_method_call( + m, + "org.freedesktop.DBus.Introspectable", + "Introspect")) { + const char *i = introspection; + + if (!(reply = dbus_message_new_method_return(m))) + goto oom; + + if (!dbus_message_append_args( + reply, + DBUS_TYPE_STRING, + &i, + DBUS_TYPE_INVALID)) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + +invalid: + if (reply) + dbus_message_unref(reply); + + if (!(reply = dbus_message_new_error( + m, + DBUS_ERROR_INVALID_ARGS, + "Invalid arguments"))) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_HANDLED; + +oom: + if (reply) + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NEED_MEMORY; +} + +static DBusHandlerResult filter_handler( + DBusConnection *c, + DBusMessage *m, + void *userdata) { + + DBusMessage *reply; + rd_device *d; + DBusError error; + + dbus_error_init(&error); + + d = userdata; + + if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) { + const char *name; + + if (!dbus_message_get_args( + m, + &error, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_INVALID)) + goto invalid; + + if (strcmp(name, d->service_name) == 0 && d->owning) { + d->owning = 0; + + if (!d->gave_up) { + d->ref++; + + if (d->request_cb) + d->request_cb(d, 1, 0); + d->gave_up = 1; + + rd_release(d); + } + + return DBUS_HANDLER_RESULT_HANDLED; + } + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + +invalid: + if (!(reply = dbus_message_new_error( + m, + DBUS_ERROR_INVALID_ARGS, + "Invalid arguments"))) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_HANDLED; + +oom: + if (reply) + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NEED_MEMORY; +} + + +static const struct DBusObjectPathVTable vtable ={ + .message_function = object_handler +}; + +int rd_acquire( + rd_device **_d, + DBusConnection *connection, + const char *device_name, + const char *application_name, + int32_t priority, + int _show_ui, + rd_request_cb_t request_cb, + DBusError *error) { + + rd_device *d = NULL; + int r, k; + DBusError _error; + DBusMessage *m = NULL, *reply = NULL; + dbus_bool_t good, show_ui; + + if (!error) + error = &_error; + + dbus_error_init(error); + + show_ui = _show_ui; + + if (!_d) + return -EINVAL; + + if (!connection) + return -EINVAL; + + if (!device_name) + return -EINVAL; + + if (!request_cb && priority != INT32_MAX) + return -EINVAL; + + if (!(d = calloc(sizeof(rd_device), 1))) + return -ENOMEM; + + d->ref = 1; + + if (!(d->device_name = strdup(device_name))) { + r = -ENOMEM; + goto fail; + } + + if (!(d->application_name = strdup(application_name))) { + r = -ENOMEM; + goto fail; + } + + d->priority = priority; + d->connection = dbus_connection_ref(connection); + d->request_cb = request_cb; + + if (!(d->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) { + r = -ENOMEM; + goto fail; + } + sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name); + + if (!(d->object_path = malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) { + r = -ENOMEM; + goto fail; + } + sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name); + + if ((k = dbus_bus_request_name( + d->connection, + d->service_name, + DBUS_NAME_FLAG_DO_NOT_QUEUE| + DBUS_NAME_FLAG_ALLOW_REPLACEMENT, + error)) < 0) { + r = -EIO; + goto fail; + } + + if (k == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) + goto success; + + if (k != DBUS_REQUEST_NAME_REPLY_EXISTS) { + r = -EIO; + goto fail; + } + + if (priority <= INT32_MIN) { + r = -EBUSY; + goto fail; + } + + if (!(m = dbus_message_new_method_call( + d->service_name, + d->object_path, + "org.freedesktop.ReserveDevice1", + "RequestRelease"))) { + r = -ENOMEM; + goto fail; + } + + if (!dbus_message_append_args( + m, + DBUS_TYPE_INT32, &d->priority, + DBUS_TYPE_BOOLEAN, &show_ui, + DBUS_TYPE_INVALID)) { + r = -ENOMEM; + goto fail; + } + + if (!(reply = dbus_connection_send_with_reply_and_block( + d->connection, + m, + show_ui ? 60*1000 : -1, /* when UI is shown, wait for a minute */ + error))) { + r = -EIO; + goto fail; + } + + if (!dbus_message_get_args( + reply, + error, + DBUS_TYPE_BOOLEAN, &good, + DBUS_TYPE_INVALID)) { + r = -EIO; + goto fail; + } + + if (!good) { + r = -EBUSY; + goto fail; + } + + if ((k = dbus_bus_request_name( + d->connection, + d->service_name, + DBUS_NAME_FLAG_DO_NOT_QUEUE| + DBUS_NAME_FLAG_ALLOW_REPLACEMENT| + DBUS_NAME_FLAG_REPLACE_EXISTING, + error)) < 0) { + r = -EIO; + goto fail; + } + + if (k != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { + r = -EIO; + goto fail; + } + +success: + d->owning = 1; + + if (!(dbus_connection_register_object_path( + d->connection, + d->object_path, + &vtable, + d))) { + r = -ENOMEM; + goto fail; + } + + d->registered = 1; + + if (!dbus_connection_add_filter( + d->connection, + filter_handler, + d, + NULL)) { + r = -ENOMEM; + goto fail; + } + + d->filtering = 1; + + *_d = d; + return 0; + +fail: + if (m) + dbus_message_unref(m); + + if (reply) + dbus_message_unref(reply); + + if (&_error == error) + dbus_error_free(&_error); + + if (d) + rd_release(d); + + return r; +} + +void rd_release( + rd_device *d) { + + if (!d) + return; + + assert(d->ref > 0); + + if (--d->ref) + return; + + + if (d->filtering) + dbus_connection_remove_filter( + d->connection, + filter_handler, + d); + + if (d->registered) + dbus_connection_unregister_object_path( + d->connection, + d->object_path); + + if (d->owning) { + DBusError error; + dbus_error_init(&error); + + dbus_bus_release_name( + d->connection, + d->service_name, + &error); + + dbus_error_free(&error); + } + + free(d->device_name); + free(d->application_name); + free(d->application_device_name); + free(d->service_name); + free(d->object_path); + + if (d->connection) + dbus_connection_unref(d->connection); + + free(d); +} + +int rd_set_application_device_name(rd_device *d, const char *n) { + char *t; + + if (!d) + return -EINVAL; + + assert(d->ref > 0); + + if (!(t = strdup(n))) + return -ENOMEM; + + free(d->application_device_name); + d->application_device_name = t; + return 0; +} + +void rd_set_userdata(rd_device *d, void *userdata) { + + if (!d) + return; + + assert(d->ref > 0); + d->userdata = userdata; +} + +void* rd_get_userdata(rd_device *d) { + + if (!d) + return NULL; + + assert(d->ref > 0); + + return d->userdata; +} diff --git a/reserve.h b/reserve.h new file mode 100644 index 0000000..6c23033 --- /dev/null +++ b/reserve.h @@ -0,0 +1,70 @@ +#ifndef fooreservehfoo +#define fooreservehfoo + +/*** + Copyright 2009 Lennart Poettering + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +***/ + +#include +#include + +typedef struct rd_device rd_device; + +/* Prototype for a function that is called whenever someone else wants + * your app to release the device you having locked. A return value <= + * 0 denies the request, a positive return value agrees to it. Before + * returning your application should close the device in question + * completely to make sure the new application may acceess it. */ +typedef int (*rd_request_cb_t)( + rd_device *d, + int forced, /* Non-zero if an application forcibly took the lock away without asking. If this is the case then the return value of this call is ignored. */ + int show_ui); /* Non-zero if your application may show a UI before unlocking the device. */ + +/* Try to lock the device. Returns 0 on success, a negative errno + * style return value on error. The DBus error might be set as well if + * the error was caused D-Bus. */ +int rd_acquire( + rd_device **d, /* On success a pointer to the newly allocated rd_device object will be filled in here */ + DBusConnection *connection, + const char *device_name, /* The device to lock, e.g. "Audio0" */ + const char *application_name, /* A human readable name of the application, e.g. "PulseAudio Sound Server" */ + int32_t priority, /* The priority for this application. If unsure use 0 */ + int show_ui, /* Is it OK if the application that currently owns the device queries the user about this in the UI before allowing/denying the request? */ + rd_request_cb_t request_cb, /* Will be called whenever someone asks that this device shall be released. May be NULL if priority is INT32_MAX */ + DBusError *error); /* If we fail due to a D-Bus related issue the error will be filled in here. May be NULL. */ + +/* Unlock (if needed) and destroy a rd_device object again */ +void rd_release(rd_device *d); + +/* Set the application device name for a rd_device object Returns 0 on + * success, a negative errno style return value on error. */ +int rd_set_application_device_name(rd_device *d, const char *name); + +/* Attach a userdata pointer to a rd_device */ +void rd_set_userdata(rd_device *d, void *userdata); + +/* Query the userdata pointer from a rd_device. Returns NULL if no + * userdata was set. */ +void* rd_get_userdata(rd_device *d); + +#endif -- cgit