OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / 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 .\" %%%LICENSE_START(VERBATIM)
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.
13 .\"
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.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH SPLICE 2 2014-12-31 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 splice \- splice data to/from a pipe
29 .SH SYNOPSIS
30 .nf
31 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
32 .B #include <fcntl.h>
33
34 .BI "ssize_t splice(int " fd_in ", loff_t *" off_in ", int " fd_out ,
35 .BI "               loff_t *" off_out ", size_t " len \
36 ", unsigned int " flags );
37 .\" Return type was long before glibc 2.7
38 .fi
39 .SH DESCRIPTION
40 .BR splice ()
41 moves data between two file descriptors
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 ,
49 where one of the descriptors must refer to a pipe.
50
51 The following semantics apply for
52 .I fd_in
53 and
54 .IR off_in :
55 .IP * 3
56 If
57 .I fd_in
58 refers to a pipe, then
59 .I off_in
60 must be NULL.
61 .IP *
62 If
63 .I fd_in
64 does not refer to a pipe and
65 .I off_in
66 is NULL, then bytes are read from
67 .I fd_in
68 starting from the current file offset,
69 and the current file offset is adjusted appropriately.
70 .IP *
71 If
72 .I fd_in
73 does not refer to a pipe and
74 .I off_in
75 is not NULL, then
76 .I off_in
77 must point to a buffer which specifies the starting
78 offset from which bytes will be read from
79 .IR fd_in ;
80 in this case, the current file offset of
81 .I fd_in
82 is not changed.
83 .PP
84 Analogous statements apply for
85 .I fd_out
86 and
87 .IR off_out .
88
89 The
90 .I flags
91 argument is a bit mask that is composed by ORing together
92 zero or more of the following values:
93 .TP 1.9i
94 .B SPLICE_F_MOVE
95 Attempt to move pages instead of copying.
96 This is only a hint to the kernel:
97 pages may still be copied if the kernel cannot move the
98 pages from the pipe, or if
99 the pipe buffers don't refer to full pages.
100 The initial implementation of this flag was buggy:
101 therefore starting in Linux 2.6.21 it is a no-op
102 (but is still permitted in a
103 .BR splice ()
104 call);
105 in the future, a correct implementation may be restored.
106 .TP
107 .B SPLICE_F_NONBLOCK
108 Do not block on I/O.
109 This makes the splice pipe operations nonblocking, but
110 .BR splice ()
111 may nevertheless block because the file descriptors that
112 are spliced to/from may block (unless they have the
113 .B O_NONBLOCK
114 flag set).
115 .TP
116 .B SPLICE_F_MORE
117 More data will be coming in a subsequent splice.
118 This is a helpful hint when
119 the
120 .I fd_out
121 refers to a socket (see also the description of
122 .B MSG_MORE
123 in
124 .BR send (2),
125 and the description of
126 .B TCP_CORK
127 in
128 .BR tcp (7))
129 .TP
130 .B SPLICE_F_GIFT
131 Unused for
132 .BR splice ();
133 see
134 .BR vmsplice (2).
135 .SH RETURN VALUE
136 Upon successful completion,
137 .BR splice ()
138 returns the number of bytes
139 spliced to or from the pipe.
140 A return value of 0 means that there was no data to transfer,
141 and it would not make sense to block, because there are no
142 writers connected to the write end of the pipe referred to by
143 .IR fd_in .
144
145 On error,
146 .BR splice ()
147 returns \-1 and
148 .I errno
149 is set to indicate the error.
150 .SH ERRORS
151 .TP
152 .B EAGAIN
153 .B SPLICE_F_NONBLOCK
154 was specified in
155 .IR flags ,
156 and the operation would block.
157 .TP
158 .B EBADF
159 One or both file descriptors are not valid,
160 or do not have proper read-write mode.
161 .TP
162 .B EINVAL
163 Target filesystem doesn't support splicing;
164 target file is opened in append mode;
165 .\" The append-mode error is given since 2.6.27; in earlier kernels,
166 .\" splice() in append mode was broken
167 neither of the descriptors refers to a pipe; or
168 offset given for nonseekable device.
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 CONFORMING TO
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 1.2i
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 .SH EXAMPLE
238 See
239 .BR tee (2).
240 .SH SEE ALSO
241 .BR sendfile (2),
242 .BR tee (2),
243 .BR vmsplice (2)
244 .SH COLOPHON
245 This page is part of release 3.79 of the Linux
246 .I man-pages
247 project.
248 A description of the project,
249 information about reporting bugs,
250 and the latest version of this page,
251 can be found at
252 \%http://www.kernel.org/doc/man\-pages/.