1

I am trying my hand on Linux Signals. Where I have created a scenario mentioned below:

  1. Initially block all SIGINT signals using sigprocmask().
  2. If sender send SIGUSR1 signal then unblock SIGINT for rest of the process life.

However first step is successfully implemented but not able to unblock (or change) process mask using sigprocmask().

What am I doing wrong?

#include<stdio.h>
#include<signal.h>
#include<stdlib.h>

sigset_t block_list, unblock_list;

void sigint_handler(int sig)
{
        printf("Ouch!!\n");
}

void sigusr1_handler(int sig)
{
        sigemptyset(&unblock_list);
        sigprocmask(SIG_SETMASK, &unblock_list, NULL);
}


int main(int argc, char **argv)
{
    int count;

    signal(SIGINT, &sigint_handler);
    signal(SIGUSR1, &sigusr1_handler);

    sigemptyset(&block_list);
    sigaddset(&block_list, SIGINT);
    sigprocmask(SIG_SETMASK, &block_list, NULL);

    for(count=1; ;++count)
    {
            printf("Process id: %ld\t%d\n", (long)getpid(), count);
            sleep(4);
    }

    exit(EXIT_SUCCESS);
    }

$kill -n SIGINT <pid>

$kill -n SIGUSER1 <pid> //This call should unblock sigint_handler() for rest of the process life, but it is only unblocking for one time. Everytime I have call $kill -n SIGUSER1 <pid> to unblock SIGINT.

Note: Error handling has been removed for simplicity.

  • You should never use signal(2), but always sigaction(2), and you should never call printf() (or any other non signal safe function) from a signal handler. – mosvy Sep 23 at 15:49
3

The kernel will restore the signal mask upon returning from a signal handler. This is specified by the standard:

When a thread's signal mask is changed in a signal-catching function that is installed by sigaction(), the restoration of the signal mask on return from the signal-catching function overrides that change (see sigaction()). If the signal-catching function was installed with signal(), it is unspecified whether this occurs.

On Linux, signal(2) is just a deprecated compatibily wrapper for sigaction(2), and that does also occur when using signal(2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.