Ruby: signal.c Source File

Ruby 3.4.5p51 (2025-07-16 revision 20cda200d3ce092571d0b5d342dadca69636cb0f)

signal.c
1/**********************************************************************
2
3 signal.c -
4
5 $Author$
6 created at: Tue Dec 20 10:13:44 JST 1994
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#include "ruby/internal/config.h"
15
16#include <errno.h>
17#include <signal.h>
18#include <stdio.h>
19
20#ifdef HAVE_UNISTD_H
21# include <unistd.h>
22#endif
23
24#ifdef HAVE_SYS_UIO_H
25# include <sys/uio.h>
26#endif
27
28#ifdef HAVE_UCONTEXT_H
29# include <ucontext.h>
30#endif
31
32#ifdef HAVE_PTHREAD_H
33# include <pthread.h>
34#endif
35
36#include "debug_counter.h"
37#include "eval_intern.h"
38#include "internal.h"
39#include "internal/error.h"
40#include "internal/eval.h"
41#include "internal/sanitizers.h"
42#include "internal/signal.h"
43#include "internal/string.h"
44#include "internal/thread.h"
45#include "ruby_atomic.h"
46#include "vm_core.h"
47#include "ractor_core.h"
48#include "ruby/internal/attr/nonstring.h"
49
50#ifdef NEED_RUBY_ATOMIC_OPS
51 rb_atomic_t
52ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)
53{
54 rb_atomic_t old = *ptr;
55 *ptr = val;
56 return old;
57}
58
59 rb_atomic_t
60ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp,
61 rb_atomic_t newval)
62{
63 rb_atomic_t old = *ptr;
64 if (old == cmp) {
65 *ptr = newval;
66 }
67 return old;
68}
69#endif
70
71#define FOREACH_SIGNAL(sig, offset) \
72 for (sig = siglist + (offset); sig < siglist + numberof(siglist); ++sig)
73enum { LONGEST_SIGNAME = 7 }; /* MIGRATE and RETRACT */
74 static const struct signals {
75 char signm[LONGEST_SIGNAME + 1];
76 int signo;
77} siglist [] = {
78 {"EXIT", 0},
79#ifdef SIGHUP
80 {"HUP", SIGHUP},
81#endif
82 {"INT", SIGINT},
83#ifdef SIGQUIT
84 {"QUIT", SIGQUIT},
85#endif
86#ifdef SIGILL
87 {"ILL", SIGILL},
88#endif
89#ifdef SIGTRAP
90 {"TRAP", SIGTRAP},
91#endif
92#ifdef SIGABRT
93 {"ABRT", SIGABRT},
94#endif
95#ifdef SIGIOT
96 {"IOT", SIGIOT},
97#endif
98#ifdef SIGEMT
99 {"EMT", SIGEMT},
100#endif
101#ifdef SIGFPE
102 {"FPE", SIGFPE},
103#endif
104#ifdef SIGKILL
105 {"KILL", SIGKILL},
106#endif
107#ifdef SIGBUS
108 {"BUS", SIGBUS},
109#endif
110#ifdef SIGSEGV
111 {"SEGV", SIGSEGV},
112#endif
113#ifdef SIGSYS
114 {"SYS", SIGSYS},
115#endif
116#ifdef SIGPIPE
117 {"PIPE", SIGPIPE},
118#endif
119#ifdef SIGALRM
120 {"ALRM", SIGALRM},
121#endif
122#ifdef SIGTERM
123 {"TERM", SIGTERM},
124#endif
125#ifdef SIGURG
126 {"URG", SIGURG},
127#endif
128#ifdef SIGSTOP
129 {"STOP", SIGSTOP},
130#endif
131#ifdef SIGTSTP
132 {"TSTP", SIGTSTP},
133#endif
134#ifdef SIGCONT
135 {"CONT", SIGCONT},
136#endif
137#ifdef RUBY_SIGCHLD
138 {"CHLD", RUBY_SIGCHLD },
139 {"CLD", RUBY_SIGCHLD },
140#endif
141#ifdef SIGTTIN
142 {"TTIN", SIGTTIN},
143#endif
144#ifdef SIGTTOU
145 {"TTOU", SIGTTOU},
146#endif
147#ifdef SIGIO
148 {"IO", SIGIO},
149#endif
150#ifdef SIGXCPU
151 {"XCPU", SIGXCPU},
152#endif
153#ifdef SIGXFSZ
154 {"XFSZ", SIGXFSZ},
155#endif
156#ifdef SIGVTALRM
157 {"VTALRM", SIGVTALRM},
158#endif
159#ifdef SIGPROF
160 {"PROF", SIGPROF},
161#endif
162#ifdef SIGWINCH
163 {"WINCH", SIGWINCH},
164#endif
165#ifdef SIGUSR1
166 {"USR1", SIGUSR1},
167#endif
168#ifdef SIGUSR2
169 {"USR2", SIGUSR2},
170#endif
171#ifdef SIGLOST
172 {"LOST", SIGLOST},
173#endif
174#ifdef SIGMSG
175 {"MSG", SIGMSG},
176#endif
177#ifdef SIGPWR
178 {"PWR", SIGPWR},
179#endif
180#ifdef SIGPOLL
181 {"POLL", SIGPOLL},
182#endif
183#ifdef SIGDANGER
184 {"DANGER", SIGDANGER},
185#endif
186#ifdef SIGMIGRATE
187 {"MIGRATE", SIGMIGRATE},
188#endif
189#ifdef SIGPRE
190 {"PRE", SIGPRE},
191#endif
192#ifdef SIGGRANT
193 {"GRANT", SIGGRANT},
194#endif
195#ifdef SIGRETRACT
196 {"RETRACT", SIGRETRACT},
197#endif
198#ifdef SIGSOUND
199 {"SOUND", SIGSOUND},
200#endif
201#ifdef SIGINFO
202 {"INFO", SIGINFO},
203#endif
204};
205
206static const char signame_prefix[] = "SIG";
207static const int signame_prefix_len = 3;
208
209static int
210signm2signo(VALUE *sig_ptr, int negative, int exit, int *prefix_ptr)
211{
212 const struct signals *sigs;
213 VALUE vsig = *sig_ptr;
214 const char *nm;
215 long len, nmlen;
216 int prefix = 0;
217
218 if (RB_SYMBOL_P(vsig)) {
219 *sig_ptr = vsig = rb_sym2str(vsig);
220 }
221 else if (!RB_TYPE_P(vsig, T_STRING)) {
222 VALUE str = rb_check_string_type(vsig);
223 if (NIL_P(str)) {
224 rb_raise(rb_eArgError, "bad signal type %s",
225 rb_obj_classname(vsig));
226 }
227 *sig_ptr = vsig = str;
228 }
229
230 rb_must_asciicompat(vsig);
231 RSTRING_GETMEM(vsig, nm, len);
232 if (memchr(nm, '\0', len)) {
233 rb_raise(rb_eArgError, "signal name with null byte");
234 }
235
236 if (len > 0 && nm[0] == '-') {
237 if (!negative)
238 rb_raise(rb_eArgError, "negative signal name: % "PRIsVALUE, vsig);
239 prefix = 1;
240 }
241 else {
242 negative = 0;
243 }
244 if (len >= prefix + signame_prefix_len) {
245 if (memcmp(nm + prefix, signame_prefix, signame_prefix_len) == 0)
246 prefix += signame_prefix_len;
247 }
248 if (len <= (long)prefix) {
249 goto unsupported;
250 }
251
252 if (prefix_ptr) *prefix_ptr = prefix;
253 nmlen = len - prefix;
254 nm += prefix;
255 if (nmlen > LONGEST_SIGNAME) goto unsupported;
256 FOREACH_SIGNAL(sigs, !exit) {
257 if (memcmp(sigs->signm, nm, nmlen) == 0 &&
258 sigs->signm[nmlen] == '\0') {
259 return negative ? -sigs->signo : sigs->signo;
260 }
261 }
262
263 unsupported:
264 if (prefix == signame_prefix_len) {
265 prefix = 0;
266 }
267 else if (prefix > signame_prefix_len) {
268 prefix -= signame_prefix_len;
269 len -= prefix;
270 vsig = rb_str_subseq(vsig, prefix, len);
271 prefix = 0;
272 }
273 else {
274 len -= prefix;
275 vsig = rb_str_subseq(vsig, prefix, len);
276 prefix = signame_prefix_len;
277 }
278 rb_raise(rb_eArgError, "unsupported signal '%.*s%"PRIsVALUE"'",
279 prefix, signame_prefix, vsig);
280 UNREACHABLE_RETURN(0);
281}
282
283static const char*
284signo2signm(int no)
285{
286 const struct signals *sigs;
287
288 FOREACH_SIGNAL(sigs, 0) {
289 if (sigs->signo == no)
290 return sigs->signm;
291 }
292 return 0;
293}
294
295/*
296 * call-seq:
297 * Signal.signame(signo) -> string or nil
298 *
299 * Convert signal number to signal name.
300 * Returns +nil+ if the signo is an invalid signal number.
301 *
302 * Signal.trap("INT") { |signo| puts Signal.signame(signo) }
303 * Process.kill("INT", 0)
304 *
305 * <em>produces:</em>
306 *
307 * INT
308 */
309static VALUE
310sig_signame(VALUE recv, VALUE signo)
311{
312 const char *signame = signo2signm(NUM2INT(signo));
313 if (!signame) return Qnil;
314 return rb_str_new_cstr(signame);
315}
316
317const char *
318ruby_signal_name(int no)
319{
320 return signo2signm(no);
321}
322
323static VALUE
324rb_signo2signm(int signo)
325{
326 const char *const signm = signo2signm(signo);
327 if (signm) {
328 return rb_sprintf("SIG%s", signm);
329 }
330 else {
331 return rb_sprintf("SIG%u", signo);
332 }
333}
334
335/*
336 * call-seq:
337 * SignalException.new(sig_name) -> signal_exception
338 * SignalException.new(sig_number [, name]) -> signal_exception
339 *
340 * Construct a new SignalException object. +sig_name+ should be a known
341 * signal name.
342 */
343
344static VALUE
345esignal_init(int argc, VALUE *argv, VALUE self)
346{
347 int argnum = 1;
348 VALUE sig = Qnil;
349 int signo;
350
351 if (argc > 0) {
352 sig = rb_check_to_integer(argv[0], "to_int");
353 if (!NIL_P(sig)) argnum = 2;
354 else sig = argv[0];
355 }
356 rb_check_arity(argc, 1, argnum);
357 if (argnum == 2) {
358 signo = NUM2INT(sig);
359 if (signo < 0 || signo > NSIG) {
360 rb_raise(rb_eArgError, "invalid signal number (%d)", signo);
361 }
362 if (argc > 1) {
363 sig = argv[1];
364 }
365 else {
366 sig = rb_signo2signm(signo);
367 }
368 }
369 else {
370 int prefix;
371 signo = signm2signo(&sig, FALSE, FALSE, &prefix);
372 if (prefix != signame_prefix_len) {
373 sig = rb_str_append(rb_str_new_cstr("SIG"), sig);
374 }
375 }
376 rb_call_super(1, &sig);
377 rb_ivar_set(self, id_signo, INT2NUM(signo));
378
379 return self;
380}
381
382/*
383 * call-seq:
384 * signal_exception.signo -> num
385 *
386 * Returns a signal number.
387 */
388
389static VALUE
390esignal_signo(VALUE self)
391{
392 return rb_ivar_get(self, id_signo);
393}
394
395/* :nodoc: */
396static VALUE
397interrupt_init(int argc, VALUE *argv, VALUE self)
398{
399 VALUE args[2];
400
401 args[0] = INT2FIX(SIGINT);
402 args[1] = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
403 return rb_call_super(2, args);
404}
405
406void rb_malloc_info_show_results(void); /* gc.c */
407#if defined(USE_SIGALTSTACK) || defined(_WIN32)
408static void reset_sigmask(int sig);
409#endif
410
411void
412ruby_default_signal(int sig)
413{
414#if USE_DEBUG_COUNTER
415 rb_debug_counter_show_results("killed by signal.");
416#endif
417 rb_malloc_info_show_results();
418
419 signal(sig, SIG_DFL);
420#if defined(USE_SIGALTSTACK) || defined(_WIN32)
421 reset_sigmask(sig);
422#endif
423 raise(sig);
424}
425
426static void sighandler(int sig);
427static int signal_ignored(int sig);
428static void signal_enque(int sig);
429
430 VALUE
431rb_f_kill(int argc, const VALUE *argv)
432{
433#ifndef HAVE_KILLPG
434#define killpg(pg, sig) kill(-(pg), (sig))
435#endif
436 int sig;
437 int i;
438 VALUE str;
439
440 rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS);
441
442 if (FIXNUM_P(argv[0])) {
443 sig = FIX2INT(argv[0]);
444 }
445 else {
446 str = argv[0];
447 sig = signm2signo(&str, TRUE, FALSE, NULL);
448 }
449
450 if (argc <= 1) return INT2FIX(0);
451
452 if (sig < 0) {
453 sig = -sig;
454 for (i=1; i<argc; i++) {
455 if (killpg(NUM2PIDT(argv[i]), sig) < 0)
456 rb_sys_fail(0);
457 }
458 }
459 else {
460 const rb_pid_t self = (GET_THREAD() == GET_VM()->ractor.main_thread) ? getpid() : -1;
461 int wakeup = 0;
462
463 for (i=1; i<argc; i++) {
464 rb_pid_t pid = NUM2PIDT(argv[i]);
465
466 if ((sig != 0) && (self != -1) && (pid == self)) {
467 int t;
468 /*
469 * When target pid is self, many caller assume signal will be
470 * delivered immediately and synchronously.
471 */
472 switch (sig) {
473 case SIGSEGV:
474#ifdef SIGBUS
475 case SIGBUS:
476#endif
477#ifdef SIGKILL
478 case SIGKILL:
479#endif
480#ifdef SIGILL
481 case SIGILL:
482#endif
483#ifdef SIGFPE
484 case SIGFPE:
485#endif
486#ifdef SIGSTOP
487 case SIGSTOP:
488#endif
489 kill(pid, sig);
490 break;
491 default:
492 t = signal_ignored(sig);
493 if (t) {
494 if (t < 0 && kill(pid, sig))
495 rb_sys_fail(0);
496 break;
497 }
498 signal_enque(sig);
499 wakeup = 1;
500 }
501 }
502 else if (kill(pid, sig) < 0) {
503 rb_sys_fail(0);
504 }
505 }
506 if (wakeup) {
507 rb_threadptr_check_signal(GET_VM()->ractor.main_thread);
508 }
509 }
510 rb_thread_execute_interrupts(rb_thread_current());
511
512 return INT2FIX(i-1);
513}
514
515static struct {
516 rb_atomic_t cnt[RUBY_NSIG];
517 rb_atomic_t size;
518} signal_buff;
519
520#define sighandler_t ruby_sighandler_t
521
522#ifdef USE_SIGALTSTACK
523typedef void ruby_sigaction_t(int, siginfo_t*, void*);
524#define SIGINFO_ARG , siginfo_t *info, void *ctx
525#define SIGINFO_CTX ctx
526#else
527typedef void ruby_sigaction_t(int);
528#define SIGINFO_ARG
529#define SIGINFO_CTX 0
530#endif
531
532#ifdef USE_SIGALTSTACK
533/* XXX: BSD_vfprintf() uses >1500B stack and x86-64 need >5KiB stack. */
534#define RUBY_SIGALTSTACK_SIZE (16*1024)
535
536static int
537rb_sigaltstack_size(void)
538{
539 int size = RUBY_SIGALTSTACK_SIZE;
540
541#ifdef MINSIGSTKSZ
542 {
543 int minsigstksz = (int)MINSIGSTKSZ;
544 if (size < minsigstksz)
545 size = minsigstksz;
546 }
547#endif
548#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
549 {
550 int pagesize;
551 pagesize = (int)sysconf(_SC_PAGE_SIZE);
552 if (size < pagesize)
553 size = pagesize;
554 }
555#endif
556
557 return size;
558}
559
560static int rb_sigaltstack_size_value = 0;
561
562void *
563rb_allocate_sigaltstack(void)
564{
565 void *altstack;
566 if (!rb_sigaltstack_size_value) {
567 rb_sigaltstack_size_value = rb_sigaltstack_size();
568 }
569 altstack = malloc(rb_sigaltstack_size_value);
570 if (!altstack) rb_memerror();
571 return altstack;
572}
573
574/* alternate stack for SIGSEGV */
575void *
576rb_register_sigaltstack(void *altstack)
577{
578 stack_t newSS, oldSS;
579
580 newSS.ss_size = rb_sigaltstack_size_value;
581 newSS.ss_sp = altstack;
582 newSS.ss_flags = 0;
583
584 sigaltstack(&newSS, &oldSS); /* ignore error. */
585
586 return newSS.ss_sp;
587}
588#endif /* USE_SIGALTSTACK */
589
590#ifdef POSIX_SIGNAL
591static sighandler_t
592ruby_signal(int signum, sighandler_t handler)
593{
594 struct sigaction sigact, old;
595
596#if 0
597 rb_trap_accept_nativethreads[signum] = 0;
598#endif
599
600 sigemptyset(&sigact.sa_mask);
601#if defined(USE_SIGALTSTACK) && !defined(__wasm__)
602 if (handler == SIG_IGN || handler == SIG_DFL) {
603 sigact.sa_handler = handler;
604 sigact.sa_flags = 0;
605 }
606 else {
607 sigact.sa_sigaction = (ruby_sigaction_t*)handler;
608 sigact.sa_flags = SA_SIGINFO;
609 }
610#else
611 sigact.sa_handler = handler;
612 sigact.sa_flags = 0;
613#endif
614
615 switch (signum) {
616#if defined(SA_ONSTACK) && defined(USE_SIGALTSTACK)
617 case SIGSEGV:
618#ifdef SIGBUS
619 case SIGBUS:
620#endif
621 sigact.sa_flags |= SA_ONSTACK;
622 break;
623#endif
624 }
625 (void)VALGRIND_MAKE_MEM_DEFINED(&old, sizeof(old));
626 if (sigaction(signum, &sigact, &old) < 0) {
627 return SIG_ERR;
628 }
629 if (old.sa_flags & SA_SIGINFO)
630 handler = (sighandler_t)old.sa_sigaction;
631 else
632 handler = old.sa_handler;
633 ASSUME(handler != SIG_ERR);
634 return handler;
635}
636
637sighandler_t
638ruby_posix_signal(int signum, sighandler_t handler)
639{
640 return ruby_signal(signum, handler);
641}
642
643#elif defined _WIN32
644static inline sighandler_t
645ruby_signal(int signum, sighandler_t handler)
646{
647 if (signum == SIGKILL) {
648 errno = EINVAL;
649 return SIG_ERR;
650 }
651 return signal(signum, handler);
652}
653
654#else /* !POSIX_SIGNAL */
655#define ruby_signal(sig,handler) (/* rb_trap_accept_nativethreads[(sig)] = 0,*/ signal((sig),(handler)))
656#if 0 /* def HAVE_NATIVETHREAD */
657static sighandler_t
658ruby_nativethread_signal(int signum, sighandler_t handler)
659{
660 sighandler_t old;
661
662 old = signal(signum, handler);
663 rb_trap_accept_nativethreads[signum] = 1;
664 return old;
665}
666#endif
667#endif
668
669static int
670signal_ignored(int sig)
671{
672 sighandler_t func;
673#ifdef POSIX_SIGNAL
674 struct sigaction old;
675 (void)VALGRIND_MAKE_MEM_DEFINED(&old, sizeof(old));
676 if (sigaction(sig, NULL, &old) < 0) return FALSE;
677 func = old.sa_handler;
678#else
679 sighandler_t old = signal(sig, SIG_DFL);
680 signal(sig, old);
681 func = old;
682#endif
683 if (func == SIG_IGN) return 1;
684 return func == sighandler ? 0 : -1;
685}
686
687static void
688signal_enque(int sig)
689{
690 ATOMIC_INC(signal_buff.cnt[sig]);
691 ATOMIC_INC(signal_buff.size);
692}
693
694static void
695sighandler(int sig)
696{
697 int old_errnum = errno;
698
699 signal_enque(sig);
700 rb_thread_wakeup_timer_thread(sig);
701
702#if !defined(BSD_SIGNAL) && !defined(POSIX_SIGNAL)
703 ruby_signal(sig, sighandler);
704#endif
705
706 errno = old_errnum;
707}
708
709int
710rb_signal_buff_size(void)
711{
712 return signal_buff.size;
713}
714
715static void
716rb_disable_interrupt(void)
717{
718#ifdef HAVE_PTHREAD_SIGMASK
719 sigset_t mask;
720 sigfillset(&mask);
721 pthread_sigmask(SIG_SETMASK, &mask, NULL);
722#endif
723}
724
725static void
726rb_enable_interrupt(void)
727{
728#ifdef HAVE_PTHREAD_SIGMASK
729 sigset_t mask;
730 sigemptyset(&mask);
731 pthread_sigmask(SIG_SETMASK, &mask, NULL);
732#endif
733}
734
735int
736rb_get_next_signal(void)
737{
738 int i, sig = 0;
739
740 if (signal_buff.size != 0) {
741 for (i=1; i<RUBY_NSIG; i++) {
742 if (signal_buff.cnt[i] > 0) {
743 ATOMIC_DEC(signal_buff.cnt[i]);
744 ATOMIC_DEC(signal_buff.size);
745 sig = i;
746 break;
747 }
748 }
749 }
750 return sig;
751}
752
753#if defined SIGSEGV || defined SIGBUS || defined SIGILL || defined SIGFPE
754static const char *received_signal;
755# define clear_received_signal() do { \
756 if (GET_VM() != NULL) rb_gc_enable(); \
757 received_signal = 0; \
758} while (0)
759#else
760# define clear_received_signal() ((void)0)
761#endif
762
763#if defined(USE_SIGALTSTACK) || defined(_WIN32)
764NORETURN(void rb_ec_stack_overflow(rb_execution_context_t *ec, int crit));
765# if defined __HAIKU__
766# define USE_UCONTEXT_REG 1
767# elif !(defined(HAVE_UCONTEXT_H) && (defined __i386__ || defined __x86_64__ || defined __amd64__))
768# elif defined __linux__
769# define USE_UCONTEXT_REG 1
770# elif defined __APPLE__
771# define USE_UCONTEXT_REG 1
772# elif defined __FreeBSD__
773# define USE_UCONTEXT_REG 1
774# endif
775#if defined(HAVE_PTHREAD_SIGMASK)
776# define ruby_sigunmask pthread_sigmask
777#elif defined(HAVE_SIGPROCMASK)
778# define ruby_sigunmask sigprocmask
779#endif
780static void
781reset_sigmask(int sig)
782{
783#if defined(ruby_sigunmask)
784 sigset_t mask;
785#endif
786 clear_received_signal();
787#if defined(ruby_sigunmask)
788 sigemptyset(&mask);
789 sigaddset(&mask, sig);
790 if (ruby_sigunmask(SIG_UNBLOCK, &mask, NULL)) {
791 rb_bug_errno(STRINGIZE(ruby_sigunmask)":unblock", errno);
792 }
793#endif
794}
795
796# ifdef USE_UCONTEXT_REG
797static void
798check_stack_overflow(int sig, const uintptr_t addr, const ucontext_t *ctx)
799{
800 const DEFINE_MCONTEXT_PTR(mctx, ctx);
801# if defined __linux__
802# if defined REG_RSP
803 const greg_t sp = mctx->gregs[REG_RSP];
804 const greg_t bp = mctx->gregs[REG_RBP];
805# else
806 const greg_t sp = mctx->gregs[REG_ESP];
807 const greg_t bp = mctx->gregs[REG_EBP];
808# endif
809# elif defined __APPLE__
810# include <AvailabilityMacros.h>
811# if defined(MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
812# define MCTX_SS_REG(reg) __ss.__##reg
813# else
814# define MCTX_SS_REG(reg) ss.reg
815# endif
816# if defined(__LP64__)
817 const uintptr_t sp = mctx->MCTX_SS_REG(rsp);
818 const uintptr_t bp = mctx->MCTX_SS_REG(rbp);
819# else
820 const uintptr_t sp = mctx->MCTX_SS_REG(esp);
821 const uintptr_t bp = mctx->MCTX_SS_REG(ebp);
822# endif
823# elif defined __FreeBSD__
824# if defined(__amd64__)
825 const __register_t sp = mctx->mc_rsp;
826 const __register_t bp = mctx->mc_rbp;
827# else
828 const __register_t sp = mctx->mc_esp;
829 const __register_t bp = mctx->mc_ebp;
830# endif
831# elif defined __HAIKU__
832# if defined(__amd64__)
833 const unsigned long sp = mctx->rsp;
834 const unsigned long bp = mctx->rbp;
835# else
836 const unsigned long sp = mctx->esp;
837 const unsigned long bp = mctx->ebp;
838# endif
839# endif
840 enum {pagesize = 4096};
841 const uintptr_t sp_page = (uintptr_t)sp / pagesize;
842 const uintptr_t bp_page = (uintptr_t)bp / pagesize;
843 const uintptr_t fault_page = addr / pagesize;
844
845 /* SP in ucontext is not decremented yet when `push` failed, so
846 * the fault page can be the next. */
847 if (sp_page == fault_page || sp_page == fault_page + 1 ||
848 (sp_page <= fault_page && fault_page <= bp_page)) {
849 rb_execution_context_t *ec = GET_EC();
850 int crit = FALSE;
851 int uplevel = roomof(pagesize, sizeof(*ec->tag)) / 2; /* XXX: heuristic */
852 while ((uintptr_t)ec->tag->buf / pagesize <= fault_page + 1) {
853 /* drop the last tag if it is close to the fault,
854 * otherwise it can cause stack overflow again at the same
855 * place. */
856 if ((crit = (!ec->tag->prev || !--uplevel)) != FALSE) break;
857 ec->tag = ec->tag->prev;
858 }
859 reset_sigmask(sig);
860 rb_ec_stack_overflow(ec, crit);
861 }
862}
863# else
864static void
865check_stack_overflow(int sig, const void *addr)
866{
867 int ruby_stack_overflowed_p(const rb_thread_t *, const void *);
868 rb_thread_t *th = GET_THREAD();
869 if (ruby_stack_overflowed_p(th, addr)) {
870 reset_sigmask(sig);
871 rb_ec_stack_overflow(th->ec, FALSE);
872 }
873}
874# endif
875
876# ifdef _WIN32
877# define CHECK_STACK_OVERFLOW() check_stack_overflow(sig, 0)
878# else
879# define FAULT_ADDRESS info->si_addr
880# ifdef USE_UCONTEXT_REG
881# define CHECK_STACK_OVERFLOW_() check_stack_overflow(sig, (uintptr_t)FAULT_ADDRESS, ctx)
882# else
883# define CHECK_STACK_OVERFLOW_() check_stack_overflow(sig, FAULT_ADDRESS)
884# endif
885# define MESSAGE_FAULT_ADDRESS " at %p", FAULT_ADDRESS
886# define SIGNAL_FROM_USER_P() ((info)->si_code == SI_USER)
887# define CHECK_STACK_OVERFLOW() (SIGNAL_FROM_USER_P() ? (void)0 : CHECK_STACK_OVERFLOW_())
888# endif
889#else
890# define CHECK_STACK_OVERFLOW() (void)0
891#endif
892#ifndef MESSAGE_FAULT_ADDRESS
893# define MESSAGE_FAULT_ADDRESS
894#endif
895
896#if defined SIGSEGV || defined SIGBUS || defined SIGILL || defined SIGFPE
897NOINLINE(static void check_reserved_signal_(const char *name, size_t name_len, int signo));
898/* noinine to reduce stack usage in signal handers */
899
900#define check_reserved_signal(name) check_reserved_signal_(name, sizeof(name)-1, sig)
901
902#ifdef SIGBUS
903
904static sighandler_t default_sigbus_handler;
905NORETURN(static ruby_sigaction_t sigbus);
906
907static void
908sigbus(int sig SIGINFO_ARG)
909{
910 check_reserved_signal("BUS");
911/*
912 * Mac OS X makes KERN_PROTECTION_FAILURE when thread touch guard page.
913 * and it's delivered as SIGBUS instead of SIGSEGV to userland. It's crazy
914 * wrong IMHO. but anyway we have to care it. Sigh.
915 */
916 /* Seems Linux also delivers SIGBUS. */
917#if defined __APPLE__ || defined __linux__
918 CHECK_STACK_OVERFLOW();
919#endif
920 rb_bug_for_fatal_signal(default_sigbus_handler, sig, SIGINFO_CTX, "Bus Error" MESSAGE_FAULT_ADDRESS);
921}
922#endif
923
924#ifdef SIGSEGV
925
926static sighandler_t default_sigsegv_handler;
927NORETURN(static ruby_sigaction_t sigsegv);
928
929static void
930sigsegv(int sig SIGINFO_ARG)
931{
932 check_reserved_signal("SEGV");
933 CHECK_STACK_OVERFLOW();
934 rb_bug_for_fatal_signal(default_sigsegv_handler, sig, SIGINFO_CTX, "Segmentation fault" MESSAGE_FAULT_ADDRESS);
935}
936#endif
937
938#ifdef SIGILL
939
940static sighandler_t default_sigill_handler;
941NORETURN(static ruby_sigaction_t sigill);
942
943static void
944sigill(int sig SIGINFO_ARG)
945{
946 check_reserved_signal("ILL");
947#if defined __APPLE__ || defined __linux__
948 CHECK_STACK_OVERFLOW();
949#endif
950 rb_bug_for_fatal_signal(default_sigill_handler, sig, SIGINFO_CTX, "Illegal instruction" MESSAGE_FAULT_ADDRESS);
951}
952#endif
953
954#ifndef __sun
955NORETURN(static void ruby_abort(void));
956#endif
957
958static void
959ruby_abort(void)
960{
961#ifdef __sun
962 /* Solaris's abort() is async signal unsafe. Of course, it is not
963 * POSIX compliant.
964 */
965 raise(SIGABRT);
966#else
967 abort();
968#endif
969}
970
971static void
972check_reserved_signal_(const char *name, size_t name_len, int signo)
973{
974 const char *prev = ATOMIC_PTR_EXCHANGE(received_signal, name);
975
976 if (prev) {
977 ssize_t RB_UNUSED_VAR(err);
978 static const int stderr_fd = 2;
979#define NOZ(name, str) RBIMPL_ATTR_NONSTRING() name[sizeof(str)-1] = str
980 static const char NOZ(msg1, " received in ");
981 static const char NOZ(msg2, " handler\n");
982
983#ifdef HAVE_WRITEV
984 struct iovec iov[4];
985 int i = 0;
986# define W(str, len) \
987 iov[i++] = (struct iovec){.iov_base = (void *)(str), .iov_len = (len)}
988#else
989# define W(str, len) err = write(stderr_fd, (str), (len))
990#endif
991
992#if __has_feature(address_sanitizer) || \
993 __has_feature(memory_sanitizer) || \
994 defined(HAVE_VALGRIND_MEMCHECK_H)
995 ruby_posix_signal(signo, SIG_DFL);
996#endif
997 W(name, name_len);
998 W(msg1, sizeof(msg1));
999 W(prev, strlen(prev));
1000 W(msg2, sizeof(msg2));
1001# undef W
1002#ifdef HAVE_WRITEV
1003 err = writev(stderr_fd, iov, i);
1004#endif
1005 ruby_abort();
1006 }
1007
1008 if (GET_VM() != NULL) {
1009 rb_gc_disable_no_rest();
1010 }
1011}
1012#endif
1013
1014#if defined SIGPIPE || defined SIGSYS
1015static void
1016sig_do_nothing(int sig)
1017{
1018}
1019#endif
1020
1021static int
1022signal_exec(VALUE cmd, int sig)
1023{
1024 rb_execution_context_t *ec = GET_EC();
1025 volatile rb_atomic_t old_interrupt_mask = ec->interrupt_mask;
1026 enum ruby_tag_type state;
1027
1028 /*
1029 * workaround the following race:
1030 * 1. signal_enque queues signal for execution
1031 * 2. user calls trap(sig, "IGNORE"), setting SIG_IGN
1032 * 3. rb_signal_exec runs on queued signal
1033 */
1034 if (IMMEDIATE_P(cmd))
1035 return FALSE;
1036
1037 ec->interrupt_mask |= TRAP_INTERRUPT_MASK;
1038 EC_PUSH_TAG(ec);
1039 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1040 VALUE signum = INT2NUM(sig);
1041 rb_eval_cmd_kw(cmd, rb_ary_new3(1, signum), RB_NO_KEYWORDS);
1042 }
1043 EC_POP_TAG();
1044 ec = GET_EC();
1045 ec->interrupt_mask = old_interrupt_mask;
1046
1047 if (state) {
1048 /* XXX: should be replaced with rb_threadptr_pending_interrupt_enque() */
1049 EC_JUMP_TAG(ec, state);
1050 }
1051 return TRUE;
1052}
1053
1054void
1055rb_vm_trap_exit(rb_vm_t *vm)
1056{
1057 VALUE trap_exit = vm->trap_list.cmd[0];
1058
1059 if (trap_exit) {
1060 vm->trap_list.cmd[0] = 0;
1061 signal_exec(trap_exit, 0);
1062 }
1063}
1064
1065/* returns true if a trap handler was run, false otherwise */
1066int
1067rb_signal_exec(rb_thread_t *th, int sig)
1068{
1069 rb_vm_t *vm = GET_VM();
1070 VALUE cmd = vm->trap_list.cmd[sig];
1071
1072 if (cmd == 0) {
1073 switch (sig) {
1074 case SIGINT:
1075 rb_interrupt();
1076 break;
1077#ifdef SIGHUP
1078 case SIGHUP:
1079#endif
1080#ifdef SIGQUIT
1081 case SIGQUIT:
1082#endif
1083#ifdef SIGTERM
1084 case SIGTERM:
1085#endif
1086#ifdef SIGALRM
1087 case SIGALRM:
1088#endif
1089#ifdef SIGUSR1
1090 case SIGUSR1:
1091#endif
1092#ifdef SIGUSR2
1093 case SIGUSR2:
1094#endif
1095 rb_threadptr_signal_raise(th, sig);
1096 break;
1097 }
1098 }
1099 else if (UNDEF_P(cmd)) {
1100 rb_threadptr_signal_exit(th);
1101 }
1102 else {
1103 return signal_exec(cmd, sig);
1104 }
1105 return FALSE;
1106}
1107
1108static sighandler_t
1109default_handler(int sig)
1110{
1111 sighandler_t func;
1112 switch (sig) {
1113 case SIGINT:
1114#ifdef SIGHUP
1115 case SIGHUP:
1116#endif
1117#ifdef SIGQUIT
1118 case SIGQUIT:
1119#endif
1120#ifdef SIGTERM
1121 case SIGTERM:
1122#endif
1123#ifdef SIGALRM
1124 case SIGALRM:
1125#endif
1126#ifdef SIGUSR1
1127 case SIGUSR1:
1128#endif
1129#ifdef SIGUSR2
1130 case SIGUSR2:
1131#endif
1132#ifdef RUBY_SIGCHLD
1133 case RUBY_SIGCHLD:
1134#endif
1135 func = sighandler;
1136 break;
1137#ifdef SIGBUS
1138 case SIGBUS:
1139 func = (sighandler_t)sigbus;
1140 break;
1141#endif
1142#ifdef SIGSEGV
1143 case SIGSEGV:
1144 func = (sighandler_t)sigsegv;
1145 break;
1146#endif
1147#ifdef SIGPIPE
1148 case SIGPIPE:
1149 func = sig_do_nothing;
1150 break;
1151#endif
1152#ifdef SIGSYS
1153 case SIGSYS:
1154 func = sig_do_nothing;
1155 break;
1156#endif
1157 default:
1158 func = SIG_DFL;
1159 break;
1160 }
1161
1162 return func;
1163}
1164
1165static sighandler_t
1166trap_handler(VALUE *cmd, int sig)
1167{
1168 sighandler_t func = sighandler;
1169 VALUE command;
1170
1171 if (NIL_P(*cmd)) {
1172 func = SIG_IGN;
1173 }
1174 else {
1175 command = rb_check_string_type(*cmd);
1176 if (NIL_P(command) && SYMBOL_P(*cmd)) {
1177 command = rb_sym2str(*cmd);
1178 if (!command) rb_raise(rb_eArgError, "bad handler");
1179 }
1180 if (!NIL_P(command)) {
1181 const char *cptr;
1182 long len;
1183 StringValue(command);
1184 *cmd = command;
1185 RSTRING_GETMEM(command, cptr, len);
1186 switch (len) {
1187 sig_ign:
1188 func = SIG_IGN;
1189 *cmd = Qtrue;
1190 break;
1191 sig_dfl:
1192 func = default_handler(sig);
1193 *cmd = 0;
1194 break;
1195 case 0:
1196 goto sig_ign;
1197 break;
1198 case 14:
1199 if (memcmp(cptr, "SYSTEM_DEFAULT", 14) == 0) {
1200 func = SIG_DFL;
1201 *cmd = 0;
1202 }
1203 break;
1204 case 7:
1205 if (memcmp(cptr, "SIG_IGN", 7) == 0) {
1206 goto sig_ign;
1207 }
1208 else if (memcmp(cptr, "SIG_DFL", 7) == 0) {
1209 goto sig_dfl;
1210 }
1211 else if (memcmp(cptr, "DEFAULT", 7) == 0) {
1212 goto sig_dfl;
1213 }
1214 break;
1215 case 6:
1216 if (memcmp(cptr, "IGNORE", 6) == 0) {
1217 goto sig_ign;
1218 }
1219 break;
1220 case 4:
1221 if (memcmp(cptr, "EXIT", 4) == 0) {
1222 *cmd = Qundef;
1223 }
1224 break;
1225 }
1226 }
1227 else {
1228 rb_proc_t *proc;
1229 GetProcPtr(*cmd, proc);
1230 (void)proc;
1231 }
1232 }
1233
1234 return func;
1235}
1236
1237static int
1238trap_signm(VALUE vsig)
1239{
1240 int sig = -1;
1241
1242 if (FIXNUM_P(vsig)) {
1243 sig = FIX2INT(vsig);
1244 if (sig < 0 || sig >= NSIG) {
1245 rb_raise(rb_eArgError, "invalid signal number (%d)", sig);
1246 }
1247 }
1248 else {
1249 sig = signm2signo(&vsig, FALSE, TRUE, NULL);
1250 }
1251 return sig;
1252}
1253
1254static VALUE
1255trap(int sig, sighandler_t func, VALUE command)
1256{
1257 sighandler_t oldfunc;
1258 VALUE oldcmd;
1259 rb_vm_t *vm = GET_VM();
1260
1261 /*
1262 * Be careful. ruby_signal() and trap_list.cmd[sig] must be changed
1263 * atomically. In current implementation, we only need to don't call
1264 * RUBY_VM_CHECK_INTS().
1265 */
1266 if (sig == 0) {
1267 oldfunc = SIG_ERR;
1268 }
1269 else {
1270 oldfunc = ruby_signal(sig, func);
1271 if (oldfunc == SIG_ERR) rb_sys_fail_str(rb_signo2signm(sig));
1272 }
1273 oldcmd = vm->trap_list.cmd[sig];
1274 switch (oldcmd) {
1275 case 0:
1276 case Qtrue:
1277 if (oldfunc == SIG_IGN) oldcmd = rb_str_new2("IGNORE");
1278 else if (oldfunc == SIG_DFL) oldcmd = rb_str_new2("SYSTEM_DEFAULT");
1279 else if (oldfunc == sighandler) oldcmd = rb_str_new2("DEFAULT");
1280 else oldcmd = Qnil;
1281 break;
1282 case Qnil:
1283 break;
1284 case Qundef:
1285 oldcmd = rb_str_new2("EXIT");
1286 break;
1287 }
1288
1289 ACCESS_ONCE(VALUE, vm->trap_list.cmd[sig]) = command;
1290
1291 return oldcmd;
1292}
1293
1294static int
1295reserved_signal_p(int signo)
1296{
1297/* Synchronous signal can't deliver to main thread */
1298#ifdef SIGSEGV
1299 if (signo == SIGSEGV)
1300 return 1;
1301#endif
1302#ifdef SIGBUS
1303 if (signo == SIGBUS)
1304 return 1;
1305#endif
1306#ifdef SIGILL
1307 if (signo == SIGILL)
1308 return 1;
1309#endif
1310#ifdef SIGFPE
1311 if (signo == SIGFPE)
1312 return 1;
1313#endif
1314
1315/* used ubf internal see thread_pthread.c. */
1316#ifdef SIGVTALRM
1317 if (signo == SIGVTALRM)
1318 return 1;
1319#endif
1320
1321 return 0;
1322}
1323
1324/*
1325 * call-seq:
1326 * Signal.trap( signal, command ) -> obj
1327 * Signal.trap( signal ) {| | block } -> obj
1328 *
1329 * Specifies the handling of signals. The first parameter is a signal
1330 * name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on) or a
1331 * signal number. The characters ``SIG'' may be omitted from the
1332 * signal name. The command or block specifies code to be run when the
1333 * signal is raised.
1334 * If the command is the string ``IGNORE'' or ``SIG_IGN'', the signal
1335 * will be ignored.
1336 * If the command is ``DEFAULT'' or ``SIG_DFL'', the Ruby's default handler
1337 * will be invoked.
1338 * If the command is ``EXIT'', the script will be terminated by the signal.
1339 * If the command is ``SYSTEM_DEFAULT'', the operating system's default
1340 * handler will be invoked.
1341 * Otherwise, the given command or block will be run.
1342 * The special signal name ``EXIT'' or signal number zero will be
1343 * invoked just prior to program termination.
1344 * trap returns the previous handler for the given signal.
1345 *
1346 * Signal.trap(0, proc { puts "Terminating: #{$$}" })
1347 * Signal.trap("CLD") { puts "Child died" }
1348 * fork && Process.wait
1349 *
1350 * <em>produces:</em>
1351 * Terminating: 27461
1352 * Child died
1353 * Terminating: 27460
1354 */
1355static VALUE
1356sig_trap(int argc, VALUE *argv, VALUE _)
1357{
1358 int sig;
1359 sighandler_t func;
1360 VALUE cmd;
1361
1362 rb_check_arity(argc, 1, 2);
1363
1364 sig = trap_signm(argv[0]);
1365 if (reserved_signal_p(sig)) {
1366 const char *name = signo2signm(sig);
1367 if (name)
1368 rb_raise(rb_eArgError, "can't trap reserved signal: SIG%s", name);
1369 else
1370 rb_raise(rb_eArgError, "can't trap reserved signal: %d", sig);
1371 }
1372
1373 if (argc == 1) {
1374 cmd = rb_block_proc();
1375 func = sighandler;
1376 }
1377 else {
1378 cmd = argv[1];
1379 func = trap_handler(&cmd, sig);
1380 }
1381
1382 if (rb_obj_is_proc(cmd) &&
1383 !rb_ractor_main_p() && !rb_ractor_shareable_p(cmd)) {
1384 cmd = rb_proc_isolate(cmd);
1385 }
1386
1387 return trap(sig, func, cmd);
1388}
1389
1390/*
1391 * call-seq:
1392 * Signal.list -> a_hash
1393 *
1394 * Returns a list of signal names mapped to the corresponding
1395 * underlying signal numbers.
1396 *
1397 * Signal.list #=> {"EXIT"=>0, "HUP"=>1, "INT"=>2, "QUIT"=>3, "ILL"=>4, "TRAP"=>5, "IOT"=>6, "ABRT"=>6, "FPE"=>8, "KILL"=>9, "BUS"=>7, "SEGV"=>11, "SYS"=>31, "PIPE"=>13, "ALRM"=>14, "TERM"=>15, "URG"=>23, "STOP"=>19, "TSTP"=>20, "CONT"=>18, "CHLD"=>17, "CLD"=>17, "TTIN"=>21, "TTOU"=>22, "IO"=>29, "XCPU"=>24, "XFSZ"=>25, "VTALRM"=>26, "PROF"=>27, "WINCH"=>28, "USR1"=>10, "USR2"=>12, "PWR"=>30, "POLL"=>29}
1398 */
1399static VALUE
1400sig_list(VALUE _)
1401{
1402 VALUE h = rb_hash_new();
1403 const struct signals *sigs;
1404
1405 FOREACH_SIGNAL(sigs, 0) {
1406 rb_hash_aset(h, rb_fstring_cstr(sigs->signm), INT2FIX(sigs->signo));
1407 }
1408 return h;
1409}
1410
1411#define INSTALL_SIGHANDLER(cond, signame, signum) do { \
1412 static const char failed[] = "failed to install "signame" handler"; \
1413 if (!(cond)) break; \
1414 if (reserved_signal_p(signum)) rb_bug(failed); \
1415 perror(failed); \
1416 } while (0)
1417
1418static int
1419install_sighandler_core(int signum, sighandler_t handler, sighandler_t *old_handler)
1420{
1421 sighandler_t old;
1422
1423 old = ruby_signal(signum, handler);
1424 if (old == SIG_ERR) return -1;
1425 if (old_handler) {
1426 *old_handler = (old == SIG_DFL || old == SIG_IGN) ? 0 : old;
1427 }
1428 else {
1429 /* signal handler should be inherited during exec. */
1430 if (old != SIG_DFL) {
1431 ruby_signal(signum, old);
1432 }
1433 }
1434 return 0;
1435}
1436
1437# define install_sighandler(signum, handler) \
1438 INSTALL_SIGHANDLER(install_sighandler_core(signum, handler, NULL), #signum, signum)
1439# define force_install_sighandler(signum, handler, old_handler) \
1440 INSTALL_SIGHANDLER(install_sighandler_core(signum, handler, old_handler), #signum, signum)
1441
1442void
1443ruby_sig_finalize(void)
1444{
1445 sighandler_t oldfunc;
1446
1447 oldfunc = ruby_signal(SIGINT, SIG_IGN);
1448 if (oldfunc == sighandler) {
1449 ruby_signal(SIGINT, SIG_DFL);
1450 }
1451}
1452
1453int ruby_enable_coredump = 0;
1454
1455/*
1456 * Many operating systems allow signals to be sent to running
1457 * processes. Some signals have a defined effect on the process, while
1458 * others may be trapped at the code level and acted upon. For
1459 * example, your process may trap the USR1 signal and use it to toggle
1460 * debugging, and may use TERM to initiate a controlled shutdown.
1461 *
1462 * pid = fork do
1463 * Signal.trap("USR1") do
1464 * $debug = !$debug
1465 * puts "Debug now: #$debug"
1466 * end
1467 * Signal.trap("TERM") do
1468 * puts "Terminating..."
1469 * shutdown()
1470 * end
1471 * # . . . do some work . . .
1472 * end
1473 *
1474 * Process.detach(pid)
1475 *
1476 * # Controlling program:
1477 * Process.kill("USR1", pid)
1478 * # ...
1479 * Process.kill("USR1", pid)
1480 * # ...
1481 * Process.kill("TERM", pid)
1482 *
1483 * <em>produces:</em>
1484 * Debug now: true
1485 * Debug now: false
1486 * Terminating...
1487 *
1488 * The list of available signal names and their interpretation is
1489 * system dependent. Signal delivery semantics may also vary between
1490 * systems; in particular signal delivery may not always be reliable.
1491 */
1492void
1493Init_signal(void)
1494{
1495 VALUE mSignal = rb_define_module("Signal");
1496
1497 rb_define_global_function("trap", sig_trap, -1);
1498 rb_define_module_function(mSignal, "trap", sig_trap, -1);
1499 rb_define_module_function(mSignal, "list", sig_list, 0);
1500 rb_define_module_function(mSignal, "signame", sig_signame, 1);
1501
1502 rb_define_method(rb_eSignal, "initialize", esignal_init, -1);
1503 rb_define_method(rb_eSignal, "signo", esignal_signo, 0);
1504 rb_alias(rb_eSignal, rb_intern_const("signm"), rb_intern_const("message"));
1505 rb_define_method(rb_eInterrupt, "initialize", interrupt_init, -1);
1506
1507 // It should be ready to call rb_signal_exec()
1508 VM_ASSERT(GET_THREAD()->pending_interrupt_queue);
1509
1510 /* At this time, there is no subthread. Then sigmask guarantee atomics. */
1511 rb_disable_interrupt();
1512
1513 install_sighandler(SIGINT, sighandler);
1514#ifdef SIGHUP
1515 install_sighandler(SIGHUP, sighandler);
1516#endif
1517#ifdef SIGQUIT
1518 install_sighandler(SIGQUIT, sighandler);
1519#endif
1520#ifdef SIGTERM
1521 install_sighandler(SIGTERM, sighandler);
1522#endif
1523#ifdef SIGALRM
1524 install_sighandler(SIGALRM, sighandler);
1525#endif
1526#ifdef SIGUSR1
1527 install_sighandler(SIGUSR1, sighandler);
1528#endif
1529#ifdef SIGUSR2
1530 install_sighandler(SIGUSR2, sighandler);
1531#endif
1532
1533 if (!ruby_enable_coredump) {
1534#ifdef SIGBUS
1535 force_install_sighandler(SIGBUS, (sighandler_t)sigbus, &default_sigbus_handler);
1536#endif
1537#ifdef SIGILL
1538 force_install_sighandler(SIGILL, (sighandler_t)sigill, &default_sigill_handler);
1539#endif
1540#ifdef SIGSEGV
1541 RB_ALTSTACK_INIT(GET_VM()->main_altstack, rb_allocate_sigaltstack());
1542 force_install_sighandler(SIGSEGV, (sighandler_t)sigsegv, &default_sigsegv_handler);
1543#endif
1544 }
1545#ifdef SIGPIPE
1546 install_sighandler(SIGPIPE, sig_do_nothing);
1547#endif
1548#ifdef SIGSYS
1549 install_sighandler(SIGSYS, sig_do_nothing);
1550#endif
1551
1552#ifdef RUBY_SIGCHLD
1553 install_sighandler(RUBY_SIGCHLD, sighandler);
1554#endif
1555
1556 rb_enable_interrupt();
1557}
rb_atomic_t
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_define_module_function
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
Definition cxxanyargs.hpp:689
rb_define_global_function
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
Definition cxxanyargs.hpp:695
rb_define_module
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1095
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
UNREACHABLE_RETURN
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
FIX2INT
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
ASSUME
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
rb_ary_new3
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
NUM2INT
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
INT2NUM
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
IMMEDIATE_P
#define IMMEDIATE_P
Old name of RB_IMMEDIATE_P.
Definition special_consts.h:54
FIXNUM_P
#define FIXNUM_P
Old name of RB_FIXNUM_P.
Definition special_consts.h:53
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
ruby_sig_finalize
void ruby_sig_finalize(void)
Clear signal handlers.
Definition signal.c:1443
rb_eInterrupt
VALUE rb_eInterrupt
Interrupt exception.
Definition error.c:1424
rb_bug_errno
void rb_bug_errno(const char *mesg, int errno_arg)
This is a wrapper of rb_bug() which automatically constructs appropriate message from the passed errn...
Definition error.c:1140
rb_eSignal
VALUE rb_eSignal
SignalException exception.
Definition error.c:1425
rb_check_to_integer
VALUE rb_check_to_integer(VALUE val, const char *mid)
Identical to rb_check_convert_type(), except the return value type is fixed to rb_cInteger.
Definition object.c:3179
rb_call_super
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
Definition vm_eval.c:362
rb_memerror
void rb_memerror(void)
Triggers out-of-memory error.
Definition gc.c:4527
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
rb_check_arity
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
rb_interrupt
void rb_interrupt(void)
Raises an instance of rb_eInterrupt.
Definition eval.c:695
rb_hash_new
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition hash.c:1477
rb_block_proc
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:839
rb_obj_is_proc
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:120
ruby_default_signal
void ruby_default_signal(int sig)
Pretends as if there was no custom signal handler.
Definition signal.c:412
ruby_signal_name
const char * ruby_signal_name(int signo)
Queries the name of the signal.
Definition signal.c:318
rb_f_kill
VALUE rb_f_kill(int argc, const VALUE *argv)
Sends a signal ("kills") to processes.
Definition signal.c:431
rb_str_append
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3681
rb_str_subseq
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition string.c:3056
rb_must_asciicompat
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2697
rb_check_string_type
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2855
rb_str_new_cstr
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1514
rb_thread_current
VALUE rb_thread_current(void)
Obtains the "current" thread.
Definition thread.c:2984
rb_ivar_set
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1939
rb_ivar_get
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1426
rb_alias
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2289
rb_eval_cmd_kw
VALUE rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat)
This API is practically a variant of rb_proc_call_kw() now.
Definition vm_eval.c:2123
rb_intern_const
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:284
rb_sym2str
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:971
len
int len
Length of the buffer.
Definition io.h:8
rb_ractor_shareable_p
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
nonstring.h
Defines RBIMPL_ATTR_NONSTRING.
NUM2PIDT
#define NUM2PIDT
Converts an instance of rb_cNumeric into C's pid_t.
Definition pid_t.h:33
StringValue
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
RSTRING_GETMEM
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:488
rb_obj_classname
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:507
errno
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
RB_NO_KEYWORDS
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
_
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
iovec
Definition win32.h:226
rb_proc_t
Definition vm_core.h:1268
signals
Definition signal.c:74
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_SYMBOL_P
static bool RB_SYMBOL_P(VALUE obj)
Queries if the object is an instance of rb_cSymbol.
Definition value_type.h:307
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376