7

i have written a small piece of code. This code first blocks the {SIGSEGV}, then adds SIGRTMIN to the same set. So, my final signal set is, {SIGSEGV,SIGRTMIN}. Thus, if i use SIG_UNBLOCK, as per my understanding, first SIGRTMIN should be unblocked, and then again if i invoke SIG_UNBLOCK, SIGSEGV should be unblocked.

That is, 1) {SIGSEGV,SIGRTMIN} 2) SIG_UNBLOCK = unblock SIGRTMIN, 3) Again invoke SIG_UNBLOCK = unblock SIGSEGV. I am giving the process a SIGRTMIN only, thus my second unblock should halt the process with SIGRTMIN. But it is not. Please help. N.B: Please don't give links to answers of other questions on sigprocmask( ), i have seen them and they don't clarify my question.

enter code here
#include <signal.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
 sigset_t old_set,new_set;
 sigemptyset(&old_set);
 sigemptyset(&new_set);

 if(sigaddset(&old_set,SIGSEGV)==0)
 {
   printf("sigaddset successfully added for SIGSEGV\n");
 }
 sigprocmask(SIG_BLOCK,&old_set,NULL); // SIGSEGV signal is masked
 kill(0,SIGSEGV);


 //*****************************************************************

 if(sigaddset(&new_set,SIGRTMIN)==0)
 {
  printf("sigaddset successfully added for SIGRTMIN\n");
 }
  sigprocmask(SIG_BLOCK,&new_set,&old_set); // SIGRTMIN signal is masked
 kill(0,SIGSEGV);

 //****************** Unblock one signal at a time ******************

 sigprocmask(SIG_UNBLOCK,&new_set,&old_set); // SIGRTMIN signal is unmasked
 sigprocmask(SIG_UNBLOCK,&new_set,&old_set); // SIGSEGV signal is unmasked

}

Output:
 [root@dhcppc0 signals]# ./a.out
  sigaddset successfully added for SIGSEGV
  sigaddset successfully added for SIGRTMIN
  (Note:SIGSEGV is not received even after sigprocmask(SIG_UNBLOCK,&new_set,&old_set); a second time)
RajSanpui
  • 11,556
  • 32
  • 79
  • 146

2 Answers2

13

Your premise is wrong. The whole set gets blocked and unblocked with a single call of sigprocmask.

Also, normally you would create a set containing every signal you want to block, then you would attempt to block them all with sigprocmask(SIG_BLOCK, pointer_to_sigset);.

Your code doesn't really unblock SIGSEGV though. Here's what i would write WITHOUT error handling, because it would make the snippet unnecessarily long. Check every function for errors though, the lists of possible errors are provided by man pages:

/* ... */
sigset_t signal_set; /* We don't need oldset in this program. You can add it,
                        but it's best to use different sigsets for the second
                        and third argument of sigprocmask. */
sigemptyset(&signal_set);

sigaddset(&signal_set, SIGSEGV);
sigaddset(&signal_set, SIGRTMIN);

/* now signal_set == {SIGSEGV, SIGRTMIN} */

sigprocmask(SIG_BLOCK, &signal_set, NULL): /* As i said, we don't bother with the 
                                              oldset argument. */

kill(0,SIGSEGV);  
kill(0,SIGSEGV);  /* SIGSEGV is not a realtime signal, so we can send it twice, but
                     it will be recieved just once */

sigprocmask(SIG_UNBLOCK, &signal_set, NULL); /* Again, don't bother with oldset */

/* SIGSEGV will be received here */

Of course, you might want to split blocking the signals into two operations on separate sets. The mechanism works like this: there is some set of blocked signals, which would replace oldset if you provided an oldset argument. You can add to that set with SIG_BLOCK, remove from that set with SIG_UNBLOCK, and change the whole set to your liking with SIG_SETMASK arguments of the sigprocmask function.

kubi
  • 313
  • 2
  • 8
  • @kubi: So why i am not receiving SIGSEGV? – RajSanpui Mar 13 '11 at 10:57
  • @kubi: No, i don't think the whole set gets unblocked at one shot. Check the link: http://stackoverflow.com/questions/25261/help-with-sigprocmask. And moreover, sigprocmask( ) itself does not block or unblock, it is the argument or first parameter which makesit do block, unblock or mask. So, please think before pointing me wrong. – RajSanpui Mar 13 '11 at 10:59
  • @kingsmasher1: How are you handling `SIGSEGV`? Could you paste a snippet of the signal handler code? Does the signal get sent correctly? Check the error codes of `sigprocmask` and `kill`. – kubi Mar 13 '11 at 11:05
  • @kingsmasher1: The manual page of `sigprocmask` is quite clear on the subject. Here's an excerpt: SIG_BLOCK - The set of blocked signals is the union of the current set and the set argument. SIG_UNBLOCK - The signals in set are removed from the current set of blocked signals. As we all know, set union and difference don't change one element at a time. – kubi Mar 13 '11 at 11:09
  • 1
    @kubi: I am not handling it, i expect the default handler todoit, and stop my code.Yes, the signal gets sent correctly, because if i comment-out the sigaddset code for SIGSEGV, the program halts with SIGSEGV received (behaves correctly) – RajSanpui Mar 13 '11 at 11:13
  • @kubi: Man page excerpt: "SIG_UNBLOCK The resulting set shall be the intersection of the current set and the complement of the signal set pointed to by set – RajSanpui Mar 13 '11 at 11:17
  • @kingsmasher1: Right, the intersection with the complement is exactly the same as set difference. But if you don't handle `SIGRTMIN` then the default handler may end the process before it gets the chance to receive `SIGSEGV`, are you handling `SIGRTMIN`? – kubi Mar 13 '11 at 11:24
  • No, please check the program...SIGRTMIN is not sent using the "kill" only SIGSEGV is sent kill(0,SIGSEGV). So if you say, that at one shot it unblocks, then in that case my program should receive SIGSEGV and halt. But it is not halting. This is my problem. What's your solution or root cause? – RajSanpui Mar 13 '11 at 11:29
  • @kingsmasher1: Oh, sorry about that. I see the problem with your code now. First you set oldset = {SIGSEGV}, then you set newset = {SIGRTMIN} (the first sigprocmask call donesn't modify the sets in this particular program, the second might modify oldset, but it doesn't matter). So when you unblock newset, it doesn't contain SIGSEGV, thus you don't really unblock SIGSEGV. Phew. – kubi Mar 13 '11 at 11:37
  • @kubi: Thank you so much, but i am a bit confused, can you please pin-point the part and tell me what to modify? Thanks in advance. – RajSanpui Mar 13 '11 at 11:40
  • @kingsmasher1: I will move the complete answer to the post above. – kubi Mar 13 '11 at 11:41
0

As kubi pointed out: Here is the modified code, problem is that, i messed up with old_set and new_set. The SIGSEGV was added in old_set, which was not unblocked, and hence i did not receive Segmentation fault (SIGSEGV signal). Thanks to kubi.

enter code here
#include <signal.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
 sigset_t old_set,new_set;
 sigemptyset(&old_set);
 sigemptyset(&new_set);

 if(sigaddset(&old_set,SIGSEGV)==0)
 {
  printf("sigaddset successfully added for SIGSEGV\n");
 }
 sigprocmask(SIG_BLOCK,&new_set,&old_set); // SIGSEGV signal is masked
 kill(0,SIGSEGV);


//*****************************************************************

if(sigaddset(&new_set,SIGRTMIN)==0)
{
  printf("sigaddset successfully added for SIGRTMIN\n");
}
if(sigprocmask(SIG_BLOCK,&new_set,&old_set)==-1) // SIGRTMIN signal is masked
{
  perror("sigprocmask");
}
kill(0,SIGSEGV);


//****************** Unblock all signals  ******************

if(sigprocmask(SIG_UNBLOCK,&new_set,&old_set)==-1) // SIGRTMIN signal is unmasked
 {
  perror("sigprocmask");
 }
}
RajSanpui
  • 11,556
  • 32
  • 79
  • 146