Bug 43178 - pwrite() should not be affected by O_APPEND
Summary: pwrite() should not be affected by O_APPEND
Status: NEW
Alias: None
Product: File System
Classification: Unclassified
Component: VFS (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: fs_vfs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-04-29 19:32 UTC by Michael Kerrisk
Modified: 2016-03-20 10:13 UTC (History)
3 users (show)

See Also:
Kernel Version: 3.4
Subsystem:
Regression: No
Bisected commit-id:


Attachments
Test program (976 bytes, text/x-csrc)
2012-04-29 19:45 UTC, Michael Kerrisk
Details

Description Michael Kerrisk 2012-04-29 19:32:30 UTC
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

The pwrite() system call (and also pwritev()) writes bytes at the given 'offset', leaving the file offset unchanged. 

But, on Linux, if a file is opened with O_APPEND, pwrite() wrongly appends data to the end of the file. It should instead honor the offset. This was the intention in the in the original POSIX.1-2001 specification [1], and wording was added to POSIX.1-2008 to make this more explicit.

Tested on 3.0 and 3.2, but appears to be still current in 3.4.

[1] http://www.opengroup.org/sophocles2/show_mail.tpl?CALLER=show_archive.tpl&source=L&listname=austin-group-l&id=9455
[2] http://www.opengroup.org/austin/docs/austin_325.txt
Comment 1 Michael Kerrisk 2012-04-29 19:37:05 UTC
Timo Sirainen also noted this issue:
http://thread.gmane.org/gmane.linux.kernel/513390
Comment 2 Michael Kerrisk 2012-04-29 19:45:09 UTC
Created attachment 73123 [details]
Test program

Test program and example that demonstrates the issue:


$ cat > a
aaaaaaa
bbbbbbb
ccccccc
ddddddd
$ ./t_pwrite_O_APPEND a 5 XXXXX y
$ cat a
aaaaaaa
bbbbbbb
ccccccc
ddddddd
XXXXX$ 


In the above, the XXXXX string should have been placed at offset 5 of the file 'a'.
Comment 3 Alan 2012-05-12 00:41:21 UTC
Not clear what the fix is however..  we can't just ignore it because of the appendonly attribute.

Note You need to log in before you can comment on or make changes to this bug.