ENOTTY:设备的 ioctl 不合适:程序有效,但在内部执行时无效 <()

问题描述 投票:0回答:0

我有一个简单的程序,可以逐行读取文件,并打印每一行。程序的核心是:

while ((size = getline(&line, &len, f)) != -1)
    printf("%s", line);

现在我有以下问题:

这有效:

./test zz

这也有效:

/bin/cat <(./test zz)

但是当我将输出通过管道传输到其他程序时,它不起作用:

/bin/cat <(./test zz) | /bin/cat

没有输出。 当我与

strace
进行比较时,一个有效,一个无效:

/bin/cat <(strace ./test zz)

/bin/cat <(strace ./test zz) | /bin/cat

我看到这个错误:

-1 ENOTTY (Inappropriate ioctl for device)

ioctl(0, TCGETS, 0x7ffc302104d0)        = -1 ENOTTY (Inappropriate ioctl for device)
brk(NULL)                               = 0x55b4e1fd3000
brk(0x55b4e1ff4000)                     = 0x55b4e1ff4000
fstat(0, {st_mode=S_IFCHR|0666, st_rdev=makedev(0x1, 0x3), ...}) = 0
ioctl(0, TCGETS, 0x7ffc30210350)        = -1 ENOTTY (Inappropriate ioctl for device)
read(0, "", 4096)                       = 0
close(0)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++

编辑:

这是我的概念验证代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <unistd.h>


int main(int argc, char **argv) {

    FILE *f;
    char *line = NULL;
    size_t len = 0;
    ssize_t size, cut;


    if (!isatty(STDIN_FILENO) || argc < 2 || (argv[1] == "-"))
        f = stdin;
    else
        if (!(f = fopen(argv[1], "r"))) {
            perror(argv[1]);
            return 1;
        }

    while ((size = getline(&line, &len, f)) != -1)
    printf("%s", line);

fflush(f);
    fclose(f);
    if (line)
        free(line);
    exit(EXIT_SUCCESS);
}
c tty ioctl strace
© www.soinside.com 2019 - 2024. All rights reserved.