【问题标题】:Debugginng a daemon that terminates unexpectedly调试意外终止的守护进程
【发布时间】:2009-08-22 19:27:47
【问题描述】:

我正在 linux 上用 c 语言编写一个守护进程。它捕获信号 SIGHUP、SIGTERM、SIGINT 和 SIGQUIT,使用 syslog 记录它们并退出。如果它收到 SIGSEGV,它会转储核心。当这些发生时,一切都会按预期发生,但偶尔会退出......不会干净地退出,不会记录信号,也不会留下核心转储。我很难过,不知道如何调试问题。除了这些信号,它还能通过哪些方式退出?有没有明显的答案,我错过了什么?对于在守护进程中调试这种看似零星的问题,您还推荐了哪些其他调试实践?

【问题讨论】:

    标签: c debugging signals daemon coredump


    【解决方案1】:

    如果您的守护程序正在使用网络套接字,则很可能是SIGPIPE - 如果您尝试写入已被另一端关闭的套接字(或管道),您会得到这个。请注意,即使您在写入之前检查套接字是否可写(例如使用select()),它也始终可以在检查和写入之间关闭。

    【讨论】:

    • 啊!我正在使用套接字并且不捕获 SIGPIPE,没有想到这一点,我敢打赌就是这样。目前我的 select() 调用处于一个循环中,如果它被中断则中断,但如果它是一个 SIGPIPE,我想留在循环中。从您的评论中,我了解到 select() 调用永远不会被 SIGPIPE 中断,只有 read()/write() 调用,这是真的吗?
    • SIGPIPE 不会从select() 向您的进程发出信号,但它会返回标记为可读的文件描述符(以便您可以发现它已被关闭)。 SIGPIPE 仅由 write()s 提出。如果您忽略或处理SIGPIPE,则write() 将返回EPIPE
    【解决方案2】:

    您可以让守护进程的父进程留下并等待它,然后让父进程记录守护进程退出的原因(即,它是收到信号还是退出)。

    【讨论】:

    • 这是一个很好的解决方案,一旦孩子去世,您如何从父母那里获得这些信息?
    • 在父进程中,调用wait,使用WIFEXITED/WIFSIGNALED 来确定发生了什么,然后使用相应的日志消息调用syslog。查看手册页等待。
    【解决方案3】:

    将gdb附加到它

    gdb -p <pid>
    确保使用 -g 标志进行编译,并在退出后立即进行回溯。 祝你好运!

    【讨论】:

    • 我不知道你能做到这一点!这很棒,因为守护进程在我没有物理访问权限的服务器上运行。我经常带着我的笔记本电脑在移动中,并且在移动时无法保持打开的终端对其进行监控。这样我可以在需要时附加/分离 gdb 而无需关闭守护进程;太好了!
    【解决方案4】:

    嗯,还有很多其他信号会导致它退出,当然包括SIGKILL,你将无能为力。基本上来自man 7 signal 的以下内容中的任何内容,其中ActionTermCore(尽管后者至少会留下核心转储):

       First the signals described in the original POSIX.1-1990 standard.
    
       Signal     Value     Action   Comment
       -------------------------------------------------------------------------
       SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process
       SIGINT        2       Term    Interrupt from keyboard
       SIGQUIT       3       Core    Quit from keyboard
       SIGILL        4       Core    Illegal Instruction
    
       SIGABRT       6       Core    Abort signal from abort(3)
       SIGFPE        8       Core    Floating point exception
       SIGKILL       9       Term    Kill signal
       SIGSEGV      11       Core    Invalid memory reference
       SIGPIPE      13       Term    Broken pipe: write to pipe with no readers
       SIGALRM      14       Term    Timer signal from alarm(2)
       SIGTERM      15       Term    Termination signal
       SIGUSR1   30,10,16    Term    User-defined signal 1
       SIGUSR2   31,12,17    Term    User-defined signal 2
       SIGCHLD   20,17,18    Ign     Child stopped or terminated
       SIGCONT   19,18,25    Cont    Continue if stopped
       SIGSTOP   17,19,23    Stop    Stop process
       SIGTSTP   18,20,24    Stop    Stop typed at tty
       SIGTTIN   21,21,26    Stop    tty input for background process
       SIGTTOU   22,22,27    Stop    tty output for background process
    
       The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
    
       Next the signals not in the POSIX.1-1990 standard but described in SUSv2 and POSIX.1-2001.
    
       Signal       Value     Action   Comment
       -------------------------------------------------------------------------
       SIGBUS      10,7,10     Core    Bus error (bad memory access)
       SIGPOLL                 Term    Pollable event (Sys V). Synonym of SIGIO
       SIGPROF     27,27,29    Term    Profiling timer expired
       SIGSYS      12,-,12     Core    Bad argument to routine (SVr4)
       SIGTRAP        5        Core    Trace/breakpoint trap
       SIGURG      16,23,21    Ign     Urgent condition on socket (4.2BSD)
       SIGVTALRM   26,26,28    Term    Virtual alarm clock (4.2BSD)
       SIGXCPU     24,24,30    Core    CPU time limit exceeded (4.2BSD)
       SIGXFSZ     25,25,31    Core    File size limit exceeded (4.2BSD)
    
       Up to and including Linux 2.2, the default behaviour for SIGSYS, SIGXCPU, SIGXFSZ, and (on architectures other than SPARC
       and  MIPS) SIGBUS was to terminate the process (without a core dump).  (On some other Unices the default action for SIGX-
       CPU and SIGXFSZ is to terminate the process without a core dump.)  Linux 2.4 conforms to  the  POSIX.1-2001  requirements
       for these signals, terminating the process with a core dump.
    
       Next various other signals.
    
       Signal       Value     Action   Comment
       --------------------------------------------------------------------
       SIGIOT         6        Core    IOT trap. A synonym for SIGABRT
       SIGEMT       7,-,7      Term
       SIGSTKFLT    -,16,-     Term    Stack fault on coprocessor (unused)
       SIGIO       23,29,22    Term    I/O now possible (4.2BSD)
       SIGCLD       -,-,18     Ign     A synonym for SIGCHLD
       SIGPWR      29,30,19    Term    Power failure (System V)
       SIGINFO      29,-,-             A synonym for SIGPWR
       SIGLOST      -,-,-      Term    File lock lost
       SIGWINCH    28,28,20    Ign     Window resize signal (4.3BSD, Sun)
       SIGUNUSED    -,31,-     Term    Unused signal (will be SIGSYS)
    

    【讨论】:

      【解决方案5】:

      shell 包装器可以捕获您的守护程序的退出状态。以下是它的工作原理:

      $ ./waitstatus true
      pid 1512: exit status 0 (success)
      
      $ ./waitstatus false
      pid 1514: exit status 1 (abnormal)
      
      $ ./waitstatus perl -e 'exit 21'
      pid 1518: exit status 21 (abnormal)
      
      $ ./waitstatus perl -e 'kill TERM => $$'
      pid 1520: terminated on signal 15
      
      $ ./waitstatus no-such-command
      pid 1522: command not found: no-such-command
      
      $ ./waitstatus /sbin/EACCES.contrived
      pid 1524: command not executable: /sbin/EACCES.contrived
      

      ... 以下是它的实现方式:

      $ cat ./waitstatus
      #! /bin/bash
      
      "$@" &
      PID=$!
      
      wait $PID
      STATUS=$?
      
      if   [ $STATUS -gt 128 ]; then
        MSG="terminated on signal $(( $STATUS - 128 ))";
      else
        case $STATUS in
          0)
            MSG="exit status 0 (success)"
            ;;
          127)
            MSG="command not found: $1"
            ;;
          126)
            MSG="command not executable: $1"
            ;;
          *)
            MSG="exit status $STATUS (abnormal)"
            ;;
        esac
      fi
      
      echo "pid $PID: $MSG"
      exit $STATUS
      

      您可能希望将最后一行 echo 更改为调用系统的 logger 命令,例如,将状态消息定向到 syslog

      【讨论】: