mdbx-windows: refine WSL1 detection.

Try to fix https://github.com/snej/nimdbx/issues/1

Change-Id: Iec1c77c82ced8095e3f4e03a27a58e0bba3df76b
This commit is contained in:
Leonid Yuriev 2020-11-18 22:38:26 +03:00
parent 87161f5920
commit 862cfb9a3b
3 changed files with 23 additions and 12 deletions

View File

@ -1516,6 +1516,7 @@ stepbystep
storz storz
stoull stoull
strcasecmp strcasecmp
strcasestr
strcat strcat
strchr strchr
strcmp strcmp

View File

@ -26,13 +26,17 @@
#ifndef MDBX_ALLOY #ifndef MDBX_ALLOY
uint32_t mdbx_linux_kernel_version; uint32_t mdbx_linux_kernel_version;
bool mdbx_RunningOnWSL; bool mdbx_RunningOnWSL1;
#endif /* MDBX_ALLOY */ #endif /* MDBX_ALLOY */
static __cold bool probe_for_WSL(const char *tag) { static __cold uint8_t probe_for_WSL(const char *tag) {
/* "Official" way of detecting WSL but not WSL2 const char *const WSL = strstr(tag, "WSL");
* https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364 */ if (WSL && WSL[3] >= '2' && WSL[3] <= '9')
return strstr(tag, "Microsoft") || strstr(tag, "WSL"); return WSL[3] - '0';
const char *const wsl = strstr(tag, "wsl");
if (wsl && wsl[3] >= '2' && wsl[3] <= '9')
return wsl[3] - '0';
return (WSL || wsl || strcasestr(tag, "Microsoft")) ? 1 : 0;
} }
#endif /* Linux */ #endif /* Linux */
@ -42,9 +46,16 @@ mdbx_global_constructor(void) {
#if defined(__linux__) || defined(__gnu_linux__) #if defined(__linux__) || defined(__gnu_linux__)
struct utsname buffer; struct utsname buffer;
if (uname(&buffer) == 0) { if (uname(&buffer) == 0) {
mdbx_RunningOnWSL = probe_for_WSL(buffer.version) || /* "Official" way of detecting WSL1 but not WSL2
probe_for_WSL(buffer.sysname) || * https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364
probe_for_WSL(buffer.release); *
* WARNING: False negative detection of WSL1 will result in DATA LOSS!
* So, the REQUIREMENTS for this code:
* 1. MUST detect WSL1 without false-negatives.
* 2. DESIRABLE detect WSL2 but without the risk of violating the first. */
mdbx_RunningOnWSL1 = probe_for_WSL(buffer.version) == 1 ||
probe_for_WSL(buffer.sysname) == 1 ||
probe_for_WSL(buffer.release) == 1;
int i = 0; int i = 0;
char *p = buffer.release; char *p = buffer.release;
while (*p && i < 4) { while (*p && i < 4) {
@ -314,10 +325,10 @@ MDBX_INTERNAL_FUNC int __cold mdbx_lck_seize(MDBX_env *env) {
int rc = MDBX_SUCCESS; int rc = MDBX_SUCCESS;
#if defined(__linux__) || defined(__gnu_linux__) #if defined(__linux__) || defined(__gnu_linux__)
if (unlikely(mdbx_RunningOnWSL)) { if (unlikely(mdbx_RunningOnWSL1)) {
rc = ENOLCK /* No record locks available */; rc = ENOLCK /* No record locks available */;
mdbx_error("%s, err %u", mdbx_error("%s, err %u",
"WSL (Windows Subsystem for Linux) is mad and trouble-full, " "WSL1 (Windows Subsystem for Linux) is mad and trouble-full, "
"injecting failure to avoid data loss", "injecting failure to avoid data loss",
rc); rc);
return rc; return rc;

View File

@ -583,8 +583,7 @@ MDBX_INTERNAL_FUNC int mdbx_vasprintf(char **strp, const char *fmt, va_list ap);
#if defined(__linux__) || defined(__gnu_linux__) #if defined(__linux__) || defined(__gnu_linux__)
MDBX_INTERNAL_VAR uint32_t mdbx_linux_kernel_version; MDBX_INTERNAL_VAR uint32_t mdbx_linux_kernel_version;
MDBX_INTERNAL_VAR bool MDBX_INTERNAL_VAR bool mdbx_RunningOnWSL1 /* Windows Subsystem 1 for Linux */;
mdbx_RunningOnWSL /* Windows Subsystem for Linux is mad and trouble-full */;
#endif /* Linux */ #endif /* Linux */
#ifndef mdbx_strdup #ifndef mdbx_strdup