From c3189cee717a47dd7936d9f82904db72b28293f6 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sat, 19 Aug 2017 16:59:19 +0200 Subject: runtime: forward crashing signals to late handlers CL 49590 made it possible for external signal handlers to catch signals from a crashing Go process. This CL extends that support to handlers registered after the Go runtime has initialized. Updates #20392 (and possibly fix it). Change-Id: I18eccd5e958a505f4d1782a7fc51c16bd3a4ff9c Reviewed-on: https://go-review.googlesource.com/57291 Run-TryBot: Elias Naur TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/runtime/testdata/testprogcgo/catchpanic.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/runtime/testdata') diff --git a/src/runtime/testdata/testprogcgo/catchpanic.go b/src/runtime/testdata/testprogcgo/catchpanic.go index f03b6d3ea3..55a606d1bc 100644 --- a/src/runtime/testdata/testprogcgo/catchpanic.go +++ b/src/runtime/testdata/testprogcgo/catchpanic.go @@ -17,17 +17,21 @@ static void abrthandler(int signum) { } } -static void __attribute__ ((constructor)) sigsetup(void) { +void registerAbortHandler() { struct sigaction act; - - if (getenv("CGOCATCHPANIC_INSTALL_HANDLER") == NULL) - return; memset(&act, 0, sizeof act); act.sa_handler = abrthandler; sigaction(SIGABRT, &act, NULL); } + +static void __attribute__ ((constructor)) sigsetup(void) { + if (getenv("CGOCATCHPANIC_EARLY_HANDLER") == NULL) + return; + registerAbortHandler(); +} */ import "C" +import "os" func init() { register("CgoCatchPanic", CgoCatchPanic) @@ -35,5 +39,8 @@ func init() { // Test that the SIGABRT raised by panic can be caught by an early signal handler. func CgoCatchPanic() { + if _, ok := os.LookupEnv("CGOCATCHPANIC_EARLY_HANDLER"); !ok { + C.registerAbortHandler() + } panic("catch me") } -- cgit v1.3-5-g9baa