machine.c revision d0af76e68a5bab2e4fd9674b1c64a9f38d7afe97
/*-*- 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 <systemd/sd-messages.h>
#include "util.h"
#include "mkdir.h"
#include "hashmap.h"
#include "strv.h"
#include "fileio.h"
#include "special.h"
#include "unit-name.h"
#include "dbus-common.h"
#include "machine.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);
if (m->in_gc_queue)
if (m->scope) {
}
if (m->create_message)
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->scope)
if (m->scope_job)
if (m->service)
if (m->root_directory)
if (m->leader != 0)
if (m->class != _MACHINE_CLASS_INVALID)
if (dual_timestamp_is_set(&m->timestamp))
fprintf(f,
"REALTIME=%llu\n"
"MONOTONIC=%llu\n",
fflush(f);
r = -errno;
unlink(m->state_file);
}
if (r < 0)
return r;
}
int machine_load(Machine *m) {
int r;
assert(m);
"SCOPE", &m->scope,
"SCOPE_JOB", &m->scope_job,
"SERVICE", &m->service,
"ROOT", &m->root_directory,
"ID", &id,
"LEADER", &leader,
"CLASS", &class,
"REALTIME", &realtime,
"MONOTONIC", &monotonic,
NULL);
if (r < 0) {
if (r == -ENOENT)
return 0;
return r;
}
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;
}
return r;
}
static int machine_start_scope(Machine *m) {
char *job;
int r;
assert(m);
if (!m->scope) {
char *scope;
if (!escaped)
return log_oom();
if (scope)
return log_oom();
r = manager_start_scope(m->manager, m->scope, m->leader, SPECIAL_MACHINE_SLICE, description, &error, &job);
if (r < 0) {
} else {
}
}
if (m->scope)
return r;
}
int machine_start(Machine *m) {
int r;
assert(m);
if (m->started)
return 0;
/* Create cgroup */
r = machine_start_scope(m);
if (r < 0)
return r;
"NAME=%s", m->name,
"LEADER=%lu", (unsigned long) m->leader,
"MESSAGE=New machine %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) {
char *job;
int r;
assert(m);
if (!m->scope)
return 0;
if (r < 0) {
return r;
}
return r;
}
int machine_stop(Machine *m) {
int r = 0, k;
assert(m);
if (m->started)
"NAME=%s", m->name,
"LEADER=%lu", (unsigned long) m->leader,
"MESSAGE=Machine %s terminated.", m->name,
NULL);
/* Kill cgroup */
k = machine_stop_scope(m);
if (k < 0)
r = k;
unlink(m->state_file);
if (m->started)
machine_send_signal(m, false);
m->started = false;
return r;
}
assert(m);
if (drop_not_started && !m->started)
return 0;
if (m->scope_job)
return 1;
if (m->scope)
return 0;
}
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->scope)
return -ESRCH;
}
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"
};