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;
}