Text file src/cmd/cgo/internal/testcarchive/testdata/main5.c

     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  	Noop();
    33  
    34  	switch (test) {
    35  		case 1: {
    36  			if (verbose) {
    37  				printf("attempting segfault\n");
    38  			}
    39  
    40  			*nilp = 0;
    41  			break;
    42  		}
    43  
    44  		case 2: {
    45  			struct timeval tv;
    46  
    47  			if (verbose) {
    48  				printf("attempting external signal test\n");
    49  			}
    50  
    51  			fprintf(stderr, "OK\n");
    52  			fflush(stderr);
    53  
    54  			// The program should be interrupted before
    55  			// this sleep finishes. We use select rather
    56  			// than sleep because in older versions of
    57  			// glibc the sleep function does some signal
    58  			// fiddling to handle SIGCHLD.  If this
    59  			// program is fiddling signals just when the
    60  			// test program sends the signal, the signal
    61  			// may be delivered to a Go thread which will
    62  			// break this test.
    63  			tv.tv_sec = 60;
    64  			tv.tv_usec = 0;
    65  			select(0, NULL, NULL, NULL, &tv);
    66  
    67  			break;
    68  		}
    69  		case 3: {
    70  			if (verbose) {
    71  				printf("attempting SIGPIPE\n");
    72  			}
    73  
    74  			int fd[2];
    75  			if (pipe(fd) != 0) {
    76  				printf("pipe(2) failed\n");
    77  				return 0;
    78  			}
    79  			// Close the reading end.
    80  			close(fd[0]);
    81  			// Expect that write(2) fails (EPIPE)
    82  			if (write(fd[1], "some data", 9) != -1) {
    83  				printf("write(2) unexpectedly succeeded\n");
    84  				return 0;
    85  			}
    86  			printf("did not receive SIGPIPE\n");
    87  			return 0;
    88  		}
    89  		case 4: {
    90  			fprintf(stderr, "OK\n");
    91  			fflush(stderr);
    92  
    93  			if (verbose) {
    94  				printf("calling Block\n");
    95  			}
    96  			Block();
    97  		}
    98  		default:
    99  			printf("Unknown test: %d\n", test);
   100  			return 0;
   101  	}
   102  
   103  	printf("FAIL\n");
   104  	return 0;
   105  }
   106  

View as plain text