teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

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

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