Blame man2/write.2

Packit 7cfc04
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
Packit 7cfc04
.\"             and Copyright (C) 1993 Michael Haardt, Ian Jackson.
Packit 7cfc04
.\" and Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified Sat Jul 24 13:35:59 1993 by Rik Faith <faith@cs.unc.edu>
Packit 7cfc04
.\" Modified Sun Nov 28 17:19:01 1993 by Rik Faith <faith@cs.unc.edu>
Packit 7cfc04
.\" Modified Sat Jan 13 12:58:08 1996 by Michael Haardt
Packit 7cfc04
.\"   <michael@cantor.informatik.rwth-aachen.de>
Packit 7cfc04
.\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
Packit 7cfc04
.\" 2001-12-13 added remark by Zack Weinberg
Packit 7cfc04
.\" 2007-06-18 mtk:
Packit 7cfc04
.\"    	Added details about seekable files and file offset.
Packit 7cfc04
.\"	Noted that write() may write less than 'count' bytes, and
Packit 7cfc04
.\"	gave some examples of why this might occur.
Packit 7cfc04
.\"	Noted what happens if write() is interrupted by a signal.
Packit 7cfc04
.\"
Packit 7cfc04
.TH WRITE 2 2018-02-02 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
write \- write to a file descriptor
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t write(int " fd ", const void *" buf ", size_t " count );
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR write ()
Packit 7cfc04
writes up to
Packit 7cfc04
.I count
Packit 7cfc04
bytes from the buffer starting at
Packit 7cfc04
.I buf
Packit 7cfc04
to the file referred to by the file descriptor
Packit 7cfc04
.IR fd .
Packit 7cfc04
.PP
Packit 7cfc04
The number of bytes written may be less than
Packit 7cfc04
.I count
Packit 7cfc04
if, for example,
Packit 7cfc04
there is insufficient space on the underlying physical medium, or the
Packit 7cfc04
.B RLIMIT_FSIZE
Packit 7cfc04
resource limit is encountered (see
Packit 7cfc04
.BR setrlimit (2)),
Packit 7cfc04
or the call was interrupted by a signal
Packit 7cfc04
handler after having written less than
Packit 7cfc04
.I count
Packit 7cfc04
bytes.
Packit 7cfc04
(See also
Packit 7cfc04
.BR pipe (7).)
Packit 7cfc04
.PP
Packit 7cfc04
For a seekable file (i.e., one to which
Packit 7cfc04
.BR lseek (2)
Packit 7cfc04
may be applied, for example, a regular file)
Packit 7cfc04
writing takes place at the file offset,
Packit 7cfc04
and the file offset is incremented by
Packit 7cfc04
the number of bytes actually written.
Packit 7cfc04
If the file was
Packit 7cfc04
.BR open (2)ed
Packit 7cfc04
with
Packit 7cfc04
.BR O_APPEND ,
Packit 7cfc04
the file offset is first set to the end of the file before writing.
Packit 7cfc04
The adjustment of the file offset and the write operation
Packit 7cfc04
are performed as an atomic step.
Packit 7cfc04
.PP
Packit 7cfc04
POSIX requires that a
Packit 7cfc04
.BR read (2)
Packit 7cfc04
that can be proved to occur after a
Packit 7cfc04
.BR write ()
Packit 7cfc04
has returned will return the new data.
Packit 7cfc04
Note that not all filesystems are POSIX conforming.
Packit 7cfc04
.PP
Packit 7cfc04
According to POSIX.1, if
Packit 7cfc04
.I count
Packit 7cfc04
is greater than
Packit 7cfc04
.BR SSIZE_MAX ,
Packit 7cfc04
the result is implementation-defined;
Packit 7cfc04
see NOTES for the upper limit on Linux.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, the number of bytes written is returned (zero indicates
Packit 7cfc04
nothing was written).
Packit 7cfc04
It is not an error if this number is smaller than the number of bytes
Packit 7cfc04
requested; this may happen for example because the disk device was filled.
Packit 7cfc04
See also NOTES.
Packit 7cfc04
.PP
Packit 7cfc04
On error, \-1 is returned, and \fIerrno\fP is set
Packit 7cfc04
appropriately.
Packit 7cfc04
.PP
Packit 7cfc04
If \fIcount\fP is zero and
Packit 7cfc04
.I fd
Packit 7cfc04
refers to a regular file, then
Packit 7cfc04
.BR write ()
Packit 7cfc04
may return a failure status if one of the errors below is detected.
Packit 7cfc04
If no errors are detected, or error detection is not performed,
Packit 7cfc04
0 will be returned without causing any other effect.
Packit 7cfc04
If
Packit 7cfc04
\fIcount\fP is zero and
Packit 7cfc04
.I fd
Packit 7cfc04
refers to a file other than a regular file,
Packit 7cfc04
the results are not specified.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EAGAIN
Packit 7cfc04
The file descriptor
Packit 7cfc04
.I fd
Packit 7cfc04
refers to a file other than a socket and has been marked nonblocking
Packit 7cfc04
.RB ( O_NONBLOCK ),
Packit 7cfc04
and the write would block.
Packit 7cfc04
See
Packit 7cfc04
.BR open (2)
Packit 7cfc04
for further details on the
Packit 7cfc04
.BR O_NONBLOCK
Packit 7cfc04
flag.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EAGAIN " or " EWOULDBLOCK
Packit 7cfc04
.\" Actually EAGAIN on Linux
Packit 7cfc04
The file descriptor
Packit 7cfc04
.I fd
Packit 7cfc04
refers to a socket and has been marked nonblocking
Packit 7cfc04
.RB ( O_NONBLOCK ),
Packit 7cfc04
and the write would block.
Packit 7cfc04
POSIX.1-2001 allows either error to be returned for this case,
Packit 7cfc04
and does not require these constants to have the same value,
Packit 7cfc04
so a portable application should check for both possibilities.
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
.I fd
Packit 7cfc04
is not a valid file descriptor or is not open for writing.
Packit 7cfc04
.TP
Packit 7cfc04
.B EDESTADDRREQ
Packit 7cfc04
.I fd
Packit 7cfc04
refers to a datagram socket for which a peer address has not been set using
Packit 7cfc04
.BR connect (2).
Packit 7cfc04
.TP
Packit 7cfc04
.B EDQUOT
Packit 7cfc04
The user's quota of disk blocks on the filesystem containing the file
Packit 7cfc04
referred to by
Packit 7cfc04
.I fd
Packit 7cfc04
has been exhausted.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
.I buf
Packit 7cfc04
is outside your accessible address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFBIG
Packit 7cfc04
An attempt was made to write a file that exceeds the implementation-defined
Packit 7cfc04
maximum file size or the process's file size limit,
Packit 7cfc04
or to write at a position past the maximum allowed offset.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINTR
Packit 7cfc04
The call was interrupted by a signal before any data was written; see
Packit 7cfc04
.BR signal (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I fd
Packit 7cfc04
is attached to an object which is unsuitable for writing;
Packit 7cfc04
or the file was opened with the
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
flag, and either the address specified in
Packit 7cfc04
.IR buf ,
Packit 7cfc04
the value specified in
Packit 7cfc04
.IR count ,
Packit 7cfc04
or the file offset is not suitably aligned.
Packit 7cfc04
.TP
Packit 7cfc04
.B EIO
Packit 7cfc04
A low-level I/O error occurred while modifying the inode.
Packit 7cfc04
This error may relate to the write-back of data written by an earlier
Packit 7cfc04
.BR write (2),
Packit 7cfc04
which may have been issued to a different file descriptor on
Packit 7cfc04
the same file.
Packit 7cfc04
Since Linux 4.13, errors from write-back come
Packit 7cfc04
with a promise that they
Packit 7cfc04
.I may
Packit 7cfc04
be reported by subsequent.
Packit 7cfc04
.BR write (2)
Packit 7cfc04
requests, and
Packit 7cfc04
.I will
Packit 7cfc04
be reported by a subsequent
Packit 7cfc04
.BR fsync (2)
Packit 7cfc04
(whether or not they were also reported by
Packit 7cfc04
.BR write (2)).
Packit 7cfc04
.\" commit 088737f44bbf6378745f5b57b035e57ee3dc4750
Packit 7cfc04
An alternate cause of
Packit 7cfc04
.B EIO
Packit 7cfc04
on networked filesystems is when an advisory lock had been taken out
Packit 7cfc04
on the file descriptor and this lock has been lost.
Packit 7cfc04
See the
Packit 7cfc04
.I "Lost locks"
Packit 7cfc04
section of
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
for further details.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOSPC
Packit 7cfc04
The device containing the file referred to by
Packit 7cfc04
.I fd
Packit 7cfc04
has no room for the data.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The operation was prevented by a file seal; see
Packit 7cfc04
.BR fcntl (2).
Packit 7cfc04
.TP
Packit 7cfc04
.B EPIPE
Packit 7cfc04
.I fd
Packit 7cfc04
is connected to a pipe or socket whose reading end is closed.
Packit 7cfc04
When this happens the writing process will also receive a
Packit 7cfc04
.B SIGPIPE
Packit 7cfc04
signal.
Packit 7cfc04
(Thus, the write return value is seen only if the program
Packit 7cfc04
catches, blocks or ignores this signal.)
Packit 7cfc04
.PP
Packit 7cfc04
Other errors may occur, depending on the object connected to
Packit 7cfc04
.IR fd .
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
SVr4, 4.3BSD, POSIX.1-2001.
Packit 7cfc04
.\" SVr4 documents additional error
Packit 7cfc04
.\" conditions EDEADLK, ENOLCK, ENOLNK, ENOSR, ENXIO, or ERANGE.
Packit 7cfc04
.PP
Packit 7cfc04
Under SVr4 a write may be interrupted and return
Packit 7cfc04
.B EINTR
Packit 7cfc04
at any point,
Packit 7cfc04
not just before any data is written.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
The types
Packit 7cfc04
.I size_t
Packit 7cfc04
and
Packit 7cfc04
.I ssize_t
Packit 7cfc04
are, respectively,
Packit 7cfc04
unsigned and signed integer data types specified by POSIX.1.
Packit 7cfc04
.PP
Packit 7cfc04
A successful return from
Packit 7cfc04
.BR write ()
Packit 7cfc04
does not make any guarantee that data has been committed to disk.
Packit 7cfc04
On some filesystems, including NFS, it does not even guarantee
Packit 7cfc04
that space has successfully been reserved for the data.
Packit 7cfc04
In this case,
Packit 7cfc04
some errors might be delayed until a future
Packit 7cfc04
.BR write (2),
Packit 7cfc04
.BR fsync (2),
Packit 7cfc04
or even
Packit 7cfc04
.BR close (2).
Packit 7cfc04
The only way to be sure is to call
Packit 7cfc04
.BR fsync (2)
Packit 7cfc04
after you are done writing all your data.
Packit 7cfc04
.PP
Packit 7cfc04
If a
Packit 7cfc04
.BR write ()
Packit 7cfc04
is interrupted by a signal handler before any bytes are written,
Packit 7cfc04
then the call fails with the error
Packit 7cfc04
.BR EINTR ;
Packit 7cfc04
if it is interrupted after at least one byte has been written,
Packit 7cfc04
the call succeeds, and returns the number of bytes written.
Packit 7cfc04
.PP
Packit 7cfc04
On Linux,
Packit 7cfc04
.BR write ()
Packit 7cfc04
(and similar system calls) will transfer at most
Packit 7cfc04
0x7ffff000 (2,147,479,552) bytes,
Packit 7cfc04
returning the number of bytes actually transferred.
Packit 7cfc04
.\" commit e28cc71572da38a5a12c1cfe4d7032017adccf69
Packit 7cfc04
(This is true on both 32-bit and 64-bit systems.)
Packit 7cfc04
.SH BUGS
Packit 7cfc04
According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
Packit 7cfc04
("Thread Interactions with Regular File Operations"):
Packit 7cfc04
.PP
Packit 7cfc04
.RS 4
Packit 7cfc04
All of the following functions shall be atomic with respect to
Packit 7cfc04
each other in the effects specified in POSIX.1-2008 when they
Packit 7cfc04
operate on regular files or symbolic links: ...
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
Among the APIs subsequently listed are
Packit 7cfc04
.BR write ()
Packit 7cfc04
and
Packit 7cfc04
.BR writev (2).
Packit 7cfc04
And among the effects that should be atomic across threads (and processes)
Packit 7cfc04
are updates of the file offset.
Packit 7cfc04
However, on Linux before version 3.14,
Packit 7cfc04
this was not the case: if two processes that share
Packit 7cfc04
an open file description (see
Packit 7cfc04
.BR open (2))
Packit 7cfc04
perform a
Packit 7cfc04
.BR write ()
Packit 7cfc04
(or
Packit 7cfc04
.BR writev (2))
Packit 7cfc04
at the same time, then the I/O operations were not atomic
Packit 7cfc04
with respect updating the file offset,
Packit 7cfc04
with the result that the blocks of data output by the two processes
Packit 7cfc04
might (incorrectly) overlap.
Packit 7cfc04
This problem was fixed in Linux 3.14.
Packit 7cfc04
.\" http://thread.gmane.org/gmane.linux.kernel/1649458
Packit 7cfc04
.\"    From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
Packit 7cfc04
.\"    Subject: Update of file offset on write() etc. is non-atomic with I/O
Packit 7cfc04
.\"    Date: 2014-02-17 15:41:37 GMT
Packit 7cfc04
.\"    Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
Packit 7cfc04
.\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
Packit 7cfc04
.\"    Author: Linus Torvalds <torvalds@linux-foundation.org>
Packit 7cfc04
.\"    Date:   Mon Mar 3 09:36:58 2014 -0800
Packit 7cfc04
.\"
Packit 7cfc04
.\"        vfs: atomic f_pos accesses as per POSIX
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR close (2),
Packit 7cfc04
.BR fcntl (2),
Packit 7cfc04
.BR fsync (2),
Packit 7cfc04
.BR ioctl (2),
Packit 7cfc04
.BR lseek (2),
Packit 7cfc04
.BR open (2),
Packit 7cfc04
.BR pwrite (2),
Packit 7cfc04
.BR read (2),
Packit 7cfc04
.BR select (2),
Packit 7cfc04
.BR writev (2),
Packit 7cfc04
.BR fwrite (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.