machined.c revision aba8b84e8d72224445b67646974b9dabc0972187
/*-*- 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 <systemd/sd-daemon.h>
#include "machined.h"
#include "dbus-common.h"
#include "dbus-loop.h"
#include "strv.h"
#include "conf-parser.h"
#include "mkdir.h"
#include "cgroup-util.h"
Manager *manager_new(void) {
Manager *m;
if (!m)
return NULL;
m->bus_fd = -1;
m->epoll_fd = -1;
if (!m->machines || !m->machine_units) {
manager_free(m);
return NULL;
}
return m;
}
void manager_free(Manager *m) {
assert(m);
hashmap_free(m->machines);
if (m->bus) {
dbus_connection_flush(m->bus);
dbus_connection_close(m->bus);
dbus_connection_unref(m->bus);
}
if (m->bus_fd >= 0)
if (m->epoll_fd >= 0)
free(m);
}
assert(m);
if (machine) {
if (_machine)
return 0;
}
if (!machine)
return -ENOMEM;
if (_machine)
return 0;
}
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;
if (k < 0) {
r = k;
continue;
}
k = machine_load(machine);
if (k < 0)
r = k;
}
return r;
}
int r;
assert(m);
if (r < 0)
return r;
if (!mm)
return 0;
return 1;
}
static int manager_connect_bus(Manager *m) {
int r;
struct epoll_event ev = {
};
assert(m);
if (!m->bus) {
r = -ECONNREFUSED;
goto fail;
}
if (!dbus_connection_register_object_path(m->bus, "/org/freedesktop/machine1", &bus_manager_vtable, m) ||
!dbus_connection_register_fallback(m->bus, "/org/freedesktop/machine1/machine", &bus_machine_vtable, m) ||
r = log_oom();
goto fail;
}
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='JobRemoved',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
}
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.DBus.Properties',"
"member='PropertiesChanged'",
&error);
if (dbus_error_is_set(&error)) {
}
m->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Subscribe",
NULL,
&error,
if (r < 0) {
}
if (dbus_error_is_set(&error)) {
r = -EIO;
goto fail;
}
if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
r = -EEXIST;
goto fail;
}
if (m->bus_fd < 0) {
r = m->bus_fd;
goto fail;
}
goto fail;
return 0;
fail:
return r;
}
assert(m);
while ((machine = m->machine_gc_queue)) {
machine->in_gc_queue = false;
}
}
}
int manager_startup(Manager *m) {
int r;
Iterator i;
assert(m);
if (m->epoll_fd < 0)
return -errno;
/* 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;
}
int manager_run(Manager *m) {
assert(m);
for (;;) {
struct epoll_event event;
int n;
manager_gc(m, true);
continue;
manager_gc(m, true);
if (n < 0) {
continue;
log_error("epoll() failed: %m");
return -errno;
}
if (n == 0)
continue;
case FD_BUS:
bus_loop_dispatch(m->bus_fd);
break;
default:
assert_not_reached("Unknown fd");
}
}
return 0;
}
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) {
goto finish;
}
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
r = manager_run(m);
sd_notify(false,
"STATUS=Shutting down...");
if (m)
manager_free(m);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}