Text file misc/cgo/testcarchive/testdata/main_unix.c

     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 <stdlib.h>
     9  #include <string.h>
    10  
    11  struct sigaction sa;
    12  struct sigaction osa;
    13  
    14  static void (*oldHandler)(int, siginfo_t*, void*);
    15  
    16  static void handler(int signo, siginfo_t* info, void* ctxt) {
    17  	if (oldHandler) {
    18  		oldHandler(signo, info, ctxt);
    19  	}
    20  }
    21  
    22  int install_handler() {
    23  	// Install our own signal handler.
    24  	memset(&sa, 0, sizeof sa);
    25  	sa.sa_sigaction = handler;
    26  	sigemptyset(&sa.sa_mask);
    27  	sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
    28  	memset(&osa, 0, sizeof osa);
    29  	sigemptyset(&osa.sa_mask);
    30  	if (sigaction(SIGSEGV, &sa, &osa) < 0) {
    31  		perror("sigaction");
    32  		return 2;
    33  	}
    34  	if (osa.sa_handler == SIG_DFL) {
    35  		fprintf(stderr, "Go runtime did not install signal handler\n");
    36  		return 2;
    37  	}
    38  	// gccgo does not set SA_ONSTACK for SIGSEGV.
    39  	if (getenv("GCCGO") == NULL && (osa.sa_flags&SA_ONSTACK) == 0) {
    40  		fprintf(stderr, "Go runtime did not install signal handler\n");
    41  		return 2;
    42  	}
    43  	oldHandler = osa.sa_sigaction;
    44  
    45  	return 0;
    46  }
    47  
    48  int check_handler() {
    49  	if (sigaction(SIGSEGV, NULL, &sa) < 0) {
    50  		perror("sigaction check");
    51  		return 2;
    52  	}
    53  	if (sa.sa_sigaction != handler) {
    54  		fprintf(stderr, "ERROR: wrong signal handler: %p != %p\n", sa.sa_sigaction, handler);
    55  		return 2;
    56  	}
    57  	return 0;
    58  }
    59  
    60  

View as plain text