Re: unsafe functions from signal handler



On 2006-01-31, bill pursell <bill.pursell@xxxxxxxxx> wrote:
> I came upon the following code yesterday in a signal handler:
>
> fflush(stderr);
> fprintf(stderr, "...%d\n", signal);
> fflush(stderr);
> /* snip */
> exit(-1);
>
> This brought to mind several questions:
>
> 1. fprintf is not a safe function, so it is a bad idea to
> call it from within a signal handler. However, it strikes
> me that the only problem with it is that the writes to stderr
> will be interleaved and munged a bit. Is there a more
> substantial problem? Also, does it matter if the signal

IIRC, the actual problem is in the fact that these functions use memory
management and thus may lead to segfault when called at wrong time.

> disposition is set with signal() or with sigaction()? In
> either case, I would not call fprintf from the signal handler,
> but there seems to be a subtlety that I'm missing. In particular,
> if the signal is SIGRTMIN or greater, the signals are reliable,
> so it would be safe to make the call; is that correct? I apologize

No. Reliable signal does not mean that it is synchronous signal :)
Also signal and sigaction lead to the same behaviour.

[...]


>
> 2. The fflush calls are totally pointless, as stderr is
> unbuffered. Am I missing something here, and am I

Wrong. It is unbuffered only on the startup of the program (usually).
But the program may reopen stderr to some file and then the buffering
type might be changed. The buffering type might be changed even without
reopening, simply by calling setvbuf function.

[...]


--
Minds, like parachutes, function best when open
.



Relevant Pages

  • Re: FINALLY THE TRUMAN PROBE GOES PUBLIC
    ... The mind doesnt think using electrical signals. ... Although it can learn to output signals to recievers. ... Let alone decipher. ...
    (sci.math)
  • Re: Conversation with God
    ... >On Fri, 26 Aug 2005, William wrote: ... >> And you can read the signals. ... >would not be right to pray for his recovery. ... something that has arrived in your mind that I can test in the real ...
    (uk.religion.christian)
  • Re: FINALLY THE TRUMAN PROBE GOES PUBLIC
    ... >>The mind doesnt think using electrical signals. ... >>Although it can learn to output signals to recievers. ... |-|erc watches movie called Truman Show ...
    (sci.math)
  • Re: Conversation with God
    ... > They are the same kinds of signals I read from my wife or friends. ... >>would not be right to pray for his recovery. ... > something that has arrived in your mind that I can test in the real ...
    (uk.religion.christian)
  • Re: OT President Directly Involved In CIA Blown Cover?
    ... >how long the propagation of a post takes from a sender to the majority ... >I had in mind a real time comparison of signals at wherever the listener ...
    (rec.radio.shortwave)