abort.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <features.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <signal.h>
  23. #include <errno.h>
  24. libc_hidden_proto(abort)
  25. libc_hidden_proto(memset)
  26. libc_hidden_proto(sigaction)
  27. libc_hidden_proto(sigprocmask)
  28. libc_hidden_proto(raise)
  29. libc_hidden_proto(_exit)
  30. /* Our last ditch effort to commit suicide */
  31. #ifdef __UCLIBC_ABORT_INSTRUCTION__
  32. # define ABORT_INSTRUCTION __asm__(__UCLIBC_ABORT_INSTRUCTION__)
  33. #else
  34. # define ABORT_INSTRUCTION
  35. # warning "no abort instruction defined for your arch"
  36. #endif
  37. #ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
  38. extern void weak_function _stdio_term(void) attribute_hidden;
  39. #endif
  40. static int been_there_done_that = 0;
  41. /* Be prepared in case multiple threads try to abort() */
  42. #include <bits/uClibc_mutex.h>
  43. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  44. /* Cause an abnormal program termination with core-dump */
  45. void abort(void)
  46. {
  47. sigset_t sigs;
  48. /* Make sure we acquire the lock before proceeding */
  49. __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
  50. /* Unmask SIGABRT to be sure we can get it */
  51. if (__sigemptyset(&sigs) == 0 && __sigaddset(&sigs, SIGABRT) == 0) {
  52. sigprocmask(SIG_UNBLOCK, &sigs, (sigset_t *) NULL);
  53. }
  54. while (1) {
  55. /* Try to suicide with a SIGABRT */
  56. if (been_there_done_that == 0) {
  57. been_there_done_that++;
  58. #ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
  59. /* If we are using stdio, try to shut it down. At the very least,
  60. * this will attempt to commit all buffered writes. It may also
  61. * unbuffer all writable files, or close them outright.
  62. * Check the stdio routines for details. */
  63. if (_stdio_term) {
  64. _stdio_term();
  65. }
  66. #endif
  67. abort_it:
  68. __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(mylock);
  69. raise(SIGABRT);
  70. __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
  71. }
  72. /* Still here? Try to remove any signal handlers */
  73. if (been_there_done_that == 1) {
  74. struct sigaction act;
  75. been_there_done_that++;
  76. memset(&act, '\0', sizeof(struct sigaction));
  77. act.sa_handler = SIG_DFL;
  78. __sigfillset(&act.sa_mask);
  79. act.sa_flags = 0;
  80. sigaction(SIGABRT, &act, NULL);
  81. goto abort_it;
  82. }
  83. /* Still here? Try to suicide with an illegal instruction */
  84. if (been_there_done_that == 2) {
  85. been_there_done_that++;
  86. ABORT_INSTRUCTION;
  87. }
  88. /* Still here? Try to at least exit */
  89. if (been_there_done_that == 3) {
  90. been_there_done_that++;
  91. _exit(127);
  92. }
  93. /* Still here? We're screwed. Sleepy time. Good night. */
  94. while (1)
  95. /* Try for ever and ever */
  96. ABORT_INSTRUCTION;
  97. }
  98. }
  99. libc_hidden_def(abort)