diff --git a/libc/intrin/__is_wsl.c b/libc/intrin/__is_wsl.c new file mode 100644 index 000000000..bb0ee25e6 --- /dev/null +++ b/libc/intrin/__is_wsl.c @@ -0,0 +1,49 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/calls/syscall-sysv.internal.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/runtime/internal.h" +#include "libc/sysv/consts/map.h" +#include "libc/sysv/consts/prot.h" + +#define MAP_GROWSDOWN_linux 0x00000100 +#define MAP_ANONYMOUS_linux 0x00000020 + +/** + * Returns true if host platform is WSL. + */ +bool __is_wsl(void) { + int e; + void *p; + bool res; + if (!IsLinux()) return false; + e = errno; + p = __sys_mmap(0, 4096, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS_linux | MAP_GROWSDOWN_linux, -1, 0, + 0); + if (p != MAP_FAILED) { + __sys_munmap(p, 4096); + return false; + } + res = errno == ENOTSUP; + errno = e; + return res; +} diff --git a/libc/runtime/internal.h b/libc/runtime/internal.h index 6345e5a26..1fc9be768 100644 --- a/libc/runtime/internal.h +++ b/libc/runtime/internal.h @@ -29,6 +29,7 @@ extern unsigned char _tls_size[]; extern unsigned char _tls_content[]; void _init(void) hidden; +bool __is_wsl(void); void __morph_tls(void); void __enable_tls(void); void __enable_threads(void) hidden; diff --git a/test/libc/calls/fcntl_test.c b/test/libc/calls/fcntl_test.c index 5af72435b..4229f241b 100644 --- a/test/libc/calls/fcntl_test.c +++ b/test/libc/calls/fcntl_test.c @@ -87,6 +87,10 @@ TEST(fcntl, getfd) { ASSERT_SYS(0, 0, fcntl(3, F_GETFD)); ASSERT_SYS(0, 4, open("/dev/null", O_RDWR | O_CLOEXEC)); ASSERT_SYS(0, FD_CLOEXEC, fcntl(4, F_GETFD)); + ASSERT_SYS(0, 0, fcntl(4, F_SETFD, FD_CLOEXEC)); + ASSERT_SYS(0, FD_CLOEXEC, fcntl(4, F_GETFD)); + ASSERT_SYS(0, 0, fcntl(4, F_SETFD, 0)); + ASSERT_SYS(0, 0, fcntl(4, F_GETFD)); ASSERT_SYS(0, 0, close(4)); ASSERT_SYS(0, 0, close(3)); } diff --git a/test/libc/sock/sendfile_test.c b/test/libc/sock/sendfile_test.c index ad1570ca9..8bacbc972 100644 --- a/test/libc/sock/sendfile_test.c +++ b/test/libc/sock/sendfile_test.c @@ -24,6 +24,7 @@ #include "libc/limits.h" #include "libc/mem/gc.internal.h" #include "libc/mem/mem.h" +#include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" #include "libc/sock/struct/sockaddr.h" @@ -125,7 +126,10 @@ TEST(sendfile, testPositioning) { ASSERT_EQ(-1, sendfile(4, 5, 0, 6)); ASSERT_TRUE(errno == EINVAL || errno == EPIPE); errno = 0; - ASSERT_EQ(12, GetFileOffset(5)); + // XXX: WSL clobbers file offset on failure! + if (!__is_wsl()) { + ASSERT_EQ(12, GetFileOffset(5)); + } _Exit(0); } ASSERT_SYS(0, 0, close(3));