diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-10-30 02:06:55 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-10-30 02:08:57 +0100 |
commit | c335068380fe8c9d843cdb2cf8a00f822cfabed3 (patch) | |
tree | 3dadd6e1f6f7172daedd97de789e39a527c23c3f /src/machine/machined-dbus.c | |
parent | 07459db69f3c48ef4d69308faa14f457bd903bcf (diff) |
machined: port over to libsystemd-bus
Diffstat (limited to 'src/machine/machined-dbus.c')
-rw-r--r-- | src/machine/machined-dbus.c | 1106 |
1 files changed, 393 insertions, 713 deletions
diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index 2b622d1793..6035597913 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -24,79 +24,20 @@ #include <unistd.h> #include <pwd.h> -#include <systemd/sd-id128.h> -#include <systemd/sd-messages.h> +#include "sd-id128.h" +#include "sd-messages.h" -#include "machined.h" -#include "dbus-common.h" #include "strv.h" #include "mkdir.h" #include "path-util.h" #include "special.h" -#include "sleep-config.h" #include "fileio-label.h" #include "label.h" #include "utf8.h" #include "unit-name.h" -#include "bus-errors.h" -#include "virt.h" -#include "cgroup-util.h" - -#define BUS_MANAGER_INTERFACE \ - " <interface name=\"org.freedesktop.machine1.Manager\">\n" \ - " <method name=\"GetMachine\">\n" \ - " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"machine\" type=\"o\" direction=\"out\"/>\n" \ - " </method>\n" \ - " <method name=\"GetMachineByPID\">\n" \ - " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \ - " <arg name=\"machine\" type=\"o\" direction=\"out\"/>\n" \ - " </method>\n" \ - " <method name=\"ListMachines\">\n" \ - " <arg name=\"machines\" type=\"a(ssso)\" direction=\"out\"/>\n" \ - " </method>\n" \ - " <method name=\"CreateMachine\">\n" \ - " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"id\" type=\"ay\" direction=\"in\"/>\n" \ - " <arg name=\"service\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"class\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \ - " <arg name=\"root_directory\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"scope_properties\" type=\"a(sv)\" direction=\"in\"/>\n" \ - " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \ - " </method>\n" \ - " <method name=\"KillMachine\">\n" \ - " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \ - " <arg name=\"signal\" type=\"s\" direction=\"in\"/>\n" \ - " </method>\n" \ - " <method name=\"TerminateMachine\">\n" \ - " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \ - " </method>\n" \ - " <signal name=\"MachineNew\">\n" \ - " <arg name=\"machine\" type=\"s\"/>\n" \ - " <arg name=\"path\" type=\"o\"/>\n" \ - " </signal>\n" \ - " <signal name=\"MachineRemoved\">\n" \ - " <arg name=\"machine\" type=\"s\"/>\n" \ - " <arg name=\"path\" type=\"o\"/>\n" \ - " </signal>\n" \ - " </interface>\n" - -#define INTROSPECTION_BEGIN \ - DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ - "<node>\n" \ - BUS_MANAGER_INTERFACE \ - BUS_PROPERTIES_INTERFACE \ - BUS_PEER_INTERFACE \ - BUS_INTROSPECTABLE_INTERFACE - -#define INTROSPECTION_END \ - "</node>\n" - -#define INTERFACES_LIST \ - BUS_GENERIC_INTERFACES_LIST \ - "org.freedesktop.machine1.Manager\0" +#include "bus-util.h" +#include "time-util.h" +#include "machined.h" static bool valid_machine_name(const char *p) { size_t l; @@ -115,95 +56,170 @@ static bool valid_machine_name(const char *p) { return true; } -static int bus_manager_create_machine(Manager *manager, DBusMessage *message) { +static int method_get_machine(sd_bus *bus, sd_bus_message *message, void *userdata) { + _cleanup_free_ char *p = NULL; + Manager *m = userdata; + Machine *machine; + const char *name; + int r; + + assert(bus); + assert(message); + assert(m); + + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); + + p = machine_bus_path(machine); + if (!p) + return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL); + + return sd_bus_reply_method_return(bus, message, "o", p); +} + +static int method_get_machine_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata) { + _cleanup_free_ char *p = NULL; + Manager *m = userdata; + Machine *machine = NULL; + uint32_t pid; + int r; + + assert(bus); + assert(message); + assert(m); + + r = sd_bus_message_read(message, "u", &pid); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + + r = manager_get_machine_by_pid(m, pid, &machine); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + if (!machine) + return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_MACHINE_FOR_PID, "PID %lu does not belong to any known machine", (unsigned long) pid); + + p = machine_bus_path(machine); + if (!p) + return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL); + + return sd_bus_reply_method_return(bus, message, "o", p); +} + +static int method_list_machines(sd_bus *bus, sd_bus_message *message, void *userdata) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + Manager *m = userdata; + Machine *machine; + Iterator i; + int r; + + assert(bus); + assert(message); + assert(m); + + r = sd_bus_message_new_method_return(bus, message, &reply); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + + r = sd_bus_message_open_container(reply, 'a', "(ssso)"); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + + HASHMAP_FOREACH(machine, m->machines, i) { + _cleanup_free_ char *p = NULL; + + p = machine_bus_path(machine); + if (!p) + return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL); + + r = sd_bus_message_append(reply, "(ssso)", + machine->name, + strempty(machine_class_to_string(machine->class)), + machine->service, + p); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + } + r = sd_bus_message_close_container(reply); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + + return sd_bus_send(bus, reply, NULL); +} + +static int method_create_machine(sd_bus *bus, sd_bus_message *message, void *userdata) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; const char *name, *service, *class, *root_directory; - DBusMessageIter iter, sub; + Manager *manager = userdata; MachineClass c; uint32_t leader; sd_id128_t id; + const void *v; Machine *m; - int n, r; - void *v; + size_t n; + int r; - assert(manager); + assert(bus); assert(message); + assert(manager); - if (!dbus_message_iter_init(message, &iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &name); - - if (!valid_machine_name(name) || - !dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || - dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE) - return -EINVAL; - - dbus_message_iter_recurse(&iter, &sub); - dbus_message_iter_get_fixed_array(&sub, &v, &n); + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + if (!valid_machine_name(name)) + return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name"); + r = sd_bus_message_read_array(message, 'y', &v, &n); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); if (n == 0) id = SD_ID128_NULL; else if (n == 16) memcpy(&id, v, n); else - return -EINVAL; + return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter"); - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &service); - - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &class); + r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); if (isempty(class)) c = _MACHINE_CLASS_INVALID; else { c = machine_class_from_string(class); if (c < 0) - return -EINVAL; + return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter"); } - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) - return -EINVAL; - - dbus_message_iter_get_basic(&iter, &leader); - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; + if (leader == 1) + return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); - dbus_message_iter_get_basic(&iter, &root_directory); + if (!isempty(root_directory) && !path_is_absolute(root_directory)) + return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path"); - if (!(isempty(root_directory) || path_is_absolute(root_directory))) - return -EINVAL; + r = sd_bus_message_enter_container(message, 'a', "(sv)"); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || - dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) - return -EINVAL; + if (leader == 0) { + assert_cc(sizeof(uint32_t) == sizeof(pid_t)); - dbus_message_iter_recurse(&iter, &sub); + r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), (pid_t*) &leader); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); + } if (hashmap_get(manager->machines, name)) - return -EEXIST; - - if (leader <= 0) { - leader = bus_get_unix_process_id(manager->bus, dbus_message_get_sender(message), NULL); - if (leader == 0) - return -EINVAL; - } + return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name); r = manager_add_machine(manager, name, &m); if (r < 0) - return r; + return sd_bus_reply_method_errno(bus, message, r, NULL); m->leader = leader; m->class = c; @@ -212,7 +228,7 @@ static int bus_manager_create_machine(Manager *manager, DBusMessage *message) { if (!isempty(service)) { m->service = strdup(service); if (!m->service) { - r = -ENOMEM; + r = sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL); goto fail; } } @@ -220,18 +236,20 @@ static int bus_manager_create_machine(Manager *manager, DBusMessage *message) { if (!isempty(root_directory)) { m->root_directory = strdup(root_directory); if (!m->root_directory) { - r = -ENOMEM; + r = sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL); goto fail; } } - r = machine_start(m, &sub); - if (r < 0) + r = machine_start(m, message, &error); + if (r < 0) { + r = sd_bus_reply_method_errno(bus, message, r, &error); goto fail; + } - m->create_message = dbus_message_ref(message); + m->create_message = sd_bus_message_ref(message); - return 0; + return 1; fail: machine_add_to_gc_queue(m); @@ -239,486 +257,224 @@ fail: return r; } -static DBusHandlerResult manager_message_handler( - DBusConnection *connection, - DBusMessage *message, - void *userdata) { - +static int method_terminate_machine(sd_bus *bus, sd_bus_message *message, void *userdata) { Manager *m = userdata; - - DBusError error; - _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; + Machine *machine; + const char *name; int r; - assert(connection); + assert(bus); assert(message); assert(m); - dbus_error_init(&error); - - if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "GetMachine")) { - Machine *machine; - const char *name; - char *p; - bool b; - - if (!dbus_message_get_args( - message, - &error, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_INVALID)) - return bus_send_error_reply(connection, message, &error, -EINVAL); - - machine = hashmap_get(m->machines, name); - if (!machine) - return bus_send_error_reply(connection, message, &error, -ENOENT); - - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; - - p = machine_bus_path(machine); - if (!p) - goto oom; - - b = dbus_message_append_args( - reply, - DBUS_TYPE_OBJECT_PATH, &p, - DBUS_TYPE_INVALID); - free(p); - - if (!b) - goto oom; - - } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "GetMachineByPID")) { - uint32_t pid; - char *p; - Machine *machine; - bool b; - - if (!dbus_message_get_args( - message, - &error, - DBUS_TYPE_UINT32, &pid, - DBUS_TYPE_INVALID)) - return bus_send_error_reply(connection, message, &error, -EINVAL); - - r = manager_get_machine_by_pid(m, pid, &machine); - if (r <= 0) - return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT); - - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; - - p = machine_bus_path(machine); - if (!p) - goto oom; - - b = dbus_message_append_args( - reply, - DBUS_TYPE_OBJECT_PATH, &p, - DBUS_TYPE_INVALID); - free(p); - - if (!b) - goto oom; - - } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "ListMachines")) { - Machine *machine; - Iterator i; - DBusMessageIter iter, sub; - - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; - - dbus_message_iter_init_append(reply, &iter); - - if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssso)", &sub)) - goto oom; - - HASHMAP_FOREACH(machine, m->machines, i) { - _cleanup_free_ char *p = NULL; - DBusMessageIter sub2; - const char *class; - - if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2)) - goto oom; - - p = machine_bus_path(machine); - if (!p) - goto oom; - - class = strempty(machine_class_to_string(machine->class)); - - if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &machine->name) || - !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &class) || - !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &machine->service) || - !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) { - free(p); - goto oom; - } - - if (!dbus_message_iter_close_container(&sub, &sub2)) - goto oom; - } + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); - if (!dbus_message_iter_close_container(&iter, &sub)) - goto oom; + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); - } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "CreateMachine")) { + r = machine_stop(machine); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); - r = bus_manager_create_machine(m, message); - if (r < 0) - return bus_send_error_reply(connection, message, NULL, r); + return sd_bus_reply_method_return(bus, message, NULL); +} - } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "KillMachine")) { - const char *swho; - int32_t signo; - KillWho who; - const char *name; - Machine *machine; +static int method_kill_machine(sd_bus *bus, sd_bus_message *message, void *userdata) { + Manager *m = userdata; + Machine *machine; + const char *name; + const char *swho; + int32_t signo; + KillWho who; + int r; - if (!dbus_message_get_args( - message, - &error, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_STRING, &swho, - DBUS_TYPE_INT32, &signo, - DBUS_TYPE_INVALID)) - return bus_send_error_reply(connection, message, &error, -EINVAL); - - if (isempty(swho)) - who = KILL_ALL; - else { - who = kill_who_from_string(swho); - if (who < 0) - return bus_send_error_reply(connection, message, &error, -EINVAL); - } + assert(bus); + assert(message); + assert(m); - if (signo <= 0 || signo >= _NSIG) - return bus_send_error_reply(connection, message, &error, -EINVAL); + r = sd_bus_message_read(message, "ssi", &name, &swho, &signo); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); - machine = hashmap_get(m->machines, name); - if (!machine) - return bus_send_error_reply(connection, message, &error, -ENOENT); + if (isempty(swho)) + who = KILL_ALL; + else { + who = kill_who_from_string(swho); + if (who < 0) + return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho); + } - r = machine_kill(machine, who, signo); - if (r < 0) - return bus_send_error_reply(connection, message, NULL, r); + if (signo <= 0 || signo >= _NSIG) + return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo); - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); - } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "TerminateMachine")) { - const char *name; - Machine *machine; + r = machine_kill(machine, who, signo); + if (r < 0) + return sd_bus_reply_method_errno(bus, message, r, NULL); - if (!dbus_message_get_args( - message, - &error, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_INVALID)) - return bus_send_error_reply(connection, message, &error, -EINVAL); + return sd_bus_reply_method_return(bus, message, NULL); +} - machine = hashmap_get(m->machines, name); - if (!machine) - return bus_send_error_reply(connection, message, &error, -ENOENT); +const sd_bus_vtable manager_vtable[] = { + SD_BUS_VTABLE_START(0), + SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine, 0), + SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, 0), + SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, 0), + SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0), + SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, 0), + SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, 0), + SD_BUS_SIGNAL("MachineNew", "so", 0), + SD_BUS_SIGNAL("MachineRemoved", "so", 0), + SD_BUS_VTABLE_END +}; - r = machine_stop(machine); - if (r < 0) - return bus_send_error_reply(connection, message, NULL, r); +int machine_node_enumerator(sd_bus *bus, const char *path, char ***nodes, void *userdata) { + Machine *machine = NULL; + Manager *m = userdata; + char **l = NULL; + Iterator i; + int r; - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; + assert(bus); + assert(path); + assert(nodes); - } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) { - char *introspection = NULL; - FILE *f; - Iterator i; - Machine *machine; - size_t size; + HASHMAP_FOREACH(machine, m->machines, i) { char *p; - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; - - /* We roll our own introspection code here, instead of - * relying on bus_default_message_handler() because we - * need to generate our introspection string - * dynamically. */ - - f = open_memstream(&introspection, &size); - if (!f) - goto oom; - - fputs(INTROSPECTION_BEGIN, f); - - HASHMAP_FOREACH(machine, m->machines, i) { - p = bus_path_escape(machine->name); - - if (p) { - fprintf(f, "<node name=\"machine/%s\"/>", p); - free(p); - } - } - - fputs(INTROSPECTION_END, f); - - if (ferror(f)) { - fclose(f); - free(introspection); - goto oom; - } - - fclose(f); - - if (!introspection) - goto oom; + p = machine_bus_path(machine); + if (!p) + return -ENOMEM; - if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) { - free(introspection); - goto oom; + r = strv_push(&l, p); + if (r < 0) { + free(p); + return r; } - - free(introspection); - } else - return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, NULL); - - if (reply) { - if (!bus_maybe_send_reply(connection, message, reply)) - goto oom; } - return DBUS_HANDLER_RESULT_HANDLED; - -oom: - dbus_error_free(&error); - - return DBUS_HANDLER_RESULT_NEED_MEMORY; + *nodes = l; + return 1; } -const DBusObjectPathVTable bus_manager_vtable = { - .message_function = manager_message_handler -}; - -DBusHandlerResult bus_message_filter( - DBusConnection *connection, - DBusMessage *message, - void *userdata) { - +int match_job_removed(sd_bus *bus, sd_bus_message *message, void *userdata) { + const char *path, *result, *unit; Manager *m = userdata; - DBusError error; + Machine *machine; + uint32_t id; + int r; - assert(m); - assert(connection); + assert(bus); assert(message); + assert(m); - dbus_error_init(&error); - - log_debug("Got message: %s %s %s", strna(dbus_message_get_sender(message)), strna(dbus_message_get_interface(message)), strna(dbus_message_get_member(message))); - - if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) { - const char *path, *result, *unit; - Machine *mm; - uint32_t id; - - if (!dbus_message_get_args(message, &error, - DBUS_TYPE_UINT32, &id, - DBUS_TYPE_OBJECT_PATH, &path, - DBUS_TYPE_STRING, &unit, - DBUS_TYPE_STRING, &result, - DBUS_TYPE_INVALID)) { - log_error("Failed to parse JobRemoved message: %s", bus_error_message(&error)); - goto finish; - } - - mm = hashmap_get(m->machine_units, unit); - if (mm) { - if (streq_ptr(path, mm->scope_job)) { - free(mm->scope_job); - mm->scope_job = NULL; - - if (mm->started) { - if (streq(result, "done")) - machine_send_create_reply(mm, NULL); - else { - dbus_set_error(&error, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); - machine_send_create_reply(mm, &error); - } - } else - machine_save(mm); - } - - machine_add_to_gc_queue(mm); - } - - } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) { - - _cleanup_free_ char *unit = NULL; - const char *path; - - path = dbus_message_get_path(message); - if (!path) - goto finish; - - unit_name_from_dbus_path(path, &unit); - if (unit) { - Machine *mm; - - mm = hashmap_get(m->machine_units, unit); - if (mm) - machine_add_to_gc_queue(mm); - } - - } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) { - const char *path, *unit; - Machine *mm; - - if (!dbus_message_get_args(message, &error, - DBUS_TYPE_STRING, &unit, - DBUS_TYPE_OBJECT_PATH, &path, - DBUS_TYPE_INVALID)) { - log_error("Failed to parse UnitRemoved message: %s", bus_error_message(&error)); - goto finish; - } - - mm = hashmap_get(m->machine_units, unit); - if (mm) - machine_add_to_gc_queue(mm); + r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); + if (r < 0) { + log_error("Failed to parse JobRemoved message: %s", strerror(-r)); + return 0; + } - } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "Reloading")) { - dbus_bool_t b; + machine = hashmap_get(m->machine_units, unit); + if (!machine) + return 0; - if (!dbus_message_get_args(message, &error, - DBUS_TYPE_BOOLEAN, &b, - DBUS_TYPE_INVALID)) { - log_error("Failed to parse Reloading message: %s", bus_error_message(&error)); - goto finish; - } + if (streq_ptr(path, machine->scope_job)) { + free(machine->scope_job); + machine->scope_job = NULL; - /* systemd finished reloading, let's recheck all our machines */ - if (!b) { - Machine *mm; - Iterator i; + if (machine->started) { + if (streq(result, "done")) + machine_send_create_reply(machine, NULL); + else { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - log_debug("System manager has been reloaded, rechecking machines..."); + sd_bus_error_setf(&error, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); - HASHMAP_FOREACH(mm, m->machines, i) - machine_add_to_gc_queue(mm); - } + machine_send_create_reply(machine, &error); + } + } else + machine_save(machine); } -finish: - dbus_error_free(&error); - - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + machine_add_to_gc_queue(machine); + return 0; } -static int copy_many_fields(DBusMessageIter *dest, DBusMessageIter *src); - -static int copy_one_field(DBusMessageIter *dest, DBusMessageIter *src) { - int type, r; - - type = dbus_message_iter_get_arg_type(src); - - switch (type) { - - case DBUS_TYPE_STRUCT: { - DBusMessageIter dest_sub, src_sub; - - dbus_message_iter_recurse(src, &src_sub); - - if (!dbus_message_iter_open_container(dest, DBUS_TYPE_STRUCT, NULL, &dest_sub)) - return log_oom(); - - r = copy_many_fields(&dest_sub, &src_sub); - if (r < 0) - return r; +int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata) { + _cleanup_free_ char *unit = NULL; + Manager *m = userdata; + Machine *machine; + const char *path; - if (!dbus_message_iter_close_container(dest, &dest_sub)) - return log_oom(); + assert(bus); + assert(message); + assert(m); + path = sd_bus_message_get_path(message); + if (!path) return 0; - } - case DBUS_TYPE_ARRAY: { - DBusMessageIter dest_sub, src_sub; + unit_name_from_dbus_path(path, &unit); + if (!unit) + return 0; - dbus_message_iter_recurse(src, &src_sub); + machine = hashmap_get(m->machine_units, unit); + if (machine) + machine_add_to_gc_queue(machine); - if (!dbus_message_iter_open_container(dest, DBUS_TYPE_ARRAY, dbus_message_iter_get_signature(&src_sub), &dest_sub)) - return log_oom(); + return 0; +} - r = copy_many_fields(&dest_sub, &src_sub); - if (r < 0) - return r; +int match_unit_removed(sd_bus *bus, sd_bus_message *message, void *userdata) { + const char *path, *unit; + Manager *m = userdata; + Machine *machine; + int r; - if (!dbus_message_iter_close_container(dest, &dest_sub)) - return log_oom(); + assert(bus); + assert(message); + assert(m); + r = sd_bus_message_read(message, "so", &unit, &path); + if (r < 0) { + log_error("Failed to parse UnitRemoved message: %s", strerror(-r)); return 0; } - case DBUS_TYPE_VARIANT: { - DBusMessageIter dest_sub, src_sub; - - dbus_message_iter_recurse(src, &src_sub); + machine = hashmap_get(m->machine_units, unit); + if (machine) + machine_add_to_gc_queue(machine); - if (!dbus_message_iter_open_container(dest, DBUS_TYPE_VARIANT, dbus_message_iter_get_signature(&src_sub), &dest_sub)) - return log_oom(); - - r = copy_one_field(&dest_sub, &src_sub); - if (r < 0) - return r; + return 0; +} - if (!dbus_message_iter_close_container(dest, &dest_sub)) - return log_oom(); +int match_reloading(sd_bus *bus, sd_bus_message *message, void *userdata) { + Manager *m = userdata; + int b, r; - return 0; - } + assert(bus); - case DBUS_TYPE_STRING: - case DBUS_TYPE_OBJECT_PATH: - case DBUS_TYPE_BYTE: - case DBUS_TYPE_BOOLEAN: - case DBUS_TYPE_UINT16: - case DBUS_TYPE_INT16: - case DBUS_TYPE_UINT32: - case DBUS_TYPE_INT32: - case DBUS_TYPE_UINT64: - case DBUS_TYPE_INT64: - case DBUS_TYPE_DOUBLE: - case DBUS_TYPE_SIGNATURE: { - const void *p; - - dbus_message_iter_get_basic(src, &p); - dbus_message_iter_append_basic(dest, type, &p); + r = sd_bus_message_read(message, "b", &b); + if (r < 0) { + log_error("Failed to parse Reloading message: %s", strerror(-r)); return 0; } - default: - return -EINVAL; - } -} - -static int copy_many_fields(DBusMessageIter *dest, DBusMessageIter *src) { - int r; - - assert(dest); - assert(src); - - while (dbus_message_iter_get_arg_type(src) != DBUS_TYPE_INVALID) { + /* systemd finished reloading, let's recheck all our machines */ + if (!b) { + Machine *machine; + Iterator i; - r = copy_one_field(dest, src); - if (r < 0) - return r; + log_debug("System manager has been reloaded, rechecking machines..."); - dbus_message_iter_next(src); + HASHMAP_FOREACH(machine, m->machines, i) + machine_add_to_gc_queue(machine); } return 0; @@ -730,108 +486,80 @@ int manager_start_scope( pid_t pid, const char *slice, const char *description, - DBusMessageIter *more_properties, - DBusError *error, + sd_bus_message *more_properties, + sd_bus_error *error, char **job) { - _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL; - DBusMessageIter iter, sub, sub2, sub3, sub4; - const char *timeout_stop_property = "TimeoutStopUSec"; - const char *pids_property = "PIDs"; - uint64_t timeout = 500 * USEC_PER_MSEC; - const char *fail = "fail"; - uint32_t u; + _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; int r; assert(manager); assert(scope); assert(pid > 1); - if (!slice) - slice = ""; - - m = dbus_message_new_method_call( + r = sd_bus_message_new_method_call( + manager->bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "StartTransientUnit"); - if (!m) - return log_oom(); + "StartTransientUnit", + &m); + if (r < 0) + return r; - dbus_message_iter_init_append(m, &iter); + r = sd_bus_message_append(m, "ss", scope, "fail"); + if (r < 0) + return r; - if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &scope) || - !dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &fail) || - !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sv)", &sub)) - return log_oom(); + r = sd_bus_message_open_container(m, 'a', "(sv)"); + if (r < 0) + return r; if (!isempty(slice)) { - const char *slice_property = "Slice"; - - if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) || - !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &slice_property) || - !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "s", &sub3) || - !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &slice) || - !dbus_message_iter_close_container(&sub2, &sub3) || - !dbus_message_iter_close_container(&sub, &sub2)) - return log_oom(); + r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); + if (r < 0) + return r; } if (!isempty(description)) { - const char *description_property = "Description"; - - if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) || - !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description_property) || - !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "s", &sub3) || - !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &description) || - !dbus_message_iter_close_container(&sub2, &sub3) || - !dbus_message_iter_close_container(&sub, &sub2)) - return log_oom(); + r = sd_bus_message_append(m, "(sv)", "Description", "s", description); + if (r < 0) + return r; } /* cgroup empty notification is not available in containers * currently. To make this less problematic, let's shorten the - * stop timeout for sessions, so that we don't wait + * stop timeout for machines, so that we don't wait * forever. */ + r = sd_bus_message_append(m, "(sv)", "TimeoutStopUSec", "t", 500 * USEC_PER_MSEC); + if (r < 0) + return r; - if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) || - !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &timeout_stop_property) || - !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "t", &sub3) || - !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_UINT64, &timeout) || - !dbus_message_iter_close_container(&sub2, &sub3) || - !dbus_message_iter_close_container(&sub, &sub2)) - return log_oom(); - - u = pid; - if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) || - !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &pids_property) || - !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "au", &sub3) || - !dbus_message_iter_open_container(&sub3, DBUS_TYPE_ARRAY, "u", &sub4) || - !dbus_message_iter_append_basic(&sub4, DBUS_TYPE_UINT32, &u) || - !dbus_message_iter_close_container(&sub3, &sub4) || - !dbus_message_iter_close_container(&sub2, &sub3) || - !dbus_message_iter_close_container(&sub, &sub2)) - return log_oom(); + r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); + if (r < 0) + return r; if (more_properties) { - r = copy_many_fields(&sub, more_properties); + r = sd_bus_message_copy(m, more_properties, true); if (r < 0) return r; } - if (!dbus_message_iter_close_container(&iter, &sub)) - return log_oom(); + r = sd_bus_message_close_container(m); + if (r < 0) + return r; - reply = dbus_connection_send_with_reply_and_block(manager->bus, m, -1, error); - if (!reply) - return -EIO; + r = sd_bus_send_with_reply_and_block(manager->bus, m, 0, error, &reply); + if (r < 0) + return r; if (job) { const char *j; char *copy; - if (!dbus_message_get_args(reply, error, DBUS_TYPE_OBJECT_PATH, &j, DBUS_TYPE_INVALID)) - return -EIO; + r = sd_bus_message_read(reply, "o", &j); + if (r < 0) + return r; copy = strdup(j); if (!copy) @@ -840,40 +568,36 @@ int manager_start_scope( *job = copy; } - return 0; + return 1; } -int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char **job) { - _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; - const char *fail = "fail"; +int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; int r; assert(manager); assert(unit); - r = bus_method_call_with_reply( + r = sd_bus_call_method( manager->bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StopUnit", - &reply, error, - DBUS_TYPE_STRING, &unit, - DBUS_TYPE_STRING, &fail, - DBUS_TYPE_INVALID); + &reply, + "ss", unit, "fail"); if (r < 0) { - if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || - dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) { + if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || + sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) { if (job) *job = NULL; - dbus_error_free(error); + sd_bus_error_free(error); return 0; } - log_error("Failed to stop unit %s: %s", unit, bus_error(error, r)); return r; } @@ -881,12 +605,9 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char const char *j; char *copy; - if (!dbus_message_get_args(reply, error, - DBUS_TYPE_OBJECT_PATH, &j, - DBUS_TYPE_INVALID)) { - log_error("Failed to parse reply."); - return -EIO; - } + r = sd_bus_message_read(reply, "o", &j); + if (r < 0) + return r; copy = strdup(j); if (!copy) @@ -898,139 +619,98 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char return 1; } -int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, DBusError *error) { - _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; - const char *w; +int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; int r; assert(manager); assert(unit); - w = who == KILL_LEADER ? "process" : "cgroup"; - assert_cc(sizeof(signo) == sizeof(int32_t)); - - r = bus_method_call_with_reply( + r = sd_bus_call_method( manager->bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "KillUnit", - &reply, error, - DBUS_TYPE_STRING, &unit, - DBUS_TYPE_STRING, &w, - DBUS_TYPE_INT32, &signo, - DBUS_TYPE_INVALID); - if (r < 0) { - log_error("Failed to stop unit %s: %s", unit, bus_error(error, r)); - return r; - } + &reply, + "ssi", unit, who == KILL_LEADER ? "main" : "all", signo); - return 0; + return r; } int manager_unit_is_active(Manager *manager, const char *unit) { - - const char *interface = "org.freedesktop.systemd1.Unit"; - const char *property = "ActiveState"; - _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_free_ char *path = NULL; - DBusMessageIter iter, sub; const char *state; - DBusError error; int r; assert(manager); assert(unit); - dbus_error_init(&error); - path = unit_dbus_path_from_name(unit); if (!path) return -ENOMEM; - r = bus_method_call_with_reply( + r = sd_bus_get_property( manager->bus, "org.freedesktop.systemd1", path, - "org.freedesktop.DBus.Properties", - "Get", - &reply, + "org.freedesktop.systemd1.Unit", + "ActiveState", &error, - DBUS_TYPE_STRING, &interface, - DBUS_TYPE_STRING, &property, - DBUS_TYPE_INVALID); + &reply, + "s"); if (r < 0) { - if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY) || - dbus_error_has_name(&error, DBUS_ERROR_DISCONNECTED)) { - dbus_error_free(&error); + if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || + sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) return true; - } - if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || - dbus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) { - dbus_error_free(&error); + if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || + sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) return false; - } - log_error("Failed to query ActiveState: %s", bus_error(&error, r)); - dbus_error_free(&error); return r; } - if (!dbus_message_iter_init(reply, &iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) { - log_error("Failed to parse reply."); + r = sd_bus_message_read(reply, "s", &state); + if (r < 0) return -EINVAL; - } - - dbus_message_iter_recurse(&iter, &sub); - if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) { - log_error("Failed to parse reply."); - return -EINVAL; - } - - dbus_message_iter_get_basic(&sub, &state); return !streq(state, "inactive") && !streq(state, "failed"); } -int manager_add_machine(Manager *m, const char *name, Machine **_machine) { - Machine *machine; - - assert(m); - assert(name); - - machine = hashmap_get(m->machines, name); - if (!machine) { - machine = machine_new(m, name); - if (!machine) - return -ENOMEM; - } - - if (_machine) - *_machine = machine; +int manager_job_is_active(Manager *manager, const char *path) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + int r; - return 0; -} + assert(manager); + assert(path); -int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) { - _cleanup_free_ char *unit = NULL; - Machine *mm; - int r; + r = sd_bus_get_property( + manager->bus, + "org.freedesktop.systemd1", + path, + "org.freedesktop.systemd1.Job", + "State", + &error, + &reply, + "s"); + if (r < 0) { + if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || + sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) + return true; - assert(m); - assert(pid >= 1); - assert(machine); + if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) + return false; - r = cg_pid_get_unit(pid, &unit); - if (r < 0) return r; + } - mm = hashmap_get(m->machine_units, unit); - if (!mm) - return 0; + /* We don't actually care about the state really. The fact + * that we could read the job state is enough for us */ - *machine = mm; - return 1; + return true; } |