]> code.delx.au - pulseaudio/blobdiff - src/modules/dbus/module-dbus-protocol.c
Remove pa_bool_t and replace it with bool.
[pulseaudio] / src / modules / dbus / module-dbus-protocol.c
index 11064c332243f4b4edb3337b2d2aff756979e9c6..a061ad37a8b4e83b6a2d2eda723559eaa42f8090 100644 (file)
 PA_MODULE_DESCRIPTION("D-Bus interface");
 PA_MODULE_USAGE(
         "access=local|remote|local,remote "
-        "tcp_port=<port number>");
-PA_MODULE_LOAD_ONCE(TRUE);
+        "tcp_port=<port number> "
+        "tcp_listen=<hostname>");
+PA_MODULE_LOAD_ONCE(true);
 PA_MODULE_AUTHOR("Tanu Kaskinen");
 PA_MODULE_VERSION(PACKAGE_VERSION);
 
-#define CLEANUP_INTERVAL 10 /* seconds */
-
 enum server_type {
     SERVER_TYPE_LOCAL,
     SERVER_TYPE_TCP
@@ -65,16 +64,17 @@ struct connection;
 
 struct userdata {
     pa_module *module;
-    pa_bool_t local_access;
-    pa_bool_t remote_access;
+    bool local_access;
+    bool remote_access;
     uint32_t tcp_port;
+    char *tcp_listen;
 
     struct server *local_server;
     struct server *tcp_server;
 
     pa_idxset *connections;
 
-    pa_time_event *cleanup_event;
+    pa_defer_event *cleanup_event;
 
     pa_dbus_protocol *dbus_protocol;
     pa_dbusiface_core *core_iface;
@@ -95,6 +95,7 @@ struct connection {
 static const char* const valid_modargs[] = {
     "access",
     "tcp_port",
+    "tcp_listen",
     NULL
 };
 
@@ -104,7 +105,6 @@ static void connection_free(struct connection *c) {
     pa_assert_se(pa_dbus_protocol_unregister_connection(c->server->userdata->dbus_protocol, pa_dbus_wrap_connection_get(c->wrap_conn)) >= 0);
 
     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);
 }
@@ -117,6 +117,7 @@ static void client_kill_cb(pa_client *c) {
     pa_assert(c->userdata);
 
     conn = c->userdata;
+    pa_idxset_remove_by_data(conn->server->userdata->connections, conn, NULL);
     connection_free(conn);
     c->userdata = NULL;
 
@@ -126,7 +127,7 @@ static void client_kill_cb(pa_client *c) {
 /* Called from pa_client_send_event(). */
 static void client_send_event_cb(pa_client *c, const char *name, pa_proplist *data) {
     struct connection *conn = NULL;
-    DBusMessage *signal = NULL;
+    DBusMessage *signal_msg = NULL;
     DBusMessageIter msg_iter;
 
     pa_assert(c);
@@ -136,22 +137,39 @@ static void client_send_event_cb(pa_client *c, const char *name, pa_proplist *da
 
     conn = c->userdata;
 
-    pa_assert_se(signal = dbus_message_new_signal(pa_dbusiface_core_get_client_path(conn->server->userdata->core_iface, c),
-                                                  PA_DBUSIFACE_CLIENT_INTERFACE,
-                                                  "ClientEvent"));
-    dbus_message_iter_init_append(signal, &msg_iter);
+    pa_assert_se(signal_msg = dbus_message_new_signal(pa_dbusiface_core_get_client_path(conn->server->userdata->core_iface, c),
+                                                      PA_DBUSIFACE_CLIENT_INTERFACE,
+                                                      "ClientEvent"));
+    dbus_message_iter_init_append(signal_msg, &msg_iter);
     pa_assert_se(dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_STRING, &name));
     pa_dbus_append_proplist(&msg_iter, data);
 
-    pa_assert_se(dbus_connection_send(pa_dbus_wrap_connection_get(conn->wrap_conn), signal, NULL));
-    dbus_message_unref(signal);
+    pa_assert_se(dbus_connection_send(pa_dbus_wrap_connection_get(conn->wrap_conn), signal_msg, NULL));
+    dbus_message_unref(signal_msg);
 }
 
 /* Called by D-Bus at the authentication phase. */
 static dbus_bool_t user_check_cb(DBusConnection *connection, unsigned long uid, void *data) {
     pa_log_debug("Allowing connection by user %lu.", uid);
 
-    return TRUE;
+    return true;
+}
+
+static DBusHandlerResult disconnection_filter_cb(DBusConnection *connection, DBusMessage *message, void *user_data) {
+    struct connection *c = user_data;
+
+    pa_assert(connection);
+    pa_assert(message);
+    pa_assert(c);
+
+    if (dbus_message_is_signal(message, "org.freedesktop.DBus.Local", "Disconnected")) {
+        /* The connection died. Now we want to free the connection object, but
+         * let's wait until this message is fully processed, in case someone
+         * else is interested in this signal too. */
+        c->server->userdata->module->core->mainloop->defer_enable(c->server->userdata->cleanup_event, 1);
+    }
+
+    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
 /* Called by D-Bus when a new client connection is received. */
@@ -180,18 +198,20 @@ static void connection_new_cb(DBusServer *dbus_server, DBusConnection *new_conne
         /* FIXME: Here we allow anyone from anywhere to access the server,
          * anonymously. Access control should be configurable. */
         dbus_connection_set_unix_user_function(new_connection, user_check_cb, NULL, NULL);
-        dbus_connection_set_allow_anonymous(new_connection, TRUE);
+        dbus_connection_set_allow_anonymous(new_connection, true);
     }
 
     c = pa_xnew(struct connection, 1);
     c->server = s;
-    c->wrap_conn = pa_dbus_wrap_connection_new_from_existing(s->userdata->module->core->mainloop, TRUE, new_connection);
+    c->wrap_conn = pa_dbus_wrap_connection_new_from_existing(s->userdata->module->core->mainloop, true, new_connection);
     c->client = client;
 
     c->client->kill = client_kill_cb;
     c->client->send_event = client_send_event_cb;
     c->client->userdata = c;
 
+    pa_assert_se(dbus_connection_add_filter(new_connection, disconnection_filter_cb, c, NULL));
+
     pa_idxset_put(s->userdata->connections, c, NULL);
 
     pa_assert_se(pa_dbus_protocol_register_connection(s->userdata->dbus_protocol, new_connection, c->client) >= 0);
@@ -282,7 +302,7 @@ static dbus_bool_t watch_add_cb(DBusWatch *watch, void *data) {
 
     dbus_watch_set_data(watch, ev, NULL);
 
-    return TRUE;
+    return true;
 }
 
 /* Called by D-Bus when a D-Bus fd watch event is removed. */
@@ -322,7 +342,7 @@ static dbus_bool_t timeout_add_cb(DBusTimeout *timeout, void *data) {
     pa_assert(s);
 
     if (!dbus_timeout_get_enabled(timeout))
-        return FALSE;
+        return false;
 
     mainloop = s->userdata->module->core->mainloop;
 
@@ -333,7 +353,7 @@ static dbus_bool_t timeout_add_cb(DBusTimeout *timeout, void *data) {
 
     dbus_timeout_set_data(timeout, ev, NULL);
 
-    return TRUE;
+    return true;
 }
 
 /* Called by D-Bus when a D-Bus timer event is removed. */
@@ -401,6 +421,7 @@ static struct server *start_server(struct userdata *u, const char *address, enum
 
     s = pa_xnew0(struct server, 1);
     s->userdata = u;
+    s->type = type;
     s->dbus_server = dbus_server_listen(address, &error);
 
     if (dbus_error_is_set(&error)) {
@@ -452,7 +473,7 @@ static struct server *start_tcp_server(struct userdata *u) {
 
     pa_assert(u);
 
-    address = pa_sprintf_malloc("tcp:host=127.0.0.1,port=%u", u->tcp_port);
+    address = pa_sprintf_malloc("tcp:host=%s,port=%u", u->tcp_listen, u->tcp_port);
 
     s = start_server(u, address, SERVER_TYPE_TCP); /* May return NULL */
 
@@ -461,7 +482,7 @@ static struct server *start_tcp_server(struct userdata *u) {
     return s;
 }
 
-static int get_access_arg(pa_modargs *ma, pa_bool_t *local_access, pa_bool_t *remote_access) {
+static int get_access_arg(pa_modargs *ma, bool *local_access, bool *remote_access) {
     const char *value = NULL;
 
     pa_assert(ma);
@@ -471,48 +492,40 @@ static int get_access_arg(pa_modargs *ma, pa_bool_t *local_access, pa_bool_t *re
     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;
-        *remote_access = TRUE;
+    if (pa_streq(value, "local")) {
+        *local_access = true;
+        *remote_access = false;
+    } else if (pa_streq(value, "remote")) {
+        *local_access = false;
+        *remote_access = true;
+    } else if (pa_streq(value, "local,remote")) {
+        *local_access = true;
+        *remote_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) {
+/* Frees dead client connections. */
+static void cleanup_cb(pa_mainloop_api *a, pa_defer_event *e, 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)) {
+    PA_IDXSET_FOREACH(conn, u->connections, idx) {
         if (!dbus_connection_get_is_connected(pa_dbus_wrap_connection_get(conn->wrap_conn))) {
+            pa_idxset_remove_by_data(u->connections, conn, NULL);
             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);
+    u->module->core->mainloop->defer_enable(e, 0);
 }
 
 int pa__init(pa_module *m) {
     struct userdata *u = NULL;
     pa_modargs *ma = NULL;
-    struct timeval cleanup_timeval;
 
     pa_assert(m);
 
@@ -523,8 +536,8 @@ int pa__init(pa_module *m) {
 
     m->userdata = u = pa_xnew0(struct userdata, 1);
     u->module = m;
-    u->local_access = TRUE;
-    u->remote_access = FALSE;
+    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) {
@@ -537,6 +550,8 @@ int pa__init(pa_module *m) {
         goto fail;
     }
 
+    u->tcp_listen = pa_xstrdup(pa_modargs_get_value(ma, "tcp_listen", "0.0.0.0"));
+
     if (u->local_access && !(u->local_server = start_local_server(u))) {
         pa_log("Starting the local D-Bus server failed.");
         goto fail;
@@ -549,13 +564,14 @@ int pa__init(pa_module *m) {
 
     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);
+    u->cleanup_event = m->core->mainloop->defer_new(m->core->mainloop, cleanup_cb, u);
+    m->core->mainloop->defer_enable(u->cleanup_event, 0);
 
     u->dbus_protocol = pa_dbus_protocol_get(m->core);
     u->core_iface = pa_dbusiface_core_new(m->core);
 
+    pa_modargs_free(ma);
+
     return 0;
 
 fail:
@@ -567,15 +583,6 @@ fail:
     return -1;
 }
 
-/* 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;
 
@@ -587,11 +594,17 @@ void pa__done(pa_module *m) {
     if (u->core_iface)
         pa_dbusiface_core_free(u->core_iface);
 
-    if (u->cleanup_event)
-        m->core->mainloop->time_free(u->cleanup_event);
-
     if (u->connections)
-        pa_idxset_free(u->connections, connection_free_cb, NULL);
+        pa_idxset_free(u->connections, (pa_free_cb_t) connection_free);
+
+    /* This must not be called before the connections are freed, because if
+     * there are any connections left, they will emit the
+     * org.freedesktop.DBus.Local.Disconnected signal, and
+     * disconnection_filter_cb() will be called. disconnection_filter_cb() then
+     * tries to enable the defer event, and if it's already freed, an assertion
+     * will be hit in mainloop.c. */
+    if (u->cleanup_event)
+        m->core->mainloop->defer_free(u->cleanup_event);
 
     if (u->tcp_server)
         server_free(u->tcp_server);
@@ -602,6 +615,7 @@ void pa__done(pa_module *m) {
     if (u->dbus_protocol)
         pa_dbus_protocol_unref(u->dbus_protocol);
 
+    pa_xfree(u->tcp_listen);
     pa_xfree(u);
     m->userdata = NULL;
 }