musl/src/signal/sigaction.c
Rich Felker 99b8a25e94 overhaul implementation-internal signal protections
the new approach relies on the fact that the only ways to create
sigset_t objects without invoking UB are to use the sig*set()
functions, or from the masks returned by sigprocmask, sigaction, etc.
or in the ucontext_t argument to a signal handler. thus, as long as
sigfillset and sigaddset avoid adding the "protected" signals, there
is no way the application will ever obtain a sigset_t including these
bits, and thus no need to add the overhead of checking/clearing them
when sigprocmask or sigaction is called.

note that the old code actually *failed* to remove the bits from
sa_mask when sigaction was called.

the new implementations are also significantly smaller, simpler, and
faster due to ignoring the useless "GNU HURD signals" 65-1024, which
are not used and, if there's any sanity in the world, never will be
used.
2011-05-07 23:23:58 -04:00

46 lines
992 B
C

#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include "syscall.h"
#include "pthread_impl.h"
void __restore(), __restore_rt();
int __libc_sigaction(int sig, const struct sigaction *sa, struct sigaction *old)
{
struct {
void *handler;
unsigned long flags;
void (*restorer)(void);
sigset_t mask;
} ksa, kold;
long pksa=0, pkold=0;
if (sa) {
ksa.handler = sa->sa_handler;
ksa.flags = sa->sa_flags | SA_RESTORER;
ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore;
ksa.mask = sa->sa_mask;
pksa = (long)&ksa;
}
if (old) pkold = (long)&kold;
if (syscall(SYS_rt_sigaction, sig, pksa, pkold, 8))
return -1;
if (old) {
old->sa_handler = kold.handler;
old->sa_flags = kold.flags;
old->sa_mask = kold.mask;
}
return 0;
}
int __sigaction(int sig, const struct sigaction *sa, struct sigaction *old)
{
if (sig-32U < 3) {
errno = EINVAL;
return -1;
}
return __libc_sigaction(sig, sa, old);
}
weak_alias(__sigaction, sigaction);