我为 SIGABRT
信号添加了一个 sighandler。
abort()
的默认行为是生成核心转储。
我想在信号处理程序中做同样的事情,但在崩溃之前,执行清理代码。
我试过如下,我知道下面的代码是行不通的。
static void
proc_sigabort_handler (int signo UNUSED)
{
/* cleanup */
abort(); /* This is to coredump */
}
signal(SIGABRT, proc_sigabort_handler);
问题:
sighandler 中的 abort()
调用会引发 SIGABRT
信号并以相同的 sighandler 函数结束并继续下去。
是否有类似abort()
的系统调用来生成核心转储?
一些回复后编辑:
user@srv1 ~/linux> uname -a
Linux srv1 2.4.21-63.ELsmp #1 SMP Wed Oct 28 23:15:46 EDT 2009 i686 i686 i386 GNU/Linux
Linux 中止 (3) 手册页:
If the SIGABRT signal is ignored, or caught by a handler that returns, the abort() function will still terminate the process. It does this by restoring the default disposition for SIGABRT and then raising the signal for a second time.
例如:
void mysigabort()
{
printf("I caught the SIGABRT signal!\n");/* I know that printf should be avoided */
return;
}
int main()
{
signal(SIGABRT, mysigabort);
while(1); /* infinite loop */
exit(0);
}
产生:
user@srv1 ~/linux> ./a.out
I caught the SIGABRT signal!
<cursor>
user@srv1 ~/linux> kill -6 25208
未找到核心。即使进程没有终止。
如果没有用户定义的信号处理程序,则生成核心。
最佳答案
您可以在第二次捕获之前恢复默认的 SIGABRT 行为:
void mysigabort(int signum)
{
// whatever you want
signal(signum, SIG_DFL);
kill(getpid(), signum); // or abort() ?
}
关于linux - SIGABRT 处理程序。在崩溃前做一些清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23736394/