Last active
June 26, 2017 10:31
-
-
Save tlewin/5432026 to your computer and use it in GitHub Desktop.
Force ruby extension process signals like Ctrl + C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <ruby.h> | |
/* Force ruby extension process signals like Ctrl + C */ | |
void process_signal(int signal) { | |
VALUE pid; | |
/* force call any block on trap function */ | |
pid = rb_funcall(rb_mProcess, rb_intern("pid"), 0); | |
rb_funcall(rb_mProcess, rb_intern("kill"), 2, INT2FIX(signal), INT2FIX(pid)); | |
} | |
void register_signals() { | |
int i; | |
for(i = 0; i < 33; i++) { | |
(void) signal(i, process_signal); | |
} | |
} | |
static VALUE start_main_loop(VALUE self) { | |
register_signals(); | |
while(1) { | |
usleep(100); | |
} | |
return Qnil; | |
} | |
void Init_mysignal() { | |
VALUE klass_mysignal = rb_define_class("MySignal", rb_cObject); | |
rb_define_singleton_method(klass_mysignal, "start!", start_main_loop, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment