Is it possible to ignore all signals?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 02:57:33

问题


I have a server application which I want to protect from being stopped by any signal which I can ignore. Is there a way to ignore all possible signals at once, without setting them one by one?


回答1:


Yes:

#include <signal.h>

sigset_t mask;
sigfillset(&mask);
sigprocmask(SIG_SETMASK, &mask, NULL);

This does not exactly ignore the signals, but blocks them; which in practice is the same effect.

I guess there's no need to mention that SIGKILL and SIGSTOP cannot be blocked nor ignored in any way.

For more detailed semantics, like mask inheritance rules and the like, check the man page




回答2:


Blocking signals is NOT the same as ignoring them.

When you block signals as suggested by C2H5OH, it gets added to a pending signal queue and will be delivered to the process as soon as you unblock it.

Unblocking can be done using

#include <signal.h>

sigset_t mask;
sigemptyset(&mask);
sigprocmask(SIG_SETMASK, &mask, NULL);

To answer your question on how to ignore signals, it has to be handled by a Signal Handler which is a user-defined function which executes whenever a signal is delivered to the process

static void foo (int bar)
{
     /*some code here. In your case, nothing*/
}

then register this function by using

signal(SIGINT,foo);   //or whatever signal you want to ignore

If you want to ignore all signals

int i;
for(i = 1; i <=31 ; i++)
{
   signal(i,foo);
}

This code will take all the signals delivered to the process and ignore them instead of blocking them.

NOTE: According to man pages , it is not the recommended way, instead sigaction is suggested. Do check out man sigaction




回答3:


Solutions based on sigprocmask() and pthread_sigmask() have not worked for me. Here's what I found to work:

#include <signal.h>
#include <unistd.h>
#include <assert.h>
int main() {
    struct sigaction act;
    act.sa_handler = SIG_IGN;
    for(int i = 1 ; i < 65 ; i++) {
        printf("i = %d\n", i);
        // 9 and 19 cannot be caught or ignored                                                                                                       
        // 32 and 33 do not exist                                                                                                                     
        if((i != SIGKILL) && (i != SIGSTOP) && (i != 32) && (i != 33)) {
            assert(sigaction(i, &act, NULL) == 0);
        }
    }
    sleep(10000);
    return 0;
}


来源:https://stackoverflow.com/questions/10046916/is-it-possible-to-ignore-all-signals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!