LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem Calling Sendfile from C Code (https://www.linuxquestions.org/questions/linux-newbie-8/problem-calling-sendfile-from-c-code-4175616846/)

quantumxaos 11-02-2017 11:51 AM

Hi, I'm trying to run a C code I found online using an old distribution of SLAX Linux 5.1.7 and Slackware 11.0. The linux kernel for this distribution is 2.6.16. The error occurs when executing the program and it calls sendfile giving a value of -1 and the error "sendfile(): Invalid argument". I've reproduced the relevant code below:

Moderator edit: Merged from a parallel question, using the same code example:
Quote:

Hi, I'm trying to run something similar for linux kernel 2.6.16, but instead of output to a file, I was trying to output to a socket since you mentioned that sendfile wouldn't support regular files for out_fd until kernel 2.6.33. I still get the error "sendfile(): Invalid Argument" even when out_fd is a socket.

I was wondering if you ever figured out why sendfile works in the newer linux kernels but not for 2.6.18? Also, is there a way to fix the error for the older linux kernel?
Code:

     
        int fd_in=0,fd_out=0,offset=1;

    if((fd_in=open(argv[0],O_RDONLY))==-1){
        perror("[-] open()");
        return -1;
    }

    if((fd_out=socket(PF_APPLETALK,SOCK_DGRAM,0))==-1){
        if((fd_out=socket(PF_BLUETOOTH,SOCK_DGRAM,0))==-1){
            perror("[-] socket()");
            return -1;
        }
    }

    if(sendfile(fd_out,fd_in,&offset,2)==-1){
        if(offset==0){
            perror("[-] sendfile()");
            return -1;
        }
    }

Running strace on this, gives sendfile(4,3,[1],2) = -1 EINVAL (Invalid Argument). I've googled this error and have seen others solve it with newer versions of the linux kernel.

My question is what is causing this error for the version I am using, how can I fix it, and what has changed in newer kernel versions to fix/change the behavior of sendfile?

Thanks for any help.

pan64 11-03-2017 04:54 AM

see man page:
EINVAL Descriptor is not valid or locked, or an mmap(2)-like operation is not available for in_fd.


All times are GMT -5. The time now is 04:58 PM.