Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
unixsig.h File Reference
#include <signal.h>
Include dependency graph for unixsig.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int setup_signals (void *nc, bool no_quit_sigs, bool no_winch_sig, int(*handler)(void *, void **))
 
int drop_signals (void *nc, void **altstack)
 
int block_signals (sigset_t *old_blocked_signals)
 
int unblock_signals (const sigset_t *old_blocked_signals)
 
void setup_alt_sig_stack (void)
 

Function Documentation

◆ block_signals()

int block_signals ( sigset_t *  old_blocked_signals)

Definition at line 78 of file unixsig.c.

78 {
79 if(pthread_sigmask(SIG_BLOCK, &wblock_signals, old_blocked_signals)){
80 return -1;
81 }
82 return 0;
83}

◆ drop_signals()

int drop_signals ( void *  nc,
void **  altstack 
)

Definition at line 100 of file unixsig.c.

100 {
101 int ret = -1;
102 void* expected = nc;
103 *altstack = NULL;
104 pthread_mutex_lock(&lock);
105 if(atomic_compare_exchange_strong(&signal_nc, &expected, nc)){
106 if(handling_winch){
107 sigaction(SIGWINCH, &old_winch, NULL);
108 sigaction(SIGCONT, &old_cont, NULL);
109 handling_winch = false;
110 }
111 if(handling_fatals){
112 sigaction(SIGABRT, &old_abrt, NULL);
113 sigaction(SIGBUS, &old_bus, NULL);
114 sigaction(SIGFPE, &old_fpe, NULL);
115 sigaction(SIGILL, &old_ill, NULL);
116 sigaction(SIGINT, &old_int, NULL);
117 sigaction(SIGQUIT, &old_quit, NULL);
118 sigaction(SIGSEGV, &old_segv, NULL);
119 sigaction(SIGTERM, &old_term, NULL);
120 handling_fatals = false;
121 }
122 if(alt_signal_stack.ss_sp){
123 alt_signal_stack.ss_flags = SS_DISABLE;
124 if(sigaltstack(&alt_signal_stack, NULL)){
125 if(errno != EPERM){
126 fprintf(stderr, "couldn't remove alternate signal stack (%s)", strerror(errno));
127 }
128 }
129 }
130 *altstack = alt_signal_stack.ss_sp;
131 alt_signal_stack.ss_sp = NULL;
132 ret = !atomic_compare_exchange_strong(&signal_nc, &expected, NULL);
133 }
134 pthread_mutex_unlock(&lock);
135 if(ret){
136 fprintf(stderr, "signals weren't registered for %p (had %p)", nc, expected);
137 }
138 // we might not have established any handlers in setup_signals(); always
139 // return 0 here, for now...
140 return 0;
141}
return NULL
Definition termdesc.h:229
Here is the caller graph for this function:

◆ setup_alt_sig_stack()

void setup_alt_sig_stack ( void  )

Definition at line 175 of file unixsig.c.

175 {
176 pthread_mutex_lock(&lock);
177 if(alt_signal_stack.ss_sp){
178 if(sigaltstack(&alt_signal_stack, NULL)){
179 logerror("error installing alternate signal stack (%s)", strerror(errno));
180 }
181 }
182 pthread_mutex_unlock(&lock);
183}
#define logerror(fmt,...)
Definition logging.h:32

◆ setup_signals()

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

Definition at line 188 of file unixsig.c.

189 {
190 notcurses* nc = vnc;
191 void* expected = NULL;
192 struct sigaction sa;
193 // don't register ourselves if we don't intend to set up signal handlers
194 // we expect NULL (nothing registered), and want to register nc
195 if(!atomic_compare_exchange_strong(&signal_nc, &expected, nc)){
196 fprintf(stderr, "%p is already registered for signals (provided %p)" NL, expected, nc);
197 return -1;
198 }
199 pthread_mutex_lock(&lock);
200 if(!no_winch_sigs){
201 memset(&sa, 0, sizeof(sa));
202 sa.sa_handler = sigwinch_handler;
203 sigaddset(&sa.sa_mask, SIGWINCH);
204 sigaddset(&sa.sa_mask, SIGCONT);
205 int ret = 0;
206 ret |= sigaction(SIGWINCH, &sa, &old_winch);
207 ret |= sigaction(SIGCONT, &sa, &old_cont);
208 if(ret){
209 atomic_store(&signal_nc, NULL);
210 pthread_mutex_unlock(&lock);
211 fprintf(stderr, "error installing term signal handler (%s)" NL, strerror(errno));
212 return -1;
213 }
214 // we're not going to be restoring the old mask at exit, as who knows,
215 // they might have masked more things afterwards.
216 pthread_sigmask(SIG_BLOCK, &sa.sa_mask, NULL);
217 handling_winch = true;
218 }
219 if(!no_quit_sigs){
220 memset(&sa, 0, sizeof(sa));
221// AddressSanitizer doesn't want us to use sigaltstack(). we could force everyone
222// to export ASAN_OPTIONS=use_sigaltstack=0, or just not fuck with the alternate
223// signal stack when built with ASAN.
224#ifndef USE_ASAN
225#ifdef _SC_SIGSTKSZ
226 long minstksz = sysconf(_SC_SIGSTKSZ);
227#else
228 long minstksz = 0;
229#endif
230 if(minstksz <= 0){
231 minstksz = SIGSTKSZ * 4;
232 }
233 alt_signal_stack.ss_sp = malloc(minstksz);
234 if(alt_signal_stack.ss_sp == NULL){
235 fprintf(stderr, "warning: couldn't create %ldB alternate signal stack (%s)" NL, minstksz, strerror(errno));
236 }else{
237 alt_signal_stack.ss_size = minstksz;
238 if(sigaltstack(&alt_signal_stack, NULL)){
239 fprintf(stderr, "warning: couldn't set up %ldB alternate signal stack (%s)" NL, minstksz, strerror(errno));
240 free(alt_signal_stack.ss_sp);
241 alt_signal_stack.ss_sp = NULL;
242 alt_signal_stack.ss_size = 0;
243 }else{
244 sa.sa_flags = SA_ONSTACK;
245 }
246 }
247#endif
248 fatal_callback = handler;
249 sa.sa_sigaction = fatal_handler;
250 sigaddset(&sa.sa_mask, SIGABRT);
251 sigaddset(&sa.sa_mask, SIGBUS);
252 sigaddset(&sa.sa_mask, SIGFPE);
253 sigaddset(&sa.sa_mask, SIGILL);
254 sigaddset(&sa.sa_mask, SIGINT);
255 sigaddset(&sa.sa_mask, SIGQUIT);
256 sigaddset(&sa.sa_mask, SIGSEGV);
257 sigaddset(&sa.sa_mask, SIGTERM);
258 // don't try to handle fatal signals twice, and use our alternative stack
259 sa.sa_flags |= SA_SIGINFO | SA_RESETHAND;
260 int ret = 0;
261 ret |= sigaction(SIGABRT, &sa, &old_abrt);
262 ret |= sigaction(SIGBUS, &sa, &old_bus);
263 ret |= sigaction(SIGFPE, &sa, &old_fpe);
264 ret |= sigaction(SIGILL, &sa, &old_ill);
265 ret |= sigaction(SIGINT, &sa, &old_int);
266 ret |= sigaction(SIGQUIT, &sa, &old_quit);
267 ret |= sigaction(SIGSEGV, &sa, &old_segv);
268 ret |= sigaction(SIGTERM, &sa, &old_term);
269 if(ret){
270 atomic_store(&signal_nc, NULL);
271 pthread_mutex_unlock(&lock);
272 fprintf(stderr, "error installing fatal signal handlers (%s)" NL, strerror(errno));
273 return -1;
274 }
275 handling_fatals = true;
276 }
277 // we don't really want to go blocking SIGSEGV etc while we write, but
278 // we *do* temporarily block user-initiated signals.
279 sigaddset(&wblock_signals, SIGINT);
280 sigaddset(&wblock_signals, SIGTERM);
281 sigaddset(&wblock_signals, SIGQUIT);
282 pthread_mutex_unlock(&lock);
283 return 0;
284}
free(duplicated)
void sigwinch_handler(int signo)
Definition in.c:31
Here is the call graph for this function:
Here is the caller graph for this function:

◆ unblock_signals()

int unblock_signals ( const sigset_t *  old_blocked_signals)

Definition at line 85 of file unixsig.c.

85 {
86 if(pthread_sigmask(SIG_SETMASK, old_blocked_signals, NULL)){
87 return -1;
88 }
89 return 0;
90}