From 4af769b60491d0efac99f3ba9ccfb0bfc28cfa1f Mon Sep 17 00:00:00 2001 From: Anita Zhang <the.anitazha@gmail.com> Date: Dec 15 2020 09:45:53 +0000 Subject: core: don't consider SERVICE_SKIP_CONDITION for abnormal or failure restarts Fixes: #16115 (cherry picked from commit bb9244781c6fc7608f7cac910269f8987b8adc01) Related: #1737283 patch_name: 0417-core-don-t-consider-SERVICE_SKIP_CONDITION-for-abnor.patch present_in_specfile: true location_in_specfile: 417 squash_commits: true --- diff --git a/src/core/service.c b/src/core/service.c index 92be428..1d98ee3 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1637,10 +1637,10 @@ static bool service_shall_restart(Service *s) { return s->result == SERVICE_SUCCESS; case SERVICE_RESTART_ON_FAILURE: - return s->result != SERVICE_SUCCESS; + return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_SKIP_CONDITION); case SERVICE_RESTART_ON_ABNORMAL: - return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE); + return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE, SERVICE_SKIP_CONDITION); case SERVICE_RESTART_ON_WATCHDOG: return s->result == SERVICE_FAILURE_WATCHDOG; diff --git a/test/TEST-51-ISSUE-16115/Makefile b/test/TEST-51-ISSUE-16115/Makefile new file mode 120000 index 0000000..e9f93b1 --- /dev/null +++ b/test/TEST-51-ISSUE-16115/Makefile @@ -0,0 +1 @@ +../TEST-01-BASIC/Makefile \ No newline at end of file diff --git a/test/TEST-51-ISSUE-16115/repro-1.service b/test/TEST-51-ISSUE-16115/repro-1.service new file mode 100644 index 0000000..96ecabe --- /dev/null +++ b/test/TEST-51-ISSUE-16115/repro-1.service @@ -0,0 +1,9 @@ +[Unit] +Description=Issue 16115 Repro with on-abnormal + +[Service] +Type=simple +Restart=on-abnormal +ExecCondition=/bin/false +ExecStart=sleep 100 +RestartSec=1 diff --git a/test/TEST-51-ISSUE-16115/repro-2.service b/test/TEST-51-ISSUE-16115/repro-2.service new file mode 100644 index 0000000..6015ad8 --- /dev/null +++ b/test/TEST-51-ISSUE-16115/repro-2.service @@ -0,0 +1,9 @@ +[Unit] +Description=Issue 16115 Repro with on-failure + +[Service] +Type=simple +Restart=on-failure +ExecCondition=/bin/false +ExecStart=sleep 100 +RestartSec=1 diff --git a/test/TEST-51-ISSUE-16115/test.sh b/test/TEST-51-ISSUE-16115/test.sh new file mode 100755 index 0000000..09ac96f --- /dev/null +++ b/test/TEST-51-ISSUE-16115/test.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -e +TEST_DESCRIPTION="Test ExecCondition= does not restart on abnormal or failure" +. $TEST_BASE_DIR/test-functions + +test_setup() { + create_empty_image + mkdir -p $TESTDIR/root + mount ${LOOPDEV}p1 $TESTDIR/root + + ( + LOG_LEVEL=5 + eval $(udevadm info --export --query=env --name=${LOOPDEV}p2) + + setup_basic_environment + + # mask some services that we do not want to run in these tests + ln -fs /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service + ln -fs /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service + ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.service + ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.socket + ln -fs /dev/null $initdir/etc/systemd/system/systemd-resolved.service + ln -fs /dev/null $initdir/etc/systemd/system/systemd-machined.service + + # 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/ + cp repro-?.service $initdir/etc/systemd/system/ + + setup_testsuite + ) + setup_nspawn_root + + ddebug "umount $TESTDIR/root" + umount $TESTDIR/root +} + +do_test "$@" diff --git a/test/TEST-51-ISSUE-16115/testsuite.sh b/test/TEST-51-ISSUE-16115/testsuite.sh new file mode 100755 index 0000000..695896a --- /dev/null +++ b/test/TEST-51-ISSUE-16115/testsuite.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -ex +set -o pipefail + +systemd-analyze log-level debug +systemd-analyze log-target console + +systemctl start repro-1 +systemctl start repro-2 +sleep 5 # wait a bit in case there are restarts so we can count them below + +[[ "$(systemctl show repro-1 --value -p NRestarts)" == "0" ]] +[[ "$(systemctl show repro-2 --value -p NRestarts)" == "0" ]] + +systemd-analyze log-level info + +echo OK > /testok + +exit 0 diff --git a/test/test-functions b/test/test-functions index 7c4230b..4d7832b 100644 --- a/test/test-functions +++ b/test/test-functions @@ -23,7 +23,7 @@ fi PATH_TO_INIT=$ROOTLIBDIR/systemd -BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm true false chmod chown ln xargs" +BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm true false chmod chown ln xargs env" DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname find" STATEDIR="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"