github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/misc/cgo/testcarchive/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 main(int argc, char** argv) {
    18  	int verbose;
    19  	int test;
    20  
    21  	if (argc < 2) {
    22  		printf("Missing argument\n");
    23  		return 1;
    24  	}
    25  
    26  	test = atoi(argv[1]);
    27  
    28  	verbose = (argc > 2);
    29  
    30  	if (verbose) {
    31  		printf("calling RunGoroutines\n");
    32  	}
    33  
    34  	Noop();
    35  
    36  	switch (test) {
    37  		case 1: {
    38  			if (verbose) {
    39  				printf("attempting segfault\n");
    40  			}
    41  
    42  			volatile int crash = *(int *) 0;
    43  			break;
    44  		}
    45  
    46  		case 2: {
    47  			struct timeval tv;
    48  
    49  			if (verbose) {
    50  				printf("attempting external signal test\n");
    51  			}
    52  
    53  			fprintf(stderr, "OK\n");
    54  			fflush(stderr);
    55  
    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);
    68  
    69  			break;
    70  		}
    71  		case 3: {
    72  			if (verbose) {
    73  				printf("attempting SIGPIPE\n");
    74  			}
    75  
    76  			int fd[2];
    77  			if (pipe(fd) != 0) {
    78  				printf("pipe(2) failed\n");
    79  				return 0;
    80  			}
    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;
    87  			}
    88  		}
    89  		default:
    90  			printf("Unknown test: %d\n", test);
    91  			return 0;
    92  	}
    93  
    94  	printf("FAIL\n");
    95  	return 0;
    96  }