2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2023-05-19 02:05:08 +00:00
|
|
|
#include "ape/sections.internal.h"
|
2021-02-08 17:19:00 +00:00
|
|
|
#include "libc/assert.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/internal.h"
|
2022-05-27 20:25:46 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2023-07-10 11:29:46 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/struct/sigaction.internal.h"
|
|
|
|
#include "libc/calls/struct/siginfo.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
|
|
|
#include "libc/calls/syscall_support-sysv.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/ucontext.h"
|
|
|
|
#include "libc/dce.h"
|
2022-05-12 13:43:59 +00:00
|
|
|
#include "libc/intrin/describeflags.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/intrin/dll.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/limits.h"
|
2022-04-28 16:42:36 +00:00
|
|
|
#include "libc/log/backtrace.internal.h"
|
|
|
|
#include "libc/log/log.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/mem/mem.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
2023-10-10 03:18:48 +00:00
|
|
|
#include "libc/runtime/syslib.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-11-07 10:22:09 +00:00
|
|
|
#include "libc/sysv/consts/limits.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/sa.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
|
|
|
#include "libc/sysv/errfuns.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/thread/posixthread.internal.h"
|
|
|
|
#include "libc/thread/thread.h"
|
2023-09-12 18:38:34 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-04-18 18:34:59 +00:00
|
|
|
#define SA_RESTORER 0x04000000
|
|
|
|
|
2021-02-05 17:44:54 +00:00
|
|
|
static void sigaction_cosmo2native(union metasigaction *sa) {
|
2022-11-07 10:22:09 +00:00
|
|
|
void *handler;
|
|
|
|
uint64_t flags;
|
|
|
|
void *restorer;
|
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
|
|
|
uint32_t masklo;
|
|
|
|
uint32_t maskhi;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!sa)
|
|
|
|
return;
|
2022-11-07 10:22:09 +00:00
|
|
|
flags = sa->cosmo.sa_flags;
|
|
|
|
handler = sa->cosmo.sa_handler;
|
|
|
|
restorer = sa->cosmo.sa_restorer;
|
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
|
|
|
masklo = sa->cosmo.sa_mask;
|
|
|
|
maskhi = sa->cosmo.sa_mask >> 32;
|
2022-11-07 10:22:09 +00:00
|
|
|
if (IsLinux()) {
|
|
|
|
sa->linux.sa_flags = flags;
|
|
|
|
sa->linux.sa_handler = handler;
|
|
|
|
sa->linux.sa_restorer = restorer;
|
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
|
|
|
sa->linux.sa_mask[0] = masklo;
|
|
|
|
sa->linux.sa_mask[1] = maskhi;
|
2023-10-10 03:18:48 +00:00
|
|
|
} else if (IsXnuSilicon()) {
|
|
|
|
sa->silicon.sa_flags = flags;
|
|
|
|
sa->silicon.sa_handler = handler;
|
|
|
|
sa->silicon.sa_mask[0] = masklo;
|
2022-11-07 10:22:09 +00:00
|
|
|
} else if (IsXnu()) {
|
|
|
|
sa->xnu_in.sa_flags = flags;
|
|
|
|
sa->xnu_in.sa_handler = handler;
|
|
|
|
sa->xnu_in.sa_restorer = restorer;
|
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
|
|
|
sa->xnu_in.sa_mask[0] = masklo;
|
2022-11-07 10:22:09 +00:00
|
|
|
} else if (IsFreebsd()) {
|
|
|
|
sa->freebsd.sa_flags = flags;
|
|
|
|
sa->freebsd.sa_handler = handler;
|
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
|
|
|
sa->freebsd.sa_mask[0] = masklo;
|
|
|
|
sa->freebsd.sa_mask[1] = maskhi;
|
|
|
|
sa->freebsd.sa_mask[2] = 0;
|
|
|
|
sa->freebsd.sa_mask[3] = 0;
|
2022-11-07 10:22:09 +00:00
|
|
|
} else if (IsOpenbsd()) {
|
|
|
|
sa->openbsd.sa_flags = flags;
|
|
|
|
sa->openbsd.sa_handler = handler;
|
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
|
|
|
sa->openbsd.sa_mask[0] = masklo;
|
2022-11-07 10:22:09 +00:00
|
|
|
} else if (IsNetbsd()) {
|
|
|
|
sa->netbsd.sa_flags = flags;
|
|
|
|
sa->netbsd.sa_handler = handler;
|
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
|
|
|
sa->netbsd.sa_mask[0] = masklo;
|
|
|
|
sa->netbsd.sa_mask[1] = maskhi;
|
|
|
|
sa->netbsd.sa_mask[2] = 0;
|
|
|
|
sa->netbsd.sa_mask[3] = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:44:54 +00:00
|
|
|
static void sigaction_native2cosmo(union metasigaction *sa) {
|
2022-11-07 10:22:09 +00:00
|
|
|
void *handler;
|
|
|
|
uint64_t flags;
|
|
|
|
void *restorer = 0;
|
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
|
|
|
uint32_t masklo;
|
|
|
|
uint32_t maskhi = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!sa)
|
|
|
|
return;
|
2022-11-07 10:22:09 +00:00
|
|
|
if (IsLinux()) {
|
|
|
|
flags = sa->linux.sa_flags;
|
|
|
|
handler = sa->linux.sa_handler;
|
|
|
|
restorer = sa->linux.sa_restorer;
|
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
|
|
|
masklo = sa->linux.sa_mask[0];
|
|
|
|
maskhi = sa->linux.sa_mask[1];
|
2023-10-10 03:18:48 +00:00
|
|
|
} else if (IsXnu()) {
|
|
|
|
flags = sa->silicon.sa_flags;
|
|
|
|
handler = sa->silicon.sa_handler;
|
|
|
|
masklo = sa->silicon.sa_mask[0];
|
2022-11-07 10:22:09 +00:00
|
|
|
} else if (IsXnu()) {
|
|
|
|
flags = sa->xnu_out.sa_flags;
|
|
|
|
handler = sa->xnu_out.sa_handler;
|
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
|
|
|
masklo = sa->xnu_out.sa_mask[0];
|
2022-11-07 10:22:09 +00:00
|
|
|
} else if (IsFreebsd()) {
|
|
|
|
flags = sa->freebsd.sa_flags;
|
|
|
|
handler = sa->freebsd.sa_handler;
|
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
|
|
|
masklo = sa->freebsd.sa_mask[0];
|
|
|
|
maskhi = sa->freebsd.sa_mask[1];
|
2022-11-07 10:22:09 +00:00
|
|
|
} else if (IsOpenbsd()) {
|
|
|
|
flags = sa->openbsd.sa_flags;
|
|
|
|
handler = sa->openbsd.sa_handler;
|
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
|
|
|
masklo = sa->openbsd.sa_mask[0];
|
2022-11-07 10:22:09 +00:00
|
|
|
} else if (IsNetbsd()) {
|
|
|
|
flags = sa->netbsd.sa_flags;
|
|
|
|
handler = sa->netbsd.sa_handler;
|
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
|
|
|
masklo = sa->netbsd.sa_mask[0];
|
|
|
|
maskhi = sa->netbsd.sa_mask[1];
|
2022-11-07 10:22:09 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-11-07 10:22:09 +00:00
|
|
|
sa->cosmo.sa_flags = flags;
|
|
|
|
sa->cosmo.sa_handler = handler;
|
|
|
|
sa->cosmo.sa_restorer = restorer;
|
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
|
|
|
sa->cosmo.sa_mask = masklo | (uint64_t)maskhi << 32;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
static int __sigaction(int sig, const struct sigaction *act,
|
|
|
|
struct sigaction *oldact) {
|
2022-11-07 10:22:09 +00:00
|
|
|
_Static_assert(
|
|
|
|
(sizeof(struct sigaction) >= sizeof(struct sigaction_linux) &&
|
|
|
|
sizeof(struct sigaction) >= sizeof(struct sigaction_xnu_in) &&
|
|
|
|
sizeof(struct sigaction) >= sizeof(struct sigaction_xnu_out) &&
|
2023-10-10 03:18:48 +00:00
|
|
|
sizeof(struct sigaction) >= sizeof(struct sigaction_silicon) &&
|
2022-11-07 10:22:09 +00:00
|
|
|
sizeof(struct sigaction) >= sizeof(struct sigaction_freebsd) &&
|
|
|
|
sizeof(struct sigaction) >= sizeof(struct sigaction_openbsd) &&
|
|
|
|
sizeof(struct sigaction) >= sizeof(struct sigaction_netbsd)),
|
|
|
|
"sigaction cosmo abi needs tuning");
|
2021-02-05 17:44:54 +00:00
|
|
|
int64_t arg4, arg5;
|
2020-06-15 14:18:57 +00:00
|
|
|
int rc, rva, oldrva;
|
2022-11-02 13:49:42 +00:00
|
|
|
sigaction_f sigenter;
|
2020-06-15 14:18:57 +00:00
|
|
|
struct sigaction *ap, copy;
|
2021-02-24 04:23:19 +00:00
|
|
|
if (IsMetal())
|
|
|
|
return enosys(); /* TODO: Signals on Metal */
|
2022-11-07 10:22:09 +00:00
|
|
|
if (!(1 <= sig && sig <= _NSIG))
|
|
|
|
return einval();
|
2021-02-03 14:22:51 +00:00
|
|
|
if (sig == SIGKILL || sig == SIGSTOP)
|
|
|
|
return einval();
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!act) {
|
|
|
|
rva = (int32_t)(intptr_t)SIG_DFL;
|
|
|
|
} else if ((intptr_t)act->sa_handler < kSigactionMinRva) {
|
|
|
|
rva = (int)(intptr_t)act->sa_handler;
|
2023-05-19 02:05:08 +00:00
|
|
|
} else if ((intptr_t)act->sa_handler >=
|
|
|
|
(intptr_t)&__executable_start + kSigactionMinRva &&
|
|
|
|
(intptr_t)act->sa_handler <
|
|
|
|
(intptr_t)&__executable_start + INT_MAX) {
|
|
|
|
rva = (int)((uintptr_t)act->sa_handler - (uintptr_t)&__executable_start);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
|
|
|
return efault();
|
|
|
|
}
|
2021-02-03 14:22:51 +00:00
|
|
|
if (__vforked && rva != (intptr_t)SIG_DFL && rva != (intptr_t)SIG_IGN) {
|
|
|
|
return einval();
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!IsWindows()) {
|
2021-02-05 17:44:54 +00:00
|
|
|
if (act) {
|
|
|
|
memcpy(©, act, sizeof(copy));
|
|
|
|
ap = ©
|
2022-11-07 10:22:09 +00:00
|
|
|
|
|
|
|
if (IsLinux()) {
|
|
|
|
if (!(ap->sa_flags & SA_RESTORER)) {
|
|
|
|
ap->sa_flags |= SA_RESTORER;
|
|
|
|
ap->sa_restorer = &__restore_rt;
|
|
|
|
}
|
2023-08-14 03:31:27 +00:00
|
|
|
if (__iswsl1()) {
|
2022-11-07 10:22:09 +00:00
|
|
|
sigenter = __sigenter_wsl;
|
|
|
|
} else {
|
|
|
|
sigenter = ap->sa_sigaction;
|
|
|
|
}
|
|
|
|
} else if (IsXnu()) {
|
2021-02-26 02:30:17 +00:00
|
|
|
ap->sa_restorer = (void *)&__sigenter_xnu;
|
2022-11-02 13:49:42 +00:00
|
|
|
sigenter = __sigenter_xnu;
|
2022-08-19 20:00:50 +00:00
|
|
|
// mitigate Rosetta signal handling strangeness
|
|
|
|
// https://github.com/jart/cosmopolitan/issues/455
|
|
|
|
ap->sa_flags |= SA_SIGINFO;
|
2023-10-10 03:18:48 +00:00
|
|
|
} else if (IsXnu()) {
|
|
|
|
sigenter = __sigenter_xnu;
|
|
|
|
ap->sa_flags |= SA_SIGINFO; // couldn't hurt
|
2021-02-05 17:44:54 +00:00
|
|
|
} else if (IsNetbsd()) {
|
2022-11-02 13:49:42 +00:00
|
|
|
sigenter = __sigenter_netbsd;
|
2021-02-26 02:30:17 +00:00
|
|
|
} else if (IsFreebsd()) {
|
2022-11-02 13:49:42 +00:00
|
|
|
sigenter = __sigenter_freebsd;
|
2021-02-26 02:30:17 +00:00
|
|
|
} else if (IsOpenbsd()) {
|
2022-11-02 13:49:42 +00:00
|
|
|
sigenter = __sigenter_openbsd;
|
2021-02-26 02:30:17 +00:00
|
|
|
} else {
|
|
|
|
return enosys();
|
2021-02-05 17:44:54 +00:00
|
|
|
}
|
2022-11-02 13:49:42 +00:00
|
|
|
if (rva < kSigactionMinRva) {
|
|
|
|
ap->sa_sigaction = (void *)(intptr_t)rva;
|
|
|
|
} else {
|
|
|
|
ap->sa_sigaction = sigenter;
|
|
|
|
}
|
2021-02-05 17:44:54 +00:00
|
|
|
sigaction_cosmo2native((union metasigaction *)ap);
|
|
|
|
} else {
|
|
|
|
ap = NULL;
|
|
|
|
}
|
|
|
|
if (IsXnu()) {
|
|
|
|
arg4 = (int64_t)(intptr_t)oldact; /* from go code */
|
|
|
|
arg5 = 0;
|
|
|
|
} else if (IsNetbsd()) {
|
2022-07-19 05:26:11 +00:00
|
|
|
/* int __sigaction_sigtramp(int signum,
|
|
|
|
const struct sigaction *nsa,
|
|
|
|
struct sigaction *osa,
|
|
|
|
const void *tramp,
|
|
|
|
int vers); */
|
2021-02-05 17:44:54 +00:00
|
|
|
if (ap) {
|
|
|
|
arg4 = (int64_t)(intptr_t)&__restore_rt_netbsd;
|
|
|
|
arg5 = 2; /* netbsd/lib/libc/arch/x86_64/sys/__sigtramp2.S */
|
2021-01-17 01:52:15 +00:00
|
|
|
} else {
|
2021-02-05 17:44:54 +00:00
|
|
|
arg4 = 0;
|
|
|
|
arg5 = 0; /* netbsd/lib/libc/arch/x86_64/sys/__sigtramp2.S */
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-05 17:44:54 +00:00
|
|
|
arg4 = 8; /* or linux whines */
|
|
|
|
arg5 = 0;
|
|
|
|
}
|
2023-10-10 03:18:48 +00:00
|
|
|
if (!IsXnuSilicon()) {
|
|
|
|
rc = sys_sigaction(sig, ap, oldact, arg4, arg5);
|
|
|
|
} else {
|
|
|
|
rc = _sysret(__syslib->__sigaction(sig, ap, oldact));
|
2024-05-08 10:47:43 +00:00
|
|
|
// xnu silicon claims to support sa_resethand but it does nothing
|
|
|
|
// this can be tested, since it clears the bit from flags as well
|
|
|
|
if (!rc && oldact &&
|
2024-05-08 11:03:51 +00:00
|
|
|
(((struct sigaction_silicon *)oldact)->sa_flags & SA_RESETHAND)) {
|
2024-05-08 10:47:43 +00:00
|
|
|
((struct sigaction_silicon *)oldact)->sa_flags |= SA_RESETHAND;
|
|
|
|
}
|
2023-10-10 03:18:48 +00:00
|
|
|
}
|
|
|
|
if (rc != -1) {
|
2021-02-05 17:44:54 +00:00
|
|
|
sigaction_native2cosmo((union metasigaction *)oldact);
|
2023-11-12 13:40:06 +00:00
|
|
|
if (oldact && //
|
|
|
|
oldact->sa_handler != SIG_DFL && //
|
|
|
|
oldact->sa_handler != SIG_IGN && //
|
|
|
|
(IsFreebsd() || IsOpenbsd() || IsNetbsd() || IsXnu())) {
|
|
|
|
oldact->sa_handler =
|
|
|
|
(sighandler_t)((uintptr_t)__executable_start + __sighandrvas[sig]);
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (oldact) {
|
2021-09-28 05:58:51 +00:00
|
|
|
bzero(oldact, sizeof(*oldact));
|
2021-01-28 23:49:15 +00:00
|
|
|
oldrva = __sighandrvas[sig];
|
2023-11-12 13:40:06 +00:00
|
|
|
oldact->sa_mask = __sighandmask[sig];
|
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
|
|
|
oldact->sa_flags = __sighandflags[sig];
|
2020-08-25 11:23:25 +00:00
|
|
|
oldact->sa_sigaction =
|
2023-05-19 02:05:08 +00:00
|
|
|
(sigaction_f)(oldrva < kSigactionMinRva
|
|
|
|
? oldrva
|
2023-11-12 13:40:06 +00:00
|
|
|
: (uintptr_t)&__executable_start + oldrva);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
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
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
if (rc != -1 && !__vforked) {
|
2020-06-15 14:18:57 +00:00
|
|
|
if (act) {
|
2021-01-28 23:49:15 +00:00
|
|
|
__sighandrvas[sig] = rva;
|
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
|
|
|
__sighandmask[sig] = act->sa_mask;
|
2022-03-25 14:11:44 +00:00
|
|
|
__sighandflags[sig] = act->sa_flags;
|
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
|
|
|
if (IsWindows() && __sig_ignored(sig)) {
|
|
|
|
__sig_delete(sig);
|
2023-05-09 08:56:56 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-07 23:18:33 +00:00
|
|
|
* Installs handler for kernel interrupt to thread, e.g.:
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
*
|
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
|
|
|
* void GotCtrlC(int sig, siginfo_t *si, void *arg) {
|
|
|
|
* ucontext_t *ctx = arg;
|
|
|
|
* }
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
* struct sigaction sa = {.sa_sigaction = GotCtrlC,
|
|
|
|
* .sa_flags = SA_RESETHAND|SA_RESTART|SA_SIGINFO};
|
|
|
|
* CHECK_NE(-1, sigaction(SIGINT, &sa, NULL));
|
|
|
|
*
|
2022-04-13 05:11:00 +00:00
|
|
|
* The following flags are supported across platforms:
|
|
|
|
*
|
|
|
|
* - `SA_SIGINFO`: Causes the `siginfo_t` and `ucontext_t` parameters to
|
2022-09-02 12:08:35 +00:00
|
|
|
* be passed. `void *ctx` actually refers to `struct ucontext *`.
|
|
|
|
* This not only gives you more information about the signal, but also
|
|
|
|
* allows your signal handler to change the CPU registers. That's
|
|
|
|
* useful for recovering from crashes. If you don't use this attribute,
|
|
|
|
* then signal delivery will go a little faster.
|
2022-04-13 05:11:00 +00:00
|
|
|
*
|
|
|
|
* - `SA_RESTART`: Enables BSD signal handling semantics. Normally i/o
|
|
|
|
* entrypoints check for pending signals to deliver. If one gets
|
|
|
|
* delivered during an i/o call, the normal behavior is to cancel the
|
|
|
|
* i/o operation and return -1 with EINTR in errno. If you use the
|
|
|
|
* `SA_RESTART` flag then that behavior changes, so that any function
|
|
|
|
* that's been annotated with @restartable will not return `EINTR` and
|
|
|
|
* will instead resume the i/o operation. This makes coding easier but
|
|
|
|
* it can be an anti-pattern if not used carefully, since poor usage
|
|
|
|
* can easily result in latency issues. It also requires one to do
|
|
|
|
* more work in signal handlers, so special care needs to be given to
|
|
|
|
* which C library functions are @asyncsignalsafe.
|
|
|
|
*
|
|
|
|
* - `SA_RESETHAND`: Causes signal handler to be single-shot. This means
|
|
|
|
* that, upon entry of delivery to a signal handler, it's reset to the
|
|
|
|
* `SIG_DFL` handler automatically. You may use the alias `SA_ONESHOT`
|
|
|
|
* for this flag, which means the same thing.
|
|
|
|
*
|
|
|
|
* - `SA_NODEFER`: Disables the reentrancy safety check on your signal
|
|
|
|
* handler. Normally that's a good thing, since for instance if your
|
|
|
|
* `SIGSEGV` signal handler happens to segfault, you're going to want
|
|
|
|
* your process to just crash rather than looping endlessly. But in
|
|
|
|
* some cases it's desirable to use `SA_NODEFER` instead, such as at
|
|
|
|
* times when you wish to `longjmp()` out of your signal handler and
|
|
|
|
* back into your program. This is only safe to do across platforms
|
|
|
|
* for non-crashing signals such as `SIGCHLD` and `SIGINT`. Crash
|
|
|
|
* handlers should use Xed instead to recover execution, because on
|
|
|
|
* Windows a `SIGSEGV` or `SIGTRAP` crash handler might happen on a
|
|
|
|
* separate stack and/or a separate thread. You may use the alias
|
|
|
|
* `SA_NOMASK` for this flag, which means the same thing.
|
|
|
|
*
|
|
|
|
* - `SA_NOCLDWAIT`: Changes `SIGCHLD` so the zombie is gone and you
|
2022-04-20 16:56:53 +00:00
|
|
|
* can't call `wait()` anymore; similar but may
|
2022-04-13 05:11:00 +00:00
|
|
|
* still deliver the SIGCHLD.
|
|
|
|
*
|
|
|
|
* - `SA_NOCLDSTOP`: Lets you set `SIGCHLD` handler that's only notified
|
|
|
|
* on exit/termination and not notified on `SIGSTOP`, `SIGTSTP`,
|
|
|
|
* `SIGTTIN`, `SIGTTOU`, or `SIGCONT`.
|
|
|
|
*
|
|
|
|
* Here's an example of the most professional way to handle signals in
|
|
|
|
* an i/o event loop. It's generally a best practice to have signal
|
|
|
|
* handlers do the fewest number of things possible. The trick is to
|
|
|
|
* have your signals work hand-in-glove with the EINTR errno. This
|
|
|
|
* obfuscates the need for having to worry about @asyncsignalsafe.
|
2022-04-12 12:20:17 +00:00
|
|
|
*
|
2022-04-17 19:25:10 +00:00
|
|
|
* static volatile bool gotctrlc;
|
2022-04-13 05:11:00 +00:00
|
|
|
*
|
2022-04-17 19:25:10 +00:00
|
|
|
* void OnCtrlC(int sig) {
|
|
|
|
* gotctrlc = true;
|
|
|
|
* }
|
2022-04-13 05:11:00 +00:00
|
|
|
*
|
2022-04-17 19:25:10 +00:00
|
|
|
* int main() {
|
|
|
|
* size_t got;
|
|
|
|
* ssize_t rc;
|
|
|
|
* char buf[1];
|
|
|
|
* struct sigaction oldint;
|
|
|
|
* struct sigaction saint = {.sa_handler = GotCtrlC};
|
|
|
|
* if (sigaction(SIGINT, &saint, &oldint) == -1) {
|
|
|
|
* perror("sigaction");
|
|
|
|
* exit(1);
|
|
|
|
* }
|
|
|
|
* for (;;) {
|
|
|
|
* rc = read(0, buf, sizeof(buf));
|
|
|
|
* if (rc == -1) {
|
|
|
|
* if (errno == EINTR) {
|
|
|
|
* if (gotctrlc) {
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* } else {
|
|
|
|
* perror("read");
|
|
|
|
* exit(2);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* if (!(got = rc)) {
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* for (;;) {
|
|
|
|
* rc = write(1, buf, got);
|
|
|
|
* if (rc != -1) {
|
|
|
|
* assert(rc == 1);
|
|
|
|
* break;
|
|
|
|
* } else if (errno != EINTR) {
|
|
|
|
* perror("write");
|
|
|
|
* exit(3);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* sigaction(SIGINT, &oldint, 0);
|
|
|
|
* }
|
2022-04-12 12:20:17 +00:00
|
|
|
*
|
|
|
|
* Please note that you can't do the above if you use SA_RESTART. Since
|
|
|
|
* the purpose of SA_RESTART is to restart i/o operations whose docs say
|
2022-04-13 05:11:00 +00:00
|
|
|
* that they're @restartable and read() is one such function. Here's
|
|
|
|
* some even better news: if you don't install any signal handlers at
|
|
|
|
* all, then your i/o calls will never be interrupted!
|
|
|
|
*
|
|
|
|
* Here's an example of the most professional way to recover from
|
|
|
|
* `SIGSEGV`, `SIGFPE`, and `SIGILL`.
|
|
|
|
*
|
|
|
|
* void ContinueOnCrash(void);
|
|
|
|
*
|
|
|
|
* void SkipOverFaultingInstruction(struct ucontext *ctx) {
|
|
|
|
* struct XedDecodedInst xedd;
|
|
|
|
* xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_LONG_64);
|
|
|
|
* xed_instruction_length_decode(&xedd, (void *)ctx->uc_mcontext.rip, 15);
|
|
|
|
* ctx->uc_mcontext.rip += xedd.length;
|
|
|
|
* }
|
|
|
|
*
|
2024-05-28 09:34:17 +00:00
|
|
|
* void OnCrash(int sig, siginfo_t *si, void *vctx) {
|
2022-09-02 12:08:35 +00:00
|
|
|
* struct ucontext *ctx = vctx;
|
2022-04-13 05:11:00 +00:00
|
|
|
* SkipOverFaultingInstruction(ctx);
|
|
|
|
* ContinueOnCrash(); // reinstall here in case *rip faults
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* void ContinueOnCrash(void) {
|
|
|
|
* struct sigaction sa = {.sa_handler = OnSigSegv,
|
|
|
|
* .sa_flags = SA_SIGINFO | SA_RESETHAND};
|
|
|
|
* sigaction(SIGSEGV, &sa, 0);
|
|
|
|
* sigaction(SIGFPE, &sa, 0);
|
|
|
|
* sigaction(SIGILL, &sa, 0);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* int main() {
|
|
|
|
* ContinueOnCrash();
|
|
|
|
* // ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* You may also edit any other CPU registers during the handler. For
|
|
|
|
* example, you can use the above technique so that division by zero
|
|
|
|
* becomes defined to a specific value of your choosing!
|
|
|
|
*
|
|
|
|
* Please note that Xed isn't needed to recover from `SIGTRAP` which can
|
|
|
|
* be raised at any time by embedding `DebugBreak()` or `asm("int3")` in
|
|
|
|
* your program code. Your signal handler will automatically skip over
|
|
|
|
* the interrupt instruction, assuming your signal handler returns.
|
|
|
|
*
|
|
|
|
* The important signals supported across all platforms are:
|
|
|
|
*
|
|
|
|
* - `SIGINT`: When you press Ctrl-C this signal gets broadcasted to
|
|
|
|
* your process session group. This is the normal way to terminate
|
|
|
|
* console applications.
|
|
|
|
*
|
|
|
|
* - `SIGQUIT`: When you press CTRL-\ this signal gets broadcasted to
|
|
|
|
* your process session group. This is the irregular way to kill an
|
|
|
|
* application in cases where maybe your `SIGINT` handler is broken
|
|
|
|
* although, Cosmopolitan Libc ShowCrashReports() should program it
|
|
|
|
* such as to attach a debugger to the process if possible, or else
|
|
|
|
* show a crash report. Also note that in New Technology you should
|
|
|
|
* press CTRL+BREAK rather than CTRL+\ to get this signal.
|
|
|
|
*
|
|
|
|
* - `SIGHUP`: This gets sent to your non-daemon processes when you
|
|
|
|
* close your terminal session.
|
|
|
|
*
|
|
|
|
* - `SIGTERM` is what the `kill` command sends by default. It's the
|
|
|
|
* choice signal for terminating daemons.
|
|
|
|
*
|
|
|
|
* - `SIGUSR1` and `SIGUSR2` can be anything you want. Their default
|
|
|
|
* action is to kill the process. By convention `SIGUSR1` is usually
|
|
|
|
* used by daemons to reload the config file.
|
|
|
|
*
|
|
|
|
* - `SIGCHLD` is sent when a process terminates and it takes a certain
|
|
|
|
* degree of UNIX mastery to address sanely.
|
|
|
|
*
|
|
|
|
* - `SIGALRM` is invoked by `setitimer()` and `alarm()`. It can be
|
|
|
|
* useful for interrupting i/o operations like `connect()`.
|
|
|
|
*
|
|
|
|
* - `SIGTRAP`: This happens when an INT3 instruction is encountered.
|
|
|
|
*
|
|
|
|
* - `SIGILL` happens on illegal instructions, e.g. `UD2`.
|
|
|
|
*
|
|
|
|
* - `SIGABRT` happens when you call `abort()`.
|
|
|
|
*
|
|
|
|
* - `SIGFPE` happens when you divide ints by zero, among other things.
|
|
|
|
*
|
|
|
|
* - `SIGSEGV` and `SIGBUS` indicate memory access errors and they have
|
|
|
|
* inconsistent semantics across platforms like FreeBSD.
|
|
|
|
*
|
|
|
|
* - `SIGWINCH` is sent when your terminal window is resized.
|
|
|
|
*
|
|
|
|
* - `SIGXCPU` and `SIGXFSZ` may be raised if you run out of resources,
|
|
|
|
* which can happen if your process, or the parent process that
|
|
|
|
* spawned your process, happened to call `setrlimit()`. Doing this is
|
|
|
|
* a wonderful idea.
|
2022-04-12 12:20:17 +00:00
|
|
|
*
|
2023-07-30 11:26:34 +00:00
|
|
|
* Signal handlers should avoid clobbering global variables like `errno`
|
|
|
|
* because most signals are asynchronous, i.e. the signal handler might
|
|
|
|
* be called at any assembly instruction. If something like a `SIGCHLD`
|
|
|
|
* handler doesn't save / restore the `errno` global when calling wait,
|
|
|
|
* then any i/o logic in the main program that checks `errno` will most
|
|
|
|
* likely break. This is rare in practice, since systems usually design
|
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
|
|
|
* signals to favor delivery from cancelation points before they block
|
2023-07-30 11:26:34 +00:00
|
|
|
* however that's not guaranteed.
|
|
|
|
*
|
2022-03-25 14:11:44 +00:00
|
|
|
* @return 0 on success or -1 w/ errno
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
* @see xsigaction() for a much better api
|
|
|
|
* @asyncsignalsafe
|
|
|
|
* @vforksafe
|
|
|
|
*/
|
2022-04-20 16:56:53 +00:00
|
|
|
int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact) {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
int rc;
|
2022-04-12 12:20:17 +00:00
|
|
|
if (sig == SIGKILL || sig == SIGSTOP) {
|
|
|
|
rc = einval();
|
|
|
|
} else {
|
|
|
|
rc = __sigaction(sig, act, oldact);
|
|
|
|
}
|
2022-06-26 01:17:31 +00:00
|
|
|
STRACE("sigaction(%G, %s, [%s]) → %d% m", sig, DescribeSigaction(0, act),
|
|
|
|
DescribeSigaction(rc, oldact), rc);
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
return rc;
|
|
|
|
}
|