1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
13 Manager* manager_new(void) {
16 if (!(m = new0(Manager, 1)))
19 if (!(m->names = hashmap_new(string_hash_func, string_compare_func)))
22 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
25 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
35 void manager_free(Manager *m) {
41 while ((n = hashmap_first(m->names)))
44 while ((j = hashmap_steal_first(m->transaction_jobs)))
47 hashmap_free(m->names);
48 hashmap_free(m->jobs);
49 hashmap_free(m->transaction_jobs);
54 static void transaction_delete_job(Manager *m, Job *j) {
58 manager_transaction_unlink_job(m, j);
64 static void transaction_abort(Manager *m) {
69 while ((j = hashmap_first(m->transaction_jobs)))
71 transaction_delete_job(m, j);
75 assert(hashmap_isempty(m->transaction_jobs));
76 assert(!m->transaction_anchor);
79 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
84 for (l = j ? j->subject_list : m->transaction_anchor; l; l = l->subject_next) {
86 /* This link does not matter */
90 /* This name has already been marked */
91 if (l->object->generation == generation)
94 l->object->matters_to_anchor = true;
95 l->object->generation = generation;
97 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
101 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
103 (a == c && b == d) ||
107 static int types_merge(JobType *a, JobType b) {
111 if (types_match(*a, b, JOB_START, JOB_VERIFY_STARTED))
113 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
114 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
115 types_match(*a, b, JOB_VERIFY_STARTED, JOB_RELOAD_OR_START) ||
116 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
117 *a = JOB_RELOAD_OR_START;
118 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
119 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
120 types_match(*a, b, JOB_VERIFY_STARTED, JOB_RESTART) ||
121 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
122 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
123 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
124 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
126 else if (types_match(*a, b, JOB_VERIFY_STARTED, JOB_RELOAD))
128 else if (types_match(*a, b, JOB_VERIFY_STARTED, JOB_TRY_RESTART) ||
129 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
130 *a = JOB_TRY_RESTART;
135 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
136 JobDependency *l, *last;
140 assert(j->name == other->name);
144 j->state = JOB_WAITING;
146 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
148 /* Patch us in as new owner of the JobDependency objects */
150 for (l = other->subject_list; l; l = l->subject_next) {
151 assert(l->subject == other);
156 /* Merge both lists */
158 last->subject_next = j->subject_list;
160 j->subject_list->subject_prev = last;
161 j->subject_list = other->subject_list;
164 /* Patch us in as new owner of the JobDependency objects */
166 for (l = other->object_list; l; l = l->object_next) {
167 assert(l->object == other);
172 /* Merge both lists */
174 last->object_next = j->object_list;
176 j->object_list->object_prev = last;
177 j->object_list = other->object_list;
180 /* Kill the other job */
181 other->subject_list = NULL;
182 other->object_list = NULL;
183 transaction_delete_job(m, other);
186 static int transaction_merge_jobs(Manager *m) {
193 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
197 for (k = j->transaction_next; k; k = k->transaction_next)
198 if ((r = types_merge(&t, k->type)) < 0)
201 while ((k = j->transaction_next)) {
203 transaction_merge_and_delete_job(m, k, j, t);
206 transaction_merge_and_delete_job(m, j, k, t);
209 assert(!j->transaction_next);
210 assert(!j->transaction_prev);
216 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
224 /* Did we find a cycle? */
225 if (j->marker && j->generation == generation) {
228 /* So, we already have been here. We have a
229 * cycle. Let's try to break it. We go backwards in our
230 * path and try to find a suitable job to remove. */
232 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
233 if (!k->matters_to_anchor) {
234 log_debug("Breaking order cycle by deleting job %s", name_id(k->name));
235 transaction_delete_job(m, k);
239 /* Check if this in fact was the beginning of
249 j->generation = generation;
251 /* We assume that the the dependencies are both-ways, and
252 * hence can ignore NAME_AFTER */
254 SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) {
257 if (!(o = hashmap_get(m->transaction_jobs, n)))
258 if (!(o = n->meta.job))
261 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
268 static int transaction_verify_order(Manager *m, unsigned *generation) {
280 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
283 assert(!j->transaction_next);
284 assert(!j->transaction_prev);
286 if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0) {
288 /* There was a cycleq, but it was fixed,
289 * we need to restart our algorithm */
303 static void transaction_collect_garbage(Manager *m) {
314 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
318 transaction_delete_job(m, j);
326 static int transaction_is_destructive(Manager *m, JobMode mode) {
332 /* Checks whether applying this transaction means that
333 * existing jobs would be replaced */
335 HASHMAP_FOREACH(j, m->transaction_jobs, state)
336 if (j->name->meta.job && j->name->meta.job != j)
342 static int transaction_apply(Manager *m, JobMode mode) {
347 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
351 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
355 while ((j = hashmap_steal_first(m->transaction_jobs))) {
359 if (j->name->meta.job)
360 job_free(j->name->meta.job);
362 j->name->meta.job = j;
365 /* We're fully installed. Now let's free data we don't
368 assert(!j->transaction_next);
369 assert(!j->transaction_prev);
371 while (j->subject_list)
372 job_dependency_free(j->subject_list);
373 while (j->object_list)
374 job_dependency_free(j->object_list);
381 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
385 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
392 static int transaction_activate(Manager *m, JobMode mode) {
394 unsigned generation = 1;
398 /* This applies the changes recorded in transaction_jobs to
399 * the actual list of jobs, if possible. */
401 /* First step: figure out which jobs matter */
402 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
404 /* Second step: let's merge entries we can merge */
405 if ((r = transaction_merge_jobs(m)) < 0)
408 /* Third step: verify order makes sense */
409 if ((r = transaction_verify_order(m, &generation)) < 0)
412 /* Third step: do garbage colletion */
413 transaction_collect_garbage(m);
415 /* Fourth step: check whether we can actually apply this */
416 if (mode == JOB_FAIL)
417 if ((r = transaction_is_destructive(m, mode)) < 0)
420 /* Fifth step: apply changes */
421 if ((r = transaction_apply(m, mode)) < 0)
424 assert(hashmap_isempty(m->transaction_jobs));
425 assert(!m->transaction_anchor);
430 transaction_abort(m);
434 static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) {
441 /* Looks for an axisting prospective job and returns that. If
442 * it doesn't exist it is created and added to the prospective
445 f = hashmap_get(m->transaction_jobs, name);
447 for (j = f; j; j = j->transaction_next) {
448 assert(j->name == name);
450 if (j->type == type) {
457 if (name->meta.job && name->meta.job->type == type)
459 else if (!(j = job_new(m, type, name)))
462 if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
467 j->transaction_next = f;
470 f->transaction_prev = j;
474 j->matters_to_anchor = false;
482 void manager_transaction_unlink_job(Manager *m, Job *j) {
486 if (j->transaction_prev)
487 j->transaction_prev->transaction_next = j->transaction_next;
488 else if (j->transaction_next)
489 hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
491 hashmap_remove_value(m->transaction_jobs, j->name, j);
493 if (j->transaction_next)
494 j->transaction_next->transaction_prev = j->transaction_prev;
496 j->transaction_prev = j->transaction_next = NULL;
498 while (j->subject_list)
499 job_dependency_free(j->subject_list);
501 while (j->object_list) {
502 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
504 job_dependency_free(j->object_list);
507 log_debug("Deleting job %s dependency of job %s", name_id(other->name), name_id(j->name));
508 transaction_delete_job(m, other);
513 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
521 assert(type < _JOB_TYPE_MAX);
524 if (name->meta.state != NAME_LOADED)
527 /* First add the job. */
528 if (!(ret = transaction_add_one_job(m, type, name, &is_new)))
531 /* Then, add a link to the job. */
532 if (!job_dependency_new(by, ret, matters))
536 /* Finally, recursively add in all dependencies. */
537 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
538 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
539 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0)
541 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
542 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0)
544 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
545 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0)
547 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
548 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0)
550 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
551 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0)
553 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
554 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0)
557 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
559 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
560 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0)
564 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
573 int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) {
578 assert(type < _JOB_TYPE_MAX);
580 assert(mode < _JOB_MODE_MAX);
582 if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) {
583 transaction_abort(m);
587 if ((r = transaction_activate(m, mode)) < 0)
596 Job *manager_get_job(Manager *m, uint32_t id) {
599 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
602 Name *manager_get_name(Manager *m, const char *name) {
606 return hashmap_get(m->names, name);
609 static int dispatch_load_queue(Manager *m) {
614 /* Make sure we are not run recursively */
615 if (m->dispatching_load_queue)
618 m->dispatching_load_queue = true;
620 /* Dispatches the load queue. Takes a name from the queue and
621 * tries to load its data until the queue is empty */
623 while ((meta = m->load_queue)) {
624 name_load(NAME(meta));
625 LIST_REMOVE(Meta, m->load_queue, meta);
628 m->dispatching_load_queue = false;
633 int manager_load_name(Manager *m, const char *name, Name **_ret) {
643 if (!name_is_valid(name))
646 /* This will load the service information files, but not actually
647 * start any services or anything */
649 if ((ret = manager_get_name(m, name)))
652 if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
655 if (!(ret = name_new(m)))
660 if (!(n = strdup(name))) {
665 if (set_put(ret->meta.names, n) < 0) {
671 if ((r = name_link(ret)) < 0) {
676 /* At this point the new entry is created and linked. However,
677 * not loaded. Now load this entry and all its dependencies
680 dispatch_load_queue(m);
688 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
695 HASHMAP_FOREACH(j, s->jobs, state)
696 job_dump(j, f, prefix);
699 void manager_dump_names(Manager *s, FILE *f, const char *prefix) {
707 HASHMAP_FOREACH_KEY(n, t, s->names, state)
709 name_dump(n, f, prefix);
712 void manager_clear_jobs(Manager *m) {
717 transaction_abort(m);
719 while ((j = hashmap_first(m->jobs)))