github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/misc/cgo/testcarchive/main_unix.c (about) 1 // Copyright 2015 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 #include <signal.h> 6 #include <stdint.h> 7 #include <stdio.h> 8 #include <string.h> 9 10 struct sigaction sa; 11 struct sigaction osa; 12 13 static void (*oldHandler)(int, siginfo_t*, void*); 14 15 static void handler(int signo, siginfo_t* info, void* ctxt) { 16 if (oldHandler) { 17 oldHandler(signo, info, ctxt); 18 } 19 } 20 21 int install_handler() { 22 // Install our own signal handler. 23 memset(&sa, 0, sizeof sa); 24 sa.sa_sigaction = handler; 25 sigemptyset(&sa.sa_mask); 26 sa.sa_flags = SA_ONSTACK | SA_SIGINFO; 27 memset(&osa, 0, sizeof osa); 28 sigemptyset(&osa.sa_mask); 29 if (sigaction(SIGSEGV, &sa, &osa) < 0) { 30 perror("sigaction"); 31 return 2; 32 } 33 if (osa.sa_handler == SIG_DFL || (osa.sa_flags&SA_ONSTACK) == 0) { 34 fprintf(stderr, "Go runtime did not install signal handler\n"); 35 return 2; 36 } 37 oldHandler = osa.sa_sigaction; 38 39 return 0; 40 } 41 42 int check_handler() { 43 if (sigaction(SIGSEGV, NULL, &sa) < 0) { 44 perror("sigaction check"); 45 return 2; 46 } 47 if (sa.sa_sigaction != handler) { 48 fprintf(stderr, "ERROR: wrong signal handler: %p != %p\n", sa.sa_sigaction, handler); 49 return 2; 50 } 51 return 0; 52 } 53