Make fixes and improvements

- Invent iso8601us() for faster timestamps
- Improve --strace descriptions of sigset_t
- Rebuild the Landlock Make bootstrap binary
- Introduce MODE=sysv for non-Windows builds
- Permit OFD fcntl() locks under pledge(flock)
- redbean can now protect your kernel from ddos
- Have vfork() fallback to sys_fork() not fork()
- Change kmalloc() to not die when out of memory
- Improve documentation for some termios functions
- Rewrite putenv() and friends to conform to POSIX
- Fix linenoise + strace verbosity issue on Windows
- Fix regressions in our ability to show backtraces
- Change redbean SetHeader() to no-op if value is nil
- Improve fcntl() so SQLite locks work in non-WAL mode
- Remove some unnecessary work during fork() on Windows
- Create redbean-based SSL reverse proxy for IPv4 TurfWar
- Fix ape/apeinstall.sh warning when using non-bash shells
- Add ProgramTrustedIp(), and IsTrustedIp() APIs to redbean
- Support $PWD, $UID, $GID, and $EUID in command interpreter
- Introduce experimental JTqFpD APE prefix for non-Windows builds
- Invent blackhole daemon for firewalling IP addresses via UNIX named socket
- Add ProgramTokenBucket(), AcquireToken(), and CountTokens() APIs to redbean
This commit is contained in:
Justine Tunney 2022-10-17 11:02:04 -07:00
parent 648bf6555c
commit f7ff77d865
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
209 changed files with 3818 additions and 998 deletions

View file

@ -11,6 +11,9 @@
* maximum legal range is PID_MAX + 2 (100001) through INT_MAX
*/
#define THR_SUSPENDED 1
#define THR_SYSTEM_SCOPE 2
#define UMTX_OP_WAIT 2
#define UMTX_OP_WAIT_UINT 11
#define UMTX_OP_WAIT_UINT_PRIVATE 15

View file

@ -16,8 +16,6 @@ COSMOPOLITAN_C_START_
* @fileoverview Cosmopolitan POSIX Thread Internals
*/
typedef void (*atfork_f)(void);
// LEGAL TRANSITIONS ┌──> TERMINATED
// pthread_create ─┬─> JOINABLE ─┴┬─> DETACHED ──> ZOMBIE
// └──────────────┘
@ -80,6 +78,8 @@ struct PosixThread {
struct _pthread_cleanup_buffer *cleanup;
};
typedef void (*atfork_f)(void);
extern struct PosixThread _pthread_main;
hidden extern pthread_spinlock_t _pthread_keys_lock;
hidden extern uint64_t _pthread_key_usage[(PTHREAD_KEYS_MAX + 63) / 64];

View file

@ -18,7 +18,12 @@
*/
#include "libc/thread/thread.h"
int pthread_attr_getscope(const pthread_attr_t *a, int *x) {
*x = a->__scope;
/**
* Gets contention scope attribute.
*
* @return 0 on success, or errno on error
*/
int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) {
*contentionscope = attr->__contentionscope;
return 0;
}

View file

@ -16,9 +16,34 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/thread/thread.h"
int pthread_attr_setscope(pthread_attr_t *a, int x) {
a->__scope = x;
return 0;
/**
* Sets contention scope attribute.
*
* @param contentionscope may be one of:
* - `PTHREAD_SCOPE_SYSTEM` to fight the system for resources
* - `PTHREAD_SCOPE_PROCESS` to fight familiar threads for resources
* @return 0 on success, or errno on error
* @raise ENOTSUP if `contentionscope` isn't supported on host OS
* @raise EINVAL if `contentionscope` was invalid
*/
int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) {
switch (contentionscope) {
case PTHREAD_SCOPE_SYSTEM:
attr->__contentionscope = contentionscope;
return 0;
case PTHREAD_SCOPE_PROCESS:
// Linux almost certainly doesn't support thread scoping
// FreeBSD has THR_SYSTEM_SCOPE but it's not implemented
// OpenBSD pthreads claims support but really ignores it
// NetBSD pthreads claims support, but really ignores it
// XNU sources appear to make no mention of thread scope
// WIN32 documentation says nothing about thread scoping
return ENOTSUP;
default:
return EINVAL;
}
}

View file

@ -19,10 +19,11 @@
#include "libc/assert.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *cb, int execute) {
struct PosixThread *pt = (struct PosixThread *)pthread_self();
_npassert(cb == pt->cleanup);
struct PosixThread *pt = (struct PosixThread *)__get_tls()->tib_pthread;
_unassert(cb == pt->cleanup);
pt->cleanup = cb->__prev;
if (execute) {
cb->__routine(cb->__arg);

View file

@ -21,7 +21,7 @@
void _pthread_cleanup_push(struct _pthread_cleanup_buffer *cb,
void (*routine)(void *), void *arg) {
struct PosixThread *pt = (struct PosixThread *)pthread_self();
struct PosixThread *pt = (struct PosixThread *)__get_tls()->tib_pthread;
cb->__routine = routine;
cb->__arg = arg;
cb->__prev = pt->cleanup;

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/blocksigs.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/sched-sysv.internal.h"
#include "libc/calls/state.internal.h"
@ -97,7 +98,7 @@ static int PosixThread(void *arg, int tid) {
// set long jump handler so pthread_exit can bring control back here
if (!setjmp(pt->exiter)) {
__get_tls()->tib_pthread = (pthread_t)pt;
sigprocmask(SIG_SETMASK, &pt->sigmask, 0);
_sigsetmask(pt->sigmask);
pt->rc = pt->start_routine(pt->arg);
// ensure pthread_cleanup_pop(), and pthread_exit() popped cleanup
_npassert(!pt->cleanup);
@ -325,12 +326,10 @@ static errno_t pthread_create_impl(pthread_t *thread,
errno_t pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg) {
errno_t rc;
sigset_t blocksigs, oldsigs;
__require_tls();
_pthread_zombies_decimate();
sigfillset(&blocksigs);
_npassert(!sigprocmask(SIG_SETMASK, &blocksigs, &oldsigs));
rc = pthread_create_impl(thread, attr, start_routine, arg, oldsigs);
_npassert(!sigprocmask(SIG_SETMASK, &oldsigs, 0));
BLOCK_SIGNALS;
rc = pthread_create_impl(thread, attr, start_routine, arg, _SigMask);
ALLOW_SIGNALS;
return rc;
}

View file

@ -29,6 +29,7 @@
* @raise EAGAIN if `RLIMIT_SIGPENDING` was exceeded
* @raise EINVAL if `tid` or `sig` was invalid
* @raise EPERM if permission was denied
* @asyncsignalsafe
*/
int pthread_kill(pthread_t thread, int sig) {
int rc, e = errno;

View file

@ -23,7 +23,6 @@
*
* @param type will be set to one of these on success
* - `PTHREAD_MUTEX_NORMAL`
* - `PTHREAD_MUTEX_DEFAULT`
* - `PTHREAD_MUTEX_RECURSIVE`
* - `PTHREAD_MUTEX_ERRORCHECK`
* @return 0 on success, or error on failure

View file

@ -22,6 +22,7 @@
/**
* Returns current POSIX thread.
* @asyncsignalsafe
*/
pthread_t pthread_self(void) {
pthread_t t;

View file

@ -23,6 +23,7 @@
* Examines and/or changes blocked signals on current thread.
*
* @return 0 on success, or errno on error
* @asyncsignalsafe
*/
int pthread_sigmask(int how, const sigset_t *set, sigset_t *old) {
int rc, e = errno;

View file

@ -31,6 +31,9 @@
#define PTHREAD_CANCEL_DEFERRED 0
#define PTHREAD_CANCEL_ASYNCHRONOUS 1
#define PTHREAD_SCOPE_SYSTEM 0
#define PTHREAD_SCOPE_PROCESS 1
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -89,7 +92,7 @@ typedef struct pthread_attr_s {
char __inheritsched;
int __schedparam;
int __schedpolicy;
int __scope;
int __contentionscope;
unsigned __guardsize;
unsigned __stacksize;
char *__stackaddr;