Search code examples
c++clinuxsignalssignal-handling

Is there any way to prevent a user from registering/using his own signal handler and always use a particlar handler?


My requirement is: I have a signal handler in my tool, which is registered and used between some particular interval (i am using timer).

Now this signal handler should NOT allow any other handler to be registered after this handler is once registered. (But this restriction is only for a short duration, means after that duration the user is free to invoke his own handler)

Is there any way to accomplish this?

 sigset_t mask;
 struct sigaction sa;
 printf("Establishing handler for signal %d\n", SIG);
 sa.sa_flags = SA_SIGINFO;
 sa.sa_sigaction = handler; // This handler should override all handlers
 sigaction(SIG, &sa, NULL);
 sev.sigev_notify = SIGEV_SIGNAL;
 sev.sigev_signo = SIGUSR1;

Note: My tool is actually written in C++, but the concepts are so close and since more people are familiar with it, i am putting a C tag too, with C++ Please feel free to ask for more clarifications (if you need)


Solution

  • This can be achieved, and in fact i have done that by writing a sigaction wrapper around the actual one, and using the dlsym and RTLD_NEXT technique.

    Here is the code snippet of my wrapper:

    enter code here
    int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
    {
    
     struct sigaction sa;
     printf("My sigaction called\n");
    
     if((Timerflag==1)&&(sig==SIGUSR1))
     {
       sa.sa_sigaction=my_handler;
       sa.sa_flags=0;
    
       return LT_SIGACTION(sig,&sa,NULL);
      }
      else if(Timerflag==0)
        return LT_SIGACTION(sig, act, oact);  
    }
    

    Finally, i think everyone knows, how to get the libc handle using dlsym :-) Unfortunately, nobody could give me this idea in "stackoverflow".