machine.c revision 2c4c73b3ffa123cc5c69f5749c58706aafb3f257
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2011 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart PoetteringMachine* machine_new(Manager *manager, const char *name) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m->state_file = strappend("/run/systemd/machines/", m->name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (hashmap_put(manager->machines, m->name, m) < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering LIST_REMOVE(Machine, gc_queue, m->manager->machine_gc_queue, m);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering hashmap_remove(m->manager->machine_units, m->scope);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering hashmap_remove(m->manager->machines, m->name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = fopen_temporary(m->state_file, &f, &temp_path);
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering "# This is private data. Do not parse.\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fprintf(f, "SCOPE_JOB=%s\n", m->scope_job);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fprintf(f, "ROOT=%s\n", m->root_directory);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!sd_id128_equal(m->id, SD_ID128_NULL))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fprintf(f, "ID=" SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->id));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fprintf(f, "LEADER=%lu\n", (unsigned long) m->leader);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fprintf(f, "CLASS=%s\n", machine_class_to_string(m->class));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (dual_timestamp_is_set(&m->timestamp))
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt "REALTIME=%llu\n"
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt "MONOTONIC=%llu\n",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (unsigned long long) m->timestamp.realtime,
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering (unsigned long long) m->timestamp.monotonic);
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering if (ferror(f) || rename(temp_path, m->state_file) < 0) {
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering log_error("Failed to save machine data for %s: %s", m->name, strerror(-r));
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = parse_env_file(m->state_file, NEWLINE,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_error("Failed to read %s: %s", m->state_file, strerror(-r));
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering unsigned long long l;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering unsigned long long l;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int machine_start_scope(Machine *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering scope = strjoin("machine-", escaped, ".scope", NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering description = strappend(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, &error, &job);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Failed to start machine scope: %s", bus_error(&error, r));
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering hashmap_put(m->manager->machine_units, m->scope, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Create cgroup */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!dual_timestamp_is_set(&m->timestamp))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Save new machine data */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int machine_stop_scope(Machine *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = manager_stop_unit(m->manager, m->scope, &error, &job);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Failed to stop machine scope: %s", bus_error(&error, r));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "MESSAGE=Machine %s terminated.", m->name,
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering /* Kill cgroup */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint machine_check_gc(Machine *m, bool drop_not_started) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return manager_unit_is_active(m->manager, m->scope) != 0;
assert(m);
if (m->in_gc_queue)
m->in_gc_queue = true;
assert(s);
if (s->scope_job)
return MACHINE_RUNNING;
assert(m);
if (!m->scope)
return -ESRCH;