简体   繁体   中英

Can't set a signal mask in signal handler

I have a little problem: If I execute the code which is inside the handlerfunction in the main (instead of calling kill) the mask works proberly and blocks SIGINT. If I try to add the signal mask on the SIGUSR2 call, like in the code below, the mask doesn't mask any SIGINT signals. Whats the problem here?

  pid_t parent_pid;
  sigset_t mask;

    int main(void) {
        signal(SIGINT, handleSigint);
        signal(SIGUSR2, handleSigUSR2);
        signal(SIGUSR1, handleSigUSR1);
        sleep(1);
        kill(getpid(), SIGUSR2);
        while (1) {
        }

    }

void handleSigUSR2(int sig) {
    signal(SIGUSR2, handleSigUSR2);
    printf("StartUSR2\n");
    if (sigaddset(&mask, SIGINT) != 0) {
        printf("Error with addset\n");
    }
    if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) {
        printf("Error with sigprocmask\n");
    }
    printf("EndUSR2\n");

}

changes performed to sigprocmask, will be undone when then signal handler returns. I believe that setting sigprocmask inside a signal handler is not clearly defined. See the manpage for rt_sigreturn(2) regarding linux

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM