52b84b
From 41346615264e01c6ff6118e09cf3ac4b4c71e89d Mon Sep 17 00:00:00 2001
52b84b
From: Anita Zhang <the.anitazha@gmail.com>
52b84b
Date: Wed, 10 Jun 2020 01:18:00 -0700
52b84b
Subject: [PATCH] core: don't consider SERVICE_SKIP_CONDITION for abnormal or
52b84b
 failure restarts
52b84b
52b84b
Fixes: #16115
52b84b
(cherry picked from commit bb9244781c6fc7608f7cac910269f8987b8adc01)
52b84b
52b84b
Related: #1737283
52b84b
---
52b84b
 src/core/service.c                       |  4 +--
52b84b
 test/TEST-51-ISSUE-16115/Makefile        |  1 +
52b84b
 test/TEST-51-ISSUE-16115/repro-1.service |  9 +++++
52b84b
 test/TEST-51-ISSUE-16115/repro-2.service |  9 +++++
52b84b
 test/TEST-51-ISSUE-16115/test.sh         | 46 ++++++++++++++++++++++++
52b84b
 test/TEST-51-ISSUE-16115/testsuite.sh    | 19 ++++++++++
52b84b
 test/test-functions                      |  2 +-
52b84b
 7 files changed, 87 insertions(+), 3 deletions(-)
52b84b
 create mode 120000 test/TEST-51-ISSUE-16115/Makefile
52b84b
 create mode 100644 test/TEST-51-ISSUE-16115/repro-1.service
52b84b
 create mode 100644 test/TEST-51-ISSUE-16115/repro-2.service
52b84b
 create mode 100755 test/TEST-51-ISSUE-16115/test.sh
52b84b
 create mode 100755 test/TEST-51-ISSUE-16115/testsuite.sh
52b84b
52b84b
diff --git a/src/core/service.c b/src/core/service.c
52b84b
index 92be4280f6..1d98ee37fd 100644
52b84b
--- a/src/core/service.c
52b84b
+++ b/src/core/service.c
52b84b
@@ -1637,10 +1637,10 @@ static bool service_shall_restart(Service *s) {
52b84b
                 return s->result == SERVICE_SUCCESS;
52b84b
 
52b84b
         case SERVICE_RESTART_ON_FAILURE:
52b84b
-                return s->result != SERVICE_SUCCESS;
52b84b
+                return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_SKIP_CONDITION);
52b84b
 
52b84b
         case SERVICE_RESTART_ON_ABNORMAL:
52b84b
-                return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE);
52b84b
+                return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE, SERVICE_SKIP_CONDITION);
52b84b
 
52b84b
         case SERVICE_RESTART_ON_WATCHDOG:
52b84b
                 return s->result == SERVICE_FAILURE_WATCHDOG;
52b84b
diff --git a/test/TEST-51-ISSUE-16115/Makefile b/test/TEST-51-ISSUE-16115/Makefile
52b84b
new file mode 120000
52b84b
index 0000000000..e9f93b1104
52b84b
--- /dev/null
52b84b
+++ b/test/TEST-51-ISSUE-16115/Makefile
52b84b
@@ -0,0 +1 @@
52b84b
+../TEST-01-BASIC/Makefile
52b84b
\ No newline at end of file
52b84b
diff --git a/test/TEST-51-ISSUE-16115/repro-1.service b/test/TEST-51-ISSUE-16115/repro-1.service
52b84b
new file mode 100644
52b84b
index 0000000000..96ecabe234
52b84b
--- /dev/null
52b84b
+++ b/test/TEST-51-ISSUE-16115/repro-1.service
52b84b
@@ -0,0 +1,9 @@
52b84b
+[Unit]
52b84b
+Description=Issue 16115 Repro with on-abnormal
52b84b
+
52b84b
+[Service]
52b84b
+Type=simple
52b84b
+Restart=on-abnormal
52b84b
+ExecCondition=/bin/false
52b84b
+ExecStart=sleep 100
52b84b
+RestartSec=1
52b84b
diff --git a/test/TEST-51-ISSUE-16115/repro-2.service b/test/TEST-51-ISSUE-16115/repro-2.service
52b84b
new file mode 100644
52b84b
index 0000000000..6015ad8080
52b84b
--- /dev/null
52b84b
+++ b/test/TEST-51-ISSUE-16115/repro-2.service
52b84b
@@ -0,0 +1,9 @@
52b84b
+[Unit]
52b84b
+Description=Issue 16115 Repro with on-failure
52b84b
+
52b84b
+[Service]
52b84b
+Type=simple
52b84b
+Restart=on-failure
52b84b
+ExecCondition=/bin/false
52b84b
+ExecStart=sleep 100
52b84b
+RestartSec=1
52b84b
diff --git a/test/TEST-51-ISSUE-16115/test.sh b/test/TEST-51-ISSUE-16115/test.sh
52b84b
new file mode 100755
52b84b
index 0000000000..09ac96ffce
52b84b
--- /dev/null
52b84b
+++ b/test/TEST-51-ISSUE-16115/test.sh
52b84b
@@ -0,0 +1,46 @@
52b84b
+#!/usr/bin/env bash
52b84b
+set -e
52b84b
+TEST_DESCRIPTION="Test ExecCondition= does not restart on abnormal or failure"
52b84b
+. $TEST_BASE_DIR/test-functions
52b84b
+
52b84b
+test_setup() {
52b84b
+    create_empty_image
52b84b
+    mkdir -p $TESTDIR/root
52b84b
+    mount ${LOOPDEV}p1 $TESTDIR/root
52b84b
+
52b84b
+    (
52b84b
+        LOG_LEVEL=5
52b84b
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
52b84b
+
52b84b
+        setup_basic_environment
52b84b
+
52b84b
+        # mask some services that we do not want to run in these tests
52b84b
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
52b84b
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
52b84b
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.service
52b84b
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
52b84b
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-resolved.service
52b84b
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-machined.service
52b84b
+
52b84b
+        # setup the testsuite service
52b84b
+        cat >$initdir/etc/systemd/system/testsuite.service <
52b84b
+[Unit]
52b84b
+Description=Testsuite service
52b84b
+
52b84b
+[Service]
52b84b
+ExecStart=/testsuite.sh
52b84b
+Type=oneshot
52b84b
+EOF
52b84b
+
52b84b
+        cp testsuite.sh $initdir/
52b84b
+        cp repro-?.service $initdir/etc/systemd/system/
52b84b
+
52b84b
+        setup_testsuite
52b84b
+    )
52b84b
+    setup_nspawn_root
52b84b
+
52b84b
+    ddebug "umount $TESTDIR/root"
52b84b
+    umount $TESTDIR/root
52b84b
+}
52b84b
+
52b84b
+do_test "$@"
52b84b
diff --git a/test/TEST-51-ISSUE-16115/testsuite.sh b/test/TEST-51-ISSUE-16115/testsuite.sh
52b84b
new file mode 100755
52b84b
index 0000000000..695896ab7b
52b84b
--- /dev/null
52b84b
+++ b/test/TEST-51-ISSUE-16115/testsuite.sh
52b84b
@@ -0,0 +1,19 @@
52b84b
+#!/usr/bin/env bash
52b84b
+set -ex
52b84b
+set -o pipefail
52b84b
+
52b84b
+systemd-analyze log-level debug
52b84b
+systemd-analyze log-target console
52b84b
+
52b84b
+systemctl start repro-1
52b84b
+systemctl start repro-2
52b84b
+sleep 5 # wait a bit in case there are restarts so we can count them below
52b84b
+
52b84b
+[[ "$(systemctl show repro-1 --value -p NRestarts)" == "0" ]]
52b84b
+[[ "$(systemctl show repro-2 --value -p NRestarts)" == "0" ]]
52b84b
+
52b84b
+systemd-analyze log-level info
52b84b
+
52b84b
+echo OK > /testok
52b84b
+
52b84b
+exit 0
52b84b
diff --git a/test/test-functions b/test/test-functions
52b84b
index 7c4230b078..4d7832b1fb 100644
52b84b
--- a/test/test-functions
52b84b
+++ b/test/test-functions
52b84b
@@ -23,7 +23,7 @@ fi
52b84b
 
52b84b
 PATH_TO_INIT=$ROOTLIBDIR/systemd
52b84b
 
52b84b
-BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm true false chmod chown ln xargs"
52b84b
+BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm true false chmod chown ln xargs env"
52b84b
 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname find"
52b84b
 
52b84b
 STATEDIR="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"