]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/testsigfwd/main.go
misc/cgo/testsigfwd: delete unused code
[gostls13.git] / misc / cgo / testsigfwd / main.go
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 package main
6
7 import "fmt"
8
9 /*
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 int *p;
16 static void sigsegv() {
17         *p = 1;
18         fprintf(stderr, "ERROR: C SIGSEGV not thrown on caught?.\n");
19         exit(2);
20 }
21
22 static void segvhandler(int signum) {
23         if (signum == SIGSEGV) {
24                 fprintf(stdout, "ok\ttestsigfwd\n");
25                 exit(0);  // success
26         }
27 }
28
29 static void __attribute__ ((constructor)) sigsetup(void) {
30         struct sigaction act;
31
32         memset(&act, 0, sizeof act);
33         act.sa_handler = segvhandler;
34         sigaction(SIGSEGV, &act, NULL);
35 }
36 */
37 import "C"
38
39 var p *byte
40
41 func f() (ret bool) {
42         defer func() {
43                 if recover() == nil {
44                         fmt.Errorf("ERROR: couldn't raise SIGSEGV in Go.")
45                         C.exit(2)
46                 }
47                 ret = true
48         }()
49         *p = 1
50         return false
51 }
52
53 func main() {
54         // Test that the signal originating in Go is handled (and recovered) by Go.
55         if !f() {
56                 fmt.Errorf("couldn't recover from SIGSEGV in Go.")
57                 C.exit(2)
58         }
59
60         // Test that the signal originating in C is handled by C.
61         C.sigsegv()
62 }