From d1327b016dec4c20c5bbb58bc438b33ec9fd178c Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Tue, 17 Sep 2019 01:04:25 +0300 Subject: [PATCH] mdbx: refine PID checking mdbx_env_close_ex() for closing-after-fork. Change-Id: I5669b3477ef2e5fc30d6211668a01d3ac78ee163 --- src/elements/core.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/elements/core.c b/src/elements/core.c index f3a27252..932fb85e 100644 --- a/src/elements/core.c +++ b/src/elements/core.c @@ -3160,11 +3160,7 @@ __cold static int mdbx_env_sync_ex(MDBX_env *env, int force, int nonblock) { if (unlikely(env->me_signature != MDBX_ME_SIGNATURE)) return MDBX_EBADSIGN; -#if MDBX_TXN_CHECKPID || !(defined(_WIN32) || defined(_WIN64)) - /* Check the PID even if MDBX_TXN_CHECKPID=0 on non-Windows - * platforms (i.e. where fork() is available). - * This is required to legitimize a call to mdbx_end_close() after fork() - * from a child process, that should be allowed to free resources. */ +#if MDBX_TXN_CHECKPID if (unlikely(env->me_pid != mdbx_getpid())) { env->me_flags |= MDBX_FATAL_ERROR; return MDBX_PANIC; @@ -7171,6 +7167,15 @@ int __cold mdbx_env_close_ex(MDBX_env *env, int dont_sync) { if (unlikely(env->me_signature != MDBX_ME_SIGNATURE)) return MDBX_EBADSIGN; +#if MDBX_TXN_CHECKPID || !(defined(_WIN32) || defined(_WIN64)) + /* Check the PID even if MDBX_TXN_CHECKPID=0 on non-Windows + * platforms (i.e. where fork() is available). + * This is required to legitimize a call after fork() + * from a child process, that should be allowed to free resources. */ + if (unlikely(env->me_pid != mdbx_getpid())) + env->me_flags |= MDBX_FATAL_ERROR; +#endif /* MDBX_TXN_CHECKPID */ + if ((env->me_flags & (MDBX_RDONLY | MDBX_FATAL_ERROR)) == 0) { if (env->me_txn0 && env->me_txn0->mt_owner && env->me_txn0->mt_owner != mdbx_thread_self())