mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 14:58:30 +00:00
Make system() and popen() thread safe
This commit is contained in:
parent
997ce29ddc
commit
f52f65b2e3
19 changed files with 135 additions and 31 deletions
|
@ -66,7 +66,7 @@ static inline textwindows int __sig_is_masked(int sig) {
|
|||
}
|
||||
|
||||
textwindows int __sig_is_applicable(struct Signal *s) {
|
||||
return (s->tid <= 0 || s->tid == gettid()) && !__sig_is_masked(s->sig);
|
||||
return s->tid <= 0 || s->tid == gettid();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,13 +74,11 @@ textwindows int __sig_is_applicable(struct Signal *s) {
|
|||
* @return signal or null if empty or none unmasked
|
||||
*/
|
||||
static textwindows struct Signal *__sig_remove(void) {
|
||||
int tid;
|
||||
struct Signal *prev, *res;
|
||||
if (__sig.queue) {
|
||||
tid = gettid();
|
||||
__sig_lock();
|
||||
for (prev = 0, res = __sig.queue; res; prev = res, res = res->next) {
|
||||
if (__sig_is_applicable(res)) {
|
||||
if (__sig_is_applicable(res) && !__sig_is_masked(res->sig)) {
|
||||
if (res == __sig.queue) {
|
||||
__sig.queue = res->next;
|
||||
} else if (prev) {
|
||||
|
|
|
@ -19,8 +19,6 @@
|
|||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
static pthread_mutex_t __sig_lock_obj;
|
||||
|
||||
void(__sig_lock)(void) {
|
||||
pthread_mutex_lock(&__sig_lock_obj);
|
||||
}
|
||||
|
|
22
libc/calls/siglock_obj.c
Normal file
22
libc/calls/siglock_obj.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
pthread_mutex_t __sig_lock_obj;
|
|
@ -11,6 +11,7 @@ hidden extern bool __time_critical;
|
|||
hidden extern unsigned __sighandrvas[NSIG];
|
||||
hidden extern unsigned __sighandflags[NSIG];
|
||||
hidden extern pthread_mutex_t __fds_lock_obj;
|
||||
hidden extern pthread_mutex_t __sig_lock_obj;
|
||||
hidden extern const struct NtSecurityAttributes kNtIsInheritable;
|
||||
|
||||
void __fds_lock(void);
|
||||
|
|
22
libc/intrin/fds_lock_obj.c
Normal file
22
libc/intrin/fds_lock_obj.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
pthread_mutex_t __fds_lock_obj;
|
|
@ -18,4 +18,5 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/stdio/fflush.internal.h"
|
||||
|
||||
pthread_mutex_t __fflush_lock_obj;
|
||||
struct StdioFlush __fflush;
|
|
@ -30,7 +30,6 @@
|
|||
STATIC_YOINK("_init_g_fds");
|
||||
|
||||
struct Fds g_fds;
|
||||
pthread_mutex_t __fds_lock_obj;
|
||||
|
||||
static textwindows dontinline void SetupWinStd(struct Fds *fds, int i, int x) {
|
||||
int64_t h;
|
||||
|
|
|
@ -21,19 +21,23 @@
|
|||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/syscall-nt.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
int _fork(uint32_t dwCreationFlags) {
|
||||
axdx_t ad;
|
||||
sigset_t old, all;
|
||||
int ax, dx, parent;
|
||||
int ax, dx, parent, parent_tid = 0;
|
||||
if (_weaken(_pthread_atfork)) {
|
||||
parent_tid = gettid();
|
||||
}
|
||||
if (!IsWindows()) {
|
||||
sigfillset(&all);
|
||||
sys_sigprocmask(SIG_BLOCK, &all, &old);
|
||||
|
@ -61,6 +65,9 @@ int _fork(uint32_t dwCreationFlags) {
|
|||
IsLinux() ? dx : sys_gettid(),
|
||||
memory_order_relaxed);
|
||||
}
|
||||
if (_weaken(_pthread_atfork)) {
|
||||
_weaken(_pthread_atfork)(parent_tid);
|
||||
}
|
||||
if (!IsWindows()) sys_sigprocmask(SIG_SETMASK, &old, 0);
|
||||
STRACE("fork() → 0 (child of %d)", parent);
|
||||
} else {
|
||||
|
|
|
@ -18,6 +18,7 @@ struct StdioFlush {
|
|||
};
|
||||
|
||||
hidden extern struct StdioFlush __fflush;
|
||||
hidden extern pthread_mutex_t __fflush_lock_obj;
|
||||
|
||||
void __fflush_lock(void);
|
||||
void __fflush_unlock(void);
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/intrin/pushpop.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/arraylist.internal.h"
|
||||
|
@ -29,8 +28,7 @@
|
|||
#include "libc/stdio/internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
|
||||
static pthread_mutex_t __fflush_lock_obj;
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
void(__fflush_lock)(void) {
|
||||
pthread_mutex_lock(&__fflush_lock_obj);
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
* Bourne-like syntax on all platforms including Windows.
|
||||
*
|
||||
* @see pclose()
|
||||
* @threadsafe
|
||||
*/
|
||||
FILE *popen(const char *cmdline, const char *mode) {
|
||||
FILE *f;
|
||||
|
|
|
@ -20,9 +20,12 @@
|
|||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/posix_spawn.h"
|
||||
#include "libc/stdio/posix_spawn.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
static int RunFileActions(struct _posix_faction *a) {
|
||||
int t;
|
||||
|
@ -68,7 +71,7 @@ int posix_spawn(int *pid, const char *path,
|
|||
int s, child;
|
||||
sigset_t allsigs;
|
||||
struct sigaction dfl;
|
||||
if (!(child = vfork())) {
|
||||
if (!(child = _weaken(pthread_create) ? fork() : vfork())) {
|
||||
if (attrp && *attrp) {
|
||||
if ((*attrp)->flags & POSIX_SPAWN_SETPGROUP) {
|
||||
if (setpgid(0, (*attrp)->pgroup)) _Exit(127);
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
* @param cmdline is an interpreted Turing-complete command
|
||||
* @return -1 if child process couldn't be created, otherwise a wait
|
||||
* status that can be accessed using macros like WEXITSTATUS(s)
|
||||
* @threadsafe
|
||||
*/
|
||||
int system(const char *cmdline) {
|
||||
int pid, wstatus;
|
||||
|
|
|
@ -82,6 +82,8 @@ hidden extern uint64_t _pthread_key_usage[(PTHREAD_KEYS_MAX + 63) / 64];
|
|||
hidden extern pthread_key_dtor _pthread_key_dtor[PTHREAD_KEYS_MAX];
|
||||
hidden extern _Thread_local void *_pthread_keys[PTHREAD_KEYS_MAX];
|
||||
|
||||
void _pthread_atfork(int) hidden;
|
||||
void _pthread_funlock(pthread_mutex_t *, int) hidden;
|
||||
int _pthread_reschedule(struct PosixThread *) hidden;
|
||||
int _pthread_setschedparam_freebsd(int, int, const struct sched_param *) hidden;
|
||||
void _pthread_free(struct PosixThread *) hidden;
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/sched-sysv.internal.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/sigaltstack.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
|
@ -35,6 +36,8 @@
|
|||
#include "libc/runtime/clone.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/stdio/fflush.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/sysv/consts/clone.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
@ -45,6 +48,7 @@
|
|||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "libc/thread/wait0.internal.h"
|
||||
#include "third_party/dlmalloc/dlmalloc.h"
|
||||
|
||||
STATIC_YOINK("nsync_mu_lock");
|
||||
STATIC_YOINK("nsync_mu_unlock");
|
||||
|
@ -72,6 +76,30 @@ void _pthread_free(struct PosixThread *pt) {
|
|||
free(pt);
|
||||
}
|
||||
|
||||
void _pthread_funlock(pthread_mutex_t *mu, int parent_tid) {
|
||||
if (mu->_type == PTHREAD_MUTEX_NORMAL ||
|
||||
(atomic_load_explicit(&mu->_lock, memory_order_relaxed) &&
|
||||
mu->_owner != parent_tid)) {
|
||||
atomic_store_explicit(&mu->_lock, 0, memory_order_relaxed);
|
||||
mu->_nsync = 0;
|
||||
mu->_depth = 0;
|
||||
mu->_owner = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void _pthread_atfork(int parent_tid) {
|
||||
FILE *f;
|
||||
if (_weaken(dlmalloc_atfork)) _weaken(dlmalloc_atfork)();
|
||||
_pthread_funlock(&__fds_lock_obj, parent_tid);
|
||||
_pthread_funlock(&__sig_lock_obj, parent_tid);
|
||||
_pthread_funlock(&__fflush_lock_obj, parent_tid);
|
||||
for (int i = 0; i < __fflush.handles.i; ++i) {
|
||||
if ((f = __fflush.handles.p[i])) {
|
||||
_pthread_funlock((pthread_mutex_t *)f->lock, parent_tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int PosixThread(void *arg, int tid) {
|
||||
struct PosixThread *pt = arg;
|
||||
enum PosixThreadStatus status;
|
||||
|
@ -281,10 +309,12 @@ errno_t pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|||
// set initial status
|
||||
switch (pt->attr.__detachstate) {
|
||||
case PTHREAD_CREATE_JOINABLE:
|
||||
pt->status = kPosixThreadJoinable;
|
||||
atomic_store_explicit(&pt->status, kPosixThreadJoinable,
|
||||
memory_order_relaxed);
|
||||
break;
|
||||
case PTHREAD_CREATE_DETACHED:
|
||||
pt->status = kPosixThreadDetached;
|
||||
atomic_store_explicit(&pt->status, kPosixThreadDetached,
|
||||
memory_order_relaxed);
|
||||
_pthread_zombies_add(pt);
|
||||
break;
|
||||
default:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue