简体   繁体   English

在Ubuntu中无法捕获SIGPIPE信号

[英]Cannot catch SIGPIPE signal in Ubuntu

I met a SIGPIPE crash issue and I would like to log it and try to exist. 我遇到了SIGPIPE崩溃问题,我想记录下来并尝试存在。
But I could not catch SIGPIPE via follow code. 但是我无法通过跟随代码来捕捉SIGPIPE。
I try to use "kill -s signal process" to verify my code, it works with signal SIGINT, SIGSTOP, SIGSEGV and SIGALRM. 我尝试使用“ kill -s signal process”来验证我的代码,它可以与信号SIGINT,SIGSTOP,SIGSEGV和SIGALRM一起使用。
But failed on SIGPIPE. 但是在SIGPIPE上失败了。
Would you please advise if I miss anything here. 如果我想念这里,请告诉我。

void handle_pipe(int sig)
{
    printf("SIG_PIPE happen, error code is %d", sig);
    exit(0);    
}   

int main(int argc, char **argv)
{
    struct sigaction action;
    sigemptyset(&action.sa_mask);
    action.sa_handler = handle_pipe;
    action.sa_flags = 0;
    //not work
    sigaction(SIGPIPE, &action, NULL);   //Not work with kill -13 process_id
    //works well
    sigaction(SIGINT, &action, NULL);    //work with kill -2 process_id
    sigaction(SIGSEGV, &action, NULL);   //work with kill -11 process_id
    sigaction(SIGALRM, &action, NULL);   //work with kill -14 process_id
    sigaction(SIGSTOP, &action, NULL);   //work with kill -17 process_id

    fooServer * pfooServer = new fooServer();
    while(1)
    {
        pfooServer->DoEvents();
    }
    delete pfooServer;
}

My environment is Ubuntu 12.04 LTS 我的环境是Ubuntu 12.04 LTS

This complete code example does work with a kill -13. 这个完整的代码示例确实使用kill -13。 I don't have ubuntu 12.04 LTS here to test it with, but it is fine on RHEL 6.5. 我在这里没有ubuntu 12.04 LTS进行测试,但在RHEL 6.5上还可以。 Try this. 尝试这个。 If it works as expected then there must be something in your "fooServer" that is altering SIGPIPE behaviour 如果它按预期工作,则您的“ fooServer”中必须有某些东西正在改变SIGPIPE行为

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>



void handle_pipe(int sig)
{
    printf("SIG_PIPE happen, error code is %d", sig);
    exit(0);    
}   

int main(int argc, char **argv)
{
    struct sigaction action;
    sigemptyset(&action.sa_mask);
    action.sa_handler = handle_pipe;
    action.sa_flags = 0;
    //not work
    sigaction(SIGPIPE, &action, NULL);   //Not work with kill -13 process_id
    //works well
    sigaction(SIGINT, &action, NULL);    //work with kill -2 process_id
    sigaction(SIGSEGV, &action, NULL);   //work with kill -11 process_id
    sigaction(SIGALRM, &action, NULL);   //work with kill -14 process_id
    sigaction(SIGSTOP, &action, NULL);   //work with kill -17 process_id

    while(1)
    {
        sleep(1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM