1SPLICE(2) Linux Programmer's Manual SPLICE(2)
2
3
4
6 splice - splice data to/from a pipe
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <fcntl.h>
11
12 ssize_t splice(int fd_in, loff_t *off_in, int fd_out,
13 loff_t *off_out, size_t len, unsigned int flags);
14
16 splice() moves data between two file descriptors without copying
17 between kernel address space and user address space. It transfers up
18 to len bytes of data from the file descriptor fd_in to the file
19 descriptor fd_out, where one of the file descriptors must refer to a
20 pipe.
21
22 The following semantics apply for fd_in and off_in:
23
24 * If fd_in refers to a pipe, then off_in must be NULL.
25
26 * If fd_in does not refer to a pipe and off_in is NULL, then bytes are
27 read from fd_in starting from the file offset, and the file offset
28 is adjusted appropriately.
29
30 * If fd_in does not refer to a pipe and off_in is not NULL, then
31 off_in must point to a buffer which specifies the starting offset
32 from which bytes will be read from fd_in; in this case, the file
33 offset of fd_in is not changed.
34
35 Analogous statements apply for fd_out and off_out.
36
37 The flags argument is a bit mask that is composed by ORing together
38 zero or more of the following values:
39
40 SPLICE_F_MOVE
41 Attempt to move pages instead of copying. This is only a hint
42 to the kernel: pages may still be copied if the kernel cannot
43 move the pages from the pipe, or if the pipe buffers don't refer
44 to full pages. The initial implementation of this flag was
45 buggy: therefore starting in Linux 2.6.21 it is a no-op (but is
46 still permitted in a splice() call); in the future, a correct
47 implementation may be restored.
48
49 SPLICE_F_NONBLOCK
50 Do not block on I/O. This makes the splice pipe operations non‐
51 blocking, but splice() may nevertheless block because the file
52 descriptors that are spliced to/from may block (unless they have
53 the O_NONBLOCK flag set).
54
55 SPLICE_F_MORE
56 More data will be coming in a subsequent splice. This is a
57 helpful hint when the fd_out refers to a socket (see also the
58 description of MSG_MORE in send(2), and the description of
59 TCP_CORK in tcp(7)).
60
61 SPLICE_F_GIFT
62 Unused for splice(); see vmsplice(2).
63
65 Upon successful completion, splice() returns the number of bytes
66 spliced to or from the pipe.
67
68 A return value of 0 means end of input. If fd_in refers to a pipe,
69 then this means that there was no data to transfer, and it would not
70 make sense to block because there are no writers connected to the write
71 end of the pipe.
72
73 On error, splice() returns -1 and errno is set to indicate the error.
74
76 EAGAIN SPLICE_F_NONBLOCK was specified in flags or one of the file
77 descriptors had been marked as nonblocking (O_NONBLOCK), and the
78 operation would block.
79
80 EBADF One or both file descriptors are not valid, or do not have
81 proper read-write mode.
82
83 EINVAL The target filesystem doesn't support splicing.
84
85 EINVAL The target file is opened in append mode.
86
87 EINVAL Neither of the file descriptors refers to a pipe.
88
89 EINVAL An offset was given for nonseekable device (e.g., a pipe).
90
91 EINVAL fd_in and fd_out refer to the same pipe.
92
93 ENOMEM Out of memory.
94
95 ESPIPE Either off_in or off_out was not NULL, but the corresponding
96 file descriptor refers to a pipe.
97
99 The splice() system call first appeared in Linux 2.6.17; library sup‐
100 port was added to glibc in version 2.5.
101
103 This system call is Linux-specific.
104
106 The three system calls splice(), vmsplice(2), and tee(2), provide user-
107 space programs with full control over an arbitrary kernel buffer,
108 implemented within the kernel using the same type of buffer that is
109 used for a pipe. In overview, these system calls perform the following
110 tasks:
111
112 · splice() moves data from the buffer to an arbitrary file descriptor,
113 or vice versa, or from one buffer to another.
114
115 · tee(2) "copies" the data from one buffer to another.
116
117 · vmsplice(2) "copies" data from user space into the buffer.
118
119 Though we talk of copying, actual copies are generally avoided. The
120 kernel does this by implementing a pipe buffer as a set of reference-
121 counted pointers to pages of kernel memory. The kernel creates
122 "copies" of pages in a buffer by creating new pointers (for the output
123 buffer) referring to the pages, and increasing the reference counts for
124 the pages: only pointers are copied, not the pages of the buffer.
125
126 In Linux 2.6.30 and earlier, exactly one of fd_in and fd_out was
127 required to be a pipe. Since Linux 2.6.31, both arguments may refer to
128 pipes.
129
131 See tee(2).
132
134 copy_file_range(2), sendfile(2), tee(2), vmsplice(2), pipe(7)
135
137 This page is part of release 5.07 of the Linux man-pages project. A
138 description of the project, information about reporting bugs, and the
139 latest version of this page, can be found at
140 https://www.kernel.org/doc/man-pages/.
141
142
143
144Linux 2020-06-09 SPLICE(2)