-   Programming (
-   -   how to detect that I'm handling signal (

igor.kopriva 10-01-2004 05:18 AM

how to detect that I'm handling signal
Hi all,
I need to detect that function has been called during signal handling. In other words, there is signal handler installed. Inside handler is called function A(). In normal code, outside singal handler, the A() is called as well. The A() function contains code which is not possible to use in signal handler, so I need to detect that A() was called in signal handler context.

It is of course impossible to pass information whether signal is handled as a parameter. It is also impossible to differ A() function into two kinds, one for signals and one for normal context. In fact the A() function is injected into running code image.

Thanx in advance,
Igor Kopriva

rjlee 10-01-2004 05:32 AM

You haven't specified a programming language; my signal experience is mostly through Perl but I suspect it'll be transferable to whatever you are using, or someone will say if not.

At least in Perl, it's bad practice to call a function from within a signal handler, as it begs the question of what happens if another signal arrives during the processing of the first signal. It's better to set a flag (a static or global variable) within the signal handler and then poll this flag to see if it's been set, calling the function from outside if necessary, then setting the flag to cleared after the function returns (or just before you call it if you want to allow the signal handler function to be interrupted by signals).

If you used a global variable as the flag, then you can test it to see if it's set or not to find out if you're in signal handler mode. There's one too many global variables in that approach, but I don't see why it wouldn't work.

igor.kopriva 10-01-2004 05:46 AM

more precise
I am programming in ANSI C.

It is impossible to modify anything in signal handler at all. The function A() acts as the notify function for function patching. In fact the A() is injected into the running code at the place where patched function B() is called. It means that A() is called instead of B(). It is modification in RAM during execution, so no modification to source of signal handler is allowed. It's a very tricky part of code written in assembler. Because there are mutexes and so on to protect it in multithread environment, it hangs when user decides to patch somethin called within signal context. That's why I need to detect if i am in signal context or not.

Thanx Igor

All times are GMT -5. The time now is 11:26 AM.