unit.c revision cba6e0627d659a33df31aaa74d0d701c4bc9f317
/*-*- 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 <stdlib.h>
#include <unistd.h>
#include "set.h"
#include "unit.h"
#include "macro.h"
#include "strv.h"
#include "path-util.h"
#include "load-fragment.h"
#include "load-dropin.h"
#include "log.h"
#include "unit-name.h"
#include "specifier.h"
#include "dbus-unit.h"
#include "special.h"
#include "cgroup-util.h"
#include "missing.h"
#include "cgroup-attr.h"
[UNIT_SERVICE] = &service_vtable,
[UNIT_TIMER] = &timer_vtable,
[UNIT_SOCKET] = &socket_vtable,
[UNIT_TARGET] = &target_vtable,
[UNIT_DEVICE] = &device_vtable,
[UNIT_MOUNT] = &mount_vtable,
[UNIT_SNAPSHOT] = &snapshot_vtable,
[UNIT_SWAP] = &swap_vtable,
[UNIT_PATH] = &path_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;
return u;
}
assert(u);
}
UnitType t;
char *s, *i = NULL;
int r;
assert(u);
if (unit_name_is_template(text)) {
if (!u->instance)
return -EINVAL;
} else
if (!s)
return -ENOMEM;
if (!unit_name_is_valid(s, false)) {
r = -EINVAL;
goto fail;
}
assert_se((t = unit_name_to_type(s)) >= 0);
r = -EINVAL;
goto fail;
}
if ((r = unit_name_to_instance(s, &i)) < 0)
goto fail;
if (i && unit_vtable[t]->no_instances) {
r = -EINVAL;
goto fail;
}
/* Ensure that this unit is either instanced or not instanced,
* but not both. */
r = -EINVAL;
goto fail;
}
if (unit_vtable[t]->no_alias &&
!set_isempty(u->names) &&
r = -EEXIST;
goto fail;
}
r = -E2BIG;
goto fail;
}
if (r == -EEXIST)
r = 0;
goto fail;
}
set_remove(u->names, s);
goto fail;
}
if (u->type == _UNIT_TYPE_INVALID) {
u->type = t;
u->id = s;
u->instance = i;
if (UNIT_VTABLE(u)->init)
UNIT_VTABLE(u)->init(u);
} else
free(i);
return 0;
fail:
free(s);
free(i);
return r;
}
char *s, *t = NULL, *i;
int r;
assert(u);
if (unit_name_is_template(name)) {
if (!u->instance)
return -EINVAL;
return -ENOMEM;
name = t;
}
/* Selects one of the names of this unit as the id */
free(t);
if (!s)
return -ENOENT;
if ((r = unit_name_to_instance(s, &i)) < 0)
return r;
u->id = s;
u->instance = i;
return 0;
}
char *s;
assert(u);
if (!(s = strdup(description)))
return -ENOMEM;
free(u->description);
u->description = s;
return 0;
}
bool unit_check_gc(Unit *u) {
assert(u);
if (u->load_state == UNIT_STUB)
return true;
if (UNIT_VTABLE(u)->no_gc)
return true;
if (u->no_gc)
return true;
if (u->job)
return true;
if (u->nop_job)
return true;
if (unit_active_state(u) != UNIT_INACTIVE)
return true;
if (UNIT_VTABLE(u)->check_gc)
if (UNIT_VTABLE(u)->check_gc(u))
return true;
return false;
}
void unit_add_to_load_queue(Unit *u) {
assert(u);
return;
u->in_load_queue = true;
}
void unit_add_to_cleanup_queue(Unit *u) {
assert(u);
if (u->in_cleanup_queue)
return;
u->in_cleanup_queue = true;
}
void unit_add_to_gc_queue(Unit *u) {
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 ++;
if (u->manager->gc_queue_timestamp <= 0)
}
void unit_add_to_dbus_queue(Unit *u) {
assert(u);
return;
/* Shortcut things if nobody cares */
if (!bus_has_subscriber(u->manager)) {
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 */
SET_FOREACH(other, s, i) {
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
}
set_free(s);
}
Iterator i;
char *t;
assert(u);
if (u->load_state != UNIT_STUB)
if (UNIT_VTABLE(u)->done)
UNIT_VTABLE(u)->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->requires_mounts_for) {
}
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--;
}
free(u->description);
strv_free(u->documentation);
free(u->fragment_path);
free(u->source_path);
set_free_free(u->names);
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);
}
const char* unit_sub_state_to_string(Unit *u) {
assert(u);
return UNIT_VTABLE(u)->sub_state_to_string(u);
}
assert(s);
if (!*other)
return;
if (*s)
else {
*s = *other;
}
}
char *t;
Iterator i;
assert(u);
SET_FOREACH(t, u->names, i)
}
Iterator i;
int r;
assert(u);
assert(d < _UNIT_DEPENDENCY_MAX);
/* Fix backwards pointers */
for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
if (r == -EEXIST)
else
}
}
}
assert(u);
if (other == u)
return 0;
return -EINVAL;
return -EINVAL;
return -EEXIST;
return -EEXIST;
return -EEXIST;
return -EEXIST;
/* Merge names */
merge_names(u, other);
/* Redirect all references */
/* Merge dependencies */
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
merge_dependencies(u, other, 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;
char *s = NULL;
assert(u);
if (unit_name_is_template(name)) {
if (!u->instance)
return -EINVAL;
return -ENOMEM;
name = s;
}
r = unit_add_name(u, name);
else
r = unit_merge(u, other);
free(s);
return r;
}
assert(u);
while (u->load_state == UNIT_MERGED)
assert_se(u = u->merged_into);
return u;
}
int r;
assert(u);
assert(c);
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 = unit_add_two_dependencies_by_name(u, UNIT_REQUIRES, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, NULL, true)) < 0)
return r;
return 0;
}
const char *unit_description(Unit *u) {
assert(u);
if (u->description)
return u->description;
}
char *t, **j;
Iterator i;
char *p2;
const char *prefix2;
char
assert(u);
if (!prefix)
prefix = "";
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\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",
prefix, unit_description(u),
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)),
SET_FOREACH(t, u->names, i)
STRV_FOREACH(j, u->documentation)
if ((following = unit_following(u)))
if (u->fragment_path)
if (u->source_path)
if (u->job_timeout > 0)
fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout));
if (dual_timestamp_is_set(&u->condition_timestamp))
fprintf(f,
"%s\tCondition Timestamp: %s\n"
"%s\tCondition 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) {
CGroupBonding *b;
CGroupAttribute *a;
fprintf(f,
"%s\tStopWhenUnneeded: %s\n"
"%s\tRefuseManualStart: %s\n"
"%s\tRefuseManualStop: %s\n"
"%s\tDefaultDependencies: %s\n"
"%s\tOnFailureIsolate: %s\n"
"%s\tIgnoreOnIsolate: %s\n"
"%s\tIgnoreOnSnapshot: %s\n",
fprintf(f, "%s\tControlGroup: %s:%s\n",
char *v = NULL;
if (a->map_callback)
fprintf(f, "%s\tControlGroupAttribute: %s %s \"%s\"\n",
free(v);
}
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 unit_load_fragment_and_dropin(Unit *u) {
int r;
assert(u);
/* Load a .service file */
if ((r = unit_load_fragment(u)) < 0)
return r;
if (u->load_state == UNIT_STUB)
return -ENOENT;
/* Load drop-in directory data */
if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
return r;
return 0;
}
/* Common implementation for multiple backends */
int unit_load_fragment_and_dropin_optional(Unit *u) {
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 */
if ((r = unit_load_fragment(u)) < 0)
return r;
if (u->load_state == UNIT_STUB)
u->load_state = UNIT_LOADED;
/* Load drop-in directory data */
if ((r = unit_load_dropin(unit_follow_merge(u))) < 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;
}
static int unit_add_default_dependencies(Unit *u) {
static const UnitDependency deps[] = {
};
Iterator i;
int r;
unsigned k;
assert(u);
for (k = 0; k < ELEMENTSOF(deps); k++)
if ((r = unit_add_default_target_dependency(u, target)) < 0)
return r;
return 0;
}
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)
if ((r = UNIT_VTABLE(u)->load(u)) < 0)
goto fail;
if (u->load_state == UNIT_STUB) {
r = -ENOENT;
goto fail;
}
if (u->load_state == UNIT_LOADED &&
if ((r = unit_add_default_dependencies(u)) < 0)
goto fail;
if (u->load_state == UNIT_LOADED) {
r = unit_add_mount_links(u);
if (r < 0)
return r;
}
if (u->on_failure_isolate &&
log_error("More than one OnFailure= dependencies specified for %s but OnFailureIsolate= enabled. Refusing.",
u->id);
r = -EINVAL;
goto fail;
}
return 0;
fail:
u->load_state = UNIT_ERROR;
u->load_error = r;
return r;
}
bool unit_condition_test(Unit *u) {
assert(u);
return u->condition_result;
}
const UnitStatusMessageFormats *format_table;
const char *format;
if (!format_table)
return;
if (!format)
return;
}
/* 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.
*/
int unit_start(Unit *u) {
assert(u);
if (u->load_state != UNIT_LOADED)
return -EINVAL;
/* 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;
/* 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)) {
return -EALREADY;
}
/* Forward to the main object, if we aren't it. */
if ((following = unit_following(u))) {
return unit_start(following);
}
unit_status_print_starting_stopping(u, false);
/* 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);
}
bool unit_can_start(Unit *u) {
assert(u);
return !!UNIT_VTABLE(u)->start;
}
bool unit_can_isolate(Unit *u) {
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;
if ((following = unit_following(u))) {
}
unit_status_print_starting_stopping(u, true);
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.
*/
int unit_reload(Unit *u) {
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)
return -ENOEXEC;
if ((following = unit_following(u))) {
return unit_reload(following);
}
return UNIT_VTABLE(u)->reload(u);
}
bool unit_can_reload(Unit *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);
}
static void unit_check_unneeded(Unit *u) {
Iterator i;
assert(u);
/* If this service shall be shut down when unneeded then do
* so. */
if (!u->stop_when_unneeded)
return;
return;
if (unit_pending_active(other))
return;
if (unit_pending_active(other))
return;
if (unit_pending_active(other))
return;
if (unit_pending_active(other))
return;
/* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
}
static void retroactively_start_dependencies(Unit *u) {
Iterator i;
assert(u);
}
static void retroactively_stop_dependencies(Unit *u) {
Iterator i;
assert(u);
/* Pull down units which are bound to us recursively if enabled */
}
static void check_unneeded_dependencies(Unit *u) {
Iterator i;
assert(u);
/* Garbage collect services that might not be needed anymore, if enabled */
}
void unit_trigger_on_failure(Unit *u) {
Iterator i;
assert(u);
return;
int r;
if ((r = manager_add_job(u->manager, JOB_START, other, u->on_failure_isolate ? JOB_ISOLATE : JOB_REPLACE, true, NULL, NULL)) < 0)
}
}
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 OK an expected
* behaviour here. For example: if a mount point is remounted
* this function will be called too! */
if (u->manager->n_reloading <= 0) {
u->inactive_exit_timestamp = ts;
u->inactive_enter_timestamp = ts;
u->active_enter_timestamp = ts;
u->active_exit_timestamp = ts;
timer_unit_notify(u, ns);
path_unit_notify(u, ns);
}
if (UNIT_IS_INACTIVE_OR_FAILED(ns))
cgroup_bonding_trim_list(u->cgroup_bondings, true);
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:
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 (u->manager->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 */
}
}
/* Some names are special */
if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
if (unit_has_name(u, SPECIAL_DBUS_SERVICE))
/* The bus just might have become available,
* hence try to connect to it, if we aren't
* yet connected. */
if (u->type == UNIT_SERVICE &&
u->manager->n_reloading <= 0) {
/* Write audit record if we have just finished starting up */
u->in_audit = true;
}
if (!UNIT_IS_ACTIVE_OR_RELOADING(os))
manager_send_unit_plymouth(u->manager, 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 &&
u->manager->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)
} else
/* Write audit record if we have just finished shutting down */
u->in_audit = false;
}
}
/* Maybe we finished startup and are now ready for being
* stopped because unneeded? */
}
struct epoll_event ev;
assert(u);
assert(w);
fd,
&ev) < 0)
return -errno;
return 0;
}
assert(u);
assert(w);
if (w->type == WATCH_INVALID)
return;
w->fd = -1;
w->type = WATCH_INVALID;
}
assert(u);
/* Watch a specific PID. We only support one unit watching
* each PID for now. */
}
assert(u);
}
struct itimerspec its;
bool ours;
assert(u);
assert(w);
/* This will try to reuse the old timer if there is one */
if (w->type == WATCH_UNIT_TIMER) {
ours = false;
} else if (w->type == WATCH_INVALID) {
ours = true;
return -errno;
} else
assert_not_reached("Invalid watch type");
if (delay <= 0) {
/* Set absolute time in the past, but not 0, since we
* don't want to disarm the timer */
} else {
flags = 0;
}
/* This will also flush the elapse counter */
goto fail;
if (w->type == WATCH_INVALID) {
struct epoll_event ev;
goto fail;
}
w->type = WATCH_UNIT_TIMER;
return 0;
fail:
if (ours)
return -errno;
}
assert(u);
assert(w);
if (w->type == WATCH_INVALID)
return;
close_nointr_nofail(w->fd);
w->fd = -1;
w->type = WATCH_INVALID;
}
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:
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");
}
}
[UNIT_WANTS] = UNIT_WANTED_BY,
[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;
return r;
if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
if ((r = set_ensure_allocated(&other->dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
return r;
if (add_reference)
if ((r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 ||
(r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0)
return r;
return q;
if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
r = v;
goto fail;
}
if (add_reference) {
r = w;
goto fail;
}
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);
return r;
return r;
return 0;
}
char *s;
assert(u);
if (!name)
if (!unit_name_is_template(name)) {
*p = NULL;
return name;
}
if (u->instance)
else {
char *i;
if (!(i = unit_name_to_prefix(u->id)))
return NULL;
s = unit_name_replace_instance(name, i);
free(i);
}
if (!s)
return NULL;
*p = s;
return s;
}
int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
int r;
char *s;
assert(u);
return -ENOMEM;
goto finish;
free(s);
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;
char *s;
assert(u);
return -ENOMEM;
goto finish;
free(s);
return r;
}
int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
int r;
char *s;
assert(u);
return -ENOMEM;
goto finish;
free(s);
return r;
}
int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
int r;
char *s;
assert(u);
return -ENOMEM;
goto finish;
goto finish;
free(s);
return r;
}
int set_unit_path(const char *p) {
char *cwd, *c;
int r;
/* This is mostly for debug purposes */
if (path_is_absolute(p)) {
if (!(c = strdup(p)))
return -ENOMEM;
} else {
if (!(cwd = get_current_dir_name()))
return -errno;
if (r < 0)
return -ENOMEM;
}
if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
r = -errno;
free(c);
return r;
}
return 0;
}
char *unit_dbus_path(Unit *u) {
assert(u);
if (!u->id)
return NULL;
return unit_dbus_path_from_name(u->id);
}
int r;
assert(u);
assert(b);
if (!b->controller) {
return -ENOMEM;
b->ours = true;
}
/* Ensure this hasn't been added yet */
CGroupBonding *l;
return r;
}
}
b->unit = u;
return 0;
}
static char *default_cgroup_path(Unit *u) {
char *p;
assert(u);
if (u->instance) {
char *t;
t = unit_name_template(u->id);
if (!t)
return NULL;
free(t);
} else
return p;
}
CGroupBonding *b = NULL;
bool ours = false;
int r;
assert(u);
return r;
if (!path) {
path = default_cgroup_path(u);
ours = true;
}
if (!controller) {
ours = true;
}
if (!path || !controller) {
return -ENOMEM;
}
r = -EEXIST;
goto fail;
}
r = -ENOMEM;
goto fail;
}
b->controller = controller;
if ((r = unit_add_cgroup(u, b)) < 0)
goto fail;
return 0;
fail:
free(b);
return r;
}
CGroupBonding *b = NULL;
int r = -ENOMEM;
assert(u);
if (!controller)
return 0;
return -ENOMEM;
goto fail;
if (!(b->path = default_cgroup_path(u)))
goto fail;
b->ours = true;
if ((r = unit_add_cgroup(u, b)) < 0)
goto fail;
return 0;
fail:
free(b->controller);
free(b);
return r;
}
int unit_add_default_cgroups(Unit *u) {
CGroupAttribute *a;
char **c;
int r;
assert(u);
/* Adds in the default cgroups, if they weren't specified
* otherwise. */
if (!u->manager->cgroup_hierarchy)
return 0;
if ((r = unit_add_one_default_cgroup(u, NULL)) < 0)
return r;
unit_add_one_default_cgroup(u, *c);
return 0;
}
assert(u);
}
int unit_add_cgroup_attribute(Unit *u, const char *controller, const char *name, const char *value, CGroupAttributeMapCallback map_callback) {
int r;
char *c = NULL;
CGroupAttribute *a;
assert(u);
if (!controller) {
const char *dot;
if (!dot)
return -EINVAL;
if (!c)
return -ENOMEM;
controller = c;
}
r = -EINVAL;
goto finish;
}
if (!a) {
r = -ENOMEM;
goto finish;
}
if (c) {
a->controller = c;
c = NULL;
} else
free(a->controller);
free(a);
return -ENOMEM;
}
a->map_callback = map_callback;
r = 0;
free(c);
return r;
}
char *t;
int r;
assert(u);
return -ENOMEM;
assert(!unit_has_name(u, t));
free(t);
return r;
}
char *t;
assert(u);
return -ENOMEM;
assert(!unit_has_name(u, t));
free(t);
if (!found)
return -ENOENT;
return 0;
}
assert(u);
return unit_name_to_prefix_and_instance(u->id);
}
assert(u);
return unit_name_to_prefix(u->id);
}
char *p, *r;
assert(u);
if (!(p = unit_name_to_prefix(u->id)))
return NULL;
r = unit_name_unescape(p);
free(p);
return r;
}
assert(u);
if (u->instance)
return unit_name_unescape(u->instance);
return strdup("");
}
assert(u);
if (u->instance)
return unit_name_path_unescape(u->instance);
return unit_name_to_path(u->instance);
}
assert(u);
return default_cgroup_path(u);
}
char *p;
assert(u);
if (specifier == 'r')
return strdup("");
if (streq(p, "/")) {
free(p);
return strdup("");
}
return p;
}
assert(u);
const char *e;
e = getenv("XDG_RUNTIME_DIR");
if (e)
return strdup(e);
}
return strdup("/run");
}
int r;
const char *username;
/* get USER env from our own env if set */
if (!s->exec_context.user)
return getusername_malloc();
/* fish username from passwd */
if (r < 0)
return NULL;
}
int r;
/* return HOME if set, otherwise from passwd */
if (!s->exec_context.user) {
char *h;
r = get_home_dir(&h);
if (r < 0)
return NULL;
return h;
}
if (r < 0)
return NULL;
}
int r;
/* return HOME if set, otherwise from passwd */
if (!s->exec_context.user) {
char *sh;
if (r < 0)
return sh;
}
if (r < 0)
}
/*
* This will use the passed string as format string and
* replace the following specifiers:
*
* %n: the full id of the unit (foo@bar.waldo)
* %N: the id of the unit without the suffix (foo@bar)
* %p: the prefix (foo)
* %i: the instance (bar)
*/
};
assert(u);
}
/* This is similar to unit_name_printf() but also supports
* unescaping. Also, adds a couple of additional codes:
*
* %c cgroup path of unit
* %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711")
* %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
* %u the username of the configured User or running user
* %h the homedir of the configured User or running user
*/
};
assert(u);
}
char **unit_full_printf_strv(Unit *u, char **l) {
size_t n;
char **r, **i, **j;
/* Applies unit_full_printf to every entry in l */
assert(u);
n = strv_length(l);
if (!(r = new(char*, n+1)))
return NULL;
for (i = l, j = r; *i; i++, j++)
if (!(*j = unit_full_printf(u, *i)))
goto fail;
*j = NULL;
return r;
fail:
for (j--; j >= r; j--)
free(*j);
free(r);
return NULL;
}
assert(u);
/* Watch a specific name on the bus. We only support one unit
* watching each name for now. */
}
assert(u);
}
bool unit_can_serialize(Unit *u) {
assert(u);
}
int r;
assert(u);
assert(f);
if (!unit_can_serialize(u))
return 0;
return r;
if (serialize_jobs) {
if (u->job) {
fprintf(f, "job\n");
}
if (u->nop_job) {
fprintf(f, "job\n");
}
}
if (dual_timestamp_is_set(&u->condition_timestamp))
/* End marker */
fputc('\n', f);
return 0;
}
assert(u);
assert(f);
fputc('=', f);
fputc('\n', f);
}
assert(u);
assert(f);
}
int r;
assert(u);
assert(f);
if (!unit_can_serialize(u))
return 0;
for (;;) {
size_t k;
if (feof(f))
return 0;
return -errno;
}
/* End marker */
if (l[0] == 0)
return 0;
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 = job_new_raw(u);
if (!j)
return -ENOMEM;
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 */
if (type < 0)
log_debug("Failed to parse job type value %s", v);
else
u->deserialized_job = type;
}
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, "condition-result")) {
int b;
if ((b = parse_boolean(v)) < 0)
log_debug("Failed to parse condition result value %s", v);
else
u->condition_result = b;
continue;
}
return r;
}
}
char *e;
int r;
assert(u);
if (!what)
return 0;
/* Adds in links to the device node that this unit is based on */
if (!is_device_path(what))
return 0;
if (!e)
return -ENOMEM;
free(e);
if (r < 0)
return r;
return r;
if (wants)
return r;
return 0;
}
int unit_coldplug(Unit *u) {
int r;
assert(u);
if (UNIT_VTABLE(u)->coldplug)
if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
return r;
if (u->job) {
r = job_coldplug(u->job);
if (r < 0)
return r;
} else if (u->deserialized_job >= 0) {
/* legacy */
r = manager_add_job(u->manager, u->deserialized_job, u, JOB_IGNORE_REQUIREMENTS, false, NULL, NULL);
if (r < 0)
return r;
}
return 0;
}
assert(u);
if (!manager_get_show_status(u->manager))
return;
if (!manager_is_booting_or_shutting_down(u->manager))
return;
}
bool unit_need_daemon_reload(Unit *u) {
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;
}
return false;
}
void unit_reset_failed(Unit *u) {
assert(u);
if (UNIT_VTABLE(u)->reset_failed)
UNIT_VTABLE(u)->reset_failed(u);
}
assert(u);
if (UNIT_VTABLE(u)->following)
return UNIT_VTABLE(u)->following(u);
return NULL;
}
bool unit_pending_inactive(Unit *u) {
assert(u);
/* Returns true if the unit is inactive or going down */
return true;
return true;
return false;
}
bool unit_pending_active(Unit *u) {
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 -ENOTSUP;
}
assert(u);
assert(s);
if (UNIT_VTABLE(u)->following_set)
return UNIT_VTABLE(u)->following_set(u, s);
*s = NULL;
return 0;
}
assert(u);
if (u->unit_file_state < 0 && u->fragment_path)
return u->unit_file_state;
}
assert(u);
return u;
}
return;
}
char **i;
assert(u);
assert(m);
if (u->load_state != UNIT_LOADED ||
return 0;
STRV_FOREACH(i, u->requires_mounts_for) {
if (UNIT(m) == u)
continue;
if (!path_startswith(*i, m->where))
continue;
}
return 0;
}
int unit_add_mount_links(Unit *u) {
int r;
assert(u);
if (r < 0)
return r;
}
return 0;
}
unsigned i;
int r;
assert(u);
assert(c);
/* This only copies in the ones that need memory */
for (i = 0; i < RLIMIT_NLIMITS; i++)
if (!c->rlimit[i])
return -ENOMEM;
}
!c->working_directory) {
r = get_home_dir(&c->working_directory);
if (r < 0)
return r;
}
return 0;
}
static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
[UNIT_ACTIVE] = "active",
[UNIT_RELOADING] = "reloading",
[UNIT_INACTIVE] = "inactive",
[UNIT_FAILED] = "failed",
[UNIT_ACTIVATING] = "activating",
[UNIT_DEACTIVATING] = "deactivating"
};
static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
[UNIT_REQUIRES] = "Requires",
[UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
[UNIT_WANTS] = "Wants",
[UNIT_REQUISITE] = "Requisite",
[UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
[UNIT_REQUIRED_BY] = "RequiredBy",
[UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
[UNIT_BINDS_TO] = "BindsTo",
[UNIT_WANTED_BY] = "WantedBy",
[UNIT_CONFLICTS] = "Conflicts",
[UNIT_CONFLICTED_BY] = "ConflictedBy",
[UNIT_BOUND_BY] = "BoundBy",
[UNIT_BEFORE] = "Before",
[UNIT_AFTER] = "After",
[UNIT_REFERENCES] = "References",
[UNIT_REFERENCED_BY] = "ReferencedBy",
[UNIT_ON_FAILURE] = "OnFailure",
[UNIT_TRIGGERS] = "Triggers",
[UNIT_TRIGGERED_BY] = "TriggeredBy",
[UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo",
[UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom"
};