Make WSL detection dynamic rather than statically compiled

This resolves the issue where running pre-compiled Linux packages from
binary package manager repositories lead fish to think that we are not
running under WSL.

- Closes #5619.
- Ping neovim/neovim#7330
This commit is contained in:
Mahmoud Al-Qudsi 2019-02-14 17:53:07 -06:00
parent 8811a10690
commit 9796a331bc
2 changed files with 34 additions and 11 deletions

View file

@ -31,6 +31,12 @@
#include <sys/ioctl.h>
#endif
#ifdef __linux__
// Includes for WSL detection
#include <cstring>
#include <sys/utsname.h>
#endif
#ifdef __FreeBSD__
#include <sys/sysctl.h>
#elif __APPLE__
@ -148,6 +154,31 @@ long convert_hex_digit(wchar_t d) {
return -1;
}
bool is_windows_subsystem_for_linux() {
#if defined(WSL)
return true;
#elif not defined(__linux__)
return false;
#endif
// We are purposely not using std::call_once as it may invoke locking, which is an unnecessary
// overhead since there's no actual race condition here - even if multiple threads call this
// routine simultaneously the first time around, we just end up needlessly querying uname(2) one
// more time.
static bool wsl_state = []() {
utsname info;
uname(&info);
// Sample utsname.release under WSL: 4.4.0-17763-Microsoft
return strstr(info.release, "Microsoft") != nullptr;
}();
// Subsequent calls to this function may take place after fork() and before exec() in
// postfork.cpp. Make sure we never dynamically allocate any memory in the fast path!
return wsl_state;
}
#ifdef HAVE_BACKTRACE_SYMBOLS
// This function produces a stack backtrace with demangled function & method names. It is based on
// https://gist.github.com/fmela/591333 but adapted to the style of the fish project.

View file

@ -866,18 +866,10 @@ void assert_is_not_forked_child(const char *who);
#define ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(x) assert_is_not_forked_child(x)
#define ASSERT_IS_NOT_FORKED_CHILD() ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(__FUNCTION__)
/// Detect if we are Windows Subsystem for Linux by inspecting /proc/sys/kernel/osrelease
/// and checking if "Microsoft" is in the first line.
/// Determines if we are running under Microsoft's Windows Subsystem for Linux to work around
/// some known limitations and/or bugs.
/// See https://github.com/Microsoft/WSL/issues/423 and Microsoft/WSL#2997
constexpr bool is_windows_subsystem_for_linux() {
// This function is called after fork() and before exec() in postfork.cpp. Make sure we
// don't allocate any memory here!
#ifdef WSL
return true;
#else
return false;
#endif
}
bool is_windows_subsystem_for_linux();
/// Detect if we are running under Cygwin or Cgywin64
constexpr bool is_cygwin() {