along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <dbus/dbus.h>
-
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <errno.h>
#include <unistd.h>
+#include <dbus/dbus.h>
#include "dbus.h"
#include "log.h"
#include "strv.h"
+#include "cgroup.h"
+#include "dbus-unit.h"
+#include "dbus-job.h"
+#include "dbus-manager.h"
-static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
+static void api_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
Manager *m = data;
assert(bus);
assert(m);
- m->request_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
+ 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) {
+ Manager *m = data;
+
+ assert(bus);
+ assert(m);
+
+ if (!m->system_bus)
+ return;
+
+ assert(m->system_bus == bus);
+
+ m->request_system_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
}
static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
/* This is called by the event loop whenever there is
* something happening on D-Bus' file handles. */
- if (!(dbus_watch_get_enabled(w->data.bus_watch)))
+ if (!dbus_watch_get_enabled(w->data.bus_watch))
return;
dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
log_error("Failed to rearm timer: %s", strerror(-r));
}
-static DBusHandlerResult bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
+static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
Manager *m = data;
DBusError error;
/* dbus_message_get_path(message)); */
if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
- log_error("Warning! D-Bus connection terminated.");
-
- /* FIXME: we probably should restart D-Bus here */
+ log_error("Warning! API D-Bus connection terminated.");
+ bus_done_api(m);
} else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
- const char *name, *old, *new;
+ const char *name, *old_owner, *new_owner;
if (!dbus_message_get_args(message, &error,
DBUS_TYPE_STRING, &name,
- DBUS_TYPE_STRING, &old,
- DBUS_TYPE_STRING, &new,
+ DBUS_TYPE_STRING, &old_owner,
+ DBUS_TYPE_STRING, &new_owner,
DBUS_TYPE_INVALID))
log_error("Failed to parse NameOwnerChanged message: %s", error.message);
else {
if (set_remove(m->subscribed, (char*) name))
log_debug("Subscription client vanished: %s (left: %u)", name, set_size(m->subscribed));
+
+ if (old_owner[0] == 0)
+ old_owner = NULL;
+
+ if (new_owner[0] == 0)
+ new_owner = NULL;
+
+ manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
}
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
+static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
+ Manager *m = data;
+ DBusError error;
+
+ assert(connection);
+ assert(message);
+ assert(m);
+
+ dbus_error_init(&error);
+
+ /* 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")) {
+ log_error("Warning! System D-Bus connection terminated.");
+ bus_done_system(m);
+
+ } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
+ const char *cgroup;
+
+ if (!dbus_message_get_args(message, &error,
+ DBUS_TYPE_STRING, &cgroup,
+ DBUS_TYPE_INVALID))
+ log_error("Failed to parse Released message: %s", error.message);
+ else
+ cgroup_notify_empty(m, cgroup);
+ }
+
+ dbus_error_free(&error);
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
unsigned bus_dispatch(Manager *m) {
assert(m);
- if (!m->request_bus_dispatch)
- return 0;
+ if (m->queued_message) {
+ /* If we cannot get rid of this message we won't
+ * 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;
+
+ dbus_message_unref(m->queued_message);
+ m->queued_message = NULL;
+ }
- if (dbus_connection_dispatch(m->bus) == DBUS_DISPATCH_COMPLETE)
- m->request_bus_dispatch = false;
+ if (m->request_api_bus_dispatch) {
+ if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE)
+ m->request_api_bus_dispatch = false;
- return 1;
+ return 1;
+ }
+
+ if (m->request_system_bus_dispatch) {
+ if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE)
+ m->request_system_bus_dispatch = false;
+
+ return 1;
+ }
+
+ return 0;
+}
+
+static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
+ DBusMessage *reply;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ assert_se(reply = dbus_pending_call_steal_reply(pending));
+
+ switch (dbus_message_get_type(reply)) {
+
+ case DBUS_MESSAGE_TYPE_ERROR:
+
+ assert_se(dbus_set_error_from_message(&error, reply));
+ log_warning("RequestName() failed: %s", error.message);
+ break;
+
+ case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
+ uint32_t r;
+
+ if (!dbus_message_get_args(reply,
+ &error,
+ DBUS_TYPE_UINT32, &r,
+ DBUS_TYPE_INVALID)) {
+ log_error("Failed to parse RequestName() reply: %s", error.message);
+ break;
+ }
+
+ if (r == 1)
+ log_debug("Successfully acquired name.");
+ else
+ log_error("Name already owned.");
+
+ break;
+ }
+
+ default:
+ assert_not_reached("Invalid reply message");
+ }
+
+ dbus_message_unref(reply);
+ dbus_error_free(&error);
}
static int request_name(Manager *m) {
- DBusMessage *message;
const char *name = "org.freedesktop.systemd1";
uint32_t flags = 0;
+ DBusMessage *message = NULL;
+ DBusPendingCall *pending = NULL;
if (!(message = dbus_message_new_method_call(
DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"RequestName")))
- return -ENOMEM;
+ goto oom;
if (!dbus_message_append_args(
message,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &flags,
- DBUS_TYPE_INVALID)) {
+ DBUS_TYPE_INVALID))
+ goto oom;
+
+ if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
+ goto oom;
+
+ if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
+ goto oom;
+
+ dbus_message_unref(message);
+ dbus_pending_call_unref(pending);
+
+ /* We simple ask for the name and don't wait for it. Sooner or
+ * later we'll have it. */
+
+ return 0;
+
+oom:
+ if (pending) {
+ dbus_pending_call_cancel(pending);
+ dbus_pending_call_unref(pending);
+ }
+
+ if (message)
dbus_message_unref(message);
+
+ return -ENOMEM;
+}
+
+static int bus_setup_loop(Manager *m, DBusConnection *bus) {
+ assert(m);
+ assert(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))
return -ENOMEM;
+
+ return 0;
+}
+
+int bus_init_system(Manager *m) {
+ DBusError error;
+ char *id;
+ int r;
+
+ assert(m);
+
+ dbus_error_init(&error);
+
+ if (m->system_bus)
+ return 0;
+
+ if (m->running_as != MANAGER_SESSION && m->api_bus)
+ m->system_bus = m->api_bus;
+ 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;
+ }
+
+ 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 (!dbus_connection_send(m->bus, message, NULL)) {
- dbus_message_unref(message);
+ if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
+ bus_done_system(m);
return -ENOMEM;
}
- /* We simple ask for the name and don't wait for it. Sooner or
- * later we'll have it, and we wouldn't know what to do on
- * error anyway. */
+ dbus_bus_add_match(m->system_bus,
+ "type='signal',"
+ "interface='org.freedesktop.systemd1.Agent',"
+ "path='/org/freedesktop/systemd1/agent'",
+ &error);
- dbus_message_unref(message);
+ 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;
+ }
+
+ log_debug("Successfully connected to system D-Bus bus %s as %s",
+ strnull((id = dbus_connection_get_server_id(m->system_bus))),
+ strnull(dbus_bus_get_unique_name(m->system_bus)));
+ dbus_free(id);
return 0;
}
-int bus_init(Manager *m) {
+int bus_init_api(Manager *m) {
DBusError error;
char *id;
int r;
assert(m);
- if (m->bus)
+ dbus_error_init(&error);
+
+ if (m->api_bus)
return 0;
- if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
- return -ENOMEM;
+ if (m->name_data_slot < 0)
+ if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot))
+ return -ENOMEM;
- dbus_connection_set_change_sigpipe(FALSE);
+ 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;
+ }
- dbus_error_init(&error);
- if (!(m->bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
- log_error("Failed to get D-Bus connection: %s", error.message);
- dbus_error_free(&error);
- return -ECONNREFUSED;
+ 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;
+ }
}
- dbus_connection_set_exit_on_disconnect(m->bus, FALSE);
- dbus_connection_set_dispatch_status_function(m->bus, bus_dispatch_status, m, NULL);
- if (!dbus_connection_set_watch_functions(m->bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
- !dbus_connection_set_timeout_functions(m->bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL) ||
- !dbus_connection_register_object_path(m->bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
- !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
- !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
- !dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL)) {
- bus_done(m);
+ 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;
}
- dbus_bus_add_match(m->bus,
+ dbus_bus_add_match(m->api_bus,
"type='signal',"
"sender='"DBUS_SERVICE_DBUS"',"
"interface='"DBUS_INTERFACE_DBUS"',"
if (dbus_error_is_set(&error)) {
log_error("Failed to register match: %s", error.message);
dbus_error_free(&error);
- bus_done(m);
+ bus_done_api(m);
return -ENOMEM;
}
if ((r = request_name(m)) < 0) {
- bus_done(m);
+ bus_done_api(m);
return r;
}
- log_debug("Successfully connected to D-Bus bus %s as %s",
- strnull((id = dbus_connection_get_server_id(m->bus))),
- strnull(dbus_bus_get_unique_name(m->bus)));
+ log_debug("Successfully connected to API D-Bus bus %s as %s",
+ strnull((id = dbus_connection_get_server_id(m->api_bus))),
+ strnull(dbus_bus_get_unique_name(m->api_bus)));
dbus_free(id);
- m->request_bus_dispatch = true;
+ if (!m->subscribed)
+ if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
+ return -ENOMEM;
return 0;
}
-void bus_done(Manager *m) {
+void bus_done_api(Manager *m) {
assert(m);
- if (m->bus) {
- dbus_connection_close(m->bus);
- dbus_connection_unref(m->bus);
- m->bus = NULL;
+ 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);
+ m->api_bus = NULL;
}
if (m->subscribed) {
set_free(m->subscribed);
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) {
+ 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);
+ m->system_bus = NULL;
+ }
+}
+
+static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
+ Manager *m = userdata;
+ DBusMessage *reply;
+ DBusError error;
+ const char *name;
+
+ dbus_error_init(&error);
+
+ assert_se(name = dbus_pending_call_get_data(pending, m->name_data_slot));
+ assert_se(reply = dbus_pending_call_steal_reply(pending));
+
+ switch (dbus_message_get_type(reply)) {
+
+ case DBUS_MESSAGE_TYPE_ERROR:
+
+ assert_se(dbus_set_error_from_message(&error, reply));
+ log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
+ break;
+
+ case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
+ uint32_t r;
+
+ if (!dbus_message_get_args(reply,
+ &error,
+ DBUS_TYPE_UINT32, &r,
+ DBUS_TYPE_INVALID)) {
+ log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
+ break;
+ }
+
+ manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
+ break;
+ }
+
+ default:
+ assert_not_reached("Invalid reply message");
+ }
+
+ dbus_message_unref(reply);
+ dbus_error_free(&error);
+}
+
+int bus_query_pid(Manager *m, const char *name) {
+ DBusMessage *message = NULL;
+ DBusPendingCall *pending = NULL;
+ char *n = NULL;
+
+ assert(m);
+ assert(name);
+
+ if (!(message = dbus_message_new_method_call(
+ DBUS_SERVICE_DBUS,
+ DBUS_PATH_DBUS,
+ DBUS_INTERFACE_DBUS,
+ "GetConnectionUnixProcessID")))
+ goto oom;
+
+ if (!(dbus_message_append_args(
+ message,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID)))
+ goto oom;
+
+ if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
+ goto oom;
+
+ if (!(n = strdup(name)))
+ goto oom;
+
+ if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
+ goto oom;
+
+ n = NULL;
+
+ if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
+ goto oom;
+
+ dbus_message_unref(message);
+ dbus_pending_call_unref(pending);
+
+ return 0;
+
+oom:
+ free(n);
+
+ if (pending) {
+ dbus_pending_call_cancel(pending);
+ dbus_pending_call_unref(pending);
+ }
+
+ if (message)
+ dbus_message_unref(message);
+
+ return -ENOMEM;
}
DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
goto oom;
- if ((r = p->append(m, &sub, property, p->data)) < 0) {
+ if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
if (r == -ENOMEM)
goto oom;
!dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
goto oom;
- if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
+ if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
if (r == -ENOMEM)
goto oom;
}
if (reply) {
- if (!dbus_connection_send(m->bus, reply, NULL))
+ if (!dbus_connection_send(m->api_bus, reply, NULL))
goto oom;
dbus_message_unref(reply);
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
-
-
static const char *error_to_dbus(int error) {
switch(error) {
if (!(reply = dbus_message_new_error(message, name, text)))
goto oom;
- if (!dbus_connection_send(m->bus, reply, NULL))
+ if (!dbus_connection_send(m->api_bus, reply, NULL))
goto oom;
dbus_message_unref(reply);
assert(property);
assert(data);
+ /* Let's ensure that pid_t is actually 64bit, and hence this
+ * function can be used for usec_t */
+ assert_cc(sizeof(uint64_t) == sizeof(usec_t));
+
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
return -ENOMEM;
assert(property);
assert(data);
+ /* Let's ensure that pid_t and mode_t is actually 32bit, and
+ * hence this function can be used for pid_t/mode_t */
+ assert_cc(sizeof(uint32_t) == sizeof(pid_t));
+ assert_cc(sizeof(uint32_t) == sizeof(mode_t));
+ assert_cc(sizeof(uint32_t) == sizeof(unsigned));
+
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
return -ENOMEM;
return 0;
}
+
+int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
+ assert(m);
+ assert(i);
+ assert(property);
+ assert(data);
+
+ assert_cc(sizeof(int32_t) == sizeof(int));
+
+ if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
+ return -ENOMEM;
+
+ return 0;
+}
+
+int bus_parse_strv(DBusMessage *m, char ***_l) {
+ DBusMessageIter iter, sub;
+ unsigned n = 0, i = 0;
+ char **l;
+
+ assert(m);
+ assert(_l);
+
+ if (!dbus_message_iter_init(m, &iter) ||
+ dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
+ dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
+ return -EINVAL;
+
+ dbus_message_iter_recurse(&iter, &sub);
+
+ while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ n++;
+ dbus_message_iter_next(&sub);
+ }
+
+ if (!(l = new(char*, n+1)))
+ return -ENOMEM;
+
+ assert_se(dbus_message_iter_init(m, &iter));
+ dbus_message_iter_recurse(&iter, &sub);
+
+ while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ const char *s;
+
+ assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
+ dbus_message_iter_get_basic(&sub, &s);
+
+ if (!(l[i++] = strdup(s))) {
+ strv_free(l);
+ return -ENOMEM;
+ }
+
+ dbus_message_iter_next(&sub);
+ }
+
+ assert(i == n);
+ l[i] = NULL;
+
+ if (_l)
+ *_l = l;
+
+ return 0;
+}