Skip to content

Commit

Permalink
Make WSL detection dynamic rather than statically compiled
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mqudsi committed Feb 15, 2019
1 parent 8811a10 commit 9796a33
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
31 changes: 31 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 3 additions & 11 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 9796a33

Please sign in to comment.