Blame stdlib/abort.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sigsetops.h>
Packit 6c4009
Packit 6c4009
/* Try to get a machine dependent instruction which will make the
Packit 6c4009
   program crash.  This is used in case everything else fails.  */
Packit 6c4009
#include <abort-instr.h>
Packit 6c4009
#ifndef ABORT_INSTRUCTION
Packit 6c4009
/* No such instruction is available.  */
Packit 6c4009
# define ABORT_INSTRUCTION
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Exported variable to locate abort message in core files etc.  */
Packit 6c4009
struct abort_msg_s *__abort_msg __attribute__ ((nocommon));
Packit 6c4009
libc_hidden_def (__abort_msg)
Packit 6c4009
Packit 6c4009
/* We must avoid to run in circles.  Therefore we remember how far we
Packit 6c4009
   already got.  */
Packit 6c4009
static int stage;
Packit 6c4009
Packit 6c4009
/* We should be prepared for multiple threads trying to run abort.  */
Packit 6c4009
__libc_lock_define_initialized_recursive (static, lock);
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Cause an abnormal program termination with core-dump.  */
Packit 6c4009
void
Packit 6c4009
abort (void)
Packit 6c4009
{
Packit 6c4009
  struct sigaction act;
Packit 6c4009
  sigset_t sigs;
Packit 6c4009
Packit 6c4009
  /* First acquire the lock.  */
Packit 6c4009
  __libc_lock_lock_recursive (lock);
Packit 6c4009
Packit 6c4009
  /* Now it's for sure we are alone.  But recursive calls are possible.  */
Packit 6c4009
Packit 6c4009
  /* Unblock SIGABRT.  */
Packit 6c4009
  if (stage == 0)
Packit 6c4009
    {
Packit 6c4009
      ++stage;
Packit 6c4009
      __sigemptyset (&sigs;;
Packit 6c4009
      __sigaddset (&sigs, SIGABRT);
Packit 6c4009
      __sigprocmask (SIG_UNBLOCK, &sigs, 0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Send signal which possibly calls a user handler.  */
Packit 6c4009
  if (stage == 1)
Packit 6c4009
    {
Packit 6c4009
      /* This stage is special: we must allow repeated calls of
Packit 6c4009
	 `abort' when a user defined handler for SIGABRT is installed.
Packit 6c4009
	 This is risky since the `raise' implementation might also
Packit 6c4009
	 fail but I don't see another possibility.  */
Packit 6c4009
      int save_stage = stage;
Packit 6c4009
Packit 6c4009
      stage = 0;
Packit 6c4009
      __libc_lock_unlock_recursive (lock);
Packit 6c4009
Packit 6c4009
      raise (SIGABRT);
Packit 6c4009
Packit 6c4009
      __libc_lock_lock_recursive (lock);
Packit 6c4009
      stage = save_stage + 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* There was a handler installed.  Now remove it.  */
Packit 6c4009
  if (stage == 2)
Packit 6c4009
    {
Packit 6c4009
      ++stage;
Packit 6c4009
      memset (&act, '\0', sizeof (struct sigaction));
Packit 6c4009
      act.sa_handler = SIG_DFL;
Packit 6c4009
      __sigfillset (&act.sa_mask);
Packit 6c4009
      act.sa_flags = 0;
Packit 6c4009
      __sigaction (SIGABRT, &act, NULL);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Try again.  */
Packit 6c4009
  if (stage == 3)
Packit 6c4009
    {
Packit 6c4009
      ++stage;
Packit 6c4009
      raise (SIGABRT);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now try to abort using the system specific command.  */
Packit 6c4009
  if (stage == 4)
Packit 6c4009
    {
Packit 6c4009
      ++stage;
Packit 6c4009
      ABORT_INSTRUCTION;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* If we can't signal ourselves and the abort instruction failed, exit.  */
Packit 6c4009
  if (stage == 5)
Packit 6c4009
    {
Packit 6c4009
      ++stage;
Packit 6c4009
      _exit (127);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* If even this fails try to use the provided instruction to crash
Packit 6c4009
     or otherwise make sure we never return.  */
Packit 6c4009
  while (1)
Packit 6c4009
    /* Try for ever and ever.  */
Packit 6c4009
    ABORT_INSTRUCTION;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (abort)