#include <pulse/client-conf.h>
#include <pulse/xmalloc.h>
+#include <pulsecore/core.h>
#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-common.h>
#include <pulsecore/dbus-shared.h>
#include <pulsecore/macro.h>
#include "server-lookup.h"
struct pa_dbusobj_server_lookup {
+ pa_core *core;
pa_dbus_connection *conn;
pa_bool_t path_registered;
- pa_daemon_conf_server_type_t server_type;
};
static const char introspection[] =
" <!-- If you are looking for documentation make sure to check out\n"
" http://pulseaudio.org/wiki/DBusInterface -->\n"
" <interface name=\"org.pulseaudio.ServerLookup\">"
- " <method name=\"GetDBusServers\">"
+ " <method name=\"GetDBusAddress\">"
" <arg name=\"result\" type=\"s\" direction=\"out\"/>"
" </method>"
" </interface>"
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
-/* Caller frees the string. */
-static char *get_dbus_server_from_type(pa_daemon_conf_server_type_t server_type) {
- char *server_string = NULL;
- char *runtime_dir = NULL;
-
- switch (server_type) {
- case PA_SERVER_TYPE_USER:
- runtime_dir = pa_get_runtime_dir();
-
- if (!runtime_dir)
- return NULL;
-
- server_string = pa_sprintf_malloc("unix:path=%s/dbus_socket", runtime_dir);
- break;
-
- case PA_SERVER_TYPE_SYSTEM:
- server_string = pa_xstrdup("unix:path=/var/run/pulse/dbus_socket");
- break;
-
- case PA_SERVER_TYPE_NONE:
- server_string = pa_xnew0(char, 1);
- break;
-
- default:
- pa_assert_not_reached();
- }
-
- return server_string;
-}
-
-static DBusHandlerResult handle_get_dbus_servers(DBusConnection *conn, DBusMessage *msg, pa_dbusobj_server_lookup *sl) {
+static DBusHandlerResult handle_get_dbus_address(DBusConnection *conn, DBusMessage *msg, pa_dbusobj_server_lookup *sl) {
DBusMessage *reply = NULL;
pa_client_conf *conf = NULL;
- char *server_string = NULL;
+ char *address = NULL;
pa_assert(conn);
pa_assert(msg);
return DBUS_HANDLER_RESULT_HANDLED;
}
- server_string = pa_xstrdup(conf->default_dbus_server);
-
pa_client_conf_free(conf);
- if (!server_string) {
- if (!(server_string = get_dbus_server_from_type(sl->server_type))) {
+ if (conf->default_dbus_server) {
+ if (!(address = dbus_address_escape_value(conf->default_dbus_server)))
+ goto oom;
+ } else {
+ if (!(address = pa_get_dbus_address_from_server_type(sl->core->server_type))) {
if (!(reply = dbus_message_new_error(msg, DBUS_ERROR_FAILED, "get_dbus_server_from_type() failed.")))
goto fail;
if (!dbus_connection_send(conn, reply, NULL))
if (!(reply = dbus_message_new_method_return(msg)))
goto oom;
- if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &server_string, DBUS_TYPE_INVALID))
+ if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &address, DBUS_TYPE_INVALID))
goto fail;
if (!dbus_connection_send(conn, reply, NULL))
goto oom;
- pa_log("Sent reply with server_string '%s'.", server_string);
+ pa_log_debug("handle_get_dbus_address(): Sent reply with address '%s'.", address);
- pa_xfree(server_string);
+ pa_xfree(address);
dbus_message_unref(reply);
if (conf)
pa_client_conf_free(conf);
- pa_xfree(server_string);
+ pa_xfree(address);
if (reply)
dbus_message_unref(reply);
if (conf)
pa_client_conf_free(conf);
- pa_xfree(server_string);
+ pa_xfree(address);
if (reply)
dbus_message_unref(reply);
if (dbus_message_is_method_call(msg, "org.freedesktop.DBus.Introspectable", "Introspect"))
return handle_introspect(conn, msg, sl);
- if (dbus_message_is_method_call(msg, "org.pulseaudio.ServerLookup", "GetDBusServers"))
- return handle_get_dbus_servers(conn, msg, sl);
+ if (dbus_message_is_method_call(msg, "org.pulseaudio.ServerLookup", "GetDBusAddress"))
+ return handle_get_dbus_address(conn, msg, sl);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
.dbus_internal_pad4 = NULL
};
-pa_dbusobj_server_lookup *pa_dbusobj_server_lookup_new(pa_core *c, pa_daemon_conf_server_type_t server_type) {
+pa_dbusobj_server_lookup *pa_dbusobj_server_lookup_new(pa_core *c) {
pa_dbusobj_server_lookup *sl;
DBusError error;
dbus_error_init(&error);
sl = pa_xnew(pa_dbusobj_server_lookup, 1);
+ sl->core = c;
sl->path_registered = FALSE;
- sl->server_type = server_type;
if (!(sl->conn = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
pa_log("Unable to contact D-Bus: %s: %s", error.name, error.message);
pa_dbus_connection_unref(sl->conn);
pa_xfree(sl);
-}
\ No newline at end of file
+}
This file is part of PulseAudio.
Copyright 2009 Tanu Kaskinen
+ Copyright 2006 Lennart Poettering
+ Copyright 2006 Shams E. King
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
#include <config.h>
#endif
+#include <dbus/dbus.h>
+
+#include <pulse/mainloop-api.h>
+#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
+#include <pulsecore/client.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-common.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/idxset.h>
#include <pulsecore/macro.h>
+#include <pulsecore/modargs.h>
#include <pulsecore/module.h>
#include "module-dbus-protocol-symdef.h"
PA_MODULE_DESCRIPTION("D-Bus interface");
-PA_MODULE_USAGE("<no module arguments>");
+PA_MODULE_USAGE(
+ "access=local|remote|local,remote "
+ "tcp_port=<port number>");
PA_MODULE_LOAD_ONCE(TRUE);
PA_MODULE_AUTHOR("Tanu Kaskinen");
PA_MODULE_VERSION(PACKAGE_VERSION);
+#define CLEANUP_INTERVAL 10 /* seconds */
+
+struct server;
+struct connection;
+
struct userdata {
pa_module *module;
+ pa_bool_t local_access;
+ pa_bool_t remote_access;
+ uint32_t tcp_port;
+
+ struct server *local_server;
+ struct server *tcp_server;
+
+ pa_idxset *connections;
+
+ pa_time_event *cleanup_event;
+};
+
+struct server {
+ struct userdata *userdata;
+ DBusServer *dbus_server;
+};
+
+struct connection {
+ struct server *server;
+ pa_dbus_wrap_connection *wrap_conn;
+ pa_client *client;
+};
+
+static const char* const valid_modargs[] = {
+ "access",
+ "tcp_port",
+ NULL
};
+static void connection_free(struct connection *c) {
+ pa_assert(c);
+
+ pa_client_free(c->client);
+ pa_assert_se(pa_idxset_remove_by_data(c->server->userdata->connections, c, NULL));
+ pa_dbus_wrap_connection_free(c->wrap_conn);
+ pa_xfree(c);
+}
+
+static void client_kill_cb(pa_client *c) {
+ struct connection *conn;
+
+ pa_assert(c);
+ pa_assert(c->userdata);
+
+ conn = c->userdata;
+ connection_free(conn);
+
+ pa_log_info("Connection killed.");
+}
+
+/* Called by D-Bus when a new client connection is received. */
+static void connection_new_cb(DBusServer *dbus_server, DBusConnection *new_connection, void *data) {
+ struct server *s = data;
+ struct connection *c;
+ pa_client_new_data new_data;
+ pa_client *client;
+
+ pa_assert(new_connection);
+ pa_assert(s);
+
+ pa_client_new_data_init(&new_data);
+ new_data.module = s->userdata->module;
+ new_data.driver = __FILE__;
+ pa_proplist_sets(new_data.proplist, PA_PROP_APPLICATION_NAME, "D-Bus client"); /* TODO: Fancier name. */
+ client = pa_client_new(s->userdata->module->core, &new_data);
+ pa_client_new_data_done(&new_data);
+
+ if (!client)
+ return;
+
+ c = pa_xnew(struct connection, 1);
+ c->server = s;
+ c->wrap_conn = pa_dbus_wrap_connection_new_from_existing(s->userdata->module->core->mainloop, new_connection);
+ c->client = client;
+
+ c->client->kill = client_kill_cb;
+ c->client->send_event = NULL;
+ c->client->userdata = c;
+
+ pa_idxset_put(s->userdata->connections, c, NULL);
+}
+
+/* Called by PA mainloop when a D-Bus fd watch event needs handling. */
+static void io_event_cb(pa_mainloop_api *mainloop, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
+ unsigned int flags = 0;
+ DBusWatch *watch = userdata;
+
+#if HAVE_DBUS_WATCH_GET_UNIX_FD
+ pa_assert(fd == dbus_watch_get_unix_fd(watch));
+#else
+ pa_assert(fd == dbus_watch_get_fd(watch));
+#endif
+
+ if (!dbus_watch_get_enabled(watch)) {
+ pa_log_warn("Asked to handle disabled watch: %p %i", (void*) watch, fd);
+ return;
+ }
+
+ if (events & PA_IO_EVENT_INPUT)
+ flags |= DBUS_WATCH_READABLE;
+ if (events & PA_IO_EVENT_OUTPUT)
+ flags |= DBUS_WATCH_WRITABLE;
+ if (events & PA_IO_EVENT_HANGUP)
+ flags |= DBUS_WATCH_HANGUP;
+ if (events & PA_IO_EVENT_ERROR)
+ flags |= DBUS_WATCH_ERROR;
+
+ dbus_watch_handle(watch, flags);
+}
+
+/* Called by PA mainloop when a D-Bus timer event needs handling. */
+static void time_event_cb(pa_mainloop_api *mainloop, pa_time_event* e, const struct timeval *tv, void *userdata) {
+ DBusTimeout *timeout = userdata;
+
+ if (dbus_timeout_get_enabled(timeout)) {
+ struct timeval next = *tv;
+ dbus_timeout_handle(timeout);
+
+ /* restart it for the next scheduled time */
+ pa_timeval_add(&next, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
+ mainloop->time_restart(e, &next);
+ }
+}
+
+/* Translates D-Bus fd watch event flags to PA IO event flags. */
+static pa_io_event_flags_t get_watch_flags(DBusWatch *watch) {
+ unsigned int flags;
+ pa_io_event_flags_t events = 0;
+
+ pa_assert(watch);
+
+ flags = dbus_watch_get_flags(watch);
+
+ /* no watch flags for disabled watches */
+ if (!dbus_watch_get_enabled(watch))
+ return PA_IO_EVENT_NULL;
+
+ if (flags & DBUS_WATCH_READABLE)
+ events |= PA_IO_EVENT_INPUT;
+ if (flags & DBUS_WATCH_WRITABLE)
+ events |= PA_IO_EVENT_OUTPUT;
+
+ return events | PA_IO_EVENT_HANGUP | PA_IO_EVENT_ERROR;
+}
+
+/* Called by D-Bus when a D-Bus fd watch event is added. */
+static dbus_bool_t watch_add_cb(DBusWatch *watch, void *data) {
+ struct server *s = data;
+ pa_mainloop_api *mainloop;
+ pa_io_event *ev;
+
+ pa_assert(watch);
+ pa_assert(s);
+
+ mainloop = s->userdata->module->core->mainloop;
+
+ ev = mainloop->io_new(
+ mainloop,
+#if HAVE_DBUS_WATCH_GET_UNIX_FD
+ dbus_watch_get_unix_fd(watch),
+#else
+ dbus_watch_get_fd(watch),
+#endif
+ get_watch_flags(watch), io_event_cb, watch);
+
+ dbus_watch_set_data(watch, ev, NULL);
+
+ return TRUE;
+}
+
+/* Called by D-Bus when a D-Bus fd watch event is removed. */
+static void watch_remove_cb(DBusWatch *watch, void *data) {
+ struct server *s = data;
+ pa_io_event *ev;
+
+ pa_assert(watch);
+ pa_assert(s);
+
+ if ((ev = dbus_watch_get_data(watch)))
+ s->userdata->module->core->mainloop->io_free(ev);
+}
+
+/* Called by D-Bus when a D-Bus fd watch event is toggled. */
+static void watch_toggled_cb(DBusWatch *watch, void *data) {
+ struct server *s = data;
+ pa_io_event *ev;
+
+ pa_assert(watch);
+ pa_assert(s);
+
+ pa_assert_se(ev = dbus_watch_get_data(watch));
+
+ /* get_watch_flags() checks if the watch is enabled */
+ s->userdata->module->core->mainloop->io_enable(ev, get_watch_flags(watch));
+}
+
+/* Called by D-Bus when a D-Bus timer event is added. */
+static dbus_bool_t timeout_add_cb(DBusTimeout *timeout, void *data) {
+ struct server *s = data;
+ pa_mainloop_api *mainloop;
+ pa_time_event *ev;
+ struct timeval tv;
+
+ pa_assert(timeout);
+ pa_assert(s);
+
+ if (!dbus_timeout_get_enabled(timeout))
+ return FALSE;
+
+ mainloop = s->userdata->module->core->mainloop;
+
+ pa_gettimeofday(&tv);
+ pa_timeval_add(&tv, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
+
+ ev = mainloop->time_new(mainloop, &tv, time_event_cb, timeout);
+
+ dbus_timeout_set_data(timeout, ev, NULL);
+
+ return TRUE;
+}
+
+/* Called by D-Bus when a D-Bus timer event is removed. */
+static void timeout_remove_cb(DBusTimeout *timeout, void *data) {
+ struct server *s = data;
+ pa_time_event *ev;
+
+ pa_assert(timeout);
+ pa_assert(s);
+
+ if ((ev = dbus_timeout_get_data(timeout)))
+ s->userdata->module->core->mainloop->time_free(ev);
+}
+
+/* Called by D-Bus when a D-Bus timer event is toggled. */
+static void timeout_toggled_cb(DBusTimeout *timeout, void *data) {
+ struct server *s = data;
+ pa_mainloop_api *mainloop;
+ pa_time_event *ev;
+
+ pa_assert(timeout);
+ pa_assert(s);
+
+ mainloop = s->userdata->module->core->mainloop;
+
+ pa_assert_se(ev = dbus_timeout_get_data(timeout));
+
+ if (dbus_timeout_get_enabled(timeout)) {
+ struct timeval tv;
+
+ pa_gettimeofday(&tv);
+ pa_timeval_add(&tv, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
+
+ mainloop->time_restart(ev, &tv);
+ } else
+ mainloop->time_restart(ev, NULL);
+}
+
+static void server_free(struct server *s) {
+ pa_assert(s);
+
+ if (s->dbus_server) {
+ dbus_server_disconnect(s->dbus_server);
+ dbus_server_unref(s->dbus_server);
+ }
+
+ pa_xfree(s);
+}
+
+static struct server *start_server(struct userdata *u, const char *address) {
+ /* XXX: We assume that when we unref the DBusServer instance at module
+ * shutdown, nobody else holds any references to it. If we stop assuming
+ * that someday, dbus_server_set_new_connection_function,
+ * dbus_server_set_watch_functions and dbus_server_set_timeout_functions
+ * calls should probably register free callbacks, instead of providing NULL
+ * as they do now. */
+
+ struct server *s = NULL;
+ DBusError error;
+
+ pa_assert(u);
+ pa_assert(address);
+
+ dbus_error_init(&error);
+
+ s = pa_xnew0(struct server, 1);
+ s->userdata = u;
+ s->dbus_server = dbus_server_listen(address, &error);
+
+ if (dbus_error_is_set(&error)) {
+ pa_log("dbus_server_listen() failed: %s: %s", error.name, error.message);
+ goto fail;
+ }
+
+ dbus_server_set_new_connection_function(s->dbus_server, connection_new_cb, s, NULL);
+
+ if (!dbus_server_set_watch_functions(s->dbus_server, watch_add_cb, watch_remove_cb, watch_toggled_cb, s, NULL)) {
+ pa_log("dbus_server_set_watch_functions() ran out of memory.");
+ goto fail;
+ }
+
+ if (!dbus_server_set_timeout_functions(s->dbus_server, timeout_add_cb, timeout_remove_cb, timeout_toggled_cb, s, NULL)) {
+ pa_log("dbus_server_set_timeout_functions() ran out of memory.");
+ goto fail;
+ }
+
+ return s;
+
+fail:
+ if (s)
+ server_free(s);
+
+ dbus_error_free(&error);
+
+ return NULL;
+}
+
+static struct server *start_local_server(struct userdata *u) {
+ struct server *s = NULL;
+ char *address = NULL;
+
+ pa_assert(u);
+
+ address = pa_get_dbus_address_from_server_type(u->module->core->server_type);
+
+ s = start_server(u, address); /* May return NULL */
+
+ pa_xfree(address);
+
+ return s;
+}
+
+static struct server *start_tcp_server(struct userdata *u) {
+ pa_log("start_tcp_server(): Not implemented!");
+ return NULL;
+}
+
+static int get_access_arg(pa_modargs *ma, pa_bool_t *local_access, pa_bool_t *remote_access) {
+ const char *value = NULL;
+
+ pa_assert(ma);
+ pa_assert(local_access);
+ pa_assert(remote_access);
+
+ if (!(value = pa_modargs_get_value(ma, "access", NULL)))
+ return 0;
+
+ if (!strcmp(value, "local")) {
+ *local_access = TRUE;
+ *remote_access = FALSE;
+ } else if (!strcmp(value, "remote")) {
+ *local_access = FALSE;
+ *remote_access = TRUE;
+ } else if (!strcmp(value, "local,remote")) {
+ *local_access = TRUE;
+ *local_access = TRUE;
+ } else
+ return -1;
+
+ return 0;
+}
+
+/* Frees dead client connections. Called every CLEANUP_INTERVAL seconds. */
+static void cleanup_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) {
+ struct userdata *u = userdata;
+ struct connection *conn = NULL;
+ uint32_t idx;
+ struct timeval cleanup_timeval;
+ unsigned free_count = 0;
+
+ for (conn = pa_idxset_first(u->connections, &idx); conn; conn = pa_idxset_next(u->connections, &idx)) {
+ if (!dbus_connection_get_is_connected(pa_dbus_wrap_connection_get(conn->wrap_conn))) {
+ connection_free(conn);
+ ++free_count;
+ }
+ }
+
+ if (free_count > 0)
+ pa_log_debug("Freed %u dead D-Bus client connections.", free_count);
+
+ pa_gettimeofday(&cleanup_timeval);
+ cleanup_timeval.tv_sec += CLEANUP_INTERVAL;
+ u->module->core->mainloop->time_restart(e, &cleanup_timeval);
+}
+
int pa__init(pa_module *m) {
struct userdata *u = NULL;
+ pa_modargs *ma = NULL;
+ struct timeval cleanup_timeval;
pa_assert(m);
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments.");
+ goto fail;
+ }
+
m->userdata = u = pa_xnew0(struct userdata, 1);
u->module = m;
+ u->local_access = TRUE;
+ u->remote_access = FALSE;
+ u->tcp_port = PA_DBUS_DEFAULT_PORT;
+
+ if (get_access_arg(ma, &u->local_access, &u->remote_access) < 0) {
+ pa_log("Invalid access argument: '%s'", pa_modargs_get_value(ma, "access", NULL));
+ goto fail;
+ }
+
+ if (pa_modargs_get_value_u32(ma, "tcp_port", &u->tcp_port) < 0 || u->tcp_port < 1 || u->tcp_port > 49150) {
+ pa_log("Invalid tcp_port argument: '%s'", pa_modargs_get_value(ma, "tcp_port", NULL));
+ goto fail;
+ }
+
+ if (u->local_access && !(u->local_server = start_local_server(u))) {
+ pa_log("Starting the local D-Bus server failed.");
+ goto fail;
+ }
+
+ if (u->remote_access && !(u->tcp_server = start_tcp_server(u))) {
+ pa_log("Starting the D-Bus server for remote connections failed.");
+ goto fail;
+ }
- pa_log_notice("Hello, world!");
+ u->connections = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ pa_gettimeofday(&cleanup_timeval);
+ cleanup_timeval.tv_sec += CLEANUP_INTERVAL;
+ u->cleanup_event = m->core->mainloop->time_new(m->core->mainloop, &cleanup_timeval, cleanup_cb, u);
return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ pa__done(m);
+
+ return -1;
}
-void pa__done(pa_module*m) {
+/* Called by idxset when the connection set is freed. */
+static void connection_free_cb(void *p, void *userdata) {
+ struct connection *conn = p;
+
+ pa_assert(conn);
+
+ connection_free(conn);
+}
+
+void pa__done(pa_module *m) {
struct userdata *u;
pa_assert(m);
if (!(u = m->userdata))
return;
+ if (u->cleanup_event)
+ m->core->mainloop->time_free(u->cleanup_event);
+
+ if (u->connections)
+ pa_idxset_free(u->connections, connection_free_cb, NULL);
+
+ if (u->tcp_server)
+ server_free(u->tcp_server);
+
+ if (u->local_server)
+ server_free(u->local_server);
+
pa_xfree(u);
m->userdata = NULL;
}