This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: determine whether code is running in a signal handler context
- From: Carlos O'Donell <carlos at redhat dot com>
- To: Yubin Ruan <ablacktshirt at gmail dot com>, libc-help at sourceware dot org
- Date: Wed, 18 Oct 2017 11:34:20 -0700
- Subject: Re: determine whether code is running in a signal handler context
- Authentication-results: sourceware.org; auth=none
- References: <CAJYFCiPkMNCr0X61AQ9MqhXyagOb0k4tTp5mRAD171VLkedVYQ@mail.gmail.com>
On 10/18/2017 07:18 AM, Yubin Ruan wrote:
> Hi,
>
> I am writing to see if this is any util functions in libc that can
> help to determine it is currently running in a signal.
>
> I wrote some library and provide a function which will be used in many
> client code. However this function is not async-signal safe (it calls
> malloc(3)) so when it is called, I want to detect whether it is
> currently running in a signal handler. If it is, I can avoid calling
> those not-async-signal-safe functions which might cause deadlock.
>
> that is, I want a `in_signal_handler_context()' utility that can be
> used as this:
>
> int mylibfunc( void ) {
> if( in_signal_handler_context() ) { return(-1) }
> // rest of function goes here
> return( 0 );
> }
Only (b).
(a) The kernel knows, but doesn't tell you.
(b) Only via architecture-specific checks which involve inspecting the
call frame to determine if there is a signal frame on the stack.
(c) glibc *could* know, but it probably needs kernel help for other
purposes.
This issue was discussed orthogonality here:
https://sourceware.org/ml/libc-alpha/2014-02/msg00798.html
Where glibc would create a wrapper for all signal handlers to save and
restore errno to work around bugs in signal handlers that modify errno.
There is no solution yet.
--
Cheers,
Carlos.