1splice(2)                     System Calls Manual                    splice(2)
2
3
4

NAME

6       splice - splice data to/from a pipe
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #define _GNU_SOURCE         /* See feature_test_macros(7) */
13       #define _FILE_OFFSET_BITS 64
14       #include <fcntl.h>
15
16       ssize_t splice(int fd_in, off_t *_Nullable off_in,
17                      int fd_out, off_t *_Nullable off_out,
18                      size_t len, unsigned int flags);
19

DESCRIPTION

21       splice()  moves  data  between two file descriptors without copying be‐
22       tween kernel address space and user address space.  It transfers up  to
23       len bytes of data from the file descriptor fd_in to the file descriptor
24       fd_out, where one of the file descriptors must refer to a pipe.
25
26       The following semantics apply for fd_in and off_in:
27
28       •  If fd_in refers to a pipe, then off_in must be NULL.
29
30       •  If fd_in does not refer to a pipe and off_in is NULL, then bytes are
31          read  from  fd_in starting from the file offset, and the file offset
32          is adjusted appropriately.
33
34       •  If fd_in does not refer to a pipe  and  off_in  is  not  NULL,  then
35          off_in  must  point  to a buffer which specifies the starting offset
36          from which bytes will be read from fd_in; in  this  case,  the  file
37          offset of fd_in is not changed.
38
39       Analogous statements apply for fd_out and off_out.
40
41       The  flags  argument  is  a bit mask that is composed by ORing together
42       zero or more of the following values:
43
44       SPLICE_F_MOVE
45              Attempt to move pages instead of copying.  This is only  a  hint
46              to  the  kernel:  pages may still be copied if the kernel cannot
47              move the pages from the pipe, or if the pipe buffers don't refer
48              to  full  pages.   The  initial  implementation of this flag was
49              buggy: therefore starting in Linux 2.6.21 it is a no-op (but  is
50              still  permitted  in  a splice() call); in the future, a correct
51              implementation may be restored.
52
53       SPLICE_F_NONBLOCK
54              Do not block on I/O.  This makes the splice pipe operations non‐
55              blocking,  but  splice() may nevertheless block because the file
56              descriptors that are spliced to/from may block (unless they have
57              the O_NONBLOCK flag set).
58
59       SPLICE_F_MORE
60              More  data  will  be  coming  in a subsequent splice.  This is a
61              helpful hint when the fd_out refers to a socket  (see  also  the
62              description  of  MSG_MORE  in  send(2),  and  the description of
63              TCP_CORK in tcp(7)).
64
65       SPLICE_F_GIFT
66              Unused for splice(); see vmsplice(2).
67

RETURN VALUE

69       Upon successful  completion,  splice()  returns  the  number  of  bytes
70       spliced to or from the pipe.
71
72       A  return  value  of  0 means end of input.  If fd_in refers to a pipe,
73       then this means that there was no data to transfer, and  it  would  not
74       make sense to block because there are no writers connected to the write
75       end of the pipe.
76
77       On error, splice() returns -1 and errno is set to indicate the error.
78

ERRORS

80       EAGAIN SPLICE_F_NONBLOCK was specified in flags or one of the file  de‐
81              scriptors  had  been marked as nonblocking (O_NONBLOCK), and the
82              operation would block.
83
84       EBADF  One or both file descriptors are  not  valid,  or  do  not  have
85              proper read-write mode.
86
87       EINVAL The target filesystem doesn't support splicing.
88
89       EINVAL The target file is opened in append mode.
90
91       EINVAL Neither of the file descriptors refers to a pipe.
92
93       EINVAL An offset was given for nonseekable device (e.g., a pipe).
94
95       EINVAL fd_in and fd_out refer to the same pipe.
96
97       ENOMEM Out of memory.
98
99       ESPIPE Either  off_in  or  off_out  was not NULL, but the corresponding
100              file descriptor refers to a pipe.
101

STANDARDS

103       Linux.
104

HISTORY

106       Linux 2.6.17, glibc 2.5.
107
108       In Linux 2.6.30 and earlier, exactly one of fd_in and  fd_out  was  re‐
109       quired  to  be a pipe.  Since Linux 2.6.31, both arguments may refer to
110       pipes.
111

NOTES

113       The three system calls splice(), vmsplice(2), and tee(2), provide user-
114       space  programs  with full control over an arbitrary kernel buffer, im‐
115       plemented within the kernel using the same type of buffer that is  used
116       for  a  pipe.   In  overview,  these system calls perform the following
117       tasks:
118
119       splice()
120              moves data from the buffer to an arbitrary file  descriptor,  or
121              vice versa, or from one buffer to another.
122
123       tee(2) "copies" the data from one buffer to another.
124
125       vmsplice(2)
126              "copies" data from user space into the buffer.
127
128       Though  we  talk  of copying, actual copies are generally avoided.  The
129       kernel does this by implementing a pipe buffer as a set  of  reference-
130       counted  pointers  to  pages  of  kernel  memory.   The  kernel creates
131       "copies" of pages in a buffer by creating new pointers (for the  output
132       buffer) referring to the pages, and increasing the reference counts for
133       the pages: only pointers are copied, not the pages of the buffer.
134
135       _FILE_OFFSET_BITS should be defined to be 64 in code that uses non-null
136       off_in  or  off_out or that takes the address of splice, if the code is
137       intended to be portable to traditional 32-bit  x86  and  ARM  platforms
138       where off_t's width defaults to 32 bits.
139

EXAMPLES

141       See tee(2).
142

SEE ALSO

144       copy_file_range(2), sendfile(2), tee(2), vmsplice(2), pipe(7)
145
146
147
148Linux man-pages 6.05              2023-07-15                         splice(2)
Impressum