transition to using functions for internal signal blocking/restoring

there are several reasons for this change. one is getting rid of the
repetition of the syscall signature all over the place. another is
sharing the constant masks without costly GOT accesses in PIC.

the main motivation, however, is accurately representing whether we
want to block signals that might be handled by the application, or all
signals.
This commit is contained in:
Rich Felker 2013-04-26 19:48:01 -04:00
parent d53c92c972
commit 2c074b0d6c
5 changed files with 57 additions and 12 deletions

View File

@ -115,6 +115,10 @@ void __acquire_ptc();
void __release_ptc();
void __inhibit_ptc();
void __block_all_sigs(void *);
void __block_app_sigs(void *);
void __restore_sigs(void *);
#define DEFAULT_STACK_SIZE 81920
#define DEFAULT_GUARD_SIZE PAGE_SIZE

44
src/signal/block.c Normal file
View File

@ -0,0 +1,44 @@
#include "pthread_impl.h"
#include "syscall.h"
#include <signal.h>
static const unsigned long all_mask[] = {
#if ULONG_MAX == 0xffffffff && _NSIG == 129
-1UL, -1UL, -1UL, -1UL
#elif ULONG_MAX == 0xffffffff
-1UL, -1UL
#else
-1UL
#endif
};
static const unsigned long app_mask[] = {
#if ULONG_MAX == 0xffffffff
#if _NSIG == 65
0x7fffffff, 0xfffffffc
#else
0x7fffffff, 0xfffffffc, -1UL, -1UL
#endif
#else
#if _NSIG == 65
0xfffffffc7fffffff
#else
0xfffffffc7fffffff, -1UL
#endif
#endif
};
void __block_all_sigs(void *set)
{
__syscall(SYS_rt_sigprocmask, SIG_BLOCK, &all_mask, set, _NSIG/8);
}
void __block_app_sigs(void *set)
{
__syscall(SYS_rt_sigprocmask, SIG_BLOCK, &app_mask, set, _NSIG/8);
}
void __restore_sigs(void *set)
{
__syscall(SYS_rt_sigprocmask, SIG_SETMASK, set, 0, _NSIG/8);
}

View File

@ -8,10 +8,10 @@ int raise(int sig)
{
int pid, tid, ret;
sigset_t set;
__syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGALL_SET, &set, _NSIG/8);
__block_app_sigs(&set);
tid = __syscall(SYS_gettid);
pid = __syscall(SYS_getpid);
ret = syscall(SYS_tgkill, pid, tid, sig);
__syscall(SYS_rt_sigprocmask, SIG_SETMASK, &set, 0, _NSIG/8);
__restore_sigs(&set);
return ret;
}

View File

@ -2,10 +2,10 @@
#include <signal.h>
#include <stdlib.h>
#include "syscall.h"
#include "pthread_impl.h"
_Noreturn void siglongjmp(sigjmp_buf buf, int ret)
{
if (buf->__fl) __syscall(SYS_rt_sigprocmask, SIG_SETMASK,
buf->__ss, 0, _NSIG/8);
if (buf->__fl) __restore_sigs(buf->__ss);
longjmp(buf->__jb, ret);
}

View File

@ -35,7 +35,7 @@ _Noreturn void pthread_exit(void *result)
* This is important to ensure that dynamically allocated TLS
* is not under-allocated/over-committed, and possibly for other
* reasons as well. */
__syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGALL_SET, &set, _NSIG/8);
__block_all_sigs(&set);
/* Wait to unlock the kill lock, which governs functions like
* pthread_kill which target a thread id, until signals have
@ -51,7 +51,7 @@ _Noreturn void pthread_exit(void *result)
* stdio cleanup code a consistent state. */
if (a_fetch_add(&libc.threads_minus_1, -1)==0) {
libc.threads_minus_1 = 0;
__syscall(SYS_rt_sigprocmask, SIG_SETMASK, &set, 0, _NSIG/8);
__restore_sigs(&set);
exit(0);
}
@ -94,8 +94,7 @@ static int start(void *p)
self->detached = 2;
pthread_exit(0);
}
__syscall(SYS_rt_sigprocmask, SIG_SETMASK,
self->sigmask, 0, _NSIG/8);
__restore_sigs(self->sigmask);
}
if (self->unblock_cancel)
__syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
@ -202,8 +201,7 @@ int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp
}
if (attr._a_sched) {
do_sched = new->startlock[0] = 1;
__syscall(SYS_rt_sigprocmask, SIG_BLOCK,
SIGALL_SET, new->sigmask, _NSIG/8);
__block_app_sigs(new->sigmask);
}
new->unblock_cancel = self->cancel;
new->canary = self->canary;
@ -214,8 +212,7 @@ int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp
__release_ptc();
if (do_sched) {
__syscall(SYS_rt_sigprocmask, SIG_SETMASK,
new->sigmask, 0, _NSIG/8);
__restore_sigs(new->sigmask);
}
if (ret < 0) {