OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man2 / write.2
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\"             and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified Sat Jul 24 13:35:59 1993 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified Sun Nov 28 17:19:01 1993 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified Sat Jan 13 12:58:08 1996 by Michael Haardt
30 .\"   <michael@cantor.informatik.rwth-aachen.de>
31 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
32 .\" 2001-12-13 added remark by Zack Weinberg
33 .\" 2007-06-18 mtk:
34 .\"     Added details about seekable files and file offset.
35 .\"     Noted that write() may write less than 'count' bytes, and
36 .\"     gave some examples of why this might occur.
37 .\"     Noted what happens if write() is interrupted by a signal.
38 .\"
39 .TH WRITE 2 2015-01-22 "Linux" "Linux Programmer's Manual"
40 .SH NAME
41 write \- write to a file descriptor
42 .SH SYNOPSIS
43 .B #include <unistd.h>
44 .sp
45 .BI "ssize_t write(int " fd ", const void *" buf ", size_t " count );
46 .SH DESCRIPTION
47 .BR write ()
48 writes up to
49 .I count
50 bytes from the buffer pointed
51 .I buf
52 to the file referred to by the file descriptor
53 .IR fd .
54
55 The number of bytes written may be less than
56 .I count
57 if, for example,
58 there is insufficient space on the underlying physical medium, or the
59 .B RLIMIT_FSIZE
60 resource limit is encountered (see
61 .BR setrlimit (2)),
62 or the call was interrupted by a signal
63 handler after having written less than
64 .I count
65 bytes.
66 (See also
67 .BR pipe (7).)
68
69 For a seekable file (i.e., one to which
70 .BR lseek (2)
71 may be applied, for example, a regular file)
72 writing takes place at the current file offset,
73 and the file offset is incremented by
74 the number of bytes actually written.
75 If the file was
76 .BR open (2)ed
77 with
78 .BR O_APPEND ,
79 the file offset is first set to the end of the file before writing.
80 The adjustment of the file offset and the write operation
81 are performed as an atomic step.
82
83 POSIX requires that a
84 .BR read (2)
85 which can be proved to occur after a
86 .BR write ()
87 has returned returns the new data.
88 Note that not all filesystems are POSIX conforming.
89 .SH RETURN VALUE
90 On success, the number of bytes written is returned (zero indicates
91 nothing was written).
92 On error, \-1 is returned, and \fIerrno\fP is set
93 appropriately.
94
95 If \fIcount\fP is zero and
96 .I fd
97 refers to a regular file, then
98 .BR write ()
99 may return a failure status if one of the errors below is detected.
100 If no errors are detected,
101 0 will be returned without causing any other effect.
102 If
103 \fIcount\fP is zero and
104 .I fd
105 refers to a file other than a regular file,
106 the results are not specified.
107 .SH ERRORS
108 .TP
109 .B EAGAIN
110 The file descriptor
111 .I fd
112 refers to a file other than a socket and has been marked nonblocking
113 .RB ( O_NONBLOCK ),
114 and the write would block.
115 .TP
116 .BR EAGAIN " or " EWOULDBLOCK
117 .\" Actually EAGAIN on Linux
118 The file descriptor
119 .I fd
120 refers to a socket and has been marked nonblocking
121 .RB ( O_NONBLOCK ),
122 and the write would block.
123 POSIX.1-2001 allows either error to be returned for this case,
124 and does not require these constants to have the same value,
125 so a portable application should check for both possibilities.
126 .TP
127 .B EBADF
128 .I fd
129 is not a valid file descriptor or is not open for writing.
130 .TP
131 .B EDESTADDRREQ
132 .I fd
133 refers to a datagram socket for which a peer address has not been set using
134 .BR connect (2).
135 .TP
136 .B EDQUOT
137 The user's quota of disk blocks on the filesystem containing the file
138 referred to by
139 .I fd
140 has been exhausted.
141 .TP
142 .B EFAULT
143 .I buf
144 is outside your accessible address space.
145 .TP
146 .B EFBIG
147 An attempt was made to write a file that exceeds the implementation-defined
148 maximum file size or the process's file size limit,
149 or to write at a position past the maximum allowed offset.
150 .TP
151 .B EINTR
152 The call was interrupted by a signal before any data was written; see
153 .BR signal (7).
154 .TP
155 .B EINVAL
156 .I fd
157 is attached to an object which is unsuitable for writing;
158 or the file was opened with the
159 .B O_DIRECT
160 flag, and either the address specified in
161 .IR buf ,
162 the value specified in
163 .IR count ,
164 or the current file offset is not suitably aligned.
165 .TP
166 .B EIO
167 A low-level I/O error occurred while modifying the inode.
168 .TP
169 .B ENOSPC
170 The device containing the file referred to by
171 .I fd
172 has no room for the data.
173 .TP
174 .B EPERM
175 The operation was prevented by a file seal; see
176 .BR fcntl (2).
177 .TP
178 .B EPIPE
179 .I fd
180 is connected to a pipe or socket whose reading end is closed.
181 When this happens the writing process will also receive a
182 .B SIGPIPE
183 signal.
184 (Thus, the write return value is seen only if the program
185 catches, blocks or ignores this signal.)
186 .PP
187 Other errors may occur, depending on the object connected to
188 .IR fd .
189 .SH CONFORMING TO
190 SVr4, 4.3BSD, POSIX.1-2001.
191 .\" SVr4 documents additional error
192 .\" conditions EDEADLK, ENOLCK, ENOLNK, ENOSR, ENXIO, or ERANGE.
193
194 Under SVr4 a write may be interrupted and return
195 .B EINTR
196 at any point,
197 not just before any data is written.
198 .SH NOTES
199 A successful return from
200 .BR write ()
201 does not make any guarantee that data has been committed to disk.
202 In fact, on some buggy implementations, it does not even guarantee
203 that space has successfully been reserved for the data.
204 The only way to be sure is to call
205 .BR fsync (2)
206 after you are done writing all your data.
207
208 If a
209 .BR write ()
210 is interrupted by a signal handler before any bytes are written,
211 then the call fails with the error
212 .BR EINTR ;
213 if it is interrupted after at least one byte has been written,
214 the call succeeds, and returns the number of bytes written.
215 .SH BUGS
216 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
217 ("Thread Interactions with Regular File Operations"):
218
219 .RS 4
220 All of the following functions shall be atomic with respect to
221 each other in the effects specified in POSIX.1-2008 when they
222 operate on regular files or symbolic links: ...
223 .RE
224
225 Among the APIs subsequently listed are
226 .BR write ()
227 and
228 .BR writev (2).
229 And among the effects that should be atomic across threads (and processes)
230 are updates of the file offset.
231 However, on Linux before version 3.14,
232 this was not the case: if two processes that share
233 an open file description (see
234 .BR open (2))
235 perform a
236 .BR write ()
237 (or
238 .BR writev (2))
239 at the same time, then the I/O operations were not atomic
240 with respect updating the file offset,
241 with the result that the blocks of data output by the two processes
242 might (incorrectly) overlap.
243 This problem was fixed in Linux 3.14.
244 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
245 .\"    From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
246 .\"    Subject: Update of file offset on write() etc. is non-atomic with I/O
247 .\"    Date: 2014-02-17 15:41:37 GMT
248 .\"    Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
249 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
250 .\"    Author: Linus Torvalds <torvalds@linux-foundation.org>
251 .\"    Date:   Mon Mar 3 09:36:58 2014 -0800
252 .\"
253 .\"        vfs: atomic f_pos accesses as per POSIX
254 .SH SEE ALSO
255 .BR close (2),
256 .BR fcntl (2),
257 .BR fsync (2),
258 .BR ioctl (2),
259 .BR lseek (2),
260 .BR open (2),
261 .BR pwrite (2),
262 .BR read (2),
263 .BR select (2),
264 .BR writev (2),
265 .BR fwrite (3)
266 .SH COLOPHON
267 This page is part of release 3.79 of the Linux
268 .I man-pages
269 project.
270 A description of the project,
271 information about reporting bugs,
272 and the latest version of this page,
273 can be found at
274 \%http://www.kernel.org/doc/man\-pages/.