core: improve finding OnSuccess=/OnFailure= dependent (#35468)

Previously if one service specified the same unit as their
success and failure handler we bailed out of resolving the triggering unit
even though it is still unique.
This commit is contained in:
Nils K
2024-12-10 20:48:09 +01:00
committed by GitHub
parent e82677aa07
commit e76d83d100
2 changed files with 24 additions and 15 deletions

View File

@@ -1648,13 +1648,7 @@ static Service *service_get_triggering_service(Service *s) {
* or OnSuccess= then we return NULL. This is since we don't know from which
* one to propagate the exit status. */
UNIT_FOREACH_DEPENDENCY(other, UNIT(s), UNIT_ATOM_ON_FAILURE_OF) {
if (candidate)
goto have_other;
candidate = other;
}
UNIT_FOREACH_DEPENDENCY(other, UNIT(s), UNIT_ATOM_ON_SUCCESS_OF) {
UNIT_FOREACH_DEPENDENCY(other, UNIT(s), UNIT_ATOM_ON_SUCCESS_OF|UNIT_ATOM_ON_FAILURE_OF) {
if (candidate)
goto have_other;
candidate = other;

View File

@@ -30,7 +30,7 @@ cat >/run/systemd/system/testservice-failure-68.service <<EOF
OnFailure=testservice-failure-exit-handler-68.service
[Service]
ExecStart=sh -c "exit 1"
ExecStart=false
EOF
cat >/run/systemd/system/testservice-failure-68-template.service <<EOF
@@ -38,7 +38,7 @@ cat >/run/systemd/system/testservice-failure-68-template.service <<EOF
OnFailure=testservice-failure-exit-handler-68-template@%n.service
[Service]
ExecStart=sh -c "exit 1"
ExecStart=false
EOF
cat >/run/systemd/system/testservice-success-68.service <<EOF
@@ -46,7 +46,7 @@ cat >/run/systemd/system/testservice-success-68.service <<EOF
OnSuccess=testservice-success-exit-handler-68.service
[Service]
ExecStart=sh -c "exit 0"
ExecStart=true
EOF
cat >/run/systemd/system/testservice-success-68-template.service <<EOF
@@ -54,7 +54,16 @@ cat >/run/systemd/system/testservice-success-68-template.service <<EOF
OnSuccess=testservice-success-exit-handler-68-template@%n.service
[Service]
ExecStart=sh -c "exit 0"
ExecStart=true
EOF
cat >/run/systemd/system/testservice-success-and-failure-68.service <<EOF
[Unit]
OnSuccess=testservice-failure-exit-handler-68.service
OnFailure=testservice-failure-exit-handler-68.service
[Service]
ExecStart=true
EOF
# Script to check that when an OnSuccess= dependency fires, the correct
@@ -86,9 +95,10 @@ fi
if [ "$MONITOR_UNIT" != "testservice-success-68.service" ] &&
[ "$MONITOR_UNIT" != "testservice-success-68-template.service" ] &&
[ "$MONITOR_UNIT" != "testservice-transient-success-68.service" ]; then
[ "$MONITOR_UNIT" != "testservice-transient-success-68.service" ] &&
[ "$MONITOR_UNIT" != "testservice-success-and-failure-68.service" ]; then
echo "MONITOR_UNIT was '$MONITOR_UNIT', expected 'testservice[-transient]-success-68[-template].service'"
echo "MONITOR_UNIT was '$MONITOR_UNIT', expected 'testservice[-transient]-success[-and-failure]-68[-template].service'"
exit 1
fi
@@ -193,13 +203,13 @@ wait_on_state_or_fail "testservice-success-exit-handler-68.service" "inactive" "
: "-------III--------------------------------------------------"
systemd-run --unit=testservice-transient-success-68 \
--property=OnSuccess=testservice-transient-success-exit-handler-68.service \
sh -c "exit 0"
true
wait_on_state_or_fail "testservice-success-exit-handler-68.service" "inactive" "10"
: "-------IIII-------------------------------------------------"
systemd-run --unit=testservice-transient-failure-68 \
--property=OnFailure=testservice-transient-failure-exit-handler-68.service \
sh -c "exit 1"
false
wait_on_state_or_fail "testservice-failure-exit-handler-68.service" "inactive" "10"
# Test template handlers too
@@ -211,6 +221,11 @@ wait_on_state_or_fail "testservice-success-exit-handler-68-template@testservice-
systemctl start testservice-failure-68-template.service
wait_on_state_or_fail "testservice-failure-exit-handler-68-template@testservice-failure-68-template.service.service" "inactive" "10"
# Test that monitor variables are set as long as OnSuccess= and OnFailure= dependant is the same
: "-------VII----------------------------------------------------"
systemctl start testservice-success-and-failure-68.service
wait_on_state_or_fail "testservice-success-exit-handler-68.service" "inactive" "10"
systemd-analyze log-level info
touch /testok