How to determine if code is running in signal-handler context?

前端 未结 6 1240
一生所求
一生所求 2021-02-03 11:11

I just found out that someone is calling - from a signal handler - a definitely not async-signal-safe function that I wrote.

So, now I\'m curious: how to circumvent this

6条回答
  •  花落未央
    2021-02-03 11:35

    If we can assume your application doesn't manually block signals using sigprocmask() or pthread_sigmask(), then this is pretty simple: get your current thread ID (tid). Open /proc/tid/status and get the values for SigBlk and SigCgt. AND those two values. If the result of that AND is non-zero, then that thread is currently running from inside a signal handler. I've tested this myself and it works.

提交回复
热议问题