2022-10-02 15:30:39 +00:00
|
|
|
/*-*- 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. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-02-23 02:58:23 +00:00
|
|
|
#include "libc/assert.h"
|
|
|
|
#include "libc/calls/blockcancel.internal.h"
|
2022-10-02 15:30:39 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2023-02-23 02:58:23 +00:00
|
|
|
#include "libc/calls/cp.internal.h"
|
2023-02-24 19:48:24 +00:00
|
|
|
#include "libc/calls/internal.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/calls/struct/sigset.internal.h"
|
2023-02-23 02:58:23 +00:00
|
|
|
#include "libc/calls/struct/stat.internal.h"
|
2022-10-02 15:30:39 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
|
|
|
#include "libc/dce.h"
|
2023-02-23 02:58:23 +00:00
|
|
|
#include "libc/errno.h"
|
2022-10-02 15:30:39 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2023-09-11 20:51:37 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2023-03-30 01:16:46 +00:00
|
|
|
#include "libc/intrin/safemacros.internal.h"
|
2022-10-02 15:30:39 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2023-02-23 02:58:23 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2023-09-06 10:54:42 +00:00
|
|
|
#include "libc/limits.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/proc/execve.internal.h"
|
2022-10-02 15:30:39 +00:00
|
|
|
#include "libc/str/str.h"
|
2023-02-24 19:48:24 +00:00
|
|
|
#include "libc/sysv/consts/f.h"
|
|
|
|
#include "libc/sysv/consts/fd.h"
|
2023-02-23 02:58:23 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/mfd.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
#include "libc/sysv/consts/shm.h"
|
2022-10-02 15:30:39 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2023-02-23 02:58:23 +00:00
|
|
|
static bool IsAPEFd(const int fd) {
|
|
|
|
char buf[8];
|
2023-09-11 20:51:37 +00:00
|
|
|
return (sys_pread(fd, buf, 8, 0, 0) == 8) && IsApeLoadable(buf);
|
2023-02-23 02:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fexecve_impl(const int fd, char *const argv[], char *const envp[]) {
|
|
|
|
int rc;
|
|
|
|
if (IsLinux()) {
|
|
|
|
char path[14 + 12];
|
|
|
|
FormatInt32(stpcpy(path, "/proc/self/fd/"), fd);
|
|
|
|
rc = __sys_execve(path, argv, envp);
|
|
|
|
} else if (IsFreebsd()) {
|
|
|
|
rc = sys_fexecve(fd, argv, envp);
|
|
|
|
} else {
|
|
|
|
rc = enosys();
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
PTF_NUM = 1 << 0,
|
|
|
|
PTF_NUM2 = 1 << 1,
|
|
|
|
PTF_NUM3 = 1 << 2,
|
|
|
|
PTF_ANY = 1 << 3
|
|
|
|
} PTF_PARSE;
|
|
|
|
|
|
|
|
static bool ape_to_elf(void *ape, const size_t apesize) {
|
|
|
|
static const char printftok[] = "printf '";
|
|
|
|
static const size_t printftoklen = sizeof(printftok) - 1;
|
|
|
|
const char *tok = memmem(ape, apesize, printftok, printftoklen);
|
|
|
|
if (tok) {
|
|
|
|
tok += printftoklen;
|
|
|
|
uint8_t *dest = ape;
|
|
|
|
PTF_PARSE state = PTF_ANY;
|
|
|
|
uint8_t value = 0;
|
|
|
|
for (; tok < (const char *)(dest + apesize); tok++) {
|
|
|
|
if ((state & (PTF_NUM | PTF_NUM2 | PTF_NUM3)) &&
|
|
|
|
(*tok >= '0' && *tok <= '7')) {
|
|
|
|
value = (value << 3) | (*tok - '0');
|
|
|
|
state <<= 1;
|
|
|
|
if (state & PTF_ANY) {
|
|
|
|
*dest++ = value;
|
|
|
|
}
|
|
|
|
} else if (state & PTF_NUM) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (state & (PTF_NUM2 | PTF_NUM3)) {
|
|
|
|
*dest++ = value;
|
|
|
|
}
|
|
|
|
if (*tok == '\\') {
|
|
|
|
state = PTF_NUM;
|
|
|
|
value = 0;
|
|
|
|
} else if (*tok == '\'') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
*dest++ = *tok;
|
|
|
|
state = PTF_ANY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errno = ENOEXEC;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-24 19:48:24 +00:00
|
|
|
/**
|
|
|
|
* Creates a memfd and copies fd to it.
|
|
|
|
*
|
|
|
|
* This does an inplace conversion of APE to ELF when detected!!!!
|
|
|
|
*/
|
|
|
|
static int fd_to_mem_fd(const int infd, char *path) {
|
2023-09-02 03:49:13 +00:00
|
|
|
if ((!IsLinux() && !IsFreebsd()) || !_weaken(mmap) || !_weaken(munmap)) {
|
2023-02-23 02:58:23 +00:00
|
|
|
return enosys();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat st;
|
2023-02-24 19:48:24 +00:00
|
|
|
if (fstat(infd, &st) == -1) {
|
2023-02-23 02:58:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int fd;
|
|
|
|
if (IsLinux()) {
|
|
|
|
fd = sys_memfd_create(__func__, MFD_CLOEXEC);
|
|
|
|
} else if (IsFreebsd()) {
|
|
|
|
fd = sys_shm_open(SHM_ANON, O_CREAT | O_RDWR, 0);
|
|
|
|
} else {
|
|
|
|
return enosys();
|
|
|
|
}
|
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
void *space;
|
|
|
|
if ((sys_ftruncate(fd, st.st_size, st.st_size) != -1) &&
|
|
|
|
((space = _weaken(mmap)(0, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
|
|
fd, 0)) != MAP_FAILED)) {
|
|
|
|
ssize_t readRc;
|
2023-02-24 19:48:24 +00:00
|
|
|
readRc = pread(infd, space, st.st_size, 0);
|
2023-02-23 02:58:23 +00:00
|
|
|
bool success = readRc != -1;
|
2023-09-11 20:51:37 +00:00
|
|
|
if (success && (st.st_size > 8) && IsApeLoadable(space)) {
|
2023-03-30 01:16:46 +00:00
|
|
|
int flags = fcntl(fd, F_GETFD);
|
2023-09-02 03:49:13 +00:00
|
|
|
if ((success = (flags != -1) &&
|
|
|
|
(fcntl(fd, F_SETFD, flags & (~FD_CLOEXEC)) != -1) &&
|
|
|
|
ape_to_elf(space, st.st_size))) {
|
2023-03-30 01:16:46 +00:00
|
|
|
const int newfd = fcntl(fd, F_DUPFD, 9001);
|
|
|
|
if (newfd != -1) {
|
|
|
|
close(fd);
|
|
|
|
fd = newfd;
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 02:58:23 +00:00
|
|
|
}
|
|
|
|
const int e = errno;
|
|
|
|
if ((_weaken(munmap)(space, st.st_size) != -1) && success) {
|
2023-03-30 01:16:46 +00:00
|
|
|
if (path) {
|
|
|
|
FormatInt32(stpcpy(path, "COSMOPOLITAN_INIT_ZIPOS="), fd);
|
|
|
|
}
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(readRc == st.st_size);
|
2023-02-23 02:58:23 +00:00
|
|
|
return fd;
|
|
|
|
} else if (!success) {
|
|
|
|
errno = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const int e = errno;
|
|
|
|
close(fd);
|
|
|
|
errno = e;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-10-02 15:30:39 +00:00
|
|
|
/**
|
|
|
|
* Executes binary executable at file descriptor.
|
|
|
|
*
|
2023-02-23 02:58:23 +00:00
|
|
|
* This is only supported on Linux and FreeBSD. APE binaries are
|
2023-02-24 19:48:24 +00:00
|
|
|
* supported. Zipos is supported. Zipos fds or FD_CLOEXEC APE fds or
|
|
|
|
* fds that fail fexecve with ENOEXEC are copied to a new memfd (with
|
|
|
|
* in-memory APE to ELF conversion) and fexecve is (re)attempted.
|
2022-10-02 15:30:39 +00:00
|
|
|
*
|
|
|
|
* @param fd is opened executable and current file position is ignored
|
|
|
|
* @return doesn't return on success, otherwise -1 w/ errno
|
|
|
|
* @raise ENOEXEC if file at `fd` isn't an assimilated ELF executable
|
|
|
|
* @raise ENOSYS on Windows, XNU, OpenBSD, NetBSD, and Metal
|
|
|
|
*/
|
|
|
|
int fexecve(int fd, char *const argv[], char *const envp[]) {
|
2023-02-24 19:48:24 +00:00
|
|
|
int rc = 0;
|
2022-10-02 15:30:39 +00:00
|
|
|
if (!argv || !envp ||
|
|
|
|
(IsAsan() &&
|
|
|
|
(!__asan_is_valid_strlist(argv) || !__asan_is_valid_strlist(envp)))) {
|
|
|
|
rc = efault();
|
|
|
|
} else {
|
2022-11-02 05:36:03 +00:00
|
|
|
STRACE("fexecve(%d, %s, %s) → ...", fd, DescribeStringList(argv),
|
|
|
|
DescribeStringList(envp));
|
2023-03-30 01:16:46 +00:00
|
|
|
int savedErr = 0;
|
2023-02-24 19:48:24 +00:00
|
|
|
do {
|
|
|
|
if (!IsLinux() && !IsFreebsd()) {
|
|
|
|
rc = enosys();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!__isfdkind(fd, kFdZip)) {
|
|
|
|
bool memfdReq;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
BEGIN_CANCELATION_POINT;
|
2023-02-24 19:48:24 +00:00
|
|
|
BLOCK_SIGNALS;
|
|
|
|
strace_enabled(-1);
|
|
|
|
memfdReq = ((rc = fcntl(fd, F_GETFD)) != -1) && (rc & FD_CLOEXEC) &&
|
|
|
|
IsAPEFd(fd);
|
|
|
|
strace_enabled(+1);
|
|
|
|
ALLOW_SIGNALS;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
END_CANCELATION_POINT;
|
2023-02-24 19:48:24 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
break;
|
|
|
|
} else if (!memfdReq) {
|
2023-03-30 01:16:46 +00:00
|
|
|
fexecve_impl(fd, argv, envp);
|
2023-02-24 19:48:24 +00:00
|
|
|
if (errno != ENOEXEC) {
|
|
|
|
break;
|
|
|
|
}
|
2023-03-30 01:16:46 +00:00
|
|
|
savedErr = ENOEXEC;
|
2023-02-24 19:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int newfd;
|
2023-03-30 01:16:46 +00:00
|
|
|
char *path = alloca(PATH_MAX);
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
BEGIN_CANCELATION_POINT;
|
2023-02-23 02:58:23 +00:00
|
|
|
BLOCK_SIGNALS;
|
|
|
|
strace_enabled(-1);
|
2023-03-30 01:16:46 +00:00
|
|
|
newfd = fd_to_mem_fd(fd, path);
|
2023-02-23 02:58:23 +00:00
|
|
|
strace_enabled(+1);
|
|
|
|
ALLOW_SIGNALS;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
END_CANCELATION_POINT;
|
2023-02-24 19:48:24 +00:00
|
|
|
if (newfd == -1) {
|
|
|
|
break;
|
2023-02-23 02:58:23 +00:00
|
|
|
}
|
2023-03-30 01:16:46 +00:00
|
|
|
size_t numenvs;
|
|
|
|
for (numenvs = 0; envp[numenvs];) ++numenvs;
|
2023-09-02 03:49:13 +00:00
|
|
|
// const size_t desenvs = min(500, max(numenvs + 1, 2));
|
2023-03-30 01:16:46 +00:00
|
|
|
static _Thread_local char *envs[500];
|
|
|
|
memcpy(envs, envp, numenvs * sizeof(char *));
|
|
|
|
envs[numenvs] = path;
|
|
|
|
envs[numenvs + 1] = NULL;
|
|
|
|
fexecve_impl(newfd, argv, envs);
|
|
|
|
if (!savedErr) {
|
|
|
|
savedErr = errno;
|
2023-02-24 19:48:24 +00:00
|
|
|
}
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
BEGIN_CANCELATION_POINT;
|
2023-02-24 19:48:24 +00:00
|
|
|
BLOCK_SIGNALS;
|
|
|
|
strace_enabled(-1);
|
|
|
|
close(newfd);
|
|
|
|
strace_enabled(+1);
|
|
|
|
ALLOW_SIGNALS;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
END_CANCELATION_POINT;
|
2023-02-24 19:48:24 +00:00
|
|
|
} while (0);
|
2023-03-30 01:16:46 +00:00
|
|
|
if (savedErr) {
|
|
|
|
errno = savedErr;
|
|
|
|
}
|
|
|
|
rc = -1;
|
2022-10-02 15:30:39 +00:00
|
|
|
}
|
|
|
|
STRACE("fexecve(%d) failed %d% m", fd, rc);
|
|
|
|
return rc;
|
|
|
|
}
|