abort.c 3.5 KB

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