summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile7
-rw-r--r--reserve-monitor-test.c102
-rw-r--r--reserve-monitor.c259
-rw-r--r--reserve-monitor.h62
5 files changed, 430 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 014a764..4026568 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
*.o
reserve-test
+reserve-monitor-test
diff --git a/Makefile b/Makefile
index c32c57c..ee3929c 100644
--- a/Makefile
+++ b/Makefile
@@ -23,8 +23,13 @@
CFLAGS=-Wall -Wextra -O0 -g -pipe `pkg-config --cflags dbus-1`
LIBS=`pkg-config --libs dbus-1`
+all: reserve-test reserve-monitor-test
+
reserve-test: reserve.h reserve.o reserve-test.o
$(CC) $(CFLAGS) $^ -o $@ $(LIBS)
+reserve-monitor-test: reserve-monitor.h reserve-monitor.o reserve-monitor-test.o
+ $(CC) $(CFLAGS) $^ -o $@ $(LIBS)
+
clean:
- rm -f *.o reserve-test
+ rm -f *.o reserve-test reserve-monitor-test
diff --git a/reserve-monitor-test.c b/reserve-monitor-test.c
new file mode 100644
index 0000000..aa9fdb1
--- /dev/null
+++ b/reserve-monitor-test.c
@@ -0,0 +1,102 @@
+/***
+ 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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
+#include <dbus/dbus.h>
+
+#include "reserve-monitor.h"
+
+static DBusConnection *connection = NULL;
+
+static void change_cb(rm_monitor *m) {
+ int e;
+
+ if ((e = rm_busy(m)) < 0) {
+ fprintf(stderr, "Failed to query device status: %s\n", strerror(-e));
+ dbus_connection_close(connection);
+ return;
+ }
+
+ printf("Device is busy: %s\n", e ? "yes" : "no");
+}
+
+int main(int argc, char *argv[]) {
+ DBusError error;
+ int r = 1, e;
+ rm_monitor *monitor = NULL;
+ const char *dname = NULL;
+
+ dbus_error_init(&error);
+
+ if (argc >= 2)
+ dname = argv[1];
+ else
+ dname = "Audio0";
+
+ if (!(connection = dbus_bus_get_private(DBUS_BUS_SESSION, &error))) {
+ fprintf(stderr, "Failed to connect to session bus: %s\n", error.message);
+ goto finish;
+ }
+
+ if ((e = rm_watch(
+ &monitor,
+ connection,
+ dname,
+ change_cb,
+ &error)) < 0) {
+
+ fprintf(stderr, "Failed to watch device: %s\n", strerror(-e));
+ goto finish;
+ }
+
+ if ((e = rm_busy(monitor)) < 0) {
+ fprintf(stderr, "Failed to query device status: %s\n", strerror(-e));
+ goto finish;
+ }
+
+ printf("Device is busy: %s\n", e ? "yes" : "no");
+
+ while (dbus_connection_read_write_dispatch(connection, -1))
+ ;
+
+ r = 0;
+ fprintf(stderr, "Exiting cleanly.\n");
+
+finish:
+ if (monitor)
+ rm_release(monitor);
+
+ if (connection) {
+ if (dbus_connection_get_is_connected(connection))
+ dbus_connection_close(connection);
+
+ dbus_connection_unref(connection);
+ }
+
+ return r;
+}
diff --git a/reserve-monitor.c b/reserve-monitor.c
new file mode 100644
index 0000000..64d2a7c
--- /dev/null
+++ b/reserve-monitor.c
@@ -0,0 +1,259 @@
+/***
+ 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 <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include "reserve-monitor.h"
+
+struct rm_monitor {
+ int ref;
+
+ char *device_name;
+ char *service_name;
+
+ DBusConnection *connection;
+
+ unsigned busy:1;
+ unsigned filtering:1;
+ unsigned matching:1;
+
+ rm_change_cb_t change_cb;
+ void *userdata;
+};
+
+#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
+
+static DBusHandlerResult filter_handler(
+ DBusConnection *c,
+ DBusMessage *s,
+ void *userdata) {
+
+ DBusMessage *reply;
+ rm_monitor *m;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ m = userdata;
+ assert(m->ref >= 1);
+
+ if (dbus_message_is_signal(s, "org.freedesktop.DBus", "NameOwnerChanged")) {
+ const char *name, *old, *new;
+
+ if (!dbus_message_get_args(
+ s,
+ &error,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_STRING, &old,
+ DBUS_TYPE_STRING, &new,
+ DBUS_TYPE_INVALID))
+ goto invalid;
+
+ if (strcmp(name, m->service_name) == 0) {
+
+ m->busy = !!(new && *new);
+
+ if (m->change_cb) {
+ m->ref++;
+ m->change_cb(m);
+ rm_release(m);
+ }
+ }
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+invalid:
+ if (!(reply = dbus_message_new_error(
+ s,
+ 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;
+}
+
+int rm_watch(
+ rm_monitor **_m,
+ DBusConnection *connection,
+ const char*device_name,
+ rm_change_cb_t change_cb,
+ DBusError *error) {
+
+ rm_monitor *m = NULL;
+ int r;
+ DBusError _error;
+
+ if (!error)
+ error = &_error;
+
+ dbus_error_init(error);
+
+ if (!_m)
+ return -EINVAL;
+
+ if (!connection)
+ return -EINVAL;
+
+ if (!device_name)
+ return -EINVAL;
+
+ if (!(m = calloc(sizeof(rm_monitor), 1)))
+ return -ENOMEM;
+
+ m->ref = 1;
+
+ if (!(m->device_name = strdup(device_name))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ m->connection = dbus_connection_ref(connection);
+ m->change_cb = change_cb;
+
+ if (!(m->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+ sprintf(m->service_name, SERVICE_PREFIX "%s", m->device_name);
+
+ if (!(dbus_connection_add_filter(m->connection, filter_handler, m, NULL))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ m->filtering = 1;
+
+ dbus_bus_add_match(m->connection,
+ "type='signal',"
+ "sender='" DBUS_SERVICE_DBUS "',"
+ "interface='" DBUS_INTERFACE_DBUS "',"
+ "member='NameOwnerChanged'", error);
+
+ if (dbus_error_is_set(error)) {
+ r = -EIO;
+ goto fail;
+ }
+
+ m->matching = 1;
+
+ m->busy = dbus_bus_name_has_owner(m->connection, m->service_name, error);
+
+ if (dbus_error_is_set(error)) {
+ r = -EIO;
+ goto fail;
+ }
+
+ *_m = m;
+ return 0;
+
+fail:
+ if (&_error == error)
+ dbus_error_free(&_error);
+
+ if (m)
+ rm_release(m);
+
+ return r;
+}
+
+void rm_release(rm_monitor *m) {
+ if (!m)
+ return;
+
+ assert(m->ref > 0);
+
+ if (--m->ref > 0)
+ return;
+
+ if (m->matching)
+ dbus_bus_remove_match(
+ m->connection,
+ "type='signal',"
+ "sender='" DBUS_SERVICE_DBUS "',"
+ "interface='" DBUS_INTERFACE_DBUS "',"
+ "member='NameOwnerChanged'", NULL);
+
+ if (m->filtering)
+ dbus_connection_remove_filter(
+ m->connection,
+ filter_handler,
+ m);
+
+ free(m->device_name);
+ free(m->service_name);
+
+ if (m->connection)
+ dbus_connection_unref(m->connection);
+
+ free(m);
+}
+
+int rm_busy(rm_monitor *m) {
+ if (!m)
+ return -EINVAL;
+
+ assert(m->ref > 0);
+
+ return m->busy;
+}
+
+void rm_set_userdata(rm_monitor *m, void *userdata) {
+
+ if (!m)
+ return;
+
+ assert(m->ref > 0);
+ m->userdata = userdata;
+}
+
+void* rm_get_userdata(rm_monitor *m) {
+
+ if (!m)
+ return NULL;
+
+ assert(m->ref > 0);
+
+ return m->userdata;
+}
diff --git a/reserve-monitor.h b/reserve-monitor.h
new file mode 100644
index 0000000..4f4a833
--- /dev/null
+++ b/reserve-monitor.h
@@ -0,0 +1,62 @@
+#ifndef fooreservemonitorhfoo
+#define fooreservemonitorhfoo
+
+/***
+ 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 <dbus/dbus.h>
+#include <inttypes.h>
+
+typedef struct rm_monitor rm_monitor;
+
+/* Prototype for a function that is called whenever the reservation
+ * device of a device changes. Use rm_monitor_busy() to find out the
+ * new state.*/
+typedef void (*rm_change_cb_t)(rm_monitor *m);
+
+/* Creates a monitor for watching the lock status of a 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 rm_watch(
+ rm_monitor **m, /* On success a pointer to the newly allocated rm_device object will be filled in here */
+ DBusConnection *connection, /* Session bus (when D-Bus learns about user busses we should switchg to user busses) */
+ const char *device_name, /* The device to monitor, e.g. "Audio0" */
+ rm_change_cb_t change_cb, /* Will be called whenever the lock status changes. May be NULL */
+ DBusError *error); /* If we fail due to a D-Bus related issue the error will be filled in here. May be NULL. */
+
+/* Free a rm_monitor object */
+void rm_release(rm_monitor *m);
+
+/* Checks whether the device is currently reserved, and returns 1
+ * then, 0 if not, negative errno style error code value on error. */
+int rm_busy(rm_monitor *m);
+
+/* Attach a userdata pointer to an rm_monitor */
+void rm_set_userdata(rm_monitor *m, void *userdata);
+
+/* Query the userdata pointer from an rm_monitor. Returns NULL if no
+ * userdata was set. */
+void* rm_get_userdata(rm_monitor *m);
+
+#endif