]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: improve finding OnSuccess=/OnFailure= dependent (#35468)
authorNils K <24257556+septatrix@users.noreply.github.com>
Tue, 10 Dec 2024 19:48:09 +0000 (20:48 +0100)
committerGitHub <noreply@github.com>
Tue, 10 Dec 2024 19:48:09 +0000 (20:48 +0100)
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.

src/core/service.c
test/units/TEST-68-PROPAGATE-EXIT-STATUS.sh

index c436650791f5844bd7405b9972d414fce1b009ee..c7c5225115c925950f8965a94e73388e37d98302 100644 (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;
index 11da48aba01e74474102c3c49b11c43f91a8704d..d717ce5a557d1a8e0cc1e08e59bbb3acf9120801 100755 (executable)
@@ -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