mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 07:29:23 +00:00
Workaround sendfile() bug in WSL
This commit is contained in:
parent
fc96af058b
commit
f44d88707e
4 changed files with 59 additions and 1 deletions
49
libc/intrin/__is_wsl.c
Normal file
49
libc/intrin/__is_wsl.c
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -29,6 +29,7 @@ extern unsigned char _tls_size[];
|
||||||
extern unsigned char _tls_content[];
|
extern unsigned char _tls_content[];
|
||||||
|
|
||||||
void _init(void) hidden;
|
void _init(void) hidden;
|
||||||
|
bool __is_wsl(void);
|
||||||
void __morph_tls(void);
|
void __morph_tls(void);
|
||||||
void __enable_tls(void);
|
void __enable_tls(void);
|
||||||
void __enable_threads(void) hidden;
|
void __enable_threads(void) hidden;
|
||||||
|
|
|
@ -87,6 +87,10 @@ TEST(fcntl, getfd) {
|
||||||
ASSERT_SYS(0, 0, fcntl(3, F_GETFD));
|
ASSERT_SYS(0, 0, fcntl(3, F_GETFD));
|
||||||
ASSERT_SYS(0, 4, open("/dev/null", O_RDWR | O_CLOEXEC));
|
ASSERT_SYS(0, 4, open("/dev/null", O_RDWR | O_CLOEXEC));
|
||||||
ASSERT_SYS(0, FD_CLOEXEC, fcntl(4, F_GETFD));
|
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(4));
|
||||||
ASSERT_SYS(0, 0, close(3));
|
ASSERT_SYS(0, 0, close(3));
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
#include "libc/mem/gc.internal.h"
|
#include "libc/mem/gc.internal.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/runtime/internal.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/sock/sock.h"
|
#include "libc/sock/sock.h"
|
||||||
#include "libc/sock/struct/sockaddr.h"
|
#include "libc/sock/struct/sockaddr.h"
|
||||||
|
@ -125,7 +126,10 @@ TEST(sendfile, testPositioning) {
|
||||||
ASSERT_EQ(-1, sendfile(4, 5, 0, 6));
|
ASSERT_EQ(-1, sendfile(4, 5, 0, 6));
|
||||||
ASSERT_TRUE(errno == EINVAL || errno == EPIPE);
|
ASSERT_TRUE(errno == EINVAL || errno == EPIPE);
|
||||||
errno = 0;
|
errno = 0;
|
||||||
ASSERT_EQ(12, GetFileOffset(5));
|
// XXX: WSL clobbers file offset on failure!
|
||||||
|
if (!__is_wsl()) {
|
||||||
|
ASSERT_EQ(12, GetFileOffset(5));
|
||||||
|
}
|
||||||
_Exit(0);
|
_Exit(0);
|
||||||
}
|
}
|
||||||
ASSERT_SYS(0, 0, close(3));
|
ASSERT_SYS(0, 0, close(3));
|
||||||
|
|
Loading…
Add table
Reference in a new issue