mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 00:02:28 +00:00
Improve system call wrappers
This change improves copy_file_range(), sendfile(), splice(), openpty(), closefrom(), close_range(), fadvise() and posix_fadvise() in addition to writing tests that confirm things like errno and seeking behavior across platforms. We now less aggressively polyfill behavior with some of these functions when the platform support isn't available. Please see: https://justine.lol/cosmopolitan/functions.html
This commit is contained in:
parent
224c12f54d
commit
c7a8cd21e9
89 changed files with 1151 additions and 414 deletions
|
@ -21,6 +21,7 @@
|
|||
#include "libc/errno.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/f.h"
|
||||
#include "libc/testlib/subprocess.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
void SetUp(void) {
|
||||
|
@ -34,6 +35,10 @@ void SetUp(void) {
|
|||
}
|
||||
}
|
||||
|
||||
TEST(closefrom, ebadf) {
|
||||
ASSERT_SYS(EBADF, -1, closefrom(-2));
|
||||
}
|
||||
|
||||
TEST(closefrom, test) {
|
||||
ASSERT_SYS(0, 3, dup(2));
|
||||
ASSERT_SYS(0, 4, dup(2));
|
||||
|
@ -48,14 +53,23 @@ TEST(closefrom, test) {
|
|||
}
|
||||
|
||||
TEST(close_range, test) {
|
||||
ASSERT_SYS(0, 3, dup(2));
|
||||
ASSERT_SYS(0, 4, dup(2));
|
||||
ASSERT_SYS(0, 5, dup(2));
|
||||
ASSERT_SYS(0, 6, dup(2));
|
||||
EXPECT_SYS(0, 0, close_range(3, -1, 0));
|
||||
ASSERT_SYS(0, 0, fcntl(2, F_GETFD));
|
||||
ASSERT_SYS(EBADF, -1, fcntl(3, F_GETFD));
|
||||
ASSERT_SYS(EBADF, -1, fcntl(4, F_GETFD));
|
||||
ASSERT_SYS(EBADF, -1, fcntl(5, F_GETFD));
|
||||
ASSERT_SYS(EBADF, -1, fcntl(6, F_GETFD));
|
||||
if (IsLinux() || IsFreebsd()) {
|
||||
ASSERT_SYS(0, 3, dup(2));
|
||||
ASSERT_SYS(0, 4, dup(2));
|
||||
ASSERT_SYS(0, 5, dup(2));
|
||||
ASSERT_SYS(0, 6, dup(2));
|
||||
EXPECT_SYS(0, 0, close_range(3, -1, 0));
|
||||
ASSERT_SYS(0, 0, fcntl(2, F_GETFD));
|
||||
ASSERT_SYS(EBADF, -1, fcntl(3, F_GETFD));
|
||||
ASSERT_SYS(EBADF, -1, fcntl(4, F_GETFD));
|
||||
ASSERT_SYS(EBADF, -1, fcntl(5, F_GETFD));
|
||||
ASSERT_SYS(EBADF, -1, fcntl(6, F_GETFD));
|
||||
} else {
|
||||
EXPECT_SYS(ENOSYS, -1, close_range(3, -1, 0));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(close_range, ignoresNonexistantRanges) {
|
||||
if (!IsLinux() && !IsFreebsd()) return;
|
||||
EXPECT_SYS(0, 0, close_range(-2, -1, 0));
|
||||
}
|
||||
|
|
161
test/libc/calls/copy_file_range_test.c
Normal file
161
test/libc/calls/copy_file_range_test.c
Normal file
|
@ -0,0 +1,161 @@
|
|||
/*-*- 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/struct/stat.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/posix.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "libc/x/xasprintf.h"
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
void Make(const char *path, int mode) {
|
||||
int fd, n = lemur64() & 0xfffff;
|
||||
char *data = _gc(malloc(n));
|
||||
rngset(data, n, lemur64, -1);
|
||||
ASSERT_NE(-1, (fd = creat(path, mode)));
|
||||
ASSERT_SYS(0, n, write(fd, data, n));
|
||||
ASSERT_SYS(0, 0, close(fd));
|
||||
}
|
||||
|
||||
void Copy(const char *from, const char *to) {
|
||||
ssize_t rc;
|
||||
char buf[512];
|
||||
struct stat st;
|
||||
int fdin, fdout, e = errno;
|
||||
ASSERT_NE(-1, (fdin = open(from, O_RDONLY)));
|
||||
ASSERT_SYS(0, 0, fstat(fdin, &st));
|
||||
ASSERT_NE(-1, (fdout = creat(to, st.st_mode | 0400)));
|
||||
if (!(IsXnu() || IsOpenbsd())) {
|
||||
ASSERT_SYS(0, 0, posix_fadvise(fdin, 0, st.st_size, POSIX_FADV_SEQUENTIAL));
|
||||
}
|
||||
ASSERT_SYS(0, 0, ftruncate(fdout, st.st_size));
|
||||
while ((rc = copy_file_range(fdin, 0, fdout, 0, -1u, 0))) {
|
||||
if (rc == -1) {
|
||||
ASSERT_EQ(ENOSYS, errno);
|
||||
errno = e;
|
||||
while ((rc = read(fdin, buf, sizeof(buf)))) {
|
||||
ASSERT_NE(-1, rc);
|
||||
ASSERT_EQ(rc, write(fdout, buf, rc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT_SYS(0, 0, close(fdin));
|
||||
ASSERT_SYS(0, 0, close(fdout));
|
||||
}
|
||||
|
||||
TEST(copy_file_range, test) {
|
||||
char *p, *q;
|
||||
size_t n, m;
|
||||
Make("foo", 0644);
|
||||
Copy("foo", "bar");
|
||||
p = _gc(xslurp("foo", &n));
|
||||
q = _gc(xslurp("bar", &m));
|
||||
ASSERT_EQ(n, m);
|
||||
ASSERT_EQ(0, memcmp(p, q, n));
|
||||
}
|
||||
|
||||
bool HasCopyFileRange(void) {
|
||||
bool ok;
|
||||
int rc, e;
|
||||
e = errno;
|
||||
rc = copy_file_range(-1, 0, -1, 0, 0, 0);
|
||||
ok = rc == -1 && errno == EBADF;
|
||||
errno = e;
|
||||
return ok;
|
||||
}
|
||||
|
||||
TEST(copy_file_range, badFd) {
|
||||
if (!HasCopyFileRange()) return;
|
||||
ASSERT_SYS(EBADF, -1, copy_file_range(-1, 0, -1, 0, -1u, 0));
|
||||
}
|
||||
|
||||
TEST(copy_file_range, badFlags) {
|
||||
if (!HasCopyFileRange()) return;
|
||||
ASSERT_SYS(EINVAL, -1, copy_file_range(0, 0, 1, 0, -1u, -1));
|
||||
}
|
||||
|
||||
TEST(copy_file_range, differentFileSystems) {
|
||||
if (!IsLinux()) return;
|
||||
if (!HasCopyFileRange()) return;
|
||||
ASSERT_SYS(0, 3, open("/proc/stat", 0));
|
||||
ASSERT_SYS(0, 4, creat("foo", 0644));
|
||||
ASSERT_SYS(EXDEV, -1, copy_file_range(3, 0, 4, 0, -1u, 0));
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(copy_file_range, twoDifferentFiles) {
|
||||
char buf[16] = {0};
|
||||
int64_t i = 1, o = 0;
|
||||
if (!HasCopyFileRange()) return;
|
||||
ASSERT_SYS(0, 3, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644));
|
||||
ASSERT_SYS(0, 4, open("bar", O_RDWR | O_CREAT | O_TRUNC, 0644));
|
||||
ASSERT_SYS(0, 5, pwrite(3, "hello", 5, 0));
|
||||
ASSERT_SYS(0, 4, copy_file_range(3, &i, 4, &o, 4, 0));
|
||||
ASSERT_SYS(0, 0, copy_file_range(3, &i, 4, &o, 0, 0));
|
||||
ASSERT_EQ(5, i);
|
||||
ASSERT_EQ(4, o);
|
||||
ASSERT_SYS(0, 4, read(4, buf, 16));
|
||||
ASSERT_STREQ("ello", buf);
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(copy_file_range, sameFile_doesntChangeFilePointer) {
|
||||
char buf[16] = {0};
|
||||
int64_t i = 1, o = 5;
|
||||
if (!HasCopyFileRange()) return;
|
||||
ASSERT_SYS(0, 3, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644));
|
||||
ASSERT_SYS(0, 5, pwrite(3, "hello", 5, 0));
|
||||
ASSERT_SYS(0, 4, copy_file_range(3, &i, 3, &o, 4, 0));
|
||||
ASSERT_EQ(5, i);
|
||||
ASSERT_EQ(9, o);
|
||||
ASSERT_SYS(0, 9, read(3, buf, 16));
|
||||
ASSERT_STREQ("helloello", buf);
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(copy_file_range, overlappingRange) {
|
||||
int rc;
|
||||
int64_t i = 1, o = 2;
|
||||
if (!HasCopyFileRange()) return;
|
||||
ASSERT_SYS(0, 3, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644));
|
||||
ASSERT_SYS(0, 5, pwrite(3, "hello", 5, 0));
|
||||
rc = copy_file_range(3, &i, 3, &o, 4, 0);
|
||||
ASSERT_EQ(-1, rc);
|
||||
if (IsLinux()) {
|
||||
// [wut] rhel7 returns enosys here
|
||||
ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
|
||||
} else {
|
||||
ASSERT_TRUE(errno == EINVAL);
|
||||
}
|
||||
errno = 0;
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
|
@ -17,7 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/copyfd.internal.h"
|
||||
#include "libc/mem/copyfd.internal.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/ioctl.h"
|
||||
#include "libc/calls/pledge.internal.h"
|
||||
|
|
106
test/libc/calls/posix_fadvise_test.c
Normal file
106
test/libc/calls/posix_fadvise_test.c
Normal file
|
@ -0,0 +1,106 @@
|
|||
/*-*- 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/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/madv.h"
|
||||
#include "libc/sysv/consts/posix.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
void SetUp(void) {
|
||||
if (IsOpenbsd() || IsXnu()) exit(0);
|
||||
}
|
||||
|
||||
TEST(fadvise, ebadf) {
|
||||
ASSERT_SYS(EBADF, -1, fadvise(-1, 0, 0, MADV_SEQUENTIAL));
|
||||
}
|
||||
|
||||
TEST(posix_fadvise, ebadf) {
|
||||
ASSERT_SYS(0, EBADF, posix_fadvise(-1, 0, 0, POSIX_FADV_SEQUENTIAL));
|
||||
}
|
||||
|
||||
TEST(fadvise, test) {
|
||||
ASSERT_SYS(0, 0, xbarf("foo", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("foo", 0));
|
||||
ASSERT_SYS(0, 0, fadvise(3, 0, 0, MADV_SEQUENTIAL));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(posix_fadvise, test) {
|
||||
ASSERT_SYS(0, 0, xbarf("foo", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("foo", 0));
|
||||
ASSERT_SYS(0, 0, posix_fadvise(3, 0, 0, POSIX_FADV_SEQUENTIAL));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(fadvise, testBadAdvice) {
|
||||
ASSERT_SYS(0, 0, xbarf("foo", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("foo", 0));
|
||||
ASSERT_SYS(EINVAL, -1, fadvise(3, 0, 0, 127));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(posix_fadvise, testBadAdvice) {
|
||||
ASSERT_SYS(0, 0, xbarf("foo", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("foo", 0));
|
||||
ASSERT_SYS(0, EINVAL, posix_fadvise(3, 0, 0, 127));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(fadvise, testPastEof_isFine) {
|
||||
ASSERT_SYS(0, 0, xbarf("foo", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("foo", 0));
|
||||
ASSERT_SYS(0, 0, fadvise(3, 100, 100, MADV_SEQUENTIAL));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(fadvise, testNegativeLen_isInvalid) {
|
||||
ASSERT_SYS(0, 0, xbarf("foo", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("foo", 0));
|
||||
ASSERT_SYS(EINVAL, -1, fadvise(3, 0, INT64_MIN, MADV_SEQUENTIAL));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(posix_fadvise, testNegativeLen_isInvalid) {
|
||||
ASSERT_SYS(0, 0, xbarf("foo", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("foo", 0));
|
||||
ASSERT_SYS(0, EINVAL, posix_fadvise(3, 0, INT64_MIN, POSIX_FADV_SEQUENTIAL));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(fadvise, espipe) {
|
||||
int fds[2];
|
||||
ASSERT_SYS(0, 0, pipe(fds));
|
||||
ASSERT_SYS(ESPIPE, -1, fadvise(3, 0, 0, MADV_SEQUENTIAL));
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(posix_fadvise, espipe) {
|
||||
int fds[2];
|
||||
ASSERT_SYS(0, 0, pipe(fds));
|
||||
ASSERT_SYS(0, ESPIPE, posix_fadvise(3, 0, 0, POSIX_FADV_SEQUENTIAL));
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
81
test/libc/calls/splice_test.c
Normal file
81
test/libc/calls/splice_test.c
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*-*- 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/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
void SetUp(void) {
|
||||
int e = errno;
|
||||
if (splice(-1, 0, -1, 0, 0, 0) == -1 && errno == ENOSYS) {
|
||||
exit(0);
|
||||
}
|
||||
errno = e;
|
||||
}
|
||||
|
||||
TEST(splice, einval) {
|
||||
ASSERT_SYS(0, 0, splice(0, 0, 1, 0, 0, -1));
|
||||
}
|
||||
|
||||
TEST(splice, espipe) {
|
||||
int64_t x;
|
||||
int fds[2];
|
||||
ASSERT_SYS(0, 0, pipe(fds));
|
||||
ASSERT_SYS(0, 0, splice(0, 0, 4, &x, 0, 0));
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(splice, noOffsets_usesFilePointer) {
|
||||
// this test fails on rhel5 and rhel7
|
||||
int fds[2];
|
||||
char buf[16] = {0};
|
||||
ASSERT_SYS(0, 0, pipe(fds));
|
||||
ASSERT_SYS(0, 5, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644));
|
||||
ASSERT_SYS(0, 5, pwrite(5, "hello", 5, 0));
|
||||
ASSERT_SYS(0, 5, splice(5, 0, 4, 0, 5, 0));
|
||||
ASSERT_SYS(0, 5, splice(3, 0, 5, 0, 5, 0));
|
||||
ASSERT_SYS(0, 10, pread(5, buf, sizeof(buf), 0));
|
||||
ASSERT_STREQ("hellohello", buf);
|
||||
ASSERT_SYS(0, 0, close(5));
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(splice, offsets_doesntChangePointerAndIsReadOnly) {
|
||||
int fds[2];
|
||||
int64_t x = 0;
|
||||
char buf[16] = {0};
|
||||
ASSERT_SYS(0, 0, pipe(fds));
|
||||
ASSERT_SYS(0, 5, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644));
|
||||
ASSERT_SYS(0, 5, pwrite(5, "hello", 5, 0));
|
||||
ASSERT_SYS(0, 5, splice(5, &x, 4, 0, 5, 0));
|
||||
ASSERT_EQ(5, x);
|
||||
ASSERT_SYS(0, 5, splice(3, 0, 5, &x, 5, 0));
|
||||
ASSERT_EQ(10, x);
|
||||
ASSERT_SYS(0, 10, read(5, buf, sizeof(buf)));
|
||||
ASSERT_STREQ("hellohello", buf);
|
||||
ASSERT_SYS(0, 0, close(5));
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/mem/copyfd.internal.h"
|
||||
#include "libc/calls/landlock.h"
|
||||
#include "libc/calls/struct/dirent.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
|
@ -25,7 +26,6 @@
|
|||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/calls/copyfd.internal.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/sock.h"
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/log/libfatal.internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -33,6 +33,7 @@ TEST(asan, test) {
|
|||
char *p;
|
||||
if (!IsAsan()) return;
|
||||
p = gc(malloc(3));
|
||||
EXPECT_TRUE(__asan_is_valid(0, 0));
|
||||
EXPECT_TRUE(__asan_is_valid(p, 3));
|
||||
EXPECT_FALSE(__asan_is_valid(p, 4));
|
||||
EXPECT_TRUE(__asan_is_valid(p + 1, 2));
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/mem/copyfd.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
|
@ -26,7 +27,6 @@
|
|||
#include "libc/log/libfatal.internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/calls/copyfd.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
|
|
@ -17,8 +17,13 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sock/struct/sockaddr.h"
|
||||
|
@ -26,20 +31,31 @@
|
|||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/ipproto.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/shut.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
#include "libc/testlib/hyperion.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
void SetUpOnce(void) {
|
||||
if (IsNetbsd()) exit(0);
|
||||
if (IsOpenbsd()) exit(0);
|
||||
ASSERT_SYS(0, 0, pledge("stdio rpath wpath cpath proc inet", 0));
|
||||
}
|
||||
|
||||
TEST(sendfile, test) {
|
||||
int ws;
|
||||
char *buf;
|
||||
int64_t inoffset;
|
||||
int64_t GetFileOffset(int fd) {
|
||||
int64_t pos;
|
||||
ASSERT_NE(-1, (pos = lseek(fd, 0, SEEK_CUR)));
|
||||
return pos;
|
||||
}
|
||||
|
||||
TEST(sendfile, testSeeking) {
|
||||
char buf[1024];
|
||||
int rc, ws, fds[2];
|
||||
int64_t inoffset = 0;
|
||||
uint32_t addrsize = sizeof(struct sockaddr_in);
|
||||
struct sockaddr_in addr = {
|
||||
.sin_family = AF_INET,
|
||||
|
@ -53,24 +69,74 @@ TEST(sendfile, test) {
|
|||
ASSERT_SYS(0, 0, getsockname(3, &addr, &addrsize));
|
||||
ASSERT_SYS(0, 0, listen(3, 1));
|
||||
if (!fork()) {
|
||||
inoffset = 0;
|
||||
ASSERT_SYS(0, 4, accept(3, &addr, &addrsize));
|
||||
ASSERT_SYS(0, 5, open("hyperion.txt", O_RDONLY));
|
||||
ASSERT_SYS(0, 512, sendfile(4, 5, &inoffset, 512));
|
||||
EXPECT_EQ(512, inoffset);
|
||||
ASSERT_SYS(0, 0, close(5));
|
||||
ASSERT_SYS(0, 0, close(4));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 12, sendfile(4, 5, &inoffset, 12));
|
||||
ASSERT_EQ(0, GetFileOffset(5));
|
||||
ASSERT_SYS(0, 8, read(5, buf, 8));
|
||||
ASSERT_EQ(0, memcmp(buf, "The fall", 8));
|
||||
ASSERT_EQ(8, GetFileOffset(5));
|
||||
ASSERT_SYS(EBADF, -1, sendfile(5, 5, &inoffset, -1));
|
||||
ASSERT_EQ(-1, sendfile(5, 5, &inoffset, -1));
|
||||
ASSERT_TRUE(errno == ESPIPE || errno == EBADF);
|
||||
errno = 0;
|
||||
ASSERT_SYS(0, 500, sendfile(4, 5, &inoffset, -1));
|
||||
ASSERT_EQ(8, GetFileOffset(5));
|
||||
ASSERT_EQ(512, inoffset);
|
||||
inoffset = -1;
|
||||
ASSERT_SYS(EINVAL, -1, sendfile(4, 5, &inoffset, -1));
|
||||
_Exit(0);
|
||||
}
|
||||
buf = gc(malloc(512));
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
EXPECT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
|
||||
EXPECT_SYS(0, 0, connect(3, &addr, sizeof(addr)));
|
||||
EXPECT_SYS(0, 512, read(3, buf, 512));
|
||||
EXPECT_EQ(0, memcmp(buf, kHyperion, 512));
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
EXPECT_NE(-1, wait(&ws));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
|
||||
ASSERT_SYS(0, 0, connect(3, &addr, sizeof(addr)));
|
||||
ASSERT_SYS(0, 12, read(3, buf, 12)); // needed due to windows
|
||||
ASSERT_SYS(0, 500, read(3, buf + 12, 700));
|
||||
ASSERT_EQ(0, memcmp(buf, kHyperion, 512));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
ASSERT_NE(-1, wait(&ws));
|
||||
ASSERT_TRUE(WIFEXITED(ws));
|
||||
ASSERT_EQ(0, WEXITSTATUS(ws));
|
||||
}
|
||||
|
||||
TEST(sendfile, testPositioning) {
|
||||
int ws, fds[2];
|
||||
char buf[1024];
|
||||
uint32_t addrsize = sizeof(struct sockaddr_in);
|
||||
struct sockaddr_in addr = {
|
||||
.sin_family = AF_INET,
|
||||
.sin_addr.s_addr = htonl(0x7f000001),
|
||||
};
|
||||
ASSERT_SYS(0, 3, creat("hyperion.txt", 0644));
|
||||
ASSERT_SYS(0, 512, write(3, kHyperion, 512));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
|
||||
ASSERT_SYS(0, 0, bind(3, &addr, sizeof(addr)));
|
||||
ASSERT_SYS(0, 0, getsockname(3, &addr, &addrsize));
|
||||
ASSERT_SYS(0, 0, listen(3, 1));
|
||||
if (!fork()) {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
ASSERT_SYS(0, 4, accept(3, &addr, &addrsize));
|
||||
ASSERT_SYS(0, 5, open("hyperion.txt", O_RDONLY));
|
||||
ASSERT_SYS(0, 6, sendfile(4, 5, 0, 6));
|
||||
ASSERT_EQ(6, GetFileOffset(5));
|
||||
ASSERT_SYS(0, 6, sendfile(4, 5, 0, 6));
|
||||
ASSERT_SYS(0, 0, shutdown(4, SHUT_WR));
|
||||
ASSERT_EQ(-1, sendfile(4, 5, 0, 6));
|
||||
ASSERT_TRUE(errno == EINVAL || errno == EPIPE);
|
||||
errno = 0;
|
||||
ASSERT_EQ(12, GetFileOffset(5));
|
||||
_Exit(0);
|
||||
}
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
|
||||
ASSERT_SYS(0, 0, connect(3, &addr, sizeof(addr)));
|
||||
ASSERT_SYS(0, 6, read(3, buf, 6));
|
||||
ASSERT_SYS(0, 6, read(3, buf + 6, 6));
|
||||
ASSERT_SYS(0, 0, read(3, buf, 12));
|
||||
ASSERT_EQ(0, memcmp(buf, kHyperion, 12));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
ASSERT_NE(-1, wait(&ws));
|
||||
ASSERT_TRUE(WIFEXITED(ws));
|
||||
ASSERT_EQ(0, WEXITSTATUS(ws));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue