abort.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA. */
  15. /* Hacked up for uClibc by Erik Andersen */
  16. #define sigaction __sigaction
  17. #define _GNU_SOURCE
  18. #include <features.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <signal.h>
  25. #include <errno.h>
  26. /* Our last ditch effort to commit suicide */
  27. #if defined(__alpha__)
  28. #define ABORT_INSTRUCTION asm ("call_pal 0")
  29. #elif defined(__arm__)
  30. #define ABORT_INSTRUCTION asm ("bl abort")
  31. #elif defined(__hppa__)
  32. #define ABORT_INSTRUCTION asm ("iitlbp %r0,(%r0)")
  33. #elif defined(__i386__)
  34. #define ABORT_INSTRUCTION asm ("hlt")
  35. #elif defined(__ia64__)
  36. #define ABORT_INSTRUCTION asm ("break 0")
  37. #elif defined(__m68k__)
  38. #define ABORT_INSTRUCTION asm ("illegal")
  39. #elif defined(__mc68000__)
  40. #define ABORT_INSTRUCTION asm (".long 0xffffffff")
  41. #elif defined(__mips__)
  42. #define ABORT_INSTRUCTION asm ("break 255")
  43. #elif defined(__powerpc__)
  44. #define ABORT_INSTRUCTION asm (".long 0")
  45. #elif defined(__s390__)
  46. #define ABORT_INSTRUCTION asm (".word 0")
  47. #elif defined(__sparc__)
  48. #define ABORT_INSTRUCTION asm ("unimp 0xf00")
  49. #elif defined(__SH5__)
  50. #define ABORT_INSTRUCTION asm ("movi 0x10, r9; shori 0xff, r9; trapa r9")
  51. #elif defined(__sh2__)
  52. #define ABORT_INSTRUCTION asm ("trapa #32")
  53. #elif defined(__sh__)
  54. #define ABORT_INSTRUCTION asm ("trapa #0xff")
  55. #elif defined(__x86_64__)
  56. #define ABORT_INSTRUCTION asm ("hlt")
  57. #else
  58. #define ABORT_INSTRUCTION
  59. #warning no abort instruction define for your arch
  60. #endif
  61. #ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
  62. extern void weak_function _stdio_term(void);
  63. #endif
  64. extern void _exit __P((int __status)) __attribute__ ((__noreturn__));
  65. static int been_there_done_that = 0;
  66. /* Be prepared in case multiple threads try to abort() */
  67. #ifdef __UCLIBC_HAS_THREADS__
  68. # include <pthread.h>
  69. static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  70. #endif
  71. #define LOCK __pthread_mutex_lock(&mylock)
  72. #define UNLOCK __pthread_mutex_unlock(&mylock)
  73. extern int __raise (int __sig) __THROW attribute_hidden;
  74. /* Cause an abnormal program termination with core-dump */
  75. void abort(void)
  76. {
  77. sigset_t sigset;
  78. /* Make sure we acquire the lock before proceeding */
  79. LOCK;
  80. /* Unmask SIGABRT to be sure we can get it */
  81. if (__sigemptyset(&sigset) == 0 && __sigaddset(&sigset, SIGABRT) == 0) {
  82. __sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *) NULL);
  83. }
  84. while (1) {
  85. /* Try to suicide with a SIGABRT */
  86. if (been_there_done_that == 0) {
  87. been_there_done_that++;
  88. #ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
  89. /* If we are using stdio, try to shut it down. At the very least,
  90. * this will attemt to commit all buffered writes. It may also
  91. * unboffer all writable files, or close them outright.
  92. * Check the stdio routines for details. */
  93. if (_stdio_term) {
  94. _stdio_term();
  95. }
  96. #endif
  97. abort_it:
  98. UNLOCK;
  99. __raise(SIGABRT);
  100. LOCK;
  101. }
  102. /* Still here? Try to remove any signal handlers */
  103. if (been_there_done_that == 1) {
  104. struct sigaction act;
  105. been_there_done_that++;
  106. __memset(&act, '\0', sizeof(struct sigaction));
  107. act.sa_handler = SIG_DFL;
  108. __sigfillset(&act.sa_mask);
  109. act.sa_flags = 0;
  110. sigaction(SIGABRT, &act, NULL);
  111. goto abort_it;
  112. }
  113. /* Still here? Try to suicide with an illegal instruction */
  114. if (been_there_done_that == 2) {
  115. been_there_done_that++;
  116. ABORT_INSTRUCTION;
  117. }
  118. /* Still here? Try to at least exit */
  119. if (been_there_done_that == 3) {
  120. been_there_done_that++;
  121. _exit(127);
  122. }
  123. /* Still here? We're screwed. Sleepy time. Good night. */
  124. while (1)
  125. /* Try for ever and ever */
  126. ABORT_INSTRUCTION;
  127. }
  128. }