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