signal_unix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2012 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. // +build darwin dragonfly freebsd linux netbsd openbsd solaris
  5. #include <sys/time.h>
  6. #include "runtime.h"
  7. #include "defs.h"
  8. #include "signal_unix.h"
  9. extern SigTab runtime_sigtab[];
  10. void
  11. runtime_initsig(void)
  12. {
  13. int32 i;
  14. SigTab *t;
  15. // First call: basic setup.
  16. for(i = 0; runtime_sigtab[i].sig != -1; i++) {
  17. t = &runtime_sigtab[i];
  18. if((t->flags == 0) || (t->flags & SigDefault))
  19. continue;
  20. // For some signals, we respect an inherited SIG_IGN handler
  21. // rather than insist on installing our own default handler.
  22. // Even these signals can be fetched using the os/signal package.
  23. switch(t->sig) {
  24. case SIGHUP:
  25. case SIGINT:
  26. if(runtime_getsig(i) == GO_SIG_IGN) {
  27. t->flags = SigNotify | SigIgnored;
  28. continue;
  29. }
  30. }
  31. t->flags |= SigHandling;
  32. runtime_setsig(i, runtime_sighandler, true);
  33. }
  34. }
  35. void
  36. runtime_sigenable(uint32 sig)
  37. {
  38. int32 i;
  39. SigTab *t;
  40. t = nil;
  41. for(i = 0; runtime_sigtab[i].sig != -1; i++) {
  42. if(runtime_sigtab[i].sig == (int32)sig) {
  43. t = &runtime_sigtab[i];
  44. break;
  45. }
  46. }
  47. if(t == nil)
  48. return;
  49. if((t->flags & SigNotify) && !(t->flags & SigHandling)) {
  50. t->flags |= SigHandling;
  51. if(runtime_getsig(i) == GO_SIG_IGN)
  52. t->flags |= SigIgnored;
  53. runtime_setsig(i, runtime_sighandler, true);
  54. }
  55. }
  56. void
  57. runtime_sigdisable(uint32 sig)
  58. {
  59. int32 i;
  60. SigTab *t;
  61. t = nil;
  62. for(i = 0; runtime_sigtab[i].sig != -1; i++) {
  63. if(runtime_sigtab[i].sig == (int32)sig) {
  64. t = &runtime_sigtab[i];
  65. break;
  66. }
  67. }
  68. if(t == nil)
  69. return;
  70. if((t->flags & SigNotify) && (t->flags & SigHandling)) {
  71. t->flags &= ~SigHandling;
  72. if(t->flags & SigIgnored)
  73. runtime_setsig(i, GO_SIG_IGN, true);
  74. else
  75. runtime_setsig(i, GO_SIG_DFL, true);
  76. }
  77. }
  78. void
  79. runtime_resetcpuprofiler(int32 hz)
  80. {
  81. struct itimerval it;
  82. runtime_memclr((byte*)&it, sizeof it);
  83. if(hz == 0) {
  84. runtime_setitimer(ITIMER_PROF, &it, nil);
  85. } else {
  86. it.it_interval.tv_sec = 0;
  87. it.it_interval.tv_usec = 1000000 / hz;
  88. it.it_value = it.it_interval;
  89. runtime_setitimer(ITIMER_PROF, &it, nil);
  90. }
  91. runtime_m()->profilehz = hz;
  92. }
  93. void
  94. os_sigpipe(void)
  95. {
  96. int32 i;
  97. for(i = 0; runtime_sigtab[i].sig != -1; i++)
  98. if(runtime_sigtab[i].sig == SIGPIPE)
  99. break;
  100. runtime_setsig(i, GO_SIG_DFL, false);
  101. runtime_raise(SIGPIPE);
  102. }
  103. void
  104. runtime_unblocksignals(void)
  105. {
  106. sigset_t sigset_none;
  107. sigemptyset(&sigset_none);
  108. pthread_sigmask(SIG_SETMASK, &sigset_none, nil);
  109. }
  110. void
  111. runtime_crash(void)
  112. {
  113. int32 i;
  114. #ifdef GOOS_darwin
  115. // OS X core dumps are linear dumps of the mapped memory,
  116. // from the first virtual byte to the last, with zeros in the gaps.
  117. // Because of the way we arrange the address space on 64-bit systems,
  118. // this means the OS X core file will be >128 GB and even on a zippy
  119. // workstation can take OS X well over an hour to write (uninterruptible).
  120. // Save users from making that mistake.
  121. if(sizeof(void*) == 8)
  122. return;
  123. #endif
  124. runtime_unblocksignals();
  125. for(i = 0; runtime_sigtab[i].sig != -1; i++)
  126. if(runtime_sigtab[i].sig == SIGABRT)
  127. break;
  128. runtime_setsig(i, GO_SIG_DFL, false);
  129. runtime_raise(SIGABRT);
  130. }
  131. void
  132. runtime_raise(int32 sig)
  133. {
  134. raise(sig);
  135. }