machined.c revision ebeccf9eecf5939a2ef772c3160e89efcad96194
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 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 <pwd.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "sd-daemon.h"
#include "strv.h"
#include "conf-parser.h"
#include "cgroup-util.h"
#include "mkdir.h"
#include "bus-util.h"
#include "bus-error.h"
#include "label.h"
#include "machined.h"
Manager *manager_new(void) {
Manager *m;
int r;
if (!m)
return NULL;
manager_free(m);
return NULL;
}
r = sd_event_default(&m->event);
if (r < 0) {
manager_free(m);
return NULL;
}
sd_event_set_watchdog(m->event, true);
return m;
}
void manager_free(Manager *m) {
assert(m);
hashmap_free(m->machines);
sd_bus_unref(m->bus);
sd_event_unref(m->event);
free(m);
}
int manager_enumerate_machines(Manager *m) {
int r = 0;
assert(m);
/* Read in machine data stored on disk */
if (!d) {
return 0;
return -errno;
}
int k;
if (!dirent_is_file(de))
continue;
/* Ignore symlinks that map the unit name to the machine */
continue;
if (k < 0) {
r = k;
continue;
}
k = machine_load(machine);
if (k < 0)
r = k;
}
return r;
}
static int manager_connect_bus(Manager *m) {
int r;
assert(m);
r = sd_bus_default_system(&m->bus);
if (r < 0)
return log_error_errno(r, "Failed to connect to system bus: %m");
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable, m);
if (r < 0)
return log_error_errno(r, "Failed to add manager object vtable: %m");
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable, machine_object_find, m);
if (r < 0)
return log_error_errno(r, "Failed to add machine object vtable: %m");
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/machine", machine_node_enumerator, m);
if (r < 0)
return log_error_errno(r, "Failed to add machine enumerator: %m");
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable, image_object_find, m);
if (r < 0)
return log_error_errno(r, "Failed to add image object vtable: %m");
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/image", image_node_enumerator, m);
if (r < 0)
return log_error_errno(r, "Failed to add image enumerator: %m");
r = sd_bus_add_match(m->bus,
NULL,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='JobRemoved',"
"path='/org/freedesktop/systemd1'",
m);
if (r < 0)
return log_error_errno(r, "Failed to add match for JobRemoved: %m");
r = sd_bus_add_match(m->bus,
NULL,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='UnitRemoved',"
"path='/org/freedesktop/systemd1'",
m);
if (r < 0)
return log_error_errno(r, "Failed to add match for UnitRemoved: %m");
r = sd_bus_add_match(m->bus,
NULL,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.DBus.Properties',"
"member='PropertiesChanged'",
m);
if (r < 0)
return log_error_errno(r, "Failed to add match for PropertiesChanged: %m");
r = sd_bus_add_match(m->bus,
NULL,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='Reloading',"
"path='/org/freedesktop/systemd1'",
m);
if (r < 0)
return log_error_errno(r, "Failed to add match for Reloading: %m");
r = sd_bus_call_method(
m->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Subscribe",
&error,
if (r < 0) {
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to register name: %m");
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
return 0;
}
assert(m);
while ((machine = m->machine_gc_queue)) {
machine->in_gc_queue = false;
}
}
}
int manager_startup(Manager *m) {
Iterator i;
int r;
assert(m);
/* Connect to the bus */
r = manager_connect_bus(m);
if (r < 0)
return r;
/* Deserialize state */
/* Remove stale objects before we start them */
manager_gc(m, false);
/* And start everything */
return 0;
}
static bool check_idle(void *userdata) {
manager_gc(m, true);
return hashmap_isempty(m->machines);
}
int manager_run(Manager *m) {
assert(m);
return bus_event_loop_with_idle(
m->event,
m->bus,
"org.freedesktop.machine1",
check_idle, m);
}
int r;
log_open();
umask(0022);
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
/* Always create the directories people can create inotify
* watches in. Note that some applications might check for the
* machined is available, so please always make sure this
* check stays in. */
m = manager_new();
if (!m) {
r = log_oom();
goto finish;
}
r = manager_startup(m);
if (r < 0) {
log_error_errno(r, "Failed to fully start up daemon: %m");
goto finish;
}
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
r = manager_run(m);
if (m)
manager_free(m);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}