manager.c revision 600b704eb0fe4b4fd2901017082254634823f02d
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 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 <assert.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <dirent.h>
#ifdef HAVE_AUDIT
#include <libaudit.h>
#endif
#include "systemd/sd-daemon.h"
#include "systemd/sd-id128.h"
#include "systemd/sd-messages.h"
#include "manager.h"
#include "transaction.h"
#include "hashmap.h"
#include "macro.h"
#include "strv.h"
#include "log.h"
#include "util.h"
#include "mkdir.h"
#include "ratelimit.h"
#include "cgroup.h"
#include "mount-setup.h"
#include "unit-name.h"
#include "dbus-unit.h"
#include "dbus-job.h"
#include "missing.h"
#include "path-lookup.h"
#include "special.h"
#include "bus-errors.h"
#include "exit-status.h"
#include "virt.h"
#include "watchdog.h"
#include "cgroup-util.h"
#include "path-util.h"
#include "audit-fd.h"
/* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
#define GC_QUEUE_ENTRIES_MAX 16
/* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
/* Where clients shall send notification messages to */
#define NOTIFY_SOCKET "@/org/freedesktop/systemd1/notify"
static int manager_setup_notify(Manager *m) {
union {
struct sockaddr_un un;
} sa;
struct epoll_event ev;
int one = 1;
assert(m);
if (m->notify_watch.fd < 0) {
log_error("Failed to allocate notification socket: %m");
return -errno;
}
else
if (bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
log_error("bind() failed: %m");
return -errno;
}
log_error("SO_PASSCRED failed: %m");
return -errno;
}
return -errno;
if (!m->notify_socket)
return -ENOMEM;
return 0;
}
static int enable_special_signals(Manager *m) {
int fd;
assert(m);
/* Enable that we get SIGINT on control-alt-del. In containers
* this will fail with EPERM (older) or EINVAL (newer), so
* ignore that. */
log_warning("Failed to enable ctrl-alt-del handling: %m");
if (fd < 0) {
/* Support systems without virtual console */
log_warning("Failed to open /dev/tty0: %m");
} else {
/* Enable that we get SIGWINCH on kbrequest */
}
return 0;
}
static int manager_setup_signals(Manager *m) {
struct epoll_event ev;
assert(m);
/* We are not interested in SIGSTOP and friends. */
SIGCHLD, /* Child died */
SIGTERM, /* Reexecute daemon */
SIGHUP, /* Reload configuration */
SIGUSR2, /* systemd: dump status */
SIGINT, /* Kernel sends us this on control-alt-del */
SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
-1);
return -errno;
return -errno;
if (m->running_as == SYSTEMD_SYSTEM)
return enable_special_signals(m);
return 0;
}
static void manager_strip_environment(Manager *m) {
assert(m);
/* Remove variables from the inherited set that are part of
* the container interface:
/* Remove variables from the inherited set that are part of
* the initrd interface:
}
Manager *m;
int r = -ENOMEM;
assert(running_as >= 0);
if (!m)
return -ENOMEM;
m->running_as = running_as;
m->pin_cgroupfs_fd = -1;
m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = m->swap_watch.fd = -1;
if (!m->environment)
goto fail;
if (running_as == SYSTEMD_SYSTEM) {
if (!m->default_controllers)
goto fail;
}
goto fail;
goto fail;
goto fail;
goto fail;
goto fail;
goto fail;
if ((r = manager_setup_signals(m)) < 0)
goto fail;
if ((r = manager_setup_cgroup(m)) < 0)
goto fail;
if ((r = manager_setup_notify(m)) < 0)
goto fail;
/* Try to connect to the busses, if possible. */
goto fail;
*_m = m;
return 0;
fail:
manager_free(m);
return r;
}
static unsigned manager_dispatch_cleanup_queue(Manager *m) {
Unit *u;
unsigned n = 0;
assert(m);
while ((u = m->cleanup_queue)) {
assert(u->in_cleanup_queue);
unit_free(u);
n++;
}
return n;
}
enum {
GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */
GC_OFFSET_UNSURE, /* No clue */
GC_OFFSET_GOOD, /* We still need this unit */
GC_OFFSET_BAD, /* We don't need this unit anymore */
};
Iterator i;
bool is_bad;
assert(u);
return;
if (u->in_cleanup_queue)
goto bad;
if (unit_check_gc(u))
goto good;
is_bad = true;
goto good;
is_bad = false;
}
if (is_bad)
goto bad;
/* We were unable to find anything out about this entry, so
* let's investigate it later */
return;
bad:
/* We definitely know that this one is not useful anymore, so
* let's mark it for deletion */
return;
good:
}
static unsigned manager_dispatch_gc_queue(Manager *m) {
Unit *u;
unsigned n = 0;
unsigned gc_marker;
assert(m);
if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
(m->gc_queue_timestamp <= 0 ||
return 0;
log_debug("Running GC...");
m->gc_marker += _GC_OFFSET_MAX;
m->gc_marker = 1;
while ((u = m->gc_queue)) {
assert(u->in_gc_queue);
unit_gc_sweep(u, gc_marker);
u->in_gc_queue = false;
n++;
}
}
m->n_in_gc_queue = 0;
m->gc_queue_timestamp = 0;
return n;
}
static void manager_clear_jobs_and_units(Manager *m) {
Unit *u;
assert(m);
while ((u = hashmap_first(m->units)))
unit_free(u);
assert(!m->load_queue);
assert(!m->dbus_unit_queue);
assert(!m->dbus_job_queue);
assert(!m->cleanup_queue);
}
void manager_free(Manager *m) {
UnitType c;
int i;
assert(m);
for (c = 0; c < _UNIT_TYPE_MAX; c++)
if (unit_vtable[c]->shutdown)
unit_vtable[c]->shutdown(m);
/* If we reexecute ourselves, we keep the root cgroup
* around */
bus_done(m);
hashmap_free(m->units);
hashmap_free(m->jobs);
hashmap_free(m->watch_pids);
hashmap_free(m->watch_bus);
if (m->epoll_fd >= 0)
if (m->signal_watch.fd >= 0)
if (m->notify_watch.fd >= 0)
free(m->notify_socket);
strv_free(m->environment);
close_pipe(m->idle_pipe);
free(m->switch_root);
free(m->switch_root_init);
for (i = 0; i < RLIMIT_NLIMITS; i++)
free(m);
}
int manager_enumerate(Manager *m) {
int r = 0, q;
UnitType c;
assert(m);
* that it might know */
for (c = 0; c < _UNIT_TYPE_MAX; c++)
if (unit_vtable[c]->enumerate)
if ((q = unit_vtable[c]->enumerate(m)) < 0)
r = q;
return r;
}
int manager_coldplug(Manager *m) {
int r = 0, q;
Iterator i;
Unit *u;
char *k;
assert(m);
/* Then, let's set up their initial state. */
HASHMAP_FOREACH_KEY(u, k, m->units, i) {
/* ignore aliases */
if (u->id != k)
continue;
if ((q = unit_coldplug(u)) < 0)
r = q;
}
return r;
}
static void manager_build_unit_path_cache(Manager *m) {
char **i;
int r;
assert(m);
log_error("Failed to allocate unit path cache.");
return;
}
/* This simply builds a list of files we know exist, so that
* we don't always have to go to disk */
if (!(d = opendir(*i))) {
log_error("Failed to open directory: %m");
continue;
}
char *p;
continue;
if (!p) {
r = -ENOMEM;
goto fail;
}
if ((r = set_put(m->unit_path_cache, p)) < 0) {
free(p);
goto fail;
}
}
closedir(d);
d = NULL;
}
return;
fail:
m->unit_path_cache = NULL;
if (d)
closedir(d);
}
int r, q;
assert(m);
r = lookup_paths_init(
&m->lookup_paths, m->running_as, true,
if (r < 0)
return r;
/* If we will deserialize make sure that during enumeration
* this is already known, so we increase the counter here
* already */
if (serialization)
m->n_reloading ++;
/* First, enumerate what we can from all config files */
r = manager_enumerate(m);
/* Second, deserialize if there is something to deserialize */
if (serialization) {
if (q < 0)
r = q;
}
/* Third, fire things up! */
q = manager_coldplug(m);
if (q < 0)
r = q;
if (serialization) {
assert(m->n_reloading > 0);
m->n_reloading --;
}
return r;
}
int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
int r;
assert(m);
return -EINVAL;
}
return -EPERM;
}
log_debug("Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
tr = transaction_new();
if (!tr)
return -ENOMEM;
mode == JOB_IGNORE_DEPENDENCIES, e);
if (r < 0)
goto tr_abort;
if (mode == JOB_ISOLATE) {
r = transaction_add_isolate_jobs(tr, m);
if (r < 0)
goto tr_abort;
}
if (r < 0)
goto tr_abort;
log_debug("Enqueued job %s/%s as %u", unit->id, job_type_to_string(type), (unsigned) tr->anchor_job->id);
if (_ret)
return 0;
return r;
}
int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
int r;
assert(m);
if (r < 0)
return r;
}
assert(m);
}
assert(m);
}
unsigned manager_dispatch_load_queue(Manager *m) {
Unit *u;
unsigned n = 0;
assert(m);
/* Make sure we are not run recursively */
if (m->dispatching_load_queue)
return 0;
m->dispatching_load_queue = true;
/* Dispatches the load queue. Takes a unit from the queue and
* tries to load its data until the queue is empty */
while ((u = m->load_queue)) {
assert(u->in_load_queue);
unit_load(u);
n++;
}
m->dispatching_load_queue = false;
return n;
}
int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
UnitType t;
int r;
assert(m);
/* This will prepare the unit for loading, but not actually
* load anything from disk. */
return -EINVAL;
}
if (!name)
t = unit_name_to_type(name);
return -EINVAL;
}
if (ret) {
return 1;
}
if (!ret)
return -ENOMEM;
if (path) {
if (!ret->fragment_path) {
return -ENOMEM;
}
}
return r;
}
if (_ret)
return 0;
}
int r;
assert(m);
/* This will load the service information files, but not actually
* start any services or anything. */
if (r != 0)
return r;
if (_ret)
return 0;
}
Iterator i;
Job *j;
assert(s);
assert(f);
HASHMAP_FOREACH(j, s->jobs, i)
}
Iterator i;
Unit *u;
const char *t;
assert(s);
assert(f);
HASHMAP_FOREACH_KEY(u, t, s->units, i)
if (u->id == t)
}
void manager_clear_jobs(Manager *m) {
Job *j;
assert(m);
while ((j = hashmap_first(m->jobs)))
/* No need to recurse. We're cancelling all jobs. */
job_finish_and_invalidate(j, JOB_CANCELED, false);
}
unsigned manager_dispatch_run_queue(Manager *m) {
Job *j;
unsigned n = 0;
if (m->dispatching_run_queue)
return 0;
m->dispatching_run_queue = true;
while ((j = m->run_queue)) {
assert(j->in_run_queue);
n++;
}
m->dispatching_run_queue = false;
return n;
}
unsigned manager_dispatch_dbus_queue(Manager *m) {
Job *j;
Unit *u;
unsigned n = 0;
assert(m);
if (m->dispatching_dbus_queue)
return 0;
m->dispatching_dbus_queue = true;
while ((u = m->dbus_unit_queue)) {
assert(u->in_dbus_queue);
n++;
}
while ((j = m->dbus_job_queue)) {
assert(j->in_dbus_queue);
n++;
}
m->dispatching_dbus_queue = false;
return n;
}
static int manager_process_notify_fd(Manager *m) {
ssize_t n;
assert(m);
for (;;) {
char buf[4096];
union {
} control;
Unit *u;
char **tags;
if (n >= 0)
return -EIO;
break;
return -errno;
}
log_warning("Received notify message without credentials. Ignoring.");
continue;
}
continue;
}
buf[n] = 0;
return -ENOMEM;
if (UNIT_VTABLE(u)->notify_message)
}
return 0;
}
static int manager_dispatch_sigchld(Manager *m) {
assert(m);
for (;;) {
Unit *u;
int r;
/* First we call waitd() for a PID and do not reap the
* zombie. That way we can still access /proc/$PID for
* it while it is a zombie. */
break;
continue;
return -errno;
}
break;
}
/* Let's flush any message the dying child might still
* have queued for us. This ensures that the process
* still exists in /proc so that we can figure out
* which cgroup and hence unit it belongs to. */
if ((r = manager_process_notify_fd(m)) < 0)
return r;
/* And now figure out the unit this belongs to */
/* And now, we actually reap the zombie. */
continue;
return -errno;
}
continue;
log_debug("Child %lu died (code=%s, status=%i/%s)",
if (!u)
continue;
}
return 0;
}
int r;
return r;
}
static int manager_process_signal_fd(Manager *m) {
ssize_t n;
struct signalfd_siginfo sfsi;
bool sigchld = false;
assert(m);
for (;;) {
if (n >= 0)
return -EIO;
break;
return -errno;
}
char *p = NULL;
log_debug("Received SIG%s from PID %lu (%s).",
free(p);
} else
case SIGCHLD:
sigchld = true;
break;
case SIGTERM:
if (m->running_as == SYSTEMD_SYSTEM) {
/* This is for compatibility with the
* original sysvinit */
m->exit_code = MANAGER_REEXECUTE;
break;
}
/* Fall through */
case SIGINT:
if (m->running_as == SYSTEMD_SYSTEM) {
break;
}
/* Run the exit target if there is one, if not, just exit. */
m->exit_code = MANAGER_EXIT;
return 0;
}
break;
case SIGWINCH:
if (m->running_as == SYSTEMD_SYSTEM)
/* This is a nop on non-init */
break;
case SIGPWR:
if (m->running_as == SYSTEMD_SYSTEM)
/* This is a nop on non-init */
break;
case SIGUSR1: {
Unit *u;
u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
log_info("Trying to reconnect to bus...");
bus_init(m, true);
}
if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
log_info("Loading D-Bus service...");
}
break;
}
case SIGUSR2: {
FILE *f;
log_warning("Failed to allocate memory stream.");
break;
}
manager_dump_units(m, f, "\t");
manager_dump_jobs(m, f, "\t");
if (ferror(f)) {
fclose(f);
log_warning("Failed to write status stream");
break;
}
fclose(f);
break;
}
case SIGHUP:
m->exit_code = MANAGER_RELOAD;
break;
default: {
/* Starting SIGRTMIN+0 */
static const char * const target_table[] = {
[0] = SPECIAL_DEFAULT_TARGET,
[1] = SPECIAL_RESCUE_TARGET,
[2] = SPECIAL_EMERGENCY_TARGET,
[3] = SPECIAL_HALT_TARGET,
[4] = SPECIAL_POWEROFF_TARGET,
[5] = SPECIAL_REBOOT_TARGET,
[6] = SPECIAL_KEXEC_TARGET
};
/* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
static const ManagerExitCode code_table[] = {
[0] = MANAGER_HALT,
[1] = MANAGER_POWEROFF,
[2] = MANAGER_REBOOT,
[3] = MANAGER_KEXEC
};
break;
}
break;
}
case 20:
log_debug("Enabling showing of status.");
manager_set_show_status(m, true);
break;
case 21:
log_debug("Disabling showing of status.");
manager_set_show_status(m, false);
break;
case 22:
log_notice("Setting log level to debug.");
break;
case 23:
log_notice("Setting log level to info.");
break;
case 24:
if (m->running_as == SYSTEMD_USER) {
m->exit_code = MANAGER_EXIT;
return 0;
}
/* This is a nop on init */
break;
case 26:
log_notice("Setting log target to journal-or-kmsg.");
break;
case 27:
log_notice("Setting log target to console.");
break;
case 28:
log_notice("Setting log target to kmsg.");
break;
case 29:
log_notice("Setting log target to syslog-or-kmsg.");
break;
default:
}
}
}
}
if (sigchld)
return manager_dispatch_sigchld(m);
return 0;
}
int r;
Watch *w;
assert(m);
if (w->type == WATCH_INVALID)
return 0;
switch (w->type) {
case WATCH_SIGNAL:
/* An incoming signal? */
return -EINVAL;
if ((r = manager_process_signal_fd(m)) < 0)
return r;
break;
case WATCH_NOTIFY:
/* An incoming daemon notification event? */
return -EINVAL;
if ((r = manager_process_notify_fd(m)) < 0)
return r;
break;
case WATCH_FD:
/* Some fd event, to be dispatched to the units */
break;
case WATCH_UNIT_TIMER:
case WATCH_JOB_TIMER: {
uint64_t v;
ssize_t k;
/* Some timer event, to be dispatched to the units */
break;
}
if (w->type == WATCH_UNIT_TIMER)
else
break;
}
case WATCH_MOUNT:
/* Some mount table change, intended for the mount subsystem */
break;
case WATCH_SWAP:
/* Some swap table change, intended for the swap subsystem */
break;
case WATCH_UDEV:
/* Some notification from udev, intended for the device subsystem */
break;
case WATCH_DBUS_WATCH:
break;
case WATCH_DBUS_TIMEOUT:
break;
default:
assert_not_reached("Unknown epoll event type.");
}
return 0;
}
int manager_loop(Manager *m) {
int r;
assert(m);
m->exit_code = MANAGER_RUNNING;
/* Release the path cache */
m->unit_path_cache = NULL;
/* There might still be some zombies hanging around from
* before we were exec()'ed. Leat's reap them */
r = manager_dispatch_sigchld(m);
if (r < 0)
return r;
while (m->exit_code == MANAGER_RUNNING) {
struct epoll_event event;
int n;
int wait_msec = -1;
if (!ratelimit_test(&rl)) {
/* Yay, something is going seriously wrong, pause a little */
log_warning("Looping too fast. Throttling execution a little.");
sleep(1);
continue;
}
if (manager_dispatch_load_queue(m) > 0)
continue;
if (manager_dispatch_run_queue(m) > 0)
continue;
if (bus_dispatch(m) > 0)
continue;
if (manager_dispatch_cleanup_queue(m) > 0)
continue;
if (manager_dispatch_gc_queue(m) > 0)
continue;
if (manager_dispatch_dbus_queue(m) > 0)
continue;
if (swap_dispatch_reload(m) > 0)
continue;
/* Sleep for half the watchdog time */
if (wait_msec <= 0)
wait_msec = 1;
} else
wait_msec = -1;
if (n < 0) {
continue;
return -errno;
} else if (n == 0)
continue;
assert(n == 1);
r = process_event(m, &event);
if (r < 0)
return r;
}
return m->exit_code;
}
char *n;
Unit *u;
int r;
assert(m);
assert(s);
if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
return -EINVAL;
n = bus_path_unescape(s+31);
if (!n)
return -ENOMEM;
r = manager_load_unit(m, n, NULL, e, &u);
free(n);
if (r < 0)
return r;
*_u = u;
return 0;
}
Job *j;
unsigned id;
int r;
assert(m);
assert(s);
if (!startswith(s, "/org/freedesktop/systemd1/job/"))
return -EINVAL;
return r;
if (!(j = manager_get_job(m, id)))
return -ENOENT;
*_j = j;
return 0;
}
#ifdef HAVE_AUDIT
char *p;
int audit_fd;
audit_fd = get_audit_fd();
if (audit_fd < 0)
return;
/* Don't generate audit events if the service was already
* started and we're just deserializing */
if (m->n_reloading > 0)
return;
if (m->running_as != SYSTEMD_SYSTEM)
return;
if (u->type != UNIT_SERVICE)
return;
if (!(p = unit_name_to_prefix_and_instance(u->id))) {
return;
}
/* We aren't allowed to send audit messages?
* Then let's not retry again. */
} else
log_warning("Failed to send audit message: %m");
}
free(p);
#endif
}
int fd = -1;
union sockaddr_union sa;
int n = 0;
/* Don't generate plymouth events if the service was already
* started and we're just deserializing */
if (m->n_reloading > 0)
return;
if (m->running_as != SYSTEMD_SYSTEM)
return;
if (u->type != UNIT_SERVICE &&
u->type != UNIT_MOUNT &&
return;
/* We set SOCK_NONBLOCK here so that we rather drop the
* message then wait for plymouth */
log_error("socket() failed: %m");
return;
}
if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
errno != ECONNREFUSED &&
errno != ECONNRESET &&
errno != ECONNABORTED)
log_error("connect() failed: %m");
goto finish;
}
log_oom();
goto finish;
}
errno = 0;
errno != ECONNREFUSED &&
errno != ECONNRESET &&
errno != ECONNABORTED)
log_error("Failed to write Plymouth message: %m");
goto finish;
}
if (fd >= 0)
}
Manager *m,
const char *name,
const char* old_owner,
const char *new_owner) {
Unit *u;
assert(m);
return;
}
Manager *m,
const char *name,
Unit *u;
assert(m);
return;
}
int fd;
FILE *f;
if (m->running_as == SYSTEMD_SYSTEM)
else
if (!path)
return -ENOMEM;
if (fd < 0) {
return -errno;
}
return -errno;
*_f = f;
return 0;
}
Iterator i;
Unit *u;
const char *t;
int r;
assert(m);
assert(f);
m->n_reloading ++;
if (!in_initrd()) {
}
fputc('\n', f);
HASHMAP_FOREACH_KEY(u, t, m->units, i) {
if (u->id != t)
continue;
if (!unit_can_serialize(u))
continue;
/* Start marker */
fputc('\n', f);
m->n_reloading --;
return r;
}
}
assert(m->n_reloading > 0);
m->n_reloading --;
if (ferror(f))
return -EIO;
r = bus_fdset_add_all(m, fds);
if (r < 0)
return r;
return 0;
}
int r = 0;
assert(m);
assert(f);
log_debug("Deserializing state...");
m->n_reloading ++;
for (;;) {
if (feof(f))
r = 0;
else
r = -errno;
goto finish;
}
if (l[0] == 0)
break;
if (startswith(l, "current-job-id=")) {
else
} else if (startswith(l, "n-installed-jobs=")) {
uint32_t n;
if (safe_atou32(l+17, &n) < 0)
else
m->n_installed_jobs += n;
} else if (startswith(l, "n-failed-jobs=")) {
uint32_t n;
if (safe_atou32(l+14, &n) < 0)
else
m->n_failed_jobs += n;
} else if (startswith(l, "taint-usr=")) {
int b;
if ((b = parse_boolean(l+10)) < 0)
else
} else if (startswith(l, "firmware-timestamp="))
else if (startswith(l, "loader-timestamp="))
else if (startswith(l, "kernel-timestamp="))
else if (startswith(l, "initrd-timestamp="))
else if (startswith(l, "userspace-timestamp="))
else if (startswith(l, "finish-timestamp="))
else
log_debug("Unknown serialization item '%s'", l);
}
for (;;) {
Unit *u;
/* Start marker */
if (feof(f))
r = 0;
else
r = -errno;
goto finish;
}
goto finish;
if ((r = unit_deserialize(u, f, fds)) < 0)
goto finish;
}
if (ferror(f)) {
r = -EIO;
goto finish;
}
assert(m->n_reloading > 0);
m->n_reloading --;
return r;
}
int manager_reload(Manager *m) {
int r, q;
FILE *f;
assert(m);
r = manager_open_serialization(m, &f);
if (r < 0)
return r;
m->n_reloading ++;
if (!fds) {
m->n_reloading --;
r = -ENOMEM;
goto finish;
}
r = manager_serialize(m, f, fds, true);
if (r < 0) {
m->n_reloading --;
goto finish;
}
m->n_reloading --;
r = -errno;
goto finish;
}
/* From here on there is no way back. */
/* Find new unit paths */
q = lookup_paths_init(
&m->lookup_paths, m->running_as, true,
if (q < 0)
r = q;
/* First, enumerate what we can from all config files */
q = manager_enumerate(m);
if (q < 0)
r = q;
/* Second, deserialize our stored data */
q = manager_deserialize(m, f, fds);
if (q < 0)
r = q;
fclose(f);
f = NULL;
/* Third, fire things up! */
q = manager_coldplug(m);
if (q < 0)
r = q;
assert(m->n_reloading > 0);
m->n_reloading--;
if (f)
fclose(f);
if (fds)
return r;
}
bool manager_is_booting_or_shutting_down(Manager *m) {
Unit *u;
assert(m);
/* Is the initial job still around? */
if (manager_get_job(m, m->default_unit_job_id))
return true;
/* Is there a job for the shutdown target? */
u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
if (u)
return !!u->job;
return false;
}
void manager_reset_failed(Manager *m) {
Unit *u;
Iterator i;
assert(m);
HASHMAP_FOREACH(u, m->units, i)
}
Unit *u;
assert(m);
/* Returns true if the unit is inactive or going down */
if (!(u = manager_get_unit(m, name)))
return true;
return unit_pending_inactive(u);
}
void manager_check_finished(Manager *m) {
char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
assert(m);
if (hashmap_size(m->jobs) > 0)
return;
/* Notify Type=idle units that we are done now */
close_pipe(m->idle_pipe);
/* Turn off confirm spawn now */
m->confirm_spawn = false;
if (dual_timestamp_is_set(&m->finish_timestamp))
return;
/* Note that m->kernel_usec.monotonic is always at 0,
* and m->firmware_usec.monotonic and
* m->loader_usec.monotonic should be considered
* negative values. */
if (dual_timestamp_is_set(&m->initrd_timestamp)) {
if (!log_on_console())
"KERNEL_USEC=%llu", (unsigned long long) kernel_usec,
"INITRD_USEC=%llu", (unsigned long long) initrd_usec,
"USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
"MESSAGE=Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
NULL);
} else {
initrd_usec = 0;
if (!log_on_console())
"KERNEL_USEC=%llu", (unsigned long long) kernel_usec,
"USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
"MESSAGE=Startup finished in %s (kernel) + %s (userspace) = %s.",
NULL);
}
} else {
if (!log_on_console())
"USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
"MESSAGE=Startup finished in %s.",
NULL);
}
bus_broadcast_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec);
sd_notifyf(false,
"READY=1\nSTATUS=Startup finished in %s.",
}
char *p;
int r;
assert(m);
if (*generator)
return 0;
if (!p)
return log_oom();
r = mkdir_p_label(p, 0755);
if (r < 0) {
free(p);
return r;
}
} else {
if (!p)
return log_oom();
if (!mkdtemp(p)) {
free(p);
log_error("Failed to create generator directory: %m");
return -errno;
}
}
*generator = p;
return 0;
}
assert(m);
if (!*generator)
return;
}
return;
}
void manager_run_generators(Manager *m) {
const char *generator_path;
const char *argv[5];
mode_t u;
int r;
assert(m);
d = opendir(generator_path);
if (!d) {
return;
log_error("Failed to enumerate generator directory: %m");
return;
}
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
u = umask(0022);
umask(u);
trim_generator_dir(m, &m->generator_unit_path);
if (d)
closedir(d);
}
assert(m);
if (!*generator)
return;
}
void manager_undo_generators(Manager *m) {
assert(m);
remove_generator_dir(m, &m->generator_unit_path);
}
char **l;
assert(m);
l = strv_copy(controllers);
if (!l)
return -ENOMEM;
m->default_controllers = l;
return 0;
}
int i;
assert(m);
for (i = 0; i < RLIMIT_NLIMITS; i++) {
if (!default_rlimit[i])
continue;
if (!m->rlimit[i])
return -ENOMEM;
}
return 0;
}
void manager_recheck_journal(Manager *m) {
Unit *u;
assert(m);
if (m->running_as != SYSTEMD_SYSTEM)
return;
u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
return;
}
u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
return;
}
/* Hmm, OK, so the socket is fully up and the service is up
* too, then let's make use of the thing. */
log_open();
}
void manager_set_show_status(Manager *m, bool b) {
assert(m);
if (m->running_as != SYSTEMD_SYSTEM)
return;
m->show_status = b;
if (b)
touch("/run/systemd/show-status");
else
unlink("/run/systemd/show-status");
}
bool manager_get_show_status(Manager *m) {
assert(m);
if (m->running_as != SYSTEMD_SYSTEM)
return false;
if (m->show_status)
return true;
/* If Plymouth is running make sure we show the status, so
* that there's something nice to see when people press Esc */
return plymouth_running();
}