How to avoid using printf in a signal handler?

后端 未结 7 963
青春惊慌失措
青春惊慌失措 2020-11-21 06:35

Since printf is not reentrant, it\'s not supposed to be safe to use it in a signal handler. But I\'ve seen lots of example codes that uses printf t

7条回答
  •  抹茶落季
    2020-11-21 06:49

    For debugging purposes, I wrote a tool which verifies that you are in fact only calling functions on the async-signal-safe list, and prints a warning message for each unsafe function called within a signal context. While it doesn't solve the problem of wanting to call non async-safe functions from a signal context, it at least helps you find cases where you have done so accidentally.

    The source code is on GitHub. It works by overloading signal/sigaction, then temporarily hijacking the PLT entries of unsafe functions; this causes calls to unsafe functions to be redirected to a wrapper.

提交回复
热议问题