Ruby: signal.c Source File

Ruby 3.3.6p108 (2024-11-05 revision 75015d4c1f6965b5e85e96fb309f1f2129f933c0)

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