I have reduced a huge fiber scheduler code that was producing the problem to the lines below.
What I expect is a clean return to the context, passed to the handler, every time.
What I get is "Handler. " printed out three times and then a Segmentation Fault.
#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(¤tly_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 both gcc-4.4.3 and gcc-4.4.5 on two different Linux distributions.