SIGIO到达文件描述符我没有设置它,什么时候没有IO

Ken*_*eth 5 c linux signals

我正在尝试在文件描述符上进行I/O时接收信号.程序在不执行I/O时需要执行其他操作,因此使用select(2)不是一个选项.

当我在下面运行示例代码时,它会尽可能快地从处理程序内部打印消息,即使stdin上没有数据也是如此.甚至更奇怪的是siginfo_t结构中报告的文件描述符因运行而异.我只为stdin(fd 0)设置了它; 为什么处理程序会报告任何其他值?有时我看到0,有时,我看到1,大多数时候我看到'?',表示除0,1或2之外的值.

这是在OpenSUSE 12.3,Linux内核3.7.10-1.16上,但我看到CentOS 6.4及其库存内核出现了同样的问题.

我在处理程序中使用write,因为signal(7)表示它是可重入的,因此在信号处理程序中使用是合法的.这也是为什么我不打印sinfo-> si_fd的值; snprintf不可重入.有一段时间我怀疑使用SIGIO的stdio库,这就是为什么示例程序中没有stdio调用的原因(除了可能在库函数err(3)中).

感谢您花时间阅读我的代码.

#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>

int needRead = 0;
const unsigned int bufsize = 256;

void handler(int sig, siginfo_t *sinfo, void *value)
{
    char *cp;

    cp = "in handler. fd: ";
    write(2, cp, strlen(cp));
    switch(sinfo->si_fd) {
        case 0: cp = "0\n"; break;
        case 1: cp = "1\n"; break;
        case 2: cp = "2\n"; break;
        default: cp = "?\n"; break;
    }
    write(2, cp, strlen(cp));

    needRead = 1;
}

int main(int argc, char *argv[])
{
    struct sigaction act;
    unsigned int counter = 0;
    int flags;
    char *outp = ".";

    /* set up the signal handler for SIGIO */
    act.sa_sigaction = handler;
    act.sa_flags = 0;
    act.sa_flags = SA_RESTART;
    sigemptyset(&act.sa_mask);
    if (sigaction(SIGIO, &act, NULL) == -1)
        err(1, "attempt to set up handler for SIGIO failed");

    /* arrange to get the signal */
    if (fcntl(0, F_SETOWN, getpid()) == -1)
        err(1, "fnctl to set F_SETOWN failed");
    flags = fcntl(0, F_GETFL);
    if (flags >= 0 && fcntl(0, F_SETFL, flags | O_ASYNC ) == -1)
        err(1, "fnctl F_SETFL to set O_ASYNC failed");

    while (1) {
        char in_buf[bufsize];
        int nc;

        counter++;

        write(STDERR_FILENO, outp, strlen(outp));

        if (needRead) {
            needRead = 0;
            if ((nc = read(STDIN_FILENO, in_buf, bufsize)) == -1) {
                err(1, "read from stdin failed");
            } else {
                outp = "Read '";
                write(STDERR_FILENO, outp, strlen(outp));
                write(STDERR_FILENO, in_buf, nc);
                outp = "'\n";
                write(STDERR_FILENO, outp, strlen(outp));
            }
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

pil*_*row 7

啊,有趣.

简短的回答是,SIGIO反复到达stdin,因为stdin是可写的,并且,单独地,你的SIGIO交付没有很好地设置.

为什么si_fd显然不可靠?

首先,您需要在sa_flags安全地使用sa_sigaction处理程序之前指定SA_SIGINFO .

其次,#define _GNU_SOURCE在Linux 为您填写si_fd(以及si_band就此而言)之前,您需要并明确地将F_SETSIG发送到SIGIO .有点傻,恕我直言,但事实如此.如果没有这个,传入的值si_fd没有意义,正如您所发现的那样.

为什么SIGIO一次又一次交付?

我猜你的程序的stdin继承自你的调用shell,我猜这是一个终端设备并且是可写的.正如fd 0将连续选择(2)可写,因此它将继续为您生成SIGIO.

无论如何,si_band坚持答案.启用F_SETSIG,#include <poll.h>并检查si_bandPOLLIN,POLLOUT等,以确定哪些I/O事件触发了信号.

真的,stdin是可写的吗?

是啊.试试这些来进行比较:

$ [ -w /dev/stdin ] && echo Yes, stdin is writeable
Yes, stdin is writeable    

# Endless SIGIOs
$ ./my-sigio-prog
^C

# No SIGIO
$ ./my-sigio-prog < /dev/null

# Two SIGIOs, after a delay.  One for line-buffered "foo\n" and one for EOF
$ { sleep 3; echo foo; sleep 3; } | ./my-sigio-prog
Run Code Online (Sandbox Code Playgroud)