使用select()时无法捕获SIGINT信号

时间:2014-11-29 16:34:07

标签: c++ c unix signals sigint

我在syscall 选择中侦听套接字时尝试处理信号。

问题:我有选择调用的工作循环。 select 等待套接字描述符准备就绪。 需要通过SIGINT或SIGQUIT中断循环并纠正关闭资源并退出程序。 下面的代码是

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>


bool bBreakJob = false;

void sig_handler(int sig)
{
    switch(sig)
    {
    case SIGHUP:
        //rneed to reload config
        break;
    case SIGINT:
        printf("SIGINT \n");
        bBreakJob = true;
        openlog("mydaemon", LOG_PID | LOG_CONS, LOG_DAEMON);
        syslog(LOG_INFO, "Catched SIGINT");
        closelog();
        break;
    case SIGQUIT:
        printf("SIGQUIT \n");
        openlog("mydaemon", LOG_PID | LOG_CONS, LOG_DAEMON);
        syslog(LOG_INFO, "Catched SIGQUIT");
        bBreakJob = true;
        break;
    case SIGPIPE:
        printf("SIGPIPE \n");
        break;
    }
}

int main(int argc, char** argv)
{
    struct sigaction act, oact;
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGINT);
    sigaddset(&set, SIGHUP);
    sigaddset(&set, SIGPIPE);
    sigaddset(&set, SIGQUIT);
    sigprocmask(SIG_UNBLOCK, &set, NULL);
    act.sa_mask = set;
    act.sa_handler = sig_handler;
    act.sa_flags = 0;
    sigaction(SIGINT, &act, NULL);
    sigaction(SIGHUP, &act, NULL);
    sigaction(SIGPIPE, &act, NULL);
    sigaction(SIGQUIT, &act, NULL);

    int fds[0], res, fmax;
    fd_set wset;
    fd_set rset;

    //next line code to open socket 
    int listen_socket = socket(AF_INET, SOCK_STREAM, 0); 
    int iFlags = fcntl(listen_socket, F_GETFL);
    iFlags |= O_NONBLOCK;
    fcntl(listen_socket, F_SETFL, iFlags);
    struct sockaddr_in sin;
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(4000);
    bind(listen_socket, (struct sockaddr *)&sin, sizeof(sin));
    listen(listen_socket, 20);

    fds[0] = listen_socket;
    FD_ZERO(&wset);
    FD_SET(fds[0], &wset);
    fmax = fds[0] + 1;
    while (FD_ISSET(fds[0], &wset))
    {
        rset = wset;
        res = select(fmax, &rset, NULL, NULL, NULL);
        if (res < 0)
        {
            if (errno == EINTR)
            {   //debug message  
                printf("Loop broken by select's result EINTR");
                break;
            } else
            {
                printf("select(...) fails in listed loop. errno %d (%s)", errno, strerror(errno));
                exit(1);
            }

        }
        else if (res == 0)
        {
          //if timeout is handled
        }
        else if (res > 0)
        {
            if (FD_ISSET(fds[0], &rset))
            {
                //Handle socket input 
            }
        }
        if(bBreakJob)
        {
          printf("Loop broken by signal handler");
          break; 
        }
    } //while( 1 );
    FD_CLR(fds[0], &wset);
    if(bBreakJob)
    { //debug message 
      printf("signal SIGINT is handled ");
    }

}

SIGINT永远不会到达sig_handler。在IDE QtCreator中,我尝试过调试。选择刚刚中断,然后返回听。条件&#34; if(errno == EINTR)&#34;甚至没有达到。在syslog中,在控制台中也没有调试消息。并且在同一时间SIGQUIT工作正常:调用sig_handler并且条件&#34; if(errno == EINTR)&#34;也达到了。

正如您所看到的,我试图检查SIGINT的方式:使用信号处理程序中的标志,以及 select 的结果

我试图在主题Not able to catch SIGINT signal while using select()中找到答案。但无法找到解决方案。我在其他WEB资源中遇到这个问题,但也没有解决方案。

从命令行发送SIGINT信号:&#34; kill -s 2(PID)&#34;

UPD问题已经解决。问题出在调试器中。在调试器下,SIGINT无法正常工作。在没有调试器的情况下运行程序正常工作。

1 个答案:

答案 0 :(得分:1)

选择和信号的交互是棘手的,因为信号总是在你调用select之前到达。以下是从信号处理程序中唤醒选择循环的两种方法:

  • &#34;自管道技巧&#34;:创建管道并将读取结束添加到您的选择读取集。从信号处理程序中,将一个字节写入此管道的写入端,它将立即返回select(因为输入已准备就绪)。

  • 不是将NULL作为select的最后一个参数传递,而是将指针传递给作为全局变量的timeval。在信号处理程序中,使时间间隔为0秒。因此,如果在您调用select之前信号到达,则select将要求0超时并立即返回。

相关问题