[英]No output using splice syscall
I have the following code:我有以下代码:
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char **argv) {
if(argc < 2) {
fputs("Error: You must provide an filename\n", stderr);
return 1;
}
FILE *handle = fopen(argv[1], "r");
if(!handle) {
perror("Error opening file:");
goto error;
}
int handle_fd = fileno(handle);
int filedes[2];
if(pipe(filedes)) {
perror("Error creating pipe:");
goto error;
}
while(1) {
ssize_t rc = splice(handle_fd, NULL, filedes[1], NULL, BUFSIZ, 0);
if(rc == -1) {
// Error occurred
perror("Error copying data:");
goto error;
} else if(rc == 0) {
break;
}
splice(filedes[0], NULL, STDOUT_FILENO, NULL, BUFSIZ, 0);
}
return 0;
error:
if(fclose(handle)) {
perror("Error closing file:");
}
return 1;
}
And when I run it I don't get any output.当我运行它时,我没有得到任何输出。 None of the perror calls are triggered either and I can't figure out why it doesn't work.也没有触发任何 perror 调用,我无法弄清楚为什么它不起作用。
When I run the program in GDB it works, however it doesn't display anything when running just plain from the shell当我在 GDB 中运行程序时,它可以工作,但是从 shell 中运行时它不显示任何内容
You should check that STDOUT_FILENO
is not opened in append mode, or the second splice(2)
will fail, as documented in its manpage :您应该检查STDOUT_FILENO
是否未在追加模式下打开,否则第二个splice(2)
将失败,如其联机帮助页中所述:
EINVAL
The target file is opened in append mode.EINVAL
目标文件以追加模式打开。
And yes that will happen even if it's aa tty, where the O_APPEND
flag shouldn't make any difference, which looks very much as a bug .是的,即使它是一个 tty,它也会发生,其中O_APPEND
标志不应该有任何区别,这看起来非常像一个错误。 Or at least a nuisance, especially since splice
doesn't care when the O_APPEND
flag is set on a socket or pipe.或者至少是一个麻烦,特别是因为splice
不关心何时在套接字或管道上设置O_APPEND
标志。
Try running your program as尝试运行您的程序
./your_program file >/dev/tty
or #include <fcntl.h>
and add this at the beginning of your main()
function:或#include <fcntl.h>
并将其添加到main()
函数的开头:
if(isatty(1)) fcntl(1, F_SETFL, fcntl(1, F_GETFL) & ~O_APPEND);
Notice that the O_APPEND
flag may get accidentally turned on on your terminal: to check if it's on, look into /proc/<pid>/fdinfo/<fd>
:请注意, O_APPEND
标志可能会在您的终端上意外打开:要检查它是否打开,请查看/proc/<pid>/fdinfo/<fd>
:
$ grep flags /proc/self/fdinfo/1
flags: 0102002
^ here it is
One curious example of a program which turns the O_APPEND
flag on its stdout is GNU make ;-)在其标准输出上打开O_APPEND
标志的程序的一个奇怪示例是GNU make ;-)
$ strace -e trace=fcntl make
fcntl(1, F_GETFL) = 0x48002 (flags O_RDWR|O_LARGEFILE|O_NOATIME)
fcntl(1, F_SETFL, O_RDWR|O_APPEND|O_LARGEFILE|O_NOATIME) = 0
...
A testcase, scat.c
:一个测试用例, scat.c
:
/*
* a simple program which copies its input to its output via splice(2)
* if given a '1' argument, it will turn the O_APPEND flag on stdout
* if given a '0' argument, it will turn it off
* otherwise it will leave it as it is
*/
#define _GNU_SOURCE 1
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <err.h>
int main(int ac, char **av){
ssize_t z; int fl;
if(ac > 1){
if((fl = fcntl(1, F_GETFL)) == -1) err(1, "fcntl(1, getfl)");
if(atoi(av[1])) fl |= O_APPEND;
else fl &= ~O_APPEND;
if(fcntl(1, F_SETFL, fl)) err(1, "fcntl(1, setfl, %x)", fl);
}
while((z = splice(0, 0, 1, 0, 65536, 0)))
if(z < 0) err(1, "splice");
}
$ cc -Wall scat.c -o scat
$ echo yup | ./scat
yup
$ echo yup | ./scat 1
scat: splice: Invalid argument
$ echo yup | ./scat
scat: splice: Invalid argument
$ echo yup | ./scat 0
yup
$ echo yup | ./scat
yup
OK with a pipe or socket as output:可以使用管道或套接字作为输出:
$ echo yup | ./scat 1 | cat
yup
$ nc -l -p 9999 &
[4] 23952
$ echo yup | ./scat 1 | nc -q0 localhost 9999
yup
[4]- Done nc -l -p 9999
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.