Printing in signal handlers (more on ( alarm(1) == alarm(INFINITY))?

Geoff Collyer geoff at utcs.UUCP
Thu Mar 21 16:23:34 AEST 1985


In article <2175 at wateng.UUCP> ksbszabo at wateng.UUCP (Kevin Szabo) writes:
>The problem is that practically none of STDIO is re-entrant. The only
>thing you can trust in a signal handler is a direct call to the kernel.

I think the real problem is that UNIX allows one to catch signals at all.
Given that we don't have cheap processes a la Thoth, catching signals is a
necessary evil but signal handlers should be kept simple and in particular
should avoid using longjmp (if you have ever read and understood the source,
you won't want to use longjmp).  At most, setting a global flag often
suffices; this also may allow one to omit calls to signal to enable
and disable signal catching around critical sections by simply noting
the state of the flag at appropriate places.

I can't speak for all UNIXes, but the system call interface routines
in the PDP-11 v7 C library often aren't re-entrant either: they copy
their arguments into static storage, then issue the system call pointing
at the static storage.  If you interrupt such a routine (say signal(2))
while it is copying its arguments and in the signal catcher you invoke
the same system call, then return from the signal catcher without calling
longjmp, the first instance of signal will continue to execute, though
some of its static storage was trashed by the second call in the signal
handler.  So if you are going to make those direct calls to the kernel
in your signal handler, you'd better do it from assembler or verify
that your C library system call interfaces are re-entrant.

This is something that I believe Whitesmith's got right:
last time I looked they had a standard routine for issuing system calls
that left the arguments on the stack and made the system call point
at the stacked arguments.



More information about the Comp.unix.wizards mailing list