27 #ifdef HAVE_UCONTEXT_H 31 #ifdef HAVE_VALGRIND_MEMCHECK_H 32 # include <valgrind/memcheck.h> 33 # ifndef VALGRIND_MAKE_MEM_DEFINED 34 # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n)) 36 # ifndef VALGRIND_MAKE_MEM_UNDEFINED 37 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n)) 40 # define VALGRIND_MAKE_MEM_DEFINED(p, n) 0 41 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) 0 44 #if defined(__native_client__) && defined(NACL_NEWLIB) 49 #define id_signo ruby_static_id_signo 51 #ifdef NEED_RUBY_ATOMIC_OPS 73 # define NSIG (_SIGMAX + 1) 76 static const struct signals {
165 {
"VTALRM", SIGVTALRM},
192 {
"DANGER", SIGDANGER},
195 {
"MIGRATE", SIGMIGRATE},
204 {
"RETRACT", SIGRETRACT},
215 static const char signame_prefix[3] =
"SIG";
218 signm2signo(
const char *nm)
220 const struct signals *sigs;
222 for (sigs = siglist; sigs->signm; sigs++)
223 if (strcmp(sigs->signm, nm) == 0)
231 const struct signals *sigs;
233 for (sigs = siglist; sigs->signm; sigs++)
234 if (sigs->signo == no)
256 const char *signame = signo2signm(
NUM2INT(signo));
257 if (!signame)
return Qnil;
264 return signo2signm(no);
268 rb_signo2signm(
int signo)
270 const char *
const signm = signo2signm(signo);
298 if (!
NIL_P(sig)) argnum = 2;
304 if (signo < 0 || signo >
NSIG) {
311 sig = rb_signo2signm(signo);
315 int len =
sizeof(signame_prefix);
318 if (strncmp(signm, signame_prefix, len) == 0) {
322 signo = signm2signo(signm);
325 len, signame_prefix, sig);
343 esignal_signo(
VALUE self)
362 signal(sig, SIG_DFL);
366 static RETSIGTYPE sighandler(
int sig);
367 static int signal_ignored(
int sig);
368 static void signal_enque(
int sig);
410 #define killpg(pg, sig) kill(-(pg), (sig)) 420 switch (
TYPE(argv[0])) {
437 if (strncmp(signame_prefix, s,
sizeof(signame_prefix)) == 0)
439 if ((sig = signm2signo(s)) == 0) {
459 if (argc <= 1)
return INT2FIX(0);
463 for (i=1; i<
argc; i++) {
469 const rb_pid_t
self = (
GET_THREAD() ==
GET_VM()->main_thread) ? getpid() : -1;
472 for (i=1; i<
argc; i++) {
475 if ((sig != 0) && (
self != -1) && (pid ==
self)) {
501 t = signal_ignored(sig);
503 if (t < 0 &&
kill(pid, sig))
511 else if (
kill(pid, sig) < 0) {
530 #define sighandler_t sh_t 532 #define sighandler_t ruby_sighandler_t 536 #ifdef USE_SIGALTSTACK 538 #define SIGINFO_ARG , siginfo_t *info, void *ctx 539 #define SIGINFO_CTX ctx 543 #define SIGINFO_CTX 0 546 #ifdef USE_SIGALTSTACK 554 if (size < MINSIGSTKSZ)
557 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE) 560 pagesize = (int)sysconf(_SC_PAGE_SIZE);
573 stack_t newSS, oldSS;
576 rb_bug(
"rb_register_sigaltstack: th->altstack not initialized\n");
578 newSS.ss_sp = th->altstack;
582 sigaltstack(&newSS, &oldSS);
590 struct sigaction sigact, old;
593 rb_trap_accept_nativethreads[signum] = 0;
596 sigemptyset(&sigact.sa_mask);
597 #ifdef USE_SIGALTSTACK 598 if (handler == SIG_IGN || handler == SIG_DFL) {
599 sigact.sa_handler = handler;
604 sigact.sa_flags = SA_SIGINFO;
607 sigact.sa_handler = handler;
614 if (handler == SIG_IGN)
615 sigact.sa_flags |= SA_NOCLDWAIT;
618 #if defined(SA_ONSTACK) && defined(USE_SIGALTSTACK) 623 sigact.sa_flags |= SA_ONSTACK;
628 if (sigaction(signum, &sigact, &old) < 0) {
631 if (old.sa_flags & SA_SIGINFO)
634 handler = old.sa_handler;
635 ASSUME(handler != SIG_ERR);
653 return signal(signum, handler);
657 #define ruby_signal(sig,handler) ( signal((sig),(handler))) 660 ruby_nativethread_signal(
int signum,
sighandler_t handler)
664 old = signal(signum, handler);
665 rb_trap_accept_nativethreads[signum] = 1;
672 signal_ignored(
int sig)
676 struct sigaction old;
678 if (sigaction(sig,
NULL, &old) < 0)
return FALSE;
679 func = old.sa_handler;
685 if (func == SIG_IGN)
return 1;
686 return func == sighandler ? 0 : -1;
690 signal_enque(
int sig)
699 int old_errnum =
errno;
703 #if !defined(BSD_SIGNAL) && !defined(POSIX_SIGNAL) 713 return signal_buff.size;
721 rb_disable_interrupt(
void)
723 #ifdef HAVE_PTHREAD_SIGMASK 726 pthread_sigmask(SIG_SETMASK, &mask,
NULL);
731 rb_enable_interrupt(
void)
733 #ifdef HAVE_PTHREAD_SIGMASK 736 pthread_sigmask(SIG_SETMASK, &mask,
NULL);
745 if (signal_buff.size != 0) {
747 if (signal_buff.cnt[i] > 0) {
758 #if defined SIGSEGV || defined SIGBUS || defined SIGILL || defined SIGFPE 759 static const char *received_signal;
760 # define clear_received_signal() (void)(ruby_disable_gc = 0, received_signal = 0) 762 # define clear_received_signal() ((void)0) 765 #if defined(USE_SIGALTSTACK) || defined(_WIN32) 767 # if defined __HAIKU__ 768 # define USE_UCONTEXT_REG 1 769 # elif !(defined(HAVE_UCONTEXT_H) && (defined __i386__ || defined __x86_64__ || defined __amd64__)) 770 # elif defined __linux__ 771 # define USE_UCONTEXT_REG 1 772 # elif defined __APPLE__ 773 # define USE_UCONTEXT_REG 1 774 # elif defined __FreeBSD__ 775 # define USE_UCONTEXT_REG 1 777 #if defined(HAVE_PTHREAD_SIGMASK) 778 # define ruby_sigunmask pthread_sigmask 779 #elif defined(HAVE_SIGPROCMASK) 780 # define ruby_sigunmask sigprocmask 783 reset_sigmask(
int sig)
785 #if defined(ruby_sigunmask) 789 #if defined(ruby_sigunmask) 791 sigaddset(&mask, sig);
792 if (ruby_sigunmask(SIG_UNBLOCK, &mask,
NULL)) {
798 # ifdef USE_UCONTEXT_REG 800 check_stack_overflow(
int sig,
const uintptr_t addr,
const ucontext_t *ctx)
802 const DEFINE_MCONTEXT_PTR(mctx, ctx);
803 # if defined __linux__ 805 const greg_t sp = mctx->gregs[REG_RSP];
806 const greg_t
bp = mctx->gregs[REG_RBP];
808 const greg_t sp = mctx->gregs[REG_ESP];
809 const greg_t bp = mctx->gregs[REG_EBP];
811 # elif defined __APPLE__ 812 # if defined(__LP64__) 819 # elif defined __FreeBSD__ 820 # if defined(__amd64__) 821 const __register_t sp = mctx->mc_rsp;
822 const __register_t bp = mctx->mc_rbp;
824 const __register_t sp = mctx->mc_esp;
825 const __register_t bp = mctx->mc_ebp;
827 # elif defined __HAIKU__ 828 # if defined(__amd64__) 829 const unsigned long sp = mctx->rsp;
830 const unsigned long bp = mctx->rbp;
832 const unsigned long sp = mctx->esp;
833 const unsigned long bp = mctx->ebp;
836 enum {pagesize = 4096};
839 const uintptr_t fault_page = addr / pagesize;
843 if (sp_page == fault_page || sp_page == fault_page + 1 ||
844 sp_page <= fault_page && fault_page <= bp_page) {
860 check_stack_overflow(
int sig,
const void *addr)
862 int ruby_stack_overflowed_p(
const rb_thread_t *,
const void *);
864 if (ruby_stack_overflowed_p(th, addr)) {
871 # define CHECK_STACK_OVERFLOW() check_stack_overflow(sig, 0) 873 # define FAULT_ADDRESS info->si_addr 874 # ifdef USE_UCONTEXT_REG 875 # define CHECK_STACK_OVERFLOW() check_stack_overflow(sig, (uintptr_t)FAULT_ADDRESS, ctx) 877 # define CHECK_STACK_OVERFLOW() check_stack_overflow(sig, FAULT_ADDRESS) 879 # define MESSAGE_FAULT_ADDRESS " at %p", FAULT_ADDRESS 882 # define CHECK_STACK_OVERFLOW() (void)0 884 #ifndef MESSAGE_FAULT_ADDRESS 885 # define MESSAGE_FAULT_ADDRESS 888 #if defined SIGSEGV || defined SIGBUS || defined SIGILL || defined SIGFPE 889 NOINLINE(
static void check_reserved_signal_(
const char *
name,
size_t name_len));
892 #define check_reserved_signal(name) check_reserved_signal_(name, sizeof(name)-1) 898 check_reserved_signal(
"BUS");
905 #if defined __APPLE__ || defined __linux__ 930 check_reserved_signal(
"SEGV");
940 check_reserved_signal(
"ILL");
941 #if defined __APPLE__ 949 check_reserved_signal_(
const char *
name,
size_t name_len)
955 #define NOZ(name, str) name[sizeof(str)-1] = str 956 static const char NOZ(msg1,
" received in ");
957 static const char NOZ(msg2,
" handler\n");
963 iov[0].iov_len = name_len;
964 iov[1].iov_base = (
void *)msg1;
965 iov[1].iov_len =
sizeof(msg1);
966 iov[2].iov_base = (
void *)prev;
967 iov[2].iov_len =
strlen(prev);
968 iov[3].iov_base = (
void *)msg2;
969 iov[3].iov_len =
sizeof(msg2);
970 err = writev(2, iov, 4);
972 err = write(2, name, name_len);
973 err = write(2, msg1,
sizeof(msg1));
975 err = write(2, msg2,
sizeof(msg2));
984 #if defined SIGPIPE || defined SIGSYS 986 sig_do_nothing(
int sig)
992 signal_exec(
VALUE cmd,
int safe,
int sig)
995 volatile unsigned long old_interrupt_mask = cur_th->
interrupt_mask;
1069 else if (cmd ==
Qundef) {
1073 signal_exec(cmd, safe, sig);
1078 default_handler(
int sig)
1115 func = sig_do_nothing;
1120 func = sig_do_nothing;
1132 trap_handler(
VALUE *cmd,
int sig)
1146 if (!
NIL_P(command)) {
1157 if (
memcmp(cptr,
"SYSTEM_DEFAULT", 14) == 0) {
1163 if (
memcmp(cptr,
"SIG_IGN", 7) == 0) {
1168 else if (
memcmp(cptr,
"SIG_DFL", 7) == 0) {
1170 func = default_handler(sig);
1173 else if (
memcmp(cptr,
"DEFAULT", 7) == 0) {
1178 if (
memcmp(cptr,
"IGNORE", 6) == 0) {
1183 if (
memcmp(cptr,
"EXIT", 4) == 0) {
1200 trap_signm(
VALUE vsig)
1205 switch (
TYPE(vsig)) {
1208 if (sig < 0 || sig >=
NSIG) {
1222 if (strncmp(signame_prefix, s,
sizeof(signame_prefix)) == 0)
1224 sig = signm2signo(s);
1225 if (sig == 0 && strcmp(s,
"EXIT") != 0) {
1257 if (oldfunc == SIG_IGN) oldcmd =
rb_str_new2(
"IGNORE");
1258 else if (oldfunc == SIG_DFL) oldcmd =
rb_str_new2(
"SYSTEM_DEFAULT");
1259 else if (oldfunc == sighandler) oldcmd =
rb_str_new2(
"DEFAULT");
1276 reserved_signal_p(
int signo)
1280 if (signo == SIGSEGV)
1284 if (signo == SIGBUS)
1288 if (signo == SIGILL)
1292 if (signo == SIGFPE)
1298 if (signo == SIGVTALRM)
1345 sig = trap_signm(argv[0]);
1346 if (reserved_signal_p(sig)) {
1347 const char *name = signo2signm(sig);
1360 func = trap_handler(&cmd, sig);
1367 return trap(sig, func, cmd);
1383 const struct signals *sigs;
1385 for (sigs = siglist; sigs->signm; sigs++) {
1391 #define INSTALL_SIGHANDLER(cond, signame, signum) do { \ 1392 static const char failed[] = "failed to install "signame" handler"; \ 1393 if (!(cond)) break; \ 1394 if (reserved_signal_p(signum)) rb_bug(failed); \ 1403 if (old == SIG_ERR)
return -1;
1405 if (old != SIG_DFL) {
1410 #ifndef __native_client__ 1411 # define install_sighandler(signum, handler) \ 1412 INSTALL_SIGHANDLER(install_sighandler(signum, handler), #signum, signum) 1415 #if defined(SIGCLD) || defined(SIGCHLD) 1417 init_sigchld(
int sig)
1422 if (oldfunc == SIG_ERR)
return -1;
1423 if (oldfunc != SIG_DFL && oldfunc != SIG_IGN) {
1427 GET_VM()->trap_list.cmd[sig] = 0;
1431 # ifndef __native_client__ 1432 # define init_sigchld(signum) \ 1433 INSTALL_SIGHANDLER(init_sigchld(signum), #signum, signum) 1443 if (oldfunc == sighandler) {
1504 rb_disable_interrupt();
1526 if (!ruby_enable_coredump) {
1534 # ifdef USE_SIGALTSTACK 1548 init_sigchld(SIGCLD);
1549 #elif defined(SIGCHLD) 1550 init_sigchld(SIGCHLD);
1553 rb_enable_interrupt();
#define MESSAGE_FAULT_ADDRESS
void rb_bug(const char *fmt,...)
const char * ruby_signal_name(int no)
size_t strlen(const char *)
rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)
#define GetProcPtr(obj, ptr)
VALUE rb_fstring_cstr(const char *str)
void rb_raise(VALUE exc, const char *fmt,...)
void rb_threadptr_signal_raise(rb_thread_t *th, int sig)
#define TH_JUMP_TAG(th, st)
RETSIGTYPE ruby_sigaction_t(int)
void rb_signal_exec(rb_thread_t *th, int sig)
VALUE rb_ivar_get(VALUE, ID)
#define RSTRING_GETMEM(str, ptrvar, lenvar)
void ruby_default_signal(int sig)
#define ATOMIC_PTR_EXCHANGE(var, val)
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
const char * rb_obj_classname(VALUE)
#define install_sighandler(signum, handler)
void rb_bug_context(const void *ctx, const char *fmt,...)
void rb_thread_wakeup_timer_thread(void)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
void rb_bug_errno(const char *mesg, int errno_arg)
VALUE rb_thread_current(void)
VALUE rb_check_to_integer(VALUE, const char *)
Tries to convert val into Integer.
rb_atomic_t cnt[RUBY_NSIG]
VALUE rb_f_kill(int argc, const VALUE *argv)
void rb_threadptr_check_signal(rb_thread_t *mth)
void rb_threadptr_stack_overflow(rb_thread_t *th, int crit)
void rb_sys_fail(const char *mesg)
VALUE rb_str_subseq(VALUE, long, long)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
VALUE rb_sprintf(const char *format,...)
struct rb_vm_struct::@139 trap_list
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
void rb_thread_execute_interrupts(VALUE th)
VALUE rb_ivar_set(VALUE, ID, VALUE)
rb_thread_t * ruby_current_thread
#define VALGRIND_MAKE_MEM_DEFINED(p, n)
void rb_alias(VALUE, ID, ID)
VALUE rb_call_super(int, const VALUE *)
VALUE rb_str_new_cstr(const char *)
int memcmp(const void *s1, const void *s2, size_t len)
int rb_sigaltstack_size(void)
register unsigned int len
int rb_get_next_signal(void)
rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp, rb_atomic_t newval)
#define CHECK_STACK_OVERFLOW()
#define UNLIMITED_ARGUMENTS
unsigned long interrupt_mask
VALUE rb_block_proc(void)
void rb_interrupt(void)
Raises an Interrupt exception.
void ruby_sig_finalize(void)
VALUE rb_check_string_type(VALUE)
int rb_signal_buff_size(void)
VALUE rb_eval_cmd(VALUE, VALUE, int)
#define SafeStringValue(v)
rb_execution_context_t ec
#define StringValuePtr(v)
unsigned char safe[RUBY_NSIG]
#define ruby_signal(sig, handler)
#define rb_intern_const(str)
VALUE rb_define_module(const char *name)
#define clear_received_signal()
void rb_sys_fail_str(VALUE mesg)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
void rb_threadptr_signal_exit(rb_thread_t *th)