Improve system call polyfills

- Polyfill open() w/ O_CLOEXEC on RHEL5
- Remove old workaround from rmdir() on the New Technology
- preadv() and pwritev() are now smarter about demodernization
- preadv() and pwritev() are now available on the New Technology
This commit is contained in:
Justine Tunney 2021-03-08 10:07:02 -08:00
parent 816b0e1851
commit 0ad609268f
21 changed files with 260 additions and 117 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/iovec.h"
@ -25,54 +26,84 @@
#include "libc/macros.internal.h"
#include "libc/sysv/consts/iov.h"
#include "libc/sysv/errfuns.h"
#include "libc/zipos/zipos.internal.h"
#define __NR_preadv_linux 0x0127
/**
* Reads data from multiple buffers from file descriptor at offset.
* Reads with maximum generality.
*
* @param count is recommended to be 16 or fewer; if it exceeds IOV_MAX
* then the extra buffers are simply ignored
* @return number of bytes actually read, or -1 w/ errno
* @asyncsignalsafe
* @vforksafe
*/
ssize_t preadv(int fd, struct iovec *iovec, int count, int64_t off) {
ssize_t preadv(int fd, struct iovec *iov, int iovlen, int64_t off) {
static bool once, demodernize;
int olderr;
int i, err;
ssize_t rc;
if (!count) return 0;
if ((count = MIN(count, IOV_MAX)) < 0) return einval();
size_t got, toto;
if (fd < 0) return einval();
if (iovlen < 0) return einval();
if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
return weaken(__zipos_read)(
(struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, iov, iovlen, off);
} else if (IsWindows()) {
if (fd < g_fds.n) {
return sys_read_nt(g_fds.p + fd, iov, iovlen, off);
} else {
return ebadf();
}
} else if (IsMetal()) {
return enosys();
}
/*
* NT, XNU, and 2007-era Linux don't support this system call.
*/
if (!once) {
once = true;
if (IsModeDbg() || (IsLinux() && iovec->iov_len >= __NR_preadv_linux)) {
/*
* Read size is too large to detect older kernels safely without
* introducing nontrivial mechanics. We'll try again later.
*/
once = false;
err = errno;
rc = sys_preadv(fd, iov, iovlen, off, off);
if (rc == -1 && errno == ENOSYS) {
errno = err;
once = true;
demodernize = true;
} else {
olderr = errno;
rc = sys_preadv(fd, iovec, count, off, off);
if (rc == -1 && errno == ENOSYS) {
errno = olderr;
demodernize = true;
} else if (IsLinux() && rc == __NR_preadv_linux /*RHEL5:CVE-2010-3301*/) {
demodernize = true;
} else if (IsLinux() && rc == __NR_preadv_linux) {
if (__iovec_size(iov, iovlen) < __NR_preadv_linux) {
demodernize = true; /*RHEL5:CVE-2010-3301*/
once = true;
} else {
return rc;
}
} else {
once = true;
return rc;
}
}
if (!demodernize) {
return sys_preadv(fd, iovec, count, off, off);
} else {
return pread(fd, iovec[0].iov_base, iovec[0].iov_len, off);
return sys_preadv(fd, iov, iovlen, off, off);
}
if (!iovlen) {
return sys_pread(fd, NULL, 0, off, off);
}
for (toto = i = 0; i < iovlen; ++i) {
rc = sys_pread(fd, iov[i].iov_base, iov[i].iov_len, off, off);
if (rc == -1) {
if (toto && (errno == EINTR || errno == EAGAIN)) {
return toto;
} else {
return -1;
}
}
got = rc;
toto += got;
if (got != iov[i].iov_len) {
break;
}
}
return toto;
}