Which “fatal” signals should a user-level program catch?

前端 未结 3 742
情歌与酒
情歌与酒 2021-02-08 21:12

First of all, I do know that there was a similar question here in the past.

But that question wasn\'t answered properly. Instead, it diverted into suggestion what to do

3条回答
  •  一向
    一向 (楼主)
    2021-02-08 21:19

    You should not catch the signal and write the code to a pipe. This is both unnecessary and not failsafe.

    Let me quote an answer from the question you linked to, to point out why it's not failsafe: "What makes you think that a SEGV hasn't already corrupted your program memory"

    Now you may be wondering how to do it better, and why I've said it is "unnecessary".

    The return code of the waitpid syscall can be checked using WIFSIGNALED() to determine whether the process was terminated normally or via a signal, and WTERMSIG() will return the signal number.

    This is failsafe and it does not require a handler or a pipe. Plus, you don't need to worry what to catch, because it will report every signal that terminates your process.

提交回复
热议问题