简体   繁体   English

sigprocmask不会恢复我的信号处理程序

[英]sigprocmask doesn't restore my signal handler

I have a problem with my code C Unix. 我的代码C Unix有问题。 I'll copy crucial parts: So after the first sigprocmask I send a signal, after the SIG_UNBLOCK, doesn't work the previous handle(gestisciSignalDopoReg) instead the standard handle manage my signal, so it simply terminate the process... What's wrong? 我将复制关键部分:所以在第一个sigprocmask之后我发送一个信号,在SIG_UNBLOCK之后,不能使用前一个句柄(gestisciSignalDopoReg)而不是标准句柄管理我的信号,所以它只是终止进程...出了什么问题? Thanks 谢谢

struct sigaction gestoreSegnale;
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask,SIGTERM);
sigaddset(&mask,SIGINT);
sigaddset(&mask,SIGALRM);
sigaddset(&mask,SIGQUIT);
sigaddset(&mask,SIGHUP);
sigaddset(&mask,SIGSEGV);
sigaddset(&mask,SIGILL);
sigaddset(&mask,SIGPIPE);
void setSegnali(int segn,__sighandler_t handler){
  gestoreSegnale.sa_handler=handler;
  gestoreSegnale.sa_mask=mask;

  sigaction(segn, &gestoreSegnale, NULL);
}
void eseguiSetSegnali(__sighandler_t handler){
  setSegnali(SIGQUIT, handler);
  setSegnali(SIGSEGV, handler);
  setSegnali(SIGILL, handler);
  setSegnali(SIGHUP, handler);
  setSegnali(SIGTERM, handler);
  setSegnali(SIGINT, handler);
}
void main(){
 eseguiSetSegnali(gestisciSIGNALDopoReg);
 sigprocmask(SIG_BLOCK,&mask,NULL);
 .........other part of code......... 
 sigprocmask(SIG_UNBLOCK,&mask,NULL);
}

please! 请! I need help! 我需要帮助!

You do not initialize the sa_flags field of your struct sigaction , so it may contain garbage values. 您没有初始化struct sigactionsa_flags字段,因此它可能包含垃圾值。 It is possible that the SA_RESETHAND bit is set, which according to the man page will give you this behavior: SA_RESETHAND位可能已设置,根据手册页将为您提供此行为:

  SA_RESETHAND Restore the signal action to the default state once the signal handler has been called. This flag is only meaningful when establishing a signal handler. SA_ONESHOT is an obsolete, non-standard synonym for this flag. 

If your signal handler has run once, that would cause it to be cleared and return to the default, so you may just need to zero out the flags like: 如果您的信号处理程序已运行一次,那么将导致它被清除并返回到默认值,因此您可能只需将标记清零,如:

void setSegnali(int segn,__sighandler_t handler){
  gestoreSegnale.sa_handler=handler;
  gestoreSegnale.sa_mask=mask;
  /* Make sure to clear flags */
  gestoreSegnale.sa_flags=0;

  sigaction(segn, &gestoreSegnale, NULL);
}

At any rate, the following little example works: 无论如何,以下小例子有效:

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

void handler(int sig) {
    printf("HUP %d\n", sig);
}

void main(){
    struct sigaction act;

    /* Block SIGHUP while the signal handler is running */
    sigemptyset(&act.sa_mask);
    sigaddset(&act.sa_mask,SIGHUP);

    /* Define handler for signal */
    act.sa_handler = handler;

    /* Clear flags */
    act.sa_flags = 0;
    act.sa_restorer = NULL;

    /* Install handler for SIGHUP */
    sigaction(SIGHUP, &act, NULL);

    /* Set mask to block SIGHUP */
    sigset_t mask;
    sigemptyset(&mask);
    sigaddset(&mask, SIGHUP);

    /* Block SIGHUP */
    printf("Blocking SIGHUP\n");
    sigprocmask(SIG_BLOCK,&mask,NULL);
    printf("Sleeping\n");
    sleep(60);
    /* Unblock SIGHUP */
    printf("Unblocking SIGHUP\n");
    sigprocmask(SIG_UNBLOCK,&mask,NULL);
    printf("Sleeping again\n");
    while(1)
        sleep(60);
}

If run, you see the desired result: 如果运行,您会看到所需的结果:

$ ./sigtest 
Blocking SIGHUP
Sleeping             <-- SIGHUP sent from another terminal here but blocked
Unblocking SIGHUP
HUP 1                <-- Signal sent earlier now delivered and handled
Sleeping again
HUP 1                <-- Further signals sent are all handled
HUP 1
HUP 1
^C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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