longjmp() out of nested signal handlers

Greg Comeau greg at csanta.UUCP
Thu Apr 7 03:30:56 AEST 1988


In article <4609 at june.cs.washington.edu> pardo at uw-june.UUCP (David Keppel) writes:
>
>According to both my Springer Verlag C book and to Chris Torek, the
>dpANS document says that longjmp() out of nested signal handlers is
>undefined.  I would like to know why this is; the reason is very
>non-obvious to me.

The problem that occurs with longjmp() is not within the function itself,
but with any side-effects that may occur because of the longjmp().
(This BTW, only happens to be spelled out in dpANSI, but has been true
even without that being said since its first implementations).  For instance,
within library functions that you don't have source code to (or even your own
routines that you do have source code to), a given routine may be setting an
external variable for later use or maybe let's say as a semaphore.  If a
longjump occurs after the setting of the variable but before it's put to any
use, then you're in trouble.

Another quite obtuse reason is that the corresponding setjmp() may be
called in a line of code where it was part of a subexpression.  Yickie
poo for that one!

And of course, their is the always problamatic code that longjmp's
back to a routine that had made use of some register variable, variable
that were made into registers by the compiler or jumping to a routine
that has already returned!

This is all about the dangers of longjmp in general though and does not
address nested signal handlers specifically, although this does present
problems for them.  Especially when it involves global variables or
ensuring that some event has completed before processing the second
signal (regardless of whether it is the same or not).

The main gist though is that a signal handler should do what it has to do
as quickly as possible.  Also of concern is the way signal are handled on
a given machine.  Whatever particular things it does to handle the interrupt
must be able to be reversable for normal return of the interrupt.
Allowing longjmp's to occur in a nested handler could make the signal
cleanup a real mess.



More information about the Comp.unix.wizards mailing list