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 /* Deletes one job from the transaction */
60 manager_transaction_unlink_job(m, j);
66 static void transaction_delete_name(Manager *m, Name *n) {
69 /* Deletes all jobs associated with a certain name from the
72 while ((j = hashmap_get(m->transaction_jobs, n)))
73 transaction_delete_job(m, j);
76 static void transaction_abort(Manager *m) {
81 while ((j = hashmap_first(m->transaction_jobs)))
83 transaction_delete_job(m, j);
87 assert(hashmap_isempty(m->transaction_jobs));
88 assert(!m->transaction_anchor);
91 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
96 /* A recursive sweep through the graph that marks all names
97 * that matter to the anchor job, i.e. are directly or
98 * indirectly a dependency of the anchor job via paths that
99 * are fully marked as mattering. */
101 for (l = j ? j->subject_list : m->transaction_anchor; l; l = l->subject_next) {
103 /* This link does not matter */
107 /* This name has already been marked */
108 if (l->object->generation == generation)
111 l->object->matters_to_anchor = true;
112 l->object->generation = generation;
114 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
118 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
119 JobDependency *l, *last;
123 assert(j->name == other->name);
126 /* Merges 'other' into 'j' and then deletes j. */
129 j->state = JOB_WAITING;
130 j->forced = j->forced || other->forced;
132 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
134 /* Patch us in as new owner of the JobDependency objects */
136 for (l = other->subject_list; l; l = l->subject_next) {
137 assert(l->subject == other);
142 /* Merge both lists */
144 last->subject_next = j->subject_list;
146 j->subject_list->subject_prev = last;
147 j->subject_list = other->subject_list;
150 /* Patch us in as new owner of the JobDependency objects */
152 for (l = other->object_list; l; l = l->object_next) {
153 assert(l->object == other);
158 /* Merge both lists */
160 last->object_next = j->object_list;
162 j->object_list->object_prev = last;
163 j->object_list = other->object_list;
166 /* Kill the other job */
167 other->subject_list = NULL;
168 other->object_list = NULL;
169 transaction_delete_job(m, other);
172 static int delete_one_unmergeable_job(Manager *m, Job *j) {
177 /* Tries to delete one item in the linked list
178 * j->transaction_next->transaction_next->... that conflicts
179 * whith another one, in an attempt to make an inconsistent
180 * transaction work. */
182 /* We rely here on the fact that if a merged with b does not
183 * merge with c, either a or b merge with c neither */
184 for (; j; j = j->transaction_next)
185 for (k = j->transaction_next; k; k = k->transaction_next) {
188 /* Is this one mergeable? Then skip it */
189 if (job_type_is_mergeable(j->type, k->type))
192 /* Ok, we found two that conflict, let's see if we can
193 * drop one of them */
194 if (!j->matters_to_anchor)
196 else if (!k->matters_to_anchor)
201 /* Ok, we can drop one, so let's do so. */
202 log_debug("Try to fix job merging by deleting job %s/%s", name_id(d->name), job_type_to_string(d->type));
203 transaction_delete_job(m, d);
210 static int transaction_merge_jobs(Manager *m) {
217 /* First step, check whether any of the jobs for one specific
218 * task conflict. If so, try to drop one of them. */
219 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
224 for (k = j->transaction_next; k; k = k->transaction_next) {
225 if ((r = job_type_merge(&t, k->type)) >= 0)
228 /* OK, we could not merge all jobs for this
229 * action. Let's see if we can get rid of one
232 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
233 /* Ok, we managed to drop one, now
234 * let's ask our callers to call us
235 * again after garbage collecting */
238 /* We couldn't merge anything. Failure */
243 /* Second step, merge the jobs. */
244 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
248 /* Merge all transactions */
249 for (k = j->transaction_next; k; k = k->transaction_next)
250 assert_se(job_type_merge(&t, k->type) == 0);
252 /* If an active job is mergeable, merge it too */
253 if (j->name->meta.job)
254 job_type_merge(&t, j->name->meta.job->type); /* Might fail. Which is OK */
256 while ((k = j->transaction_next)) {
258 transaction_merge_and_delete_job(m, k, j, t);
261 transaction_merge_and_delete_job(m, j, k, t);
264 assert(!j->transaction_next);
265 assert(!j->transaction_prev);
271 static bool name_matters_to_anchor(Name *n, Job *j) {
273 assert(!j->transaction_prev);
275 /* Checks whether at least one of the jobs for this name
276 * matters to the anchor. */
278 for (; j; j = j->transaction_next)
279 if (j->matters_to_anchor)
285 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
292 assert(!j->transaction_prev);
294 /* Does a recursive sweep through the ordering graph, looking
295 * for a cycle. If we find cycle we try to break it. */
297 /* Did we find a cycle? */
298 if (j->marker && j->generation == generation) {
301 /* So, we already have been here. We have a
302 * cycle. Let's try to break it. We go backwards in
303 * our path and try to find a suitable job to
304 * remove. We use the marker to find our way back,
305 * since smart how we are we stored our way back in
308 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
311 !name_matters_to_anchor(k->name, k)) {
312 /* Ok, we can drop this one, so let's
314 log_debug("Breaking order cycle by deleting job %s/%s", name_id(k->name), job_type_to_string(k->type));
315 transaction_delete_name(m, k->name);
319 /* Check if this in fact was the beginning of
328 /* Make the marker point to where we come from, so that we can
329 * find our way backwards if we want to break a cycle */
331 j->generation = generation;
333 /* We assume that the the dependencies are bidirectional, and
334 * hence can ignore NAME_AFTER */
335 SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) {
338 /* Is there a job for this name? */
339 if (!(o = hashmap_get(m->transaction_jobs, n)))
341 /* Ok, there is no job for this in the
342 * transaction, but maybe there is already one
344 if (!(o = n->meta.job))
347 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
354 static int transaction_verify_order(Manager *m, unsigned *generation) {
362 /* Check if the ordering graph is cyclic. If it is, try to fix
363 * that up by dropping one of the jobs. */
365 HASHMAP_FOREACH(j, m->transaction_jobs, state)
366 if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)
372 static void transaction_collect_garbage(Manager *m) {
377 /* Drop jobs that are not required by any other job */
385 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
389 log_debug("Garbage collecting job %s/%s", name_id(j->name), job_type_to_string(j->type));
390 transaction_delete_job(m, j);
398 static int transaction_is_destructive(Manager *m, JobMode mode) {
404 /* Checks whether applying this transaction means that
405 * existing jobs would be replaced */
407 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
410 assert(!j->transaction_prev);
411 assert(!j->transaction_next);
413 if (j->name->meta.job &&
414 j->name->meta.job != j &&
415 !job_type_is_superset(j->type, j->name->meta.job->type))
422 static void transaction_minimize_impact(Manager *m) {
426 /* Drops all unnecessary jobs that reverse already active jobs
427 * or that stop a running service. */
435 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
436 for (; j; j = j->transaction_next) {
438 /* If it matters, we shouldn't drop it */
439 if (j->matters_to_anchor)
442 /* Would this stop a running service?
443 * Would this change an existing job?
444 * If so, let's drop this entry */
445 if ((j->type != JOB_STOP || NAME_IS_INACTIVE_OR_DEACTIVATING(name_active_state(j->name))) &&
446 (!j->name->meta.job || job_type_is_conflicting(j->type, j->name->meta.job->state)))
449 /* Ok, let's get rid of this */
450 log_debug("Deleting %s/%s to minimize impact", name_id(j->name), job_type_to_string(j->type));
451 transaction_delete_job(m, j);
463 static int transaction_apply(Manager *m, JobMode mode) {
468 /* Moves the transaction jobs to the set of active jobs */
470 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
472 assert(!j->transaction_prev);
473 assert(!j->transaction_next);
478 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
482 while ((j = hashmap_steal_first(m->transaction_jobs))) {
486 if (j->name->meta.job)
487 job_free(j->name->meta.job);
489 j->name->meta.job = j;
492 /* We're fully installed. Now let's free data we don't
495 assert(!j->transaction_next);
496 assert(!j->transaction_prev);
498 while (j->subject_list)
499 job_dependency_free(j->subject_list);
500 while (j->object_list)
501 job_dependency_free(j->object_list);
504 m->transaction_anchor = NULL;
510 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
514 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
520 static int transaction_activate(Manager *m, JobMode mode) {
522 unsigned generation = 1;
526 /* This applies the changes recorded in transaction_jobs to
527 * the actual list of jobs, if possible. */
529 /* First step: figure out which jobs matter */
530 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
532 /* Second step: Try not to stop any running services if
533 * we don't have to. Don't try to reverse running
534 * jobs if we don't have to. */
535 transaction_minimize_impact(m);
538 /* Third step: Let's remove unneeded jobs that might
540 transaction_collect_garbage(m);
542 /* Fourth step: verify order makes sense and correct
543 * cycles if necessary and possible */
544 if ((r = transaction_verify_order(m, &generation)) >= 0)
550 /* Let's see if the resulting transaction ordering
551 * graph is still cyclic... */
555 /* Fifth step: let's drop unmergeable entries if
556 * necessary and possible, merge entries we can
558 if ((r = transaction_merge_jobs(m)) >= 0)
564 /* Sixth step: an entry got dropped, let's garbage
565 * collect its dependencies. */
566 transaction_collect_garbage(m);
568 /* Let's see if the resulting transaction still has
569 * unmergeable entries ... */
572 /* Seventh step: check whether we can actually apply this */
573 if (mode == JOB_FAIL)
574 if ((r = transaction_is_destructive(m, mode)) < 0)
577 /* Eights step: apply changes */
578 if ((r = transaction_apply(m, mode)) < 0)
581 assert(hashmap_isempty(m->transaction_jobs));
582 assert(!m->transaction_anchor);
587 transaction_abort(m);
591 static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool force, bool *is_new) {
598 /* Looks for an axisting prospective job and returns that. If
599 * it doesn't exist it is created and added to the prospective
602 f = hashmap_get(m->transaction_jobs, name);
604 for (j = f; j; j = j->transaction_next) {
605 assert(j->name == name);
607 if (j->type == type) {
614 if (name->meta.job && name->meta.job->type == type)
616 else if (!(j = job_new(m, type, name)))
619 if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
624 j->transaction_next = f;
627 f->transaction_prev = j;
631 j->matters_to_anchor = false;
640 void manager_transaction_unlink_job(Manager *m, Job *j) {
644 if (j->transaction_prev)
645 j->transaction_prev->transaction_next = j->transaction_next;
646 else if (j->transaction_next)
647 hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
649 hashmap_remove_value(m->transaction_jobs, j->name, j);
651 if (j->transaction_next)
652 j->transaction_next->transaction_prev = j->transaction_prev;
654 j->transaction_prev = j->transaction_next = NULL;
656 while (j->subject_list)
657 job_dependency_free(j->subject_list);
659 while (j->object_list) {
660 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
662 job_dependency_free(j->object_list);
665 log_debug("Deleting job %s/%s as dependency of job %s/%s",
666 name_id(other->name), job_type_to_string(other->type),
667 name_id(j->name), job_type_to_string(j->type));
668 transaction_delete_job(m, other);
673 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
681 assert(type < _JOB_TYPE_MAX);
684 if (name->meta.load_state != NAME_LOADED)
687 if (!job_type_is_applicable(type, name->meta.type))
690 /* First add the job. */
691 if (!(ret = transaction_add_one_job(m, type, name, force, &is_new)))
694 /* Then, add a link to the job. */
695 if (!job_dependency_new(by, ret, matters))
699 /* Finally, recursively add in all dependencies. */
700 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
701 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
702 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
704 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
705 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
707 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
708 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0 && r != -EBADR)
710 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
711 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
713 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
714 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
716 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
717 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
720 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
722 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
723 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
727 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
736 int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) {
741 assert(type < _JOB_TYPE_MAX);
743 assert(mode < _JOB_MODE_MAX);
745 if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) {
746 transaction_abort(m);
750 if ((r = transaction_activate(m, mode)) < 0)
759 Job *manager_get_job(Manager *m, uint32_t id) {
762 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
765 Name *manager_get_name(Manager *m, const char *name) {
769 return hashmap_get(m->names, name);
772 static int dispatch_load_queue(Manager *m) {
777 /* Make sure we are not run recursively */
778 if (m->dispatching_load_queue)
781 m->dispatching_load_queue = true;
783 /* Dispatches the load queue. Takes a name from the queue and
784 * tries to load its data until the queue is empty */
786 while ((meta = m->load_queue)) {
787 name_load(NAME(meta));
788 LIST_REMOVE(Meta, m->load_queue, meta);
791 m->dispatching_load_queue = false;
796 int manager_load_name(Manager *m, const char *name, Name **_ret) {
806 if (!name_is_valid(name))
809 /* This will load the service information files, but not actually
810 * start any services or anything */
812 if ((ret = manager_get_name(m, name)))
815 if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
818 if (!(ret = name_new(m)))
823 if (!(n = strdup(name))) {
828 if ((r = set_put(ret->meta.names, n)) < 0) {
834 if ((r = name_link(ret)) < 0) {
839 /* At this point the new entry is created and linked. However,
840 * not loaded. Now load this entry and all its dependencies
843 dispatch_load_queue(m);
851 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
858 HASHMAP_FOREACH(j, s->jobs, state)
859 job_dump(j, f, prefix);
862 void manager_dump_names(Manager *s, FILE *f, const char *prefix) {
870 HASHMAP_FOREACH_KEY(n, t, s->names, state)
872 name_dump(n, f, prefix);
875 void manager_clear_jobs(Manager *m) {
880 transaction_abort(m);
882 while ((j = hashmap_first(m->jobs)))