0

Wrote simple program to learn writing POSIX style signal handling.

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

static void sig_int(int);

int
main(void)
{
    sigset_t    waitmask;

    if (signal(SIGINT, sig_int) == SIG_ERR) {
        printf("error occured\n");
        exit(1);
    }
    sigemptyset(&waitmask);
    sigaddset(&waitmask, SIGUSR1);
    if (sigsuspend(&waitmask) != -1) {
        printf("error\n");
        exit(1);
    }
}

static void
sig_int(int signo)
{
    sigset_t sigset;
    sigprocmask(0, NULL, &sigset);
    if (sigismember(&sigset, SIGINT))
        printf("SIGINT\n");
    if (sigismember(&sigset, SIGUSR1))
        printf("SIGUSR1\n");
}
  1. I create empty signal set.

  2. Then I add SIGUSR to the sigset.

  3. Install the signal handler

  4. When I launch the program and it pauses waiting for the signal, I generate SIGINT, the handler prints that SIGINT also is member of the sigset, that seems to be very strange behaviour.

Why sigismember thinks that SIGINT is member of sigset, although it was not added explicitly with sigaddset(&waitmask, SIGINT)?

1 Answers1

2

From man 2 signal:

If  the  disposition  is  set  to  a function, then first either the
disposition is reset to SIG_DFL,  or  the  signal  is  blocked  (see
Portability below), and then handler is called with argument signum.
If invocation of the handler caused the signal to be  blocked,  then
the signal is unblocked upon return from the handler.
muru
  • 207,228