|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
| #include <errno.h>
|
| #include <excpt.h>
|
| #include <float.h>
|
| #include <malloc.h>
|
| #include <signal.h>
|
| #include <stddef.h>
|
| #include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static __crt_state_management::dual_state_global<__crt_signal_handler_t> ctrlc_action;
|
| static __crt_state_management::dual_state_global<__crt_signal_handler_t> ctrlbreak_action;
|
| static __crt_state_management::dual_state_global<__crt_signal_handler_t> abort_action;
|
| static __crt_state_management::dual_state_global<__crt_signal_handler_t> term_action;
|
|
|
| |
| |
| |
|
|
| static bool console_ctrl_handler_installed = false;
|
|
|
| #define _SIGHUP_IGNORE 1
|
| #define _SIGQUIT_IGNORE 3
|
| #define _SIGPIPE_IGNORE 13
|
| #define _SIGIOINT_IGNORE 16
|
| #define _SIGSTOP_IGNORE 17
|
|
|
|
|
|
|
|
|
| extern "C" void __cdecl __acrt_initialize_signal_handlers(void* const encoded_nullptr)
|
| {
|
|
|
| ctrlc_action.initialize (reinterpret_cast<__crt_signal_handler_t>(encoded_nullptr));
|
| ctrlbreak_action.initialize(reinterpret_cast<__crt_signal_handler_t>(encoded_nullptr));
|
| abort_action.initialize (reinterpret_cast<__crt_signal_handler_t>(encoded_nullptr));
|
| term_action.initialize (reinterpret_cast<__crt_signal_handler_t>(encoded_nullptr));
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| static __crt_signal_handler_t* __cdecl get_global_action_nolock(int const signum) throw()
|
| {
|
| switch (signum)
|
| {
|
|
|
| case SIGINT: return &ctrlc_action.value();
|
| case SIGBREAK: return &ctrlbreak_action.value();
|
| case SIGABRT: return &abort_action.value();
|
| case SIGABRT_COMPAT: return &abort_action.value();
|
| case SIGTERM: return &term_action.value();
|
| }
|
|
|
| return nullptr;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| static __crt_signal_action_t* __cdecl siglookup(
|
| int const signum,
|
| __crt_signal_action_t* const action_table
|
| ) throw()
|
| {
|
|
|
|
|
|
|
| for (__crt_signal_action_t* p = action_table; p != action_table + __acrt_signal_action_table_count; ++p)
|
| if (p->_signal_number == signum)
|
| return p;
|
|
|
|
|
| return nullptr;
|
| }
|
|
|
|
|
|
|
|
|
| static __crt_signal_handler_t __cdecl signal_failed(int const signum) throw()
|
| {
|
| switch (signum)
|
| {
|
| case _SIGHUP_IGNORE:
|
| case _SIGQUIT_IGNORE:
|
| case _SIGPIPE_IGNORE:
|
| case _SIGIOINT_IGNORE:
|
| case _SIGSTOP_IGNORE:
|
| return SIG_ERR;
|
|
|
| default:
|
| errno = EINVAL;
|
| return SIG_ERR;
|
| }
|
| }
|
|
|
|
|
| #ifdef _UCRT_ENCLAVE_BUILD
|
|
|
| __inline static BOOL is_unsupported_signal(int const signum, __crt_signal_handler_t const sigact)
|
| {
|
| return (sigact == SIG_ACK || sigact == SIG_SGE || signum == SIGINT || signum == SIGBREAK);
|
| }
|
|
|
| __inline static BOOL is_console_signal(int const)
|
| {
|
| return FALSE;
|
| }
|
|
|
| #else
|
|
|
| __inline static BOOL is_unsupported_signal(int const, __crt_signal_handler_t const sigact)
|
| {
|
| return (sigact == SIG_ACK || sigact == SIG_SGE);
|
| }
|
|
|
| __inline static BOOL is_console_signal(int const signum)
|
| {
|
| return (signum == SIGINT || signum == SIGBREAK);
|
| }
|
|
|
| #endif
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| static BOOL WINAPI ctrlevent_capture(DWORD const ctrl_type) throw()
|
| {
|
| __crt_signal_handler_t ctrl_action = nullptr;
|
| int signal_code = 0;
|
|
|
| __acrt_lock(__acrt_signal_lock);
|
| __try
|
| {
|
| __crt_signal_handler_t* pctrl_action;
|
|
|
|
|
|
|
| if (ctrl_type == CTRL_C_EVENT)
|
| {
|
| pctrl_action = &ctrlc_action.value();
|
| ctrl_action = __crt_fast_decode_pointer(*pctrl_action);
|
| signal_code = SIGINT;
|
| }
|
| else
|
| {
|
| pctrl_action = &ctrlbreak_action.value();
|
| ctrl_action = __crt_fast_decode_pointer(*pctrl_action);
|
| signal_code = SIGBREAK;
|
| }
|
|
|
| if (ctrl_action != SIG_DFL && ctrl_action != SIG_IGN)
|
| {
|
|
|
| *pctrl_action = __crt_fast_encode_pointer(nullptr);
|
| }
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_signal_lock);
|
| }
|
|
|
|
|
|
|
| if (ctrl_action == SIG_DFL)
|
| return FALSE;
|
|
|
|
|
| if (ctrl_action != SIG_IGN)
|
| (*ctrl_action)(signal_code);
|
|
|
|
|
|
|
| return TRUE;
|
| }
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| extern "C" __crt_signal_handler_t __cdecl signal(int signum, __crt_signal_handler_t sigact)
|
| {
|
|
|
|
|
| if (is_unsupported_signal(signum, sigact))
|
| return signal_failed(signum);
|
|
|
|
|
|
|
| if (signum == SIGINT ||
|
| signum == SIGBREAK ||
|
| signum == SIGABRT ||
|
| signum == SIGABRT_COMPAT ||
|
| signum == SIGTERM)
|
| {
|
| bool set_console_ctrl_error = false;
|
| __crt_signal_handler_t old_action = nullptr;
|
|
|
| __acrt_lock(__acrt_signal_lock);
|
| __try
|
| {
|
|
|
|
|
|
|
| #pragma warning( suppress: 4127 )
|
| if (is_console_signal(signum) && !console_ctrl_handler_installed)
|
| {
|
| if (SetConsoleCtrlHandler(ctrlevent_capture, TRUE))
|
| {
|
| console_ctrl_handler_installed = true;
|
| }
|
| else
|
| {
|
| _doserrno = GetLastError();
|
| set_console_ctrl_error = true;
|
| }
|
| }
|
|
|
| __crt_signal_handler_t* const action_pointer = get_global_action_nolock(signum);
|
| if (action_pointer != nullptr)
|
| {
|
| old_action = __crt_fast_decode_pointer(*action_pointer);
|
| if (sigact != SIG_GET)
|
| *action_pointer = __crt_fast_encode_pointer(sigact);
|
| }
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_signal_lock);
|
| }
|
|
|
| if (set_console_ctrl_error)
|
| return signal_failed(signum);
|
|
|
| return old_action;
|
| }
|
|
|
|
|
|
|
|
|
|
|
| if (signum != SIGFPE && signum != SIGILL && signum != SIGSEGV)
|
| return signal_failed(signum);
|
|
|
| __acrt_ptd* const ptd = __acrt_getptd_noexit();
|
| if (ptd == nullptr)
|
| return signal_failed(signum);
|
|
|
|
|
|
|
| if (ptd->_pxcptacttab == __acrt_exception_action_table)
|
| {
|
|
|
| ptd->_pxcptacttab = static_cast<__crt_signal_action_t*>(_malloc_crt(__acrt_signal_action_table_size));
|
| if (ptd->_pxcptacttab == nullptr)
|
| return signal_failed(signum);
|
|
|
|
|
| memcpy(ptd->_pxcptacttab, __acrt_exception_action_table, __acrt_signal_action_table_size);
|
| }
|
|
|
|
|
|
|
|
|
|
|
| __crt_signal_action_t* const xcpt_action = siglookup(signum, ptd->_pxcptacttab);
|
| if (xcpt_action == nullptr)
|
| return signal_failed(signum);
|
|
|
|
|
|
|
|
|
|
|
| __crt_signal_handler_t const old_action = xcpt_action->_action;
|
|
|
|
|
|
|
|
|
| if (sigact != SIG_GET)
|
| {
|
| __crt_signal_action_t* const last = ptd->_pxcptacttab + __acrt_signal_action_table_count;
|
|
|
|
|
|
|
| for (__crt_signal_action_t* p = xcpt_action; p != last && p->_signal_number == signum; ++p)
|
| {
|
| p->_action = sigact;
|
| }
|
| }
|
|
|
| return old_action;
|
| }
|
|
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| extern "C" int __cdecl raise(int const signum)
|
| {
|
| __acrt_ptd* ptd = nullptr;
|
| int old_fpecode = 0;
|
|
|
| __crt_signal_handler_t* action_pointer = nullptr;
|
| bool action_is_global = true;
|
| switch (signum)
|
| {
|
| case SIGINT:
|
| case SIGBREAK:
|
| case SIGABRT:
|
| case SIGABRT_COMPAT:
|
| case SIGTERM:
|
| action_pointer = get_global_action_nolock(signum);;
|
| break;
|
|
|
| case SIGFPE:
|
| case SIGILL:
|
| case SIGSEGV:
|
| {
|
| ptd = __acrt_getptd_noexit();
|
| if (ptd == nullptr)
|
| return -1;
|
|
|
| __crt_signal_action_t* const local_action = siglookup(signum, ptd->_pxcptacttab);
|
| _VALIDATE_RETURN(local_action != nullptr, EINVAL, -1);
|
| action_pointer = &local_action->_action;
|
| action_is_global = false;
|
| break;
|
| }
|
| default:
|
|
|
| _VALIDATE_RETURN(("Invalid signal or error", 0), EINVAL, -1);
|
| }
|
|
|
|
|
| PEXCEPTION_POINTERS old_pxcptinfoptrs = nullptr;
|
|
|
|
|
| if (action_is_global)
|
| __acrt_lock(__acrt_signal_lock);
|
|
|
| __crt_signal_handler_t action = nullptr;
|
| bool return0 = false;
|
| __try
|
| {
|
|
|
| action = action_is_global ? __crt_fast_decode_pointer(*action_pointer) : *action_pointer;
|
|
|
|
|
| return0 = action == SIG_IGN;
|
| if (return0)
|
| __leave;
|
|
|
|
|
|
|
|
|
| if (action == SIG_DFL)
|
| {
|
|
|
|
|
| if (action_is_global)
|
| __acrt_unlock(__acrt_signal_lock);
|
|
|
| _exit(3);
|
| }
|
|
|
|
|
|
|
| if (signum == SIGFPE || signum == SIGSEGV || signum == SIGILL)
|
| {
|
| old_pxcptinfoptrs = ptd->_tpxcptinfoptrs;
|
| ptd->_tpxcptinfoptrs = nullptr;
|
|
|
|
|
| if ( signum == SIGFPE )
|
| {
|
| old_fpecode = _fpecode;
|
| _fpecode = _FPE_EXPLICITGEN;
|
| }
|
| }
|
|
|
|
|
|
|
| if (signum == SIGFPE)
|
| {
|
| __crt_signal_action_t* const first = ptd->_pxcptacttab + __acrt_signal_action_first_fpe_index;
|
| __crt_signal_action_t* const last = first + __acrt_signal_action_fpe_count;
|
|
|
| for (__crt_signal_action_t* p = first; p != last; ++p)
|
| {
|
| p->_action = SIG_DFL;
|
| }
|
| }
|
| else
|
| {
|
| *action_pointer = __crt_fast_encode_pointer(nullptr);
|
| }
|
| }
|
| __finally
|
| {
|
| if (action_is_global)
|
| __acrt_unlock(__acrt_signal_lock);
|
| }
|
|
|
| if (return0)
|
| return 0;
|
|
|
|
|
|
|
|
|
| if (signum == SIGFPE)
|
| {
|
| reinterpret_cast<void(__cdecl*)(int,int)>(action)(SIGFPE, _fpecode);
|
| }
|
| else
|
| {
|
| action(signum);
|
| }
|
|
|
|
|
|
|
|
|
| if (signum == SIGFPE || signum == SIGSEGV || signum == SIGILL)
|
| {
|
| ptd->_tpxcptinfoptrs = old_pxcptinfoptrs;
|
|
|
|
|
| if (signum == SIGFPE)
|
| _fpecode = old_fpecode;
|
| }
|
|
|
| return 0;
|
| }
|
|
|
|
|
|
|
| extern "C" __crt_signal_handler_t __cdecl __acrt_get_sigabrt_handler()
|
| {
|
| return __acrt_lock_and_call(__acrt_signal_lock, []
|
| {
|
| return __crt_fast_decode_pointer(abort_action.value());
|
| });
|
| }
|
|
|
|
|
| extern "C" int* __cdecl __fpecode()
|
| {
|
| return &__acrt_getptd()->_tfpecode;
|
| }
|
|
|
|
|
| extern "C" void** __cdecl __pxcptinfoptrs()
|
| {
|
| return reinterpret_cast<void**>(&__acrt_getptd()->_tpxcptinfoptrs);
|
| }
|
|
|