github.com/s1s1ty/go@v0.0.0-20180207192209-104445e3140f/src/runtime/testdata/testprogcgo/catchpanic.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !plan9,!windows
     6  
     7  package main
     8  
     9  /*
    10  #include <signal.h>
    11  #include <stdlib.h>
    12  #include <string.h>
    13  
    14  static void abrthandler(int signum) {
    15  	if (signum == SIGABRT) {
    16  		exit(0);  // success
    17  	}
    18  }
    19  
    20  void registerAbortHandler() {
    21  	struct sigaction act;
    22  	memset(&act, 0, sizeof act);
    23  	act.sa_handler = abrthandler;
    24  	sigaction(SIGABRT, &act, NULL);
    25  }
    26  
    27  static void __attribute__ ((constructor)) sigsetup(void) {
    28  	if (getenv("CGOCATCHPANIC_EARLY_HANDLER") == NULL)
    29  		return;
    30  	registerAbortHandler();
    31  }
    32  */
    33  import "C"
    34  import "os"
    35  
    36  func init() {
    37  	register("CgoCatchPanic", CgoCatchPanic)
    38  }
    39  
    40  // Test that the SIGABRT raised by panic can be caught by an early signal handler.
    41  func CgoCatchPanic() {
    42  	if _, ok := os.LookupEnv("CGOCATCHPANIC_EARLY_HANDLER"); !ok {
    43  		C.registerAbortHandler()
    44  	}
    45  	panic("catch me")
    46  }