Using the third parameter (void * context) of the sigaction handler with SIG_INFO results in a segmentation error

I cut down the huge fiber scheduler code that was causing the problem for the lines below.
I expect this to be a pure return to the context passed to the handler every time.
What I get is a Handler. Printed three times and then segmentation error.

#include <ucontext.h> #include <signal.h> #include <stdio.h> ucontext_t currently_executed_context; void handler_sigusr1(int signum, siginfo_t* siginfo, void* context) { currently_executed_context = (*(ucontext_t*)context); printf("Handler. "); setcontext(&currently_executed_context); } int main() { setbuf(stdout,0); struct sigaction action_handler; action_handler.sa_sigaction = handler_sigusr1; action_handler.sa_flags = SA_SIGINFO; sigaction(SIGUSR1,&action_handler,NULL); for(;;) { kill(getpid(),SIGUSR1); sleep(1); } return 0; } 

Used by both gcc-4.4.3 and gcc-4.4.5 for two different Linux distributions.

+6
source share
1 answer

At this point, my own research into the problem can be provided as a partial answer.

Firstly, I found this article old and do not cite any official sources of information: http://zwillow.blogspot.com/2007/04/linux-signal-handling-is-broken.html . This is a current quote:

The second problem: you cannot use setcontext () to leave the signal handler and switch to another, previously saved context. (Or, for that matter, you cannot use it to return to the same context that is passed as an argument to the signal handler.) In other words, a signal handler, for example

 static void sighandler( int signo, siginfo_t *psi, void *pv) { memcpy(puc_old, pv, sizeof(ucontext_t)); /* choose another context to dispatch */ setcontext(puc_another); } 

does not work. It does not restore the signal mask specified in puc_other, does not restore the alternative signal stack, etc. However, this scheme works flawlessly on Solaris.

If someone can confirm the Solaris part, it will be appreciated.

Secondly, after talking with the teacher at the university, I realized that setting up / exchanging the context with the signal handler is not as straightforward as it happens in other situations. Unfortunately, the person who explained this to me could not provide more detailed information at that time.

Thus, both of my sources do not seem completely reliable, but, nevertheless, are clues.

+1
source

All Articles