Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signal handler async safe functions

Is there a way I can call non async safe functions from within a signal handler? Or is this impossible?

I am not looking for setting a flag from within the signal handler and then print something in my "main code" but in this case the signal handlers will define the logical flow of my program in itself.

like image 267
Curious Avatar asked Nov 21 '25 12:11

Curious


1 Answers

Is there a way I can call non async safe functions from within a signal handler? Or is this impossible?

No. Not safely. Doing so results in undefined behavior - most likely deadlocks, but other things can happen, too.

The reason any function call is labeled as "async signal safe" is for the very purpose of marking it as safe to call from within a signal handler.

From the signal(7) Linux man page:

Async-signal-safe functions

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:

...

If the function call is not listed, it's not safe to call it from within a signal handler.

like image 158
Andrew Henle Avatar answered Nov 24 '25 03:11

Andrew Henle