-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.html
422 lines (321 loc) · 12.6 KB
/
write.html
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:25:40 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>WRITE</title>
</head>
<body>
<h1 align="center">WRITE</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">write - write
to a file descriptor</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<unistd.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>ssize_t
write(int</b> <i>fd</i><b>, const void *</b><i>buf</i><b>,
size_t</b> <i>count</i><b>);</b></p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>write</b>()
writes up to <i>count</i> bytes from the buffer starting at
<i>buf</i> to the file referred to by the file descriptor
<i>fd</i>.</p>
<p style="margin-left:11%; margin-top: 1em">The number of
bytes written may be less than <i>count</i> if, for example,
there is insufficient space on the underlying physical
medium, or the <b>RLIMIT_FSIZE</b> resource limit is
encountered (see <b>setrlimit</b>(2)), or the call was
interrupted by a signal handler after having written less
than <i>count</i> bytes. (See also <b>pipe</b>(7).)</p>
<p style="margin-left:11%; margin-top: 1em">For a seekable
file (i.e., one to which <b>lseek</b>(2) may be applied, for
example, a regular file) writing takes place at the file
offset, and the file offset is incremented by the number of
bytes actually written. If the file was <b>open</b>(2)ed
with <b>O_APPEND</b>, the file offset is first set to the
end of the file before writing. The adjustment of the file
offset and the write operation are performed as an atomic
step.</p>
<p style="margin-left:11%; margin-top: 1em">POSIX requires
that a <b>read</b>(2) that can be proved to occur after a
<b>write</b>() has returned will return the new data. Note
that not all filesystems are POSIX conforming.</p>
<p style="margin-left:11%; margin-top: 1em">According to
POSIX.1, if <i>count</i> is greater than <b>SSIZE_MAX</b>,
the result is implementation-defined; see NOTES for the
upper limit on Linux.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On success, the
number of bytes written is returned. On error, -1 is
returned, and <i>errno</i> is set to indicate the cause of
the error.</p>
<p style="margin-left:11%; margin-top: 1em">Note that a
successful <b>write</b>() may transfer fewer than
<i>count</i> bytes. Such partial writes can occur for
various reasons; for example, because there was insufficient
space on the disk device to write all of the requested
bytes, or because a blocked <b>write</b>() to a socket,
pipe, or similar was interrupted by a signal handler after
it had transferred some, but before it had transferred all
of the requested bytes. In the event of a partial write, the
caller can make another <b>write</b>() call to transfer the
remaining bytes. The subsequent call will either transfer
further bytes or may result in an error (e.g., if the disk
is now full).</p>
<p style="margin-left:11%; margin-top: 1em">If <i>count</i>
is zero and <i>fd</i> refers to a regular file, then
<b>write</b>() may return a failure status if one of the
errors below is detected. If no errors are detected, or
error detection is not performed, 0 will be returned without
causing any other effect. If <i>count</i> is zero and
<i>fd</i> refers to a file other than a regular file, the
results are not specified.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EAGAIN</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">The file descriptor <i>fd</i>
refers to a file other than a socket and has been marked
nonblocking (<b>O_NONBLOCK</b>), and the write would block.
See <b>open</b>(2) for further details on the
<b>O_NONBLOCK</b> flag.</p></td></tr>
</table>
<p style="margin-left:11%;"><b>EAGAIN</b> or
<b>EWOULDBLOCK</b></p>
<p style="margin-left:22%;">The file descriptor <i>fd</i>
refers to a socket and has been marked nonblocking
(<b>O_NONBLOCK</b>), and the write would block. POSIX.1-2001
allows either error to be returned for this case, and does
not require these constants to have the same value, so a
portable application should check for both
possibilities.</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p><b>EBADF</b></p></td>
<td width="4%"></td>
<td width="78%">
<p><i>fd</i> is not a valid file descriptor or is not open
for writing.</p></td></tr>
</table>
<p style="margin-left:11%;"><b>EDESTADDRREQ</b></p>
<p style="margin-left:22%;"><i>fd</i> refers to a datagram
socket for which a peer address has not been set using
<b>connect</b>(2).</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EDQUOT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The user’s quota of disk blocks on the filesystem
containing the file referred to by <i>fd</i> has been
exhausted.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EFAULT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>buf</i> is outside your accessible address space.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EFBIG</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>An attempt was made to write a file that exceeds the
implementation-defined maximum file size or the
process’s file size limit, or to write at a position
past the maximum allowed offset.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINTR</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The call was interrupted by a signal before any data was
written; see <b>signal</b>(7).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>fd</i> is attached to an object which is unsuitable
for writing; or the file was opened with the <b>O_DIRECT</b>
flag, and either the address specified in <i>buf</i>, the
value specified in <i>count</i>, or the file offset is not
suitably aligned.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EIO</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>A low-level I/O error occurred while modifying the
inode. This error may relate to the write-back of data
written by an earlier <b>write</b>(2), which may have been
issued to a different file descriptor on the same file.
Since Linux 4.13, errors from write-back come with a promise
that they <i>may</i> be reported by subsequent.
<b>write</b>(2) requests, and <i>will</i> be reported by a
subsequent <b>fsync</b>(2) (whether or not they were also
reported by <b>write</b>(2)). An alternate cause of
<b>EIO</b> on networked filesystems is when an advisory lock
had been taken out on the file descriptor and this lock has
been lost. See the <i>Lost locks</i> section of
<b>fcntl</b>(2) for further details.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOSPC</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The device containing the file referred to by <i>fd</i>
has no room for the data.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EPERM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The operation was prevented by a file seal; see
<b>fcntl</b>(2).</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EPIPE</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>fd</i> is connected to a pipe or socket whose reading
end is closed. When this happens the writing process will
also receive a <b>SIGPIPE</b> signal. (Thus, the write
return value is seen only if the program catches, blocks or
ignores this signal.)</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">Other errors
may occur, depending on the object connected to
<i>fd</i>.</p>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">SVr4, 4.3BSD,
POSIX.1-2001.</p>
<p style="margin-left:11%; margin-top: 1em">Under SVr4 a
write may be interrupted and return <b>EINTR</b> at any
point, not just before any data is written.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The types
<i>size_t</i> and <i>ssize_t</i> are, respectively, unsigned
and signed integer data types specified by POSIX.1.</p>
<p style="margin-left:11%; margin-top: 1em">A successful
return from <b>write</b>() does not make any guarantee that
data has been committed to disk. On some filesystems,
including NFS, it does not even guarantee that space has
successfully been reserved for the data. In this case, some
errors might be delayed until a future <b>write</b>(2),
<b>fsync</b>(2), or even <b>close</b>(2). The only way to be
sure is to call <b>fsync</b>(2) after you are done writing
all your data.</p>
<p style="margin-left:11%; margin-top: 1em">If a
<b>write</b>() is interrupted by a signal handler before any
bytes are written, then the call fails with the error
<b>EINTR</b>; if it is interrupted after at least one byte
has been written, the call succeeds, and returns the number
of bytes written.</p>
<p style="margin-left:11%; margin-top: 1em">On Linux,
<b>write</b>() (and similar system calls) 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.)</p>
<p style="margin-left:11%; margin-top: 1em">An error return
value while performing <b>write</b>() using direct I/O does
not mean the entire write has failed. Partial data may be
written and the data at the file offset on which the
<b>write</b>() was attempted should be considered
inconsistent.</p>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">According to
POSIX.1-2008/SUSv4 Section XSI 2.9.7 ("Thread
Interactions with Regular File Operations"):</p>
<p style="margin-left:17%; margin-top: 1em">All of the
following functions shall be atomic with respect to each
other in the effects specified in POSIX.1-2008 when they
operate on regular files or symbolic links: ...</p>
<p style="margin-left:11%; margin-top: 1em">Among the APIs
subsequently listed are <b>write</b>() and <b>writev</b>(2).
And among the effects that should be atomic across threads
(and processes) are updates of the file offset. However, on
Linux before version 3.14, this was not the case: if two
processes that share an open file description (see
<b>open</b>(2)) perform a <b>write</b>() (or
<b>writev</b>(2)) at the same time, then the I/O operations
were not atomic with respect updating the file offset, with
the result that the blocks of data output by the two
processes might (incorrectly) overlap. This problem was
fixed in Linux 3.14.</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>close</b>(2),
<b>fcntl</b>(2), <b>fsync</b>(2), <b>ioctl</b>(2),
<b>lseek</b>(2), <b>open</b>(2), <b>pwrite</b>(2),
<b>read</b>(2), <b>select</b>(2), <b>writev</b>(2),
<b>fwrite</b>(3)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>