Subject: Re: save/restore fpu for the signal handler call
To: None <port-i386@netbsd.org>
From: Christos Zoulas <christos@astron.com>
List: port-i386
Date: 11/30/2005 21:13:55
In article <20051130210451.GB588@beta.martani.n2.repy.czf>,
Pavel Cahyna  <pcah8322@artax.karlin.mff.cuni.cz> wrote:
>On Wed, Nov 30, 2005 at 08:54:55PM +0000, Rui Paulo wrote:
>> On 2005.11.30 21:36:06 +0100, Pavel Cahyna wrote:
>> | On Wed, Nov 30, 2005 at 09:16:53PM +0100, Jaromir Dolecek wrote:
>> | > Except that printf() isn't :) You cannot safely access
>> | > stdio within signal handler.
>> | 
>> | Why not?
>> 
>> signal(7):
>> 
>>      A function that is async-signal-safe is either reentrant or non-inter-
>>      ruptible by signals.  This means that they can be used in signal handlers
>>      and in the child of threaded programs after doing fork(2).
>> 
>>      The following functions are async-signal-safe.  Any function not listed
>>      below is unsafe to use in signal handlers.
>
>Thanks for the reference. That is IMHO a bug, because POSIX says :
>
>"All functions not in the above table are considered to be unsafe with
>respect to signals. In the presence of signals, all functions defined by
>this volume of IEEE Std 1003.1-2001 shall behave as defined when called
>from or interrupted by a signal-catching function, with a single
>exception: when a signal interrupts an unsafe function and the
>signal-catching function calls an unsafe function, the behavior is
>undefined."
>
>So it should be perfectly legal to call printf() from a signal handler
>which is carefully blocked around calls to unsafe functions, because this
>does not fall in the exception above, no?

It is not legal. Each invocation of the "unsafe" function needs to
be protected from being interrupted by a signal for this to work.
I.e.  you are in the middle of a printf, you get a signal, you end
up in the signal handler, you call your protected printf that
trashes the internal stdio state. Unfortunately you cannot predict
which "unsafe" function might call another "unsafe" function, so
you'll have to protect all of them.

christos