Skip to content

Commit

Permalink
[signals] hand signo back to fatal sig handler to correct output #2842
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Jan 14, 2025
1 parent bbb6781 commit bf5e78c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/lib/direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ int ncdirect_printf_aligned(ncdirect* n, int y, ncalign_e align, const char* fmt
}

static int
ncdirect_stop_minimal(void* vnc, void** altstack){
ncdirect_stop_minimal(void* vnc, void** altstack, int errret){
ncdirect* nc = vnc;
int ret = drop_signals(nc, altstack);
fbuf f = {0};
Expand Down Expand Up @@ -862,6 +862,9 @@ ncdirect_stop_minimal(void* vnc, void** altstack){
#ifndef __MINGW32__
del_curterm(cur_term);
#endif
if(errret){
ret = errret;
}
return ret;
}

Expand Down Expand Up @@ -955,7 +958,7 @@ int ncdirect_stop(ncdirect* nc){
int ret = 0;
if(nc){
void* altstack;
ret |= ncdirect_stop_minimal(nc, &altstack);
ret |= ncdirect_stop_minimal(nc, &altstack, 0);
free_terminfo_cache(&nc->tcache);
if(nc->tcache.ttyfd >= 0){
ret |= close(nc->tcache.ttyfd);
Expand Down
9 changes: 6 additions & 3 deletions src/lib/notcurses.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int reset_term_palette(const tinfo* ti, fbuf* f, unsigned touchedpalette){
// to tear down and account for internal structures. note that we do lots of
// shit here that is unsafe within a signal handler =[ FIXME.
static int
notcurses_stop_minimal(void* vnc, void** altstack){
notcurses_stop_minimal(void* vnc, void** altstack, int errret){
notcurses* nc = vnc;
int ret = 0;
ret |= drop_signals(nc, altstack);
Expand Down Expand Up @@ -170,6 +170,9 @@ notcurses_stop_minimal(void* vnc, void** altstack){
}
}
}
if(errret){
ret = errret;
}
logdebug("restored terminal, returning %d", ret);
return ret;
}
Expand Down Expand Up @@ -1390,7 +1393,7 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){
err:{
void* altstack;
logpanic("alas, you will not be going to space today.");
notcurses_stop_minimal(ret, &altstack);
notcurses_stop_minimal(ret, &altstack, -1);
fbuf_free(&ret->rstate.f);
if(ret->tcache.ttyfd >= 0 && ret->tcache.tpreserved){
(void)tcsetattr(ret->tcache.ttyfd, TCSAFLUSH, ret->tcache.tpreserved);
Expand Down Expand Up @@ -1449,7 +1452,7 @@ int notcurses_stop(notcurses* nc){
int ret = 0;
if(nc){
void* altstack;
ret |= notcurses_stop_minimal(nc, &altstack);
ret |= notcurses_stop_minimal(nc, &altstack, 0);
// if we were not using the alternate screen, our cursor's wherever we last
// wrote. move it to the furthest place to which it advanced.
if(!get_escape(&nc->tcache, ESCAPE_SMCUP)){
Expand Down
10 changes: 6 additions & 4 deletions src/lib/unixsig.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int drop_signals(void* nc, void** altstack){
// inhibited), and ensures that only one notcurses/ncdirect context is active
// at any given time.
int setup_signals(void* vnc, bool no_quit_sigs, bool no_winch_sigs,
int(*handler)(void*, void**)){
int(*handler)(void*, void**, int)){
(void)no_quit_sigs;
(void)no_winch_sigs;
(void)handler;
Expand Down Expand Up @@ -75,7 +75,9 @@ static struct sigaction old_term;
// Prepared in setup_signals(), and never changes across our lifetime.
static sigset_t wblock_signals;

static int(*fatal_callback)(void*, void**); // fatal handler callback
// fatal handler callback, takes notcurses struct, pointer to altstack,
// and return value override.
static int(*fatal_callback)(void*, void**, int);

int block_signals(sigset_t* old_blocked_signals){
if(pthread_sigmask(SIG_BLOCK, &wblock_signals, old_blocked_signals)){
Expand Down Expand Up @@ -160,7 +162,7 @@ static void
fatal_handler(int signo, siginfo_t* siginfo, void* v){
notcurses* nc = atomic_load(&signal_nc);
if(nc){
fatal_callback(nc, NULL); // fuck the alt stack save yourselves
fatal_callback(nc, NULL, signo); // fuck the alt stack save yourselves
switch(signo){
case SIGTERM: invoke_old(&old_term, signo, siginfo, v); break;
case SIGSEGV: invoke_old(&old_segv, signo, siginfo, v); break;
Expand Down Expand Up @@ -191,7 +193,7 @@ void setup_alt_sig_stack(void){
// inhibited), and ensures that only one notcurses/ncdirect context is active
// at any given time.
int setup_signals(void* vnc, bool no_quit_sigs, bool no_winch_sigs,
int(*handler)(void*, void**)){
int(*handler)(void*, void**, int)){
notcurses* nc = vnc;
void* expected = NULL;
struct sigaction sa;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/unixsig.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern "C" {
#include <signal.h>

int setup_signals(void* nc, bool no_quit_sigs, bool no_winch_sig,
int(*handler)(void*, void**));
int(*handler)(void*, void**, int));

// call at the beginning of shutdown (we don't want to run fatal signal
// handlers during shutdown!). altstack is written to be freed late.
Expand Down

0 comments on commit bf5e78c

Please sign in to comment.