Re: unsafe functions from signal handler
- From: Andrei Voropaev <avorop@xxxxxxx>
- Date: Tue, 31 Jan 2006 10:01:07 +0000 (UTC)
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
.
- References:
- unsafe functions from signal handler
- From: bill pursell
- unsafe functions from signal handler
- Prev by Date: unsafe functions from signal handler
- Next by Date: Re: Newbie - Shared library
- Previous by thread: unsafe functions from signal handler
- Next by thread: Re: unsafe functions from signal handler
- Index(es):
Relevant Pages
|