Don't use uninitialized sigset_t.

If signal handlers haven't been established, then it's
useless to try to block them, especially since the sigset_t
used for blocking hasn't been initialized yet.
This commit is contained in:
Lasse Collin 2010-01-31 12:53:56 +02:00
parent 231c3c7098
commit b3cc4d8edd
1 changed files with 23 additions and 9 deletions

View File

@ -27,6 +27,11 @@ static volatile sig_atomic_t exit_signal = 0;
/// user_abort to true. /// user_abort to true.
static sigset_t hooked_signals; static sigset_t hooked_signals;
/// True once signals_init() has finished. This is used to skip blocking
/// signals (with uninitialized hooked_signals) if signals_block() and
/// signals_unblock() are called before signals_init() has been called.
static bool signals_are_initialized = false;
/// signals_block() and signals_unblock() can be called recursively. /// signals_block() and signals_unblock() can be called recursively.
static size_t signals_block_count = 0; static size_t signals_block_count = 0;
@ -91,6 +96,8 @@ signals_init(void)
message_signal_handler(); message_signal_handler();
} }
signals_are_initialized = true;
return; return;
} }
@ -99,10 +106,12 @@ signals_init(void)
extern void extern void
signals_block(void) signals_block(void)
{ {
if (signals_block_count++ == 0) { if (signals_are_initialized) {
const int saved_errno = errno; if (signals_block_count++ == 0) {
mythread_sigmask(SIG_BLOCK, &hooked_signals, NULL); const int saved_errno = errno;
errno = saved_errno; mythread_sigmask(SIG_BLOCK, &hooked_signals, NULL);
errno = saved_errno;
}
} }
return; return;
@ -112,12 +121,14 @@ signals_block(void)
extern void extern void
signals_unblock(void) signals_unblock(void)
{ {
assert(signals_block_count > 0); if (signals_are_initialized) {
assert(signals_block_count > 0);
if (--signals_block_count == 0) { if (--signals_block_count == 0) {
const int saved_errno = errno; const int saved_errno = errno;
mythread_sigmask(SIG_UNBLOCK, &hooked_signals, NULL); mythread_sigmask(SIG_UNBLOCK, &hooked_signals, NULL);
errno = saved_errno; errno = saved_errno;
}
} }
return; return;
@ -157,6 +168,9 @@ signal_handler(DWORD type lzma_attribute((unused)))
// Since we don't get a signal number which we could raise() at // Since we don't get a signal number which we could raise() at
// signals_exit() like on POSIX, just set the exit status to // signals_exit() like on POSIX, just set the exit status to
// indicate an error, so that we cannot return with zero exit status. // indicate an error, so that we cannot return with zero exit status.
//
// FIXME: Since this function runs in its own thread,
// set_exit_status() should have a mutex.
set_exit_status(E_ERROR); set_exit_status(E_ERROR);
user_abort = true; user_abort = true;
return TRUE; return TRUE;