Search code examples
csignalssigaction

sigaction handle signal just once


Is there a way to catch a signal just once with sigaction structure and function? To be more specific, I would like to simply reset to default a specific signal (SIGINT). Is it possible to achieve this in a handler?

Edit

So, something like this would be correct:

void sig_int(int sig)
{
    printf(" -> Ctrl-C\n");
    struct sigaction act;
    act.sa_handler = SIG_DFL;

    if(sigaction(SIGINT, &act, NULL) < 0)
    {
        exit(-1);
    }

}

int main()
{
    struct sigaction act;
    act.sa_handler = sig_int;

    if(sigaction(SIGINT, &act, NULL) < 0)
    {
        exit(-1);
    }

    while(1)
    {
        sleep(1);
    }

    return 0;   
}

Solution

  • The standard SA_RESETHAND flag, set in the sa_flags member of struct sigaction, does precisely this.

    Set that flag when specifying your SIGINT handler, and the handler will be reset to SIG_DFL upon entry.