Improve quality of raise(), abort(), and tkill()

This change fixes a nasty bug where SIG_IGN and SIG_DFL weren't working
as advertised on BSDs. This change also fixes the tkill() definition on
MacOS so it maps to __pthread_kill().
This commit is contained in:
Justine Tunney 2022-09-03 18:12:01 -07:00
parent c5659b93f8
commit c5c4dfcd21
12 changed files with 293 additions and 63 deletions

View file

@ -17,30 +17,29 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/sigset.h"
#include "libc/dce.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/sig.h"
/**
* Terminates program abnormally.
*
* This function first tries to trigger your SIGABRT handler. If
* there isn't one or execution resumes, then abort() terminates
* the program using an escalating variety methods of increasing
* brutality.
* This function first tries to trigger your SIGABRT handler. If the
* signal handler returns, then `signal(SIGABRT, SIG_DFL)` is called
* before SIGABRT is raised again.
*
* @asyncsignalsafe
* @noreturn
*/
privileged void abort(void) {
sigset_t sm;
sigfillset(&sm);
sigdelset(&sm, SIGABRT);
sigprocmask(SIG_SETMASK, &sm, 0);
wontreturn void abort(void) {
sigset_t m;
sigemptyset(&m);
sigaddset(&m, SIGABRT);
sigprocmask(SIG_UNBLOCK, &m, 0);
raise(SIGABRT);
__restorewintty();
_Exit(128 + SIGABRT);
signal(SIGABRT, SIG_DFL);
raise(SIGABRT);
asm("hlt");
unreachable;
}

View file

@ -65,7 +65,7 @@ void _exit(int) libcesque wontreturn;
void _Exit(int) libcesque wontreturn;
void _Exit1(int) libcesque wontreturn;
void quick_exit(int) wontreturn;
void abort(void) wontreturn noinstrument;
void abort(void) wontreturn;
int __cxa_atexit(void *, void *, void *) libcesque;
int atfork(void *, void *) libcesque;
int atexit(void (*)(void)) libcesque;