2021-03-08 04:14:07 +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 2021 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. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/proc/posix_spawn.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/assert.h"
|
2023-09-12 15:58:57 +00:00
|
|
|
#include "libc/atomic.h"
|
2021-03-08 04:14:07 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2023-09-10 15:12:43 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/calls/struct/fd.internal.h"
|
2023-09-10 15:12:43 +00:00
|
|
|
#include "libc/calls/struct/rlimit.h"
|
2023-09-12 15:58:57 +00:00
|
|
|
#include "libc/calls/struct/rlimit.internal.h"
|
|
|
|
#include "libc/calls/struct/rusage.internal.h"
|
2021-03-08 04:14:07 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2022-10-12 12:26:58 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
2023-09-10 15:12:43 +00:00
|
|
|
#include "libc/calls/struct/sigset.internal.h"
|
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2023-09-12 15:58:57 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/dce.h"
|
2021-03-08 04:14:07 +00:00
|
|
|
#include "libc/errno.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
|
|
|
#include "libc/fmt/magnumstrs.internal.h"
|
|
|
|
#include "libc/intrin/asan.internal.h"
|
2023-09-12 15:58:57 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2023-09-12 08:07:51 +00:00
|
|
|
#include "libc/intrin/handlock.internal.h"
|
2023-09-12 15:58:57 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2023-06-04 17:57:11 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-10-13 22:38:31 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/mem/alloca.h"
|
2023-09-12 15:58:57 +00:00
|
|
|
#include "libc/nt/createfile.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/nt/enum/processcreationflags.h"
|
|
|
|
#include "libc/nt/enum/startf.h"
|
2023-09-12 08:07:51 +00:00
|
|
|
#include "libc/nt/files.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/struct/processinformation.h"
|
|
|
|
#include "libc/nt/struct/startupinfo.h"
|
2023-10-03 02:25:19 +00:00
|
|
|
#include "libc/proc/ntspawn.h"
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/proc/posix_spawn.h"
|
|
|
|
#include "libc/proc/posix_spawn.internal.h"
|
|
|
|
#include "libc/proc/proc.internal.h"
|
2022-10-12 04:06:27 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/sock/sock.h"
|
2023-09-10 15:12:43 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/str/str.h"
|
2023-09-12 15:58:57 +00:00
|
|
|
#include "libc/sysv/consts/at.h"
|
|
|
|
#include "libc/sysv/consts/f.h"
|
|
|
|
#include "libc/sysv/consts/fd.h"
|
2023-09-10 15:12:43 +00:00
|
|
|
#include "libc/sysv/consts/limits.h"
|
2023-08-20 09:13:02 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2023-09-10 15:12:43 +00:00
|
|
|
#include "libc/sysv/consts/ok.h"
|
2022-10-16 19:05:08 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2022-11-04 08:04:43 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2022-10-13 22:38:31 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2022-10-12 12:26:58 +00:00
|
|
|
|
2023-09-12 15:58:57 +00:00
|
|
|
#ifndef SYSDEBUG
|
|
|
|
#define read sys_read
|
|
|
|
#define write sys_write
|
|
|
|
#define close sys_close
|
|
|
|
#define pipe2 sys_pipe2
|
|
|
|
#define getgid sys_getgid
|
|
|
|
#define setgid sys_setgid
|
|
|
|
#define getuid sys_getuid
|
|
|
|
#define setuid sys_setuid
|
|
|
|
#define setsid sys_setsid
|
|
|
|
#define setpgid sys_setpgid
|
|
|
|
#define fcntl __sys_fcntl
|
|
|
|
#define wait4 __sys_wait4
|
|
|
|
#define openat __sys_openat
|
|
|
|
#define setrlimit sys_setrlimit
|
|
|
|
#define sigprocmask sys_sigprocmask
|
|
|
|
#endif
|
|
|
|
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
static atomic_bool has_vfork; // i.e. not qemu/wsl/xnu/openbsd
|
2023-09-12 15:58:57 +00:00
|
|
|
|
|
|
|
static void posix_spawn_unhand(int64_t hands[3]) {
|
2023-08-20 09:13:02 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2023-09-12 15:58:57 +00:00
|
|
|
if (hands[i] != -1) {
|
|
|
|
CloseHandle(hands[i]);
|
2023-08-20 09:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 08:07:51 +00:00
|
|
|
static void posix_spawn_inherit(int64_t hands[3], bool32 bInherit) {
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
if (hands[i] != -1) {
|
|
|
|
SetHandleInformation(hands[i], kNtHandleFlagInherit, bInherit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 09:13:02 +00:00
|
|
|
static textwindows errno_t posix_spawn_windows_impl(
|
|
|
|
int *pid, const char *path, const posix_spawn_file_actions_t *file_actions,
|
|
|
|
const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// create file descriptor work area
|
|
|
|
char stdio_kind[3] = {kFdEmpty, kFdEmpty, kFdEmpty};
|
|
|
|
intptr_t stdio_handle[3] = {-1, -1, -1};
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
if (g_fds.p[i].kind != kFdEmpty && !(g_fds.p[i].flags & O_CLOEXEC)) {
|
|
|
|
stdio_kind[i] = g_fds.p[i].kind;
|
|
|
|
stdio_handle[i] = g_fds.p[i].handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
// reserve object for tracking proces
|
|
|
|
struct Proc *proc;
|
|
|
|
__proc_lock();
|
|
|
|
proc = __proc_new();
|
|
|
|
__proc_unlock();
|
|
|
|
if (!proc) return -1;
|
2023-08-20 09:13:02 +00:00
|
|
|
|
|
|
|
// apply user file actions
|
2023-09-12 15:58:57 +00:00
|
|
|
intptr_t close_handle[3] = {-1, -1, -1};
|
2023-08-20 09:13:02 +00:00
|
|
|
if (file_actions) {
|
|
|
|
int err = 0;
|
|
|
|
for (struct _posix_faction *a = *file_actions; a && !err; a = a->next) {
|
|
|
|
switch (a->action) {
|
|
|
|
case _POSIX_SPAWN_CLOSE:
|
|
|
|
unassert(a->fildes < 3u);
|
|
|
|
stdio_kind[a->fildes] = kFdEmpty;
|
|
|
|
stdio_handle[a->fildes] = -1;
|
|
|
|
break;
|
|
|
|
case _POSIX_SPAWN_DUP2:
|
|
|
|
unassert(a->newfildes < 3u);
|
|
|
|
if (__isfdopen(a->fildes)) {
|
|
|
|
stdio_kind[a->newfildes] = g_fds.p[a->fildes].kind;
|
|
|
|
stdio_handle[a->newfildes] = g_fds.p[a->fildes].handle;
|
|
|
|
} else {
|
|
|
|
err = EBADF;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case _POSIX_SPAWN_OPEN: {
|
2023-09-12 15:58:57 +00:00
|
|
|
int64_t hand;
|
|
|
|
int e = errno;
|
|
|
|
char16_t path16[PATH_MAX];
|
|
|
|
uint32_t perm, share, disp, attr;
|
2023-08-20 09:13:02 +00:00
|
|
|
unassert(a->fildes < 3u);
|
2023-09-12 15:58:57 +00:00
|
|
|
if (__mkntpathat(AT_FDCWD, a->path, 0, path16) != -1 &&
|
|
|
|
GetNtOpenFlags(a->oflag, a->mode, //
|
|
|
|
&perm, &share, &disp, &attr) != -1 &&
|
|
|
|
(hand = CreateFile(path16, perm, share, 0, disp, attr, 0))) {
|
|
|
|
stdio_kind[a->fildes] = kFdFile;
|
|
|
|
close_handle[a->fildes] = hand;
|
|
|
|
stdio_handle[a->fildes] = hand;
|
2023-08-20 09:13:02 +00:00
|
|
|
} else {
|
|
|
|
err = errno;
|
|
|
|
errno = e;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err) {
|
2023-09-12 15:58:57 +00:00
|
|
|
posix_spawn_unhand(close_handle);
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
__proc_lock();
|
|
|
|
__proc_free(proc);
|
|
|
|
__proc_unlock();
|
2023-08-20 09:13:02 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the windows process start info
|
|
|
|
int bits;
|
|
|
|
char buf[32], *v = 0;
|
|
|
|
if (_weaken(socket)) {
|
|
|
|
for (bits = i = 0; i < 3; ++i) {
|
|
|
|
if (stdio_kind[i] == kFdSocket) {
|
|
|
|
bits |= 1 << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FormatInt32(stpcpy(buf, "__STDIO_SOCKETS="), bits);
|
|
|
|
v = buf;
|
|
|
|
}
|
|
|
|
struct NtStartupInfo startinfo = {
|
|
|
|
.cb = sizeof(struct NtStartupInfo),
|
|
|
|
.dwFlags = kNtStartfUsestdhandles,
|
|
|
|
.hStdInput = stdio_handle[0],
|
|
|
|
.hStdOutput = stdio_handle[1],
|
|
|
|
.hStdError = stdio_handle[2],
|
|
|
|
};
|
|
|
|
|
|
|
|
// figure out the flags
|
2023-09-10 15:12:43 +00:00
|
|
|
short flags = 0;
|
2023-08-20 09:13:02 +00:00
|
|
|
bool bInheritHandles = false;
|
|
|
|
uint32_t dwCreationFlags = 0;
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
bInheritHandles |= stdio_handle[i] != -1;
|
|
|
|
}
|
2023-09-10 15:12:43 +00:00
|
|
|
if (attrp && *attrp) {
|
|
|
|
flags = (*attrp)->flags;
|
2023-08-20 09:13:02 +00:00
|
|
|
if (flags & POSIX_SPAWN_SETSID) {
|
|
|
|
dwCreationFlags |= kNtDetachedProcess;
|
|
|
|
}
|
|
|
|
if (flags & POSIX_SPAWN_SETPGROUP) {
|
|
|
|
dwCreationFlags |= kNtCreateNewProcessGroup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// launch the process
|
|
|
|
int rc, e = errno;
|
|
|
|
struct NtProcessInformation procinfo;
|
|
|
|
if (!envp) envp = environ;
|
2023-09-12 08:07:51 +00:00
|
|
|
__hand_rlock();
|
|
|
|
posix_spawn_inherit(stdio_handle, true);
|
2023-08-20 09:13:02 +00:00
|
|
|
rc = ntspawn(path, argv, envp, v, 0, 0, bInheritHandles, dwCreationFlags, 0,
|
|
|
|
&startinfo, &procinfo);
|
2023-09-12 08:07:51 +00:00
|
|
|
posix_spawn_inherit(stdio_handle, false);
|
2023-09-12 15:58:57 +00:00
|
|
|
posix_spawn_unhand(close_handle);
|
2023-09-12 08:07:51 +00:00
|
|
|
__hand_runlock();
|
2023-08-20 09:13:02 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
int err = errno;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
__proc_lock();
|
|
|
|
__proc_free(proc);
|
|
|
|
__proc_unlock();
|
2023-08-20 09:13:02 +00:00
|
|
|
errno = e;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the result
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
CloseHandle(procinfo.hThread);
|
|
|
|
proc->pid = procinfo.dwProcessId;
|
|
|
|
proc->handle = procinfo.hProcess;
|
|
|
|
if (pid) *pid = proc->pid;
|
|
|
|
__proc_lock();
|
|
|
|
__proc_add(proc);
|
|
|
|
__proc_unlock();
|
2023-08-20 09:13:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-12 15:58:57 +00:00
|
|
|
static const char *DescribePid(char buf[12], int err, int *pid) {
|
|
|
|
if (err) return "n/a";
|
|
|
|
if (!pid) return "NULL";
|
|
|
|
FormatInt32(buf, *pid);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2023-08-20 09:13:02 +00:00
|
|
|
static textwindows dontinline errno_t posix_spawn_windows(
|
|
|
|
int *pid, const char *path, const posix_spawn_file_actions_t *file_actions,
|
|
|
|
const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) {
|
|
|
|
int err;
|
|
|
|
if (!path || !argv ||
|
|
|
|
(IsAsan() && (!__asan_is_valid_str(path) || //
|
|
|
|
!__asan_is_valid_strlist(argv) || //
|
|
|
|
(envp && !__asan_is_valid_strlist(envp))))) {
|
|
|
|
err = EFAULT;
|
|
|
|
} else {
|
|
|
|
err = posix_spawn_windows_impl(pid, path, file_actions, attrp, argv, envp);
|
|
|
|
}
|
|
|
|
STRACE("posix_spawn([%s], %#s, %s, %s) → %s",
|
|
|
|
DescribePid(alloca(12), err, pid), path, DescribeStringList(argv),
|
|
|
|
DescribeStringList(envp), !err ? "0" : _strerrno(err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-03-08 04:14:07 +00:00
|
|
|
/**
|
2022-10-16 19:05:08 +00:00
|
|
|
* Spawns process, the POSIX way.
|
|
|
|
*
|
2023-09-10 15:12:43 +00:00
|
|
|
* This provides superior process creation performance across systems
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
*
|
2023-08-20 09:13:02 +00:00
|
|
|
* Processes are normally spawned by calling fork() and execve(), but
|
|
|
|
* that goes slow on Windows if the caller has allocated a nontrivial
|
|
|
|
* number of memory mappings, all of which need to be copied into the
|
|
|
|
* forked child, only to be destroyed a moment later. On UNIX systems
|
|
|
|
* fork() bears a similar cost that's 100x less bad, which is copying
|
|
|
|
* the page tables. So what this implementation does is on Windows it
|
|
|
|
* calls CreateProcess() directly and on UNIX it uses vfork() if it's
|
2023-09-10 15:12:43 +00:00
|
|
|
* possible (XNU and OpenBSD don't have it). On UNIX this API has the
|
|
|
|
* benefit of avoiding the footguns of using vfork() directly because
|
|
|
|
* this implementation will ensure signal handlers can't be called in
|
|
|
|
* the child process since that'd likely corrupt the parent's memory.
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
* On systems with a real vfork() implementation, the execve() status
|
|
|
|
* code is returned by this function via shared memory; otherwise, it
|
|
|
|
* gets passed via a temporary pipe (on systems like QEmu, Blink, and
|
|
|
|
* XNU/OpenBSD) whose support is auto-detected at runtime.
|
2023-08-20 09:13:02 +00:00
|
|
|
*
|
2022-10-12 12:26:58 +00:00
|
|
|
* @param pid if non-null shall be set to child pid on success
|
|
|
|
* @param path is resolved path of program which is not `$PATH` searched
|
|
|
|
* @param file_actions specifies close(), dup2(), and open() operations
|
|
|
|
* @param attrp specifies signal masks, user ids, scheduling, etc.
|
|
|
|
* @param envp is environment variables, or `environ` if null
|
2021-03-08 04:14:07 +00:00
|
|
|
* @return 0 on success or error number on failure
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
* @raise ETXTBSY if another process has `path` open in write mode
|
|
|
|
* @raise ENOEXEC if file is executable but not a valid format
|
|
|
|
* @raise ENOMEM if remaining stack memory is insufficient
|
|
|
|
* @raise EACCES if execute permission was denied
|
2022-10-12 12:26:58 +00:00
|
|
|
* @see posix_spawnp() for `$PATH` searching
|
2023-07-24 15:31:54 +00:00
|
|
|
* @returnserrno
|
2022-10-16 19:05:08 +00:00
|
|
|
* @tlsrequired
|
2021-03-08 04:14:07 +00:00
|
|
|
*/
|
2023-07-24 15:31:54 +00:00
|
|
|
errno_t posix_spawn(int *pid, const char *path,
|
|
|
|
const posix_spawn_file_actions_t *file_actions,
|
|
|
|
const posix_spawnattr_t *attrp, char *const argv[],
|
|
|
|
char *const envp[]) {
|
2023-08-20 09:13:02 +00:00
|
|
|
if (IsWindows()) {
|
|
|
|
return posix_spawn_windows(pid, path, file_actions, attrp, argv, envp);
|
|
|
|
}
|
2023-09-12 15:58:57 +00:00
|
|
|
int pfds[2];
|
|
|
|
bool use_pipe;
|
|
|
|
volatile int status = 0;
|
2023-09-10 15:12:43 +00:00
|
|
|
sigset_t blockall, oldmask;
|
|
|
|
int child, res, cs, e = errno;
|
2023-09-12 15:58:57 +00:00
|
|
|
volatile bool can_clobber = false;
|
2023-09-10 15:12:43 +00:00
|
|
|
sigfillset(&blockall);
|
2023-09-12 15:58:57 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &blockall, &oldmask);
|
2023-09-10 15:12:43 +00:00
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
if ((use_pipe = !atomic_load_explicit(&has_vfork, memory_order_acquire))) {
|
2023-09-12 15:58:57 +00:00
|
|
|
if (pipe2(pfds, O_CLOEXEC)) {
|
|
|
|
res = errno;
|
|
|
|
goto ParentFailed;
|
|
|
|
}
|
|
|
|
}
|
2022-10-16 19:05:08 +00:00
|
|
|
if (!(child = vfork())) {
|
2023-09-12 15:58:57 +00:00
|
|
|
can_clobber = true;
|
2023-09-10 15:12:43 +00:00
|
|
|
sigset_t *childmask;
|
2023-09-12 15:58:57 +00:00
|
|
|
bool lost_cloexec = 0;
|
2023-09-10 15:12:43 +00:00
|
|
|
struct sigaction dfl = {0};
|
|
|
|
short flags = attrp && *attrp ? (*attrp)->flags : 0;
|
2023-09-12 15:58:57 +00:00
|
|
|
if (use_pipe) close(pfds[0]);
|
2023-09-10 15:12:43 +00:00
|
|
|
for (int sig = 1; sig < _NSIG; sig++) {
|
|
|
|
if (__sighandrvas[sig] != (long)SIG_DFL &&
|
|
|
|
(__sighandrvas[sig] != (long)SIG_IGN ||
|
|
|
|
((flags & POSIX_SPAWN_SETSIGDEF) &&
|
2023-09-12 15:58:57 +00:00
|
|
|
sigismember(&(*attrp)->sigdefault, sig) == 1))) {
|
2023-09-10 15:12:43 +00:00
|
|
|
sigaction(sig, &dfl, 0);
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-10 15:12:43 +00:00
|
|
|
if (flags & POSIX_SPAWN_SETSID) {
|
|
|
|
setsid();
|
|
|
|
}
|
|
|
|
if ((flags & POSIX_SPAWN_SETPGROUP) && setpgid(0, (*attrp)->pgroup)) {
|
2023-09-12 15:58:57 +00:00
|
|
|
goto ChildFailed;
|
2023-09-10 15:12:43 +00:00
|
|
|
}
|
|
|
|
if ((flags & POSIX_SPAWN_RESETIDS) && setgid(getgid())) {
|
2023-09-12 15:58:57 +00:00
|
|
|
goto ChildFailed;
|
2023-09-10 15:12:43 +00:00
|
|
|
}
|
2023-09-12 15:58:57 +00:00
|
|
|
if ((flags & POSIX_SPAWN_RESETIDS) && setuid(getuid())) {
|
|
|
|
goto ChildFailed;
|
2023-09-10 15:12:43 +00:00
|
|
|
}
|
2021-03-08 04:14:07 +00:00
|
|
|
if (file_actions) {
|
2023-09-12 15:58:57 +00:00
|
|
|
struct _posix_faction *a;
|
|
|
|
for (a = *file_actions; a; a = a->next) {
|
|
|
|
if (use_pipe && pfds[1] == a->fildes) {
|
|
|
|
int p2;
|
|
|
|
if ((p2 = dup(pfds[1])) == -1) {
|
|
|
|
goto ChildFailed;
|
|
|
|
}
|
|
|
|
lost_cloexec = true;
|
|
|
|
close(pfds[1]);
|
|
|
|
pfds[1] = p2;
|
|
|
|
}
|
|
|
|
switch (a->action) {
|
|
|
|
case _POSIX_SPAWN_CLOSE:
|
|
|
|
if (close(a->fildes)) {
|
|
|
|
goto ChildFailed;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case _POSIX_SPAWN_DUP2:
|
|
|
|
if (dup2(a->fildes, a->newfildes) == -1) {
|
|
|
|
goto ChildFailed;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case _POSIX_SPAWN_OPEN: {
|
|
|
|
int t;
|
|
|
|
if ((t = openat(AT_FDCWD, a->path, a->oflag, a->mode)) == -1) {
|
|
|
|
goto ChildFailed;
|
|
|
|
}
|
|
|
|
if (t != a->fildes) {
|
|
|
|
if (dup2(t, a->fildes) == -1) {
|
|
|
|
close(t);
|
|
|
|
goto ChildFailed;
|
|
|
|
}
|
|
|
|
if (close(t)) {
|
|
|
|
goto ChildFailed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
2023-09-10 15:12:43 +00:00
|
|
|
if (IsLinux() || IsFreebsd() || IsNetbsd()) {
|
2022-10-16 19:05:08 +00:00
|
|
|
if (flags & POSIX_SPAWN_SETSCHEDULER) {
|
2023-09-10 15:12:43 +00:00
|
|
|
if (sched_setscheduler(0, (*attrp)->schedpolicy,
|
|
|
|
&(*attrp)->schedparam) == -1) {
|
2023-09-12 15:58:57 +00:00
|
|
|
goto ChildFailed;
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-16 19:05:08 +00:00
|
|
|
if (flags & POSIX_SPAWN_SETSCHEDPARAM) {
|
2023-09-10 15:12:43 +00:00
|
|
|
if (sched_setparam(0, &(*attrp)->schedparam)) {
|
2023-09-12 15:58:57 +00:00
|
|
|
goto ChildFailed;
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-10 15:12:43 +00:00
|
|
|
if (flags & POSIX_SPAWN_SETRLIMIT) {
|
|
|
|
for (int rez = 0; rez <= ARRAYLEN((*attrp)->rlim); ++rez) {
|
|
|
|
if ((*attrp)->rlimset & (1u << rez)) {
|
|
|
|
if (setrlimit(rez, (*attrp)->rlim + rez)) {
|
2023-09-12 15:58:57 +00:00
|
|
|
goto ChildFailed;
|
2023-09-10 15:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 15:58:57 +00:00
|
|
|
if (lost_cloexec) {
|
|
|
|
fcntl(pfds[1], F_SETFD, FD_CLOEXEC);
|
|
|
|
}
|
|
|
|
if (flags & POSIX_SPAWN_SETSIGMASK) {
|
|
|
|
childmask = &(*attrp)->sigmask;
|
|
|
|
} else {
|
|
|
|
childmask = &oldmask;
|
|
|
|
}
|
|
|
|
sigprocmask(SIG_SETMASK, childmask, 0);
|
2022-10-12 04:06:27 +00:00
|
|
|
if (!envp) envp = environ;
|
2021-03-08 04:14:07 +00:00
|
|
|
execve(path, argv, envp);
|
2023-09-12 15:58:57 +00:00
|
|
|
ChildFailed:
|
|
|
|
res = errno;
|
|
|
|
if (!use_pipe) {
|
|
|
|
status = res;
|
|
|
|
} else {
|
|
|
|
write(pfds[1], &res, sizeof(res));
|
2023-09-10 15:12:43 +00:00
|
|
|
}
|
2023-09-12 15:58:57 +00:00
|
|
|
_Exit(127);
|
|
|
|
}
|
|
|
|
if (use_pipe) {
|
|
|
|
close(pfds[1]);
|
2023-09-10 15:12:43 +00:00
|
|
|
}
|
|
|
|
if (child != -1) {
|
2023-09-12 15:58:57 +00:00
|
|
|
if (!use_pipe) {
|
|
|
|
res = status;
|
|
|
|
} else {
|
|
|
|
if (can_clobber) {
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
atomic_store_explicit(&has_vfork, true, memory_order_release);
|
2023-09-12 15:58:57 +00:00
|
|
|
}
|
|
|
|
res = 0;
|
|
|
|
read(pfds[0], &res, sizeof(res));
|
|
|
|
}
|
|
|
|
if (!res) {
|
|
|
|
if (pid) *pid = child;
|
|
|
|
} else {
|
|
|
|
wait4(child, 0, 0, 0);
|
|
|
|
}
|
2022-10-12 12:26:58 +00:00
|
|
|
} else {
|
2023-09-12 15:58:57 +00:00
|
|
|
res = errno;
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
2023-09-12 15:58:57 +00:00
|
|
|
if (use_pipe) {
|
|
|
|
close(pfds[0]);
|
|
|
|
}
|
|
|
|
ParentFailed:
|
|
|
|
sigprocmask(SIG_SETMASK, &oldmask, 0);
|
|
|
|
pthread_setcancelstate(cs, 0);
|
|
|
|
errno = e;
|
|
|
|
return res;
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|