machined-dbus.c revision 190700621f95160d364f8ec1d3e360246c41ce75
/*-*- 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 "sd-id128.h"
#include "strv.h"
#include "path-util.h"
#include "unit-name.h"
#include "bus-util.h"
#include "bus-common-errors.h"
#include "cgroup-util.h"
#include "btrfs-util.h"
#include "machine-image.h"
#include "machine-pool.h"
#include "image-dbus.h"
#include "machined.h"
#include "machine-dbus.h"
#include "formats-util.h"
static int property_get_pool_path(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
}
static int property_get_pool_usage(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
* well as the usage of the loopback file
* /var/lib/machines.raw, and pick the larger value. */
if (fd >= 0) {
if (btrfs_subvol_get_quota_fd(fd, &q) >= 0)
usage = q.referenced;
}
}
}
static int property_get_pool_limit(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
* well as the size of the loopback file
* /var/lib/machines.raw, and pick the smaller value. */
if (fd >= 0) {
if (btrfs_subvol_get_quota_fd(fd, &q) >= 0)
size = q.referenced_max;
}
}
}
_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;
}
_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_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;
}
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_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_message *message, void *userdata, sd_bus_error *error) {
}
}
static int method_register_machine_internal(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;
}
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_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)
}
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_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_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)
}
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;
}
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_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_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
int r;
assert(m);
if (r < 0)
return r;
if (!machine)
}
const char *name;
int r;
assert(m);
if (r < 0)
return r;
if (!machine)
}
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)
}
const char *old_name;
int r;
if (r < 0)
return r;
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);
r = image_find(old_name, &i);
if (r < 0)
return r;
if (r == 0)
}
const char *old_name;
int r;
if (r < 0)
return r;
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);
r = image_find(old_name, &i);
if (r < 0)
return r;
if (r == 0)
}
static int method_mark_image_read_only(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)
}
int r;
if (r < 0)
return r;
"org.freedesktop.machine1.manage-machines",
false,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* Will call us back */
/* Set up the machine directory if necessary */
if (r < 0)
return r;
if (r == -ENOTTY)
if (r < 0 && r != -ENODEV) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */
if (r == -ENOTTY)
if (r < 0)
}
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)
}
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("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),
SD_BUS_METHOD("BindMountMachine", "sssbb", NULL, method_bind_mount_machine, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL, method_mark_image_read_only, 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 r;
assert(m);
if (!path)
return 0;
if (r == -EINVAL) /* not for a unit */
return 0;
if (r < 0){
log_oom();
return 0;
}
if (!machine)
return 0;
if (r < 0) {
return 0;
}
struct properties {
char *active_state;
char *sub_state;
} properties = {};
const struct bus_properties_map map[] = {
{}
};
if (r < 0)
}
return 0;
}
int r;
assert(m);
if (r < 0) {
return 0;
}
if (!machine)
return 0;
return 0;
}
Iterator i;
int b, r;
assert(m);
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;
}