Make system() and popen() thread safe

This commit is contained in:
Justine Tunney 2022-10-13 15:38:31 -07:00
parent 997ce29ddc
commit f52f65b2e3
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
19 changed files with 135 additions and 31 deletions

View file

@ -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) {

View file

@ -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
View 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;

View file

@ -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);