+ assert_not_reached("Unknown job type");
+ }
+
+ 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;
+ UnitType 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));
+
+ /* 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_schedule_run(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);
+ }