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 log_debug("Garbage collecting job %s", name_id(j->name));
320 transaction_delete_job(m, j);
328 static int transaction_is_destructive(Manager *m, JobMode mode) {
334 /* Checks whether applying this transaction means that
335 * existing jobs would be replaced */
337 HASHMAP_FOREACH(j, m->transaction_jobs, state)
338 if (j->name->meta.job && j->name->meta.job != j)
344 static int transaction_apply(Manager *m, JobMode mode) {
349 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
353 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
357 while ((j = hashmap_steal_first(m->transaction_jobs))) {
361 if (j->name->meta.job)
362 job_free(j->name->meta.job);
364 j->name->meta.job = j;
367 /* We're fully installed. Now let's free data we don't
370 assert(!j->transaction_next);
371 assert(!j->transaction_prev);
373 while (j->subject_list)
374 job_dependency_free(j->subject_list);
375 while (j->object_list)
376 job_dependency_free(j->object_list);
383 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
387 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
394 static int transaction_activate(Manager *m, JobMode mode) {
396 unsigned generation = 1;
400 /* This applies the changes recorded in transaction_jobs to
401 * the actual list of jobs, if possible. */
403 /* First step: figure out which jobs matter */
404 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
406 /* Second step: let's merge entries we can merge */
407 if ((r = transaction_merge_jobs(m)) < 0)
410 /* Third step: verify order makes sense */
411 if ((r = transaction_verify_order(m, &generation)) < 0)
414 /* Third step: do garbage colletion */
415 transaction_collect_garbage(m);
417 /* Fourth step: check whether we can actually apply this */
418 if (mode == JOB_FAIL)
419 if ((r = transaction_is_destructive(m, mode)) < 0)
422 /* Fifth step: apply changes */
423 if ((r = transaction_apply(m, mode)) < 0)
426 assert(hashmap_isempty(m->transaction_jobs));
427 assert(!m->transaction_anchor);
432 transaction_abort(m);
436 static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) {
443 /* Looks for an axisting prospective job and returns that. If
444 * it doesn't exist it is created and added to the prospective
447 f = hashmap_get(m->transaction_jobs, name);
449 for (j = f; j; j = j->transaction_next) {
450 assert(j->name == name);
452 if (j->type == type) {
459 if (name->meta.job && name->meta.job->type == type)
461 else if (!(j = job_new(m, type, name)))
464 if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
469 j->transaction_next = f;
472 f->transaction_prev = j;
476 j->matters_to_anchor = false;
484 void manager_transaction_unlink_job(Manager *m, Job *j) {
488 if (j->transaction_prev)
489 j->transaction_prev->transaction_next = j->transaction_next;
490 else if (j->transaction_next)
491 hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
493 hashmap_remove_value(m->transaction_jobs, j->name, j);
495 if (j->transaction_next)
496 j->transaction_next->transaction_prev = j->transaction_prev;
498 j->transaction_prev = j->transaction_next = NULL;
500 while (j->subject_list)
501 job_dependency_free(j->subject_list);
503 while (j->object_list) {
504 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
506 job_dependency_free(j->object_list);
509 log_debug("Deleting job %s as dependency of job %s", name_id(other->name), name_id(j->name));
510 transaction_delete_job(m, other);
515 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
523 assert(type < _JOB_TYPE_MAX);
526 if (name->meta.state != NAME_LOADED)
529 /* First add the job. */
530 if (!(ret = transaction_add_one_job(m, type, name, &is_new)))
533 /* Then, add a link to the job. */
534 if (!job_dependency_new(by, ret, matters))
538 /* Finally, recursively add in all dependencies. */
539 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
540 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
541 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0)
543 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
544 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0)
546 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
547 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0)
549 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
550 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0)
552 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
553 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0)
555 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
556 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0)
559 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
561 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
562 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0)
566 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
575 int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) {
580 assert(type < _JOB_TYPE_MAX);
582 assert(mode < _JOB_MODE_MAX);
584 if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) {
585 transaction_abort(m);
589 if ((r = transaction_activate(m, mode)) < 0)
598 Job *manager_get_job(Manager *m, uint32_t id) {
601 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
604 Name *manager_get_name(Manager *m, const char *name) {
608 return hashmap_get(m->names, name);
611 static int dispatch_load_queue(Manager *m) {
616 /* Make sure we are not run recursively */
617 if (m->dispatching_load_queue)
620 m->dispatching_load_queue = true;
622 /* Dispatches the load queue. Takes a name from the queue and
623 * tries to load its data until the queue is empty */
625 while ((meta = m->load_queue)) {
626 name_load(NAME(meta));
627 LIST_REMOVE(Meta, m->load_queue, meta);
630 m->dispatching_load_queue = false;
635 int manager_load_name(Manager *m, const char *name, Name **_ret) {
645 if (!name_is_valid(name))
648 /* This will load the service information files, but not actually
649 * start any services or anything */
651 if ((ret = manager_get_name(m, name)))
654 if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
657 if (!(ret = name_new(m)))
662 if (!(n = strdup(name))) {
667 if (set_put(ret->meta.names, n) < 0) {
673 if ((r = name_link(ret)) < 0) {
678 /* At this point the new entry is created and linked. However,
679 * not loaded. Now load this entry and all its dependencies
682 dispatch_load_queue(m);
690 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
697 HASHMAP_FOREACH(j, s->jobs, state)
698 job_dump(j, f, prefix);
701 void manager_dump_names(Manager *s, FILE *f, const char *prefix) {
709 HASHMAP_FOREACH_KEY(n, t, s->names, state)
711 name_dump(n, f, prefix);
714 void manager_clear_jobs(Manager *m) {
719 transaction_abort(m);
721 while ((j = hashmap_first(m->jobs)))