根据abort()在manpage中的描述:
The abort() first unblocks the SIGABRT signal, and then raises that signal for the calling process. This results in the abnormal termination of the process unless the SIGABRT signal is caught and the signal handler does not return (see longjmp(3)).
If the abort() function causes process termination, all open streams are closed and flushed.
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.
#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include "utils.h"
#define USE_SIG_LONG_JMP
static sigjmp_buf senv;
void handler(int sig)
{
printf("In the handler.\n");
#ifdef USE_SIG_LONG_JMP
siglongjmp(senv, 1);
printf("Never see this.\n");
#endif
}
void m_abort(void)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGABRT);
sigprocmask(SIG_UNBLOCK, &set, NULL);
printf("m_abort: raise first SIGABRT.\n");
raise(SIGABRT);
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;
sa.sa_handler = SIG_DFL;
sigaction(SIGABRT, &sa, NULL);
printf("m_abort: raise second SIGABRT.\n");
raise(SIGABRT);
}
int main(void)
{
printf("Now call the abort.\n");
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handler;
sigaction(SIGABRT, &sa, NULL);
#ifdef USE_SIG_LONG_JMP
if (sigsetjmp(senv, 1) == 0)
m_abort();
else
printf("abort failed.\n");
#else
m_abort();
printf("abort failed.\n");
#endif
return 0;
}
转载于:https://blog.51cto.com/study0tlpi/1544915