From 10e72727eeeeb1a495303ec406fa8d1e1a83dc6e Mon Sep 17 00:00:00 2001 From: Claudio Zumbo Date: Tue, 8 Oct 2019 15:04:29 -0700 Subject: [PATCH] Allow restart for oneshot units Picked up from https://github.com/systemd/systemd/pull/7474 , so coauthored by @robermorales. --- man/systemd.service.xml | 7 ++--- src/core/service.c | 5 ++-- test/TEST-41-ONESHOT-RESTART/Makefile | 9 +++++++ test/TEST-41-ONESHOT-RESTART/test.sh | 32 ++++++++++++++++++++++ test/TEST-41-ONESHOT-RESTART/testsuite.sh | 33 +++++++++++++++++++++++ 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 test/TEST-41-ONESHOT-RESTART/Makefile create mode 100755 test/TEST-41-ONESHOT-RESTART/test.sh create mode 100755 test/TEST-41-ONESHOT-RESTART/testsuite.sh diff --git a/man/systemd.service.xml b/man/systemd.service.xml index af14eedfcc6..f57e37ca5b4 100644 --- a/man/systemd.service.xml +++ b/man/systemd.service.xml @@ -1328,9 +1328,10 @@ WantedBy=multi-user.target Type= are the only service units that may have more than one - ExecStart= specified. They will be executed - in order until either they are all successful or one of them - fails. + ExecStart= specified. For units with multiple + commands (Type=oneshot), all commands will be run again. + For Type=oneshot, Restart= + and Restart= are not allowed. diff --git a/src/core/service.c b/src/core/service.c index ada25e634a0..45b5f4b6a04 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -577,8 +577,9 @@ static int service_verify(Service *s) { return -ENOEXEC; } - if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) { - log_unit_error(UNIT(s), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing."); + if (s->type == SERVICE_ONESHOT + && !IN_SET(s->restart, SERVICE_RESTART_NO, SERVICE_RESTART_ON_FAILURE, SERVICE_RESTART_ON_ABNORMAL, SERVICE_RESTART_ON_WATCHDOG, SERVICE_RESTART_ON_ABORT)) { + log_unit_error(UNIT(s), "Service has Restart= set to either always or on-success, which isn't allowed for Type=oneshot services. Refusing."); return -ENOEXEC; } diff --git a/test/TEST-41-ONESHOT-RESTART/Makefile b/test/TEST-41-ONESHOT-RESTART/Makefile new file mode 100644 index 00000000000..45e9bfc67cd --- /dev/null +++ b/test/TEST-41-ONESHOT-RESTART/Makefile @@ -0,0 +1,9 @@ +BUILD_DIR=$(shell ../../tools/find-build-dir.sh) + +all setup run: + @basedir=../.. TEST_BASE_DIR=../ BUILD_DIR=$(BUILD_DIR) ./test.sh --$@ + +clean clean-again: + @basedir=../.. TEST_BASE_DIR=../ BUILD_DIR=$(BUILD_DIR) ./test.sh --clean + +.PHONY: all setup run clean clean-again diff --git a/test/TEST-41-ONESHOT-RESTART/test.sh b/test/TEST-41-ONESHOT-RESTART/test.sh new file mode 100755 index 00000000000..96c21da6421 --- /dev/null +++ b/test/TEST-41-ONESHOT-RESTART/test.sh @@ -0,0 +1,32 @@ +#!/bin/bash +set -e +TEST_DESCRIPTION="Test oneshot unit restart on failure" +. $TEST_BASE_DIR/test-functions + +test_setup() { + create_empty_image_rootdir + + ( + LOG_LEVEL=5 + eval $(udevadm info --export --query=env --name=${LOOPDEV}p2) + + setup_basic_environment + mask_supporting_services + + # setup the testsuite service + cat >$initdir/etc/systemd/system/testsuite.service <> $TMP_FILE\"" /bin/bash -c "exit 1" + +sleep 5 + +if [[ $(cat $TMP_FILE) != "aaa" ]]; then + exit 1 +fi + +systemd-analyze log-level info + +echo OK > /testok + +exit 0 -- 2.47.3