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