machined-dbus.c revision 86b8d289717bad2800342efca0a5023aa8374e9c
/*-*- 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-errors.h"
#include "time-util.h"
#include "cgroup-util.h"
#include "machined.h"
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;
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;
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_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 (!machine)
return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID, "PID %lu does not belong to any known machine", (unsigned long) 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_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
MachineClass c;
const void *v;
Machine *m;
size_t n;
int r;
if (r < 0)
return r;
if (!valid_machine_name(name))
if (r < 0)
return r;
if (n == 0)
id = SD_ID128_NULL;
else if (n == 16)
else
if (r < 0)
return r;
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 (r < 0)
return r;
if (leader == 0) {
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 (r < 0)
goto fail;
return 1;
fail:
return r;
}
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)
r = machine_stop(machine);
if (r < 0)
return sd_bus_error_set_errno(error, r);
}
static int method_kill_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
const char *swho;
int r;
assert(m);
if (r < 0)
return sd_bus_error_set_errno(error, r);
else {
if (who < 0)
}
if (!machine)
if (r < 0)
return sd_bus_error_set_errno(error, r);
}
const sd_bus_vtable manager_vtable[] = {
};
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;
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartTransientUnit",
&m);
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;
}
/* cgroup empty notification is not available in containers
* currently. To make this less problematic, let's shorten the
* stop timeout for machines, so that we don't wait
* forever. */
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;
}
int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
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;
}