github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/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  		default:
    72  			printf("Unknown test: %d\n", test);
    73  			return 0;
    74  	}
    75  
    76  	printf("FAIL\n");
    77  	return 0;
    78  }