LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sendfile() fails with regular files on RHEL5.10 (https://www.linuxquestions.org/questions/linux-newbie-8/sendfile-fails-with-regular-files-on-rhel5-10-a-4175472138/)

manjuhr 08-05-2013 04:20 AM

sendfile() fails with regular files on RHEL5.10
 
Hello All,

I am executing following program on RHEL5.10, sendfile() is failing with errno:22

uname -rvio
2.6.18-366.el5 #1 SMP Fri Jul 19 13:36:15 EDT 2013 ppc64 GNU/Linux

#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, 4 ) == -1 ) err( 1, "sendfile" );

exit( 0 );
}


Here sendfile fails with : EINVAL

> part of strace output:
----------------------------------------------------------------------
open("input_file", O_RDONLY) = 3
open("output_file", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
sendfile(4, 3, NULL, 4) = -1 EINVAL (Invalid argument)
-----------------------------------------------------------------------

Please, let me know if sendfile() supports regular files on "2.6.18-366.el5"

Thanks in advance :)

salsalinux 08-05-2013 11:22 AM

Do you have permissions to the files that you are giving as arguments?

salsalinux 08-05-2013 11:27 AM

For what it's worth, I copied your code and compiled it, and it worked for me. I created in.txt, and out.txt, and ran the program with those 2 files as arguments, and here is the last portion of strace:

...
open("in.txt", O_RDONLY) = 3
open("out.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
sendfile(4, 3, NULL, 4) = 4
exit_group(0) = ?

However, I am running kernel 2.6.38-8-generic #42-Ubuntu SMP x86_64

The man page for sendfile says about the error code EINVAL: Descriptor is not valid or locked, or an mmap(2)-like operation is not available for in_fd.

You can find other info about it in the man page that may be useful for your particular files.

manjuhr 08-05-2013 12:49 PM

Hi,

Thanks for quick reply ...

The input and output files have read/write permissions.

I tested code on "3.0.80-0.7-ppc64 #1 SMP Tue Jun 25 18:32:49 UTC 2013 (25740f8) ppc64 GNU/Linux" and test passes.

I am seeing the issue only : "Linux 2.6.18-366.el5 #1 SMP Fri Jul 19 13:36:15 EDT 2013 ppc64 ppc64 ppc64 GNU/Linux"

Regarding man page: There is a reference to kernel release -
---------------------
Presently (Linux 2.6.9): in_fd, must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket); and out_fd must refer to a socket.
---------------------

Following link also mentions "sendfile()" supports regular file only after "since Linux 2.6.33".
Link: http://marc.info/?l=linux-man&m=129767829918385

So want to know if sendfile() supports regular file for "fd_out".


All times are GMT -5. The time now is 08:44 AM.