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