From 69dd2852bb2c433b517d89792adb4461a4178aa1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 9 Aug 2010 22:32:30 +0200 Subject: [PATCH] manager: when two pending jobs conflict, keep the one that "conflicts", remove the one that is "conflicted" This gives the writer of units control which unit is kept and which is stopped when two units conflict. --- fixme | 2 -- man/systemd.unit.xml | 17 +++++++++- man/systemd.xml | 70 +++++++++++++++++++++++++++++++-------- src/automount.c | 2 +- src/dbus-unit.h | 2 ++ src/job.c | 11 +++++- src/job.h | 3 +- src/manager.c | 70 +++++++++++++++++++++++++++++++++------ src/mount.c | 2 +- src/path.c | 2 +- src/service.c | 2 +- src/socket.c | 2 +- src/systemctl.c | 1 + src/timer.c | 2 +- src/unit.c | 8 ++++- src/unit.h | 3 +- units/graphical.target.m4 | 3 -- 17 files changed, 161 insertions(+), 41 deletions(-) diff --git a/fixme b/fixme index 765d9d121..373d100fa 100644 --- a/fixme +++ b/fixme @@ -1,7 +1,5 @@ * dot output for --test for 'initial description' -* conflicted-by: to have a defined winner for conflicts: - * check 'disable' "Warning: Unit file changed in disk, 'systemctl --system daemon-reload' recomended when does it do that? diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml index 585145ab3..e5d5968ab 100644 --- a/man/systemd.unit.xml +++ b/man/systemd.unit.xml @@ -336,7 +336,22 @@ independent of and orthogonal to the After= and Before= ordering - dependencies. + dependencies. + + If a unit A that conflicts with + a unit B is scheduled to be started at + the same time as B, the transaction + will either fail (in case both are + required part of the transaction) or + be modified to be fixed (in case one + or both jobs are not a required part + of the transaction). In the latter + case the job that is not the required + will be removed, or in case both are + not required the unit that conflicts + will be started and the unit that is + conflicted is + stopped. diff --git a/man/systemd.xml b/man/systemd.xml index d2112b1bf..b35469c10 100644 --- a/man/systemd.xml +++ b/man/systemd.xml @@ -166,9 +166,27 @@ Ask for confirmation when spawning processes. This switch has no effect when run as session instance. - + + + Show terse service + status information while booting. This + switch has no effect when run as + session instance. Takes a boolean + argument which may be omitted + which is interpreted as + . + + + - Show terse service status information while booting. This switch has no effect when run as session instance. + Controls whether + output of SysV init scripts will be + directed to the console. This switch + has no effect when run as session + instance. Takes a boolean argument + which may be omitted which is + interpreted as + . @@ -805,18 +823,6 @@ units. - - systemd.log_target= - systemd.log_level= - systemd.log_color= - systemd.log_location= - - Controls log output, - with the same effect as the - $SYSTEMD_LOG_TARGET, $SYSTEMD_LOG_LEVEL, $SYSTEMD_LOG_COLOR, $SYSTEMD_LOG_LOCATION - environment variables described above. - - systemd.dump_core= @@ -852,6 +858,16 @@ -1. + + systemd.confirm_spawn= + + Takes a boolean + argument. If + asks for confirmation when spawning + processes. Defaults to + . + + systemd.show_status= @@ -862,6 +878,32 @@ . + + systemd.sysv_console= + + Takes a boolean + argument. If + output of SysV init scripts will be + directed to the console. Defaults to + , unless + is passed as + kernel command line option in which + case it defaults to + . + + + + systemd.log_target= + systemd.log_level= + systemd.log_color= + systemd.log_location= + + Controls log output, + with the same effect as the + $SYSTEMD_LOG_TARGET, $SYSTEMD_LOG_LEVEL, $SYSTEMD_LOG_COLOR, $SYSTEMD_LOG_LOCATION + environment variables described above. + + diff --git a/src/automount.c b/src/automount.c index 57d106504..9843481a6 100644 --- a/src/automount.c +++ b/src/automount.c @@ -156,7 +156,7 @@ static int automount_add_default_dependencies(Automount *a) { if ((r = unit_add_dependency_by_name(UNIT(a), UNIT_AFTER, SPECIAL_FSCK_TARGET, NULL, true)) < 0) return r; - if ((r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0) + if ((r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0) return r; } diff --git a/src/dbus-unit.h b/src/dbus-unit.h index 0d1732270..0744377ed 100644 --- a/src/dbus-unit.h +++ b/src/dbus-unit.h @@ -70,6 +70,7 @@ " \n" \ " \n" \ " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -108,6 +109,7 @@ { "org.freedesktop.systemd1.Unit", "RequiredByOverridable",bus_unit_append_dependencies, "as", u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE] }, \ { "org.freedesktop.systemd1.Unit", "WantedBy", bus_unit_append_dependencies, "as", u->meta.dependencies[UNIT_WANTED_BY] }, \ { "org.freedesktop.systemd1.Unit", "Conflicts", bus_unit_append_dependencies, "as", u->meta.dependencies[UNIT_CONFLICTS] }, \ + { "org.freedesktop.systemd1.Unit", "ConflictedBy", bus_unit_append_dependencies, "as", u->meta.dependencies[UNIT_CONFLICTED_BY] }, \ { "org.freedesktop.systemd1.Unit", "Before", bus_unit_append_dependencies, "as", u->meta.dependencies[UNIT_BEFORE] }, \ { "org.freedesktop.systemd1.Unit", "After", bus_unit_append_dependencies, "as", u->meta.dependencies[UNIT_AFTER] }, \ { "org.freedesktop.systemd1.Unit", "OnFailure", bus_unit_append_dependencies, "as", u->meta.dependencies[UNIT_ON_FAILURE] }, \ diff --git a/src/job.c b/src/job.c index d08bfe9a7..c3b529e92 100644 --- a/src/job.c +++ b/src/job.c @@ -93,7 +93,7 @@ void job_free(Job *j) { free(j); } -JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) { +JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) { JobDependency *l; assert(object); @@ -109,6 +109,7 @@ JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) { l->subject = subject; l->object = object; l->matters = matters; + l->conflicts = conflicts; if (subject) LIST_PREPEND(JobDependency, subject, subject->subject_list, l); @@ -533,6 +534,14 @@ int job_finish_and_invalidate(Job *j, bool success) { other->meta.job->type == JOB_VERIFY_ACTIVE || other->meta.job->type == JOB_RELOAD_OR_START)) job_finish_and_invalidate(other->meta.job, false); + + SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i) + if (!other->meta.ignore_dependency_failure && + other->meta.job && + (other->meta.job->type == JOB_START || + other->meta.job->type == JOB_VERIFY_ACTIVE || + other->meta.job->type == JOB_RELOAD_OR_START)) + job_finish_and_invalidate(other->meta.job, false); } } diff --git a/src/job.h b/src/job.h index 41d697e84..a69c4aaf1 100644 --- a/src/job.h +++ b/src/job.h @@ -80,6 +80,7 @@ struct JobDependency { LIST_FIELDS(JobDependency, object); bool matters; + bool conflicts; }; struct Job { @@ -121,7 +122,7 @@ Job* job_new(Manager *m, JobType type, Unit *unit); void job_free(Job *job); void job_dump(Job *j, FILE*f, const char *prefix); -JobDependency* job_dependency_new(Job *subject, Job *object, bool matters); +JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts); void job_dependency_free(JobDependency *l); void job_dependency_delete(Job *subject, Job *object, bool *matters); diff --git a/src/manager.c b/src/manager.c index 9b6cbc94e..ddb253ae4 100644 --- a/src/manager.c +++ b/src/manager.c @@ -715,6 +715,20 @@ static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, Job other->object_list = NULL; transaction_delete_job(m, other, true); } +static bool job_is_conflicted_by(Job *j) { + JobDependency *l; + + assert(j); + + /* Returns true if this job is pulled in by a least one + * ConflictedBy dependency. */ + + LIST_FOREACH(object, l, j->object_list) + if (l->conflicts) + return true; + + return false; +} static int delete_one_unmergeable_job(Manager *m, Job *j) { Job *k; @@ -738,7 +752,36 @@ static int delete_one_unmergeable_job(Manager *m, Job *j) { /* Ok, we found two that conflict, let's see if we can * drop one of them */ - if (!j->matters_to_anchor) + if (!j->matters_to_anchor && !k->matters_to_anchor) { + + /* Both jobs don't matter, so let's + * find the one that is smarter to + * remove. Let's think positive and + * rather remove stops then starts -- + * except if something is being + * stopped because it is conflicted by + * another unit in which case we + * rather remove the start. */ + + log_debug("Looking at job %s/%s conflicted_by=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(j->type == JOB_STOP && job_is_conflicted_by(j))); + log_debug("Looking at job %s/%s conflicted_by=%s", k->unit->meta.id, job_type_to_string(k->type), yes_no(k->type == JOB_STOP && job_is_conflicted_by(k))); + + if (j->type == JOB_STOP) { + + if (job_is_conflicted_by(j)) + d = k; + else + d = j; + + } else if (k->type == JOB_STOP) { + + if (job_is_conflicted_by(k)) + d = j; + else + d = k; + } + + } else if (!j->matters_to_anchor) d = j; else if (!k->matters_to_anchor) d = k; @@ -1324,6 +1367,7 @@ static int transaction_add_job_and_dependencies( Job *by, bool matters, bool override, + bool conflicts, DBusError *e, Job **_ret) { Job *ret; @@ -1352,46 +1396,50 @@ static int transaction_add_job_and_dependencies( return -ENOMEM; /* Then, add a link to the job. */ - if (!job_dependency_new(by, ret, matters)) + if (!job_dependency_new(by, ret, matters, conflicts)) 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, override, e, NULL)) < 0 && r != -EBADR) + if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, e, NULL)) < 0 && r != -EBADR) goto fail; SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i) - if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, e, NULL)) < 0 && r != -EBADR) { + if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, false, e, NULL)) < 0 && r != -EBADR) { log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r)); dbus_error_free(e); } SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i) - if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, e, NULL)) < 0) { + if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, false, e, NULL)) < 0) { log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r)); dbus_error_free(e); } SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i) - if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, e, NULL)) < 0 && r != -EBADR) + if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, e, NULL)) < 0 && r != -EBADR) goto fail; SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i) - if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, e, NULL)) < 0 && r != -EBADR) { + if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, e, NULL)) < 0 && r != -EBADR) { log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r)); dbus_error_free(e); } SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i) - if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, e, NULL)) < 0 && r != -EBADR) + if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, true, e, NULL)) < 0 && r != -EBADR) + goto fail; + + SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTED_BY], i) + if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, false, e, 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, override, e, NULL)) < 0 && r != -EBADR) + if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, e, NULL)) < 0 && r != -EBADR) goto fail; } @@ -1432,7 +1480,7 @@ static int transaction_add_isolate_jobs(Manager *m) { if (hashmap_get(m->transaction_jobs, u)) continue; - if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, NULL, NULL)) < 0) + if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, false, NULL, NULL)) < 0) log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r)); } @@ -1455,7 +1503,7 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove log_debug("Trying to enqueue job %s/%s", unit->meta.id, job_type_to_string(type)); - if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, e, &ret)) < 0) { + if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, false, e, &ret)) < 0) { transaction_abort(m); return r; } diff --git a/src/mount.c b/src/mount.c index ba85d8c0a..cf2355a33 100644 --- a/src/mount.c +++ b/src/mount.c @@ -285,7 +285,7 @@ static int mount_add_default_dependencies(Mount *m) { if ((r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_FSCK_TARGET, NULL, true)) < 0) return r; - if ((r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0) + if ((r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0) return r; } diff --git a/src/path.c b/src/path.c index f4c20940d..91de56f34 100644 --- a/src/path.c +++ b/src/path.c @@ -110,7 +110,7 @@ static int path_add_default_dependencies(Path *p) { if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0) return r; - return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); + return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); } static int path_load(Unit *u) { diff --git a/src/service.c b/src/service.c index c052d7cdc..d6086ca41 100644 --- a/src/service.c +++ b/src/service.c @@ -873,7 +873,7 @@ static int service_add_default_dependencies(Service *s) { } /* Second, activate normal shutdown */ - return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); + return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); } static int service_load(Unit *u) { diff --git a/src/socket.c b/src/socket.c index 2a5270f33..7c280e006 100644 --- a/src/socket.c +++ b/src/socket.c @@ -296,7 +296,7 @@ static int socket_add_default_dependencies(Socket *s) { if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0) return r; - return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); + return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); } static int socket_load(Unit *u) { diff --git a/src/systemctl.c b/src/systemctl.c index 27e5e3708..e49c5b867 100644 --- a/src/systemctl.c +++ b/src/systemctl.c @@ -311,6 +311,7 @@ static int dot_one_property(const char *name, const char *prop, DBusMessageIter "RequisiteOverridable", "[color=\"darkblue\"]", "Wants", "[color=\"darkgrey\"]", "Conflicts", "[color=\"red\"]", + "ConflictedBy", "[color=\"red\"]", "After", "[color=\"green\"]" }; diff --git a/src/timer.c b/src/timer.c index cd6728a18..2c21b4919 100644 --- a/src/timer.c +++ b/src/timer.c @@ -82,7 +82,7 @@ static int timer_add_default_dependencies(Timer *t) { if ((r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0) return r; - return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); + return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); } static int timer_load(Unit *u) { diff --git a/src/unit.c b/src/unit.c index dd665e2d5..d205ca4aa 100644 --- a/src/unit.c +++ b/src/unit.c @@ -934,6 +934,10 @@ static void retroactively_start_dependencies(Unit *u) { SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i) if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); + + SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i) + if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) + manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); } static void retroactively_stop_dependencies(Unit *u) { @@ -1312,7 +1316,8 @@ int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_referen [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID, [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID, [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID, - [UNIT_CONFLICTS] = UNIT_CONFLICTS, + [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, + [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, [UNIT_BEFORE] = UNIT_AFTER, [UNIT_AFTER] = UNIT_BEFORE, [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID, @@ -2138,6 +2143,7 @@ static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = { [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable", [UNIT_WANTED_BY] = "WantedBy", [UNIT_CONFLICTS] = "Conflicts", + [UNIT_CONFLICTED_BY] = "ConflictedBy", [UNIT_BEFORE] = "Before", [UNIT_AFTER] = "After", [UNIT_REFERENCES] = "References", diff --git a/src/unit.h b/src/unit.h index 34e86d108..f657aea3e 100644 --- a/src/unit.h +++ b/src/unit.h @@ -108,7 +108,8 @@ enum UnitDependency { UNIT_WANTED_BY, /* inverse of 'wants' */ /* Negative dependencies */ - UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicts' */ + UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicted_by' */ + UNIT_CONFLICTED_BY, /* Order */ UNIT_BEFORE, /* inverse of 'before' is 'after' and vice versa */ diff --git a/units/graphical.target.m4 b/units/graphical.target.m4 index 3fb152634..c1534095e 100644 --- a/units/graphical.target.m4 +++ b/units/graphical.target.m4 @@ -16,9 +16,6 @@ m4_dnl m4_ifdef(`TARGET_FEDORA', # On Fedora Runlevel 5 is graphical login Names=runlevel5.target -# Pull in prefdm via requires, to make sure when it conflicts with -# getty@tty1.service it takes precedence. -Requires=prefdm.service )m4_dnl m4_ifdef(`TARGET_SUSE', Names=runlevel5.target -- 2.30.2