machine.c revision 9b420b3cfb8b93daf50e4cdbc92b05f2209ef893
/*-*- 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 <string.h>
#include <unistd.h>
#include <errno.h>
#include "sd-messages.h"
#include "util.h"
#include "mkdir.h"
#include "hashmap.h"
#include "fileio.h"
#include "special.h"
#include "unit-name.h"
#include "bus-util.h"
#include "bus-error.h"
#include "machine.h"
#include "machine-dbus.h"
#include "formats-util.h"
Machine *m;
if (!m)
return NULL;
if (!m->name)
goto fail;
if (!m->state_file)
goto fail;
goto fail;
m->class = _MACHINE_CLASS_INVALID;
return m;
fail:
free(m->state_file);
free(m);
return NULL;
}
void machine_free(Machine *m) {
assert(m);
while (m->operations)
if (m->in_gc_queue)
if (m->leader > 0)
free(m->state_file);
free(m->root_directory);
free(m);
}
int machine_save(Machine *m) {
int r;
assert(m);
assert(m->state_file);
if (!m->started)
return 0;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
fprintf(f,
"# This is private data. Do not parse.\n"
"NAME=%s\n",
m->name);
if (m->unit) {
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto finish;
}
fprintf(f, "SCOPE=%s\n", escaped); /* We continue to call this "SCOPE=" because it is internal only, and we want to stay compatible with old files */
}
if (m->scope_job)
if (m->service) {
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto finish;
}
}
if (m->root_directory) {
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto finish;
}
}
if (m->leader != 0)
if (m->class != _MACHINE_CLASS_INVALID)
if (dual_timestamp_is_set(&m->timestamp))
fprintf(f,
if (m->n_netif > 0) {
unsigned i;
fputs("NETIF=", f);
for (i = 0; i < m->n_netif; i++) {
if (i != 0)
fputc(' ', f);
}
fputc('\n', f);
}
r = fflush_and_check(f);
if (r < 0)
goto finish;
r = -errno;
goto finish;
}
if (m->unit) {
char *sl;
/* Create a symlink from the unit name to the machine
* name, so that we can quickly find the machine for
* each given unit. Ignore error. */
}
if (temp_path)
if (r < 0)
return r;
}
static void machine_unlink(Machine *m) {
assert(m);
if (m->unit) {
char *sl;
}
if (m->state_file)
unlink(m->state_file);
}
int machine_load(Machine *m) {
_cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL, *netif = NULL;
int r;
assert(m);
"SCOPE", &m->unit,
"SCOPE_JOB", &m->scope_job,
"SERVICE", &m->service,
"ROOT", &m->root_directory,
"ID", &id,
"LEADER", &leader,
"CLASS", &class,
"REALTIME", &realtime,
"MONOTONIC", &monotonic,
"NETIF", &netif,
NULL);
if (r < 0) {
if (r == -ENOENT)
return 0;
}
if (id)
if (leader)
if (class) {
MachineClass c;
if (c >= 0)
m->class = c;
}
if (realtime) {
unsigned long long l;
}
if (monotonic) {
unsigned long long l;
}
if (netif) {
char buf[l+1];
int ifi;
continue;
if (ifi <= 0)
continue;
return log_oom();
}
}
}
return r;
}
int r = 0;
assert(m);
if (!m->unit) {
if (!escaped)
return log_oom();
if (!scope)
return log_oom();
r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, properties, error, &job);
if (r < 0) {
return r;
} else {
}
}
if (m->unit)
return r;
}
int r;
assert(m);
if (m->started)
return 0;
if (r < 0)
return r;
/* Create cgroup */
if (r < 0)
return r;
"NAME=%s", m->name,
NULL);
if (!dual_timestamp_is_set(&m->timestamp))
dual_timestamp_get(&m->timestamp);
m->started = true;
/* Save new machine data */
machine_save(m);
machine_send_signal(m, true);
return 0;
}
static int machine_stop_scope(Machine *m) {
int r;
assert(m);
if (!m->unit)
return 0;
if (r < 0) {
return r;
}
return 0;
}
int machine_stop(Machine *m) {
int r = 0, k;
assert(m);
if (m->started)
"NAME=%s", m->name,
NULL);
/* Kill cgroup */
k = machine_stop_scope(m);
if (k < 0)
r = k;
machine_unlink(m);
if (m->started)
machine_send_signal(m, false);
m->started = false;
return r;
}
assert(m);
if (drop_not_started && !m->started)
return false;
return true;
return true;
return false;
}
void machine_add_to_gc_queue(Machine *m) {
assert(m);
if (m->in_gc_queue)
return;
m->in_gc_queue = true;
}
assert(s);
if (s->scope_job)
return MACHINE_RUNNING;
}
assert(m);
if (!m->unit)
return -ESRCH;
if (who == KILL_LEADER) {
/* If we shall simply kill the leader, do so directly */
return -errno;
return 0;
}
/* Otherwise make PID 1 do it for us, for the entire cgroup */
}
if (!o)
return NULL;
safe_close(o->errno_fd);
if (o->pid > 1)
if (o->machine) {
o->machine->n_operations--;
}
free(o);
return NULL;
}
void machine_release_unit(Machine *m) {
assert(m);
if (!m->unit)
return;
}
static const char* const machine_class_table[_MACHINE_CLASS_MAX] = {
[MACHINE_CONTAINER] = "container",
[MACHINE_VM] = "vm"
};
static const char* const machine_state_table[_MACHINE_STATE_MAX] = {
[MACHINE_OPENING] = "opening",
[MACHINE_RUNNING] = "running",
[MACHINE_CLOSING] = "closing"
};
static const char* const kill_who_table[_KILL_WHO_MAX] = {
[KILL_LEADER] = "leader",
[KILL_ALL] = "all"
};