libgo: update to go1.9
[official-gcc.git] / libgo / misc / cgo / testcarchive / main5.c
blob2437bf07c58f8f530bf42e5b6dac9396fdf26b34
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.
5 // Test for verifying that the Go runtime properly forwards
6 // signals when non-Go signals are raised.
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <sys/select.h>
15 #include "libgo2.h"
17 int main(int argc, char** argv) {
18 int verbose;
19 int test;
21 if (argc < 2) {
22 printf("Missing argument\n");
23 return 1;
26 test = atoi(argv[1]);
28 verbose = (argc > 2);
30 if (verbose) {
31 printf("calling RunGoroutines\n");
34 Noop();
36 switch (test) {
37 case 1: {
38 if (verbose) {
39 printf("attempting segfault\n");
42 volatile int crash = *(int *) 0;
43 break;
46 case 2: {
47 struct timeval tv;
49 if (verbose) {
50 printf("attempting external signal test\n");
53 fprintf(stderr, "OK\n");
54 fflush(stderr);
56 // The program should be interrupted before
57 // this sleep finishes. We use select rather
58 // than sleep because in older versions of
59 // glibc the sleep function does some signal
60 // fiddling to handle SIGCHLD. If this
61 // program is fiddling signals just when the
62 // test program sends the signal, the signal
63 // may be delivered to a Go thread which will
64 // break this test.
65 tv.tv_sec = 60;
66 tv.tv_usec = 0;
67 select(0, NULL, NULL, NULL, &tv);
69 break;
71 case 3: {
72 if (verbose) {
73 printf("attempting SIGPIPE\n");
76 int fd[2];
77 if (pipe(fd) != 0) {
78 printf("pipe(2) failed\n");
79 return 0;
81 // Close the reading end.
82 close(fd[0]);
83 // Expect that write(2) fails (EPIPE)
84 if (write(fd[1], "some data", 9) != -1) {
85 printf("write(2) unexpectedly succeeded\n");
86 return 0;
89 default:
90 printf("Unknown test: %d\n", test);
91 return 0;
94 printf("FAIL\n");
95 return 0;