machined-dbus.c revision 086821244b5113f00a0ef993b78dc56aae2a8f6c
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include "sd-id128.h"
#include "sd-messages.h"
#include "strv.h"
#include "mkdir.h"
#include "path-util.h"
#include "special.h"
#include "fileio-label.h"
#include "label.h"
#include "utf8.h"
#include "unit-name.h"
#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"
static int method_get_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
const char *name;
int r;
assert(m);
if (r < 0)
return r;
if (!machine)
p = machine_bus_path(machine);
if (!p)
return -ENOMEM;
}
static int method_get_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
const char *name;
int r;
assert(m);
if (r < 0)
return r;
if (r == 0)
if (r < 0)
return r;
p = image_bus_path(name);
if (!p)
return -ENOMEM;
}
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;
int r;
assert(m);
if (r < 0)
return r;
if (pid == 0) {
if (r < 0)
return r;
if (r < 0)
return r;
}
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;
}
static int method_list_machines(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Iterator i;
int r;
assert(m);
if (r < 0)
return sd_bus_error_set_errno(error, r);
if (r < 0)
return sd_bus_error_set_errno(error, r);
_cleanup_free_ char *p = NULL;
p = machine_bus_path(machine);
if (!p)
return -ENOMEM;
p);
if (r < 0)
return sd_bus_error_set_errno(error, r);
}
if (r < 0)
return sd_bus_error_set_errno(error, r);
}
static int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool read_network, Machine **_m, sd_bus_error *error) {
MachineClass c;
const void *v;
Machine *m;
int r;
if (r < 0)
return r;
if (!machine_name_is_valid(name))
if (r < 0)
return r;
if (n == 0)
id = SD_ID128_NULL;
else if (n == 16)
else
if (r < 0)
return r;
if (read_network) {
size_t i;
if (r < 0)
return r;
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]);
}
}
else {
if (c < 0)
}
if (leader == 1)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path");
if (leader == 0) {
if (r < 0)
return r;
if (r < 0)
return r;
}
if (r < 0)
return r;
m->class = c;
if (!m->service) {
r = -ENOMEM;
goto fail;
}
}
if (!isempty(root_directory)) {
if (!m->root_directory) {
r = -ENOMEM;
goto fail;
}
}
if (n_netif > 0) {
if (!m->netif) {
r = -ENOMEM;
goto fail;
}
}
*_m = m;
return 1;
fail:
return r;
}
static int method_create_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
int r;
if (r < 0)
return r;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
return 1;
fail:
return r;
}
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) {
_cleanup_free_ char *p = NULL;
int r;
if (r < 0)
return r;
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;
}
m->registered = true;
if (r < 0)
goto fail;
p = machine_bus_path(m);
if (!p) {
r = -ENOMEM;
goto fail;
}
fail:
return r;
}
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;
int r;
assert(m);
if (r < 0)
return sd_bus_error_set_errno(error, r);
if (!machine)
}
static int method_kill_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
int r;
assert(m);
if (r < 0)
return sd_bus_error_set_errno(error, r);
if (!machine)
}
static int method_get_machine_addresses(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
int r;
assert(m);
if (r < 0)
return sd_bus_error_set_errno(error, r);
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;
int r;
assert(m);
if (r < 0)
return sd_bus_error_set_errno(error, r);
if (!machine)
}
static int method_list_images(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Iterator i;
int r;
assert(m);
if (!images)
return -ENOMEM;
r = image_discover(images);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
_cleanup_free_ char *p = NULL;
if (!p)
return -ENOMEM;
p);
if (r < 0)
return r;
}
if (r < 0)
return r;
}
static int method_open_machine_pty(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
int r;
assert(m);
if (r < 0)
return sd_bus_error_set_errno(error, r);
if (!machine)
}
static int method_open_machine_login(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
int r;
assert(m);
if (r < 0)
return r;
if (!machine)
}
static int method_remove_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
int r;
if (r < 0)
return r;
if (!image_name_is_valid(name))
r = image_find(name, &i);
if (r < 0)
return r;
if (r == 0)
r = image_remove(i);
if (r < 0)
return r;
}
const sd_bus_vtable manager_vtable[] = {
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),
};
int r;
assert(m);
if (r < 0) {
return r;
}
if (!machine)
return 0;
else {
sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
}
} else
}
return 0;
}
int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *path;
int r;
assert(m);
if (!path)
return 0;
if (r < 0)
return r;
if (machine)
return 0;
}
int r;
assert(m);
if (r < 0) {
return r;
}
if (machine)
return 0;
}
Iterator i;
int b, r;
if (r < 0) {
return r;
}
if (b)
return 0;
/* systemd finished reloading, let's recheck all our machines */
log_debug("System manager has been reloaded, rechecking machines...");
return 0;
}
int manager_start_scope(
const char *scope,
const char *slice,
const char *description,
char **job) {
int r;
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartTransientUnit");
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
}
if (!isempty(description)) {
if (r < 0)
return r;
}
if (r < 0)
return r;
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;
if (r < 0)
return r;
if (job) {
const char *j;
char *copy;
if (r < 0)
return r;
if (!copy)
return -ENOMEM;
}
return 1;
}
int r;
r = sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StopUnit",
&reply,
if (r < 0) {
if (job)
return 0;
}
return r;
}
if (job) {
const char *j;
char *copy;
if (r < 0)
return r;
if (!copy)
return -ENOMEM;
}
return 1;
}
return sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"KillUnit",
NULL,
}
const char *state;
int r;
if (!path)
return -ENOMEM;
r = sd_bus_get_property(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"ActiveState",
&error,
&reply,
"s");
if (r < 0) {
return true;
return false;
return r;
}
if (r < 0)
return -EINVAL;
}
int r;
r = sd_bus_get_property(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Job",
"State",
&error,
&reply,
"s");
if (r < 0) {
return true;
return false;
return r;
}
/* 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 r;
assert(m);
if (r < 0)
else
if (!mm)
return 0;
return 1;
}
assert(m);
if (!machine) {
if (!machine)
return -ENOMEM;
}
if (_machine)
return 0;
}