Is there a way to check if I am in a signal handler?

I need to work with the registration module, which can be called from different places in a large project. The problem is that sometimes a module can be called from code executed inside a signal handler. Typically, the registration module includes time data using localtime () and strftime (), but, of course, these calls are not safe for an asynchronous signal and can cause deadlocks when called from a signal handler. Is there a way (on a GNU / Linux system) to determine if my code is currently executing in the context of a signal handler, except, for example, if each signal handler sets a flag during processing? I think it would be better to simplify our signal handlers, but in this case I have no choice where to call the registration module. It would be nice if I could check and just omit the timestamp information,if the module is called during signal processing.

+3
source share
3 answers

First, your question (“Am I in a signal handler?”) Does not have a clearly defined answer. Consider the following code:

#include <setjmp.h>
#include <signal.h>

jmp_buf jb;
int foo(int s)
{
    longjmp(jb,1);
}

int main()
{
    if (setjmp(jb)) {
        puts("Am I in a signal handler now, or not?");
        return 0;
    }
    signal(SIGINT, foo);
    raise(SIGINT);
}

, , , . , , sa_mask , , sigaction. sigprocmask , , , ( ).

+2

- () ( PIPE_MAX ) UDP- (idem). , . , , , .


BTW: , ( ) fd_set ( ) .

+3

Is there a system in your system sigpending? I do not know what the behavior of this function is during the signal handler. However, if it returns the installation flag, you can be pessimistic and skip asynchronous calls if any signals are expected.

0
source

Source: https://habr.com/ru/post/1735656/


All Articles