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