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