日期:2014-05-16  浏览次数:21066 次

linux abort函数的实现
各位大虾:
  看这段代码时,不明白牙。具体请看下面代码:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
abort(void) /* POSIX-style abort() function */
{
  sigset_t mask;
  struct sigaction action;

  /*
  * Caller can't ignore SIGABRT, if so reset to default.
  */
  sigaction(SIGABRT, NULL, &action);
  if (action.sa_handler == SIG_IGN) {
  action.sa_handler = SIG_DFL;
  sigaction(SIGABRT, &action, NULL);
  }
  if (action.sa_handler == SIG_DFL)
  fflush(NULL); /* flush all open stdio streams */

  /*
  * Caller can't block SIGABRT; make sure it's unblocked.
  */
  sigfillset(&mask);
  sigdelset(&mask, SIGABRT); /* mask has only SIGABRT turned off */
  sigprocmask(SIG_SETMASK, &mask, NULL);
  kill(getpid(), SIGABRT); /* send the signal */

  /*
  * If we're here, process caught SIGABRT and returned.
  */
  fflush(NULL); /* flush all open stdio streams */
  /*为什么这里要重新设置信号处理函数并再次发送SIGABRT信号呢?为什么不直接exit*/
  action.sa_handler = SIG_DFL;
  sigaction(SIGABRT, &action, NULL); /* reset to default */
  sigprocmask(SIG_SETMASK, &mask, NULL); /* just in case ... */
  kill(getpid(), SIGABRT); /* and one more time */
  exit(1); /* this should never be executed ... */
}

------解决方案--------------------
abort() 通常伴随core文件产生
如果直接exit,不会产生core文件