/***
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 <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sd-id128.h"
#include "sd-messages.h"
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-util.h"
#include "cgroup-util.h"
#include "dbus-unit.h"
#include "dbus.h"
#include "dropin.h"
#include "escape.h"
#include "execute.h"
#include "fileio-label.h"
#include "formats-util.h"
#include "load-dropin.h"
#include "load-fragment.h"
#include "log.h"
#include "macro.h"
#include "missing.h"
#include "mkdir.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "set.h"
#include "special.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "unit-name.h"
#include "unit.h"
#include "user-util.h"
#include "virt.h"
[UNIT_SERVICE] = &service_vtable,
[UNIT_SOCKET] = &socket_vtable,
[UNIT_BUSNAME] = &busname_vtable,
[UNIT_TARGET] = &target_vtable,
[UNIT_DEVICE] = &device_vtable,
[UNIT_MOUNT] = &mount_vtable,
[UNIT_SWAP] = &swap_vtable,
[UNIT_TIMER] = &timer_vtable,
[UNIT_PATH] = &path_vtable,
[UNIT_SLICE] = &slice_vtable,
[UNIT_SCOPE] = &scope_vtable
};
Unit *u;
assert(m);
if (!u)
return NULL;
if (!u->names) {
free(u);
return NULL;
}
u->manager = m;
u->type = _UNIT_TYPE_INVALID;
u->default_dependencies = true;
u->unit_file_preset = -1;
u->cgroup_inotify_wd = -1;
u->job_timeout = USEC_INFINITY;
return u;
}
assert(u);
}
assert(u);
cc = unit_get_cgroup_context(u);
if (cc) {
/* Copy in the manager defaults into the cgroup
* context, _before_ the rest of the settings have
* been initialized */
if (u->type != UNIT_SLICE)
}
ec = unit_get_exec_context(u);
if (ec)
kc = unit_get_kill_context(u);
if (kc)
if (UNIT_VTABLE(u)->init)
UNIT_VTABLE(u)->init(u);
}
UnitType t;
int r;
assert(u);
if (!u->instance)
return -EINVAL;
if (r < 0)
return r;
} else {
if (!s)
return -ENOMEM;
}
if (set_contains(u->names, s))
return 0;
return -EEXIST;
return -EINVAL;
t = unit_name_to_type(s);
if (t < 0)
return -EINVAL;
return -EINVAL;
r = unit_name_to_instance(s, &i);
if (r < 0)
return r;
if (i && unit_vtable[t]->no_instances)
return -EINVAL;
/* Ensure that this unit is either instanced or not instanced,
* but not both. Note that we do allow names with different
* instance names however! */
return -EINVAL;
return -EEXIST;
return -E2BIG;
if (r < 0)
return r;
assert(r > 0);
if (r < 0) {
(void) set_remove(u->names, s);
return r;
}
if (u->type == _UNIT_TYPE_INVALID) {
u->type = t;
u->id = s;
u->instance = i;
unit_init(u);
i = NULL;
}
s = NULL;
return 0;
}
_cleanup_free_ char *t = NULL;
char *s, *i;
int r;
assert(u);
if (!u->instance)
return -EINVAL;
if (r < 0)
return r;
name = t;
}
/* Selects one of the names of this unit as the id */
if (!s)
return -ENOENT;
/* Determine the new instance from the new id */
r = unit_name_to_instance(s, &i);
if (r < 0)
return r;
u->id = s;
u->instance = i;
return 0;
}
char *s;
assert(u);
if (isempty(description))
s = NULL;
else {
s = strdup(description);
if (!s)
return -ENOMEM;
}
free(u->description);
u->description = s;
return 0;
}
assert(u);
if (u->job)
return true;
if (u->nop_job)
return true;
state = unit_active_state(u);
/* If the unit is inactive and failed and no job is queued for
* it, then release its runtime resources */
if (UNIT_IS_INACTIVE_OR_FAILED(state) &&
UNIT_VTABLE(u)->release_resources(u);
/* But we keep the unit object around for longer when it is
* referenced or configured to not be gc'ed */
if (state != UNIT_INACTIVE)
return true;
if (u->no_gc)
return true;
if (u->refs)
return true;
if (UNIT_VTABLE(u)->check_gc)
if (UNIT_VTABLE(u)->check_gc(u))
return true;
return false;
}
assert(u);
return;
u->in_load_queue = true;
}
assert(u);
if (u->in_cleanup_queue)
return;
u->in_cleanup_queue = true;
}
assert(u);
if (u->in_gc_queue || u->in_cleanup_queue)
return;
if (unit_check_gc(u))
return;
u->in_gc_queue = true;
u->manager->n_in_gc_queue ++;
}
assert(u);
return;
/* Shortcut things if nobody cares */
u->sent_dbus_new_signal = true;
return;
}
u->in_dbus_queue = true;
}
Iterator i;
assert(u);
/* Frees the set and makes sure we are dropped from the
* inverse pointers */
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
}
set_free(s);
}
char **i;
assert(u);
if (!u->transient)
return;
if (u->fragment_path)
(void) unlink(u->fragment_path);
STRV_FOREACH(i, u->dropin_paths) {
_cleanup_free_ char *p = NULL;
(void) unlink(*i);
p = dirname_malloc(*i);
if (p)
(void) rmdir(p);
}
}
char **j;
STRV_FOREACH(j, u->requires_mounts_for) {
char s[strlen(*j) + 1];
PATH_FOREACH_PREFIX_MORE(s, *j) {
char *y;
Set *x;
if (!x)
continue;
set_remove(x, u);
if (set_isempty(x)) {
free(y);
set_free(x);
}
}
}
}
assert(u);
if (u->type < 0)
return;
if (UNIT_VTABLE(u)->done)
UNIT_VTABLE(u)->done(u);
ec = unit_get_exec_context(u);
if (ec)
cc = unit_get_cgroup_context(u);
if (cc)
}
Iterator i;
char *t;
assert(u);
if (u->manager->n_reloading <= 0)
unit_done(u);
SET_FOREACH(t, u->names, i)
if (u->job) {
job_uninstall(j);
job_free(j);
}
if (u->nop_job) {
job_uninstall(j);
job_free(j);
}
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
bidi_set_free(u, u->dependencies[d]);
if (u->type != _UNIT_TYPE_INVALID)
if (u->in_load_queue)
if (u->in_dbus_queue)
if (u->in_cleanup_queue)
if (u->in_gc_queue) {
u->manager->n_in_gc_queue--;
}
if (u->in_cgroup_queue)
(void) manager_update_failed_units(u->manager, u, false);
free(u->description);
strv_free(u->documentation);
free(u->fragment_path);
free(u->source_path);
strv_free(u->dropin_paths);
set_free_free(u->names);
free(u->reboot_arg);
unit_ref_unset(&u->slice);
while (u->refs)
unit_ref_unset(u->refs);
free(u);
}
assert(u);
if (u->load_state == UNIT_MERGED)
return unit_active_state(unit_follow_merge(u));
/* After a reload it might happen that a unit is not correctly
* loaded but still has a process around. That's why we won't
* shortcut failed loading to UNIT_INACTIVE_FAILED. */
return UNIT_VTABLE(u)->active_state(u);
}
assert(u);
return UNIT_VTABLE(u)->sub_state_to_string(u);
}
int r;
assert(s);
if (!*other)
return 0;
if (*s) {
if (r < 0)
return r;
} else {
*s = *other;
}
return 0;
}
char *t;
Iterator i;
int r;
assert(u);
if (r < 0)
return r;
SET_FOREACH(t, u->names, i)
return 0;
}
unsigned n_reserve;
assert(u);
assert(d < _UNIT_DEPENDENCY_MAX);
/*
* If u does not have this dependency set allocated, there is no need
* to reserve anything. In that case other's set will be transferred
* as a whole to u by complete_move().
*/
if (!u->dependencies[d])
return 0;
/* merge_dependencies() will skip a u-on-u dependency */
}
Iterator i;
int r;
assert(u);
assert(d < _UNIT_DEPENDENCY_MAX);
/* Fix backwards pointers */
for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++) {
/* Do not add dependencies between u and itself */
if (back == u) {
maybe_warn_about_dependency(u, other_id, k);
} else {
if (r == -EEXIST)
else
}
}
}
/* Also do not move dependencies on u to itself */
if (back)
maybe_warn_about_dependency(u, other_id, d);
/* The move cannot fail. The caller must have performed a reservation. */
}
int r;
assert(u);
if (other == u)
return 0;
return -EINVAL;
return -EINVAL;
return -EEXIST;
return -EEXIST;
return -EEXIST;
return -EEXIST;
/* Make reservations to ensure merge_dependencies() won't fail */
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
r = reserve_dependencies(u, other, d);
/*
* We don't rollback reservations if we fail. We don't have
* a way to undo reservations. A reservation is not a leak.
*/
if (r < 0)
return r;
}
/* Merge names */
r = merge_names(u, other);
if (r < 0)
return r;
/* Redirect all references */
/* Merge dependencies */
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
other->merged_into = u;
/* If there is still some data attached to the other node, we
* don't need it anymore, and can free it. */
return 0;
}
int r;
_cleanup_free_ char *s = NULL;
assert(u);
if (!u->instance)
return -EINVAL;
if (r < 0)
return r;
name = s;
}
if (other)
return unit_merge(u, other);
return unit_add_name(u, name);
}
assert(u);
while (u->load_state == UNIT_MERGED)
assert_se(u = u->merged_into);
return u;
}
int r;
assert(u);
assert(c);
if (c->working_directory) {
r = unit_require_mounts_for(u, c->working_directory);
if (r < 0)
return r;
}
if (c->root_directory) {
r = unit_require_mounts_for(u, c->root_directory);
if (r < 0)
return r;
}
return 0;
if (c->private_tmp) {
r = unit_require_mounts_for(u, "/tmp");
if (r < 0)
return r;
r = unit_require_mounts_for(u, "/var/tmp");
if (r < 0)
return r;
}
if (c->std_output != EXEC_OUTPUT_KMSG &&
c->std_output != EXEC_OUTPUT_SYSLOG &&
c->std_output != EXEC_OUTPUT_JOURNAL &&
c->std_output != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
c->std_output != EXEC_OUTPUT_SYSLOG_AND_CONSOLE &&
c->std_error != EXEC_OUTPUT_KMSG &&
c->std_error != EXEC_OUTPUT_SYSLOG &&
c->std_error != EXEC_OUTPUT_JOURNAL &&
c->std_error != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
c->std_error != EXEC_OUTPUT_JOURNAL_AND_CONSOLE &&
return 0;
/* If syslog or kernel logging is requested, make sure our own
* logging daemon is run first. */
if (r < 0)
return r;
return 0;
}
assert(u);
if (u->description)
return u->description;
}
char *t, **j;
Iterator i;
const char *prefix2;
char
int r;
assert(u);
fprintf(f,
"%s-> Unit %s:\n"
"%s\tDescription: %s\n"
"%s\tInstance: %s\n"
"%s\tUnit Load State: %s\n"
"%s\tUnit Active State: %s\n"
"%s\nState Change Timestamp: %s\n"
"%s\tInactive Exit Timestamp: %s\n"
"%s\tActive Enter Timestamp: %s\n"
"%s\tActive Exit Timestamp: %s\n"
"%s\tInactive Enter Timestamp: %s\n"
"%s\tGC Check Good: %s\n"
"%s\tNeed Daemon Reload: %s\n"
"%s\tTransient: %s\n"
"%s\tSlice: %s\n"
"%s\tCGroup: %s\n"
"%s\tCGroup realized: %s\n"
"%s\tCGroup mask: 0x%x\n"
"%s\tCGroup members mask: 0x%x\n",
prefix, unit_description(u),
prefix, strna(format_timestamp(timestamp0, sizeof(timestamp0), u->state_change_timestamp.realtime)),
prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->inactive_exit_timestamp.realtime)),
prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->active_enter_timestamp.realtime)),
prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->inactive_enter_timestamp.realtime)),
prefix, u->cgroup_members_mask);
SET_FOREACH(t, u->names, i)
STRV_FOREACH(j, u->documentation)
following = unit_following(u);
if (following)
r = unit_following_set(u, &following_set);
if (r >= 0) {
}
if (u->fragment_path)
if (u->source_path)
STRV_FOREACH(j, u->dropin_paths)
if (u->job_timeout != USEC_INFINITY)
fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout, 0));
if (u->job_timeout_action != FAILURE_ACTION_NONE)
fprintf(f, "%s\tJob Timeout Action: %s\n", prefix, failure_action_to_string(u->job_timeout_action));
if (u->job_timeout_reboot_arg)
if (dual_timestamp_is_set(&u->condition_timestamp))
fprintf(f,
"%s\tCondition Timestamp: %s\n"
"%s\tCondition Result: %s\n",
if (dual_timestamp_is_set(&u->assert_timestamp))
fprintf(f,
"%s\tAssert Timestamp: %s\n"
"%s\tAssert Result: %s\n",
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
}
if (!strv_isempty(u->requires_mounts_for)) {
fprintf(f,
"%s\tRequiresMountsFor:", prefix);
STRV_FOREACH(j, u->requires_mounts_for)
fprintf(f, " %s", *j);
fputs("\n", f);
}
if (u->load_state == UNIT_LOADED) {
fprintf(f,
"%s\tStopWhenUnneeded: %s\n"
"%s\tRefuseManualStart: %s\n"
"%s\tRefuseManualStop: %s\n"
"%s\tDefaultDependencies: %s\n"
"%s\tOnFailureJobMode: %s\n"
"%s\tIgnoreOnIsolate: %s\n",
if (UNIT_VTABLE(u)->dump)
} else if (u->load_state == UNIT_MERGED)
fprintf(f,
"%s\tMerged into: %s\n",
else if (u->load_state == UNIT_ERROR)
if (u->job)
if (u->nop_job)
}
/* Common implementation for multiple backends */
int r;
assert(u);
/* Load a .{service,socket,...} file */
r = unit_load_fragment(u);
if (r < 0)
return r;
if (u->load_state == UNIT_STUB)
return -ENOENT;
/* Load drop-in directory data */
r = unit_load_dropin(unit_follow_merge(u));
if (r < 0)
return r;
return 0;
}
/* Common implementation for multiple backends */
int r;
assert(u);
/* Same as unit_load_fragment_and_dropin(), but whether
* something can be loaded or not doesn't matter. */
/* Load a .service file */
r = unit_load_fragment(u);
if (r < 0)
return r;
if (u->load_state == UNIT_STUB)
u->load_state = UNIT_LOADED;
/* Load drop-in directory data */
r = unit_load_dropin(unit_follow_merge(u));
if (r < 0)
return r;
return 0;
}
assert(u);
return 0;
/* Only add the dependency if both units are loaded, so that
* that loop check below is reliable */
if (u->load_state != UNIT_LOADED ||
return 0;
/* If either side wants no automatic dependencies, then let's
* skip this */
if (!u->default_dependencies ||
return 0;
/* Don't create loops */
return 0;
}
};
Iterator i;
unsigned k;
int r = 0;
assert(u);
for (k = 0; k < ELEMENTSOF(deps); k++)
r = unit_add_default_target_dependency(u, target);
if (r < 0)
return r;
}
return r;
}
assert(u);
if (!UNIT_HAS_CGROUP_CONTEXT(u))
return 0;
if (UNIT_ISSET(u->slice))
if (unit_has_name(u, SPECIAL_ROOT_SLICE))
return 0;
return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_ROOT_SLICE, NULL, true);
}
char **i;
int r;
assert(u);
STRV_FOREACH(i, u->requires_mounts_for) {
PATH_FOREACH_PREFIX_MORE(prefix, *i) {
_cleanup_free_ char *p = NULL;
Unit *m;
if (r < 0)
return r;
m = manager_get_unit(u->manager, p);
if (!m) {
/* Make sure to load the mount unit if
* it exists. If so the dependencies
* on this unit will be added later
* during the loading of the mount
* unit. */
continue;
}
if (m == u)
continue;
if (m->load_state != UNIT_LOADED)
continue;
r = unit_add_dependency(u, UNIT_AFTER, m, true);
if (r < 0)
return r;
if (m->fragment_path) {
r = unit_add_dependency(u, UNIT_REQUIRES, m, true);
if (r < 0)
return r;
}
}
}
return 0;
}
CGroupContext *c;
int r;
c = unit_get_cgroup_context(u);
if (!c)
return 0;
if (c->startup_cpu_shares == CGROUP_CPU_SHARES_INVALID &&
return 0;
if (r < 0)
return r;
}
int r;
assert(u);
if (u->in_load_queue) {
u->in_load_queue = false;
}
if (u->type == _UNIT_TYPE_INVALID)
return -EINVAL;
if (u->load_state != UNIT_STUB)
return 0;
if (UNIT_VTABLE(u)->load) {
r = UNIT_VTABLE(u)->load(u);
if (r < 0)
goto fail;
}
if (u->load_state == UNIT_STUB) {
r = -ENOENT;
goto fail;
}
if (u->load_state == UNIT_LOADED) {
r = unit_add_target_dependencies(u);
if (r < 0)
goto fail;
r = unit_add_slice_dependencies(u);
if (r < 0)
goto fail;
r = unit_add_mount_dependencies(u);
if (r < 0)
goto fail;
r = unit_add_startup_units(u);
if (r < 0)
goto fail;
log_unit_error(u, "More than one OnFailure= dependencies specified but OnFailureJobMode=isolate set. Refusing.");
r = -EINVAL;
goto fail;
}
}
return 0;
fail:
u->load_error = r;
log_unit_debug_errno(u, r, "Failed to load configuration: %m");
return r;
}
static bool unit_condition_test_list(Unit *u, Condition *first, const char *(*to_string)(ConditionType t)) {
Condition *c;
assert(u);
/* If the condition list is empty, then it is true */
if (!first)
return true;
/* Otherwise, if all of the non-trigger conditions apply and
* if any of the trigger conditions apply (unless there are
* none) we return true */
int r;
r = condition_test(c);
if (r < 0)
"Couldn't determine result for %s=%s%s%s, assuming failed: %m",
c->parameter);
else
"%s=%s%s%s %s.",
c->parameter,
if (!c->trigger && r <= 0)
return false;
triggered = r > 0;
}
return triggered != 0;
}
assert(u);
return u->condition_result;
}
assert(u);
return u->assert_result;
}
manager_status_printf(u->manager, STATUS_TYPE_NORMAL, status, unit_status_msg_format, unit_description(u));
}
const char *format;
assert(u);
if (t != JOB_RELOAD) {
if (format_table) {
if (format)
return format;
}
}
/* Return generic strings */
if (t == JOB_START)
return "Starting %s.";
else if (t == JOB_STOP)
return "Stopping %s.";
else
return "Reloading %s.";
}
const char *format;
assert(u);
/* Reload status messages have traditionally not been printed to console. */
return;
format = unit_get_status_message_format(u, t);
}
const char *format;
assert(u);
return;
if (log_on_console())
return;
/* We log status messages for all units and all operations. */
format = unit_get_status_message_format(u, t);
t == JOB_STOP ? SD_MESSAGE_UNIT_STOPPING :
/* Note that we deliberately use LOG_MESSAGE() instead of
* LOG_UNIT_MESSAGE() here, since this is supposed to mimic
* closely what is written to screen using the status output,
* which is supposed the highest level, friendliest output
* possible, which means we should avoid the low-level unit
* name. */
LOG_UNIT_ID(u),
NULL);
}
assert(u);
assert(t >= 0);
assert(t < _JOB_TYPE_MAX);
}
assert(u);
if (ratelimit_test(&u->start_limit)) {
u->start_limit_hit = false;
return 0;
}
log_unit_warning(u, "Start request repeated too quickly.");
u->start_limit_hit = true;
}
/* Errors:
* -EBADR: This unit type does not support starting.
* -EALREADY: Unit is already started.
* -EAGAIN: An operation is already in progress. Retry later.
* -ECANCELED: Too many requests for now.
* -EPROTO: Assert failed
* -EINVAL: Unit not loaded
* -EOPNOTSUPP: Unit type not supported
*/
int r;
assert(u);
/* If this is already started, then this will succeed. Note
* that this will even succeed if this unit is not startable
* by the user. This is relied on to detect when we need to
* wait for units and when waiting is finished. */
state = unit_active_state(u);
return -EALREADY;
/* Make sure we don't enter a busy loop of some kind. */
r = unit_start_limit_test(u);
if (r < 0)
return r;
/* Units that aren't loaded cannot be started */
if (u->load_state != UNIT_LOADED)
return -EINVAL;
/* If the conditions failed, don't do anything at all. If we
* already are activating this call might still be useful to
* speed up activation in case there is some hold-off time,
* but we don't want to recheck the condition in that case. */
if (state != UNIT_ACTIVATING &&
!unit_condition_test(u)) {
log_unit_debug(u, "Starting requested but condition failed. Not starting unit.");
return -EALREADY;
}
/* If the asserts failed, fail the entire job */
if (state != UNIT_ACTIVATING &&
!unit_assert_test(u)) {
log_unit_notice(u, "Starting requested but asserts failed.");
return -EPROTO;
}
/* Units of types that aren't supported cannot be
* started. Note that we do this test only after the condition
* checks, so that we rather return condition check errors
* (which are usually not considered a true failure) than "not
* supported" errors (which are considered a failure).
*/
if (!unit_supported(u))
return -EOPNOTSUPP;
/* Forward to the main object, if we aren't it. */
following = unit_following(u);
if (following) {
return unit_start(following);
}
/* If it is stopped, but we cannot start it, then fail */
if (!UNIT_VTABLE(u)->start)
return -EBADR;
/* We don't suppress calls to ->start() here when we are
* already starting, to allow this request to be used as a
* "hurry up" call, for example when the unit is in some "auto
* restart" state where it waits for a holdoff timer to elapse
* before it will start again. */
return UNIT_VTABLE(u)->start(u);
}
assert(u);
if (u->load_state != UNIT_LOADED)
return false;
if (!unit_supported(u))
return false;
return !!UNIT_VTABLE(u)->start;
}
assert(u);
return unit_can_start(u) &&
u->allow_isolate;
}
/* Errors:
* -EBADR: This unit type does not support stopping.
* -EALREADY: Unit is already stopped.
* -EAGAIN: An operation is already in progress. Retry later.
*/
assert(u);
state = unit_active_state(u);
return -EALREADY;
following = unit_following(u);
if (following) {
}
if (!UNIT_VTABLE(u)->stop)
return -EBADR;
return UNIT_VTABLE(u)->stop(u);
}
/* Errors:
* -EBADR: This unit type does not support reloading.
* -ENOEXEC: Unit is not started.
* -EAGAIN: An operation is already in progress. Retry later.
*/
assert(u);
if (u->load_state != UNIT_LOADED)
return -EINVAL;
if (!unit_can_reload(u))
return -EBADR;
state = unit_active_state(u);
if (state == UNIT_RELOADING)
return -EALREADY;
if (state != UNIT_ACTIVE) {
log_unit_warning(u, "Unit cannot be reloaded because it is inactive.");
return -ENOEXEC;
}
following = unit_following(u);
if (following) {
return unit_reload(following);
}
return UNIT_VTABLE(u)->reload(u);
}
assert(u);
if (!UNIT_VTABLE(u)->reload)
return false;
if (!UNIT_VTABLE(u)->can_reload)
return true;
return UNIT_VTABLE(u)->can_reload(u);
}
};
Iterator i;
unsigned j;
int r;
assert(u);
/* If this service shall be shut down when unneeded then do
* so. */
if (!u->stop_when_unneeded)
return;
return;
for (j = 0; j < ELEMENTSOF(needed_dependencies); j++)
if (unit_active_or_pending(other))
return;
/* If stopping a unit fails continously we might enter a stop
* loop here, hence stop acting on the service being
* unnecessary after a while. */
if (!ratelimit_test(&u->auto_stop_ratelimit)) {
log_unit_warning(u, "Unit not needed anymore, but not stopping since we tried this too often recently.");
return;
}
log_unit_info(u, "Unit not needed anymore. Stopping.");
/* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
if (r < 0)
log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
}
bool stop = false;
Iterator i;
int r;
assert(u);
if (u->job)
return;
if (unit_active_state(u) != UNIT_ACTIVE)
return;
continue;
continue;
stop = true;
break;
}
if (!stop)
return;
/* If stopping a unit fails continously we might enter a stop
* loop here, hence stop acting on the service being
* unnecessary after a while. */
if (!ratelimit_test(&u->auto_stop_ratelimit)) {
log_unit_warning(u, "Unit is bound to inactive unit %s, but not stopping since we tried this too often recently.", other->id);
return;
}
/* A unit we need to run is gone. Sniff. Let's stop this. */
if (r < 0)
log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
}
Iterator i;
assert(u);
}
Iterator i;
assert(u);
/* Pull down units which are bound to us recursively if enabled */
}
Iterator i;
assert(u);
/* Garbage collect services that might not be needed anymore, if enabled */
}
Iterator i;
assert(u);
return;
log_unit_info(u, "Triggering OnFailure= dependencies.");
int r;
if (r < 0)
log_unit_error_errno(u, r, "Failed to enqueue OnFailure= job: %m");
}
}
Iterator i;
assert(u);
}
Manager *m;
bool unexpected;
assert(u);
/* Note that this is called for all low-level state changes,
* even if they might map to the same high-level
* UnitActiveState! That means that ns == os is an expected
* behavior here. For example: if a mount point is remounted
* this function will be called too! */
m = u->manager;
/* Update timestamps for state changes */
if (m->n_reloading <= 0) {
}
/* Keep track of failed units */
/* Make sure the cgroup is always removed when we become inactive */
if (UNIT_IS_INACTIVE_OR_FAILED(ns))
/* Note that this doesn't apply to RemainAfterExit services exiting
* successfully, since there's no change of state in that case. Which is
* why it is handled in service_set_state() */
ec = unit_get_exec_context(u);
if (UNIT_IS_INACTIVE_OR_FAILED(ns)) {
m->n_on_console --;
if (m->n_on_console == 0)
/* unset no_console_output flag, since the console is free */
m->no_console_output = false;
} else
m->n_on_console ++;
}
}
if (u->job) {
unexpected = false;
/* So we reached a different state for this
* job. Let's see if we can run it now if it
* failed previously due to EAGAIN. */
job_add_to_run_queue(u->job);
/* Let's check whether this state change constitutes a
* finished job, or maybe contradicts a running job and
* hence needs to invalidate jobs. */
case JOB_START:
case JOB_VERIFY_ACTIVE:
unexpected = true;
if (UNIT_IS_INACTIVE_OR_FAILED(ns))
}
break;
case JOB_RELOAD:
case JOB_RELOAD_OR_START:
case JOB_TRY_RELOAD:
if (ns == UNIT_ACTIVE)
unexpected = true;
if (UNIT_IS_INACTIVE_OR_FAILED(ns))
}
}
break;
case JOB_STOP:
case JOB_RESTART:
case JOB_TRY_RESTART:
if (UNIT_IS_INACTIVE_OR_FAILED(ns))
unexpected = true;
}
break;
default:
assert_not_reached("Job type unknown");
}
} else
unexpected = true;
if (m->n_reloading <= 0) {
/* If this state change happened without being
* requested by a job, then let's retroactively start
* or stop dependencies. We skip that step when
* deserializing, since we don't want to create any
* additional jobs just because something is already
* activated. */
if (unexpected) {
}
/* stop unneeded units regardless if going down was expected or not */
log_unit_notice(u, "Unit entered failed state.");
}
}
/* Some names are special */
if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
if (unit_has_name(u, SPECIAL_DBUS_SERVICE))
/* The bus might have just become available,
* hence try to connect to it, if we aren't
* yet connected. */
bus_init(m, true);
if (u->type == UNIT_SERVICE &&
m->n_reloading <= 0) {
/* Write audit record if we have just finished starting up */
manager_send_unit_audit(m, u, AUDIT_SERVICE_START, true);
u->in_audit = true;
}
if (!UNIT_IS_ACTIVE_OR_RELOADING(os))
manager_send_unit_plymouth(m, u);
} else {
/* We don't care about D-Bus here, since we'll get an
* asynchronous notification for it anyway. */
if (u->type == UNIT_SERVICE &&
m->n_reloading <= 0) {
/* Hmm, if there was no start record written
* write it now, so that we always have a nice
* pair */
if (!u->in_audit) {
if (ns == UNIT_INACTIVE)
manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, true);
} else
/* Write audit record if we have just finished shutting down */
u->in_audit = false;
}
}
if (u->manager->n_reloading <= 0) {
/* Maybe we finished startup and are now ready for
* being stopped because unneeded? */
/* Maybe we finished startup, but something we needed
* has vanished? Let's die then. (This happens when
* something BindsTo= to a Type=oneshot unit, as these
* units go directly from starting to inactive,
* without ever entering started.) */
}
}
int q, r;
assert(u);
/* Watch a specific PID. We only support one or two units
* watching each PID for now, not more. */
if (r < 0)
return r;
if (r < 0)
return r;
if (r == -EEXIST) {
if (r < 0)
return r;
}
if (q < 0)
return q;
return r;
}
assert(u);
}
assert(u);
while (!set_isempty(u->pids))
}
Iterator i;
void *e;
assert(u);
/* Cleans dead PIDs from our list */
SET_FOREACH(e, u->pids, i) {
continue;
if (!pid_is_unwaited(pid))
unit_unwatch_pid(u, pid);
}
}
assert(u);
assert(j >= 0 && j < _JOB_TYPE_MAX);
switch (j) {
case JOB_VERIFY_ACTIVE:
case JOB_START:
case JOB_STOP:
case JOB_NOP:
return true;
case JOB_RESTART:
case JOB_TRY_RESTART:
return unit_can_start(u);
case JOB_RELOAD:
case JOB_TRY_RELOAD:
return unit_can_reload(u);
case JOB_RELOAD_OR_START:
return unit_can_reload(u) && unit_can_start(u);
default:
assert_not_reached("Invalid job type");
}
}
assert(u);
/* Only warn about some unit types */
if (!IN_SET(dependency, UNIT_CONFLICTS, UNIT_CONFLICTED_BY, UNIT_BEFORE, UNIT_AFTER, UNIT_ON_FAILURE, UNIT_TRIGGERS, UNIT_TRIGGERED_BY))
return;
else
log_unit_warning(u, "Dependency %s=%s dropped, merged into %s", unit_dependency_to_string(dependency), strna(other), u->id);
}
[UNIT_WANTS] = UNIT_WANTED_BY,
[UNIT_WANTED_BY] = UNIT_WANTS,
[UNIT_BEFORE] = UNIT_AFTER,
[UNIT_AFTER] = UNIT_BEFORE,
};
int r, q = 0, v = 0, w = 0;
assert(u);
assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
u = unit_follow_merge(u);
/* We won't allow dependencies on ourselves. We will not
* consider them an error however. */
if (u == other) {
return 0;
}
if (r < 0)
return r;
if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) {
if (r < 0)
return r;
}
if (add_reference) {
if (r < 0)
return r;
if (r < 0)
return r;
}
if (q < 0)
return q;
if (v < 0) {
r = v;
goto fail;
}
}
if (add_reference) {
if (w < 0) {
r = w;
goto fail;
}
if (r < 0)
goto fail;
}
return 0;
fail:
if (q > 0)
if (v > 0)
if (w > 0)
return r;
}
int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) {
int r;
assert(u);
if (r < 0)
return r;
}
static int resolve_template(Unit *u, const char *name, const char*path, char **buf, const char **ret) {
int r;
assert(u);
if (!name)
return 0;
}
if (u->instance)
else {
_cleanup_free_ char *i = NULL;
r = unit_name_to_prefix(u->id, &i);
if (r < 0)
return r;
}
if (r < 0)
return r;
return 0;
}
int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
int r;
assert(u);
if (r < 0)
return r;
if (r < 0)
return r;
}
int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
int r;
assert(u);
if (r < 0)
return r;
if (r < 0)
return r;
}
int set_unit_path(const char *p) {
/* This is mostly for debug purposes */
return -errno;
return 0;
}
assert(u);
if (!u->id)
return NULL;
return unit_dbus_path_from_name(u->id);
}
assert(u);
/* Sets the unit slice if it has not been set before. Is extra
* careful, to only allow this for units that actually have a
* cgroup context. Also, we don't allow to set this for slices
* (since the parent slice is derived from the name). Make
* sure the unit we set is actually a slice. */
if (!UNIT_HAS_CGROUP_CONTEXT(u))
return -EOPNOTSUPP;
if (u->type == UNIT_SLICE)
return -EINVAL;
if (unit_active_state(u) != UNIT_INACTIVE)
return -EBUSY;
return -EINVAL;
if (unit_has_name(u, SPECIAL_INIT_SCOPE) &&
return -EPERM;
return 0;
if (UNIT_ISSET(u->slice))
return -EBUSY;
return 1;
}
_cleanup_free_ char *b = NULL;
const char *slice_name;
int r;
assert(u);
if (UNIT_ISSET(u->slice))
return 0;
if (u->instance) {
/* Implicitly place all instantiated units in their
* own per-template slice */
if (r < 0)
return r;
/* The prefix is already escaped, but it might include
* "-" which has a special meaning for slice units,
* hence escape it here extra. */
if (!escaped)
return -ENOMEM;
else
if (!b)
return -ENOMEM;
slice_name = b;
} else
if (r < 0)
return r;
return unit_set_slice(u, slice);
}
assert(u);
if (!UNIT_ISSET(u->slice))
return NULL;
}
_cleanup_free_ char *t = NULL;
int r;
assert(u);
if (r < 0)
return r;
if (unit_has_name(u, t))
return -EINVAL;
return r;
}
static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
int r;
assert(u);
if (r < 0) {
return 0;
}
if (UNIT_VTABLE(u)->bus_name_owner_change)
return 0;
}
const char *match;
assert(u);
if (u->match_bus_slot)
return -EBUSY;
"sender='org.freedesktop.DBus',"
"path='/org/freedesktop/DBus',"
"interface='org.freedesktop.DBus',"
"member='NameOwnerChanged',"
NULL);
}
int r;
assert(u);
/* Watch a specific name on the bus. We only support one unit
* watching each name for now. */
/* If the bus is already available, install the match directly.
* Otherwise, just put the name in the list. bus_setup_api() will take care later. */
if (r < 0)
}
if (r < 0) {
return log_warning_errno(r, "Failed to put bus name to hashmap: %m");
}
return 0;
}
assert(u);
}
assert(u);
}
int r;
assert(u);
assert(f);
if (unit_can_serialize(u)) {
if (r < 0)
return r;
rt = unit_get_exec_runtime(u);
if (rt) {
if (r < 0)
return r;
}
}
if (dual_timestamp_is_set(&u->condition_timestamp))
if (dual_timestamp_is_set(&u->assert_timestamp))
if (u->cgroup_path)
if (serialize_jobs) {
if (u->job) {
fprintf(f, "job\n");
}
if (u->nop_job) {
fprintf(f, "job\n");
}
}
/* End marker */
fputc('\n', f);
return 0;
}
assert(u);
assert(f);
if (!value)
return 0;
fputc('=', f);
fputc('\n', f);
return 1;
}
_cleanup_free_ char *c = NULL;
assert(u);
assert(f);
if (!value)
return 0;
if (!c)
return -ENOMEM;
fputc('=', f);
fputs(c, f);
fputc('\n', f);
return 1;
}
int copy;
assert(u);
assert(f);
if (fd < 0)
return 0;
if (copy < 0)
return copy;
return 1;
}
assert(u);
assert(f);
fputc('=', f);
fputc('\n', f);
}
int r;
assert(u);
assert(f);
if (offset > 0)
for (;;) {
size_t k;
if (feof(f))
return 0;
return -errno;
}
/* End marker */
if (isempty(l))
break;
k = strcspn(l, "=");
if (l[k] == '=') {
l[k] = 0;
v = l+k+1;
} else
v = l+k;
if (streq(l, "job")) {
if (v[0] == '\0') {
/* new-style serialized job */
Job *j;
j = job_new_raw(u);
if (!j)
return log_oom();
r = job_deserialize(j, f, fds);
if (r < 0) {
job_free(j);
return r;
}
if (r < 0) {
job_free(j);
return r;
}
r = job_install_deserialized(j);
if (r < 0) {
job_free(j);
return r;
}
} else /* legacy for pre-44 */
log_unit_warning(u, "Update from too old systemd versions are unsupported, cannot deserialize job: %s", v);
continue;
} else if (streq(l, "state-change-timestamp")) {
continue;
} else if (streq(l, "inactive-exit-timestamp")) {
continue;
} else if (streq(l, "active-enter-timestamp")) {
continue;
} else if (streq(l, "active-exit-timestamp")) {
continue;
} else if (streq(l, "inactive-enter-timestamp")) {
continue;
} else if (streq(l, "condition-timestamp")) {
continue;
} else if (streq(l, "assert-timestamp")) {
continue;
} else if (streq(l, "condition-result")) {
r = parse_boolean(v);
if (r < 0)
log_unit_debug(u, "Failed to parse condition result value %s, ignoring.", v);
else
u->condition_result = r;
continue;
} else if (streq(l, "assert-result")) {
r = parse_boolean(v);
if (r < 0)
log_unit_debug(u, "Failed to parse assert result value %s, ignoring.", v);
else
u->assert_result = r;
continue;
} else if (streq(l, "transient")) {
r = parse_boolean(v);
if (r < 0)
log_unit_debug(u, "Failed to parse transient bool %s, ignoring.", v);
else
u->transient = r;
continue;
} else if (streq(l, "cpuacct-usage-base")) {
r = safe_atou64(v, &u->cpuacct_usage_base);
if (r < 0)
log_unit_debug(u, "Failed to parse CPU usage %s, ignoring.", v);
continue;
} else if (streq(l, "cgroup")) {
r = unit_set_cgroup_path(u, v);
if (r < 0)
log_unit_debug_errno(u, r, "Failed to set cgroup path %s, ignoring: %m", v);
(void) unit_watch_cgroup(u);
continue;
} else if (streq(l, "cgroup-realized")) {
int b;
b = parse_boolean(v);
if (b < 0)
log_unit_debug(u, "Failed to parse cgroup-realized bool %s, ignoring.", v);
else
u->cgroup_realized = b;
continue;
}
if (unit_can_serialize(u)) {
if (rt) {
if (r < 0) {
log_unit_warning(u, "Failed to deserialize runtime parameter '%s', ignoring.", l);
continue;
}
/* Returns positive if key was handled by the call */
if (r > 0)
continue;
}
if (r < 0)
log_unit_warning(u, "Failed to deserialize unit parameter '%s', ignoring.", l);
}
}
/* Versions before 228 did not carry a state change timestamp. In this case, take the current time. This is
* useful, so that timeouts based on this timestamp don't trigger too early, and is in-line with the logic from
* before 228 where the base for timeouts was not persistent across reboots. */
if (!dual_timestamp_is_set(&u->state_change_timestamp))
return 0;
}
_cleanup_free_ char *e = NULL;
int r;
assert(u);
/* Adds in links to the device node that this unit is based on */
return 0;
if (!is_device_path(what))
return 0;
/* When device units aren't supported (such as in a
* container), don't create dependencies on them. */
if (!unit_type_supported(UNIT_DEVICE))
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
r = unit_add_two_dependencies(u, UNIT_AFTER,
device, true);
if (r < 0)
return r;
if (wants) {
if (r < 0)
return r;
}
return 0;
}
int r = 0, q = 0;
assert(u);
/* Make sure we don't enter a loop, when coldplugging
* recursively. */
if (u->coldplugged)
return 0;
u->coldplugged = true;
if (UNIT_VTABLE(u)->coldplug)
r = UNIT_VTABLE(u)->coldplug(u);
if (u->job)
q = job_coldplug(u->job);
if (r < 0)
return r;
if (q < 0)
return q;
return 0;
}
_cleanup_strv_free_ char **t = NULL;
char **path;
assert(u);
if (u->fragment_path) {
/* What, cannot access this anymore? */
return true;
if (u->fragment_mtime > 0 &&
return true;
}
if (u->source_path) {
return true;
if (u->source_mtime > 0 &&
return true;
}
(void) unit_find_dropin_paths(u, &t);
loaded_cnt = strv_length(t);
if (loaded_cnt == current_cnt) {
if (loaded_cnt == 0)
return false;
if (strv_overlap(u->dropin_paths, t)) {
return true;
if (u->dropin_mtime > 0 &&
return true;
}
return false;
} else
return true;
} else
return true;
}
assert(u);
if (UNIT_VTABLE(u)->reset_failed)
UNIT_VTABLE(u)->reset_failed(u);
u->start_limit_hit = false;
}
assert(u);
if (UNIT_VTABLE(u)->following)
return UNIT_VTABLE(u)->following(u);
return NULL;
}
assert(u);
/* This call does check the current state of the unit. It's
* hence useful to be called from state change calls of the
* unit itself, where the state isn't updated yet. This is
* different from unit_inactive_or_pending() which checks both
* the current state and for a queued job. */
}
assert(u);
/* Returns true if the unit is inactive or going down */
return true;
if (unit_stop_pending(u))
return true;
return false;
}
assert(u);
/* Returns true if the unit is active or going up */
return true;
if (u->job &&
return true;
return false;
}
assert(u);
assert(w >= 0 && w < _KILL_WHO_MAX);
if (!UNIT_VTABLE(u)->kill)
return -EOPNOTSUPP;
}
int r;
if (!pid_set)
return NULL;
if (main_pid > 0) {
if (r < 0)
goto fail;
}
if (control_pid > 0) {
if (r < 0)
goto fail;
}
return pid_set;
fail:
return NULL;
}
int unit_kill_common(
Unit *u,
int signo,
sd_bus_error *error) {
int r = 0;
bool killed = false;
if (main_pid < 0)
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type));
else if (main_pid == 0)
}
if (control_pid < 0)
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type));
else if (control_pid == 0)
}
if (control_pid > 0) {
r = -errno;
else
killed = true;
}
if (main_pid > 0) {
r = -errno;
else
killed = true;
}
int q;
if (!pid_set)
return -ENOMEM;
q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, false, false, false, pid_set);
r = q;
else
killed = true;
}
return -ESRCH;
return r;
}
assert(u);
assert(s);
if (UNIT_VTABLE(u)->following_set)
return UNIT_VTABLE(u)->following_set(u, s);
*s = NULL;
return 0;
}
int r;
assert(u);
if (u->unit_file_state < 0 && u->fragment_path) {
r = unit_file_get_state(
NULL,
basename(u->fragment_path),
&u->unit_file_state);
if (r < 0)
u->unit_file_state = UNIT_FILE_BAD;
}
return u->unit_file_state;
}
assert(u);
if (u->unit_file_preset < 0 && u->fragment_path)
NULL,
basename(u->fragment_path));
return u->unit_file_preset;
}
assert(u);
return u;
}
return;
}
unsigned i;
int r;
assert(u);
/* Patch in the manager defaults into the exec and cgroup
* contexts, _after_ the rest of the settings have been
* initialized */
ec = unit_get_exec_context(u);
if (ec) {
/* This only copies in the ones that need memory */
for (i = 0; i < _RLIMIT_MAX; i++)
return -ENOMEM;
}
!ec->working_directory) {
if (r < 0)
return r;
/* Allow user services to run, even if the
* home directory is missing */
ec->working_directory_missing_ok = true;
}
(ec->syscall_whitelist ||
ec->no_new_privileges = true;
if (ec->private_devices)
}
cc = unit_get_cgroup_context(u);
if (cc) {
if (ec &&
ec->private_devices &&
}
return 0;
}
assert(u);
if (u->type < 0)
return NULL;
if (offset <= 0)
return NULL;
}
assert(u);
if (u->type < 0)
return NULL;
if (offset <= 0)
return NULL;
}
if (u->type < 0)
return NULL;
if (offset <= 0)
return NULL;
}
if (u->type < 0)
return NULL;
if (offset <= 0)
return NULL;
}
assert(u);
int r;
r = user_config_home(dir);
else
r = user_runtime_dir(dir);
if (r == 0)
return -ENOENT;
return r;
}
else
if (!*dir)
return -ENOMEM;
return 0;
}
int r;
assert(u);
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = strv_extend(&u->dropin_paths, q);
if (r < 0)
return r;
strv_sort(u->dropin_paths);
strv_uniq(u->dropin_paths);
return 0;
}
int unit_write_drop_in_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) {
_cleanup_free_ char *p = NULL;
int r;
assert(u);
return 0;
if (r < 0)
return -ENOMEM;
}
int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) {
assert(u);
if (!UNIT_VTABLE(u)->private_section)
return -EINVAL;
return 0;
if (!ndata)
return -ENOMEM;
}
int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) {
_cleanup_free_ char *p = NULL;
int r;
assert(u);
return 0;
if (r < 0)
return -ENOMEM;
}
assert(u);
if (!UNIT_VTABLE(u)->can_transient)
return -EOPNOTSUPP;
u->load_state = UNIT_STUB;
u->load_error = 0;
u->transient = true;
return 0;
}
int unit_kill_context(
Unit *u,
KillContext *c,
bool main_pid_alien) {
bool wait_for_exit = false;
int sig, r;
assert(u);
assert(c);
return 0;
switch (k) {
case KILL_KILL:
break;
case KILL_ABORT:
break;
case KILL_TERMINATE:
sig = c->kill_signal;
break;
default:
assert_not_reached("KillOperation unknown");
}
if (main_pid > 0) {
if (r < 0 && r != -ESRCH) {
log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid, strna(comm));
} else {
if (!main_pid_alien)
wait_for_exit = true;
if (c->send_sighup && k == KILL_TERMINATE)
}
}
if (control_pid > 0) {
if (r < 0 && r != -ESRCH) {
log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid, strna(comm));
} else {
wait_for_exit = true;
if (c->send_sighup && k == KILL_TERMINATE)
}
}
if (u->cgroup_path &&
if (!pid_set)
return -ENOMEM;
r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, sig, true, k != KILL_TERMINATE, false, pid_set);
if (r < 0) {
} else if (r > 0) {
/* FIXME: For now, on the legacy hierarchy, we
* will not wait for the cgroup members to die
* if we are running in a container or if this
* is a delegation unit, simply because cgroup
* notification is unreliable in these
* cases. It doesn't work at all in
* containers, and outside of containers it
* can be confused easily by left-over
* directories in the cgroup -- which however
* should not exist in non-delegated units. On
* the unified hierarchy that's different,
* there we get proper events. Hence rely on
* them.*/
if (cg_unified() > 0 ||
(detect_container() == 0 && !unit_cgroup_delegate(u)))
wait_for_exit = true;
if (c->send_sighup && k != KILL_KILL) {
if (!pid_set)
return -ENOMEM;
}
}
}
return wait_for_exit;
}
int r;
assert(u);
/* Registers a unit for requiring a certain path and all its
* prefixes. We keep a simple array of these paths in the
* unit, since its usually short. However, we build a prefix
* table for all possible prefixes so that new appearing mount
* units can easily determine which units to make themselves a
* dependency of. */
if (!path_is_absolute(path))
return -EINVAL;
if (!p)
return -ENOMEM;
if (!path_is_safe(p)) {
free(p);
return -EPERM;
}
if (strv_contains(u->requires_mounts_for, p)) {
free(p);
return 0;
}
r = strv_consume(&u->requires_mounts_for, p);
if (r < 0)
return r;
Set *x;
if (!x) {
char *q;
if (r < 0)
return r;
if (!q)
return -ENOMEM;
if (!x) {
free(q);
return -ENOMEM;
}
if (r < 0) {
free(q);
set_free(x);
return r;
}
}
r = set_put(x, u);
if (r < 0)
return r;
}
return 0;
}
Iterator i;
/* Check if there already is an ExecRuntime for this unit? */
if (*rt)
return 0;
/* Try to get it from somebody else */
if (*rt) {
return 0;
}
}
}
if (_unlikely_(t < 0))
return false;
if (_unlikely_(t >= _UNIT_TYPE_MAX))
return false;
if (!unit_vtable[t]->supported)
return true;
return unit_vtable[t]->supported();
}
int r;
assert(u);
r = dir_is_empty(where);
if (r > 0)
return;
if (r < 0) {
return;
}
LOG_UNIT_ID(u),
"WHERE=%s", where,
NULL);
}
int r;
assert(u);
r = is_symlink(where);
if (r < 0) {
return 0;
}
if (r == 0)
return 0;
LOG_UNIT_ID(u),
"WHERE=%s", where,
NULL);
return -ELOOP;
}
assert(u);
/* Check if the unit already exists or is already around,
* in a number of different ways. Note that to cater for unit
* types such as slice, we are generally fine with units that
* are marked UNIT_LOADED even even though nothing was
* actually loaded, as those unit types don't require a file
* on disk to validly load. */
u->fragment_path ||
u->source_path ||
!strv_isempty(u->dropin_paths) ||
u->job ||
u->merged_into);
}