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