summaryrefslogtreecommitdiff
path: root/src/dbus.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2010-06-19 03:04:04 +0200
committerLennart Poettering <lennart@poettering.net>2010-06-19 03:04:04 +0200
commit5e8d1c9a9f15b7453474dc4879bdb4021c3f50a1 (patch)
treeff305b07ce88ea21de22fc86b53c246e40d857c1 /src/dbus.c
parentf9704703f35a281d9f8fa12018bd396b2be30d36 (diff)
dbus: to make sure that systemd stays controllable during early bootup, register our services on our own micro usb server in addition to the bus
Diffstat (limited to 'src/dbus.c')
-rw-r--r--src/dbus.c367
1 files changed, 267 insertions, 100 deletions
diff --git a/src/dbus.c b/src/dbus.c
index a4e350c33b..a34c7dc17b 100644
--- a/src/dbus.c
+++ b/src/dbus.c
@@ -43,6 +43,8 @@
#include "dbus-timer.h"
#include "dbus-path.h"
+#define CONNECTIONS_MAX 52
+
static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
@@ -66,33 +68,24 @@ const char *const bus_interface_table[] = {
};
static const char *error_to_dbus(int error);
+static void bus_done_api(Manager *m);
+static void bus_done_system(Manager *m);
+static void bus_done_private(Manager *m);
-static void api_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
- Manager *m = data;
-
- assert(bus);
- assert(m);
-
- if (!m->api_bus)
- return;
-
- assert(m->api_bus == bus);
-
- m->request_api_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
-}
-
-static void system_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
+static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
Manager *m = data;
assert(bus);
assert(m);
- if (!m->system_bus)
- return;
-
- assert(m->system_bus == bus);
+ /* We maintain two sets, one for those connections where we
+ * requested a dispatch, and another where we didn't. And then,
+ * we move the connections between the two sets. */
- m->request_system_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
+ if (status == DBUS_DISPATCH_COMPLETE)
+ set_move_one(m->bus_connections, m->bus_connections_for_dispatch, bus);
+ else
+ set_move_one(m->bus_connections_for_dispatch, m->bus_connections, bus);
}
static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
@@ -472,7 +465,30 @@ static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, D
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
+static DBusHandlerResult private_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
+ Manager *m = data;
+
+ assert(connection);
+ assert(message);
+ assert(m);
+
+ /* log_debug("Got D-Bus request: %s.%s() on %s", */
+ /* dbus_message_get_interface(message), */
+ /* dbus_message_get_member(message), */
+ /* dbus_message_get_path(message)); */
+
+ if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
+ set_remove(m->bus_connections, connection);
+ set_remove(m->bus_connections_for_dispatch, connection);
+ dbus_connection_unref(connection);
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
unsigned bus_dispatch(Manager *m) {
+ DBusConnection *c;
+
assert(m);
if (m->queued_message) {
@@ -480,23 +496,17 @@ unsigned bus_dispatch(Manager *m) {
* dispatch any D-Bus messages, so that we won't end
* up wanting to queue another message. */
- if (!dbus_connection_send(m->api_bus, m->queued_message, NULL))
- return 0;
+ if (m->api_bus)
+ if (!dbus_connection_send(m->api_bus, m->queued_message, NULL))
+ return 0;
dbus_message_unref(m->queued_message);
m->queued_message = NULL;
}
- if (m->request_api_bus_dispatch) {
- if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE)
- m->request_api_bus_dispatch = false;
-
- return 1;
- }
-
- if (m->request_system_bus_dispatch) {
- if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE)
- m->request_system_bus_dispatch = false;
+ if ((c = set_first(m->bus_connections_for_dispatch))) {
+ if (dbus_connection_dispatch(c) == DBUS_DISPATCH_COMPLETE)
+ set_move_one(m->bus_connections, m->bus_connections_for_dispatch, c);
return 1;
}
@@ -689,13 +699,57 @@ static int bus_setup_loop(Manager *m, DBusConnection *bus) {
dbus_connection_set_exit_on_disconnect(bus, FALSE);
if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
- !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL))
+ !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
+ log_error("Not enough memory");
return -ENOMEM;
+ }
+ if (set_put(m->bus_connections_for_dispatch, bus) < 0) {
+ log_error("Not enough memory");
+ return -ENOMEM;
+ }
+
+ dbus_connection_set_dispatch_status_function(bus, bus_dispatch_status, m, NULL);
return 0;
}
-int bus_init_system(Manager *m) {
+static dbus_bool_t allow_only_root(DBusConnection *connection, unsigned long uid, void *data) {
+ return uid == 0;
+}
+
+static void bus_new_connection(
+ DBusServer *server,
+ DBusConnection *new_connection,
+ void *data) {
+
+ Manager *m = data;
+
+ assert(m);
+
+ if (set_size(m->bus_connections) >= CONNECTIONS_MAX) {
+ log_error("Too many concurrent connections.");
+ return;
+ }
+
+ dbus_connection_set_unix_user_function(new_connection, allow_only_root, NULL, NULL);
+
+ if (bus_setup_loop(m, new_connection) < 0)
+ return;
+
+ if (!dbus_connection_register_object_path(new_connection, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
+ !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
+ !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
+ !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) {
+ log_error("Not enough memory.");
+ return;
+ }
+
+ log_debug("Accepted connection on private bus.");
+
+ dbus_connection_ref(new_connection);
+}
+
+static int bus_init_system(Manager *m) {
DBusError error;
char *id;
int r;
@@ -712,22 +766,18 @@ int bus_init_system(Manager *m) {
else {
if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
- dbus_error_free(&error);
- return 0;
+ r = 0;
+ goto fail;
}
- dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL);
- m->request_system_bus_dispatch = true;
-
- if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
- bus_done_system(m);
- return r;
- }
+ if ((r = bus_setup_loop(m, m->system_bus)) < 0)
+ goto fail;
}
if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
- bus_done_system(m);
- return -ENOMEM;
+ log_error("Not enough memory");
+ r = -EIO;
+ goto fail;
}
dbus_bus_add_match(m->system_bus,
@@ -739,9 +789,8 @@ int bus_init_system(Manager *m) {
if (dbus_error_is_set(&error)) {
log_error("Failed to register match: %s", error.message);
- dbus_error_free(&error);
- bus_done_system(m);
- return -ENOMEM;
+ r = -EIO;
+ goto fail;
}
log_debug("Successfully connected to system D-Bus bus %s as %s",
@@ -750,9 +799,15 @@ int bus_init_system(Manager *m) {
dbus_free(id);
return 0;
+
+fail:
+ bus_done_system(m);
+ dbus_error_free(&error);
+
+ return r;
}
-int bus_init_api(Manager *m) {
+static int bus_init_api(Manager *m) {
DBusError error;
char *id;
int r;
@@ -764,34 +819,26 @@ int bus_init_api(Manager *m) {
if (m->api_bus)
return 0;
- if (m->name_data_slot < 0)
- if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot))
- return -ENOMEM;
-
if (m->running_as != MANAGER_SESSION && m->system_bus)
m->api_bus = m->system_bus;
else {
if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
- dbus_error_free(&error);
- return 0;
+ r = 0;
+ goto fail;
}
- dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL);
- m->request_api_bus_dispatch = true;
-
- if ((r = bus_setup_loop(m, m->api_bus)) < 0) {
- bus_done_api(m);
- return r;
- }
+ if ((r = bus_setup_loop(m, m->api_bus)) < 0)
+ goto fail;
}
if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
!dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
!dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
!dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
- bus_done_api(m);
- return -ENOMEM;
+ log_error("Not enough memory");
+ r = -ENOMEM;
+ goto fail;
}
/* Get NameOwnerChange messages */
@@ -805,9 +852,8 @@ int bus_init_api(Manager *m) {
if (dbus_error_is_set(&error)) {
log_error("Failed to register match: %s", error.message);
- dbus_error_free(&error);
- bus_done_api(m);
- return -ENOMEM;
+ r = -EIO;
+ goto fail;
}
/* Get activation requests */
@@ -821,20 +867,15 @@ int bus_init_api(Manager *m) {
if (dbus_error_is_set(&error)) {
log_error("Failed to register match: %s", error.message);
- dbus_error_free(&error);
- bus_done_api(m);
- return -ENOMEM;
+ r = -EIO;
+ goto fail;
}
- if ((r = request_name(m)) < 0) {
- bus_done_api(m);
- return r;
- }
+ if ((r = request_name(m)) < 0)
+ goto fail;
- if ((r = query_name_list(m)) < 0) {
- bus_done_api(m);
- return r;
- }
+ if ((r = query_name_list(m)) < 0)
+ goto fail;
log_debug("Successfully connected to API D-Bus bus %s as %s",
strnull((id = dbus_connection_get_server_id(m->api_bus))),
@@ -842,23 +883,106 @@ int bus_init_api(Manager *m) {
dbus_free(id);
if (!m->subscribed)
- if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
+ if (!(m->subscribed = set_new(string_hash_func, string_compare_func))) {
+ log_error("Not enough memory");
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ return 0;
+
+fail:
+ bus_done_api(m);
+ dbus_error_free(&error);
+
+ return r;
+}
+
+static int bus_init_private(Manager *m) {
+ DBusError error;
+ int r;
+ const char *const external_only[] = {
+ "EXTERNAL",
+ NULL
+ };
+
+ assert(m);
+
+ dbus_error_init(&error);
+
+ if (m->private_bus)
+ return 0;
+
+ /* We want the private bus only when running as init */
+ if (m->running_as != MANAGER_INIT)
+ return 0;
+
+ if (!(m->private_bus = dbus_server_listen("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
+ log_error("Failed to create private D-Bus server: %s", error.message);
+ r = -EIO;
+ goto fail;
+ }
+
+ if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) ||
+ !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
+ !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
+ log_error("Not enough memory");
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ dbus_server_set_new_connection_function(m->private_bus, bus_new_connection, m, NULL);
+
+ log_debug("Successfully create private D-Bus server.");
+
+ return 0;
+
+fail:
+ bus_done_private(m);
+ dbus_error_free(&error);
+
+ return r;
+}
+
+int bus_init(Manager *m) {
+ int r;
+
+ if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
+ set_ensure_allocated(&m->bus_connections_for_dispatch, trivial_hash_func, trivial_compare_func) < 0) {
+ log_error("Not enough memory");
+ return -ENOMEM;
+ }
+
+ if (m->name_data_slot < 0)
+ if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) {
+ log_error("Not enough memory");
return -ENOMEM;
+ }
+
+ if ((r = bus_init_system(m)) < 0 ||
+ (r = bus_init_api(m)) < 0 ||
+ (r = bus_init_private(m)) < 0)
+ return r;
return 0;
}
-void bus_done_api(Manager *m) {
+static void shutdown_connection(DBusConnection *c) {
+ dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
+ dbus_connection_flush(c);
+ dbus_connection_close(c);
+ dbus_connection_unref(c);
+}
+
+static void bus_done_api(Manager *m) {
assert(m);
if (m->api_bus) {
if (m->system_bus == m->api_bus)
m->system_bus = NULL;
- dbus_connection_set_dispatch_status_function(m->api_bus, NULL, NULL, NULL);
- dbus_connection_flush(m->api_bus);
- dbus_connection_close(m->api_bus);
- dbus_connection_unref(m->api_bus);
+ set_remove(m->bus_connections, m->api_bus);
+ shutdown_connection(m->api_bus);
m->api_bus = NULL;
}
@@ -872,30 +996,54 @@ void bus_done_api(Manager *m) {
m->subscribed = NULL;
}
- if (m->name_data_slot >= 0)
- dbus_pending_call_free_data_slot(&m->name_data_slot);
-
if (m->queued_message) {
dbus_message_unref(m->queued_message);
m->queued_message = NULL;
}
}
-void bus_done_system(Manager *m) {
+static void bus_done_system(Manager *m) {
assert(m);
if (m->system_bus == m->api_bus)
bus_done_api(m);
if (m->system_bus) {
- dbus_connection_set_dispatch_status_function(m->system_bus, NULL, NULL, NULL);
- dbus_connection_flush(m->system_bus);
- dbus_connection_close(m->system_bus);
- dbus_connection_unref(m->system_bus);
+ set_remove(m->bus_connections, m->system_bus);
+ shutdown_connection(m->system_bus);
m->system_bus = NULL;
}
}
+static void bus_done_private(Manager *m) {
+
+ if (m->private_bus) {
+ dbus_server_disconnect(m->private_bus);
+ dbus_server_unref(m->private_bus);
+ m->private_bus = NULL;
+ }
+}
+
+void bus_done(Manager *m) {
+ DBusConnection *c;
+
+ bus_done_api(m);
+ bus_done_system(m);
+ bus_done_private(m);
+
+ while ((c = set_steal_first(m->bus_connections)))
+ shutdown_connection(c);
+
+ while ((c = set_steal_first(m->bus_connections_for_dispatch)))
+ shutdown_connection(c);
+
+ set_free(m->bus_connections);
+ set_free(m->bus_connections_for_dispatch);
+
+ if (m->name_data_slot >= 0)
+ dbus_pending_call_free_data_slot(&m->name_data_slot);
+}
+
static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
Manager *m = userdata;
DBusMessage *reply;
@@ -992,7 +1140,7 @@ oom:
return -ENOMEM;
}
-DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
+DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char*introspection, const BusProperty *properties) {
DBusError error;
DBusMessage *reply = NULL;
int r;
@@ -1020,7 +1168,7 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message,
DBUS_TYPE_STRING, &interface,
DBUS_TYPE_STRING, &property,
DBUS_TYPE_INVALID))
- return bus_send_error_reply(m, message, &error, -EINVAL);
+ return bus_send_error_reply(m, c, message, &error, -EINVAL);
for (p = properties; p->property; p++)
if (streq(p->interface, interface) && streq(p->property, property))
@@ -1043,7 +1191,7 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message,
goto oom;
dbus_message_unref(reply);
- return bus_send_error_reply(m, message, NULL, r);
+ return bus_send_error_reply(m, c, message, NULL, r);
}
if (!dbus_message_iter_close_container(&iter, &sub))
@@ -1060,7 +1208,7 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message,
&error,
DBUS_TYPE_STRING, &interface,
DBUS_TYPE_INVALID))
- return bus_send_error_reply(m, message, &error, -EINVAL);
+ return bus_send_error_reply(m, c, message, &error, -EINVAL);
if (!(reply = dbus_message_new_method_return(message)))
goto oom;
@@ -1085,7 +1233,7 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message,
goto oom;
dbus_message_unref(reply);
- return bus_send_error_reply(m, message, NULL, r);
+ return bus_send_error_reply(m, c, message, NULL, r);
}
if (!dbus_message_iter_close_container(&sub2, &sub3) ||
@@ -1100,7 +1248,7 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message,
}
if (reply) {
- if (!dbus_connection_send(m->api_bus, reply, NULL))
+ if (!dbus_connection_send(c, reply, NULL))
goto oom;
dbus_message_unref(reply);
@@ -1156,7 +1304,7 @@ static const char *error_to_dbus(int error) {
return DBUS_ERROR_FAILED;
}
-DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
+DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *bus_error, int error) {
DBusMessage *reply = NULL;
const char *name, *text;
@@ -1171,7 +1319,7 @@ DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusErr
if (!(reply = dbus_message_new_error(message, name, text)))
goto oom;
- if (!dbus_connection_send(m->api_bus, reply, NULL))
+ if (!dbus_connection_send(c, reply, NULL))
goto oom;
dbus_message_unref(reply);
@@ -1191,6 +1339,25 @@ oom:
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
+int bus_broadcast(Manager *m, DBusMessage *message) {
+ bool oom = false;
+ Iterator i;
+ DBusConnection *c;
+
+ assert(m);
+ assert(message);
+
+ SET_FOREACH(c, m->bus_connections_for_dispatch, i)
+ if (c != m->system_bus || m->running_as != MANAGER_SESSION)
+ oom = !dbus_connection_send(c, message, NULL);
+
+ SET_FOREACH(c, m->bus_connections, i)
+ if (c != m->system_bus || m->running_as != MANAGER_SESSION)
+ oom = !dbus_connection_send(c, message, NULL);
+
+ return oom ? -ENOMEM : 0;
+}
+
int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
const char *t = data;