teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0782-Allow-restart-for-oneshot-units.patch

b7dd4d
From ffc616d9c4d2c64d211c9e63601a321524fe2d31 Mon Sep 17 00:00:00 2001
b7dd4d
From: Claudio Zumbo <claudioz@fb.com>
b7dd4d
Date: Tue, 8 Oct 2019 15:04:29 -0700
b7dd4d
Subject: [PATCH] Allow restart for oneshot units
b7dd4d
b7dd4d
Picked up from https://github.com/systemd/systemd/pull/7474 , so
b7dd4d
coauthored by @robermorales.
b7dd4d
b7dd4d
(cherry picked from commit 10e72727eeeeb1a495303ec406fa8d1e1a83dc6e)
b7dd4d
b7dd4d
Resolves: #2042896
b7dd4d
---
b7dd4d
 man/systemd.service.xml                   |  7 ++--
b7dd4d
 src/core/service.c                        |  5 +--
b7dd4d
 test/TEST-41-ONESHOT-RESTART/Makefile     |  9 +++++
b7dd4d
 test/TEST-41-ONESHOT-RESTART/test.sh      | 44 +++++++++++++++++++++++
b7dd4d
 test/TEST-41-ONESHOT-RESTART/testsuite.sh | 33 +++++++++++++++++
b7dd4d
 5 files changed, 93 insertions(+), 5 deletions(-)
b7dd4d
 create mode 100644 test/TEST-41-ONESHOT-RESTART/Makefile
b7dd4d
 create mode 100755 test/TEST-41-ONESHOT-RESTART/test.sh
b7dd4d
 create mode 100755 test/TEST-41-ONESHOT-RESTART/testsuite.sh
b7dd4d
b7dd4d
diff --git a/man/systemd.service.xml b/man/systemd.service.xml
b7dd4d
index 1e30a564df..133296d386 100644
b7dd4d
--- a/man/systemd.service.xml
b7dd4d
+++ b/man/systemd.service.xml
b7dd4d
@@ -1276,9 +1276,10 @@ WantedBy=multi-user.target</programlisting>
b7dd4d
 
b7dd4d
       <para><varname>Type=</varname><option>oneshot</option> are the
b7dd4d
       only service units that may have more than one
b7dd4d
-      <varname>ExecStart=</varname> specified. They will be executed
b7dd4d
-      in order until either they are all successful or one of them
b7dd4d
-      fails.</para>
b7dd4d
+      <varname>ExecStart=</varname> specified. For units with multiple
b7dd4d
+      commands (<varname noindex="true">Type=oneshot</varname>), all commands will be run again.</para>
b7dd4d
+      <para> For <varname noindex="true">Type=oneshot</varname>, <varname>Restart=</varname><option>always</option>
b7dd4d
+      and <varname>Restart=</varname><option>on-success</option> are <emphasis>not</emphasis> allowed.</para>
b7dd4d
     </example>
b7dd4d
 
b7dd4d
     <example>
b7dd4d
diff --git a/src/core/service.c b/src/core/service.c
b7dd4d
index e8ae1a5772..e05d0e0514 100644
b7dd4d
--- a/src/core/service.c
b7dd4d
+++ b/src/core/service.c
b7dd4d
@@ -581,8 +581,9 @@ static int service_verify(Service *s) {
b7dd4d
                 return -ENOEXEC;
b7dd4d
         }
b7dd4d
 
b7dd4d
-        if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
b7dd4d
-                log_unit_error(UNIT(s), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
b7dd4d
+        if (s->type == SERVICE_ONESHOT
b7dd4d
+            && !IN_SET(s->restart, SERVICE_RESTART_NO, SERVICE_RESTART_ON_FAILURE, SERVICE_RESTART_ON_ABNORMAL, SERVICE_RESTART_ON_WATCHDOG, SERVICE_RESTART_ON_ABORT)) {
b7dd4d
+                log_unit_error(UNIT(s), "Service has Restart= set to either always or on-success, which isn't allowed for Type=oneshot services. Refusing.");
b7dd4d
                 return -ENOEXEC;
b7dd4d
         }
b7dd4d
 
b7dd4d
diff --git a/test/TEST-41-ONESHOT-RESTART/Makefile b/test/TEST-41-ONESHOT-RESTART/Makefile
b7dd4d
new file mode 100644
b7dd4d
index 0000000000..45e9bfc67c
b7dd4d
--- /dev/null
b7dd4d
+++ b/test/TEST-41-ONESHOT-RESTART/Makefile
b7dd4d
@@ -0,0 +1,9 @@
b7dd4d
+BUILD_DIR=$(shell ../../tools/find-build-dir.sh)
b7dd4d
+
b7dd4d
+all setup run:
b7dd4d
+	@basedir=../.. TEST_BASE_DIR=../ BUILD_DIR=$(BUILD_DIR) ./test.sh --$@
b7dd4d
+
b7dd4d
+clean clean-again:
b7dd4d
+	@basedir=../.. TEST_BASE_DIR=../ BUILD_DIR=$(BUILD_DIR) ./test.sh --clean
b7dd4d
+
b7dd4d
+.PHONY: all setup run clean clean-again
b7dd4d
diff --git a/test/TEST-41-ONESHOT-RESTART/test.sh b/test/TEST-41-ONESHOT-RESTART/test.sh
b7dd4d
new file mode 100755
b7dd4d
index 0000000000..35de08b1e9
b7dd4d
--- /dev/null
b7dd4d
+++ b/test/TEST-41-ONESHOT-RESTART/test.sh
b7dd4d
@@ -0,0 +1,44 @@
b7dd4d
+#!/bin/bash
b7dd4d
+set -e
b7dd4d
+TEST_DESCRIPTION="Test oneshot unit restart on failure"
b7dd4d
+. $TEST_BASE_DIR/test-functions
b7dd4d
+
b7dd4d
+test_setup() {
b7dd4d
+    create_empty_image
b7dd4d
+    mkdir -p $TESTDIR/root
b7dd4d
+    mount ${LOOPDEV}p1 $TESTDIR/root
b7dd4d
+
b7dd4d
+    (
b7dd4d
+        LOG_LEVEL=5
b7dd4d
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
b7dd4d
+
b7dd4d
+        setup_basic_environment
b7dd4d
+
b7dd4d
+        # mask some services that we do not want to run in these tests
b7dd4d
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
b7dd4d
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
b7dd4d
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.service
b7dd4d
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
b7dd4d
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-resolved.service
b7dd4d
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-machined.service
b7dd4d
+
b7dd4d
+        # setup the testsuite service
b7dd4d
+        cat >$initdir/etc/systemd/system/testsuite.service <
b7dd4d
+[Unit]
b7dd4d
+Description=Testsuite service
b7dd4d
+
b7dd4d
+[Service]
b7dd4d
+ExecStart=/testsuite.sh
b7dd4d
+Type=oneshot
b7dd4d
+EOF
b7dd4d
+        cp testsuite.sh $initdir/
b7dd4d
+
b7dd4d
+        setup_testsuite
b7dd4d
+    )
b7dd4d
+    setup_nspawn_root
b7dd4d
+
b7dd4d
+    ddebug "umount $TESTDIR/root"
b7dd4d
+    umount $TESTDIR/root
b7dd4d
+}
b7dd4d
+
b7dd4d
+do_test "$@"
b7dd4d
diff --git a/test/TEST-41-ONESHOT-RESTART/testsuite.sh b/test/TEST-41-ONESHOT-RESTART/testsuite.sh
b7dd4d
new file mode 100755
b7dd4d
index 0000000000..f7423dbf9a
b7dd4d
--- /dev/null
b7dd4d
+++ b/test/TEST-41-ONESHOT-RESTART/testsuite.sh
b7dd4d
@@ -0,0 +1,33 @@
b7dd4d
+#!/bin/bash
b7dd4d
+set -ex
b7dd4d
+set -o pipefail
b7dd4d
+
b7dd4d
+systemd-analyze log-level debug
b7dd4d
+systemd-analyze log-target console
b7dd4d
+
b7dd4d
+# These three commands should succeed.
b7dd4d
+! systemd-run --unit=one -p Type=oneshot -p Restart=on-failure /bin/bash -c "exit 1"
b7dd4d
+
b7dd4d
+sleep 5
b7dd4d
+
b7dd4d
+if [[ "$(systemctl show one.service -p NRestarts --value)" -le 0 ]]; then
b7dd4d
+  exit 1
b7dd4d
+fi
b7dd4d
+
b7dd4d
+TMP_FILE="/test-41-oneshot-restart-test"
b7dd4d
+
b7dd4d
+touch $TMP_FILE
b7dd4d
+
b7dd4d
+! systemd-run --unit=two -p StartLimitBurst=3 -p Type=oneshot -p Restart=on-failure -p ExecStart="/bin/bash -c \"printf a >>  $TMP_FILE\"" /bin/bash -c "exit 1"
b7dd4d
+
b7dd4d
+sleep 5
b7dd4d
+
b7dd4d
+if [[ $(cat $TMP_FILE) != "aaa" ]]; then
b7dd4d
+  exit 1
b7dd4d
+fi
b7dd4d
+
b7dd4d
+systemd-analyze log-level info
b7dd4d
+
b7dd4d
+echo OK > /testok
b7dd4d
+
b7dd4d
+exit 0