Debian Codegraph @

upstream/fedora-44/man2/splice.2

raw
1 .\" This manpage is Copyright (C) 2006 Jens Axboe
2 .\" and Copyright (C) 2006 Michael Kerrisk <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH splice 2 2024-11-17 "Linux man-pages 6.13"
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>
17 .P
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 " size ", 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 size
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.
34 .P
35 The following semantics apply for
36 .I fd_in
37 and
38 .IR off_in :
39 .IP \[bu] 3
40 If
41 .I fd_in
42 refers to a pipe, then
43 .I off_in
44 must be NULL.
45 .IP \[bu]
46 If
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]
55 If
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,
67 and the offset pointed to by
68 .I off_in
69 is adjusted appropriately instead.
70 .P
71 Analogous statements apply for
72 .I fd_out
73 and
74 .IR off_out .
75 .P
76 The
77 .I flags
78 argument is a bit mask that is composed by ORing together
79 zero or more of the following values:
80 .TP
81 .B SPLICE_F_MOVE
82 Attempt to move pages instead of copying.
83 This is only a hint to the kernel:
84 pages may still be copied if the kernel cannot move the
85 pages from the pipe, or if
86 the pipe buffers don't refer to full pages.
87 The initial implementation of this flag was buggy:
88 therefore starting in Linux 2.6.21 it is a no-op
89 (but is still permitted in a
90 .BR splice ()
91 call);
92 in the future, a correct implementation may be restored.
93 .TP
94 .B SPLICE_F_NONBLOCK
95 Do not block on I/O.
96 This makes the splice pipe operations nonblocking, but
97 .BR splice ()
98 may nevertheless block because the file descriptors that
99 are spliced to/from may block (unless they have the
100 .B O_NONBLOCK
101 flag set).
102 .TP
103 .B SPLICE_F_MORE
104 More data will be coming in a subsequent splice.
105 This is a helpful hint when
106 the
107 .I fd_out
108 refers to a socket (see also the description of
109 .B MSG_MORE
110 in
111 .BR send (2),
112 and the description of
113 .B TCP_CORK
114 in
115 .BR tcp (7)).
116 .TP
117 .B SPLICE_F_GIFT
118 Unused for
119 .BR splice ();
120 see
121 .BR vmsplice (2).
122 .SH RETURN VALUE
123 Upon successful completion,
124 .BR splice ()
125 returns the number of bytes
126 spliced to or from the pipe.
127 .P
128 A return value of 0 means end of input.
129 If
130 .I fd_in
131 refers to a pipe, then this means that there was no data to transfer,
132 and it would not make sense to block because there are no writers
133 connected to the write end of the pipe.
134 .P
135 On error,
136 .BR splice ()
137 returns \-1 and
138 .I errno
139 is set to indicate the error.
140 .SH ERRORS
141 .TP
142 .B EAGAIN
143 .B SPLICE_F_NONBLOCK
144 was specified in
145 .I flags
146 or one of the file descriptors had been marked as nonblocking
147 .RB ( O_NONBLOCK ) ,
148 and the operation would block.
149 .TP
150 .B EBADF
151 One or both file descriptors are not valid,
152 or do not have proper read-write mode.
153 .TP
154 .B EINVAL
155 The target filesystem doesn't support splicing.
156 .TP
157 .B EINVAL
158 The target file is opened in append mode.
159 .\" The append-mode error is given since Linux 2.6.27; in earlier kernels,
160 .\" splice() in append mode was broken
161 .TP
162 .B EINVAL
163 Neither of the file descriptors refers to a pipe.
164 .TP
165 .B EINVAL
166 An offset was given for nonseekable device (e.g., a pipe).
167 .TP
168 .B EINVAL
169 .I fd_in
170 and
171 .I fd_out
172 refer to the same pipe.
173 .TP
174 .B ENOMEM
175 Out of memory.
176 .TP
177 .B ESPIPE
178 Either
179 .I off_in
180 or
181 .I off_out
182 was not NULL, but the corresponding file descriptor refers to a pipe.
183 .SH STANDARDS
184 Linux.
185 .SH HISTORY
186 Linux 2.6.17,
187 glibc 2.5.
188 .P
189 In Linux 2.6.30 and earlier,
190 exactly one of
191 .I fd_in
192 and
193 .I fd_out
194 was required to be a pipe.
195 Since Linux 2.6.31,
196 .\" commit 7c77f0b3f9208c339a4b40737bb2cb0f0319bb8d
197 both arguments may refer to pipes.
198 .SH NOTES
199 The three system calls
200 .BR splice (),
201 .BR vmsplice (2),
202 and
203 .BR tee (2),
204 provide user-space programs with full control over an arbitrary
205 kernel buffer, implemented within the kernel using the same type
206 of buffer that is used for a pipe.
207 In overview, these system calls perform the following tasks:
208 .TP
209 .BR splice ()
210 moves data from the buffer to an arbitrary file descriptor, or vice versa,
211 or from one buffer to another.
212 .TP
213 .BR tee (2)
214 "copies" the data from one buffer to another.
215 .TP
216 .BR vmsplice (2)
217 "copies" data from user space into the buffer.
218 .P
219 Though we talk of copying, actual copies are generally avoided.
220 The kernel does this by implementing a pipe buffer as a set
221 of reference-counted pointers to pages of kernel memory.
222 The kernel creates "copies" of pages in a buffer by creating new
223 pointers (for the output buffer) referring to the pages,
224 and increasing the reference counts for the pages:
225 only pointers are copied, not the pages of the buffer.
226 .\"
227 .\" Linus: Now, imagine using the above in a media server, for example.
228 .\" Let's say that a year or two has passed, so that the video drivers
229 .\" have been updated to be able to do the splice thing, and what can
230 .\" you do? You can:
231 .\"
232 .\" - splice from the (mpeg or whatever - let's just assume that the video
233 .\" input is either digital or does the encoding on its own - like they
234 .\" pretty much all do) video input into a pipe (remember: no copies - the
235 .\" video input will just DMA directly into memory, and splice will just
236 .\" set up the pages in the pipe buffer)
237 .\" - tee that pipe to split it up
238 .\" - splice one end to a file (ie "save the compressed stream to disk")
239 .\" - splice the other end to a real-time video decoder window for your
240 .\" real-time viewing pleasure.
241 .\"
242 .\" Linus: Now, the advantage of splice()/tee() is that you can
243 .\" do zero-copy movement of data, and unlike sendfile() you can
244 .\" do it on _arbitrary_ data (and, as shown by "tee()", it's more
245 .\" than just sending the data to somebody else: you can duplicate
246 .\" the data and choose to forward it to two or more different
247 .\" users - for things like logging etc.).
248 .\"
249 .P
250 .B _FILE_OFFSET_BITS
251 should be defined to be 64 in code that uses non-null
252 .I off_in
253 or
254 .I off_out
255 or that takes the address of
256 .BR splice ,
257 if the code is intended to be portable
258 to traditional 32-bit x86 and ARM platforms where
259 .BR off_t 's
260 width defaults to 32 bits.
261 .SH EXAMPLES
262 See
263 .BR tee (2)
264 for another example.
265 .P
266 .\" SRC BEGIN (splice.c)
267 .EX
268 \&
269 #define _GNU_SOURCE
270 #define _FILE_OFFSET_BITS 64
271 #include <err.h>
272 #include <fcntl.h>
273 #include <stdint.h>
274 #include <stdio.h>
275 #include <stdlib.h>
276 #include <sys/types.h>
277 #include <unistd.h>
278 \&
279 int
280 main(void)
281 {
282 int fd;
283 int pfd[2];
284 off_t off;
285 const char s[12] = "Hello, world";
286 \&
287 fd = open("out", O_WRONLY | O_CREAT | O_EXCL, 0666);
288 if (fd == \-1)
289 err(EXIT_FAILURE, "open");
290 \&
291 if (pipe(pfd) == \-1)
292 err(EXIT_FAILURE, "pipe");
293 \&
294 if (write(pfd[1], s, sizeof(s)) != sizeof(s))
295 err(EXIT_FAILURE, "write");
296 if (close(pfd[1]) == \-1)
297 err(EXIT_FAILURE, "close");
298 \&
299 off = 10;
300 if (splice(pfd[0], NULL, fd, &off, sizeof(s), 0) != sizeof(s))
301 err(EXIT_FAILURE, "splice");
302 if (close(pfd[0]) == \-1)
303 err(EXIT_FAILURE, "close");
304 \&
305 printf("New offset is %jd\[rs]n", (intmax_t) off);
306 \&
307 if (close(fd) == \-1)
308 err(EXIT_FAILURE, "close");
309 exit(EXIT_SUCCESS);
310 }
311 .EE
312 .\" SRC END
313 .SH SEE ALSO
314 .BR copy_file_range (2),
315 .BR sendfile (2),
316 .BR tee (2),
317 .BR vmsplice (2),
318 .BR pipe (7)