Please enable Javascript to view the contents

sendfile

 ·  ☕ 4 分钟

sendfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
SENDFILE(2)                                                                 Linux Programmer's Manual                                                                SENDFILE(2)

NAME
       sendfile - transfer data between file descriptors

SYNOPSIS
       #include <sys/sendfile.h>

       ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

DESCRIPTION
       sendfile()  copies  data  between  one file descriptor and another.  Because this copying is done within the kernel, sendfile() is more efficient than the combination of
       read(2) and write(2), which would require transferring data to and from user space.

       in_fd should be a file descriptor opened for reading and out_fd should be a descriptor opened for writing.

       If offset is not NULL, then it points to a variable holding the file offset from which sendfile() will start reading data from  in_fd.   When  sendfile()  returns,  this
       variable  will be set to the offset of the byte following the last byte that was read.  If offset is not NULL, then sendfile() does not modify the current file offset of
       in_fd; otherwise the current file offset is adjusted to reflect the number of bytes read from in_fd.

       If offset is NULL, then data will be read from in_fd starting at the current file offset, and the file offset will be updated by the call.

       count is the number of bytes to copy between the file descriptors.

       The in_fd argument must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket).

       In Linux kernels before 2.6.33, out_fd must refer to a socket.  Since Linux 2.6.33 it can be any file.  If it is a regular file, then sendfile() changes the file  offset
       appropriately.

RETURN VALUE
       If  the  transfer was successful, the number of bytes written to out_fd is returned.  Note that a successful call to sendfile() may write fewer bytes than requested; the
       caller should be prepared to retry the call if there were unsent bytes.  See also NOTES.

       On error, -1 is returned, and errno is set appropriately.

ERRORS
       EAGAIN Nonblocking I/O has been selected using O_NONBLOCK and the write would block.

       EBADF  The input file was not opened for reading or the output file was not opened for writing.

       EFAULT Bad address.

       EINVAL Descriptor is not valid or locked, or an mmap(2)-like operation is not available for in_fd, or count is negative.

       EINVAL out_fd has the O_APPEND flag set.  This is not currently supported by sendfile().

       EIO    Unspecified error while reading from in_fd.

       ENOMEM Insufficient memory to read from in_fd.

       EOVERFLOW
              count is too large, the operation would result in exceeding the maximum size of either the input file or the output file.

       ESPIPE offset is not NULL but the input file is not seek(2)-able.

VERSIONS
       sendfile() first appeared in Linux 2.2.  The include file <sys/sendfile.h> is present since glibc 2.1.

CONFORMING TO
       Not specified in POSIX.1-2001, nor in other standards.

       Other UNIX systems implement sendfile() with different semantics and prototypes.  It should not be used in portable programs.

NOTES
       sendfile() will transfer at most 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes actually transferred.  (This is true on both 32-bit and 64-bit systems.)

       If you plan to use sendfile() for sending files to a TCP socket, but need to send some header data in front of the file contents, you will find it useful to  employ  the
       TCP_CORK option, described in tcp(7), to minimize the number of packets and to tune performance.

       In Linux 2.4 and earlier, out_fd could also refer to a regular file; this possibility went away in the Linux 2.6.x kernel series, but was restored in Linux 2.6.33.

       The  original  Linux  sendfile() system call was not designed to handle large file offsets.  Consequently, Linux 2.4 added sendfile64(), with a wider type for the offset
       argument.  The glibc sendfile() wrapper function transparently deals with the kernel differences.

       Applications may wish to fall back to read(2)/write(2) in the case where sendfile() fails with EINVAL or ENOSYS.

       If out_fd refers to a socket or pipe with zero-copy support, callers must ensure the transferred portions of the file referred to by in_fd remain  unmodified  until  the
       reader on the other end of out_fd has consumed the transferred data.

       The Linux-specific splice(2) call supports transferring data between arbitrary files (e.g., a pair of sockets).

SEE ALSO
       mmap(2), open(2), socket(2), splice(2)

COLOPHON
       This  page  is part of release 4.04 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the latest version of this page,
       can be found at http://www.kernel.org/doc/man-pages/.

splice

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
SPLICE(2)                                                                   Linux Programmer's Manual                                                                  SPLICE(2)

NAME
       splice - splice data to/from a pipe

SYNOPSIS
       #define _GNU_SOURCE         /* See feature_test_macros(7) */
       #include <fcntl.h>

       ssize_t splice(int fd_in, loff_t *off_in, int fd_out,
                      loff_t *off_out, size_t len, unsigned int flags);

DESCRIPTION
       splice() moves data between two file descriptors
       without copying between kernel address space and user address space. 
       It transfers up to len bytes of data from the file
       descriptor fd_in to the file descriptor fd_out, 
       where one of the descriptors must refer to a pipe.

       The following semantics apply for fd_in and off_in:

       *  If fd_in refers to a pipe, then off_in must be NULL.

       *  If fd_in does not refer to a pipe and off_in is NULL, then bytes are read from fd_in starting from the current file offset, and the current file  offset  is  adjusted
          appropriately.

       *  If  fd_in  does  not refer to a pipe and off_in is not NULL, then off_in must point to a buffer which specifies the starting offset from which bytes will be read from
          fd_in; in this case, the current file offset of fd_in is not changed.

       Analogous statements apply for fd_out and off_out.

       The flags argument is a bit mask that is composed by ORing together zero or more of the following values:

       SPLICE_F_MOVE      Attempt to move pages instead of copying.  This is only a hint to the kernel: pages may still be copied if the kernel cannot move the pages  from  the
                          pipe, or if the pipe buffers don't refer to full pages.  The initial implementation of this flag was buggy: therefore starting in Linux 2.6.21 it is a
                          no-op (but is still permitted in a splice() call); in the future, a correct implementation may be restored.

       SPLICE_F_NONBLOCK  Do not block on I/O.  This makes the splice pipe operations nonblocking, but splice() may nevertheless block because the  file  descriptors  that  are
                          spliced to/from may block (unless they have the O_NONBLOCK flag set).

       SPLICE_F_MORE      More  data  will be coming in a subsequent splice.  This is a helpful hint when the fd_out refers to a socket (see also the description of MSG_MORE in
                          send(2), and the description of TCP_CORK in tcp(7)).

       SPLICE_F_GIFT      Unused for splice(); see vmsplice(2).

RETURN VALUE
       Upon successful completion, splice() returns the number of bytes spliced to or from the pipe.  A return value of 0 means that there was no data to transfer, and it would
       not make sense to block, because there are no writers connected to the write end of the pipe referred to by fd_in.

       On error, splice() returns -1 and errno is set to indicate the error.

ERRORS
       EAGAIN SPLICE_F_NONBLOCK was specified in flags, and the operation would block.

       EBADF  One or both file descriptors are not valid, or do not have proper read-write mode.

       EINVAL Target  filesystem  doesn't  support  splicing; target file is opened in append mode; neither of the descriptors refers to a pipe; or offset given for nonseekable
              device.

       ENOMEM Out of memory.

       ESPIPE Either off_in or off_out was not NULL, but the corresponding file descriptor refers to a pipe.

VERSIONS
       The splice() system call first appeared in Linux 2.6.17; library support was added to glibc in version 2.5.

CONFORMING TO
       This system call is Linux-specific.

NOTES
       The three system calls splice(), vmsplice(2), and tee(2), provide user-space programs with full control over an arbitrary kernel buffer, implemented  within  the  kernel
       using the same type of buffer that is used for a pipe.  In overview, these system calls perform the following tasks:

       splice()    moves data from the buffer to an arbitrary file descriptor, or vice versa, or from one buffer to another.

       tee(2)      "copies" the data from one buffer to another.

       vmsplice(2) "copies" data from user space into the buffer.

       Though  we  talk  of copying, actual copies are generally avoided.  The kernel does this by implementing a pipe buffer as a set of reference-counted pointers to pages of
       kernel memory.  The kernel creates "copies" of pages in a buffer by creating new pointers (for the output buffer) referring to the pages, and  increasing  the  reference
       counts for the pages: only pointers are copied, not the pages of the buffer.

EXAMPLE
       See tee(2).

SEE ALSO
       sendfile(2), tee(2), vmsplice(2)

COLOPHON
       This  page  is part of release 4.04 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the latest version of this page,
       can be found at http://www.kernel.org/doc/man-pages/.