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;
131 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
133 /* Patch us in as new owner of the JobDependency objects */
135 for (l = other->subject_list; l; l = l->subject_next) {
136 assert(l->subject == other);
141 /* Merge both lists */
143 last->subject_next = j->subject_list;
145 j->subject_list->subject_prev = last;
146 j->subject_list = other->subject_list;
149 /* Patch us in as new owner of the JobDependency objects */
151 for (l = other->object_list; l; l = l->object_next) {
152 assert(l->object == other);
157 /* Merge both lists */
159 last->object_next = j->object_list;
161 j->object_list->object_prev = last;
162 j->object_list = other->object_list;
165 /* Kill the other job */
166 other->subject_list = NULL;
167 other->object_list = NULL;
168 transaction_delete_job(m, other);
171 static int delete_one_unmergable_job(Manager *m, Job *j) {
176 /* Tries to delete one item in the linked list
177 * j->transaction_next->transaction_next->... that conflicts
178 * whith another one, in an attempt to make an inconsistent
179 * transaction work. */
181 /* We rely here on the fact that if a merged with b does not
182 * merge with c, either a or b merge with c neither */
183 for (; j; j = j->transaction_next)
184 for (k = j->transaction_next; k; k = k->transaction_next) {
187 /* Is this one mergeable? Then skip it */
188 if (job_type_mergeable(j->type, k->type))
191 /* Ok, we found two that conflict, let's see if we can
192 * drop one of them */
193 if (!j->matters_to_anchor)
195 else if (!k->matters_to_anchor)
200 /* Ok, we can drop one, so let's do so. */
201 log_debug("Try to fix job merging by deleting job %s", name_id(d->name));
202 transaction_delete_job(m, d);
209 static int transaction_merge_jobs(Manager *m) {
216 /* First step, check whether any of the jobs for one specific
217 * task conflict. If so, try to drop one of them. */
218 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
223 for (k = j->transaction_next; k; k = k->transaction_next) {
224 if ((r = job_type_merge(&t, k->type)) >= 0)
227 /* OK, we could not merge all jobs for this
228 * action. Let's see if we can get rid of one
231 if ((r = delete_one_unmergable_job(m, j)) >= 0)
232 /* Ok, we managed to drop one, now
233 * let's ask our callers to call us
234 * again after garbage collecting */
237 /* We couldn't merge anything. Failure */
242 /* Second step, merge the jobs. */
243 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
247 for (k = j->transaction_next; k; k = k->transaction_next)
248 assert_se(job_type_merge(&t, k->type) == 0);
250 while ((k = j->transaction_next)) {
252 transaction_merge_and_delete_job(m, k, j, t);
255 transaction_merge_and_delete_job(m, j, k, t);
258 assert(!j->transaction_next);
259 assert(!j->transaction_prev);
265 static bool name_matters_to_anchor(Name *n, Job *j) {
267 assert(!j->transaction_prev);
269 /* Checks whether at least one of the jobs for this name
270 * matters to the anchor. */
272 for (; j; j = j->transaction_next)
273 if (j->matters_to_anchor)
279 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
286 assert(!j->transaction_prev);
288 /* Does a recursive sweep through the ordering graph, looking
289 * for a cycle. If we find cycle we try to break it. */
291 /* Did we find a cycle? */
292 if (j->marker && j->generation == generation) {
295 /* So, we already have been here. We have a
296 * cycle. Let's try to break it. We go backwards in
297 * our path and try to find a suitable job to
298 * remove. We use the marker to find our way back,
299 * since smart how we are we stored our way back in
302 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
305 !name_matters_to_anchor(k->name, k)) {
306 /* Ok, we can drop this one, so let's
308 log_debug("Breaking order cycle by deleting job %s", name_id(k->name));
309 transaction_delete_name(m, k->name);
313 /* Check if this in fact was the beginning of
322 /* Make the marker point to where we come from, so that we can
323 * find our way backwards if we want to break a cycle */
325 j->generation = generation;
327 /* We assume that the the dependencies are bidirectional, and
328 * hence can ignore NAME_AFTER */
329 SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) {
332 /* Is there a job for this name? */
333 if (!(o = hashmap_get(m->transaction_jobs, n)))
335 /* Ok, there is no job for this in the
336 * transaction, but maybe there is already one
338 if (!(o = n->meta.job))
341 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
348 static int transaction_verify_order(Manager *m, unsigned *generation) {
356 /* Check if the ordering graph is cyclic. If it is, try to fix
357 * that up by dropping one of the jobs. */
359 HASHMAP_FOREACH(j, m->transaction_jobs, state)
360 if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)
366 static void transaction_collect_garbage(Manager *m) {
371 /* Drop jobs that are not required by any other job */
379 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
383 log_debug("Garbage collecting job %s", name_id(j->name));
385 transaction_delete_job(m, j);
393 static int transaction_is_destructive(Manager *m, JobMode mode) {
399 /* Checks whether applying this transaction means that
400 * existing jobs would be replaced */
402 HASHMAP_FOREACH(j, m->transaction_jobs, state)
403 if (j->name->meta.job &&
404 j->name->meta.job != j &&
405 !job_type_is_superset(j->type, j->name->meta.job->type))
411 static int transaction_apply(Manager *m, JobMode mode) {
416 /* Moves the transaction jobs to the set of active jobs */
418 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
422 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
426 while ((j = hashmap_steal_first(m->transaction_jobs))) {
430 if (j->name->meta.job)
431 job_free(j->name->meta.job);
433 j->name->meta.job = j;
436 /* We're fully installed. Now let's free data we don't
439 assert(!j->transaction_next);
440 assert(!j->transaction_prev);
442 while (j->subject_list)
443 job_dependency_free(j->subject_list);
444 while (j->object_list)
445 job_dependency_free(j->object_list);
448 m->transaction_anchor = NULL;
454 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
458 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
465 static int transaction_activate(Manager *m, JobMode mode) {
467 unsigned generation = 1;
471 /* This applies the changes recorded in transaction_jobs to
472 * the actual list of jobs, if possible. */
474 /* First step: figure out which jobs matter */
475 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
478 /* Second step: Let's remove unneeded jobs that might
480 transaction_collect_garbage(m);
482 /* Third step: verify order makes sense and correct
483 * cycles if necessary and possible */
484 if ((r = transaction_verify_order(m, &generation)) >= 0)
490 /* Let's see if the resulting transaction ordering
491 * graph is still cyclic... */
495 /* Fourth step: let's drop unmergable entries if
496 * necessary and possible, merge entries we can
498 if ((r = transaction_merge_jobs(m)) >= 0)
504 /* Fifth step: an entry got dropped, let's garbage
505 * collect its dependencies. */
506 transaction_collect_garbage(m);
508 /* Let's see if the resulting transaction still has
509 * unmergable entries ... */
512 /* Sith step: check whether we can actually apply this */
513 if (mode == JOB_FAIL)
514 if ((r = transaction_is_destructive(m, mode)) < 0)
517 /* Seventh step: apply changes */
518 if ((r = transaction_apply(m, mode)) < 0)
521 assert(hashmap_isempty(m->transaction_jobs));
522 assert(!m->transaction_anchor);
527 transaction_abort(m);
531 static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) {
538 /* Looks for an axisting prospective job and returns that. If
539 * it doesn't exist it is created and added to the prospective
542 f = hashmap_get(m->transaction_jobs, name);
544 for (j = f; j; j = j->transaction_next) {
545 assert(j->name == name);
547 if (j->type == type) {
554 if (name->meta.job && name->meta.job->type == type)
556 else if (!(j = job_new(m, type, name)))
559 if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
564 j->transaction_next = f;
567 f->transaction_prev = j;
571 j->matters_to_anchor = false;
579 void manager_transaction_unlink_job(Manager *m, Job *j) {
583 if (j->transaction_prev)
584 j->transaction_prev->transaction_next = j->transaction_next;
585 else if (j->transaction_next)
586 hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
588 hashmap_remove_value(m->transaction_jobs, j->name, j);
590 if (j->transaction_next)
591 j->transaction_next->transaction_prev = j->transaction_prev;
593 j->transaction_prev = j->transaction_next = NULL;
595 while (j->subject_list)
596 job_dependency_free(j->subject_list);
598 while (j->object_list) {
599 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
601 job_dependency_free(j->object_list);
604 log_debug("Deleting job %s as dependency of job %s", name_id(other->name), name_id(j->name));
605 transaction_delete_job(m, other);
610 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
618 assert(type < _JOB_TYPE_MAX);
621 if (name->meta.state != NAME_LOADED)
624 /* First add the job. */
625 if (!(ret = transaction_add_one_job(m, type, name, &is_new)))
628 /* Then, add a link to the job. */
629 if (!job_dependency_new(by, ret, matters))
633 /* Finally, recursively add in all dependencies. */
634 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
635 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
636 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0)
638 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
639 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0)
641 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
642 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0)
644 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
645 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0)
647 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
648 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0)
650 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
651 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0)
654 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
656 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
657 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0)
661 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
670 int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) {
675 assert(type < _JOB_TYPE_MAX);
677 assert(mode < _JOB_MODE_MAX);
679 if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) {
680 transaction_abort(m);
684 if ((r = transaction_activate(m, mode)) < 0)
693 Job *manager_get_job(Manager *m, uint32_t id) {
696 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
699 Name *manager_get_name(Manager *m, const char *name) {
703 return hashmap_get(m->names, name);
706 static int dispatch_load_queue(Manager *m) {
711 /* Make sure we are not run recursively */
712 if (m->dispatching_load_queue)
715 m->dispatching_load_queue = true;
717 /* Dispatches the load queue. Takes a name from the queue and
718 * tries to load its data until the queue is empty */
720 while ((meta = m->load_queue)) {
721 name_load(NAME(meta));
722 LIST_REMOVE(Meta, m->load_queue, meta);
725 m->dispatching_load_queue = false;
730 int manager_load_name(Manager *m, const char *name, Name **_ret) {
740 if (!name_is_valid(name))
743 /* This will load the service information files, but not actually
744 * start any services or anything */
746 if ((ret = manager_get_name(m, name)))
749 if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
752 if (!(ret = name_new(m)))
757 if (!(n = strdup(name))) {
762 if ((r = set_put(ret->meta.names, n)) < 0) {
768 if ((r = name_link(ret)) < 0) {
773 /* At this point the new entry is created and linked. However,
774 * not loaded. Now load this entry and all its dependencies
777 dispatch_load_queue(m);
785 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
792 HASHMAP_FOREACH(j, s->jobs, state)
793 job_dump(j, f, prefix);
796 void manager_dump_names(Manager *s, FILE *f, const char *prefix) {
804 HASHMAP_FOREACH_KEY(n, t, s->names, state)
806 name_dump(n, f, prefix);
809 void manager_clear_jobs(Manager *m) {
814 transaction_abort(m);
816 while ((j = hashmap_first(m->jobs)))