1sendfile(2) System Calls Manual sendfile(2)
2
3
4
6 sendfile - transfer data between file descriptors
7
9 Standard C library (libc, -lc)
10
12 #include <sys/sendfile.h>
13
14 ssize_t sendfile(int out_fd, int in_fd, off_t *_Nullable offset,
15 size_t count);
16
18 sendfile() copies data between one file descriptor and another. Be‐
19 cause this copying is done within the kernel, sendfile() is more effi‐
20 cient than the combination of read(2) and write(2), which would require
21 transferring data to and from user space.
22
23 in_fd should be a file descriptor opened for reading and out_fd should
24 be a descriptor opened for writing.
25
26 If offset is not NULL, then it points to a variable holding the file
27 offset from which sendfile() will start reading data from in_fd. When
28 sendfile() returns, this variable will be set to the offset of the byte
29 following the last byte that was read. If offset is not NULL, then
30 sendfile() does not modify the file offset of in_fd; otherwise the file
31 offset is adjusted to reflect the number of bytes read from in_fd.
32
33 If offset is NULL, then data will be read from in_fd starting at the
34 file offset, and the file offset will be updated by the call.
35
36 count is the number of bytes to copy between the file descriptors.
37
38 The in_fd argument must correspond to a file which supports
39 mmap(2)-like operations (i.e., it cannot be a socket).
40
41 Before Linux 2.6.33, out_fd must refer to a socket. Since Linux 2.6.33
42 it can be any file. If it is a regular file, then sendfile() changes
43 the file offset appropriately.
44
46 If the transfer was successful, the number of bytes written to out_fd
47 is returned. Note that a successful call to sendfile() may write fewer
48 bytes than requested; the caller should be prepared to retry the call
49 if there were unsent bytes. See also NOTES.
50
51 On error, -1 is returned, and errno is set to indicate the error.
52
54 EAGAIN Nonblocking I/O has been selected using O_NONBLOCK and the write
55 would block.
56
57 EBADF The input file was not opened for reading or the output file was
58 not opened for writing.
59
60 EFAULT Bad address.
61
62 EINVAL Descriptor is not valid or locked, or an mmap(2)-like operation
63 is not available for in_fd, or count is negative.
64
65 EINVAL out_fd has the O_APPEND flag set. This is not currently sup‐
66 ported by sendfile().
67
68 EIO Unspecified error while reading from in_fd.
69
70 ENOMEM Insufficient memory to read from in_fd.
71
72 EOVERFLOW
73 count is too large, the operation would result in exceeding the
74 maximum size of either the input file or the output file.
75
76 ESPIPE offset is not NULL but the input file is not seekable.
77
79 Other UNIX systems implement sendfile() with different semantics and
80 prototypes. It should not be used in portable programs.
81
83 None.
84
86 Linux 2.2, glibc 2.1.
87
88 In Linux 2.4 and earlier, out_fd could also refer to a regular file;
89 this possibility went away in the Linux 2.6.x kernel series, but was
90 restored in Linux 2.6.33.
91
92 The original Linux sendfile() system call was not designed to handle
93 large file offsets. Consequently, Linux 2.4 added sendfile64(), with a
94 wider type for the offset argument. The glibc sendfile() wrapper func‐
95 tion transparently deals with the kernel differences.
96
98 sendfile() will transfer at most 0x7ffff000 (2,147,479,552) bytes, re‐
99 turning the number of bytes actually transferred. (This is true on
100 both 32-bit and 64-bit systems.)
101
102 If you plan to use sendfile() for sending files to a TCP socket, but
103 need to send some header data in front of the file contents, you will
104 find it useful to employ the TCP_CORK option, described in tcp(7), to
105 minimize the number of packets and to tune performance.
106
107 Applications may wish to fall back to read(2) and write(2) in the case
108 where sendfile() fails with EINVAL or ENOSYS.
109
110 If out_fd refers to a socket or pipe with zero-copy support, callers
111 must ensure the transferred portions of the file referred to by in_fd
112 remain unmodified until the reader on the other end of out_fd has con‐
113 sumed the transferred data.
114
115 The Linux-specific splice(2) call supports transferring data between
116 arbitrary file descriptors provided one (or both) of them is a pipe.
117
119 copy_file_range(2), mmap(2), open(2), socket(2), splice(2)
120
121
122
123Linux man-pages 6.04 2023-03-30 sendfile(2)