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