Merge pull request #19322 from poettering/dep-split
core: rework dependency system to be based on atoms + add three new dep types
This commit is contained in:
@@ -70,13 +70,13 @@ static int test_cgroup_mask(void) {
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "parent-deep.slice", NULL, &parent_deep) >= 0);
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "nomem.slice", NULL, &nomem_parent) >= 0);
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "nomemleaf.service", NULL, &nomem_leaf) >= 0);
|
||||
assert_se(UNIT_DEREF(son->slice) == parent);
|
||||
assert_se(UNIT_DEREF(daughter->slice) == parent);
|
||||
assert_se(UNIT_DEREF(parent_deep->slice) == parent);
|
||||
assert_se(UNIT_DEREF(grandchild->slice) == parent_deep);
|
||||
assert_se(UNIT_DEREF(nomem_leaf->slice) == nomem_parent);
|
||||
root = UNIT_DEREF(parent->slice);
|
||||
assert_se(UNIT_DEREF(nomem_parent->slice) == root);
|
||||
assert_se(UNIT_GET_SLICE(son) == parent);
|
||||
assert_se(UNIT_GET_SLICE(daughter) == parent);
|
||||
assert_se(UNIT_GET_SLICE(parent_deep) == parent);
|
||||
assert_se(UNIT_GET_SLICE(grandchild) == parent_deep);
|
||||
assert_se(UNIT_GET_SLICE(nomem_leaf) == nomem_parent);
|
||||
root = UNIT_GET_SLICE(parent);
|
||||
assert_se(UNIT_GET_SLICE(nomem_parent) == root);
|
||||
|
||||
/* Verify per-unit cgroups settings. */
|
||||
ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(son), CGROUP_MASK_CPU);
|
||||
|
||||
@@ -91,28 +91,28 @@ static int test_default_memory_low(void) {
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml.slice", NULL, &dml) >= 0);
|
||||
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-passthrough.slice", NULL, &dml_passthrough) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_passthrough->slice) == dml);
|
||||
assert_se(UNIT_GET_SLICE(dml_passthrough) == dml);
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-passthrough-empty.service", NULL, &dml_passthrough_empty) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_passthrough_empty->slice) == dml_passthrough);
|
||||
assert_se(UNIT_GET_SLICE(dml_passthrough_empty) == dml_passthrough);
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-passthrough-set-dml.service", NULL, &dml_passthrough_set_dml) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_passthrough_set_dml->slice) == dml_passthrough);
|
||||
assert_se(UNIT_GET_SLICE(dml_passthrough_set_dml) == dml_passthrough);
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-passthrough-set-ml.service", NULL, &dml_passthrough_set_ml) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_passthrough_set_ml->slice) == dml_passthrough);
|
||||
assert_se(UNIT_GET_SLICE(dml_passthrough_set_ml) == dml_passthrough);
|
||||
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-override.slice", NULL, &dml_override) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_override->slice) == dml);
|
||||
assert_se(UNIT_GET_SLICE(dml_override) == dml);
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-override-empty.service", NULL, &dml_override_empty) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_override_empty->slice) == dml_override);
|
||||
assert_se(UNIT_GET_SLICE(dml_override_empty) == dml_override);
|
||||
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-discard.slice", NULL, &dml_discard) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_discard->slice) == dml);
|
||||
assert_se(UNIT_GET_SLICE(dml_discard) == dml);
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-discard-empty.service", NULL, &dml_discard_empty) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_discard_empty->slice) == dml_discard);
|
||||
assert_se(UNIT_GET_SLICE(dml_discard_empty) == dml_discard);
|
||||
assert_se(manager_load_startable_unit_or_warn(m, "dml-discard-set-ml.service", NULL, &dml_discard_set_ml) >= 0);
|
||||
assert_se(UNIT_DEREF(dml_discard_set_ml->slice) == dml_discard);
|
||||
assert_se(UNIT_GET_SLICE(dml_discard_set_ml) == dml_discard);
|
||||
|
||||
root = UNIT_DEREF(dml->slice);
|
||||
assert_se(!UNIT_ISSET(root->slice));
|
||||
assert_se(root = UNIT_GET_SLICE(dml));
|
||||
assert_se(!UNIT_GET_SLICE(root));
|
||||
|
||||
assert_se(unit_get_ancestor_memory_low(root) == CGROUP_LIMIT_MIN);
|
||||
|
||||
|
||||
+123
-18
@@ -6,16 +6,75 @@
|
||||
#include "bus-util.h"
|
||||
#include "manager.h"
|
||||
#include "rm-rf.h"
|
||||
#include "service.h"
|
||||
#include "special.h"
|
||||
#include "strv.h"
|
||||
#include "tests.h"
|
||||
#include "service.h"
|
||||
#include "unit-serialize.h"
|
||||
|
||||
static void verify_dependency_atoms(void) {
|
||||
UnitDependencyAtom combined = 0, multi_use_atoms = 0;
|
||||
|
||||
/* Let's guarantee that our dependency type/atom translation tables are fully correct */
|
||||
|
||||
for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
|
||||
UnitDependencyAtom a;
|
||||
UnitDependency reverse;
|
||||
bool has_superset = false;
|
||||
|
||||
assert_se((a = unit_dependency_to_atom(d)) >= 0);
|
||||
|
||||
for (UnitDependency t = 0; t < _UNIT_DEPENDENCY_MAX; t++) {
|
||||
UnitDependencyAtom b;
|
||||
|
||||
if (t == d)
|
||||
continue;
|
||||
|
||||
assert_se((b = unit_dependency_to_atom(t)) >= 0);
|
||||
|
||||
if ((a & b) == a) {
|
||||
has_superset = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
reverse = unit_dependency_from_unique_atom(a);
|
||||
assert_se(reverse == _UNIT_DEPENDENCY_INVALID || reverse >= 0);
|
||||
|
||||
assert_se((reverse < 0) == has_superset); /* If one dependency type is a superset of another,
|
||||
* then the reverse mapping is not unique, verify
|
||||
* that. */
|
||||
|
||||
log_info("Verified dependency type: %s", unit_dependency_to_string(d));
|
||||
|
||||
multi_use_atoms |= combined & a;
|
||||
combined |= a;
|
||||
}
|
||||
|
||||
/* Make sure all atoms are used, i.e. there's at least one dependency type that references it. */
|
||||
assert_se(combined == _UNIT_DEPENDENCY_ATOM_MAX);
|
||||
|
||||
for (UnitDependencyAtom a = 1; a <= _UNIT_DEPENDENCY_ATOM_MAX; a <<= 1) {
|
||||
|
||||
if (multi_use_atoms & a) {
|
||||
/* If an atom is used by multiple dep types, then mapping the atom to a dependency is
|
||||
* not unique and *must* fail */
|
||||
assert_se(unit_dependency_from_unique_atom(a) == _UNIT_DEPENDENCY_INVALID);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* If only a single dep type uses specific atom, let's guarantee our mapping table is
|
||||
complete, and thus the atom can be mapped to the single dep type that is used. */
|
||||
assert_se(unit_dependency_from_unique_atom(a) >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
_cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(manager_freep) Manager *m = NULL;
|
||||
Unit *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *g = NULL,
|
||||
*h = NULL, *i = NULL, *a_conj = NULL, *unit_with_multiple_dashes = NULL;
|
||||
*h = NULL, *i = NULL, *a_conj = NULL, *unit_with_multiple_dashes = NULL, *stub = NULL;
|
||||
Job *j;
|
||||
int r;
|
||||
|
||||
@@ -122,37 +181,83 @@ int main(int argc, char *argv[]) {
|
||||
assert_se(manager_add_job(m, JOB_START, a_conj, JOB_REPLACE, NULL, NULL, &j) == -EDEADLK);
|
||||
manager_dump_jobs(m, stdout, "\t");
|
||||
|
||||
assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], b));
|
||||
assert_se(!hashmap_get(b->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
|
||||
assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], c));
|
||||
assert_se(!hashmap_get(c->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), b));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(b, UNIT_RELOAD_PROPAGATED_FROM), a));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), c));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(c, UNIT_RELOAD_PROPAGATED_FROM), a));
|
||||
|
||||
assert_se(unit_add_dependency(a, UNIT_PROPAGATES_RELOAD_TO, b, true, UNIT_DEPENDENCY_UDEV) == 0);
|
||||
assert_se(unit_add_dependency(a, UNIT_PROPAGATES_RELOAD_TO, c, true, UNIT_DEPENDENCY_PROC_SWAP) == 0);
|
||||
|
||||
assert_se(hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], b));
|
||||
assert_se(hashmap_get(b->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
|
||||
assert_se(hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], c));
|
||||
assert_se(hashmap_get(c->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
|
||||
assert_se(hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), b));
|
||||
assert_se(hashmap_get(unit_get_dependencies(b, UNIT_RELOAD_PROPAGATED_FROM), a));
|
||||
assert_se(hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), c));
|
||||
assert_se(hashmap_get(unit_get_dependencies(c, UNIT_RELOAD_PROPAGATED_FROM), a));
|
||||
|
||||
unit_remove_dependencies(a, UNIT_DEPENDENCY_UDEV);
|
||||
|
||||
assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], b));
|
||||
assert_se(!hashmap_get(b->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
|
||||
assert_se(hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], c));
|
||||
assert_se(hashmap_get(c->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), b));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(b, UNIT_RELOAD_PROPAGATED_FROM), a));
|
||||
assert_se(hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), c));
|
||||
assert_se(hashmap_get(unit_get_dependencies(c, UNIT_RELOAD_PROPAGATED_FROM), a));
|
||||
|
||||
unit_remove_dependencies(a, UNIT_DEPENDENCY_PROC_SWAP);
|
||||
|
||||
assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], b));
|
||||
assert_se(!hashmap_get(b->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
|
||||
assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], c));
|
||||
assert_se(!hashmap_get(c->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), b));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(b, UNIT_RELOAD_PROPAGATED_FROM), a));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), c));
|
||||
assert_se(!hashmap_get(unit_get_dependencies(c, UNIT_RELOAD_PROPAGATED_FROM), a));
|
||||
|
||||
assert_se(manager_load_unit(m, "unit-with-multiple-dashes.service", NULL, NULL, &unit_with_multiple_dashes) >= 0);
|
||||
|
||||
assert_se(strv_equal(unit_with_multiple_dashes->documentation, STRV_MAKE("man:test", "man:override2", "man:override3")));
|
||||
assert_se(streq_ptr(unit_with_multiple_dashes->description, "override4"));
|
||||
|
||||
/* Now merge a synthetic unit into the existing one */
|
||||
assert_se(unit_new_for_name(m, sizeof(Service), "merged.service", &stub) >= 0);
|
||||
assert_se(unit_add_dependency_by_name(stub, UNIT_AFTER, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_FILE) >= 0);
|
||||
assert_se(unit_add_dependency_by_name(stub, UNIT_AFTER, "quux.target", true, UNIT_DEPENDENCY_FILE) >= 0);
|
||||
assert_se(unit_add_dependency_by_name(stub, UNIT_AFTER, SPECIAL_ROOT_SLICE, true, UNIT_DEPENDENCY_FILE) >= 0);
|
||||
assert_se(unit_add_dependency_by_name(stub, UNIT_REQUIRES, "non-existing.mount", true, UNIT_DEPENDENCY_FILE) >= 0);
|
||||
assert_se(unit_add_dependency_by_name(stub, UNIT_ON_FAILURE, "non-existing-on-failure.target", true, UNIT_DEPENDENCY_FILE) >= 0);
|
||||
|
||||
log_info("/* Merging a+stub, dumps before */");
|
||||
unit_dump(a, stderr, NULL);
|
||||
unit_dump(stub, stderr, NULL);
|
||||
assert_se(unit_merge(a, stub) >= 0);
|
||||
log_info("/* Dump of merged a+stub */");
|
||||
unit_dump(a, stderr, NULL);
|
||||
|
||||
assert_se(unit_has_dependency(a, UNIT_ATOM_AFTER, manager_get_unit(m, SPECIAL_BASIC_TARGET)));
|
||||
assert_se(unit_has_dependency(a, UNIT_ATOM_AFTER, manager_get_unit(m, "quux.target")));
|
||||
assert_se(unit_has_dependency(a, UNIT_ATOM_AFTER, manager_get_unit(m, SPECIAL_ROOT_SLICE)));
|
||||
assert_se(unit_has_dependency(a, UNIT_ATOM_PULL_IN_START, manager_get_unit(m, "non-existing.mount")));
|
||||
assert_se(unit_has_dependency(a, UNIT_ATOM_RETROACTIVE_START_REPLACE, manager_get_unit(m, "non-existing.mount")));
|
||||
assert_se(unit_has_dependency(a, UNIT_ATOM_ON_FAILURE, manager_get_unit(m, "non-existing-on-failure.target")));
|
||||
assert_se(!unit_has_dependency(a, UNIT_ATOM_ON_FAILURE, manager_get_unit(m, "basic.target")));
|
||||
assert_se(!unit_has_dependency(a, UNIT_ATOM_PROPAGATES_RELOAD_TO, manager_get_unit(m, "non-existing-on-failure.target")));
|
||||
|
||||
assert_se(unit_has_name(a, "a.service"));
|
||||
assert_se(unit_has_name(a, "merged.service"));
|
||||
|
||||
unsigned mm = 1;
|
||||
Unit *other;
|
||||
|
||||
UNIT_FOREACH_DEPENDENCY(other, a, UNIT_ATOM_AFTER) {
|
||||
mm *= unit_has_name(other, SPECIAL_BASIC_TARGET) ? 3 : 1;
|
||||
mm *= unit_has_name(other, "quux.target") ? 5 : 1;
|
||||
mm *= unit_has_name(other, SPECIAL_ROOT_SLICE) ? 7 : 1;
|
||||
}
|
||||
|
||||
UNIT_FOREACH_DEPENDENCY(other, a, UNIT_ATOM_ON_FAILURE)
|
||||
mm *= unit_has_name(other, "non-existing-on-failure.target") ? 11 : 1;
|
||||
|
||||
UNIT_FOREACH_DEPENDENCY(other, a, UNIT_ATOM_PULL_IN_START)
|
||||
mm *= unit_has_name(other, "non-existing.mount") ? 13 : 1;
|
||||
|
||||
assert_se(mm == 3U*5U*7U*11U*13U);
|
||||
|
||||
verify_dependency_atoms();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user