#include <unistd.h>
#include <pwd.h>
-#include <systemd/sd-id128.h>
-#include <systemd/sd-messages.h>
-
-#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 \
- " <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"
-
-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;
- const char *name, *service, *class, *root_directory;
+ 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);
+}
+
+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;
+
+ n_netif /= sizeof(int32_t);
- dbus_message_iter_get_basic(&iter, &class);
+ 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, &root_directory);
+ if (leader == 0) {
+ _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
- if (!(isempty(root_directory) || path_is_absolute(root_directory)))
- return -EINVAL;
+ r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
+ if (r < 0)
+ return r;
- 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;
+ assert_cc(sizeof(uint32_t) == sizeof(pid_t));
- dbus_message_iter_recurse(&iter, &sub);
+ r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
+ if (r < 0)
+ return r;
+ }
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_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;
}
}
- r = machine_start(m, &sub);
- if (r < 0)
- goto fail;
+ 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->create_message = dbus_message_ref(message);
+ m->n_netif = n_netif;
+ }
- return 0;
+ *_m = m;
-fail:
- if (m)
- machine_add_to_gc_queue(m);
+ return 1;
+fail:
+ machine_add_to_gc_queue(m);
return r;
}
-static DBusHandlerResult manager_message_handler(
- DBusConnection *connection,
- DBusMessage *message,
- void *userdata) {
-
- Manager *m = userdata;
-
- DBusError error;
- _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
+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;
- assert(connection);
- assert(message);
- assert(m);
-
- dbus_error_init(&error);
+ r = method_create_or_register_machine(manager, message, read_network, &m, error);
+ if (r < 0)
+ return r;
- if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "GetMachine")) {
- Machine *machine;
- const char *name;
- char *p;
- bool b;
+ r = sd_bus_message_enter_container(message, 'a', "(sv)");
+ if (r < 0)
+ goto fail;
- 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, message, error);
+ if (r < 0)
+ goto fail;
- machine = hashmap_get(m->machines, name);
- if (!machine)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
+ m->create_message = sd_bus_message_ref(message);
+ return 1;
- 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;
-
- 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;
+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);
+}
- p = machine_bus_path(machine);
- if (!p)
- goto oom;
+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);
+}
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
+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;
- if (!b)
- goto oom;
+ r = method_create_or_register_machine(manager, message, read_network, &m, error);
+ if (r < 0)
+ return r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "ListMachines")) {
- Machine *machine;
- Iterator i;
- DBusMessageIter iter, sub;
+ 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;
+ }
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ r = machine_start(m, NULL, error);
+ if (r < 0)
+ goto fail;
- dbus_message_iter_init_append(reply, &iter);
+ p = machine_bus_path(m);
+ if (!p) {
+ r = -ENOMEM;
+ goto fail;
+ }
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssso)", &sub))
- goto oom;
+ return sd_bus_reply_method_return(message, "o", p);
- HASHMAP_FOREACH(machine, m->machines, i) {
- _cleanup_free_ char *p = NULL;
- DBusMessageIter sub2;
- const char *class;
+fail:
+ machine_add_to_gc_queue(m);
+ return r;
+}
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- 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);
+}
- p = machine_bus_path(machine);
- if (!p)
- goto oom;
+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);
+}
- class = strempty(machine_class_to_string(machine->class));
+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;
- 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;
- }
+ assert(bus);
+ assert(message);
+ assert(m);
- if (!dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return sd_bus_error_set_errno(error, r);
- if (!dbus_message_iter_close_container(&iter, &sub))
- 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);
- } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "CreateMachine")) {
+ return bus_machine_method_terminate(bus, message, machine, error);
+}
- 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);
- }
+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 (signo <= 0 || signo >= _NSIG)
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+ assert(bus);
+ assert(message);
+ assert(m);
- machine = hashmap_get(m->machines, name);
- if (!machine)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return sd_bus_error_set_errno(error, r);
- r = machine_kill(machine, who, signo);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, 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);
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ return bus_machine_method_kill(bus, message, machine, error);
+}
- } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "TerminateMachine")) {
- const char *name;
- Machine *machine;
+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_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+ assert(bus);
+ assert(message);
+ assert(m);
- machine = hashmap_get(m->machines, name);
- if (!machine)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return sd_bus_error_set_errno(error, r);
- r = machine_stop(machine);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, 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);
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ return bus_machine_method_get_addresses(bus, message, machine, error);
+}
- } 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;
+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;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ assert(bus);
+ assert(message);
+ assert(m);
- /* 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_read(message, "s", &name);
+ if (r < 0)
+ return sd_bus_error_set_errno(error, r);
- f = open_memstream(&introspection, &size);
- if (!f)
- 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);
- fputs(INTROSPECTION_BEGIN, f);
+ return bus_machine_method_get_os_release(bus, message, machine, error);
+}
- HASHMAP_FOREACH(machine, m->machines, i) {
- p = bus_path_escape(machine->name);
+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;
- if (p) {
- fprintf(f, "<node name=\"machine/%s\"/>", p);
- free(p);
- }
- }
+ assert(bus);
+ assert(message);
+ assert(m);
- fputs(INTROSPECTION_END, f);
+ images = hashmap_new(&string_hash_ops);
+ if (!images)
+ return -ENOMEM;
- if (ferror(f)) {
- fclose(f);
- free(introspection);
- goto oom;
- }
+ r = image_discover(images);
+ if (r < 0)
+ return r;
- fclose(f);
+ r = sd_bus_message_new_method_return(message, &reply);
+ if (r < 0)
+ return r;
- if (!introspection)
- goto oom;
+ r = sd_bus_message_open_container(reply, 'a', "(ssbttto)");
+ if (r < 0)
+ return r;
- if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
- free(introspection);
- goto oom;
- }
+ HASHMAP_FOREACH(image, images, i) {
+ _cleanup_free_ char *p = NULL;
- free(introspection);
- } else
- return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, NULL);
+ p = image_bus_path(image->name);
+ if (!p)
+ return -ENOMEM;
- if (reply) {
- if (!bus_maybe_send_reply(connection, message, reply))
- goto oom;
+ r = sd_bus_message_append(reply, "(ssbttto)",
+ image->name,
+ image_type_to_string(image->type),
+ image->read_only,
+ image->crtime,
+ image->mtime,
+ image->usage,
+ p);
+ if (r < 0)
+ return r;
}
- return DBUS_HANDLER_RESULT_HANDLED;
-
-oom:
- dbus_error_free(&error);
+ r = sd_bus_message_close_container(reply);
+ if (r < 0)
+ return r;
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
+ return sd_bus_send(bus, reply, NULL);
}
-const DBusObjectPathVTable bus_manager_vtable = {
- .message_function = manager_message_handler
-};
+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;
-DBusHandlerResult bus_message_filter(
- DBusConnection *connection,
- DBusMessage *message,
- void *userdata) {
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return sd_bus_error_set_errno(error, 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);
+
+ return bus_machine_method_open_pty(bus, message, machine, error);
+}
+static int method_open_machine_login(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
- DBusError error;
+ Machine *machine;
+ const char *name;
+ 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)));
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
- const char *path, *result, *unit;
- Machine *mm;
- uint32_t id;
+ 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_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;
- }
+ return bus_machine_method_open_login(bus, message, machine, error);
+}
- 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);
- }
+static int method_remove_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_(image_unrefp) Image* i = NULL;
+ const char *name;
+ int r;
- machine_add_to_gc_queue(mm);
- }
+ assert(bus);
+ assert(message);
- } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
- _cleanup_free_ char *unit = NULL;
- const char *path;
+ if (!image_name_is_valid(name))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
- path = dbus_message_get_path(message);
- if (!path)
- goto finish;
+ r = image_find(name, &i);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
- unit_name_from_dbus_path(path, &unit);
- if (unit) {
- Machine *mm;
+ return bus_image_method_remove(bus, message, i, error);
+}
- mm = hashmap_get(m->machine_units, unit);
- if (mm)
- machine_add_to_gc_queue(mm);
- }
+static int method_rename_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_(image_unrefp) Image* i = NULL;
+ const char *old_name;
+ int r;
- } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
- const char *path, *unit;
- Machine *mm;
+ assert(bus);
+ assert(message);
- 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;
- }
+ r = sd_bus_message_read(message, "s", &old_name);
+ if (r < 0)
+ return r;
- mm = hashmap_get(m->machine_units, unit);
- if (mm)
- machine_add_to_gc_queue(mm);
+ if (!image_name_is_valid(old_name))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
- } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "Reloading")) {
- dbus_bool_t b;
+ r = image_find(old_name, &i);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", old_name);
- 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;
- }
+ return bus_image_method_rename(bus, message, i, error);
+}
- /* systemd finished reloading, let's recheck all our machines */
- if (!b) {
- Machine *mm;
- Iterator i;
+static int method_clone_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_(image_unrefp) Image *i = NULL;
+ const char *old_name;
+ int r;
- log_debug("System manager has been reloaded, rechecking machines...");
+ assert(bus);
+ r = sd_bus_message_read(message, "s", &old_name);
+ if (r < 0)
+ return r;
- HASHMAP_FOREACH(mm, m->machines, i)
- machine_add_to_gc_queue(mm);
- }
- }
+ if (!image_name_is_valid(old_name))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
-finish:
- dbus_error_free(&error);
+ r = image_find(old_name, &i);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", old_name);
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ return bus_image_method_clone(bus, message, i, error);
}
-static int copy_many_fields(DBusMessageIter *dest, DBusMessageIter *src);
+static int method_mark_image_read_only(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_(image_unrefp) Image *i = NULL;
+ const char *name;
+ int r;
-static int copy_one_field(DBusMessageIter *dest, DBusMessageIter *src) {
- int type, r;
+ assert(bus);
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- type = dbus_message_iter_get_arg_type(src);
+ if (!image_name_is_valid(name))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
- switch (type) {
+ r = image_find(name, &i);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
- case DBUS_TYPE_STRUCT: {
- DBusMessageIter dest_sub, src_sub;
+ return bus_image_method_mark_read_only(bus, message, i, error);
+}
- dbus_message_iter_recurse(src, &src_sub);
+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(ssbttto)", 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_METHOD("RemoveImage", "s", NULL, method_remove_image, 0),
+ SD_BUS_METHOD("RenameImage", "ss", NULL, method_rename_image, 0),
+ SD_BUS_METHOD("CloneImage", "ssb", NULL, method_clone_image, 0),
+ SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL, method_mark_image_read_only, 0),
+ SD_BUS_SIGNAL("MachineNew", "so", 0),
+ SD_BUS_SIGNAL("MachineRemoved", "so", 0),
+ SD_BUS_VTABLE_END
+};
- if (!dbus_message_iter_open_container(dest, DBUS_TYPE_STRUCT, NULL, &dest_sub))
- return log_oom();
+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;
+ Machine *machine;
+ uint32_t id;
+ int r;
- r = copy_many_fields(&dest_sub, &src_sub);
- if (r < 0)
- return r;
+ assert(bus);
+ assert(message);
+ assert(m);
- if (!dbus_message_iter_close_container(dest, &dest_sub))
- return log_oom();
+ r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
+ if (r < 0) {
+ bus_log_parse_error(r);
+ return r;
+ }
+ machine = hashmap_get(m->machine_units, unit);
+ if (!machine)
return 0;
- }
- case DBUS_TYPE_ARRAY: {
- DBusMessageIter dest_sub, src_sub;
+ if (streq_ptr(path, machine->scope_job)) {
+ free(machine->scope_job);
+ machine->scope_job = NULL;
- dbus_message_iter_recurse(src, &src_sub);
+ 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;
- if (!dbus_message_iter_open_container(dest, DBUS_TYPE_ARRAY, dbus_message_iter_get_signature(&src_sub), &dest_sub))
- return log_oom();
+ sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
- r = copy_many_fields(&dest_sub, &src_sub);
- if (r < 0)
- return r;
+ machine_send_create_reply(machine, &e);
+ }
+ } else
+ machine_save(machine);
+ }
+
+ machine_add_to_gc_queue(machine);
+ return 0;
+}
- if (!dbus_message_iter_close_container(dest, &dest_sub))
- return log_oom();
+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;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+ path = sd_bus_message_get_path(message);
+ if (!path)
return 0;
- }
- case DBUS_TYPE_VARIANT: {
- DBusMessageIter dest_sub, src_sub;
+ r = unit_name_from_dbus_path(path, &unit);
+ if (r == -EINVAL) /* not for a unit */
+ return 0;
+ if (r < 0)
+ return r;
- 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();
+ return 0;
+}
- r = copy_one_field(&dest_sub, &src_sub);
- if (r < 0)
- return r;
+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;
- if (!dbus_message_iter_close_container(dest, &dest_sub))
- return log_oom();
+ assert(bus);
+ assert(message);
+ assert(m);
- return 0;
+ r = sd_bus_message_read(message, "so", &unit, &path);
+ if (r < 0) {
+ bus_log_parse_error(r);
+ return r;
}
- 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);
- return 0;
- }
+ machine = hashmap_get(m->machine_units, unit);
+ if (machine)
+ machine_add_to_gc_queue(machine);
- default:
- return -EINVAL;
- }
+ return 0;
}
-static int copy_many_fields(DBusMessageIter *dest, DBusMessageIter *src) {
- int r;
+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(dest);
- assert(src);
+ assert(bus);
- while (dbus_message_iter_get_arg_type(src) != DBUS_TYPE_INVALID) {
+ r = sd_bus_message_read(message, "b", &b);
+ if (r < 0) {
+ bus_log_parse_error(r);
+ return r;
+ }
+ if (b)
+ return 0;
- r = copy_one_field(dest, src);
- if (r < 0)
- return r;
+ /* systemd finished reloading, let's recheck all our machines */
+ 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;
}
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,
+ &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))
- return log_oom();
+ 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 = 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_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)
*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;
}
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)
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) {
- 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;
+
+ 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;
}