HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/manager.c
Git blob: e0aa34f4c11534f81d2a7e0a2a8c2aa218451eb3
Lines 1001–2000 of 5,689
r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops);
if (r < 0)
return r;
r = prioq_ensure_allocated(&m->run_queue, compare_job_priority);
if (r < 0)
return r;
r = manager_setup_prefix(m);
if (r < 0)
return r;
r = manager_find_credentials_dirs(m);
if (r < 0)
return r;
r = sd_event_default(&m->event);
if (r < 0)
return r;
r = manager_setup_run_queue(m);
if (r < 0)
return r;
if (FLAGS_SET(test_run_flags, MANAGER_TEST_RUN_MINIMAL)) {
m->cgroup_root = strdup("");
if (!m->cgroup_root)
return -ENOMEM;
} else {
r = manager_setup_signals(m);
if (r < 0)
return r;
r = manager_setup_cgroup(m);
if (r < 0)
return r;
r = manager_setup_time_change(m);
if (r < 0)
return r;
r = manager_read_timezone_stat(m);
if (r < 0)
return r;
(void) manager_setup_timezone_change(m);
r = manager_setup_sigchld_event_source(m);
if (r < 0)
return r;
for (PressureResource t = 0; t < _PRESSURE_RESOURCE_MAX; t++) {
r = manager_setup_pressure_event_source(m, t);
if (r < 0)
return r;
}
(void) bpf_restrict_fs_setup(m);
}
if (test_run_flags == 0) {
if (MANAGER_IS_SYSTEM(m))
r = mkdir_label("/run/systemd/units", 0755);
else {
_cleanup_free_ char *units_path = NULL;
r = xdg_user_runtime_dir("/systemd/units", &units_path);
if (r < 0)
return r;
r = mkdir_label(units_path, 0755);
}
if (r < 0 && r != -EEXIST)
return r;
}
if (!FLAGS_SET(test_run_flags, MANAGER_TEST_DONT_OPEN_EXECUTOR)) {
r = pin_executor_binary(&m->executor_fd);
if (r < 0)
return r;
}
/* Note that we do not set up the notify fd here. We do that after deserialization,
* since they might have gotten serialized across the reexec. */
*ret = TAKE_PTR(m);
return 0;
}
static int manager_setup_notify(Manager *m) {
int r;
if (MANAGER_IS_TEST_RUN(m))
return 0;
if (m->notify_fd < 0) {
_cleanup_close_ int fd = -EBADF;
union sockaddr_union sa;
socklen_t sa_len;
/* First free all secondary fields */
m->notify_socket = mfree(m->notify_socket);
m->notify_event_source = sd_event_source_disable_unref(m->notify_event_source);
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (fd < 0)
return log_error_errno(errno, "Failed to allocate notification socket: %m");
(void) fd_increase_rxbuf(fd, MANAGER_SOCKET_RCVBUF_SIZE);
m->notify_socket = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd/notify");
if (!m->notify_socket)
return log_oom();
r = sockaddr_un_set_path(&sa.un, m->notify_socket);
if (r < 0)
return log_error_errno(r, "Notify socket '%s' not valid for AF_UNIX socket address, refusing.",
m->notify_socket);
sa_len = r;
(void) sockaddr_un_unlink(&sa.un);
r = mac_selinux_bind(fd, &sa.sa, sa_len);
if (r < 0)
return log_error_errno(r, "Failed to bind notify fd to '%s': %m", m->notify_socket);
r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
if (r < 0)
return log_error_errno(r, "Failed to enable SO_PASSCRED for notify socket: %m");
// TODO: enforce SO_PASSPIDFD when our baseline of the kernel version is bumped to >= 6.5.
r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true);
if (r < 0 && r != -ENOPROTOOPT)
log_warning_errno(r, "Failed to enable SO_PASSPIDFD for notify socket, ignoring: %m");
m->notify_fd = TAKE_FD(fd);
log_debug("Using notification socket %s", m->notify_socket);
}
if (!m->notify_event_source) {
r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m);
if (r < 0)
return log_error_errno(r, "Failed to allocate notify event source: %m");
/* Process notification messages a bit earlier than SIGCHLD, so that we can still identify to which
* service an exit message belongs. */
r = sd_event_source_set_priority(m->notify_event_source, EVENT_PRIORITY_NOTIFY);
if (r < 0)
return log_error_errno(r, "Failed to set priority of notify event source: %m");
(void) sd_event_source_set_description(m->notify_event_source, "manager-notify");
}
return 0;
}
static int manager_setup_user_lookup_fd(Manager *m) {
int r;
assert(m);
/* Set up the socket pair used for passing UID/GID resolution results from forked off processes to PID
* 1. Background: we can't do name lookups (NSS) from PID 1, since it might involve IPC and thus activation,
* and we might hence deadlock on ourselves. Hence we do all user/group lookups asynchronously from the forked
* off processes right before executing the binaries to start. In order to be able to clean up any IPC objects
* created by a unit (see RemoveIPC=) we need to know in PID 1 the used UID/GID of the executed processes,
* hence we establish this communication channel so that forked off processes can pass their UID/GID
* information back to PID 1. The forked off processes send their resolved UID/GID to PID 1 in a simple
* datagram, along with their unit name, so that we can share one communication socket pair among all units for
* this purpose.
*
* You might wonder why we need a communication channel for this that is independent of the usual notification
* socket scheme (i.e. $NOTIFY_SOCKET). The primary difference is about trust: data sent via the $NOTIFY_SOCKET
* channel is only accepted if it originates from the right unit and if reception was enabled for it. The user
* lookup socket OTOH is only accessible by PID 1 and its children until they exec(), and always available.
*
* Note that this function is called under two circumstances: when we first initialize (in which case we
* allocate both the socket pair and the event source to listen on it), and when we deserialize after a reload
* (in which case the socket pair already exists but we still need to allocate the event source for it). */
if (m->user_lookup_fds[0] < 0) {
/* Free all secondary fields */
safe_close_pair(m->user_lookup_fds);
m->user_lookup_event_source = sd_event_source_disable_unref(m->user_lookup_event_source);
if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->user_lookup_fds) < 0)
return log_error_errno(errno, "Failed to allocate user lookup socket: %m");
r = setsockopt_int(m->user_lookup_fds[0], SOL_SOCKET, SO_PASSRIGHTS, false);
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r))
log_warning_errno(r, "Failed to turn off SO_PASSRIGHTS on user lookup socket, ignoring: %m");
(void) fd_increase_rxbuf(m->user_lookup_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
}
if (!m->user_lookup_event_source) {
r = sd_event_add_io(m->event, &m->user_lookup_event_source, m->user_lookup_fds[0], EPOLLIN, manager_dispatch_user_lookup_fd, m);
if (r < 0)
return log_error_errno(r, "Failed to allocate user lookup event source: %m");
/* Process even earlier than the notify event source, so that we always know first about valid UID/GID
* resolutions */
r = sd_event_source_set_priority(m->user_lookup_event_source, EVENT_PRIORITY_USER_LOOKUP);
if (r < 0)
return log_error_errno(r, "Failed to set priority of user lookup event source: %m");
(void) sd_event_source_set_description(m->user_lookup_event_source, "user-lookup");
}
return 0;
}
static int manager_setup_handoff_timestamp_fd(Manager *m) {
int r;
assert(m);
/* Set up the socket pair used for passing timestamps back when the executor processes we fork
* off invokes execve(), i.e. when we hand off control to our payload processes. */
if (m->handoff_timestamp_fds[0] < 0) {
m->handoff_timestamp_event_source = sd_event_source_disable_unref(m->handoff_timestamp_event_source);
safe_close_pair(m->handoff_timestamp_fds);
if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->handoff_timestamp_fds) < 0)
return log_error_errno(errno, "Failed to allocate handoff timestamp socket: %m");
/* Make sure children never have to block */
(void) fd_increase_rxbuf(m->handoff_timestamp_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSCRED, true);
if (r < 0)
return log_error_errno(r, "Failed to enable SO_PASSCRED on handoff timestamp socket: %m");
r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSRIGHTS, false);
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r))
log_warning_errno(r, "Failed to turn off SO_PASSRIGHTS on handoff timestamp socket, ignoring: %m");
/* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */
r = fd_nonblock(m->handoff_timestamp_fds[0], true);
if (r < 0)
return log_error_errno(r, "Failed to make handoff timestamp socket O_NONBLOCK: %m");
}
if (!m->handoff_timestamp_event_source) {
r = sd_event_add_io(m->event, &m->handoff_timestamp_event_source, m->handoff_timestamp_fds[0], EPOLLIN, manager_dispatch_handoff_timestamp_fd, m);
if (r < 0)
return log_error_errno(r, "Failed to allocate handoff timestamp event source: %m");
r = sd_event_source_set_priority(m->handoff_timestamp_event_source, EVENT_PRIORITY_HANDOFF_TIMESTAMP);
if (r < 0)
return log_error_errno(r, "Failed to set priority of handoff timestamp event source: %m");
(void) sd_event_source_set_description(m->handoff_timestamp_event_source, "handoff-timestamp");
}
return 0;
}
static int manager_setup_pidref_transport_fd(Manager *m) {
int r;
assert(m);
/* Set up the socket pair used for passing parent and child pidrefs back when the executor unshares
* a PID namespace and forks again when using PrivatePIDs=yes. */
if (m->pidref_transport_fds[0] < 0) {
m->pidref_event_source = sd_event_source_disable_unref(m->pidref_event_source);
safe_close_pair(m->pidref_transport_fds);
if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->pidref_transport_fds) < 0)
return log_error_errno(errno, "Failed to allocate pidref socket: %m");
/* Make sure children never have to block */
(void) fd_increase_rxbuf(m->pidref_transport_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSCRED, true);
if (r < 0)
return log_error_errno(r, "Failed to enable SO_PASSCRED for pidref socket: %m");
r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSPIDFD, true);
if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
log_debug_errno(r, "SO_PASSPIDFD is not supported for pidref socket, ignoring.");
else if (r < 0)
log_warning_errno(r, "Failed to enable SO_PASSPIDFD for pidref socket, ignoring: %m");
/* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */
r = fd_nonblock(m->pidref_transport_fds[0], true);
if (r < 0)
return log_error_errno(r, "Failed to make pidref socket O_NONBLOCK: %m");
}
if (!m->pidref_event_source) {
r = sd_event_add_io(m->event, &m->pidref_event_source, m->pidref_transport_fds[0], EPOLLIN, manager_dispatch_pidref_transport_fd, m);
if (r < 0)
return log_error_errno(r, "Failed to allocate pidref event source: %m");
r = sd_event_source_set_priority(m->pidref_event_source, EVENT_PRIORITY_PIDREF);
if (r < 0)
return log_error_errno(r, "Failed to set priority of pidref event source: %m");
(void) sd_event_source_set_description(m->pidref_event_source, "pidref");
}
return 0;
}
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;
}
static unsigned manager_dispatch_release_resources_queue(Manager *m) {
unsigned n = 0;
Unit *u;
assert(m);
while ((u = LIST_POP(release_resources_queue, m->release_resources_queue))) {
assert(u->in_release_resources_queue);
u->in_release_resources_queue = false;
n++;
unit_release_resources(u);
}
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 */
_GC_OFFSET_MAX
};
static void unit_gc_mark_good(Unit *u, unsigned gc_marker) {
Unit *other;
u->gc_marker = gc_marker + GC_OFFSET_GOOD;
/* Recursively mark referenced units as GOOD as well */
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCES)
if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE)
unit_gc_mark_good(other, gc_marker);
}
static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
Unit *other;
bool is_bad;
assert(u);
if (IN_SET(u->gc_marker - gc_marker,
GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH))
return;
if (u->in_cleanup_queue)
goto bad;
if (!unit_may_gc(u))
goto good;
u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
is_bad = true;
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCED_BY) {
unit_gc_sweep(other, gc_marker);
if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
goto good;
if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
is_bad = false;
}
LIST_FOREACH(refs_by_target, ref, u->refs_by_target) {
unit_gc_sweep(ref->source, gc_marker);
if (ref->source->gc_marker == gc_marker + GC_OFFSET_GOOD)
goto good;
if (ref->source->gc_marker != gc_marker + GC_OFFSET_BAD)
is_bad = false;
}
if (is_bad)
goto bad;
/* We were unable to find anything out about this entry, so
* let's investigate it later */
u->gc_marker = gc_marker + GC_OFFSET_UNSURE;
unit_add_to_gc_queue(u);
return;
bad:
/* We definitely know that this one is not useful anymore, so
* let's mark it for deletion */
u->gc_marker = gc_marker + GC_OFFSET_BAD;
unit_add_to_cleanup_queue(u);
return;
good:
unit_gc_mark_good(u, gc_marker);
}
static unsigned manager_dispatch_gc_unit_queue(Manager *m) {
unsigned n = 0, gc_marker;
assert(m);
/* log_debug("Running GC..."); */
m->gc_marker += _GC_OFFSET_MAX;
if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
m->gc_marker = 1;
gc_marker = m->gc_marker;
Unit *u;
while ((u = m->gc_unit_queue)) {
assert(u->in_gc_queue);
unit_gc_sweep(u, gc_marker);
LIST_REMOVE(gc_queue, m->gc_unit_queue, u);
u->in_gc_queue = false;
n++;
if (IN_SET(u->gc_marker - gc_marker,
GC_OFFSET_BAD, GC_OFFSET_UNSURE)) {
if (u->id)
log_unit_debug(u, "Collecting.");
u->gc_marker = gc_marker + GC_OFFSET_BAD;
unit_add_to_cleanup_queue(u);
}
}
return n;
}
static unsigned manager_dispatch_gc_job_queue(Manager *m) {
unsigned n = 0;
Job *j;
assert(m);
while ((j = LIST_POP(gc_queue, m->gc_job_queue))) {
assert(j->in_gc_queue);
j->in_gc_queue = false;
n++;
if (!job_may_gc(j))
continue;
log_unit_debug(j->unit, "Collecting job.");
(void) job_finish_and_invalidate(j, JOB_COLLECTED, false, false);
}
return n;
}
static int manager_ratelimit_requeue(sd_event_source *s, uint64_t usec, void *userdata) {
Unit *u = userdata;
assert(u);
assert(s == u->auto_start_stop_event_source);
u->auto_start_stop_event_source = sd_event_source_unref(u->auto_start_stop_event_source);
/* Re-queue to all queues, if the rate limit hit we might have been throttled on any of them. */
unit_submit_to_stop_when_unneeded_queue(u);
unit_submit_to_start_when_upheld_queue(u);
unit_submit_to_stop_when_bound_queue(u);
return 0;
}
static int manager_ratelimit_check_and_queue(Unit *u) {
int r;
assert(u);
if (ratelimit_below(&u->auto_start_stop_ratelimit))
return 1;
/* Already queued, no need to requeue */
if (u->auto_start_stop_event_source)
return 0;
r = sd_event_add_time(
u->manager->event,
&u->auto_start_stop_event_source,
CLOCK_BOOTTIME,
ratelimit_end(&u->auto_start_stop_ratelimit),
0,
manager_ratelimit_requeue,
u);
if (r < 0)
return log_unit_error_errno(u, r, "Failed to queue timer on event loop: %m");
return 0;
}
static unsigned manager_dispatch_stop_when_unneeded_queue(Manager *m) {
unsigned n = 0;
Unit *u;
int r;
assert(m);
while ((u = LIST_POP(stop_when_unneeded_queue, m->stop_when_unneeded_queue))) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
assert(u->in_stop_when_unneeded_queue);
u->in_stop_when_unneeded_queue = false;
n++;
if (!unit_is_unneeded(u))
continue;
log_unit_debug(u, "Unit is not needed anymore.");
/* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the
* service being unnecessary after a while. */
r = manager_ratelimit_check_and_queue(u);
if (r <= 0) {
log_unit_warning(u,
"Unit not needed anymore, but not stopping since we tried this too often recently.%s",
r == 0 ? " Will retry later." : "");
continue;
}
/* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, &error, /* ret= */ NULL);
if (r < 0)
log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
}
return n;
}
static unsigned manager_dispatch_start_when_upheld_queue(Manager *m) {
unsigned n = 0;
Unit *u;
int r;
assert(m);
while ((u = LIST_POP(start_when_upheld_queue, m->start_when_upheld_queue))) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
Unit *culprit = NULL;
assert(u->in_start_when_upheld_queue);
u->in_start_when_upheld_queue = false;
n++;
if (!unit_is_upheld_by_active(u, &culprit))
continue;
log_unit_debug(u, "Unit is started because upheld by active unit %s.", culprit->id);
/* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the
* service being unnecessary after a while. */
r = manager_ratelimit_check_and_queue(u);
if (r <= 0) {
log_unit_warning(u,
"Unit needs to be started because active unit %s upholds it, but not starting since we tried this too often recently.%s",
culprit->id,
r == 0 ? " Will retry later." : "");
continue;
}
r = manager_add_job(u->manager, JOB_START, u, JOB_FAIL, &error, /* ret= */ NULL);
if (r < 0)
log_unit_warning_errno(u, r, "Failed to enqueue start job, ignoring: %s", bus_error_message(&error, r));
}
return n;
}
static unsigned manager_dispatch_stop_when_bound_queue(Manager *m) {
unsigned n = 0;
Unit *u;
int r;
assert(m);
while ((u = LIST_POP(stop_when_bound_queue, m->stop_when_bound_queue))) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
Unit *culprit = NULL;
assert(u->in_stop_when_bound_queue);
u->in_stop_when_bound_queue = false;
n++;
if (!unit_is_bound_by_inactive(u, &culprit))
continue;
log_unit_debug(u, "Unit is stopped because bound to inactive unit %s.", culprit->id);
/* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the
* service being unnecessary after a while. */
r = manager_ratelimit_check_and_queue(u);
if (r <= 0) {
log_unit_warning(u,
"Unit needs to be stopped because it is bound to inactive unit %s it, but not stopping since we tried this too often recently.%s",
culprit->id,
r == 0 ? " Will retry later." : "");
continue;
}
r = manager_add_job(u->manager, JOB_STOP, u, JOB_REPLACE, &error, /* ret= */ NULL);
if (r < 0)
log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
}
return n;
}
static unsigned manager_dispatch_stop_notify_queue(Manager *m) {
unsigned n = 0;
assert(m);
if (m->may_dispatch_stop_notify_queue < 0)
m->may_dispatch_stop_notify_queue = hashmap_isempty(m->jobs);
if (!m->may_dispatch_stop_notify_queue)
return 0;
m->may_dispatch_stop_notify_queue = false;
LIST_FOREACH(stop_notify_queue, u, m->stop_notify_queue) {
assert(u->in_stop_notify_queue);
assert(UNIT_VTABLE(u)->stop_notify);
if (UNIT_VTABLE(u)->stop_notify(u)) {
assert(!u->in_stop_notify_queue);
n++;
}
}
return n;
}
static void manager_clear_unit_dependencies(Manager *m) {
Unit *u;
const char *name;
assert(m);
HASHMAP_FOREACH_KEY(u, name, m->units) {
/* ignore aliases */
if (u->id != name)
continue;
for (Hashmap *dependencies; (dependencies = hashmap_steal_first(u->dependencies));)
hashmap_free(dependencies);
u->dependencies = hashmap_free(u->dependencies);
u->dependency_generation++;
}
}
static void manager_clear_jobs_and_units(Manager *m) {
Unit *u;
assert(m);
/* All units are going away, hence discard the full dependency graph in one pass. Otherwise
* unit_free() removes every edge from both endpoints separately, which becomes very costly with
* large numbers of synthesized mount units. */
manager_clear_unit_dependencies(m);
while ((u = hashmap_first(m->units)))
unit_free(u);
manager_dispatch_cleanup_queue(m);
assert(!m->load_queue);
assert(prioq_isempty(m->run_queue));
assert(!m->dbus_unit_queue);
assert(!m->dbus_job_queue);
assert(!m->cleanup_queue);
assert(!m->gc_unit_queue);
assert(!m->gc_job_queue);
assert(!m->cgroup_realize_queue);
assert(!m->cgroup_empty_queue);
assert(!m->cgroup_oom_queue);
assert(!m->target_deps_queue);
assert(!m->stop_when_unneeded_queue);
assert(!m->start_when_upheld_queue);
assert(!m->stop_when_bound_queue);
assert(!m->release_resources_queue);
assert(!m->stop_notify_queue);
assert(hashmap_isempty(m->jobs));
assert(hashmap_isempty(m->units));
assert(hashmap_isempty(m->units_by_invocation_id));
m->n_on_console = 0;
m->n_running_jobs = 0;
m->n_installed_jobs = 0;
m->n_failed_jobs = 0;
m->transactions_with_cycle = set_free(m->transactions_with_cycle);
}
Manager* manager_free(Manager *m) {
if (!m)
return NULL;
manager_clear_jobs_and_units(m);
for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++)
if (unit_vtable[c]->shutdown)
unit_vtable[c]->shutdown(m);
/* Keep the cgroup hierarchy in place except when we know we are going down for good */
manager_shutdown_cgroup(m, /* delete= */ IN_SET(m->objective, MANAGER_EXIT, MANAGER_REBOOT, MANAGER_POWEROFF, MANAGER_HALT, MANAGER_KEXEC));
lookup_paths_flush_generator(&m->lookup_paths);
bus_done(m);
manager_varlink_done(m);
exec_shared_runtime_vacuum(m);
hashmap_free(m->exec_shared_runtime_by_id);
dynamic_user_vacuum(m, false);
hashmap_free(m->dynamic_users);
hashmap_free(m->units);
hashmap_free(m->units_by_invocation_id);
hashmap_free(m->jobs);
hashmap_free(m->watch_pids);
hashmap_free(m->watch_pids_more);
hashmap_free(m->watch_bus);
prioq_free(m->run_queue);
set_free(m->startup_units);
set_free(m->failed_units);
sd_event_source_unref(m->signal_event_source);
sd_event_source_unref(m->sigchld_event_source);
sd_event_source_unref(m->notify_event_source);
sd_event_source_unref(m->time_change_event_source);
sd_event_source_unref(m->timezone_change_event_source);
sd_event_source_unref(m->jobs_in_progress_event_source);
sd_event_source_unref(m->run_queue_event_source);
sd_event_source_unref(m->user_lookup_event_source);
sd_event_source_unref(m->handoff_timestamp_event_source);
sd_event_source_unref(m->pidref_event_source);
FOREACH_ARRAY(pressure_event_source, m->pressure_event_source, _PRESSURE_RESOURCE_MAX)
sd_event_source_unref(*pressure_event_source);
safe_close(m->signal_fd);
safe_close(m->notify_fd);
safe_close_pair(m->user_lookup_fds);
safe_close_pair(m->handoff_timestamp_fds);
safe_close_pair(m->pidref_transport_fds);
manager_close_ask_password(m);
manager_close_idle_pipe(m);
sd_event_unref(m->event);
free(m->notify_socket);
lookup_paths_done(&m->lookup_paths);
strv_free(m->transient_environment);
strv_free(m->client_environment);
hashmap_free(m->cgroup_unit);
manager_free_unit_name_maps(m);
free(m->switch_root);
free(m->switch_root_init);
sd_bus_track_unref(m->subscribed);
strv_free(m->subscribed_as_strv);
unit_defaults_done(&m->defaults);
FOREACH_ARRAY(map, m->units_needing_mounts_for, _UNIT_MOUNT_DEPENDENCY_TYPE_MAX) {
assert(hashmap_isempty(*map));
hashmap_free(*map);
}
hashmap_free(m->uid_refs);
hashmap_free(m->gid_refs);
FOREACH_ARRAY(i, m->prefix, _EXEC_DIRECTORY_TYPE_MAX)
free(*i);
free(m->received_credentials_directory);
free(m->received_encrypted_credentials_directory);
free(m->watchdog_pretimeout_governor);
free(m->watchdog_pretimeout_governor_overridden);
sd_netlink_unref(m->nfnl);
#if BPF_FRAMEWORK
bpf_restrict_fs_destroy(m->restrict_fs);
#endif
close_many(m->restrict_fsaccess_link_fds, ELEMENTSOF(m->restrict_fsaccess_link_fds));
safe_close(m->restrict_fsaccess_bss_map_fd);
safe_close(m->executor_fd);
return mfree(m);
}
static void manager_enumerate_perpetual(Manager *m) {
assert(m);
if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
return;
/* Let's ask every type to load all units from disk/kernel that it might know */
for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) {
if (!unit_type_supported(c)) {
log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c));
continue;
}
if (unit_vtable[c]->enumerate_perpetual)
unit_vtable[c]->enumerate_perpetual(m);
}
}
static void manager_enumerate(Manager *m) {
assert(m);
if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
return;
/* Let's ask every type to load all units from disk/kernel that it might know */
for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) {
if (!unit_type_supported(c)) {
log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c));
continue;
}
if (unit_vtable[c]->enumerate)
unit_vtable[c]->enumerate(m);
}
manager_dispatch_load_queue(m);
}
static void manager_coldplug(Manager *m) {
Unit *u;
char *k;
int r;
assert(m);
log_debug("Invoking unit coldplug() handlers%s", glyph(GLYPH_ELLIPSIS));
/* Let's place the units back into their deserialized state */
HASHMAP_FOREACH_KEY(u, k, m->units) {
/* ignore aliases */
if (u->id != k)
continue;
r = unit_coldplug(u);
if (r < 0)
log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id);
}
}
static void manager_catchup(Manager *m) {
Unit *u;
char *k;
assert(m);
log_debug("Invoking unit catchup() handlers%s", glyph(GLYPH_ELLIPSIS));
/* Let's catch up on any state changes that happened while we were reloading/reexecing */
HASHMAP_FOREACH_KEY(u, k, m->units) {
/* ignore aliases */
if (u->id != k)
continue;
unit_catchup(u);
}
}
ListenFDsTag* listen_fds_tag_free(ListenFDsTag *t) {
if (!t)
return NULL;
free(t->unit_id);
free(t->fdname);
return mfree(t);
}
DEFINE_HASH_OPS_FULL(
fd_to_listen_fds_tag_hash_ops,
void, trivial_hash_func, trivial_compare_func, close_fd_ptr,
ListenFDsTag, listen_fds_tag_free);
int manager_dispatch_external_fd_to_unit(
Manager *m,
const char *unit_id,
const char *fdname,
uint64_t index,
int fd_in,
const char *log_context) {
_cleanup_close_ int fd = ASSERT_FD(fd_in);
Unit *u = NULL;
int r;
assert(m);
assert(unit_id);
assert(fdname);
assert(log_context);
/* Load the unit eagerly: if the unit file exists this brings it into UNIT_LOADED, otherwise it
* lands in UNIT_NOT_FOUND. In both cases we want to attach the fd so it's preserved until the
* unit is fully stopped (or its file appears via daemon-reload). */
r = manager_load_unit(m, unit_id, /* path= */ NULL, /* e= */ NULL, &u);
if (r < 0)
return log_warning_errno(r, "%s: failed to load unit '%s', closing fd '%s': %m",
log_context, unit_id, fdname);
if (!UNIT_VTABLE(u)->attach_external_fd_to_fdstore)
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
"%s: unit '%s' does not support fd restoration, closing fd '%s'.",
log_context, unit_id, fdname);
r = UNIT_VTABLE(u)->attach_external_fd_to_fdstore(u, TAKE_FD(fd), fdname, index);
if (r < 0)
return log_unit_warning_errno(u, r, "%s: failed to attach fd '%s' to fd store: %m",
log_context, fdname);
return 1; /* fd consumed */
}
static int manager_distribute_listen_fds_named(Manager *m, Hashmap *named_listen_fds) {
assert(m);
/* Route fds whose LISTEN_FDNAMES name was a numeric index into the matching unit's fd store.
* The hashmap is built and owned by main.c's collect_fds(), keyed by fd, with ListenFDsTag* values
* that already carry the parsed unit-id, original fdname and index (resolved against the
* upstream-pushed fdstore-mapping memfd). We steal entries here so any leftover (skipped) entries
* are still cleaned up by the hashmap's destructor on the caller side. */
if (MANAGER_IS_TEST_RUN(m))
return 0;
for (;;) {
_cleanup_(listen_fds_tag_freep) ListenFDsTag *t = NULL;
_cleanup_close_ int fd = -EBADF;
void *key;
t = hashmap_steal_first_key_and_value(named_listen_fds, &key);
if (!t)
break;
fd = PTR_TO_FD(key);
if (!t->unit_id || !t->fdname)
continue;