unit.c revision 47be870bd83fb3719dffc3ee9348a409ab762a14
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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 "load-fragment.h"
#include "load-dropin.h"
#include "log.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,
};
UnitType unit_name_to_type(const char *n) {
UnitType t;
assert(n);
for (t = 0; t < _UNIT_TYPE_MAX; t++)
return t;
return _UNIT_TYPE_INVALID;
}
#define VALID_CHARS \
"0123456789" \
"abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"-_.\\"
bool unit_name_is_valid(const char *n) {
UnitType t;
const char *e, *i;
assert(n);
if (strlen(n) >= UNIT_NAME_MAX)
return false;
t = unit_name_to_type(n);
if (t < 0 || t >= _UNIT_TYPE_MAX)
return false;
if (!(e = strrchr(n, '.')))
return false;
if (e == n)
return false;
for (i = n; i < e; i++)
if (!strchr(VALID_CHARS, *i))
return false;
return true;
}
char *unit_name_change_suffix(const char *n, const char *suffix) {
char *e, *r;
size_t a, b;
assert(n);
assert(unit_name_is_valid(n));
a = e - n;
if (!(r = new(char, a + b + 1)))
return NULL;
memcpy(r, n, a);
return r;
}
Unit *u;
assert(m);
return NULL;
free(u);
return NULL;
}
return u;
}
UnitType t;
char *s;
int r;
assert(u);
if (!unit_name_is_valid(text))
return -EINVAL;
return -EINVAL;
return -EINVAL;
return -ENOMEM;
free(s);
if (r == -EEXIST)
return 0;
return r;
}
free(s);
return r;
}
return 0;
}
char *s;
assert(u);
/* Selects one of the names of this unit as the id */
return -ENOENT;
return 0;
}
char *s;
assert(u);
if (!(s = strdup(description)))
return -ENOMEM;
u->meta.description = s;
return 0;
}
void unit_add_to_load_queue(Unit *u) {
assert(u);
return;
u->meta.in_load_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);
/* Detach from next 'bigger' objects */
if (u->meta.in_load_queue)
if (UNIT_VTABLE(u)->done)
UNIT_VTABLE(u)->done(u);
/* Free data and next 'smaller' objects */
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
free(t);
free(u);
}
assert(u);
return UNIT_INACTIVE;
return UNIT_VTABLE(u)->active_state(u);
}
if (!other)
return 0;
if (*s)
return -ENOMEM;
return 0;
}
/* FIXME: Does not rollback on failure! Needs to fix special unit
* pointers. Needs to merge names and dependencies properly.*/
int r;
assert(u);
/* This merges 'other' into 'unit'. FIXME: This does not
* rollback on failure. */
return -EINVAL;
return -EINVAL;
/* Merge names */
return r;
/* Merge dependencies */
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
/* fixme, the inverse mapping is missing */
return r;
return 0;
}
assert(u);
}
const char *unit_description(Unit *u) {
assert(u);
if (u->meta.description)
return u->meta.description;
return unit_id(u);
}
char *t;
Iterator i;
char *p2;
const char *prefix2;
assert(u);
if (!prefix)
prefix = "";
fprintf(f,
"%s→ Unit %s:\n"
"%s\tDescription: %s\n"
"%s\tUnit Load State: %s\n"
"%s\tUnit Active State: %s\n"
"%s\tRecursive Stop: %s\n"
"%s\tStop When Unneeded: %s\n",
prefix, unit_description(u),
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
continue;
}
if (UNIT_VTABLE(u)->dump)
}
/* Common implementation for multiple backends */
int unit_load_fragment_and_dropin(Unit *u) {
int r, ret;
assert(u);
/* Load a .socket file */
if ((r = unit_load_fragment(u)) < 0)
return r;
ret = r > 0;
/* Load drop-in directory data */
if ((r = unit_load_dropin(u)) < 0)
return r;
return ret;
}
int r;
assert(u);
if (u->meta.in_load_queue) {
u->meta.in_load_queue = false;
}
return 0;
if (UNIT_VTABLE(u)->init)
if ((r = UNIT_VTABLE(u)->init(u)) < 0)
goto fail;
return 0;
fail:
return r;
}
/* Errors:
* -EBADR: This unit type does not support starting.
* -EALREADY: Unit is already started.
* -EAGAIN: An operation is already in progress. Retry later.
*/
int unit_start(Unit *u) {
assert(u);
if (!UNIT_VTABLE(u)->start)
return -EBADR;
state = unit_active_state(u);
return -EALREADY;
/* 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;
}
/* 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);
if (!UNIT_VTABLE(u)->stop)
return -EBADR;
state = unit_active_state(u);
if (state == UNIT_INACTIVE)
return -EALREADY;
if (state == UNIT_DEACTIVATING)
return 0;
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 (!unit_can_reload(u))
return -EBADR;
state = unit_active_state(u);
if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
return -EALREADY;
if (unit_active_state(u) != UNIT_ACTIVE)
return -ENOEXEC;
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_uneeded(Unit *u) {
Iterator i;
assert(u);
/* If this service shall be shut down when unneeded then do
* so. */
if (!u->meta.stop_when_unneeded)
return;
return;
return;
return;
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);
if (u->meta.recursive_stop) {
/* Pull down units need us recursively if enabled */
}
/* Garbage collect services that might not be needed anymore, if enabled */
}
assert(u);
return;
/* 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. */
else {
/* Let's check whether this state change
* constitutes a finished job, or maybe
* cotradicts a running job and hence needs to
* invalidate jobs. */
case JOB_START:
case JOB_VERIFY_ACTIVE:
if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
return;
} else if (ns == UNIT_ACTIVATING)
return;
else
break;
case JOB_RELOAD:
case JOB_RELOAD_OR_START:
if (ns == UNIT_ACTIVE) {
return;
return;
else
break;
case JOB_STOP:
case JOB_RESTART:
case JOB_TRY_RESTART:
if (ns == UNIT_INACTIVE) {
return;
} else if (ns == UNIT_DEACTIVATING)
return;
else
break;
default:
assert_not_reached("Job type unknown");
}
}
}
/* If this state change happened without being requested by a
* job, then let's retroactively start or stop dependencies */
/* 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);
}
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_TIMER) {
ours = false;
} else {
ours = true;
return -errno;
}
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_TIMER;
return 0;
fail:
if (ours)
return -errno;
}
assert(u);
assert(w);
if (w->type == WATCH_INVALID)
return;
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:
return true;
case JOB_STOP:
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,
};
int r;
assert(u);
assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
/* We won't allow dependencies on ourselves. We will not
* consider them an error however. */
if (u == other)
return 0;
if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
return r;
if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
return r;
return r;
return r;
}
return 0;
}
int r;
return r;
if ((r = unit_add_dependency(u, d, other)) < 0)
return r;
return 0;
}
const char *unit_path(void) {
char *e;
if ((e = getenv("UNIT_PATH")))
if (path_is_absolute(e))
return e;
return UNIT_PATH;
}
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("UNIT_PATH", c, 0) < 0) {
r = -errno;
free(c);
return r;
}
return 0;
}
char *r, *t;
const char *f;
size_t a, b, c;
/* Takes a path and a suffix and prefix and makes a nice
* string suitable as unit name of it, escaping all weird
* chars on the way.
*
* / becomes ., and all chars not alloweed in a unit name get
* escaped as \xFF, including \ and ., of course. This
* escaping is hence reversible.
*/
if (!prefix)
prefix = "";
if (!suffix)
suffix = "";
return NULL;
for (f = path, t = r+a; *f; f++) {
if (*f == '/')
*(t++) = '.';
*(t++) = '\\';
*(t++) = 'x';
*(t++) = hexchar(*f > 4);
*(t++) = hexchar(*f);
} else
*(t++) = *f;
}
return r;
}
char *unit_dbus_path(Unit *u) {
char *p, *e;
assert(u);
if (!(e = bus_path_escape(unit_id(u))))
return NULL;
if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
free(e);
return NULL;
}
free(e);
return p;
}
static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
[UNIT_SERVICE] = "service",
[UNIT_TIMER] = "timer",
[UNIT_SOCKET] = "socket",
[UNIT_TARGET] = "target",
[UNIT_DEVICE] = "device",
[UNIT_MOUNT] = "mount",
[UNIT_AUTOMOUNT] = "automount",
[UNIT_SNAPSHOT] = "snapshot"
};
static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
[UNIT_STUB] = "stub",
[UNIT_LOADED] = "loaded",
[UNIT_FAILED] = "failed"
};
static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
[UNIT_ACTIVE] = "active",
[UNIT_INACTIVE] = "inactive",
[UNIT_ACTIVATING] = "activating",
[UNIT_DEACTIVATING] = "deactivating"
};
static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
[UNIT_REQUIRES] = "Requires",
[UNIT_SOFT_REQUIRES] = "SoftRequires",
[UNIT_WANTS] = "Wants",
[UNIT_REQUISITE] = "Requisite",
[UNIT_SOFT_REQUISITE] = "SoftRequisite",
[UNIT_REQUIRED_BY] = "RequiredBy",
[UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
[UNIT_WANTED_BY] = "WantedBy",
[UNIT_CONFLICTS] = "Conflicts",
[UNIT_BEFORE] = "Before",
[UNIT_AFTER] = "After",
};