mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +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
|
@ -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