【问题标题】:How to log signals to an application signals with a log handler?如何使用日志处理程序将信号记录到应用程序信号?
【发布时间】:2018-07-10 02:08:32
【问题描述】:

有一些方法可以用 linux signal handlers 做一些工作。

  1. 我们可以为每个信号注册系统处理程序(如果我们有源代码)或
  2. 运行strace下的进程来查看它们。

策略 1: 但是,如果我们没有源代码,我们如何将signals 捕获到应用程序以对其进行处理并返回? (不是一次性调试,而是永久功能)。 [可能是system call?]

策略 2: 如果我们确实有源代码,在多个信号的情况下写入文件是否安全?还是在fork() 进程中执行信号处理程序并丢弃SIGCHLD 更安全?在处理前一个信号时,如果另一个信号进来会怎样?

【问题讨论】:

    标签: linux linux-kernel signals signal-handling


    【解决方案1】:

    对于您的策略 2,取决于您的日志文件的写入方式以及信号的触发方式(异步与否)。通常 stdio 库函数不是异步信号安全的。

    详情见http://man7.org/linux/man-pages/man7/signal-safety.7.html

       To avoid problems with unsafe functions, there are two possible
       choices:
    
       1. Ensure that (a) the signal handler calls only async-signal-safe
          functions, and (b) the signal handler itself is reentrant with
          respect to global variables in the main program.
    
       2. Block signal delivery in the main program when calling functions
          that are unsafe or operating on global data that is also accessed
          by the signal handler.
    

    【讨论】:

      【解决方案2】:

      策略 1:但是如果我们没有源代码,我们如何捕捉到应用程序的信号以对其进行处理并返回? (不是一次性调试,而是永久功能)。 [可能是破解系统调用?]

      要拦截传递给进程的信号,至少有两种方法:

      • ptrace(2)(这是 strace 使用的)示例参见 this answer
      • LD_PRELOAD:(我不建议使用这种方法)您可以使用它为每个信号设置处理程序,并用两个包装函数替换 signalsigaction,以防止程序覆盖您的信号处理程序(请注意建议在this other answer)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-19
        • 1970-01-01
        相关资源
        最近更新 更多