mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-27 04:50:28 +00:00
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:
parent
c20dad3534
commit
45b72485ad
1032 changed files with 6083 additions and 2348 deletions
|
@ -34,9 +34,10 @@ TOOL_BUILD_DIRECTDEPS = \
|
|||
LIBC_FMT \
|
||||
LIBC_INTRIN \
|
||||
LIBC_LOG \
|
||||
LIBC_LOG_ASAN \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_NT_KERNEL32 \
|
||||
LIBC_NT_WS2_32 \
|
||||
LIBC_NT_USER32 \
|
||||
LIBC_RAND \
|
||||
LIBC_RUNTIME \
|
||||
|
|
|
@ -40,7 +40,7 @@ static const unsigned char kBlocks[] = {
|
|||
[0b0001] = 0xdf, // ▀
|
||||
};
|
||||
|
||||
forceinline void *memset(void *di, int al, unsigned long cx) {
|
||||
forceinline void *SetMemory(void *di, int al, unsigned long cx) {
|
||||
asm("rep stosb"
|
||||
: "=D"(di), "=c"(cx), "=m"(*(char(*)[cx])di)
|
||||
: "0"(di), "1"(cx), "a"(al));
|
||||
|
@ -130,9 +130,9 @@ int main() {
|
|||
SetVideoMode(3);
|
||||
for (g = 0;; ++g) {
|
||||
SetEs(0);
|
||||
memset((void *)(0x7c00 + 512), 0, 25 * 80);
|
||||
SetMemory((void *)(0x7c00 + 512), 0, 25 * 80);
|
||||
SetEs(0xb8000 >> 4);
|
||||
/* memset((void *)0, 0, 25 * 80 * 2); */
|
||||
/* SetMemory((void *)0, 0, 25 * 80 * 2); */
|
||||
spiral((void *)0, (void *)(0x7c00 + 512), g);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,42 +19,46 @@
|
|||
#include "libc/assert.h"
|
||||
#include "libc/bits/safemacros.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/hefty/spawn.h"
|
||||
#include "libc/calls/struct/iovec.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "tool/build/lib/demangle.h"
|
||||
|
||||
struct CxxFilt {
|
||||
int pid;
|
||||
int fds[3];
|
||||
int reader;
|
||||
int writer;
|
||||
} g_cxxfilt;
|
||||
|
||||
void CloseCxxFilt(void) {
|
||||
close(g_cxxfilt.fds[0]);
|
||||
close(g_cxxfilt.fds[1]);
|
||||
close(g_cxxfilt.reader);
|
||||
close(g_cxxfilt.writer);
|
||||
g_cxxfilt.pid = -1;
|
||||
}
|
||||
|
||||
void SpawnCxxFilt(void) {
|
||||
int pid;
|
||||
int pipefds[2][2];
|
||||
const char *cxxfilt;
|
||||
char path[PATH_MAX];
|
||||
cxxfilt = firstnonnull(getenv("CXXFILT"), "c++filt");
|
||||
cxxfilt = firstnonnull(emptytonull(getenv("CXXFILT")), "c++filt");
|
||||
if (commandv(cxxfilt, path)) {
|
||||
g_cxxfilt.fds[0] = -1;
|
||||
g_cxxfilt.fds[1] = -1;
|
||||
g_cxxfilt.fds[2] = 2;
|
||||
if ((pid = spawnve(0, g_cxxfilt.fds, path, (char *const[]){cxxfilt, NULL},
|
||||
environ)) != -1) {
|
||||
atexit(CloseCxxFilt);
|
||||
pipe2(pipefds[0], O_CLOEXEC);
|
||||
pipe2(pipefds[1], O_CLOEXEC);
|
||||
if (!(g_cxxfilt.pid = vfork())) {
|
||||
dup2(pipefds[1][0], 0);
|
||||
dup2(pipefds[0][1], 1);
|
||||
execv(path, (char *const[]){cxxfilt, NULL});
|
||||
abort();
|
||||
}
|
||||
g_cxxfilt.reader = pipefds[0][0];
|
||||
g_cxxfilt.writer = pipefds[1][1];
|
||||
atexit(CloseCxxFilt);
|
||||
} else {
|
||||
pid = -1;
|
||||
g_cxxfilt.pid = -1;
|
||||
}
|
||||
g_cxxfilt.pid = pid;
|
||||
}
|
||||
|
||||
char *CopySymbol(char *p, size_t pn, const char *s, size_t sn) {
|
||||
|
@ -90,8 +94,8 @@ char *DemangleCxxFilt(char *p, size_t pn, const char *s, size_t sn) {
|
|||
iov[0].iov_len = sn;
|
||||
iov[1].iov_base = buf;
|
||||
iov[1].iov_len = 1;
|
||||
writev(g_cxxfilt.fds[0], iov, ARRAYLEN(iov));
|
||||
if ((rc = read(g_cxxfilt.fds[1], buf, sizeof(buf))) != -1) {
|
||||
writev(g_cxxfilt.writer, iov, ARRAYLEN(iov));
|
||||
if ((rc = read(g_cxxfilt.reader, buf, sizeof(buf))) != -1) {
|
||||
got = rc;
|
||||
if (got >= 2 && buf[got - 1] == '\n') {
|
||||
if (buf[got - 2] == '\r') --got;
|
||||
|
|
|
@ -20,10 +20,11 @@
|
|||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/safemacros.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/hefty/spawn.h"
|
||||
#include "libc/calls/struct/flock.h"
|
||||
#include "libc/calls/struct/itimerval.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/dns/dns.h"
|
||||
#include "libc/errno.h"
|
||||
|
@ -43,10 +44,14 @@
|
|||
#include "libc/sysv/consts/ai.h"
|
||||
#include "libc/sysv/consts/ex.h"
|
||||
#include "libc/sysv/consts/exit.h"
|
||||
#include "libc/sysv/consts/f.h"
|
||||
#include "libc/sysv/consts/fd.h"
|
||||
#include "libc/sysv/consts/fileno.h"
|
||||
#include "libc/sysv/consts/ipproto.h"
|
||||
#include "libc/sysv/consts/itimer.h"
|
||||
#include "libc/sysv/consts/lock.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/pr.h"
|
||||
#include "libc/sysv/consts/shut.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
|
@ -109,9 +114,14 @@ char *g_prog;
|
|||
char *g_runitd;
|
||||
jmp_buf g_jmpbuf;
|
||||
uint16_t g_sshport;
|
||||
uint16_t g_runitdport;
|
||||
char g_ssh[PATH_MAX];
|
||||
char g_hostname[128];
|
||||
uint16_t g_runitdport;
|
||||
volatile bool alarmed;
|
||||
|
||||
static void OnAlarm(void) {
|
||||
alarmed = true;
|
||||
}
|
||||
|
||||
forceinline pureconst size_t GreatestTwoDivisor(size_t x) {
|
||||
return x & (~x + 1);
|
||||
|
@ -154,74 +164,104 @@ void Upload(int pipe, int fd, struct stat *st) {
|
|||
}
|
||||
|
||||
void DeployEphemeralRunItDaemonRemotelyViaSsh(struct addrinfo *ai) {
|
||||
int lock;
|
||||
size_t got;
|
||||
char *args[7];
|
||||
struct stat st;
|
||||
char linebuf[32];
|
||||
int sshpid, wstatus, binfd, sshfds[3];
|
||||
DEBUGF("spawning %s on %s:%hu", g_runitd, g_hostname, g_runitdport);
|
||||
CHECK_NE(-1, (binfd = open(g_runitd, O_RDONLY | O_CLOEXEC)));
|
||||
CHECK_NE(-1, fstat(binfd, &st));
|
||||
sshfds[0] = -1;
|
||||
sshfds[1] = -1;
|
||||
sshfds[2] = STDERR_FILENO;
|
||||
CHECK_NE(-1, (sshpid = spawnve(
|
||||
0, sshfds, g_ssh,
|
||||
(char *const[]){"ssh", "-C", "-p",
|
||||
gc(xasprintf("%hu", g_sshport)), g_hostname,
|
||||
gc(MakeDeployScript(ai, st.st_size)), NULL},
|
||||
environ)));
|
||||
Upload(sshfds[0], binfd, &st);
|
||||
CHECK_NE(-1, close(sshfds[0]));
|
||||
CHECK_NE(-1, (got = read(sshfds[1], linebuf, sizeof(linebuf))));
|
||||
CHECK_GT(got, 0);
|
||||
linebuf[sizeof(linebuf) - 1] = '\0';
|
||||
if (strncmp(linebuf, "ready ", 6) != 0) {
|
||||
FATALF("expected ready response but got %`'.*s", got, linebuf);
|
||||
struct timeval now, then;
|
||||
int sshpid, wstatus, binfd, pipefds[2][2];
|
||||
mkdir("o", 0755);
|
||||
CHECK_NE(-1, (lock = open(gc(xasprintf("o/lock.%s", g_hostname)),
|
||||
O_RDWR | O_CREAT, 0644)));
|
||||
CHECK_NE(-1, flock(lock, LOCK_EX));
|
||||
CHECK_NE(-1, gettimeofday(&now, 0));
|
||||
if (!read(lock, &then, 16) || ((now.tv_sec * 1000 + now.tv_usec / 1000) -
|
||||
(then.tv_sec * 1000 + then.tv_usec / 1000)) >=
|
||||
(RUNITD_TIMEOUT_MS >> 1)) {
|
||||
DEBUGF("spawning %s on %s:%hu", g_runitd, g_hostname, g_runitdport);
|
||||
CHECK_NE(-1, (binfd = open(g_runitd, O_RDONLY | O_CLOEXEC)));
|
||||
CHECK_NE(-1, fstat(binfd, &st));
|
||||
args[0] = "ssh";
|
||||
args[1] = "-C";
|
||||
args[2] = "-p";
|
||||
args[3] = gc(xasprintf("%hu", g_sshport));
|
||||
args[4] = g_hostname;
|
||||
args[5] = gc(MakeDeployScript(ai, st.st_size));
|
||||
args[6] = NULL;
|
||||
CHECK_NE(-1, pipe2(pipefds[0], O_CLOEXEC));
|
||||
CHECK_NE(-1, pipe2(pipefds[1], O_CLOEXEC));
|
||||
if (!(sshpid = vfork())) {
|
||||
dup2(pipefds[0][0], 0);
|
||||
dup2(pipefds[1][1], 1);
|
||||
execv(g_ssh, args);
|
||||
abort();
|
||||
}
|
||||
close(pipefds[0][0]);
|
||||
close(pipefds[1][1]);
|
||||
Upload(pipefds[0][1], binfd, &st);
|
||||
LOGIFNEG1(close(pipefds[0][1]));
|
||||
CHECK_NE(-1, (got = read(pipefds[1][0], linebuf, sizeof(linebuf))));
|
||||
CHECK_GT(got, 0, "on host %s", g_hostname);
|
||||
linebuf[sizeof(linebuf) - 1] = '\0';
|
||||
if (strncmp(linebuf, "ready ", 6) != 0) {
|
||||
FATALF("expected ready response but got %`'.*s", got, linebuf);
|
||||
} else {
|
||||
DEBUGF("got ready response");
|
||||
}
|
||||
g_runitdport = (uint16_t)atoi(&linebuf[6]);
|
||||
LOGIFNEG1(close(pipefds[1][0]));
|
||||
CHECK_NE(-1, waitpid(sshpid, &wstatus, 0));
|
||||
CHECK_EQ(0, WEXITSTATUS(wstatus));
|
||||
CHECK_NE(-1, gettimeofday(&now, 0));
|
||||
CHECK_NE(-1, lseek(lock, 0, SEEK_SET));
|
||||
CHECK_NE(-1, write(lock, &now, 16));
|
||||
} else {
|
||||
DEBUGF("nospawn %s on %s:%hu", g_runitd, g_hostname, g_runitdport);
|
||||
}
|
||||
g_runitdport = (uint16_t)atoi(&linebuf[6]);
|
||||
CHECK_NE(-1, close(sshfds[1]));
|
||||
CHECK_NE(-1, waitpid(sshpid, &wstatus, 0));
|
||||
CHECK_EQ(0, WEXITSTATUS(wstatus));
|
||||
LOGIFNEG1(close(lock));
|
||||
}
|
||||
|
||||
void SetDeadline(int micros) {
|
||||
setitimer(ITIMER_REAL, &(const struct itimerval){{0, 0}, {0, micros}}, NULL);
|
||||
}
|
||||
|
||||
void Connect(int attempt) {
|
||||
int rc, olderr;
|
||||
void Connect(void) {
|
||||
const char *ip4;
|
||||
int rc, err, expo;
|
||||
struct addrinfo *ai;
|
||||
if ((rc = getaddrinfo(g_hostname, gc(xasprintf("%hu", g_runitdport)),
|
||||
&kResolvHints, &ai)) != 0) {
|
||||
FATALF("%s:%hu: EAI_%s %m", g_hostname, g_runitdport, eai2str(rc));
|
||||
unreachable;
|
||||
}
|
||||
ip4 = (const char *)&ai->ai_addr4->sin_addr;
|
||||
if (ispublicip(ai->ai_family, &ai->ai_addr4->sin_addr)) {
|
||||
ip4 = (const char *)&ai->ai_addr4->sin_addr;
|
||||
FATALF("%s points to %hhu.%hhu.%hhu.%hhu"
|
||||
" which isn't part of a local/private/testing subnet",
|
||||
g_hostname, ip4[0], ip4[1], ip4[2], ip4[3]);
|
||||
unreachable;
|
||||
}
|
||||
CHECK_NE(-1, (g_sock = socket(ai->ai_family, ai->ai_socktype | SOCK_CLOEXEC,
|
||||
ai->ai_protocol)));
|
||||
SetDeadline(50000);
|
||||
olderr = errno;
|
||||
DEBUGF("connecting to %s (%hhu.%hhu.%hhu.%hhu) to run %s", g_hostname, ip4[0],
|
||||
ip4[1], ip4[2], ip4[3], g_prog);
|
||||
CHECK_NE(-1,
|
||||
(g_sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)));
|
||||
expo = 1;
|
||||
TryAgain:
|
||||
alarmed = false;
|
||||
SetDeadline(100000);
|
||||
rc = connect(g_sock, ai->ai_addr, ai->ai_addrlen);
|
||||
err = errno;
|
||||
SetDeadline(0);
|
||||
if (rc == -1) {
|
||||
if (!attempt &&
|
||||
(errno == ECONNREFUSED || errno == EHOSTUNREACH || errno == EINTR)) {
|
||||
errno = olderr;
|
||||
if ((err == ECONNREFUSED || err == EHOSTUNREACH || err == ECONNRESET ||
|
||||
err == EINTR)) {
|
||||
usleep((expo *= 2));
|
||||
DeployEphemeralRunItDaemonRemotelyViaSsh(ai);
|
||||
Connect(1);
|
||||
} else if (errno == EINTR) {
|
||||
fprintf(stderr, "%s(%s:%hu): %s\n", "connect", g_hostname, g_runitdport,
|
||||
"offline, icmp misconfigured, or too slow; tune make HOSTS=...");
|
||||
exit(1);
|
||||
goto TryAgain;
|
||||
} else {
|
||||
FATALF("%s(%s:%hu): %m", "connect", g_hostname, g_runitdport);
|
||||
FATALF("%s(%s:%hu): %s", "connect", g_hostname, g_runitdport,
|
||||
strerror(err));
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
|
@ -268,10 +308,15 @@ int ReadResponse(void) {
|
|||
size_t n, m;
|
||||
unsigned char *p;
|
||||
enum RunitCommand cmd;
|
||||
static long backoff;
|
||||
static unsigned char msg[512];
|
||||
res = -1;
|
||||
for (;;) {
|
||||
CHECK_NE(-1, (rc = recv(g_sock, msg, sizeof(msg), 0)));
|
||||
if ((rc = recv(g_sock, msg, sizeof(msg), 0)) == -1) {
|
||||
CHECK_EQ(ECONNRESET, errno);
|
||||
usleep((backoff = (backoff + 1000) * 2));
|
||||
break;
|
||||
}
|
||||
p = &msg[0];
|
||||
n = (size_t)rc;
|
||||
if (!n) break;
|
||||
|
@ -283,7 +328,9 @@ int ReadResponse(void) {
|
|||
switch (cmd) {
|
||||
case kRunitExit:
|
||||
CHECK_GE(n, 1);
|
||||
res = *p;
|
||||
if ((res = *p & 0xff)) {
|
||||
WARNF("%s on %s exited with %d", g_prog, g_hostname, res);
|
||||
}
|
||||
goto drop;
|
||||
case kRunitStderr:
|
||||
CHECK_GE(n, 4);
|
||||
|
@ -312,6 +359,7 @@ drop:
|
|||
}
|
||||
|
||||
int RunOnHost(char *spec) {
|
||||
int rc;
|
||||
char *p;
|
||||
for (p = spec; *p; ++p) {
|
||||
if (*p == ':') *p = ' ';
|
||||
|
@ -319,9 +367,12 @@ int RunOnHost(char *spec) {
|
|||
CHECK_GE(sscanf(spec, "%100s %hu %hu", g_hostname, &g_runitdport, &g_sshport),
|
||||
1);
|
||||
if (!strchr(g_hostname, '.')) strcat(g_hostname, ".test.");
|
||||
Connect(0);
|
||||
SendRequest();
|
||||
return ReadResponse();
|
||||
do {
|
||||
Connect();
|
||||
SendRequest();
|
||||
rc = ReadResponse();
|
||||
} while (rc == -1);
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool IsParallelBuild(void) {
|
||||
|
@ -384,8 +435,8 @@ int RunRemoteTestsInParallel(char *hosts[], int count) {
|
|||
|
||||
int main(int argc, char *argv[]) {
|
||||
showcrashreports();
|
||||
g_loglevel = kLogDebug;
|
||||
const struct sigaction onsigalrm = {.sa_handler = (void *)missingno};
|
||||
/* g_loglevel = kLogDebug; */
|
||||
const struct sigaction onsigalrm = {.sa_handler = (void *)OnAlarm};
|
||||
if (argc > 1 &&
|
||||
(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
|
||||
ShowUsage(stdout, 0);
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/safemacros.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/hefty/spawn.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/dce.h"
|
||||
|
@ -54,6 +53,7 @@
|
|||
#include "libc/sysv/consts/so.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
#include "libc/sysv/consts/sol.h"
|
||||
#include "libc/sysv/consts/w.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/getopt/getopt.h"
|
||||
|
@ -99,6 +99,7 @@
|
|||
jmp_buf g_jb;
|
||||
char *g_exepath;
|
||||
volatile bool g_childterm;
|
||||
volatile int g_childstatus;
|
||||
struct sockaddr_in g_servaddr;
|
||||
unsigned char g_buf[PAGESIZE];
|
||||
bool g_daemonize, g_sendready;
|
||||
|
@ -110,14 +111,18 @@ void OnInterrupt(int sig) {
|
|||
once = true;
|
||||
kill(0, sig);
|
||||
for (;;) {
|
||||
if (waitpid(-1, NULL, 0) == -1) break;
|
||||
if (waitpid(-1, NULL, 0) == -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
gclongjmp(g_jb, sig);
|
||||
unreachable;
|
||||
}
|
||||
|
||||
void OnChildTerminated(int sig) {
|
||||
g_childterm = true;
|
||||
while (waitpid(-1, &g_childstatus, WNOHANG) > 0) {
|
||||
g_childterm = true;
|
||||
}
|
||||
}
|
||||
|
||||
wontreturn void ShowUsage(FILE *f, int rc) {
|
||||
|
@ -126,10 +131,6 @@ wontreturn void ShowUsage(FILE *f, int rc) {
|
|||
exit(rc);
|
||||
}
|
||||
|
||||
/\
|
||||
* hi
|
||||
*/
|
||||
|
||||
void GetOpts(int argc, char *argv[]) {
|
||||
int opt;
|
||||
g_timeout = RUNITD_TIMEOUT_MS;
|
||||
|
@ -175,8 +176,8 @@ void StartTcpServer(void) {
|
|||
int yes = true;
|
||||
uint32_t asize;
|
||||
CHECK_NE(-1, (g_servfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)));
|
||||
setsockopt(g_servfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
|
||||
setsockopt(g_servfd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes));
|
||||
LOGIFNEG1(setsockopt(g_servfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)));
|
||||
LOGIFNEG1(setsockopt(g_servfd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)));
|
||||
if (bind(g_servfd, &g_servaddr, sizeof(g_servaddr)) == -1) {
|
||||
if (g_servaddr.sin_port != 0) {
|
||||
g_servaddr.sin_port = 0;
|
||||
|
@ -192,6 +193,7 @@ void StartTcpServer(void) {
|
|||
if (g_sendready) {
|
||||
printf("ready %hu\n", ntohs(g_servaddr.sin_port));
|
||||
fflush(stdout);
|
||||
fclose(stdout);
|
||||
stdout->fd = g_devnullfd;
|
||||
}
|
||||
CHECK_NE(-1, fcntl(g_servfd, F_SETFD, FD_CLOEXEC));
|
||||
|
@ -240,13 +242,17 @@ void HandleClient(void) {
|
|||
ssize_t got, wrote;
|
||||
struct sockaddr_in addr;
|
||||
char *addrstr, *exename;
|
||||
int wstatus, child, stdiofds[3];
|
||||
int rc, wstatus, child, pipefds[2];
|
||||
uint32_t addrsize, namesize, filesize, remaining;
|
||||
|
||||
/* read request to run program */
|
||||
addrsize = sizeof(addr);
|
||||
CHECK_NE(-1, (g_clifd = accept4(g_servfd, &addr, &addrsize, SOCK_CLOEXEC)));
|
||||
defer(close_s, &g_clifd);
|
||||
if (fork()) {
|
||||
close(g_clifd);
|
||||
return;
|
||||
}
|
||||
g_childterm = false;
|
||||
addrstr = gc(DescribeAddress(&addr));
|
||||
DEBUGF("%s %s %s", gc(DescribeAddress(&g_servaddr)), "accepted", addrstr);
|
||||
got = recv(g_clifd, (p = &g_buf[0]), sizeof(g_buf), 0);
|
||||
|
@ -270,8 +276,6 @@ void HandleClient(void) {
|
|||
/* write the file to disk */
|
||||
remaining = filesize;
|
||||
CHECK_NE(-1, (g_exefd = creat(g_exepath, 0700)));
|
||||
defer(unlink_s, &g_exepath);
|
||||
defer(close_s, &g_exefd);
|
||||
ftruncate(g_exefd, filesize);
|
||||
if (got) {
|
||||
CHECK_EQ(got, write(g_exefd, p, got));
|
||||
|
@ -284,7 +288,8 @@ void HandleClient(void) {
|
|||
if (!got) {
|
||||
LOGF("%s %s %,u/%,u %s", addrstr, "sent", remaining, filesize,
|
||||
"bytes before hangup");
|
||||
return;
|
||||
unlink(g_exepath);
|
||||
_exit(0);
|
||||
}
|
||||
remaining -= got;
|
||||
p = &g_buf[0];
|
||||
|
@ -293,68 +298,86 @@ void HandleClient(void) {
|
|||
CHECK_LE(wrote, got);
|
||||
} while ((got -= wrote));
|
||||
}
|
||||
/* CHECK_NE(-1, shutdown(g_clifd, SHUT_RD)); */
|
||||
CHECK_NE(-1, close_s(&g_exefd));
|
||||
LOGIFNEG1(shutdown(g_clifd, SHUT_RD));
|
||||
LOGIFNEG1(close(g_exefd));
|
||||
|
||||
/* run program, tee'ing stderr to both log and client */
|
||||
DEBUGF("spawning %s", exename);
|
||||
g_childterm = false;
|
||||
stdiofds[0] = g_devnullfd;
|
||||
stdiofds[1] = g_devnullfd;
|
||||
stdiofds[2] = -1;
|
||||
CHECK_NE(-1, (child = spawnve(0, stdiofds, g_exepath,
|
||||
(char *const[]){g_exepath, NULL}, environ)));
|
||||
CHECK_NE(-1, pipe2(pipefds, O_CLOEXEC));
|
||||
if (!(child = vfork())) {
|
||||
dup2(pipefds[1], 2);
|
||||
execv(g_exepath, (char *const[]){g_exepath, NULL});
|
||||
abort();
|
||||
}
|
||||
close(pipefds[1]);
|
||||
DEBUGF("communicating %s[%d]", exename, child);
|
||||
for (;;) {
|
||||
CHECK_NE(-1, (got = read(stdiofds[2], g_buf, sizeof(g_buf))));
|
||||
CHECK_NE(-1, (got = read(pipefds[0], g_buf, sizeof(g_buf))));
|
||||
if (!got) {
|
||||
close_s(&stdiofds[2]);
|
||||
close(pipefds[0]);
|
||||
break;
|
||||
}
|
||||
fwrite(g_buf, got, 1, stderr);
|
||||
SendOutputFragmentMessage(g_clifd, kRunitStderr, g_buf, got);
|
||||
}
|
||||
if (!g_childterm) {
|
||||
CHECK_NE(-1, waitpid(child, &wstatus, 0));
|
||||
|
||||
if ((rc = waitpid(child, &wstatus, 0)) != -1) {
|
||||
g_childstatus = wstatus;
|
||||
} else {
|
||||
CHECK_EQ(ECHILD, errno);
|
||||
CHECK(g_childterm);
|
||||
}
|
||||
DEBUGF("exited %s[%d] → %d", exename, child, WEXITSTATUS(wstatus));
|
||||
if (WIFSIGNALED(g_childstatus)) {
|
||||
rc = 128 + WTERMSIG(g_childstatus);
|
||||
} else {
|
||||
rc = WEXITSTATUS(g_childstatus);
|
||||
}
|
||||
DEBUGF("exited %s[%d] -> %d", exename, child, rc);
|
||||
|
||||
/* let client know how it went */
|
||||
SendExitMessage(g_clifd, WEXITSTATUS(wstatus));
|
||||
/* CHECK_NE(-1, shutdown(g_clifd, SHUT_RDWR)); */
|
||||
CHECK_NE(-1, close(g_clifd));
|
||||
LOGIFNEG1(unlink(g_exepath));
|
||||
SendExitMessage(g_clifd, rc);
|
||||
LOGIFNEG1(shutdown(g_clifd, SHUT_RDWR));
|
||||
LOGIFNEG1(close(g_clifd));
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
int Poll(void) {
|
||||
int i, evcount;
|
||||
struct pollfd fds[] = {{g_servfd, POLLIN}};
|
||||
int i, wait, evcount;
|
||||
struct pollfd fds[1];
|
||||
TryAgain:
|
||||
evcount = poll(fds, ARRAYLEN(fds), g_timeout);
|
||||
fds[0].fd = g_servfd;
|
||||
fds[0].events = POLLIN;
|
||||
wait = MIN(1000, g_timeout);
|
||||
evcount = poll(fds, ARRAYLEN(fds), wait);
|
||||
if (!evcount) g_timeout -= wait;
|
||||
if (evcount == -1 && errno == EINTR) goto TryAgain;
|
||||
CHECK_NE(-1, evcount);
|
||||
for (i = 0; i < evcount; ++i) {
|
||||
CHECK(fds[i].revents & POLLIN);
|
||||
HandleClient();
|
||||
}
|
||||
/* manually do this because of nt */
|
||||
while (waitpid(-1, NULL, WNOHANG) > 0) donothing;
|
||||
return evcount;
|
||||
}
|
||||
|
||||
int Serve(void) {
|
||||
int rc;
|
||||
const struct sigaction onsigint = {.sa_handler = (void *)OnInterrupt,
|
||||
.sa_flags = SA_RESETHAND};
|
||||
.sa_flags = SA_NODEFER};
|
||||
const struct sigaction onsigterm = {.sa_handler = (void *)OnInterrupt,
|
||||
.sa_flags = SA_RESETHAND};
|
||||
const struct sigaction onsigchld = {.sa_handler = SIG_IGN,
|
||||
.sa_flags = SA_RESETHAND | SA_RESTART};
|
||||
.sa_flags = SA_NODEFER};
|
||||
const struct sigaction onsigchld = {.sa_handler = (void *)OnChildTerminated,
|
||||
.sa_flags = SA_RESTART};
|
||||
StartTcpServer();
|
||||
defer(close_s, &g_servfd);
|
||||
if (!(rc = setjmp(g_jb))) {
|
||||
sigaction(SIGINT, &onsigint, NULL);
|
||||
sigaction(SIGTERM, &onsigterm, NULL);
|
||||
sigaction(SIGCHLD, &onsigchld, NULL);
|
||||
while (g_servfd != -1) {
|
||||
if (!Poll()) break;
|
||||
for (;;) {
|
||||
if (!Poll() && !g_timeout) break;
|
||||
}
|
||||
LOGF("timeout expired, shutting down");
|
||||
} else {
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
/*-*- 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 2020 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/internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *p;
|
||||
int i, j, rc, fd;
|
||||
char16_t glyphs[17] = {0};
|
||||
p = malloc(4096);
|
||||
fd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0);
|
||||
if ((rc = getdents(fd, p, 4096)) != -1) {
|
||||
for (i = 0; i < ROUNDUP(rc, 16); ++i) {
|
||||
if (i % 16 == 0) printf("%08x ", i);
|
||||
if (i < rc) {
|
||||
glyphs[i % 16] = kCp437[p[i] & 0xff];
|
||||
printf("%02x ", p[i] & 0xff);
|
||||
} else {
|
||||
glyphs[i % 16] = u'\0';
|
||||
printf(" ");
|
||||
}
|
||||
if (i % 8 == 7) printf(" ");
|
||||
if (i % 16 == 15) printf("%hs\n", glyphs);
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
free(p);
|
||||
return 0;
|
||||
}
|
|
@ -1791,9 +1791,7 @@ Keywords={
|
|||
"FRAMESIZE",
|
||||
"BIGPAGESIZE",
|
||||
"STACKSIZE",
|
||||
"ENV_MAX",
|
||||
"ARG_MAX",
|
||||
"CMD_MAX",
|
||||
"PATH_MAX",
|
||||
"BUFSIZ",
|
||||
"CACHELINE",
|
||||
|
|
|
@ -107,6 +107,7 @@
|
|||
"note"
|
||||
"protip"
|
||||
"nxbitsafe"
|
||||
"vforksafe"
|
||||
"preinitsafe"
|
||||
"asyncsignalsafe"
|
||||
"notasyncsignalsafe"
|
||||
|
|
|
@ -1287,9 +1287,7 @@
|
|||
"YOINK"
|
||||
"STATIC_YOINK"
|
||||
"STATIC_YOINK_SOURCE"
|
||||
"STRINGIFY"
|
||||
"likely"
|
||||
"unlikely"))
|
||||
"STRINGIFY"))
|
||||
|
||||
)
|
||||
(concat "\\_<"
|
||||
|
|
|
@ -134,9 +134,7 @@
|
|||
"FRAMESIZE"
|
||||
"BIGPAGESIZE"
|
||||
"STACKSIZE"
|
||||
"ENV_MAX"
|
||||
"ARG_MAX"
|
||||
"CMD_MAX"
|
||||
"PATH_MAX"
|
||||
"BUFSIZ"
|
||||
"CACHELINE"
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os,sys,re
|
||||
|
||||
kar_types = frozenset([
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue