diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-08-30 13:00:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-30 13:00:57 +0200 |
commit | 3a5a08bbb4919070c55f1e417c46809b087b5881 (patch) | |
tree | 8cfaeca93cc09ead0901706eb7d8812392a00a4d | |
parent | a08c8c17da08f46b4680b7a37dfd9af4bdab9ced (diff) | |
parent | 8246bb204c14b0eda0627d00d3201ceaf5f11241 (diff) | |
download | systemd-3a5a08bbb4919070c55f1e417c46809b087b5881.tar.gz |
Merge pull request #13384 from yuwata/core-runtime-directory-preserve
core: make RuntimeDirectoryPreserve= works with non-service units
-rw-r--r-- | src/core/mount.c | 4 | ||||
-rw-r--r-- | src/core/service.c | 12 | ||||
-rw-r--r-- | src/core/socket.c | 4 | ||||
-rw-r--r-- | src/core/swap.c | 4 | ||||
-rw-r--r-- | src/core/unit.c | 17 | ||||
-rw-r--r-- | src/core/unit.h | 2 | ||||
l--------- | test/TEST-37-RUNTIMEDIRECTORYPRESERVE/Makefile | 1 | ||||
-rwxr-xr-x | test/TEST-37-RUNTIMEDIRECTORYPRESERVE/test.sh | 44 | ||||
-rwxr-xr-x | test/TEST-37-RUNTIMEDIRECTORYPRESERVE/testsuite.sh | 19 |
9 files changed, 95 insertions, 12 deletions
diff --git a/src/core/mount.c b/src/core/mount.c index 4f37d3e9a9..959b8fbed2 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -826,7 +826,7 @@ static void mount_enter_dead(Mount *m, MountResult f) { m->exec_runtime = exec_runtime_unref(m->exec_runtime, true); - exec_context_destroy_runtime_directory(&m->exec_context, UNIT(m)->manager->prefix[EXEC_DIRECTORY_RUNTIME]); + unit_destroy_runtime_directory(UNIT(m), &m->exec_context); unit_unref_uid_gid(UNIT(m), true); @@ -1996,6 +1996,8 @@ const UnitVTable mount_vtable = { .active_state = mount_active_state, .sub_state_to_string = mount_sub_state_to_string, + .will_restart = unit_will_restart_default, + .may_gc = mount_may_gc, .sigchld_event = mount_sigchld_event, diff --git a/src/core/service.c b/src/core/service.c index d8cfb40145..61e18de5fb 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1721,12 +1721,8 @@ static bool service_will_restart(Unit *u) { return true; if (s->state == SERVICE_AUTO_RESTART) return true; - if (!UNIT(s)->job) - return false; - if (UNIT(s)->job->type == JOB_START) - return true; - return false; + return unit_will_restart_default(u); } static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { @@ -1789,10 +1785,8 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) /* We want fresh tmpdirs in case service is started again immediately */ s->exec_runtime = exec_runtime_unref(s->exec_runtime, true); - if (s->exec_context.runtime_directory_preserve_mode == EXEC_PRESERVE_NO || - (s->exec_context.runtime_directory_preserve_mode == EXEC_PRESERVE_RESTART && !service_will_restart(UNIT(s)))) - /* Also, remove the runtime directory */ - exec_context_destroy_runtime_directory(&s->exec_context, UNIT(s)->manager->prefix[EXEC_DIRECTORY_RUNTIME]); + /* Also, remove the runtime directory */ + unit_destroy_runtime_directory(UNIT(s), &s->exec_context); /* Get rid of the IPC bits of the user */ unit_unref_uid_gid(UNIT(s), true); diff --git a/src/core/socket.c b/src/core/socket.c index 46fe405a17..e94d6feef9 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -2035,7 +2035,7 @@ static void socket_enter_dead(Socket *s, SocketResult f) { s->exec_runtime = exec_runtime_unref(s->exec_runtime, true); - exec_context_destroy_runtime_directory(&s->exec_context, UNIT(s)->manager->prefix[EXEC_DIRECTORY_RUNTIME]); + unit_destroy_runtime_directory(UNIT(s), &s->exec_context); unit_unref_uid_gid(UNIT(s), true); @@ -3353,6 +3353,8 @@ const UnitVTable socket_vtable = { .active_state = socket_active_state, .sub_state_to_string = socket_sub_state_to_string, + .will_restart = unit_will_restart_default, + .may_gc = socket_may_gc, .sigchld_event = socket_sigchld_event, diff --git a/src/core/swap.c b/src/core/swap.c index 74381c0c95..4ce9d06280 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -683,7 +683,7 @@ static void swap_enter_dead(Swap *s, SwapResult f) { s->exec_runtime = exec_runtime_unref(s->exec_runtime, true); - exec_context_destroy_runtime_directory(&s->exec_context, UNIT(s)->manager->prefix[EXEC_DIRECTORY_RUNTIME]); + unit_destroy_runtime_directory(UNIT(s), &s->exec_context); unit_unref_uid_gid(UNIT(s), true); @@ -1529,6 +1529,8 @@ const UnitVTable swap_vtable = { .active_state = swap_active_state, .sub_state_to_string = swap_sub_state_to_string, + .will_restart = unit_will_restart_default, + .may_gc = swap_may_gc, .sigchld_event = swap_sigchld_event, diff --git a/src/core/unit.c b/src/core/unit.c index 31ed473f91..a2944cbc15 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -4069,6 +4069,17 @@ bool unit_active_or_pending(Unit *u) { return false; } +bool unit_will_restart_default(Unit *u) { + assert(u); + + if (!u->job) + return false; + if (u->job->type == JOB_START) + return true; + + return false; +} + bool unit_will_restart(Unit *u) { assert(u); @@ -5880,6 +5891,12 @@ int unit_test_trigger_loaded(Unit *u) { return 0; } +void unit_destroy_runtime_directory(Unit *u, const ExecContext *context) { + if (context->runtime_directory_preserve_mode == EXEC_PRESERVE_NO || + (context->runtime_directory_preserve_mode == EXEC_PRESERVE_RESTART && !unit_will_restart(u))) + exec_context_destroy_runtime_directory(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]); +} + int unit_clean(Unit *u, ExecCleanMask mask) { UnitActiveState state; diff --git a/src/core/unit.h b/src/core/unit.h index 4732d72202..7f1d2f651c 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -752,6 +752,7 @@ const char *unit_slice_name(Unit *u); bool unit_stop_pending(Unit *u) _pure_; bool unit_inactive_or_pending(Unit *u) _pure_; bool unit_active_or_pending(Unit *u); +bool unit_will_restart_default(Unit *u); bool unit_will_restart(Unit *u); int unit_add_default_target_dependency(Unit *u, Unit *target); @@ -860,6 +861,7 @@ int unit_failure_action_exit_status(Unit *u); int unit_test_trigger_loaded(Unit *u); +void unit_destroy_runtime_directory(Unit *u, const ExecContext *context); int unit_clean(Unit *u, ExecCleanMask mask); int unit_can_clean(Unit *u, ExecCleanMask *ret_mask); diff --git a/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/Makefile b/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/Makefile new file mode 120000 index 0000000000..e9f93b1104 --- /dev/null +++ b/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/Makefile @@ -0,0 +1 @@ +../TEST-01-BASIC/Makefile
\ No newline at end of file diff --git a/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/test.sh b/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/test.sh new file mode 100755 index 0000000000..9e87e61dcd --- /dev/null +++ b/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/test.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- +# ex: ts=8 sw=4 sts=4 et filetype=sh +set -e +TEST_DESCRIPTION="test RuntimeDirectoryPreserve=yes" + +. $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 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=/bin/bash -x /testsuite.sh +Type=oneshot +StandardOutput=tty +StandardError=tty +EOF + cp testsuite.sh $initdir/ + + setup_testsuite + ) || return 1 + setup_nspawn_root +} + +do_test "$@" diff --git a/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/testsuite.sh b/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/testsuite.sh new file mode 100755 index 0000000000..4e63a07326 --- /dev/null +++ b/test/TEST-37-RUNTIMEDIRECTORYPRESERVE/testsuite.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- +# ex: ts=8 sw=4 sts=4 et filetype=sh +set -ex +set -o pipefail + +systemd-mount -p RuntimeDirectory=hoge -p RuntimeDirectoryPreserve=yes -t tmpfs tmpfs /tmp/aaa + +touch /run/hoge/foo +touch /tmp/aaa/bbb + +systemctl restart tmp-aaa.mount + +test -e /run/hoge/foo +! test -e /tmp/aaa/bbb + +echo OK > /testok + +exit 0 |