]>
Commit | Line | Data |
---|---|---|
2bc4bb77 | 1 | .\" This manpage is Copyright (C) 2006 Jens Axboe |
c11b1abf | 2 | .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com> |
2bc4bb77 | 3 | .\" |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
2bc4bb77 MK |
5 | .\" Permission is granted to make and distribute verbatim copies of this |
6 | .\" manual provided the copyright notice and this permission notice are | |
7 | .\" preserved on all copies. | |
8 | .\" | |
9 | .\" Permission is granted to copy and distribute modified versions of this | |
10 | .\" manual under the conditions for verbatim copying, provided that the | |
11 | .\" entire resulting derived work is distributed under the terms of a | |
12 | .\" permission notice identical to this one. | |
c13182ef | 13 | .\" |
2bc4bb77 MK |
14 | .\" Since the Linux kernel and libraries are constantly changing, this |
15 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
16 | .\" responsibility for errors or omissions, or for damages resulting from | |
17 | .\" the use of the information contained herein. The author(s) may not | |
18 | .\" have taken the same level of care in the production of this manual, | |
19 | .\" which is licensed free of charge, as they might when working | |
20 | .\" professionally. | |
c13182ef | 21 | .\" |
2bc4bb77 MK |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
2bc4bb77 | 25 | .\" |
c95b6ae1 | 26 | .TH SPLICE 2 2012-05-04 "Linux" "Linux Programmer's Manual" |
2bc4bb77 MK |
27 | .SH NAME |
28 | splice \- splice data to/from a pipe | |
29 | .SH SYNOPSIS | |
30 | .nf | |
b80f966b | 31 | .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" |
2bc4bb77 MK |
32 | .B #include <fcntl.h> |
33 | ||
52520fb5 MH |
34 | .BI "ssize_t splice(int " fd_in ", loff_t *" off_in ", int " fd_out , |
35 | .BI " loff_t *" off_out ", size_t " len \ | |
2bc4bb77 | 36 | ", unsigned int " flags ); |
52520fb5 | 37 | .\" Return type was long before glibc 2.7 |
2bc4bb77 MK |
38 | .fi |
39 | .SH DESCRIPTION | |
40 | .BR splice () | |
c13182ef | 41 | moves data between two file descriptors |
2bc4bb77 MK |
42 | without copying between kernel address space and user address space. |
43 | It transfers up to | |
44 | .I len | |
45 | bytes of data from the file descriptor | |
46 | .I fd_in | |
47 | to the file descriptor | |
48 | .IR fd_out , | |
c13182ef | 49 | where one of the descriptors must refer to a pipe. |
2bc4bb77 MK |
50 | |
51 | If | |
ae39cf8f | 52 | .I fd_in |
2bc4bb77 | 53 | refers to a pipe, then |
c13182ef | 54 | .I off_in |
2bc4bb77 MK |
55 | must be NULL. |
56 | If | |
c13182ef | 57 | .I fd_in |
2bc4bb77 | 58 | does not refer to a pipe and |
ae39cf8f | 59 | .I off_in |
c13182ef | 60 | is NULL, then bytes are read from |
ae39cf8f | 61 | .I fd_in |
2bc4bb77 MK |
62 | starting from the current file offset, |
63 | and the current file offset is adjusted appropriately. | |
64 | If | |
c13182ef | 65 | .I fd_in |
2bc4bb77 | 66 | does not refer to a pipe and |
ae39cf8f | 67 | .I off_in |
c13182ef | 68 | is not NULL, then |
ae39cf8f | 69 | .I off_in |
2bc4bb77 MK |
70 | must point to a buffer which specifies the starting |
71 | offset from which bytes will be read from | |
ae39cf8f | 72 | .IR fd_in ; |
2bc4bb77 | 73 | in this case, the current file offset of |
0daa9e92 | 74 | .I fd_in |
2bc4bb77 MK |
75 | is not changed. |
76 | Analogous statements apply for | |
e267688b | 77 | .I fd_out |
2bc4bb77 | 78 | and |
ae39cf8f | 79 | .IR off_out . |
2bc4bb77 MK |
80 | |
81 | The | |
82 | .I flags | |
83 | argument is a bit mask that is composed by ORing together | |
84 | zero or more of the following values: | |
85 | .TP 1.9i | |
86 | .B SPLICE_F_MOVE | |
c13182ef | 87 | Attempt to move pages instead of copying. |
2bc4bb77 | 88 | This is only a hint to the kernel: |
c13182ef | 89 | pages may still be copied if the kernel cannot move the |
2bc4bb77 MK |
90 | pages from the pipe, or if |
91 | the pipe buffers don't refer to full pages. | |
986992f3 MK |
92 | The initial implementation of this flag was buggy: |
93 | therefore starting in Linux 2.6.21 it is a no-op | |
94 | (but is still permitted in a | |
95 | .BR splice () | |
96 | call); | |
97 | in the future, a correct implementation may be restored. | |
2bc4bb77 MK |
98 | .TP |
99 | .B SPLICE_F_NONBLOCK | |
100 | Do not block on I/O. | |
ff40dbb3 | 101 | This makes the splice pipe operations nonblocking, but |
2bc4bb77 MK |
102 | .BR splice () |
103 | may nevertheless block because the file descriptors that | |
104 | are spliced to/from may block (unless they have the | |
0daa9e92 | 105 | .B O_NONBLOCK |
2bc4bb77 MK |
106 | flag set). |
107 | .TP | |
108 | .B SPLICE_F_MORE | |
109 | More data will be coming in a subsequent splice. | |
110 | This is a helpful hint when | |
c13182ef | 111 | the |
2bc4bb77 MK |
112 | .I fd_out |
113 | refers to a socket (see also the description of | |
114 | .B MSG_MORE | |
115 | in | |
116 | .BR send (2), | |
117 | and the description of | |
118 | .B TCP_CORK | |
119 | in | |
120 | .BR tcp (7)) | |
121 | .TP | |
122 | .B SPLICE_F_GIFT | |
123 | Unused for | |
124 | .BR splice (); | |
125 | see | |
126 | .BR vmsplice (2). | |
127 | .SH RETURN VALUE | |
128 | Upon successful completion, | |
129 | .BR splice () | |
130 | returns the number of bytes | |
c13182ef MK |
131 | spliced to or from the pipe. |
132 | A return value of 0 means that there was no data to transfer, | |
133 | and it would not make sense to block, because there are no | |
134 | writers connected to the write end of the pipe referred to by | |
2bc4bb77 MK |
135 | .IR fd_in . |
136 | ||
c13182ef | 137 | On error, |
2bc4bb77 MK |
138 | .BR splice () |
139 | returns \-1 and | |
140 | .I errno | |
141 | is set to indicate the error. | |
142 | .SH ERRORS | |
143 | .TP | |
0655aa15 MK |
144 | .B EAGAIN |
145 | .B SPLICE_F_NONBLOCK | |
146 | was specified in | |
147 | .IR flags , | |
148 | and the operation would block. | |
149 | .TP | |
2bc4bb77 | 150 | .B EBADF |
c13182ef | 151 | One or both file descriptors are not valid, |
2bc4bb77 MK |
152 | or do not have proper read-write mode. |
153 | .TP | |
154 | .B EINVAL | |
9ee4a2b6 | 155 | Target filesystem doesn't support splicing; |
28081592 MH |
156 | target file is opened in append mode; |
157 | .\" The append-mode error is given since 2.6.27; in earlier kernels, | |
158 | .\" splice() in append mode was broken | |
c13182ef | 159 | neither of the descriptors refers to a pipe; or |
24b74457 | 160 | offset given for nonseekable device. |
2bc4bb77 MK |
161 | .TP |
162 | .B ENOMEM | |
163 | Out of memory. | |
164 | .TP | |
165 | .B ESPIPE | |
c13182ef | 166 | Either |
2bc4bb77 | 167 | .I off_in |
c13182ef | 168 | or |
2bc4bb77 MK |
169 | .I off_out |
170 | was not NULL, but the corresponding file descriptor refers to a pipe. | |
ff457ccb | 171 | .SH VERSIONS |
2bc4bb77 | 172 | The |
2777b1ca | 173 | .BR splice () |
c95b6ae1 MK |
174 | system call first appeared in Linux 2.6.17; |
175 | library support was added to glibc in version 2.5. | |
47297adb | 176 | .SH CONFORMING TO |
8382f16d | 177 | This system call is Linux-specific. |
2bc4bb77 MK |
178 | .SH NOTES |
179 | The three system calls | |
2777b1ca | 180 | .BR splice (), |
2bc4bb77 MK |
181 | .BR vmsplice (2), |
182 | and | |
e267688b | 183 | .BR tee (2), |
7fac88a9 | 184 | provide user-space programs with full control over an arbitrary |
2bc4bb77 | 185 | kernel buffer, implemented within the kernel using the same type |
c13182ef | 186 | of buffer that is used for a pipe. |
2bc4bb77 MK |
187 | In overview, these system calls perform the following tasks: |
188 | .TP 1.2i | |
189 | .BR splice () | |
190 | moves data from the buffer to an arbitrary file descriptor, or vice versa, | |
191 | or from one buffer to another. | |
192 | .TP | |
0bfa087b | 193 | .BR tee (2) |
2bc4bb77 MK |
194 | "copies" the data from one buffer to another. |
195 | .TP | |
0bfa087b | 196 | .BR vmsplice (2) |
2bc4bb77 MK |
197 | "copies" data from user space into the buffer. |
198 | .PP | |
199 | Though we talk of copying, actual copies are generally avoided. | |
c13182ef | 200 | The kernel does this by implementing a pipe buffer as a set |
2bc4bb77 | 201 | of reference-counted pointers to pages of kernel memory. |
c13182ef MK |
202 | The kernel creates "copies" of pages in a buffer by creating new |
203 | pointers (for the output buffer) referring to the pages, | |
204 | and increasing the reference counts for the pages: | |
2bc4bb77 MK |
205 | only pointers are copied, not the pages of the buffer. |
206 | .\" | |
c13182ef MK |
207 | .\" Linus: Now, imagine using the above in a media server, for example. |
208 | .\" Let's say that a year or two has passed, so that the video drivers | |
209 | .\" have been updated to be able to do the splice thing, and what can | |
2bc4bb77 | 210 | .\" you do? You can: |
c13182ef | 211 | .\" |
2bc4bb77 MK |
212 | .\" - splice from the (mpeg or whatever - let's just assume that the video |
213 | .\" input is either digital or does the encoding on its own - like they | |
214 | .\" pretty much all do) video input into a pipe (remember: no copies - the | |
c13182ef | 215 | .\" video input will just DMA directly into memory, and splice will just |
2bc4bb77 MK |
216 | .\" set up the pages in the pipe buffer) |
217 | .\" - tee that pipe to split it up | |
218 | .\" - splice one end to a file (ie "save the compressed stream to disk") | |
c13182ef | 219 | .\" - splice the other end to a real-time video decoder window for your |
2bc4bb77 MK |
220 | .\" real-time viewing pleasure. |
221 | .\" | |
c13182ef MK |
222 | .\" Linus: Now, the advantage of splice()/tee() is that you can |
223 | .\" do zero-copy movement of data, and unlike sendfile() you can | |
224 | .\" do it on _arbitrary_ data (and, as shown by "tee()", it's more | |
225 | .\" than just sending the data to somebody else: you can duplicate | |
226 | .\" the data and choose to forward it to two or more different | |
0967c11f | 227 | .\" users - for things like logging etc.). |
2bc4bb77 MK |
228 | .\" |
229 | .SH EXAMPLE | |
230 | See | |
231 | .BR tee (2). | |
2bc4bb77 MK |
232 | .SH SEE ALSO |
233 | .BR sendfile (2), | |
0a90178c | 234 | .BR tee (2), |
0a4f8b7b | 235 | .BR vmsplice (2) |