Skip to content

Commit

Permalink
Allow restart for oneshot units
Browse files Browse the repository at this point in the history
Picked up from systemd#7474 , so
coauthored by @robermorales.
  • Loading branch information
claudiozz authored and chpatrick committed Mar 26, 2020
1 parent e7d8814 commit 9578769
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
7 changes: 4 additions & 3 deletions man/systemd.service.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1330,9 +1330,10 @@ WantedBy=multi-user.target</programlisting>

<para><varname>Type=</varname><option>oneshot</option> are the
only service units that may have more than one
<varname>ExecStart=</varname> specified. They will be executed
in order until either they are all successful or one of them
fails.</para>
<varname>ExecStart=</varname> specified. For units with multiple
commands (<varname noindex="true">Type=oneshot</varname>), all commands will be run again.</para>
<para> For <varname noindex="true">Type=oneshot</varname>, <varname>Restart=</varname><option>always</option>
and <varname>Restart=</varname><option>on-success</option> are <emphasis>not</emphasis> allowed.</para>
</example>

<example>
Expand Down
5 changes: 3 additions & 2 deletions src/core/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,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;
}

Expand Down
9 changes: 9 additions & 0 deletions test/TEST-41-ONESHOT-RESTART/Makefile
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions test/TEST-41-ONESHOT-RESTART/test.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
[Unit]
Description=Testsuite service
[Service]
ExecStart=/testsuite.sh
Type=oneshot
EOF
cp testsuite.sh $initdir/

setup_testsuite
)
setup_nspawn_root
}

do_test "$@"
33 changes: 33 additions & 0 deletions test/TEST-41-ONESHOT-RESTART/testsuite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
set -ex
set -o pipefail

systemd-analyze log-level debug
systemd-analyze log-target console

# These three commands should succeed.
! systemd-run --unit=one -p Type=oneshot -p Restart=on-failure /bin/bash -c "exit 1"

sleep 5

if [[ "$(systemctl show one.service -p NRestarts --value)" -le 0 ]]; then
exit 1
fi

TMP_FILE="/test-41-oneshot-restart-test"

touch $TMP_FILE

! 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"

sleep 5

if [[ $(cat $TMP_FILE) != "aaa" ]]; then
exit 1
fi

systemd-analyze log-level info

echo OK > /testok

exit 0

0 comments on commit 9578769

Please sign in to comment.