diff --git a/src/core/mount.c b/src/core/mount.c index 92e0d7737a7..689ef5672db 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -910,7 +910,7 @@ static void mount_enter_dead(Mount *m, MountResult f, bool flush_result) { m->exec_runtime = exec_runtime_destroy(m->exec_runtime); - unit_destroy_runtime_data(UNIT(m), &m->exec_context); + unit_destroy_runtime_data(UNIT(m), &m->exec_context, /* destroy_runtime_dir = */ true); unit_unref_uid_gid(UNIT(m), true); diff --git a/src/core/service.c b/src/core/service.c index f4919bb2b1b..c436650791f 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1341,7 +1341,7 @@ static void service_set_state(Service *s, ServiceState state) { } if (start_only) - unit_destroy_runtime_data(u, &s->exec_context); + unit_destroy_runtime_data(u, &s->exec_context, /* destroy_runtime_dir = */ false); } if (old_state != state) @@ -2163,7 +2163,7 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) s->exec_runtime = exec_runtime_destroy(s->exec_runtime); /* Also, remove the runtime directory */ - unit_destroy_runtime_data(UNIT(s), &s->exec_context); + unit_destroy_runtime_data(UNIT(s), &s->exec_context, /* destroy_runtime_dir = */ true); /* Also get rid of the fd store, if that's configured. */ if (s->fd_store_preserve_mode == EXEC_PRESERVE_NO) diff --git a/src/core/socket.c b/src/core/socket.c index e4a19285c92..65995275150 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -2070,7 +2070,7 @@ static void socket_enter_dead(Socket *s, SocketResult f) { s->exec_runtime = exec_runtime_destroy(s->exec_runtime); - unit_destroy_runtime_data(UNIT(s), &s->exec_context); + unit_destroy_runtime_data(UNIT(s), &s->exec_context, /* destroy_runtime_dir = */ true); unit_unref_uid_gid(UNIT(s), true); } diff --git a/src/core/swap.c b/src/core/swap.c index 312db05db57..f2d1d5608cd 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -676,7 +676,7 @@ static void swap_enter_dead(Swap *s, SwapResult f) { s->exec_runtime = exec_runtime_destroy(s->exec_runtime); - unit_destroy_runtime_data(UNIT(s), &s->exec_context); + unit_destroy_runtime_data(UNIT(s), &s->exec_context, /* destroy_runtime_dir = */ true); unit_unref_uid_gid(UNIT(s), true); } diff --git a/src/core/unit.c b/src/core/unit.c index 71488a4555c..0d88f4f6416 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -6148,13 +6148,13 @@ int unit_test_trigger_loaded(Unit *u) { return 0; } -void unit_destroy_runtime_data(Unit *u, const ExecContext *context) { +void unit_destroy_runtime_data(Unit *u, const ExecContext *context, bool destroy_runtime_dir) { assert(u); assert(u->manager); assert(context); /* EXEC_PRESERVE_RESTART is handled via unit_release_resources()! */ - if (context->runtime_directory_preserve_mode == EXEC_PRESERVE_NO) + if (destroy_runtime_dir && context->runtime_directory_preserve_mode == EXEC_PRESERVE_NO) exec_context_destroy_runtime_directory(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]); exec_context_destroy_credentials(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME], u->id); diff --git a/src/core/unit.h b/src/core/unit.h index a8eb3663370..8a3b812a4bf 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -1036,7 +1036,7 @@ int unit_failure_action_exit_status(Unit *u); int unit_test_trigger_loaded(Unit *u); -void unit_destroy_runtime_data(Unit *u, const ExecContext *context); +void unit_destroy_runtime_data(Unit *u, const ExecContext *context, bool destroy_runtime_dir); int unit_clean(Unit *u, ExecCleanMask mask); int unit_can_clean(Unit *u, ExecCleanMask *ret_mask); diff --git a/test/units/TEST-23-UNIT-FILE.RuntimeDirectoryPreserve.sh b/test/units/TEST-23-UNIT-FILE.RuntimeDirectory.sh similarity index 56% rename from test/units/TEST-23-UNIT-FILE.RuntimeDirectoryPreserve.sh rename to test/units/TEST-23-UNIT-FILE.RuntimeDirectory.sh index ca57702cf14..d29331e99ff 100755 --- a/test/units/TEST-23-UNIT-FILE.RuntimeDirectoryPreserve.sh +++ b/test/units/TEST-23-UNIT-FILE.RuntimeDirectory.sh @@ -5,8 +5,6 @@ set -eux set -o pipefail -# Test RuntimeDirectoryPreserve=yes - at_exit() { set +e @@ -15,6 +13,21 @@ at_exit() { trap at_exit EXIT +# RuntimeDirectory= should be preserved for oneshot units if RemainAfterExit=yes + +systemd-run --service-type=oneshot --remain-after-exit \ + -u TEST-23-remain-after-exit.service \ + -p RuntimeDirectory=TEST-23-remain-after-exit \ + true + +[[ -d /run/TEST-23-remain-after-exit ]] + +systemctl stop TEST-23-remain-after-exit.service + +[[ ! -e /run/TEST-23-remain-after-exit ]] + +# Test RuntimeDirectoryPreserve=yes + systemd-mount -p RuntimeDirectory=hoge -p RuntimeDirectoryPreserve=yes -t tmpfs tmpfs /tmp/aaa touch /run/hoge/foo