Bug 82841

Summary: sendfile doesn't work with O_APPEND
Product: Documentation Reporter: Jason Newton (nevion)
Component: man-pagesAssignee: documentation_man-pages (documentation_man-pages)
Status: RESOLVED CODE_FIX    
Severity: normal CC: alan, mtk.manpages
Priority: P1    
Hardware: All   
OS: Linux   
Kernel Version: 3.10-3.15 Subsystem:
Regression: No Bisected commit-id:
Attachments: sendfile append bug test case

Description Jason Newton 2014-08-20 08:56:50 UTC
Created attachment 147521 [details]
sendfile append bug test case

attached test case

dd if=/dev/zero of=/tmp/input bs=1M count=1
gcc sendfile_append_bug.c -o sendfile_append_bug
./sendfile_append_bug 
sendfile failed: Invalid argument

Removing O_APPEND on the output file descriptor will prevent sendfile from squaking and you will receive this output:
sendfile copied 1 bytes


sendfile is critical to the IO workloads I work with - real time/mission critical stuff that needs a fair amount of fault tolerance from say a machine being force rebooted.  I use O_APPEND to ensure the file cursor is always pointing at the end of file and believe some filesystems make use of this flag internally in addition to it doing the right thing more often than me issuing equivalent lseeks. I mainly use XFS for these workloads and shared memory files which then act as input fds to sendfile.
Comment 1 Alan 2014-08-21 15:22:42 UTC
O_APPEND is not supported for sendfile().

It's documented in the splice() manual page but not for sendfile so I've moved this to a documentation bug
Comment 2 Jason Newton 2014-08-22 02:18:48 UTC
(In reply to Alan from comment #1)
> O_APPEND is not supported for sendfile().
> 
> It's documented in the splice() manual page but not for sendfile so I've
> moved this to a documentation bug

OK so this quirk was not out in the open in documentation - before I opened this bug report I read a few years old discussion: http://fixunix.com/kernel/543017-splice-vs-o_append.html

Most of what they're talking about is splice, not sendfile.  They might use the same implementation but sendfile should be O_APPEND safe as there is no specifiable offset in the output filedescriptor.  Is there a reason to not support it?
Comment 3 Alan 2014-08-22 08:31:48 UTC
sendfile is just a wrapper around splice()

Whether it could therefore support O_APPEND is a good question - but one that would need asking and discussing on the lists not here, and someone to contribute the patches to do so.

In the meantime we ought to get the man page fixed.
Comment 4 Michael Kerrisk 2015-05-05 09:13:28 UTC
I added the following text in ERRORS for sendfile(2).

       EINVAL out_fd  was has the O_APPEND flag set.  This is not cur‐
              rently supported by sendfile()

So, am closing this bug now.