1SENDFILE(2)                Linux Programmer's Manual               SENDFILE(2)
2
3
4

NAME

6       sendfile - transfer data between file descriptors
7

SYNOPSIS

9       #include <sys/sendfile.h>
10
11       ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
12

DESCRIPTION

14       sendfile()  copies  data  between  one  file  descriptor  and  another.
15       Because this copying is done within  the  kernel,  sendfile()  is  more
16       efficient  than  the  combination  of read(2) and write(2), which would
17       require transferring data to and from user space.
18
19       in_fd should be a file descriptor opened for reading and out_fd  should
20       be a descriptor opened for writing.
21
22       If  offset  is  not NULL, then it points to a variable holding the file
23       offset from which sendfile() will start reading data from in_fd.   When
24       sendfile() returns, this variable will be set to the offset of the byte
25       following the last byte that was read.  If offset  is  not  NULL,  then
26       sendfile() does not modify the file offset of in_fd; otherwise the file
27       offset is adjusted to reflect the number of bytes read from in_fd.
28
29       If offset is NULL, then data will be read from in_fd  starting  at  the
30       file offset, and the file offset will be updated by the call.
31
32       count is the number of bytes to copy between the file descriptors.
33
34       The   in_fd   argument   must  correspond  to  a  file  which  supports
35       mmap(2)-like operations (i.e., it cannot be a socket).
36
37       In Linux kernels before 2.6.33, out_fd must refer to a  socket.   Since
38       Linux  2.6.33  it can be any file.  If it is a regular file, then send‐
39       file() changes the file offset appropriately.
40

RETURN VALUE

42       If the transfer was successful, the number of bytes written  to  out_fd
43       is returned.  Note that a successful call to sendfile() may write fewer
44       bytes than requested; the caller should be prepared to retry  the  call
45       if there were unsent bytes.  See also NOTES.
46
47       On error, -1 is returned, and errno is set appropriately.
48

ERRORS

50       EAGAIN Nonblocking I/O has been selected using O_NONBLOCK and the write
51              would block.
52
53       EBADF  The input file was not opened for reading or the output file was
54              not opened for writing.
55
56       EFAULT Bad address.
57
58       EINVAL Descriptor  is not valid or locked, or an mmap(2)-like operation
59              is not available for in_fd, or count is negative.
60
61       EINVAL out_fd has the O_APPEND flag set.  This is  not  currently  sup‐
62              ported by sendfile().
63
64       EIO    Unspecified error while reading from in_fd.
65
66       ENOMEM Insufficient memory to read from in_fd.
67
68       EOVERFLOW
69              count  is too large, the operation would result in exceeding the
70              maximum size of either the input file or the output file.
71
72       ESPIPE offset is not NULL but the input file is not seek(2)-able.
73

VERSIONS

75       sendfile() first appeared in Linux 2.2.  The  include  file  <sys/send‐
76       file.h> is present since glibc 2.1.
77

CONFORMING TO

79       Not specified in POSIX.1-2001, nor in other standards.
80
81       Other  UNIX  systems  implement sendfile() with different semantics and
82       prototypes.  It should not be used in portable programs.
83

NOTES

85       sendfile() will transfer  at  most  0x7ffff000  (2,147,479,552)  bytes,
86       returning  the  number of bytes actually transferred.  (This is true on
87       both 32-bit and 64-bit systems.)
88
89       If you plan to use sendfile() for sending files to a  TCP  socket,  but
90       need  to  send some header data in front of the file contents, you will
91       find it useful to employ the TCP_CORK option, described in  tcp(7),  to
92       minimize the number of packets and to tune performance.
93
94       In  Linux  2.4  and earlier, out_fd could also refer to a regular file;
95       this possibility went away in the Linux 2.6.x kernel  series,  but  was
96       restored in Linux 2.6.33.
97
98       The  original  Linux  sendfile() system call was not designed to handle
99       large file offsets.  Consequently, Linux 2.4 added sendfile64(), with a
100       wider type for the offset argument.  The glibc sendfile() wrapper func‐
101       tion transparently deals with the kernel differences.
102
103       Applications may wish to fall back  to  read(2)/write(2)  in  the  case
104       where sendfile() fails with EINVAL or ENOSYS.
105
106       If  out_fd  refers  to a socket or pipe with zero-copy support, callers
107       must ensure the transferred portions of the file referred to  by  in_fd
108       remain  unmodified until the reader on the other end of out_fd has con‐
109       sumed the transferred data.
110
111       The Linux-specific splice(2) call supports  transferring  data  between
112       arbitrary file descriptors provided one (or both) of them is a pipe.
113

SEE ALSO

115       copy_file_range(2), mmap(2), open(2), socket(2), splice(2)
116

COLOPHON

118       This  page  is  part of release 4.16 of the Linux man-pages project.  A
119       description of the project, information about reporting bugs,  and  the
120       latest     version     of     this    page,    can    be    found    at
121       https://www.kernel.org/doc/man-pages/.
122
123
124
125Linux                             2017-09-15                       SENDFILE(2)
Impressum