X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fmachine%2Fmachined-dbus.c;h=66594ab0b1db6f0b241ae542406a101028253321;hp=a2e00d7102bbd1bc7422adc03285de44ef837264;hb=003dffde2c1b93afbc9aff24b277276f65424406;hpb=1ee306e1248866617c96ed9f4263f375588ad838 diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index a2e00d710..66594ab0b 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -24,185 +24,246 @@ #include #include -#include -#include - -#include "machined.h" -#include "dbus-common.h" +#include "sd-id128.h" +#include "sd-messages.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" - -#define BUS_MANAGER_INTERFACE \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" - -#define INTROSPECTION_BEGIN \ - DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ - "\n" \ - BUS_MANAGER_INTERFACE \ - BUS_PROPERTIES_INTERFACE \ - BUS_PEER_INTERFACE \ - BUS_INTROSPECTABLE_INTERFACE - -#define INTROSPECTION_END \ - "\n" - -#define INTERFACES_LIST \ - BUS_GENERIC_INTERFACES_LIST \ - "org.freedesktop.machine1.Manager\0" - -static bool valid_machine_name(const char *p) { - size_t l; - - if (!filename_is_safe(p)) - return false; - - if (!ascii_is_valid(p)) - return false; - - l = strlen(p); - - if (l < 1 || l> 64) - return false; +#include "bus-util.h" +#include "bus-common-errors.h" +#include "time-util.h" +#include "cgroup-util.h" +#include "machine-image.h" +#include "image-dbus.h" +#include "machined.h" +#include "machine-dbus.h" - return true; +static int method_get_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + _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 r; + + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); + + p = machine_bus_path(machine); + if (!p) + return -ENOMEM; + + return sd_bus_reply_method_return(message, "o", p); } -static int bus_manager_create_machine(Manager *manager, DBusMessage *message) { +static int method_get_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_free_ char *p = NULL; + Manager *m = userdata; + const char *name; + int r; + + assert(bus); + assert(message); + assert(m); + + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return r; + + r = image_find(name, NULL); + if (r == 0) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); + if (r < 0) + return r; + + p = image_bus_path(name); + if (!p) + return -ENOMEM; + + return sd_bus_reply_method_return(message, "o", p); +} - const char *name, *service, *class, *slice, *root_directory; +static int method_get_machine_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_free_ char *p = NULL; - DBusMessageIter iter, sub; + Manager *m = userdata; + Machine *machine = NULL; + pid_t pid; + int r; + + assert(bus); + assert(message); + assert(m); + + assert_cc(sizeof(pid_t) == sizeof(uint32_t)); + + r = sd_bus_message_read(message, "u", &pid); + if (r < 0) + return r; + + if (pid == 0) { + _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; + + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); + if (r < 0) + return r; + + r = sd_bus_creds_get_pid(creds, &pid); + if (r < 0) + return r; + } + + r = manager_get_machine_by_pid(m, pid, &machine); + if (r < 0) + return r; + if (!machine) + return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID, "PID "PID_FMT" does not belong to any known machine", pid); + + p = machine_bus_path(machine); + if (!p) + return -ENOMEM; + + return sd_bus_reply_method_return(message, "o", p); +} + +static int method_list_machines(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + _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(message, &reply); + if (r < 0) + return sd_bus_error_set_errno(error, r); + + r = sd_bus_message_open_container(reply, 'a', "(ssso)"); + if (r < 0) + return sd_bus_error_set_errno(error, r); + + HASHMAP_FOREACH(machine, m->machines, i) { + _cleanup_free_ char *p = NULL; + + p = machine_bus_path(machine); + if (!p) + return -ENOMEM; + + 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_error_set_errno(error, r); + } + + r = sd_bus_message_close_container(reply); + if (r < 0) + return sd_bus_error_set_errno(error, r); + + return sd_bus_send(bus, reply, NULL); +} + +static int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool read_network, Machine **_m, sd_bus_error *error) { + const char *name, *service, *class, *root_directory; + const int32_t *netif = NULL; MachineClass c; uint32_t leader; sd_id128_t id; + const void *v; Machine *m; - int n, r; - void *v; + size_t n, n_netif = 0; + int r; assert(manager); assert(message); + assert(_m); - 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 r; + if (!machine_name_is_valid(name)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name"); + r = sd_bus_message_read_array(message, 'y', &v, &n); + if (r < 0) + return r; if (n == 0) id = SD_ID128_NULL; else if (n == 16) memcpy(&id, v, n); else - return -EINVAL; + return sd_bus_error_setf(error, 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; + r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory); + if (r < 0) + return r; - dbus_message_iter_get_basic(&iter, &service); + if (read_network) { + size_t i; - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; + r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif); + if (r < 0) + return r; - dbus_message_iter_get_basic(&iter, &class); + n_netif /= sizeof(int32_t); + + for (i = 0; i < n_netif; i++) { + if (netif[i] <= 0) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]); + } + } if (isempty(class)) c = _MACHINE_CLASS_INVALID; else { c = machine_class_from_string(class); if (c < 0) - return -EINVAL; + return sd_bus_error_setf(error, 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; + if (leader == 1) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); - 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 (!isempty(root_directory) && !path_is_absolute(root_directory)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path"); - dbus_message_iter_get_basic(&iter, &slice); - if (!(isempty(slice) || (unit_name_is_valid(slice, false) && endswith(slice, ".slice"))) || - !dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return -EINVAL; + if (leader == 0) { + _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; - dbus_message_iter_get_basic(&iter, &root_directory); + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); + if (r < 0) + return r; - if (!(isempty(root_directory) || path_is_absolute(root_directory))) - return -EINVAL; + assert_cc(sizeof(uint32_t) == sizeof(pid_t)); - 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; + r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); + if (r < 0) + return r; } + if (hashmap_get(manager->machines, name)) + return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name); + r = manager_add_machine(manager, name, &m); if (r < 0) - goto fail; + return r; m->leader = leader; m->class = c; @@ -224,363 +285,418 @@ static int bus_manager_create_machine(Manager *manager, DBusMessage *message) { } } - r = machine_start(m); + if (n_netif > 0) { + assert_cc(sizeof(int32_t) == sizeof(int)); + m->netif = memdup(netif, sizeof(int32_t) * n_netif); + if (!m->netif) { + r = -ENOMEM; + goto fail; + } + + m->n_netif = n_netif; + } + + *_m = m; + + return 1; + +fail: + machine_add_to_gc_queue(m); + return r; +} + +static int method_create_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) { + Manager *manager = userdata; + Machine *m = NULL; + int r; + + r = method_create_or_register_machine(manager, message, read_network, &m, error); + if (r < 0) + return r; + + r = sd_bus_message_enter_container(message, 'a', "(sv)"); if (r < 0) goto fail; - m->create_message = dbus_message_ref(message); + r = machine_start(m, message, error); + if (r < 0) + goto fail; - return 0; + m->create_message = sd_bus_message_ref(message); + return 1; fail: - if (m) - machine_add_to_gc_queue(m); - + machine_add_to_gc_queue(m); return r; } -static DBusHandlerResult manager_message_handler( - DBusConnection *connection, - DBusMessage *message, - void *userdata) { +static int method_create_machine_with_network(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + return method_create_machine_internal(bus, message, true, userdata, error); +} - Manager *m = userdata; +static int method_create_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + return method_create_machine_internal(bus, message, false, userdata, error); +} - DBusError error; - _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; +static int method_register_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) { + Manager *manager = userdata; + _cleanup_free_ char *p = NULL; + Machine *m = NULL; int r; - assert(connection); - assert(message); - assert(m); + r = method_create_or_register_machine(manager, message, read_network, &m, error); + if (r < 0) + return r; - dbus_error_init(&error); + r = cg_pid_get_unit(m->leader, &m->unit); + if (r < 0) { + r = sd_bus_error_set_errnof(error, r, "Failed to determine unit of process "PID_FMT" : %s", m->leader, strerror(-r)); + goto fail; + } - if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "GetMachine")) { - Machine *machine; - const char *name; - char *p; - bool b; + m->registered = true; - if (!dbus_message_get_args( - message, - &error, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_INVALID)) - return bus_send_error_reply(connection, message, &error, -EINVAL); + r = machine_start(m, NULL, error); + if (r < 0) + goto fail; - machine = hashmap_get(m->machines, name); - if (!machine) - return bus_send_error_reply(connection, message, &error, -ENOENT); + p = machine_bus_path(m); + if (!p) { + r = -ENOMEM; + goto fail; + } - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; + return sd_bus_reply_method_return(message, "o", p); - 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; +fail: + machine_add_to_gc_queue(m); + return r; +} - p = machine_bus_path(machine); - if (!p) - goto oom; +static int method_register_machine_with_network(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + return method_register_machine_internal(bus, message, true, userdata, error); +} - b = dbus_message_append_args( - reply, - DBUS_TYPE_OBJECT_PATH, &p, - DBUS_TYPE_INVALID); - free(p); +static int method_register_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + return method_register_machine_internal(bus, message, false, userdata, error); +} - if (!b) - goto oom; +static int method_terminate_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = userdata; + Machine *machine; + const char *name; + int r; - } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "ListMachines")) { - Machine *machine; - Iterator i; - DBusMessageIter iter, sub; + assert(bus); + assert(message); + assert(m); - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_error_set_errno(error, r); - dbus_message_iter_init_append(reply, &iter); + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); - if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssso)", &sub)) - goto oom; + return bus_machine_method_terminate(bus, message, machine, error); +} - HASHMAP_FOREACH(machine, m->machines, i) { - _cleanup_free_ char *p = NULL; - DBusMessageIter sub2; - const char *class; +static int method_kill_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = userdata; + Machine *machine; + const char *name; + int r; - if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2)) - goto oom; + assert(bus); + assert(message); + assert(m); - p = machine_bus_path(machine); - if (!p) - goto oom; + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_error_set_errno(error, r); - class = strempty(machine_class_to_string(machine->class)); + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); - 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; - } + return bus_machine_method_kill(bus, message, machine, error); +} - if (!dbus_message_iter_close_container(&sub, &sub2)) - goto oom; - } +static int method_get_machine_addresses(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = userdata; + Machine *machine; + const char *name; + int r; - if (!dbus_message_iter_close_container(&iter, &sub)) - goto oom; + assert(bus); + assert(message); + assert(m); - } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "CreateMachine")) { + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_error_set_errno(error, r); - r = bus_manager_create_machine(m, message); - if (r < 0) - return bus_send_error_reply(connection, message, NULL, r); - - } 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; - - 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); - } + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); - if (signo <= 0 || signo >= _NSIG) - return bus_send_error_reply(connection, message, &error, -EINVAL); + return bus_machine_method_get_addresses(bus, message, machine, error); +} - machine = hashmap_get(m->machines, name); - if (!machine) - return bus_send_error_reply(connection, message, &error, -ENOENT); +static int method_get_machine_os_release(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = userdata; + Machine *machine; + const char *name; + int r; - r = machine_kill(machine, who, signo); - if (r < 0) - return bus_send_error_reply(connection, message, NULL, r); + assert(bus); + assert(message); + assert(m); - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_error_set_errno(error, r); - } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "TerminateMachine")) { - const char *name; - Machine *machine; + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); - if (!dbus_message_get_args( - message, - &error, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_INVALID)) - return bus_send_error_reply(connection, message, &error, -EINVAL); + return bus_machine_method_get_os_release(bus, message, machine, error); +} - machine = hashmap_get(m->machines, name); - if (!machine) - return bus_send_error_reply(connection, message, &error, -ENOENT); +static int method_list_images(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + _cleanup_(image_hashmap_freep) Hashmap *images = NULL; + Manager *m = userdata; + Image *image; + Iterator i; + int r; - r = machine_stop(machine); - if (r < 0) - return bus_send_error_reply(connection, message, NULL, r); + assert(bus); + assert(message); + assert(m); - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; + images = hashmap_new(&string_hash_ops); + if (!images) + return -ENOMEM; - } 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; - char *p; + r = image_discover(images); + if (r < 0) + return r; - reply = dbus_message_new_method_return(message); - if (!reply) - goto oom; + r = sd_bus_message_new_method_return(message, &reply); + if (r < 0) + return r; - /* We roll our own introspection code here, instead of - * relying on bus_default_message_handler() because we - * need to generate our introspection string - * dynamically. */ + r = sd_bus_message_open_container(reply, 'a', "(ssbtto)"); + if (r < 0) + return r; - f = open_memstream(&introspection, &size); - if (!f) - goto oom; + HASHMAP_FOREACH(image, images, i) { + _cleanup_free_ char *p = NULL; - fputs(INTROSPECTION_BEGIN, f); + p = image_bus_path(image->name); + if (!p) + return -ENOMEM; - HASHMAP_FOREACH(machine, m->machines, i) { - p = bus_path_escape(machine->name); + r = sd_bus_message_append(reply, "(ssbtto)", + image->name, + image_type_to_string(image->type), + image->read_only, + image->crtime, + image->mtime, + p); + if (r < 0) + return r; + } - if (p) { - fprintf(f, "", p); - free(p); - } - } + r = sd_bus_message_close_container(reply); + if (r < 0) + return r; - fputs(INTROSPECTION_END, f); + return sd_bus_send(bus, reply, NULL); +} - if (ferror(f)) { - fclose(f); - free(introspection); - goto oom; - } +static int method_open_machine_pty(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = userdata; + Machine *machine; + const char *name; + int r; - fclose(f); + assert(bus); + assert(message); + assert(m); - if (!introspection) - goto oom; + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_error_set_errno(error, r); - if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) { - free(introspection); - goto oom; - } + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); - free(introspection); - } else - return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, NULL); + return bus_machine_method_open_pty(bus, message, machine, error); +} - if (reply) { - if (!bus_maybe_send_reply(connection, message, reply)) - goto oom; - } +static int method_open_machine_login(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = userdata; + Machine *machine; + const char *name; + int r; + + assert(bus); + assert(message); + assert(m); - return DBUS_HANDLER_RESULT_HANDLED; + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return sd_bus_error_set_errno(error, r); -oom: - dbus_error_free(&error); + machine = hashmap_get(m->machines, name); + if (!machine) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); - return DBUS_HANDLER_RESULT_NEED_MEMORY; + return bus_machine_method_open_login(bus, message, machine, error); } -const DBusObjectPathVTable bus_manager_vtable = { - .message_function = manager_message_handler +const sd_bus_vtable manager_vtable[] = { + SD_BUS_VTABLE_START(0), + SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ListImages", NULL, "a(ssbtto)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0), + SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0), + SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0), + SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0), + SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)), + SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)), + SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty, 0), + SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_SIGNAL("MachineNew", "so", 0), + SD_BUS_SIGNAL("MachineRemoved", "so", 0), + SD_BUS_VTABLE_END }; -DBusHandlerResult bus_message_filter( - DBusConnection *connection, - DBusMessage *message, - void *userdata) { - +int match_job_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + 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); + r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); + if (r < 0) { + bus_log_parse_error(r); + return r; + } - 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))); + machine = hashmap_get(m->machine_units, unit); + if (!machine) + return 0; - if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) { - const char *path, *result, *unit; - Machine *mm; - uint32_t id; + if (streq_ptr(path, machine->scope_job)) { + free(machine->scope_job); + machine->scope_job = NULL; - 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; - } + if (machine->started) { + if (streq(result, "done")) + machine_send_create_reply(machine, NULL); + else { + _cleanup_bus_error_free_ sd_bus_error e = SD_BUS_ERROR_NULL; + sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); - 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); - } - } + machine_send_create_reply(machine, &e); } + } else + machine_save(machine); + } - machine_add_to_gc_queue(mm); - } + machine_add_to_gc_queue(machine); + return 0; +} - } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) { +int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_free_ char *unit = NULL; + Manager *m = userdata; + Machine *machine; + const char *path; + int r; - _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; - _cleanup_free_ char *unit = NULL; - const char *path; + assert(bus); + assert(message); + assert(m); - path = dbus_message_get_path(message); - if (!path) - goto finish; + path = sd_bus_message_get_path(message); + if (!path) + return 0; - unit_name_from_dbus_path(path, &unit); - if (unit) { - Machine *mm; + r = unit_name_from_dbus_path(path, &unit); + if (r < 0) + return r; - mm = hashmap_get(m->machine_units, unit); - if (mm) - machine_add_to_gc_queue(mm); - } + machine = hashmap_get(m->machine_units, unit); + if (machine) + machine_add_to_gc_queue(machine); + + return 0; +} + +int match_unit_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + const char *path, *unit; + Manager *m = userdata; + Machine *machine; + int r; + + assert(bus); + assert(message); + assert(m); + + r = sd_bus_message_read(message, "so", &unit, &path); + if (r < 0) { + bus_log_parse_error(r); + return r; + } + + machine = hashmap_get(m->machine_units, unit); + if (machine) + machine_add_to_gc_queue(machine); + + return 0; +} + +int match_reloading(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = userdata; + Machine *machine; + Iterator i; + int b, r; + + assert(bus); + + r = sd_bus_message_read(message, "b", &b); + if (r < 0) { + bus_log_parse_error(r); + return r; } + if (b) + return 0; + + /* systemd finished reloading, let's recheck all our machines */ + log_debug("System manager has been reloaded, rechecking machines..."); -finish: - dbus_error_free(&error); + HASHMAP_FOREACH(machine, m->machines, i) + machine_add_to_gc_queue(machine); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + return 0; } int manager_start_scope( @@ -589,98 +705,80 @@ int manager_start_scope( pid_t pid, const char *slice, const char *description, - 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, + &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartTransientUnit"); - if (!m) - return log_oom(); + if (r < 0) + return r; - dbus_message_iter_init_append(m, &iter); + r = sd_bus_message_append(m, "ss", strempty(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 - * forever. */ - - 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) || - !dbus_message_iter_close_container(&iter, &sub)) - return log_oom(); - - reply = dbus_connection_send_with_reply_and_block(manager->bus, m, -1, error); - if (!reply) - return -EIO; + r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); + if (r < 0) + return r; + + r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1); + if (r < 0) + return r; + + if (more_properties) { + r = sd_bus_message_copy(m, more_properties, true); + if (r < 0) + return r; + } + + r = sd_bus_message_close_container(m); + if (r < 0) + return r; + + r = sd_bus_message_append(m, "a(sa(sv))", 0); + if (r < 0) + return r; + + r = sd_bus_call(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) @@ -689,30 +787,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) { - log_error("Failed to stop unit %s: %s", unit, bus_error(error, r)); + 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; + + sd_bus_error_free(error); + return 0; + } + return r; } @@ -720,12 +824,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) @@ -734,91 +835,137 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char *job = copy; } - return 0; + 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 r; - +int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) { assert(manager); assert(unit); - w = who == KILL_LEADER ? "process" : "cgroup"; - assert_cc(sizeof(signo) == sizeof(int32_t)); - - r = bus_method_call_with_reply( + return 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; - } - - return 0; + NULL, + "ssi", unit, "all", signo); } 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) { - log_error("Failed to query ActiveState: %s", bus_error(&error, r)); - 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 (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || + sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) + return false; + 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; + + return !streq(state, "inactive") && !streq(state, "failed"); +} + +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; + + assert(manager); + assert(path); + + 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; + + if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) + return false; + + return r; } - 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; + /* We don't actually care about the state really. The fact + * that we could read the job state is enough for us */ + + return true; +} + +int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) { + _cleanup_free_ char *unit = NULL; + Machine *mm; + int r; + + assert(m); + assert(pid >= 1); + assert(machine); + + r = cg_pid_get_unit(pid, &unit); + if (r < 0) + mm = hashmap_get(m->machine_leaders, UINT_TO_PTR(pid)); + else + mm = hashmap_get(m->machine_units, unit); + + if (!mm) + return 0; + + *machine = mm; + return 1; +} + +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; } - dbus_message_iter_get_basic(&sub, &state); + if (_machine) + *_machine = machine; - return !streq(state, "inactive") && !streq(state, "failed"); + return 0; }