diff options
author | Ivan Shapovalov <intelfx100@gmail.com> | 2015-03-07 08:44:52 -0500 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2015-03-07 08:44:57 -0500 |
commit | 6e392c9c45643d106673c6643ac8bf4e65da13c1 (patch) | |
tree | 77cd87fdc7f0e104f2f48fb0ea282111dde94766 /src/core/manager.c | |
parent | 6829cec4dce1b41c895425a120b70d0a3ed677ab (diff) |
core: do not spawn jobs or touch other units during coldplugging
Because the order of coldplugging is not defined, we can reference a
not-yet-coldplugged unit and read its state while it has not yet been
set to a meaningful value.
This way, already active units may get started again.
We fix this by deferring such actions until all units have been at
least somehow coldplugged.
Fixes https://bugs.freedesktop.org/show_bug.cgi?id=88401
Diffstat (limited to 'src/core/manager.c')
-rw-r--r-- | src/core/manager.c | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/core/manager.c b/src/core/manager.c index 7a6d5190e7..3e87aa969b 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -975,7 +975,28 @@ static int manager_coldplug(Manager *m) { Unit *u; char *k; - assert(m); + /* + * Some unit types tend to spawn jobs or check other units' state + * during coldplug. This is wrong because it is undefined whether the + * units in question have been already coldplugged (i. e. their state + * restored). This way, we can easily re-start an already started unit + * or otherwise make a wrong decision based on the unit's state. + * + * Solve this by providing a way for coldplug functions to defer + * such actions until after all units have been coldplugged. + * + * We store Unit* -> int(*)(Unit*). + * + * https://bugs.freedesktop.org/show_bug.cgi?id=88401 + */ + _cleanup_hashmap_free_ Hashmap *deferred_work = NULL; + int(*proc)(Unit*); + + assert(m); + + deferred_work = hashmap_new(&trivial_hash_ops); + if (!deferred_work) + return -ENOMEM; /* Then, let's set up their initial state. */ HASHMAP_FOREACH_KEY(u, k, m->units, i) { @@ -985,7 +1006,17 @@ static int manager_coldplug(Manager *m) { if (u->id != k) continue; - q = unit_coldplug(u); + q = unit_coldplug(u, deferred_work); + if (q < 0) + r = q; + } + + /* After coldplugging and setting up initial state of the units, + * let's perform operations which spawn jobs or query units' state. */ + HASHMAP_FOREACH_KEY(proc, u, deferred_work, i) { + int q; + + q = proc(u); if (q < 0) r = q; } |