Registering signal inside signal handler

Registering signal inside signal handler

Post by Luke » Sat, 11 Aug 2001 15:08:42



Hi,
        Is it safe to register for a signal inside a signal handler
function as in the following code segment? If not, what is the alternate
way?

void sighand(int signo)
{
  signal(SIGINT, sighandintr);
  return;

Quote:}

 
 
 

Registering signal inside signal handler

Post by Nithyanandha » Sat, 11 Aug 2001 15:35:54



> Hi,
>         Is it safe to register for a signal inside a signal handler
> function as in the following code segment? If not, what is the alternate
> way?

> void sighand(int signo)
> {
>   signal(SIGINT, sighandintr);

Never try to install a signal handler inside the handler function. Also use
signal() only when you want the handler as SIG_DFL or SIG_IGN.

It seems that you want to reinstall the handler as the handler that is
already installed is reset after the signal is delivered and handled. Go for
sigaction(). This is the posix way to establish the disposition of the
signal. You don't need to reinstall the handler again if you use
sigaction().

Quote:>   return;
> }

The above 'return' may interrupt the system calls in your code. So, don't
forget to check for EINTR in your code.

--

Nithyanand.
Siemens, Bangalore, India.
(Opinions expressed are my own and do not reflect the opinions of my
employer, Siemens)

 
 
 

Registering signal inside signal handler

Post by nos.. » Sat, 11 Aug 2001 21:18:34



>         Is it safe to register for a signal inside a signal handler
> function as in the following code segment? If not, what is the
> alternate way?

> void sighand(int signo)
> {
>   signal(SIGINT, sighandintr);
>   return;
> }

If you're using signal() to setup handlers, then you have to do this
in order to reset the signal in the handler. This creates a race
condition though, and you're better off using sigaction() for signal
handling.

In general, there are a number of functions which are safe to call
from within a signal handler. They're defined in the sigaction() man
page at

http://www.rdg.opengroup.org/onlinepubs/7908799/xsh/sigaction.html

Nevertheless, you're best off not doing much of anything in your
handler except setting a global sig_atomic_t variable that your main
loop can check.

Note that if you setup a handler for SIGFPE, SIGSEGV, or similar
signals, you should not return from the handler. No telling what will
happen if you do.

 
 
 

1. Signal handlers inside signal handlers

Greetings Netters,

Unfortunately, the project I'm working on requires I mess with nested signal
handlers, and I've checked out obvious manuals and the POSIX std for clues,
but I'm having no luck.

What I'm trying to do is within a signal handler, plant another handler.
For example

#include        blah blah blah

foo2( int signo )
{
        printf("Caught the second sigalrm\n");

foo1( int signo )
{
        printf("Caught the first sigalrm\n");
        signal( SIGARLM, foo2 );
        alarm(1);

        for (;;)
                ;

main()
{
        signal( SIGALRM, foo1 );
        alarm(3);

        /* Wait for the first alarm */
        sleep(10);

The above program when run, prints the message from function foo1
but never reaches foo2.  Note that my project dictates that I can't
exit foo2, until foo1 has run.
I've tried messing with posix signals (sigaction etc), but have the
same problem.

Has anyone tried to do this sort of thing before??

Thanks in advance,
Scott Wallace

2. Looking for AlsaPlayer help.

3. Registering client data on signal manager side andreceiving it in signal handler

4. US-CA-Los Angeles $$$ Killer Career Opportunities $$$

5. Signal handlers are not reset after signal delivery

6. Reg. Netscape Enterprise Server. (Urgent Help !!!!)

7. blocking signals in signal handler -- True or False quiz

8. RAID-1 and 2.2.9 revisited

9. signal inside signal

10. --- called from signal handler with signal -24242176 (SIG Unknown)

11. signal mask and signal handler

12. Setting a Signal handler to signal the input on a socket

13. signals, signal handlers, glibc reentrant?