Question

I have a strange behavior where manpage and google didn't help out.

In my code I want to block / unblock SIGINT when SIGUSR2 is sent. For this I install the signal handler and prepare the set for mask in a function:

void installSignallHandler(){

    sBlock.sa_handler = handleBlock;
    sigemptyset(&sBlock.sa_mask);
    sigaddset(&sBlock.sa_mask, SIGUSR2);
    sBlock.sa_flags = 0;
    sigaction(SIGUSR2, &sBlock, NULL);

    sigemptyset(&signals_protected);
    sigaddset(&signals_protected, SIGINT);

    // For some reason sigprocmask only works if it is called here the first time
    // sigprocmask(SIG_BLOCK, &signals_protected, NULL);
    // sigintBlocked = true;
}

Then if SIGUSR2 is sent this function is called:

void handleBlock(int signal){
    if(sigintBlocked){
        printf("Unblocking SIGINT ...\n");
        sigprocmask(SIG_UNBLOCK, &signals_protected, NULL);
        sigintBlocked = false;
    }
    else{
        printf("Blocking SIGINT ...\n");
        sigprocmask(SIG_BLOCK, &signals_protected, NULL);
        sigintBlocked = true;
    }
}

For testing I called it like this:

int main(int argc, char **argv) {
    installSignallHandler();
    while(1){
        printf("processing...\n");
        sleep(1);
    }
    return EXIT_SUCCESS;
}

Now the problem: The way I posted the code, sigprocmask takes no effect. But if I uncomment the two lines above, it works. So my two questions:

  1. Can you explain this behavior?
  2. What can I do to solve it? - I don't want to start with blocked signal.
Was it helpful?

Solution

because it is race-condition. set sigintBlocked in sig_handler and then do validation in main function if it is set then mask the signal.

this link has more information sigprocmask during signal's execution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top