1SPLICE(2)                  Linux Programmer's Manual                 SPLICE(2)
2
3
4

NAME

6       splice - splice data to/from a pipe
7

SYNOPSIS

9       #define _GNU_SOURCE         /* See feature_test_macros(7) */
10       #include <fcntl.h>
11
12       ssize_t splice(int fd_in, off64_t *off_in, int fd_out,
13                      off64_t *off_out, size_t len, unsigned int flags);
14

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

VERSIONS

98       The splice() system call first appeared in Linux 2.6.17;  library  sup‐
99       port was added to glibc in version 2.5.
100

CONFORMING TO

102       This system call is Linux-specific.
103

NOTES

105       The three system calls splice(), vmsplice(2), and tee(2), provide user-
106       space programs with full control over an arbitrary kernel  buffer,  im‐
107       plemented  within the kernel using the same type of buffer that is used
108       for a pipe.  In overview, these  system  calls  perform  the  following
109       tasks:
110
111splice()  moves data from the buffer to an arbitrary file descriptor,
112         or vice versa, or from one buffer to another.
113
114tee(2) "copies" the data from one buffer to another.
115
116vmsplice(2) "copies" data from user space into the buffer.
117
118       Though we talk of copying, actual copies are  generally  avoided.   The
119       kernel  does  this by implementing a pipe buffer as a set of reference-
120       counted pointers  to  pages  of  kernel  memory.   The  kernel  creates
121       "copies"  of pages in a buffer by creating new pointers (for the output
122       buffer) referring to the pages, and increasing the reference counts for
123       the pages: only pointers are copied, not the pages of the buffer.
124
125       In  Linux  2.6.30  and earlier, exactly one of fd_in and fd_out was re‐
126       quired to be a pipe.  Since Linux 2.6.31, both arguments may  refer  to
127       pipes.
128

EXAMPLES

130       See tee(2).
131

SEE ALSO

133       copy_file_range(2), sendfile(2), tee(2), vmsplice(2), pipe(7)
134

COLOPHON

136       This  page  is  part of release 5.13 of the Linux man-pages project.  A
137       description of the project, information about reporting bugs,  and  the
138       latest     version     of     this    page,    can    be    found    at
139       https://www.kernel.org/doc/man-pages/.
140
141
142
143Linux                             2021-03-22                         SPLICE(2)
Impressum