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 os/signal.Notify and os/signal.Reset.
6 // This is a lot like misc/cgo/testcshared/main5.c.
18 static void die(const char* msg
) {
23 static volatile sig_atomic_t sigioSeen
;
25 static void ioHandler(int signo
, siginfo_t
* info
, void* ctxt
) {
29 // Set up the SIGPIPE signal handler in a high priority constructor, so
30 // that it is installed before the Go code starts.
32 static void pipeHandler(int signo
, siginfo_t
* info
, void* ctxt
) {
33 const char *s
= "unexpected SIGPIPE\n";
34 write(2, s
, strlen(s
));
38 static void init(void) __attribute__ ((constructor (200)));
43 memset(&sa
, 0, sizeof sa
);
44 sa
.sa_sigaction
= pipeHandler
;
45 if (sigemptyset(&sa
.sa_mask
) < 0) {
48 sa
.sa_flags
= SA_SIGINFO
;
49 if (sigaction(SIGPIPE
, &sa
, NULL
) < 0) {
54 int main(int argc
, char** argv
) {
61 setvbuf(stdout
, NULL
, _IONBF
, 0);
64 printf("raising SIGPIPE\n");
67 // Test that the Go runtime handles SIGPIPE, even if we installed
68 // a non-default SIGPIPE handler before the runtime initializes.
72 printf("calling sigaction\n");
75 memset(&sa
, 0, sizeof sa
);
76 sa
.sa_sigaction
= ioHandler
;
77 if (sigemptyset(&sa
.sa_mask
) < 0) {
80 sa
.sa_flags
= SA_SIGINFO
;
81 if (sigaction(SIGIO
, &sa
, NULL
) < 0) {
85 // At this point there should not be a Go signal handler
86 // installed for SIGIO.
89 printf("raising SIGIO\n");
92 if (raise(SIGIO
) < 0) {
97 printf("waiting for sigioSeen\n");
100 // Wait until the signal has been delivered.
104 ts
.tv_nsec
= 1000000;
105 nanosleep(&ts
, NULL
);
108 fprintf(stderr
, "looping too long waiting for signal\n");
115 // Tell the Go code to catch SIGIO.
118 printf("calling CatchSIGIO\n");
124 printf("raising SIGIO\n");
127 if (raise(SIGIO
) < 0) {
132 printf("calling SawSIGIO\n");
136 fprintf(stderr
, "Go handler did not see SIGIO\n");
140 if (sigioSeen
!= 0) {
141 fprintf(stderr
, "C handler saw SIGIO when only Go handler should have\n");
145 // Tell the Go code to stop catching SIGIO.
148 printf("calling ResetSIGIO\n");
154 printf("raising SIGIO\n");
157 if (raise(SIGIO
) < 0) {
162 printf("calling SawSIGIO\n");
166 fprintf(stderr
, "Go handler saw SIGIO after Reset\n");
171 printf("waiting for sigioSeen\n");
174 // Wait until the signal has been delivered.
178 ts
.tv_nsec
= 1000000;
179 nanosleep(&ts
, NULL
);
182 fprintf(stderr
, "looping too long waiting for signal\n");