]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/sendfile.2
Formatting fixes
[thirdparty/man-pages.git] / man2 / sendfile.2
1 .\" This man page is Copyright (C) 1998 Pawel Krawczyk.
2 .\" Permission is granted to distribute possibly modified copies
3 .\" of this page provided the header is included verbatim,
4 .\" and in case of nontrivial modification author and date
5 .\" of the modification is added to the header.
6 .\" $Id: sendfile.2,v 1.5 1999/05/18 11:54:11 freitag Exp $
7 .\" 2000-11-19 bert hubert <ahu@ds9a.nl>: in_fd cannot be socket
8 .\"
9 .\" 2004-12-17, mtk
10 .\" updated description of in_fd and out_fd for 2.6
11 .\" Various wording and formatting changes
12 .\"
13 .\" 2005-03-31 Martin Pool <mbp@sourcefrog.net> mmap() improvements
14 .\"
15 .TH SENDFILE 2 2004-12-17 "Linux Man Page" "Linux Programmer's Manual"
16 .SH NAME
17 sendfile \- transfer data between file descriptors
18 .SH SYNOPSIS
19 .B #include <sys/sendfile.h>
20 .sp
21 .BI "ssize_t sendfile(int" " out_fd" ", int" " in_fd" ", off_t *" \
22 offset ", size_t" " count" );
23 .\" The below is too ugly. Comments about glibc versions belong
24 .\" in the notes, not in the header.
25 .\"
26 .\" .B #include <features.h>
27 .\" .br
28 .\" .B #if (__GLIBC__==2 && __GLIBC_MINOR__>=1) || __GLIBC__>2
29 .\" .br
30 .\" .B #include <sys/sendfile.h>
31 .\" .br
32 .\" #else
33 .\" .br
34 .\" .B #include <sys/types.h>
35 .\" .br
36 .\" .B /* No system prototype before glibc 2.1. */
37 .\" .br
38 .\" .BI "ssize_t sendfile(int" " out_fd" ", int" " in_fd" ", off_t *" \
39 .\" offset ", size_t" " count" )
40 .\" .br
41 .\" .B #endif
42 .\"
43 .SH DESCRIPTION
44 .BR sendfile ()
45 copies data between one file descriptor and another.
46 Because this copying is done within the kernel,
47 .BR sendfile ()
48 is more efficient than the combination of
49 .BR read (2)
50 and
51 .BR write (2),
52 which would require transferring data to and from user space.
53
54 .I in_fd
55 should be a file descriptor opened for reading and
56 .I out_fd
57 should be a descriptor opened for writing.
58
59 .I offset
60 is a pointer to a variable holding the file offset from
61 which
62 .BR sendfile ()
63 will start reading data from
64 .IR in_fd .
65 When
66 .BR sendfile ()
67 returns, this variable
68 will be set to the offset of the byte following the last byte that was read.
69 .BR sendfile ()
70 does not modify the current file offset of
71 .IR in_fd .
72
73 .I count
74 is the number of bytes to copy between the file descriptors.
75
76 Presently (Linux 2.6.9):
77 .IR in_fd ,
78 must correspond to a file which supports
79 .BR mmap ()-like
80 operations
81 (i.e., it cannot be a socket);
82 and
83 .I out_fd
84 must refer to a socket.
85
86 Applications may wish to fall back to
87 .BR read (2)/ write (2)
88 in the case where
89 .BR sendfile ()
90 fails with EINVAL or ENOSYS.
91 .SH NOTES
92 If you plan to use
93 .BR sendfile ()
94 for sending files to a TCP socket, but need
95 to send some header data in front of the file contents, you will find
96 it useful to employ the
97 .B TCP_CORK
98 option, described in
99 .BR tcp (7),
100 to minimize the number of packets and to tune performance.
101
102 In Linux 2.4 and earlier,
103 .I out_fd
104 could refer to a regular file, and
105 .BR sendfile ()
106 changed the current offset of that file.
107 .SH "RETURN VALUE"
108 If the transfer was successful, the number of bytes written to
109 .I out_fd
110 is returned. On error, \-1 is returned, and
111 .I errno
112 is set appropriately.
113 .SH ERRORS
114 .TP
115 .B EAGAIN
116 Non-blocking I/O has been selected using
117 .B O_NONBLOCK
118 and the write would block.
119 .TP
120 .B EBADF
121 The input file was not opened for reading or the output file
122 was not opened for writing.
123 .TP
124 .B EFAULT
125 Bad address.
126 .TP
127 .B EINVAL
128 Descriptor is not valid or locked, or an
129 .BR mmap ()-like
130 operation is not available for
131 .IR in_fd .
132 .TP
133 .B EIO
134 Unspecified error while reading from
135 .IR in_fd .
136 .TP
137 .B ENOMEM
138 Insufficient memory to read from
139 .IR in_fd .
140 .SH VERSIONS
141 .BR sendfile ()
142 is a new feature in Linux 2.2.
143 The include file <sys/sendfile.h> is present since glibc2.1.
144
145 Other Unixes implement
146 .BR sendfile ()
147 with different semantics and prototypes. It should
148 not be used in portable programs.
149 .SH "SEE ALSO"
150 .BR open (2),
151 .BR mmap (2),
152 .BR socket (2)