manager.c revision 1ffba6fe82d65f2a87b53a21c7927bca8176038c
d657c51f14601d0235434ffb78cf6ac0f27cc83cLennart Poettering/*-*- Mode: C; c-basic-offset: 8 -*-*/
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann#include <assert.h>
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann#include <errno.h>
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann#include <string.h>
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann#include "manager.h"
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann#include "hashmap.h"
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann#include "macro.h"
01608bc86a104423d192364f9534b83d0c75db7fKay Sievers#include "strv.h"
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann#include "log.h"
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid HerrmannManager* manager_new(void) {
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann Manager *m;
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack if (!(m = new0(Manager, 1)))
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack return NULL;
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack if (!(m->names = hashmap_new(string_hash_func, string_compare_func)))
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack goto fail;
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack goto fail;
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack goto fail;
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack return m;
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mackfail:
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack manager_free(m);
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack return NULL;
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack}
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mackvoid manager_free(Manager *m) {
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack Name *n;
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack Job *j;
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack
931618d08c64083ff7b29c494f482c40a5b05608Daniel Mack assert(m);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann while ((n = hashmap_first(m->names)))
01608bc86a104423d192364f9534b83d0c75db7fKay Sievers name_free(n);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann while ((j = hashmap_steal_first(m->transaction_jobs)))
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann job_free(j);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann hashmap_free(m->names);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann hashmap_free(m->jobs);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann hashmap_free(m->transaction_jobs);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann free(m);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann}
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmannstatic void transaction_delete_job(Manager *m, Job *j) {
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann assert(m);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann assert(j);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann /* Deletes one job from the transaction */
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann manager_transaction_unlink_job(m, j);
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann if (!j->linked)
e57eaef8a187762ca92838c24b9b6460878a800cDavid Herrmann job_free(j);
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann}
5541c88977e63215e74b7517fb33cb27e5a04f17David Herrmann
861b02ebd6ec997a6880824960ba8903bac74f7dKay Sieversstatic void transaction_delete_name(Manager *m, Name *n) {
861b02ebd6ec997a6880824960ba8903bac74f7dKay Sievers Job *j;
861b02ebd6ec997a6880824960ba8903bac74f7dKay Sievers
861b02ebd6ec997a6880824960ba8903bac74f7dKay Sievers /* Deletes all jobs associated with a certain name from the
861b02ebd6ec997a6880824960ba8903bac74f7dKay Sievers * transaction */
861b02ebd6ec997a6880824960ba8903bac74f7dKay Sievers
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann while ((j = hashmap_get(m->transaction_jobs, n)))
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann transaction_delete_job(m, j);
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann}
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann
0db83ad7334809a6605501e24bad55f3b652c072David Herrmannstatic void transaction_abort(Manager *m) {
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann Job *j;
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann
5541c88977e63215e74b7517fb33cb27e5a04f17David Herrmann assert(m);
5541c88977e63215e74b7517fb33cb27e5a04f17David Herrmann
5541c88977e63215e74b7517fb33cb27e5a04f17David Herrmann while ((j = hashmap_first(m->transaction_jobs)))
5541c88977e63215e74b7517fb33cb27e5a04f17David Herrmann if (j->linked)
9b361114f568e839784a3aeba5c1df5a95e86832Daniel Mack transaction_delete_job(m, j);
9b361114f568e839784a3aeba5c1df5a95e86832Daniel Mack else
9b361114f568e839784a3aeba5c1df5a95e86832Daniel Mack job_free(j);
9b361114f568e839784a3aeba5c1df5a95e86832Daniel Mack
9b361114f568e839784a3aeba5c1df5a95e86832Daniel Mack assert(hashmap_isempty(m->transaction_jobs));
9b361114f568e839784a3aeba5c1df5a95e86832Daniel Mack assert(!m->transaction_anchor);
9b361114f568e839784a3aeba5c1df5a95e86832Daniel Mack}
9b361114f568e839784a3aeba5c1df5a95e86832Daniel Mack
0db83ad7334809a6605501e24bad55f3b652c072David Herrmannstatic void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann JobDependency *l;
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann assert(m);
5541c88977e63215e74b7517fb33cb27e5a04f17David Herrmann
5541c88977e63215e74b7517fb33cb27e5a04f17David Herrmann /* A recursive sweep through the graph that marks all names
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann * that matter to the anchor job, i.e. are directly or
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann * indirectly a dependency of the anchor job via paths that
2d1ca11270e66777c90a449096203afebc37ec9cDavid Herrmann * are fully marked as mattering. */
2d1ca11270e66777c90a449096203afebc37ec9cDavid Herrmann
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann for (l = j ? j->subject_list : m->transaction_anchor; l; l = l->subject_next) {
2d1ca11270e66777c90a449096203afebc37ec9cDavid Herrmann
0db83ad7334809a6605501e24bad55f3b652c072David Herrmann /* This link does not matter */
0f0467e63b0e0688ae9edb1512c1a2637d62ddb4Martin Pitt if (!l->matters)
0f0467e63b0e0688ae9edb1512c1a2637d62ddb4Martin Pitt continue;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
5f92d24fa85d6652c4754e3b3b2a3393026bd0b9Kay Sievers /* This name has already been marked */
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering if (l->object->generation == generation)
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering continue;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering l->object->matters_to_anchor = true;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering l->object->generation = generation;
0aee49d5fba2b2ec94e5c069d937004858a04b4fThomas Hindoe Paaboel Andersen
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering }
5f92d24fa85d6652c4754e3b3b2a3393026bd0b9Kay Sievers}
0f0467e63b0e0688ae9edb1512c1a2637d62ddb4Martin Pitt
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poetteringstatic void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering JobDependency *l, *last;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
c65514649680e5d5ee6a118db6e5b20438cb1710Ronny Chevalier assert(j);
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering assert(other);
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering assert(j->name == other->name);
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering assert(!j->linked);
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
0aee49d5fba2b2ec94e5c069d937004858a04b4fThomas Hindoe Paaboel Andersen /* Merges 'other' into 'j' and then deletes j. */
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering j->type = t;
0f0467e63b0e0688ae9edb1512c1a2637d62ddb4Martin Pitt j->state = JOB_WAITING;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering /* Patch us in as new owner of the JobDependency objects */
0aee49d5fba2b2ec94e5c069d937004858a04b4fThomas Hindoe Paaboel Andersen last = NULL;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering for (l = other->subject_list; l; l = l->subject_next) {
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering assert(l->subject == other);
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering l->subject = j;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering last = l;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering }
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering /* Merge both lists */
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering if (last) {
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering last->subject_next = j->subject_list;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering if (j->subject_list)
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering j->subject_list->subject_prev = last;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering j->subject_list = other->subject_list;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering }
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering /* Patch us in as new owner of the JobDependency objects */
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering last = NULL;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering for (l = other->object_list; l; l = l->object_next) {
0aee49d5fba2b2ec94e5c069d937004858a04b4fThomas Hindoe Paaboel Andersen assert(l->object == other);
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering l->object = j;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering last = l;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering }
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering /* Merge both lists */
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering if (last) {
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering last->object_next = j->object_list;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering if (j->object_list)
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering j->object_list->object_prev = last;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering j->object_list = other->object_list;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering }
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering /* Kill the other job */
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering other->subject_list = NULL;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering other->object_list = NULL;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering transaction_delete_job(m, other);
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering}
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poetteringstatic int delete_one_unmergable_job(Manager *m, Job *j) {
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering Job *k;
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering
b912e251812bb65bed1d545d9748f5b0918f1559Lennart Poettering assert(j);
b912e251812bb65bed1d545d9748f5b0918f1559Lennart Poettering
b912e251812bb65bed1d545d9748f5b0918f1559Lennart Poettering /* Tries to delete one item in the linked list
b912e251812bb65bed1d545d9748f5b0918f1559Lennart Poettering * j->transaction_next->transaction_next->... that conflicts
b912e251812bb65bed1d545d9748f5b0918f1559Lennart Poettering * whith another one, in an attempt to make an inconsistent
470e72d4081c7d0fd74666b7a45358d5ee2abee1Lennart Poettering * transaction work. */
b912e251812bb65bed1d545d9748f5b0918f1559Lennart Poettering
0f0467e63b0e0688ae9edb1512c1a2637d62ddb4Martin Pitt /* We rely here on the fact that if a merged with b does not
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * merge with c, either a or b merge with c neither */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering for (; j; j = j->transaction_next)
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann for (k = j->transaction_next; k; k = k->transaction_next) {
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann Job *d;
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann /* Is this one mergeable? Then skip it */
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann if (job_type_mergeable(j->type, k->type))
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann continue;
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann /* Ok, we found two that conflict, let's see if we can
f7a73a2558bceffd983eb7642680e718cd981122David Herrmann * drop one of them */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (!j->matters_to_anchor)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering d = j;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering else if (!k->matters_to_anchor)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering d = k;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering else
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return -ENOEXEC;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Ok, we can drop one, so let's do so. */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering log_debug("Try to fix job merging by deleting job %s", name_id(d->name));
29d1fcb4a3c921a3d4490353474e9775f7b13b0eZbigniew Jędrzejewski-Szmek transaction_delete_job(m, d);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return 0;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return -EINVAL;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering}
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poetteringstatic int transaction_merge_jobs(Manager *m) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering Job *j;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering void *state;
29d1fcb4a3c921a3d4490353474e9775f7b13b0eZbigniew Jędrzejewski-Szmek int r;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(m);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* First step, check whether any of the jobs for one specific
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * task conflict. If so, try to drop one of them. */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering HASHMAP_FOREACH(j, m->transaction_jobs, state) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering JobType t;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering Job *k;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering t = j->type;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering for (k = j->transaction_next; k; k = k->transaction_next) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if ((r = job_type_merge(&t, k->type)) >= 0)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering continue;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* OK, we could not merge all jobs for this
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * action. Let's see if we can get rid of one
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * of them */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if ((r = delete_one_unmergable_job(m, j)) >= 0)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Ok, we managed to drop one, now
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * let's ask our callers to call us
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * again after garbage collecting */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return -EAGAIN;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* We couldn't merge anything. Failure */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return r;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Second step, merge the jobs. */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering HASHMAP_FOREACH(j, m->transaction_jobs, state) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering JobType t = j->type;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering Job *k;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering for (k = j->transaction_next; k; k = k->transaction_next)
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering assert_se(job_type_merge(&t, k->type) == 0);
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering while ((k = j->transaction_next)) {
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering if (j->linked) {
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering transaction_merge_and_delete_job(m, k, j, t);
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering j = k;
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering } else
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering transaction_merge_and_delete_job(m, j, k, t);
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering }
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering assert(!j->transaction_next);
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering assert(!j->transaction_prev);
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return 0;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering}
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poetteringstatic bool name_matters_to_anchor(Name *n, Job *j) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(n);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(!j->transaction_prev);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Checks whether at least one of the jobs for this name
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * matters to the anchor. */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering for (; j; j = j->transaction_next)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (j->matters_to_anchor)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return true;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return false;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering}
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poetteringstatic int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering void *state;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering Name *n;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering int r;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(m);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(j);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(!j->transaction_prev);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Does a recursive sweep through the ordering graph, looking
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * for a cycle. If we find cycle we try to break it. */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Did we find a cycle? */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (j->marker && j->generation == generation) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering Job *k;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* So, we already have been here. We have a
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * cycle. Let's try to break it. We go backwards in
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * our path and try to find a suitable job to
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * remove. We use the marker to find our way back,
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * since smart how we are we stored our way back in
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * there. */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (!k->linked &&
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering !name_matters_to_anchor(k->name, k)) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Ok, we can drop this one, so let's
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * do so. */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering log_debug("Breaking order cycle by deleting job %s", name_id(k->name));
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering transaction_delete_name(m, k->name);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return -EAGAIN;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Check if this in fact was the beginning of
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * the cycle */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (k == j)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering break;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return -ENOEXEC;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Make the marker point to where we come from, so that we can
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * find our way backwards if we want to break a cycle */
1579dd2c9b8f97e5ec4016d3928d73fea160e55aLennart Poettering j->marker = from;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering j->generation = generation;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* We assume that the the dependencies are bidirectional, and
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * hence can ignore NAME_AFTER */
1a2d5fbe7efa04181a2d5518bc510b84b280baf9David Herrmann SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) {
1a2d5fbe7efa04181a2d5518bc510b84b280baf9David Herrmann Job *o;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Is there a job for this name? */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (!(o = hashmap_get(m->transaction_jobs, n)))
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Ok, there is no job for this in the
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * transaction, but maybe there is already one
1a2d5fbe7efa04181a2d5518bc510b84b280baf9David Herrmann * running? */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (!(o = n->meta.job))
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering continue;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return r;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return 0;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering}
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poetteringstatic int transaction_verify_order(Manager *m, unsigned *generation) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering Job *j;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering int r;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering void *state;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(m);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(generation);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Check if the ordering graph is cyclic. If it is, try to fix
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * that up by dropping one of the jobs. */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering HASHMAP_FOREACH(j, m->transaction_jobs, state)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return r;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return 0;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering}
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poetteringstatic void transaction_collect_garbage(Manager *m) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering bool again;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(m);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Drop jobs that are not required by any other job */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering do {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering void *state;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering Job *j;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering again = false;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering HASHMAP_FOREACH(j, m->transaction_jobs, state) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (j->object_list)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering continue;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering log_debug("Garbage collecting job %s", name_id(j->name));
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering transaction_delete_job(m, j);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering again = true;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering break;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering }
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering } while (again);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering}
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poetteringstatic int transaction_is_destructive(Manager *m, JobMode mode) {
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering void *state;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering Job *j;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering assert(m);
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering /* Checks whether applying this transaction means that
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering * existing jobs would be replaced */
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering HASHMAP_FOREACH(j, m->transaction_jobs, state)
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering if (j->name->meta.job &&
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering j->name->meta.job != j &&
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering !job_type_is_superset(j->type, j->name->meta.job->type))
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return -EEXIST;
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering return 0;
39315f9f8dd5a16b4561c5efffc6114c75835011Lennart Poettering}
481a0aa2c9803a62cda413b8a1d05571957bb4b5Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringstatic int transaction_apply(Manager *m, JobMode mode) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering void *state;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering Job *j;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering int r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Moves the transaction jobs to the set of active jobs */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering HASHMAP_FOREACH(j, m->transaction_jobs, state) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (j->linked)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering continue;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto rollback;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering while ((j = hashmap_steal_first(m->transaction_jobs))) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (j->linked)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering continue;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (j->name->meta.job)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering job_free(j->name->meta.job);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->name->meta.job = j;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->linked = true;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
7edecf218e5884ec8d1549707b4c7a0572c2d93bThomas Hindoe Paaboel Andersen /* We're fully installed. Now let's free data we don't
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * need anymore. */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(!j->transaction_next);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(!j->transaction_prev);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering while (j->subject_list)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering job_dependency_free(j->subject_list);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering while (j->object_list)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering job_dependency_free(j->object_list);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering m->transaction_anchor = NULL;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return 0;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringrollback:
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering HASHMAP_FOREACH(j, m->transaction_jobs, state) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (j->linked)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering continue;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering}
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringstatic int transaction_activate(Manager *m, JobMode mode) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering int r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering unsigned generation = 1;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* This applies the changes recorded in transaction_jobs to
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * the actual list of jobs, if possible. */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* First step: figure out which jobs matter */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering for (;;) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Second step: Let's remove unneeded jobs that might
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * be lurking. */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering transaction_collect_garbage(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Third step: verify order makes sense and correct
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * cycles if necessary and possible */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_verify_order(m, &generation)) >= 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering break;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (r != -EAGAIN)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto rollback;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Let's see if the resulting transaction ordering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * graph is still cyclic... */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering for (;;) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Fourth step: let's drop unmergable entries if
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * necessary and possible, merge entries we can
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * merge */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_merge_jobs(m)) >= 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering break;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (r != -EAGAIN)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto rollback;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Fifth step: an entry got dropped, let's garbage
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * collect its dependencies. */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering transaction_collect_garbage(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Let's see if the resulting transaction still has
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * unmergable entries ... */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Sith step: check whether we can actually apply this */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (mode == JOB_FAIL)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_is_destructive(m, mode)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto rollback;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Seventh step: apply changes */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_apply(m, mode)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto rollback;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(hashmap_isempty(m->transaction_jobs));
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(!m->transaction_anchor);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return 0;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringrollback:
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering transaction_abort(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering}
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringstatic Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering Job *j, *f;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering int r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(name);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Looks for an axisting prospective job and returns that. If
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * it doesn't exist it is created and added to the prospective
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering * jobs list. */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering f = hashmap_get(m->transaction_jobs, name);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering for (j = f; j; j = j->transaction_next) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(j->name == name);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (j->type == type) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (is_new)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering *is_new = false;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return j;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (name->meta.job && name->meta.job->type == type)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j = name->meta.job;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering else if (!(j = job_new(m, type, name)))
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return NULL;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering job_free(j);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return NULL;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->transaction_next = f;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (f)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering f->transaction_prev = j;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->generation = 0;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->marker = NULL;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->matters_to_anchor = false;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (is_new)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering *is_new = true;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return j;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering}
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringvoid manager_transaction_unlink_job(Manager *m, Job *j) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(j);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (j->transaction_prev)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->transaction_prev->transaction_next = j->transaction_next;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering else if (j->transaction_next)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering else
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering hashmap_remove_value(m->transaction_jobs, j->name, j);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (j->transaction_next)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->transaction_next->transaction_prev = j->transaction_prev;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering j->transaction_prev = j->transaction_next = NULL;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering while (j->subject_list)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering job_dependency_free(j->subject_list);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering while (j->object_list) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering Job *other = j->object_list->matters ? j->object_list->subject : NULL;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering job_dependency_free(j->object_list);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (other) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering log_debug("Deleting job %s as dependency of job %s", name_id(other->name), name_id(j->name));
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering transaction_delete_job(m, other);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering}
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringstatic int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering Job *ret;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering void *state;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering Name *dep;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering int r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering bool is_new;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(type < _JOB_TYPE_MAX);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(name);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (name->meta.state != NAME_LOADED)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return -EINVAL;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* First add the job. */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (!(ret = transaction_add_one_job(m, type, name, &is_new)))
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return -ENOMEM;
dd2fd155901a965ec0efa3adc460b33d2048d4c2Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Then, add a link to the job. */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (!job_dependency_new(by, ret, matters))
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return -ENOMEM;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (is_new) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* Finally, recursively add in all dependencies. */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (type == JOB_START || type == JOB_RELOAD_OR_START) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto fail;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto fail;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto fail;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto fail;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
7edecf218e5884ec8d1549707b4c7a0572c2d93bThomas Hindoe Paaboel Andersen if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto fail;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto fail;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering goto fail;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return 0;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringfail:
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering}
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringint manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering int r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering Job *ret;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(type < _JOB_TYPE_MAX);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(name);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(mode < _JOB_MODE_MAX);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering transaction_abort(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering }
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if ((r = transaction_activate(m, mode)) < 0)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return r;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering if (_ret)
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering *_ret = ret;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return 0;
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering}
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart PoetteringJob *manager_get_job(Manager *m, uint32_t id) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return hashmap_get(m->jobs, UINT32_TO_PTR(id));
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering}
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart PoetteringName *manager_get_name(Manager *m, const char *name) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(m);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering assert(name);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering return hashmap_get(m->names, name);
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering}
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poetteringstatic int dispatch_load_queue(Manager *m) {
615aaf412c441e67c7cad2e5ae966b03fca1fae7Lennart Poettering Meta *meta;
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering assert(m);
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering /* Make sure we are not run recursively */
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering if (m->dispatching_load_queue)
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering return 0;
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering m->dispatching_load_queue = true;
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering /* Dispatches the load queue. Takes a name from the queue and
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering * tries to load its data until the queue is empty */
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering while ((meta = m->load_queue)) {
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering name_load(NAME(meta));
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering LIST_REMOVE(Meta, m->load_queue, meta);
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering }
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering m->dispatching_load_queue = false;
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering return 0;
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering}
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poetteringint manager_load_name(Manager *m, const char *name, Name **_ret) {
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering Name *ret;
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering NameType t;
d2c643c662e2cb3b6d1445c17c80b4b2998d5c61Lennart Poettering int r;
11ea2781eea4b912d2feb26785ece475e504c57bLennart Poettering char *n;
d4f5a1f47dbd04f26f2ddf951c97c4cb0ebbbe62David Herrmann
d4f5a1f47dbd04f26f2ddf951c97c4cb0ebbbe62David Herrmann assert(m);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering assert(name);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering assert(_ret);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering if (!name_is_valid(name))
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering return -EINVAL;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering /* This will load the service information files, but not actually
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering * start any services or anything */
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering if ((ret = manager_get_name(m, name)))
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering goto finish;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering return -EINVAL;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering if (!(ret = name_new(m)))
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering return -ENOMEM;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering ret->meta.type = t;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering if (!(n = strdup(name))) {
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering name_free(ret);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering return -ENOMEM;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering }
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering if ((r = set_put(ret->meta.names, n)) < 0) {
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering name_free(ret);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering free(n);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering return r;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering }
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering if ((r = name_link(ret)) < 0) {
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering name_free(ret);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering return r;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering }
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering /* At this point the new entry is created and linked. However,
122676c9d9737f8591429fd5ffc9b454a994741dLennart Poettering * not loaded. Now load this entry and all its dependencies
122676c9d9737f8591429fd5ffc9b454a994741dLennart Poettering * recursively */
122676c9d9737f8591429fd5ffc9b454a994741dLennart Poettering
122676c9d9737f8591429fd5ffc9b454a994741dLennart Poettering dispatch_load_queue(m);
122676c9d9737f8591429fd5ffc9b454a994741dLennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poetteringfinish:
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering *_ret = ret;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering return 0;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering}
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poetteringvoid manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering void *state;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering Job *j;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering assert(s);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering assert(f);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering HASHMAP_FOREACH(j, s->jobs, state)
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering job_dump(j, f, prefix);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering}
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poetteringvoid manager_dump_names(Manager *s, FILE *f, const char *prefix) {
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering void *state;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering Name *n;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering const char *t;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering assert(s);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering assert(f);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering HASHMAP_FOREACH_KEY(n, t, s->names, state)
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering if (name_id(n) == t)
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering name_dump(n, f, prefix);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering}
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poetteringvoid manager_clear_jobs(Manager *m) {
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering Job *j;
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering assert(m);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering transaction_abort(m);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering while ((j = hashmap_first(m->jobs)))
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering job_free(j);
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering}
f9e00a9f5870a9bcae2de8bf1cb3ce04703112e1Lennart Poettering