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

前端 未结 6 1244
一生所求
一生所求 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:12

    I guess you need to do the following. This is a complex solution, which combines the best practices not only from coding, but from software engineering as well!

    1. Persuade your boss that naming convention on signal handlers is a good thing. Propose, for example, a Hungarian notation, and tell that it was used in Microsoft with great success. So, all signal handlers will start with sighnd, like sighndInterrupt.
    2. Your function that detects signal handling context would do the following:
      1. Get the backtrace().
      2. Look if any of the functions in it begin with sighnd.... If it does, then congratulations, you're inside a signal handler!
      3. Otherwise, you're not.
    3. Try to avoid working with Jimmy in the same company. "There can be only one", you know.

提交回复
热议问题