简体   繁体   中英

Will this code block all signals to spawned thread but still catch some in MAIN()?

I wrote a similar question at How to block all SIGNALS in thread WITHOUT using SIGWAIT? but must admit I still need 100% clarity on the topic...C is not my day job ;-) Sorry for the similar question....

All i need 100% clarification on is:

  • I want to block all signals from going to the thread I create, but I want to catch the signals mentioned below in the MAIN thread, hence the SIG_UNBLOCK after the thread creation.

  • Also very important is to prevent any interruptions to libraries I have no control over from being "disturbed" during ie a SIGINT. I have a situation where a WAIT step is happening in a thread on a "message GET from a queue". That wait step seems to reject SIGINT even though the signal handler is defined in main, hence the SA_RESTART below.

Could you please let me know if the code below will accomplish this? I am pretty sure it is ok.

Thanks for the help, much appreciated

Lynton

The following is a snippet of the MAIN program:

int main(int argc, char * argv[]){
    sigset_t set;
    struct sigaction sa_shutdown;
    struct sigaction sa_error;  

    //Shutdown hook for CTRL-C    
    sa_shutdown.sa_handler = shutdownHook;
    sigemptyset(&sa_shutdown.sa_mask);
    sa_shutdown.sa_flags = SA_RESTART;
    sigaction(SIGINT, &sa_shutdown, NULL);        
    //Error handlers for erroneous signals
    sa_error.sa_handler = signalErrorHandler;
    sigemptyset(&sa_error.sa_mask);
    sa_error.sa_flags = SA_RESTART;
    sigaction(SIGSEGV, &sa_error, NULL);    
    sigaction(SIGBUS, &sa_error, NULL); 
    sigaction(SIGILL, &sa_error, NULL); 
    sigaction(SIGTERM, &sa_error, NULL); 
    sigaction(SIGABRT, &sa_error, NULL);    

    //BLOCK all SIGNALS in threads  
    sigfillset(&set);
    rc = pthread_sigmask(SIG_BLOCK, &set, NULL);    
    if(rc != 0){
        printf("Thread sigmask failed\n");
        exit(1);
    }
    rc = pthread_create(&outboundThread, NULL, outboundThreadMainLoop, (void *)argv);
    if(rc != 0){
        printf("Thread creation failed\n");
        exit(1);
    }
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
    pthread_join(outboundThread, NULL); 
    return 0;
}

Regarding to block all signals, your code looks OK to me.

Anyway I'd say you should not block SIGFPE, SIGILL, SIGSEGV, or SIGBUS.

pthread_sigmask() 's man page says: " If any of the SIGFPE, SIGILL, SIGSEGV, or SIGBUS signals are generated while they are blocked, the result is undefined, unless the signal was generated by the kill() function, the sigqueue() function, or the raise() function. "

Regarding " ... interruptions to libraries ... ": If your are referring to any call into libraries from a thread created the way you did in your example you, I'd also say that's OK, they won't be interrupted.

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