Invalid argument in sendfile() with two regular files

Posted by Daniel Hershcovich on Stack Overflow See other posts from Stack Overflow or by Daniel Hershcovich
Published on 2010-05-31T18:39:37Z Indexed on 2010/05/31 19:03 UTC
Read the original article Hit count: 356

Filed under:
|
|
|

I'm trying to test the sendfile() system call under Linux 2.6.32 to zero-copy data between two regular files. As far as I understand, it should work: ever since 2.6.22, sendfile() has been implemented using splice(), and both the input file and the output file can be either regular files or sockets.

The following is the content of 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;
}

And when I'm running the following commands:

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

The output is

sendfile: Bad file descriptor

Which means that the system call resulted in errno = -EINVAL, I think. What am I doing wrong?

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux