unit.c revision c1b6628d64e5340d905d974bb95d673a4aae60c1
/*-*- 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 "systemd/sd-id128.h"
#include "systemd/sd-messages.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 "dbus-unit.h"
#include "special.h"
#include "cgroup-util.h"
#include "missing.h"
#include "cgroup-attr.h"
#include "mkdir.h"
#include "label.h"
#include "fileio-label.h"
#include "bus-errors.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 (u->refs)
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);
strv_free(u->dropin_paths);
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 < 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)
STRV_FOREACH(j, u->dropin_paths)
if (u->job_timeout > 0)
fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout, 0));
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",
_cleanup_free_ char *v = NULL;
fprintf(f, "%s\tControlGroupAttribute: %s %s \"%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 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_unit(u->id,
"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;
assert(u);
assert(t >= 0);
assert(t < _JOB_TYPE_MAX);
return NULL;
if (!format_table)
return NULL;
}
const char *format;
assert(u);
assert(t >= 0);
assert(t < _JOB_TYPE_MAX);
format = unit_get_status_message_format(u, t);
if (format)
return format;
/* Return generic strings */
if (t == JOB_START)
return "Starting %s.";
else if (t == JOB_STOP)
return "Stopping %s.";
else if (t == JOB_RELOAD)
return "Reloading %s.";
return NULL;
}
const char *format;
assert(u);
/* We only print status messages for selected units on
* selected operations. */
format = unit_get_status_message_format(u, t);
if (!format)
return;
}
const char *format;
assert(u);
return;
if (log_on_console())
return;
/* We log status messages for all units and all operations. */
if (!format)
return;
t == JOB_STOP ? SD_MESSAGE_UNIT_STOPPING :
u->id,
"MESSAGE=%s", buf,
NULL);
}
/* 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);
}
/* 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))) {
}
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;
r = manager_add_job(u->manager, JOB_START, other, u->on_failure_isolate ? JOB_ISOLATE : JOB_REPLACE, true, NULL, NULL);
if (r < 0)
}
}
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 OK an expected
* behavior here. For example: if a mount point is remounted
* this function will be called too! */
m = u->manager;
if (m->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 (UNIT_IS_INACTIVE_OR_FAILED(ns))
m->n_on_console--;
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:
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_notice_unit(u->id,
"MESSAGE=Unit %s entered failed state.", u->id);
}
}
/* 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. */
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;
}
}
/* Maybe we finished startup and are now ready for being
* stopped because unneeded? */
if (u->manager->n_reloading <= 0)
}
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;
if (fd < 0)
return -errno;
} else
assert_not_reached("Invalid watch type");
if (usec <= 0) {
/* Set absolute time in the past, but not 0, since we
* don't want to disarm the timer */
} else {
}
/* 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);
assert(p);
if (!name)
if (!unit_name_is_template(name)) {
*p = NULL;
return name;
}
if (u->instance)
else {
char *i;
i = unit_name_to_prefix(u->id);
if (!i)
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;
_cleanup_free_ char *s = NULL;
assert(u);
if (!name)
return -ENOMEM;
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;
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) {
_cleanup_free_ char *c = NULL;
/* This is mostly for debug purposes */
c = path_make_absolute_cwd(p);
if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0)
return -errno;
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) {
if (!b->controller)
return log_oom();
b->ours = true;
}
/* Ensure this hasn't been added yet */
CGroupBonding *l;
if (r < 0) {
return r;
}
}
b->unit = u;
return 0;
}
char *unit_default_cgroup_path(Unit *u) {
assert(u);
if (u->instance) {
_cleanup_free_ char *t = NULL;
t = unit_name_template(u->id);
if (!t)
return NULL;
} else
}
CGroupBonding *b = NULL;
bool ours = false;
int r;
assert(u);
if (r < 0)
return r;
if (!path) {
path = unit_default_cgroup_path(u);
ours = true;
}
if (!controller) {
ours = true;
}
if (!path || !controller) {
return log_oom();
}
if (b) {
if (ret)
*ret = b;
return 0;
}
b->realized = false;
if (ret)
*ret = b;
return 1;
}
r = -EEXIST;
b = NULL;
goto fail;
}
if (!b) {
r = -ENOMEM;
goto fail;
}
b->controller = controller;
r = unit_add_cgroup(u, b);
if (r < 0)
goto fail;
if (ret)
*ret = b;
return 1;
fail:
free(b);
return r;
}
CGroupBonding *b = NULL;
int r = -ENOMEM;
assert(u);
if (!controller)
return 0;
if (!b)
return -ENOMEM;
if (!b->controller)
goto fail;
b->path = unit_default_cgroup_path(u);
if (!b->path)
goto fail;
b->ours = true;
r = unit_add_cgroup(u, b);
if (r < 0)
goto fail;
return 1;
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;
r = unit_add_one_default_cgroup(u, NULL);
if (r < 0)
return r;
unit_add_one_default_cgroup(u, *c);
return 0;
}
assert(u);
}
Unit *u,
const CGroupSemantics *semantics,
const char *controller,
const char *name,
const char *value,
CGroupAttribute **ret) {
_cleanup_free_ char *c = NULL;
CGroupAttribute *a;
int r;
assert(u);
if (semantics) {
/* Semantics always take precedence */
if (semantics->controller)
}
if (!name)
return -EINVAL;
if (!controller) {
r = cg_controller_from_attr(name, &c);
if (r < 0)
return -EINVAL;
controller = c;
}
return -EINVAL;
if (!filename_is_safe(name))
return -EINVAL;
if (!filename_is_safe(controller))
return -EINVAL;
/* Check if this attribute already exists. Note that we will
* explicitly check for the value here too, as there are
* attributes which accept multiple values. */
if (a) {
/* Exactly the same value is always OK, let's ignore this */
if (ret)
*ret = a;
return 0;
}
char *v;
/* If this is a single-item entry, we can
* simply patch the existing attribute */
if (!v)
return -ENOMEM;
a->value = v;
if (ret)
*ret = a;
return 1;
}
}
if (!a)
return -ENOMEM;
if (c) {
a->controller = c;
c = NULL;
} else
free(a->controller);
free(a);
return -ENOMEM;
}
a->unit = u;
if (ret)
*ret = a;
return 1;
}
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);
/* 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;
}
if (j->state == JOB_RUNNING)
u->manager->n_running_jobs++;
} 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;
if (r < 0)
return r;
if (wants) {
if (r < 0)
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;
}
}
bool unit_need_daemon_reload(Unit *u) {
_cleanup_strv_free_ char **t = NULL;
char **path;
unsigned loaded_cnt, current_cnt;
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;
}
t = unit_find_dropin_paths(u);
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;
}
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;
}
int unit_kill_common(
Unit *u,
int signo,
int r = 0;
if (main_pid < 0)
dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type));
else
return -ESRCH;
}
if (control_pid < 0)
dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type));
else
return -ESRCH;
}
if (control_pid > 0)
r = -errno;
if (main_pid > 0)
r = -errno;
int q;
if (!pid_set)
return -ENOMEM;
if (control_pid > 0) {
if (q < 0)
return q;
}
if (main_pid > 0) {
if (q < 0)
return q;
}
r = q;
}
return r;
}
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;
}
assert(u);
if (offset <= 0)
return NULL;
}
char *p, *q;
int r;
assert(u);
return -ENOTSUP;
if (!filename_is_safe(name))
return -EINVAL;
_cleanup_free_ char *c = NULL;
r = user_config_home(&c);
if (r < 0)
return r;
if (r == 0)
return -ENOENT;
} else if (runtime)
else
if (!p)
return -ENOMEM;
if (!q) {
free(p);
return -ENOMEM;
}
*_p = p;
*_q = q;
return 0;
}
int r;
assert(u);
if (r < 0)
return r;
mkdir_p(p, 0755);
return write_string_file_atomic_label(q, data);
}
int r;
assert(u);
if (unlink(q) < 0)
r = -errno;
else
r = 0;
rmdir(p);
return r;
}
int unit_kill_context(
Unit *u,
KillContext *c,
bool sigkill,
bool main_pid_alien) {
int sig, wait_for_exit = 0, r;
assert(u);
assert(c);
return 0;
if (main_pid > 0) {
if (r < 0 && r != -ESRCH) {
} else
}
if (control_pid > 0) {
if (r < 0 && r != -ESRCH) {
log_warning_unit(u->id,
"Failed to kill control process %li (%s): %s",
} else
wait_for_exit = true;
}
if (c->kill_mode == KILL_CONTROL_GROUP) {
if (!pid_set)
return -ENOMEM;
if (main_pid > 0) {
if (r < 0)
return r;
}
if (control_pid > 0) {
if (r < 0)
return r;
}
if (r < 0) {
} else if (r > 0)
wait_for_exit = true;
}
return wait_for_exit;
}
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_REQUISITE] = "Requisite",
[UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
[UNIT_WANTS] = "Wants",
[UNIT_BINDS_TO] = "BindsTo",
[UNIT_PART_OF] = "PartOf",
[UNIT_REQUIRED_BY] = "RequiredBy",
[UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
[UNIT_WANTED_BY] = "WantedBy",
[UNIT_BOUND_BY] = "BoundBy",
[UNIT_CONSISTS_OF] = "ConsistsOf",
[UNIT_CONFLICTS] = "Conflicts",
[UNIT_CONFLICTED_BY] = "ConflictedBy",
[UNIT_BEFORE] = "Before",
[UNIT_AFTER] = "After",
[UNIT_ON_FAILURE] = "OnFailure",
[UNIT_TRIGGERS] = "Triggers",
[UNIT_TRIGGERED_BY] = "TriggeredBy",
[UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo",
[UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom",
[UNIT_REFERENCES] = "References",
[UNIT_REFERENCED_BY] = "ReferencedBy",
};