c - 带有两个常规文件的 sendfile() 中的参数无效

标签 c linux system-calls sendfile

我正在尝试在 Linux 2.6.32 下测试 sendfile() 系统调用以在两个常规文件之间进行零拷贝数据。 据我了解,它应该可以工作:从 2.6.22 开始,sendfile() 已使用 splice() 实现,并且输入文件和输出文件可以是常规文件或套接字。

sendfile_test.c内容如下:

#include <sys/sendfile.h>

#include <fcntl.h>
#include <stdio.h>

int main(int argc, char **argv) {
  int result;
  int in_file;
  int out_file;

  in_file = open(argv[1], O_RDONLY);
  out_file = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);

  result = sendfile(out_file, in_file, NULL, 1);
  if (result == -1)
    perror("sendfile");

  close(in_file);
  close(out_file);

  return 0;
}

当我运行以下命令时:

$ gcc sendfile_test.c 
$ ./a.out infile outfile

输出是

sendfile: Invalid argument

运行时

$ strace ./a.out infile outfile

输出包含

open("infile", O_RDONLY)                = 3
open("outfile", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
sendfile(4, 3, NULL, 1)                 = -1 EINVAL (Invalid argument)

我做错了什么?

最佳答案

您忘记检查 argc 是否等于 3,即您打开名称为 argv[2] 的输出文件,但是只给你的程序一个参数(并且你不会在 open(2) 之后检查错误。)

您可以使用 strace(1)找出哪个系统调用失败。

编辑:

这对我来说看起来像是旧内核。相同的源代码(模数错误检查)在 2.6.33.4 #3 SMP 下工作正常。另外,您只复制一个字节有什么特别的原因吗?

#include <sys/sendfile.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main( int argc, char *argv[] )
{
    int in_file, out_file;

    if ( argc != 3 )
    {
        fprintf( stderr, "usage: %s <in-file> <out-file>\n", argv[0] );
        exit( 1 );
    }

    if (( in_file = open( argv[1], O_RDONLY )) == -1 ) err( 1, "open" );
    if (( out_file = open( argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644 )) == -1 )
        err( 1, "open(2)" );

    if ( sendfile( out_file, in_file, NULL, 4096 ) == -1 ) err( 1, "sendfile" );

    exit( 0 );
}

跟踪:

nickf@slack:~/csource/linux/splice$ cc -Wall -pedantic -ggdb -g3 -o sf sndf.c 
nickf@slack:~/csource/linux/splice$ strace ./sf Makefile mm
...
open("Makefile", O_RDONLY)              = 3
open("mm", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
sendfile(4, 3, NULL, 4096)              = 239
exit_group(0)                           = ?
nickf@slack:~/csource/linux/splice$ diff Makefile mm 
nickf@slack:~/csource/linux/splice$

关于c - 带有两个常规文件的 sendfile() 中的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2945372/

相关文章:

go - 系统调用变量未定义

c - stat st_mode 始终等于 16877

linux - 为什么我们需要在poll中调用poll_wait?

c - 各种浮点值的 epsilon

c - 修改CRC64的哈希表生成65536个值而不是256个值

javascript - emscripten - 如何从 JavaScript (Chrome/Firefox) 读取 C 数组?

c++ - 使用 Linux 时出现段错误,但在 Xcode 中没有

linux - 共享对象名称冲突

c++ - 我可以为两个子进程重定向标准输出吗?

c - 为什么编译器在这种情况下使用 32 位寄存器将指针传递给 amd64 linux 上的函数