c - SIGIO 到达文件描述符,我没有设置它,当没有 IO 是可能的

标签 c linux signals

我正在尝试在文件描述符上可以进行 I/O 时接收信号。该程序在不执行 I/O 时需要执行其他操作,因此不能使用 select(2)。

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

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

我在处理程序中使用写入,因为 signal(7) 表示它是可重入的,因此在信号处理程序中使用是合法的。这也是我不打印sinfo->si_fd的值的原因; snprintf 不可重入。有一段时间我怀疑 stdio 库使用了 SIGIO,这就是为什么示例程序中的任何地方都没有 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;
}

最佳答案

啊,有趣。

简短的回答是,SIGIO 反复到达 stdin,因为 stdin 是可写,另外,您的 SIGIO 传递设置不正确。

为什么 si_fd 显然不可靠?

首先,需要在sa_flags中指定SA_SIGINFO在你可以安全地使用 sa_sigaction 之前处理程序。

其次,你需要#define _GNU_SOURCE并在 Linux 填写 si_fd 之前将 F_SETSIG 显式设置为 SIGIO (和 si_band ,就此而言)给你。有点傻,恕我直言,但事实就是如此。没有这个,值传递给 si_fd正如您发现的那样,没有意义。

为什么要一遍又一遍地交付 SIGIO?

我猜你程序的标准输入是从你的调用 shell 继承的,我猜它是一个终端设备并且是可写的。正如 fd 0 会不断地 select(2) 可写一样,它也会不断地为您生成 SIGIO。

无论如何,si_band持有答案。启用 F_SETSIG,#include <poll.h> ,并检查 si_band对于 POLLIN、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

关于c - SIGIO 到达文件描述符,我没有设置它,当没有 IO 是可能的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866754/

相关文章:

linux - Git - 将 Github 存储库与本地存储库同步?

linux - centos和jboss上的文件日志?

php - 如何从 Apache PHP 脚本(Ubuntu 14.04)向守护进程发送信号 SIGUSR1

linux - Bash 脚本在杀死它自己及其所有子进程的 PGID 后没有被杀死

c++ - 如果整数溢出, (unsigned int) * (int) 的结果是什么?无符号或整数?

c - 从文件更新数组,在 C 中进行验证

linux - 使用 Linux 系统凭据登录 Flask

linux - 为什么 shell 在后台进程中忽略 SIGINT 和 SIGQUIT?

c - Malloc 在为整数分配空间时表现异常

C 程序未接收到所有输入