+
+ if (r == -EALREADY)
+ r = job_finish_and_invalidate(j, true);
+ else if (r == -EAGAIN) {
+ j->state = JOB_WAITING;
+ return -EAGAIN;
+ } else if (r < 0)
+ r = job_finish_and_invalidate(j, false);
+
+ return r;
+}
+
+int job_finish_and_invalidate(Job *j, bool success) {
+ Unit *u;
+ Unit *other;
+ JobType t;
+ Iterator i;
+
+ assert(j);
+ assert(j->installed);
+
+ log_debug("Job %s/%s finished, success=%s", unit_id(j->unit), job_type_to_string(j->type), yes_no(success));
+ job_add_to_dbus_queue(j);
+
+ /* Patch restart jobs so that they become normal start jobs */
+ if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
+
+ log_debug("Converting job %s/%s → %s/%s",
+ unit_id(j->unit), job_type_to_string(j->type),
+ unit_id(j->unit), job_type_to_string(JOB_START));
+
+ j->state = JOB_RUNNING;
+ j->type = JOB_START;
+
+ job_add_to_run_queue(j);
+ return 0;
+ }
+
+ u = j->unit;
+ t = j->type;
+ job_free(j);
+
+ /* Fail depending jobs on failure */
+ if (!success) {
+
+ if (t == JOB_START ||
+ t == JOB_VERIFY_ACTIVE ||
+ t == JOB_RELOAD_OR_START) {
+
+ SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
+ if (other->meta.job &&
+ (other->meta.type == JOB_START ||
+ other->meta.type == JOB_VERIFY_ACTIVE ||
+ other->meta.type == JOB_RELOAD_OR_START))
+ job_finish_and_invalidate(other->meta.job, false);
+
+ SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
+ if (other->meta.job &&
+ !other->meta.job->forced &&
+ (other->meta.type == JOB_START ||
+ other->meta.type == JOB_VERIFY_ACTIVE ||
+ other->meta.type == JOB_RELOAD_OR_START))
+ job_finish_and_invalidate(other->meta.job, false);
+
+ } else if (t == JOB_STOP) {
+
+ SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
+ if (other->meta.job &&
+ (t == JOB_START ||
+ t == JOB_VERIFY_ACTIVE ||
+ t == JOB_RELOAD_OR_START))
+ job_finish_and_invalidate(other->meta.job, false);
+ }
+ }
+
+ /* Try to start the next jobs that can be started */
+ SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
+ if (other->meta.job)
+ job_add_to_run_queue(other->meta.job);
+ SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
+ if (other->meta.job)
+ job_add_to_run_queue(other->meta.job);
+
+ return 0;
+}
+
+void job_add_to_run_queue(Job *j) {
+ assert(j);
+ assert(j->installed);
+
+ if (j->in_run_queue)
+ return;
+
+ LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
+ j->in_run_queue = true;
+}
+
+void job_add_to_dbus_queue(Job *j) {
+ assert(j);
+ assert(j->installed);
+
+ if (j->in_dbus_queue)
+ return;
+
+ LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
+ j->in_dbus_queue = true;