share/mk/: Fix includes
[man-pages.git] / man2 / splice.2
blobe5d05a05c130f471ba421aeb95564f516c3e556d
1 .\" This manpage is Copyright (C) 2006 Jens Axboe
2 .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH splice 2 (date) "Linux man-pages (unreleased)"
7 .SH NAME
8 splice \- splice data to/from a pipe
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ", " \-lc )
12 .SH SYNOPSIS
13 .nf
14 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
15 .B #define _FILE_OFFSET_BITS  64
16 .B #include <fcntl.h>
18 .BI "ssize_t splice(int " fd_in ", off_t *_Nullable " off_in ,
19 .BI "               int " fd_out ", off_t *_Nullable " off_out ,
20 .BI "               size_t " len ", unsigned int " flags );
21 .\" Return type was long before glibc 2.7
22 .fi
23 .SH DESCRIPTION
24 .BR splice ()
25 moves data between two file descriptors
26 without copying between kernel address space and user address space.
27 It transfers up to
28 .I len
29 bytes of data from the file descriptor
30 .I fd_in
31 to the file descriptor
32 .IR fd_out ,
33 where one of the file descriptors must refer to a pipe.
35 The following semantics apply for
36 .I fd_in
37 and
38 .IR off_in :
39 .IP \[bu] 3
41 .I fd_in
42 refers to a pipe, then
43 .I off_in
44 must be NULL.
45 .IP \[bu]
47 .I fd_in
48 does not refer to a pipe and
49 .I off_in
50 is NULL, then bytes are read from
51 .I fd_in
52 starting from the file offset,
53 and the file offset is adjusted appropriately.
54 .IP \[bu]
56 .I fd_in
57 does not refer to a pipe and
58 .I off_in
59 is not NULL, then
60 .I off_in
61 must point to a buffer which specifies the starting
62 offset from which bytes will be read from
63 .IR fd_in ;
64 in this case, the file offset of
65 .I fd_in
66 is not changed.
68 Analogous statements apply for
69 .I fd_out
70 and
71 .IR off_out .
73 The
74 .I flags
75 argument is a bit mask that is composed by ORing together
76 zero or more of the following values:
77 .TP
78 .B SPLICE_F_MOVE
79 Attempt to move pages instead of copying.
80 This is only a hint to the kernel:
81 pages may still be copied if the kernel cannot move the
82 pages from the pipe, or if
83 the pipe buffers don't refer to full pages.
84 The initial implementation of this flag was buggy:
85 therefore starting in Linux 2.6.21 it is a no-op
86 (but is still permitted in a
87 .BR splice ()
88 call);
89 in the future, a correct implementation may be restored.
90 .TP
91 .B SPLICE_F_NONBLOCK
92 Do not block on I/O.
93 This makes the splice pipe operations nonblocking, but
94 .BR splice ()
95 may nevertheless block because the file descriptors that
96 are spliced to/from may block (unless they have the
97 .B O_NONBLOCK
98 flag set).
99 .TP
100 .B SPLICE_F_MORE
101 More data will be coming in a subsequent splice.
102 This is a helpful hint when
104 .I fd_out
105 refers to a socket (see also the description of
106 .B MSG_MORE
108 .BR send (2),
109 and the description of
110 .B TCP_CORK
112 .BR tcp (7)).
114 .B SPLICE_F_GIFT
115 Unused for
116 .BR splice ();
118 .BR vmsplice (2).
119 .SH RETURN VALUE
120 Upon successful completion,
121 .BR splice ()
122 returns the number of bytes
123 spliced to or from the pipe.
125 A return value of 0 means end of input.
127 .I fd_in
128 refers to a pipe, then this means that there was no data to transfer,
129 and it would not make sense to block because there are no writers
130 connected to the write end of the pipe.
132 On error,
133 .BR splice ()
134 returns \-1 and
135 .I errno
136 is set to indicate the error.
137 .SH ERRORS
139 .B EAGAIN
140 .B SPLICE_F_NONBLOCK
141 was specified in
142 .I flags
143 or one of the file descriptors had been marked as nonblocking
144 .RB ( O_NONBLOCK ) ,
145 and the operation would block.
147 .B EBADF
148 One or both file descriptors are not valid,
149 or do not have proper read-write mode.
151 .B EINVAL
152 The target filesystem doesn't support splicing.
154 .B EINVAL
155 The target file is opened in append mode.
156 .\" The append-mode error is given since Linux 2.6.27; in earlier kernels,
157 .\" splice() in append mode was broken
159 .B EINVAL
160 Neither of the file descriptors refers to a pipe.
162 .B EINVAL
163 An offset was given for nonseekable device (e.g., a pipe).
165 .B EINVAL
166 .I fd_in
168 .I fd_out
169 refer to the same pipe.
171 .B ENOMEM
172 Out of memory.
174 .B ESPIPE
175 Either
176 .I off_in
178 .I off_out
179 was not NULL, but the corresponding file descriptor refers to a pipe.
180 .SH STANDARDS
181 Linux.
182 .SH HISTORY
183 Linux 2.6.17,
184 glibc 2.5.
186 In Linux 2.6.30 and earlier,
187 exactly one of
188 .I fd_in
190 .I fd_out
191 was required to be a pipe.
192 Since Linux 2.6.31,
193 .\" commit 7c77f0b3f9208c339a4b40737bb2cb0f0319bb8d
194 both arguments may refer to pipes.
195 .SH NOTES
196 The three system calls
197 .BR splice (),
198 .BR vmsplice (2),
200 .BR tee (2),
201 provide user-space programs with full control over an arbitrary
202 kernel buffer, implemented within the kernel using the same type
203 of buffer that is used for a pipe.
204 In overview, these system calls perform the following tasks:
206 .BR splice ()
207 moves data from the buffer to an arbitrary file descriptor, or vice versa,
208 or from one buffer to another.
210 .BR tee (2)
211 "copies" the data from one buffer to another.
213 .BR vmsplice (2)
214 "copies" data from user space into the buffer.
216 Though we talk of copying, actual copies are generally avoided.
217 The kernel does this by implementing a pipe buffer as a set
218 of reference-counted pointers to pages of kernel memory.
219 The kernel creates "copies" of pages in a buffer by creating new
220 pointers (for the output buffer) referring to the pages,
221 and increasing the reference counts for the pages:
222 only pointers are copied, not the pages of the buffer.
224 .\" Linus: Now, imagine using the above in a media server, for example.
225 .\" Let's say that a year or two has passed, so that the video drivers
226 .\" have been updated to be able to do the splice thing, and what can
227 .\" you do? You can:
229 .\" - splice from the (mpeg or whatever - let's just assume that the video
230 .\"   input is either digital or does the encoding on its own - like they
231 .\"   pretty much all do) video input into a pipe (remember: no copies - the
232 .\"   video input will just DMA directly into memory, and splice will just
233 .\"   set up the pages in the pipe buffer)
234 .\" - tee that pipe to split it up
235 .\" - splice one end to a file (ie "save the compressed stream to disk")
236 .\" - splice the other end to a real-time video decoder window for your
237 .\"   real-time viewing pleasure.
239 .\" Linus: Now, the advantage of splice()/tee() is that you can
240 .\" do zero-copy movement of data, and unlike sendfile() you can
241 .\" do it on _arbitrary_ data (and, as shown by "tee()", it's more
242 .\" than just sending the data to somebody else: you can duplicate
243 .\" the data and choose to forward it to two or more different
244 .\" users - for things like logging etc.).
247 .B _FILE_OFFSET_BITS
248 should be defined to be 64 in code that uses non-null
249 .I off_in
251 .I off_out
252 or that takes the address of
253 .BR splice ,
254 if the code is intended to be portable
255 to traditional 32-bit x86 and ARM platforms where
256 .BR off_t 's
257 width defaults to 32 bits.
258 .SH EXAMPLES
260 .BR tee (2).
261 .SH SEE ALSO
262 .BR copy_file_range (2),
263 .BR sendfile (2),
264 .BR tee (2),
265 .BR vmsplice (2),
266 .BR pipe (7)