Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can exit() fail to terminate process?

I have a registered a signal handler in my program. Upon receiving an undesired signal (SIGABRT), i call 'exit(-1)' in signal handler to exit the process. But as noticed on few ocassions, it calls exit() but fails to terminate the process.

The issue was randomly generated and I strongly suspect on execution of exit().

Can there be any reasons or cases in which the exit() can fail to terminate the process.

Thanks.

like image 545
Mandar Avatar asked Jan 12 '12 10:01

Mandar


1 Answers

Are you calling exit() from the signal handler?

In man 7 signal, section Async-signal-safe functions you can see all the functions that are guaranteed to work when called from an signal handler:

A signal handler function must be very careful, since processing elsewhere may be interrupted at some arbitrary point in the execution of the program. POSIX has the concept of "safe function". If a signal interrupts the execution of an unsafe function, and handler calls an unsafe function, then the behavior of the program is undefined.

POSIX.1-2004 (also known as POSIX.1-2001 Technical Corrigendum 2) requires an implementation to guarantee that the following functions can be safely called inside a signal handler:

There you can see functions _Exit(), _exit() and abort(), but notably not exit(). So you should not call it from a signal handler.

The nasty thing is that even if you call an unsafe function from a signal handler (printf() any?) it will just work most of the time... but not always.

like image 51
rodrigo Avatar answered Sep 17 '22 15:09

rodrigo