Systemd/src/core/target.c

220 lines
5.6 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: LGPL-2.1-or-later */
2010-04-18 03:08:16 +02:00
#include "dbus-target.h"
#include "dbus-unit.h"
#include "log.h"
#include "serialize.h"
2010-06-18 04:22:59 +02:00
#include "special.h"
#include "string-util.h"
#include "target.h"
#include "unit-name.h"
#include "unit.h"
2010-01-26 19:06:50 +01:00
2010-01-27 06:33:27 +01:00
static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
[TARGET_DEAD] = UNIT_INACTIVE,
[TARGET_ACTIVE] = UNIT_ACTIVE
};
static void target_set_state(Target *t, TargetState state) {
TargetState old_state;
assert(t);
if (t->state != state)
bus_unit_send_pending_change_signal(UNIT(t), false);
2010-01-27 06:33:27 +01:00
old_state = t->state;
t->state = state;
if (state != old_state)
log_debug("%s changed %s -> %s",
UNIT(t)->id,
2010-04-21 03:27:44 +02:00
target_state_to_string(old_state),
target_state_to_string(state));
2010-01-26 19:06:50 +01:00
unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
2010-01-27 06:33:27 +01:00
}
static int target_add_default_dependencies(Target *t) {
static const UnitDependency deps[] = {
UNIT_REQUIRES,
UNIT_REQUISITE,
UNIT_WANTS,
UNIT_BINDS_TO,
UNIT_PART_OF
};
int r;
unsigned k;
assert(t);
if (!UNIT(t)->default_dependencies)
return 0;
/* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
* ordering manually we won't add anything in here to make sure we don't create a loop. */
for (k = 0; k < ELEMENTSOF(deps); k++) {
Unit *other;
void *v;
HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]]) {
r = unit_add_default_target_dependency(other, UNIT(t));
if (r < 0)
return r;
}
}
if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
return 0;
/* Make sure targets are unloaded on shutdown */
return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
}
static int target_load(Unit *u) {
Target *t = TARGET(u);
int r;
assert(t);
r = unit_load_fragment_and_dropin(u, true);
if (r < 0)
return r;
if (u->load_state != UNIT_LOADED)
return 0;
/* This is a new unit? Then let's add in some extras */
return target_add_default_dependencies(t);
}
static int target_coldplug(Unit *u) {
2010-04-21 03:27:44 +02:00
Target *t = TARGET(u);
assert(t);
assert(t->state == TARGET_DEAD);
if (t->deserialized_state != t->state)
target_set_state(t, t->deserialized_state);
return 0;
}
static void target_dump(Unit *u, FILE *f, const char *prefix) {
Target *t = TARGET(u);
assert(t);
assert(f);
fprintf(f,
"%sTarget State: %s\n",
prefix, target_state_to_string(t->state));
}
2010-01-27 06:33:27 +01:00
static int target_start(Unit *u) {
Target *t = TARGET(u);
core: add "invocation ID" concept to service manager This adds a new invocation ID concept to the service manager. The invocation ID identifies each runtime cycle of a unit uniquely. A new randomized 128bit ID is generated each time a unit moves from and inactive to an activating or active state. The primary usecase for this concept is to connect the runtime data PID 1 maintains about a service with the offline data the journal stores about it. Previously we'd use the unit name plus start/stop times, which however is highly racy since the journal will generally process log data after the service already ended. The "invocation ID" kinda matches the "boot ID" concept of the Linux kernel, except that it applies to an individual unit instead of the whole system. The invocation ID is passed to the activated processes as environment variable. It is additionally stored as extended attribute on the cgroup of the unit. The latter is used by journald to automatically retrieve it for each log logged message and attach it to the log entry. The environment variable is very easily accessible, even for unprivileged services. OTOH the extended attribute is only accessible to privileged processes (this is because cgroupfs only supports the "trusted." xattr namespace, not "user."). The environment variable may be altered by services, the extended attribute may not be, hence is the better choice for the journal. Note that reading the invocation ID off the extended attribute from journald is racy, similar to the way reading the unit name for a logging process is. This patch adds APIs to read the invocation ID to sd-id128: sd_id128_get_invocation() may be used in a similar fashion to sd_id128_get_boot(). PID1's own logging is updated to always include the invocation ID when it logs information about a unit. A new bus call GetUnitByInvocationID() is added that allows retrieving a bus path to a unit by its invocation ID. The bus path is built using the invocation ID, thus providing a path for referring to a unit that is valid only for the current runtime cycleof it. Outlook for the future: should the kernel eventually allow passing of cgroup information along AF_UNIX/SOCK_DGRAM messages via a unique cgroup id, then we can alter the invocation ID to be generated as hash from that rather than entirely randomly. This way we can derive the invocation race-freely from the messages.
2016-08-30 23:18:46 +02:00
int r;
2010-01-27 06:33:27 +01:00
assert(t);
assert(t->state == TARGET_DEAD);
core: add "invocation ID" concept to service manager This adds a new invocation ID concept to the service manager. The invocation ID identifies each runtime cycle of a unit uniquely. A new randomized 128bit ID is generated each time a unit moves from and inactive to an activating or active state. The primary usecase for this concept is to connect the runtime data PID 1 maintains about a service with the offline data the journal stores about it. Previously we'd use the unit name plus start/stop times, which however is highly racy since the journal will generally process log data after the service already ended. The "invocation ID" kinda matches the "boot ID" concept of the Linux kernel, except that it applies to an individual unit instead of the whole system. The invocation ID is passed to the activated processes as environment variable. It is additionally stored as extended attribute on the cgroup of the unit. The latter is used by journald to automatically retrieve it for each log logged message and attach it to the log entry. The environment variable is very easily accessible, even for unprivileged services. OTOH the extended attribute is only accessible to privileged processes (this is because cgroupfs only supports the "trusted." xattr namespace, not "user."). The environment variable may be altered by services, the extended attribute may not be, hence is the better choice for the journal. Note that reading the invocation ID off the extended attribute from journald is racy, similar to the way reading the unit name for a logging process is. This patch adds APIs to read the invocation ID to sd-id128: sd_id128_get_invocation() may be used in a similar fashion to sd_id128_get_boot(). PID1's own logging is updated to always include the invocation ID when it logs information about a unit. A new bus call GetUnitByInvocationID() is added that allows retrieving a bus path to a unit by its invocation ID. The bus path is built using the invocation ID, thus providing a path for referring to a unit that is valid only for the current runtime cycleof it. Outlook for the future: should the kernel eventually allow passing of cgroup information along AF_UNIX/SOCK_DGRAM messages via a unique cgroup id, then we can alter the invocation ID to be generated as hash from that rather than entirely randomly. This way we can derive the invocation race-freely from the messages.
2016-08-30 23:18:46 +02:00
r = unit_acquire_invocation_id(u);
if (r < 0)
return r;
2010-01-27 06:33:27 +01:00
target_set_state(t, TARGET_ACTIVE);
return 1;
2010-01-27 06:33:27 +01:00
}
2010-01-26 19:06:50 +01:00
2010-01-27 06:33:27 +01:00
static int target_stop(Unit *u) {
Target *t = TARGET(u);
assert(t);
assert(t->state == TARGET_ACTIVE);
target_set_state(t, TARGET_DEAD);
return 1;
2010-01-26 19:06:50 +01:00
}
2010-04-21 03:27:44 +02:00
static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
Target *s = TARGET(u);
assert(s);
assert(f);
assert(fds);
(void) serialize_item(f, "state", target_state_to_string(s->state));
2010-04-21 03:27:44 +02:00
return 0;
}
static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
Target *s = TARGET(u);
assert(u);
assert(key);
assert(value);
assert(fds);
if (streq(key, "state")) {
TargetState state;
state = target_state_from_string(value);
if (state < 0)
2010-04-21 03:27:44 +02:00
log_debug("Failed to parse state value %s", value);
else
s->deserialized_state = state;
} else
log_debug("Unknown serialization key '%s'", key);
return 0;
}
_pure_ static UnitActiveState target_active_state(Unit *u) {
2010-01-27 06:33:27 +01:00
assert(u);
return state_translation_table[TARGET(u)->state];
2010-01-26 19:06:50 +01:00
}
_pure_ static const char *target_sub_state_to_string(Unit *u) {
assert(u);
2010-04-21 03:27:44 +02:00
return target_state_to_string(TARGET(u)->state);
}
2010-01-26 21:39:06 +01:00
const UnitVTable target_vtable = {
.object_size = sizeof(Target),
.sections =
"Unit\0"
"Target\0"
"Install\0",
2010-01-26 19:06:50 +01:00
.can_fail = true,
.load = target_load,
2010-04-21 03:27:44 +02:00
.coldplug = target_coldplug,
2010-01-27 06:33:27 +01:00
.dump = target_dump,
.start = target_start,
.stop = target_stop,
2010-01-26 19:06:50 +01:00
2010-04-21 03:27:44 +02:00
.serialize = target_serialize,
.deserialize_item = target_deserialize_item,
.active_state = target_active_state,
2010-04-18 03:08:16 +02:00
.sub_state_to_string = target_sub_state_to_string,
.status_message_formats = {
.finished_start_job = {
[JOB_DONE] = "Reached target %s.",
},
.finished_stop_job = {
[JOB_DONE] = "Stopped target %s.",
},
},
2010-01-26 19:06:50 +01:00
};