github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/misc/cgo/testcarchive/testdata/main5.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 // Test for verifying that the Go runtime properly forwards 6 // signals when non-Go signals are raised. 7 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> 14 15 #include "libgo2.h" 16 17 int *nilp; 18 19 int main(int argc, char** argv) { 20 int verbose; 21 int test; 22 23 if (argc < 2) { 24 printf("Missing argument\n"); 25 return 1; 26 } 27 28 test = atoi(argv[1]); 29 30 verbose = (argc > 2); 31 32 if (verbose) { 33 printf("calling RunGoroutines\n"); 34 } 35 36 Noop(); 37 38 switch (test) { 39 case 1: { 40 if (verbose) { 41 printf("attempting segfault\n"); 42 } 43 44 *nilp = 0; 45 break; 46 } 47 48 case 2: { 49 struct timeval tv; 50 51 if (verbose) { 52 printf("attempting external signal test\n"); 53 } 54 55 fprintf(stderr, "OK\n"); 56 fflush(stderr); 57 58 // The program should be interrupted before 59 // this sleep finishes. We use select rather 60 // than sleep because in older versions of 61 // glibc the sleep function does some signal 62 // fiddling to handle SIGCHLD. If this 63 // program is fiddling signals just when the 64 // test program sends the signal, the signal 65 // may be delivered to a Go thread which will 66 // break this test. 67 tv.tv_sec = 60; 68 tv.tv_usec = 0; 69 select(0, NULL, NULL, NULL, &tv); 70 71 break; 72 } 73 case 3: { 74 if (verbose) { 75 printf("attempting SIGPIPE\n"); 76 } 77 78 int fd[2]; 79 if (pipe(fd) != 0) { 80 printf("pipe(2) failed\n"); 81 return 0; 82 } 83 // Close the reading end. 84 close(fd[0]); 85 // Expect that write(2) fails (EPIPE) 86 if (write(fd[1], "some data", 9) != -1) { 87 printf("write(2) unexpectedly succeeded\n"); 88 return 0; 89 } 90 printf("did not receive SIGPIPE\n"); 91 return 0; 92 } 93 default: 94 printf("Unknown test: %d\n", test); 95 return 0; 96 } 97 98 printf("FAIL\n"); 99 return 0; 100 }