Notcurses 3.0.17
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))
 
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 89 of file unixsig.c.

89 {
90 if(pthread_sigmask(SIG_BLOCK, &wblock_signals, old_blocked_signals)){
91 return -1;
92 }
93 return 0;
94}

◆ drop_signals()

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

Definition at line 111 of file unixsig.c.

111 {
112 int ret = -1;
113 void* expected = nc;
114 if(!altstack){ // came in via signal handler
115 return 0;
116 }
117 *altstack = NULL;
118 pthread_mutex_lock(&lock);
119 if(atomic_compare_exchange_strong(&signal_nc, &expected, nc)){
120 if(handling_winch){
121 sigaction(SIGWINCH, &old_winch, NULL);
122 sigaction(SIGCONT, &old_cont, NULL);
123 handling_winch = false;
124 }
125 if(handling_fatals){
126 for(unsigned i = 0 ; i < sizeof(fatal_signals) / sizeof(*fatal_signals) ; ++i){
127 sigaction(fatal_signals[i].sig, &fatal_signals[i].oldfxn, NULL);
128 }
129 handling_fatals = false;
130 }
131 if(alt_signal_stack.ss_sp){
132 alt_signal_stack.ss_flags = SS_DISABLE;
133 if(sigaltstack(&alt_signal_stack, NULL)){
134 if(errno != EPERM){
135 fprintf(stderr, "couldn't remove alternate signal stack (%s)", strerror(errno));
136 }
137 }
138 }
139 *altstack = alt_signal_stack.ss_sp;
140 alt_signal_stack.ss_sp = NULL;
141 ret = !atomic_compare_exchange_strong(&signal_nc, &expected, NULL);
142 }
143 pthread_mutex_unlock(&lock);
144 if(ret){
145 fprintf(stderr, "signals weren't registered for %p (had %p)", nc, expected);
146 }
147 // we might not have established any handlers in setup_signals(); always
148 // return 0 here, for now...
149 return 0;
150}
return NULL
Definition termdesc.h:228
Here is the caller graph for this function:

◆ setup_alt_sig_stack()

void setup_alt_sig_stack ( void  )

Definition at line 179 of file unixsig.c.

179 {
180 pthread_mutex_lock(&lock);
181 if(alt_signal_stack.ss_sp){
182 if(sigaltstack(&alt_signal_stack, NULL)){
183 logerror("error installing alternate signal stack (%s)", strerror(errno));
184 }
185 }
186 pthread_mutex_unlock(&lock);
187}
#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 **, int)  handler 
)

Definition at line 192 of file unixsig.c.

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

96 {
97 if(pthread_sigmask(SIG_SETMASK, old_blocked_signals, NULL)){
98 return -1;
99 }
100 return 0;
101}