kumar_m_kiran
kumar_m_kiran

Reputation: 4022

abort functionality implementation using signals

Below is the example of abort functionality implementation using signals as given in "Advanced Programming in Unix". Few doubts in the below code -


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 */ **STEP 1**

    /*
     * If we're here, process caught SIGABRT and returned.
     */
    fflush(NULL);               /* flush all open stdio streams */
    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 */ **STEP 2**
    exit(1);    /* this should never be executed ... */
}

Question
a. When we send the first kill with SIGABRT (marked by Step 1), why are we expecting the code to continue to next line? (see the comment - 'If we're here, process caught SIGABRT and returned' )

b. Why do we need to deliver kill signal again (in Step 2) and then exit(1) is not supposed to be hit. (refer comment in the code)

Upvotes: 3

Views: 645

Answers (1)

Most programs don't do anything particular with SIGABRT.

But some weird programs could install their own signal handler on SIGABRT, and the abort function should still work, even for them.

So most programs -those not catching SIGABRT- won't go past step 1 (because the default behavior for SIGABRT is to dump core, according to signal(7) ...).

The few programs who do catch SIGABRT will go till step 2 (if calling your abort). At that point, the default behavior of SIGABRT has been reinstalled. So the program dump core at step 2. And the final exit(1) cannot be reached.

Upvotes: 5

Related Questions