How to properly terminate a stream in a signal handler?

I want to set up a signal handler for SIGSEGV, SIGILL, and possibly a few other signals that, instead of ending the whole process, just terminate the abusive stream and possibly set a flag somewhere so that the monitoring thread can complain and start another thread. I'm not sure there is a safe way to do this. Pthreads seems to provide functions for exiting the current thread as well as for canceling another thread, but they could potentially call a bunch of at-exit handlers. Even if they do not, it seems that there are many situations where they are not safe for an asynchronous signal, although it is possible that these situations can be avoided. Is there a lower level function that I can name that just destroys the thread? Assumingthat I am modifying my own data structures in a safe way with an asynchronous signal and not getting any mutexes, are there pthread / other global data structures that can be left in an inconsistent state simply by a stream ending in SIGSEGV? malloc comes to mind, but malloc itself should not SIGSEGV / SIGILL if libc is not buggy. I understand that POSIX is very conservative here and does not give any guarantees. As long as there is a way to do this in practice, I'm happy. A fork is not an option, by the way.that POSIX is very conservative here and makes no guarantees. As long as there is a way to do this in practice, I'm happy. A fork is not an option, by the way.that POSIX is very conservative here and makes no guarantees. As long as there is a way to do this in practice, I'm happy. A fork is not an option, by the way.

+1
source share
1 answer

If SIGSEGV/ SIGILL/ etc. happens in your own code, the signal handler will not work in the context of an asynchronous signal (this is a fundamentally synchronous signal, but it will still be an AS context if this happens inside the standard library function), so you can legally call it pthread_exitfrom the signal handler. However, there are still problems that make this practice questionable:

  • SIGSEGV/SIGILL/ ... , , raise, kill, pthread_kill, sigqueue .. ( ). undefined . undefined, . UB . UB, / .

  • (, - access-after- free, , ,...), , (, malloc), . AS- pthread_exit. , UB (. ), , , .

, , . - . , , , , , , ( , ).

+2

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


All Articles