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