main5.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 for verifying that the Go runtime properly forwards
  5. // signals when non-Go signals are raised.
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/time.h>
  11. #include <sys/select.h>
  12. #include "libgo2.h"
  13. int *nilp;
  14. int main(int argc, char** argv) {
  15. int verbose;
  16. int test;
  17. if (argc < 2) {
  18. printf("Missing argument\n");
  19. return 1;
  20. }
  21. test = atoi(argv[1]);
  22. verbose = (argc > 2);
  23. Noop();
  24. switch (test) {
  25. case 1: {
  26. if (verbose) {
  27. printf("attempting segfault\n");
  28. }
  29. *nilp = 0;
  30. break;
  31. }
  32. case 2: {
  33. struct timeval tv;
  34. if (verbose) {
  35. printf("attempting external signal test\n");
  36. }
  37. fprintf(stderr, "OK\n");
  38. fflush(stderr);
  39. // The program should be interrupted before
  40. // this sleep finishes. We use select rather
  41. // than sleep because in older versions of
  42. // glibc the sleep function does some signal
  43. // fiddling to handle SIGCHLD. If this
  44. // program is fiddling signals just when the
  45. // test program sends the signal, the signal
  46. // may be delivered to a Go thread which will
  47. // break this test.
  48. tv.tv_sec = 60;
  49. tv.tv_usec = 0;
  50. select(0, NULL, NULL, NULL, &tv);
  51. break;
  52. }
  53. case 3: {
  54. if (verbose) {
  55. printf("attempting SIGPIPE\n");
  56. }
  57. int fd[2];
  58. if (pipe(fd) != 0) {
  59. printf("pipe(2) failed\n");
  60. return 0;
  61. }
  62. // Close the reading end.
  63. close(fd[0]);
  64. // Expect that write(2) fails (EPIPE)
  65. if (write(fd[1], "some data", 9) != -1) {
  66. printf("write(2) unexpectedly succeeded\n");
  67. return 0;
  68. }
  69. printf("did not receive SIGPIPE\n");
  70. return 0;
  71. }
  72. case 4: {
  73. fprintf(stderr, "OK\n");
  74. fflush(stderr);
  75. if (verbose) {
  76. printf("calling Block\n");
  77. }
  78. Block();
  79. }
  80. default:
  81. printf("Unknown test: %d\n", test);
  82. return 0;
  83. }
  84. printf("FAIL\n");
  85. return 0;
  86. }