machined-dbus.c revision c19de71113f956809995fc68817e055e9f61f607
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen This file is part of systemd.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Copyright 2011 Lennart Poettering
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen systemd is free software; you can redistribute it and/or modify it
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen under the terms of the GNU Lesser General Public License as published by
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen (at your option) any later version.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen systemd is distributed in the hope that it will be useful, but
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Lesser General Public License for more details.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen You should have received a copy of the GNU Lesser General Public License
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic int method_get_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_reply_method_return(message, "o", p);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic int method_get_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_reply_method_return(message, "o", p);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic int method_get_machine_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = manager_get_machine_by_pid(m, pid, &machine);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID, "PID "PID_FMT" does not belong to any known machine", pid);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_reply_method_return(message, "o", p);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic int method_list_machines(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = sd_bus_message_new_method_return(message, &reply);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = sd_bus_message_open_container(reply, 'a', "(ssso)");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen strempty(machine_class_to_string(machine->class)),
be19c5b5e0c0f78b8429b126936fa15856550a23David Herrmannstatic int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool read_network, Machine **_m, sd_bus_error *error) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const char *name, *service, *class, *root_directory;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const void *v;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = sd_bus_message_read_array(message, 'y', &v, &n);
e0ee46f29028e291eb67f435aff1b6202d75d9d6Lennart Poettering else if (n == 16)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen for (i = 0; i < n_netif; i++) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path");
if (leader == 0) {
m->class = c;
if (!m->service) {
r = -ENOMEM;
goto fail;
if (!m->root_directory) {
r = -ENOMEM;
goto fail;
if (n_netif > 0) {
if (!m->netif) {
r = -ENOMEM;
goto fail;
*_m = m;
fail:
static int method_create_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
goto fail;
goto fail;
fail:
static int method_create_machine_with_network(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int method_create_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int method_register_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
r = sd_bus_error_set_errnof(error, r, "Failed to determine unit of process "PID_FMT" : %s", m->leader, strerror(-r));
goto fail;
goto fail;
p = machine_bus_path(m);
r = -ENOMEM;
goto fail;
fail:
static int method_register_machine_with_network(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int method_register_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int method_terminate_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
assert(m);
if (!machine)
static int method_kill_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
assert(m);
if (!machine)
static int method_get_machine_addresses(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
assert(m);
if (!machine)
static int method_get_machine_os_release(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
assert(m);
if (!machine)
static int method_list_images(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Iterator i;
assert(m);
if (!images)
return -ENOMEM;
return -ENOMEM;
static int method_open_machine_pty(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
assert(m);
if (!machine)
static int method_open_machine_login(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
assert(m);
if (!machine)
static int method_remove_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
static int method_rename_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *old_name;
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
static int method_clone_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *old_name;
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
static int method_mark_image_read_only(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0),
SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0),
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("OpenMachineLogin", "s", "hs", method_open_machine_login, SD_BUS_VTABLE_UNPRIVILEGED),
assert(m);
if (!machine)
sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *path;
assert(m);
if (!path)
if (machine)
assert(m);
if (machine)
Iterator i;
int manager_start_scope(
const char *scope,
const char *slice,
const char *description,
char **job) {
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
if (more_properties) {
r = sd_bus_message_close_container(m);
if (job) {
char *copy;
if (!copy)
return -ENOMEM;
r = sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&reply,
if (job)
if (job) {
char *copy;
if (!copy)
return -ENOMEM;
return sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
NULL,
const char *state;
if (!path)
return -ENOMEM;
r = sd_bus_get_property(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
&error,
&reply,
return -EINVAL;
r = sd_bus_get_property(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Job",
&error,
&reply,
assert(m);
if (!mm)
assert(m);
if (!machine) {
if (!machine)
return -ENOMEM;
if (_machine)