Fix XNU / FreeBSD / OpenBSD / RHEL5 / NT bugs

For the first time ever, all tests in this codebase now pass, when
run automatically on macos, freebsd, openbsd, rhel5, rhel7, alpine
and windows via the network using the runit and runitd build tools

- Fix vfork exec path etc.
- Add XNU opendir() support
- Add OpenBSD opendir() support
- Add Linux history to syscalls.sh
- Use copy_file_range on FreeBSD 13+
- Fix system calls with 7+ arguments
- Fix Windows with greater than 16 FDs
- Fix RUNIT.COM and RUNITD.COM flakiness
- Fix OpenBSD munmap() when files are mapped
- Fix long double so it's actually long on Windows
- Fix OpenBSD truncate() and ftruncate() thunk typo
- Let Windows fcntl() be used on socket files descriptors
- Fix Windows fstat() which had an accidental printf statement
- Fix RHEL5 CLOCK_MONOTONIC by not aliasing to CLOCK_MONOTONIC_RAW

This is wonderful. I never could have dreamed it would be possible
to get it working so well on so many platforms with tiny binaries.

Fixes #31
Fixes #25
Fixes #14
This commit is contained in:
Justine Tunney 2021-01-25 13:08:05 -08:00
parent c20dad3534
commit 45b72485ad
1032 changed files with 6083 additions and 2348 deletions

View file

@ -19,7 +19,6 @@
#include "dsp/core/core.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/struct/winsize.h"
#include "libc/dce.h"
@ -35,6 +34,7 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/ok.h"
#include "libc/sysv/consts/termios.h"
#include "libc/x/x.h"
@ -216,8 +216,8 @@ static void ReadAll(int fd, void *buf, size_t n) {
static void LoadImageOrDie(const char *path, size_t size, long yn, long xn,
unsigned char RGB[yn][xn][4]) {
int pid, ws, fds[3];
char *convert, pathbuf[PATH_MAX];
int pid, ws, pipefds[2];
char *convert, dim[64], pathbuf[PATH_MAX];
if (isempty((convert = getenv("CONVERT"))) &&
!(IsWindows() && access((convert = "\\msys64\\mingw64\\bin\\convert.exe"),
X_OK) != -1) &&
@ -227,18 +227,20 @@ static void LoadImageOrDie(const char *path, size_t size, long yn, long xn,
stderr);
exit(1);
}
fds[0] = STDIN_FILENO;
fds[1] = -1;
fds[2] = STDERR_FILENO;
pid = spawnve(0, fds, convert,
(char *const[]){"convert", path, "-resize",
gc(xasprintf("%ux%u!", xn, yn)), "-colorspace",
"RGB", "-depth", "8", "rgba:-", NULL},
environ);
CHECK_NE(-1, pid);
ReadAll(fds[1], RGB, size);
CHECK_NE(-1, close(fds[1]));
sprintf(dim, "%ux%u!", xn, yn);
CHECK_NE(-1, pipe2(pipefds, O_CLOEXEC));
if (!(pid = vfork())) {
dup2(pipefds[1], 1);
execv(convert,
(char *const[]){"convert", path, "-resize", dim, "-colorspace", "RGB",
"-depth", "8", "rgba:-", NULL});
abort();
}
close(pipefds[1]);
ReadAll(pipefds[0], RGB, size);
close(pipefds[0]);
CHECK_NE(-1, waitpid(pid, &ws, 0));
CHECK(WIFEXITED(ws));
CHECK_EQ(0, WEXITSTATUS(ws));
}

View file

@ -19,7 +19,6 @@
#include "dsp/tty/itoa8.h"
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/termios.h"
@ -486,23 +485,25 @@ static int ReadAll(int fd, void *data, size_t size) {
static void LoadFileViaImageMagick(const char *path, unsigned yn, unsigned xn,
unsigned char rgb[yn][YS][xn][XS][CN]) {
const char *convert;
char pathbuf[PATH_MAX];
int pid, ws, fds[3] = {STDIN_FILENO, -1, STDERR_FILENO};
int pid, ws, pipefds[2];
char pathbuf[PATH_MAX], dim[32];
if (!(convert = commandv("convert", pathbuf))) {
fputs("error: `convert` command not found\n"
"try: apt-get install imagemagick\n",
stderr);
exit(EXIT_FAILURE);
}
CHECK_NE(-1,
(pid = spawnve(
0, fds, convert,
(char *const[]){"convert", path, "-resize",
xasprintf("%ux%u!", xn * XS, yn * YS), "-depth",
"8", "-colorspace", "sRGB", "rgb:-", NULL},
environ)));
CHECK_NE(-1, ReadAll(fds[STDOUT_FILENO], rgb, yn * YS * xn * XS * CN));
CHECK_NE(-1, close(fds[STDOUT_FILENO]));
sprintf(dim, "%ux%u!", xn * XS, yn * YS);
CHECK_NE(-1, pipe2(pipefds, O_CLOEXEC));
if (!(pid = vfork())) {
dup2(pipefds[1], 1);
execv(convert, (char *const[]){"convert", path, "-resize", dim, "-depth",
"8", "-colorspace", "sRGB", "rgb:-", NULL});
abort();
}
CHECK_NE(-1, close(pipefds[1]));
CHECK_NE(-1, ReadAll(pipefds[0], rgb, yn * YS * xn * XS * CN));
CHECK_NE(-1, close(pipefds[0]));
CHECK_NE(-1, waitpid(pid, &ws, 0));
CHECK_EQ(0, WEXITSTATUS(ws));
}

View file

@ -30,7 +30,6 @@
#include "libc/bits/safemacros.h"
#include "libc/bits/xchg.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/calls/internal.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/sigbits.h"
@ -483,22 +482,18 @@ static int WriteAudio(int fd, const void *data, size_t size, int deadlinems) {
}
static bool TrySpeaker(const char *prog, char *const *args) {
int rc;
int fds[3];
fds[0] = -1;
fds[1] = fileno(g_logfile);
fds[2] = fileno(g_logfile);
LOGF("spawning %s", prog);
if ((rc = spawnve(0, fds, prog, args, environ)) != -1) {
playpid_ = rc;
playfd_ = fds[0];
/* CHECK_NE(-1, fcntl(playfd_, F_SETFL, O_NONBLOCK)); */
LOGF("spawned %s pid=%d fd=%d", prog, playpid_, playfd_);
return true;
} else {
WARNF("couldn't spawn %s", prog);
return false;
int pipefds[2];
CHECK_NE(-1, pipe2(pipefds, O_CLOEXEC));
if (!(playpid_ = fork())) {
dup2(pipefds[0], 0);
dup2(fileno(g_logfile), 1);
dup2(fileno(g_logfile), 2);
close(fileno(g_logfile));
execv(prog, args);
abort();
}
playfd_ = pipefds[1];
return true;
}
static bool TrySox(void) {
@ -1340,7 +1335,6 @@ static void OnExit(void) {
ttyidentclear(&ti_);
close_s(&infd_);
close_s(&outfd_);
close_s(&nullfd_);
bfree(&graphic_[0].b);
bfree(&graphic_[1].b);
bfree(&vtframe_[0].b);
@ -1483,7 +1477,6 @@ int main(int argc, char *argv[]) {
ffplay_ = commandvenv("FFPLAY", "ffplay");
infd_ = STDIN_FILENO;
outfd_ = STDOUT_FILENO;
nullfd_ = open("/dev/null", O_APPEND | O_RDWR);
if (!setjmp(jb_)) {
xsigaction(SIGINT, OnCtrlC, 0, 0, NULL);
xsigaction(SIGHUP, OnCtrlC, 0, 0, NULL);