main5.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // Test that a signal handler works in non-Go code when using
  5. // os/signal.Notify.
  6. // This is a lot like misc/cgo/testcarchive/main3.c.
  7. #include <signal.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <sched.h>
  13. #include <dlfcn.h>
  14. static void die(const char* msg) {
  15. perror(msg);
  16. exit(EXIT_FAILURE);
  17. }
  18. static volatile sig_atomic_t sigioSeen;
  19. static void ioHandler(int signo, siginfo_t* info, void* ctxt) {
  20. sigioSeen = 1;
  21. }
  22. int main(int argc, char** argv) {
  23. int verbose;
  24. struct sigaction sa;
  25. void* handle;
  26. void (*fn1)(void);
  27. int (*sawSIGIO)(void);
  28. int i;
  29. struct timespec ts;
  30. verbose = argc > 2;
  31. setvbuf(stdout, NULL, _IONBF, 0);
  32. if (verbose) {
  33. printf("calling sigaction\n");
  34. }
  35. memset(&sa, 0, sizeof sa);
  36. sa.sa_sigaction = ioHandler;
  37. if (sigemptyset(&sa.sa_mask) < 0) {
  38. die("sigemptyset");
  39. }
  40. sa.sa_flags = SA_SIGINFO;
  41. if (sigaction(SIGIO, &sa, NULL) < 0) {
  42. die("sigaction");
  43. }
  44. if (verbose) {
  45. printf("calling dlopen\n");
  46. }
  47. handle = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
  48. if (handle == NULL) {
  49. fprintf(stderr, "%s\n", dlerror());
  50. exit(EXIT_FAILURE);
  51. }
  52. // At this point there should not be a Go signal handler
  53. // installed for SIGIO.
  54. if (verbose) {
  55. printf("raising SIGIO\n");
  56. }
  57. if (raise(SIGIO) < 0) {
  58. die("raise");
  59. }
  60. if (verbose) {
  61. printf("waiting for sigioSeen\n");
  62. }
  63. // Wait until the signal has been delivered.
  64. i = 0;
  65. while (!sigioSeen) {
  66. ts.tv_sec = 0;
  67. ts.tv_nsec = 1000000;
  68. nanosleep(&ts, NULL);
  69. i++;
  70. if (i > 5000) {
  71. fprintf(stderr, "looping too long waiting for signal\n");
  72. exit(EXIT_FAILURE);
  73. }
  74. }
  75. sigioSeen = 0;
  76. // Tell the Go code to catch SIGIO.
  77. if (verbose) {
  78. printf("calling dlsym\n");
  79. }
  80. fn1 = (void(*)(void))dlsym(handle, "CatchSIGIO");
  81. if (fn1 == NULL) {
  82. fprintf(stderr, "%s\n", dlerror());
  83. exit(EXIT_FAILURE);
  84. }
  85. if (verbose) {
  86. printf("calling CatchSIGIO\n");
  87. }
  88. fn1();
  89. if (verbose) {
  90. printf("raising SIGIO\n");
  91. }
  92. if (raise(SIGIO) < 0) {
  93. die("raise");
  94. }
  95. if (verbose) {
  96. printf("calling dlsym\n");
  97. }
  98. // Check that the Go code saw SIGIO.
  99. sawSIGIO = (int (*)(void))dlsym(handle, "SawSIGIO");
  100. if (sawSIGIO == NULL) {
  101. fprintf(stderr, "%s\n", dlerror());
  102. exit(EXIT_FAILURE);
  103. }
  104. if (verbose) {
  105. printf("calling SawSIGIO\n");
  106. }
  107. if (!sawSIGIO()) {
  108. fprintf(stderr, "Go handler did not see SIGIO\n");
  109. exit(EXIT_FAILURE);
  110. }
  111. if (sigioSeen != 0) {
  112. fprintf(stderr, "C handler saw SIGIO when only Go handler should have\n");
  113. exit(EXIT_FAILURE);
  114. }
  115. // Tell the Go code to stop catching SIGIO.
  116. if (verbose) {
  117. printf("calling dlsym\n");
  118. }
  119. fn1 = (void(*)(void))dlsym(handle, "ResetSIGIO");
  120. if (fn1 == NULL) {
  121. fprintf(stderr, "%s\n", dlerror());
  122. exit(EXIT_FAILURE);
  123. }
  124. if (verbose) {
  125. printf("calling ResetSIGIO\n");
  126. }
  127. fn1();
  128. if (verbose) {
  129. printf("raising SIGIO\n");
  130. }
  131. if (raise(SIGIO) < 0) {
  132. die("raise");
  133. }
  134. if (verbose) {
  135. printf("calling SawSIGIO\n");
  136. }
  137. if (sawSIGIO()) {
  138. fprintf(stderr, "Go handler saw SIGIO after Reset\n");
  139. exit(EXIT_FAILURE);
  140. }
  141. if (verbose) {
  142. printf("waiting for sigioSeen\n");
  143. }
  144. // Wait until the signal has been delivered.
  145. i = 0;
  146. while (!sigioSeen) {
  147. ts.tv_sec = 0;
  148. ts.tv_nsec = 1000000;
  149. nanosleep(&ts, NULL);
  150. i++;
  151. if (i > 5000) {
  152. fprintf(stderr, "looping too long waiting for signal\n");
  153. exit(EXIT_FAILURE);
  154. }
  155. }
  156. printf("PASS\n");
  157. return 0;
  158. }