core: delay adding target dependencies until all units are loaded and aliases resolved (#8381)
Currently we add target dependencies while we are loading units. This can create ordering loops even if configuration doesn't contain any loop. Take for example following configuration, $ systemctl get-default multi-user.target $ cat /etc/systemd/system/test.service [Unit] After=default.target [Service] ExecStart=/bin/true [Install] WantedBy=multi-user.target If we encounter such unit file early during manager start-up (e.g. load queue is dispatched while enumerating devices due to SYSTEMD_WANTS in udev rules) we would add stub unit default.target and we order it Before test.service. At the same time we add implicit Before to multi-user.target. Later we merge two units and we create ordering cycle in the process. To fix the issue we will now never add any target dependencies until we loaded all the unit files and resolved all the aliases.
This commit is contained in:
committed by
Lennart Poettering
parent
959071cac2
commit
19496554e2
@@ -1670,6 +1670,42 @@ Unit *manager_get_unit(Manager *m, const char *name) {
|
||||
return hashmap_get(m->units, name);
|
||||
}
|
||||
|
||||
static int manager_dispatch_target_deps_queue(Manager *m) {
|
||||
Unit *u;
|
||||
unsigned k;
|
||||
int r = 0;
|
||||
|
||||
static const UnitDependency deps[] = {
|
||||
UNIT_REQUIRED_BY,
|
||||
UNIT_REQUISITE_OF,
|
||||
UNIT_WANTED_BY,
|
||||
UNIT_BOUND_BY
|
||||
};
|
||||
|
||||
assert(m);
|
||||
|
||||
while ((u = m->target_deps_queue)) {
|
||||
assert(u->in_target_deps_queue);
|
||||
|
||||
LIST_REMOVE(target_deps_queue, u->manager->target_deps_queue, u);
|
||||
u->in_target_deps_queue = false;
|
||||
|
||||
for (k = 0; k < ELEMENTSOF(deps); k++) {
|
||||
Unit *target;
|
||||
Iterator i;
|
||||
void *v;
|
||||
|
||||
HASHMAP_FOREACH_KEY(v, target, u->dependencies[deps[k]], i) {
|
||||
r = unit_add_default_target_dependency(u, target);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
unsigned manager_dispatch_load_queue(Manager *m) {
|
||||
Unit *u;
|
||||
unsigned n = 0;
|
||||
@@ -1693,6 +1729,11 @@ unsigned manager_dispatch_load_queue(Manager *m) {
|
||||
}
|
||||
|
||||
m->dispatching_load_queue = false;
|
||||
|
||||
/* Dispatch the units waiting for their target dependencies to be added now, as all targets that we know about
|
||||
* should be loaded and have aliases resolved */
|
||||
(void) manager_dispatch_target_deps_queue(m);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user