+ f = hashmap_get(m->transaction_jobs, unit);
+
+ LIST_FOREACH(transaction, j, f) {
+ assert(j->unit == unit);
+
+ if (j->type == type) {
+ if (is_new)
+ *is_new = false;
+ return j;
+ }
+ }
+
+ if (unit->meta.job && unit->meta.job->type == type)
+ j = unit->meta.job;
+ else if (!(j = job_new(m, type, unit)))
+ return NULL;
+
+ j->generation = 0;
+ j->marker = NULL;
+ j->matters_to_anchor = false;
+ j->forced = force;
+
+ LIST_PREPEND(Job, transaction, f, j);
+
+ if ((r = hashmap_replace(m->transaction_jobs, unit, f)) < 0) {
+ job_free(j);
+ return NULL;
+ }
+
+ if (is_new)
+ *is_new = true;
+
+ return j;
+}
+
+void manager_transaction_unlink_job(Manager *m, Job *j) {
+ assert(m);
+ assert(j);
+
+ if (j->transaction_prev)
+ j->transaction_prev->transaction_next = j->transaction_next;
+ else if (j->transaction_next)
+ hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
+ else
+ hashmap_remove_value(m->transaction_jobs, j->unit, j);
+
+ if (j->transaction_next)
+ j->transaction_next->transaction_prev = j->transaction_prev;
+
+ j->transaction_prev = j->transaction_next = NULL;
+
+ while (j->subject_list)
+ job_dependency_free(j->subject_list);
+
+ while (j->object_list) {
+ Job *other = j->object_list->matters ? j->object_list->subject : NULL;
+
+ job_dependency_free(j->object_list);
+
+ if (other) {
+ log_debug("Deleting job %s/%s as dependency of job %s/%s",
+ unit_id(other->unit), job_type_to_string(other->type),
+ unit_id(j->unit), job_type_to_string(j->type));
+ transaction_delete_job(m, other);
+ }
+ }
+}
+
+static int transaction_add_job_and_dependencies(Manager *m, JobType type, Unit *unit, Job *by, bool matters, bool force, Job **_ret) {
+ Job *ret;
+ Iterator i;
+ Unit *dep;
+ int r;
+ bool is_new;
+
+ assert(m);
+ assert(type < _JOB_TYPE_MAX);
+ assert(unit);
+
+ if (unit->meta.load_state != UNIT_LOADED)
+ return -EINVAL;
+
+ if (!unit_job_is_applicable(unit, type))
+ return -EBADR;
+
+ /* First add the job. */
+ if (!(ret = transaction_add_one_job(m, type, unit, force, &is_new)))
+ return -ENOMEM;
+
+ /* Then, add a link to the job. */
+ if (!job_dependency_new(by, ret, matters))
+ return -ENOMEM;
+
+ if (is_new) {
+ /* Finally, recursively add in all dependencies. */
+ if (type == JOB_START || type == JOB_RELOAD_OR_START) {
+ SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
+ goto fail;
+ SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_SOFT_REQUIRES], i)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
+ goto fail;
+ SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0 && r != -EBADR)
+ goto fail;
+ SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
+ goto fail;
+ SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_SOFT_REQUISITE], i)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
+ goto fail;
+ SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
+ if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
+ goto fail;
+
+ } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
+
+ SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
+ if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
+ goto fail;
+ }
+
+ /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
+ }
+
+ return 0;
+
+fail:
+ return r;
+}
+
+int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool force, Job **_ret) {
+ int r;
+ Job *ret;
+
+ assert(m);
+ assert(type < _JOB_TYPE_MAX);
+ assert(unit);
+ assert(mode < _JOB_MODE_MAX);
+
+ log_debug("Trying to enqueue job %s/%s", unit_id(unit), job_type_to_string(type));
+
+ if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, force, &ret))) {
+ transaction_abort(m);
+ return r;
+ }
+
+ if ((r = transaction_activate(m, mode)) < 0)