tags:

views:

144

answers:

4

Hi guys, consider a signal handler that call exit() as last instruction: is safe to call non-reentrant functions (e.g. free()) in that handler?

IMHO it would be legal due to the fact that the handler does not return to the normal sequence of execution.

Thank you in advance.

+1  A: 

No, this is illegal, more then that, there are very few safe functions to call.

There is a list of safe functions to call, see http://linux.die.net/man/2/signal Notes section.

Artyom
A: 

Reentrancy as more to do with the "entrance" to a function and, side-effects and state maintained by the said function than the exit...

You might want to consult this man page.

jldupont
A: 

Ok got it. Thanks.

I'm still in trouble. I have a daemon that terminate on a signal and I need to free data structures on termination.

Any advice?

Marco
Please edit your original question instead of posting an answer :) This is not a forum.
Aaron Digulla
Sorry, I'll care next time
Marco
A: 

You must distinguish between two signals: Those which tell the daemon to "reload" and those which terminate the daemon ("kill"). In the "kill" case, you don't need to free anything. Your process is going to die, the OS will clean up anything you have allocated. If you use shared memory, you must do the cleanup when you're started again. Don't do anything in the "kill" handler which might cause problems. Just die.

In the reload case, you can call any function you like since the user wants you to "shut down orderly". In this case, there is little chance that you will get the same signal again (so it doesn't matter whether a function is reentrant or not).

Aaron Digulla
Ok, thank you.This project is for an exam. I was worried about leaving a malloc() without free() :)
Marco