landlock_restrict_self.2: tfix
[man-pages.git] / man2 / write.2
blob64c884c945061716ecbc88b73eff4006edf230b0
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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .\" Modified Sat Jul 24 13:35:59 1993 by Rik Faith <faith@cs.unc.edu>
8 .\" Modified Sun Nov 28 17:19:01 1993 by Rik Faith <faith@cs.unc.edu>
9 .\" Modified Sat Jan 13 12:58:08 1996 by Michael Haardt
10 .\"   <michael@cantor.informatik.rwth-aachen.de>
11 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
12 .\" 2001-12-13 added remark by Zack Weinberg
13 .\" 2007-06-18 mtk:
14 .\"     Added details about seekable files and file offset.
15 .\"     Noted that write() may write less than 'count' bytes, and
16 .\"     gave some examples of why this might occur.
17 .\"     Noted what happens if write() is interrupted by a signal.
18 .\"
19 .TH WRITE 2 2021-03-22 "Linux" "Linux Programmer's Manual"
20 .SH NAME
21 write \- write to a file descriptor
22 .SH LIBRARY
23 Standard C library
24 .RI ( libc ", " \-lc )
25 .SH SYNOPSIS
26 .nf
27 .B #include <unistd.h>
28 .PP
29 .BI "ssize_t write(int " fd ", const void *" buf ", size_t " count );
30 .fi
31 .SH DESCRIPTION
32 .BR write ()
33 writes up to
34 .I count
35 bytes from the buffer starting at
36 .I buf
37 to the file referred to by the file descriptor
38 .IR fd .
39 .PP
40 The number of bytes written may be less than
41 .I count
42 if, for example,
43 there is insufficient space on the underlying physical medium, or the
44 .B RLIMIT_FSIZE
45 resource limit is encountered (see
46 .BR setrlimit (2)),
47 or the call was interrupted by a signal
48 handler after having written less than
49 .I count
50 bytes.
51 (See also
52 .BR pipe (7).)
53 .PP
54 For a seekable file (i.e., one to which
55 .BR lseek (2)
56 may be applied, for example, a regular file)
57 writing takes place at the file offset,
58 and the file offset is incremented by
59 the number of bytes actually written.
60 If the file was
61 .BR open (2)ed
62 with
63 .BR O_APPEND ,
64 the file offset is first set to the end of the file before writing.
65 The adjustment of the file offset and the write operation
66 are performed as an atomic step.
67 .PP
68 POSIX requires that a
69 .BR read (2)
70 that can be proved to occur after a
71 .BR write ()
72 has returned will return the new data.
73 Note that not all filesystems are POSIX conforming.
74 .PP
75 According to POSIX.1, if
76 .I count
77 is greater than
78 .BR SSIZE_MAX ,
79 the result is implementation-defined;
80 see NOTES for the upper limit on Linux.
81 .SH RETURN VALUE
82 On success, the number of bytes written is returned.
83 On error, \-1 is returned, and \fIerrno\fP is set
84 to indicate the error.
85 .PP
86 Note that a successful
87 .BR write ()
88 may transfer fewer than
89 .I count
90 bytes.
91 Such partial writes can occur for various reasons;
92 for example, because there was insufficient space on the disk device
93 to write all of the requested bytes, or because a blocked
94 .BR write ()
95 to a socket, pipe, or similar was interrupted by a signal handler
96 after it had transferred some, but before it had transferred all
97 of the requested bytes.
98 In the event of a partial write, the caller can make another
99 .BR write ()
100 call to transfer the remaining bytes.
101 The subsequent call will either transfer further bytes or
102 may result in an error (e.g., if the disk is now full).
104 If \fIcount\fP is zero and
105 .I fd
106 refers to a regular file, then
107 .BR write ()
108 may return a failure status if one of the errors below is detected.
109 If no errors are detected, or error detection is not performed,
110 0 is returned without causing any other effect.
112 \fIcount\fP is zero and
113 .I fd
114 refers to a file other than a regular file,
115 the results are not specified.
116 .SH ERRORS
118 .B EAGAIN
119 The file descriptor
120 .I fd
121 refers to a file other than a socket and has been marked nonblocking
122 .RB ( O_NONBLOCK ),
123 and the write would block.
125 .BR open (2)
126 for further details on the
127 .B O_NONBLOCK
128 flag.
130 .BR EAGAIN " or " EWOULDBLOCK
131 .\" Actually EAGAIN on Linux
132 The file descriptor
133 .I fd
134 refers to a socket and has been marked nonblocking
135 .RB ( O_NONBLOCK ),
136 and the write would block.
137 POSIX.1-2001 allows either error to be returned for this case,
138 and does not require these constants to have the same value,
139 so a portable application should check for both possibilities.
141 .B EBADF
142 .I fd
143 is not a valid file descriptor or is not open for writing.
145 .B EDESTADDRREQ
146 .I fd
147 refers to a datagram socket for which a peer address has not been set using
148 .BR connect (2).
150 .B EDQUOT
151 The user's quota of disk blocks on the filesystem containing the file
152 referred to by
153 .I fd
154 has been exhausted.
156 .B EFAULT
157 .I buf
158 is outside your accessible address space.
160 .B EFBIG
161 An attempt was made to write a file that exceeds the implementation-defined
162 maximum file size or the process's file size limit,
163 or to write at a position past the maximum allowed offset.
165 .B EINTR
166 The call was interrupted by a signal before any data was written; see
167 .BR signal (7).
169 .B EINVAL
170 .I fd
171 is attached to an object which is unsuitable for writing;
172 or the file was opened with the
173 .B O_DIRECT
174 flag, and either the address specified in
175 .IR buf ,
176 the value specified in
177 .IR count ,
178 or the file offset is not suitably aligned.
180 .B EIO
181 A low-level I/O error occurred while modifying the inode.
182 This error may relate to the write-back of data written by an earlier
183 .BR write (),
184 which may have been issued to a different file descriptor on
185 the same file.
186 Since Linux 4.13, errors from write-back come
187 with a promise that they
188 .I may
189 be reported by subsequent.
190 .BR write ()
191 requests, and
192 .I will
193 be reported by a subsequent
194 .BR fsync (2)
195 (whether or not they were also reported by
196 .BR write ()).
197 .\" commit 088737f44bbf6378745f5b57b035e57ee3dc4750
198 An alternate cause of
199 .B EIO
200 on networked filesystems is when an advisory lock had been taken out
201 on the file descriptor and this lock has been lost.
202 See the
203 .I "Lost locks"
204 section of
205 .BR fcntl (2)
206 for further details.
208 .B ENOSPC
209 The device containing the file referred to by
210 .I fd
211 has no room for the data.
213 .B EPERM
214 The operation was prevented by a file seal; see
215 .BR fcntl (2).
217 .B EPIPE
218 .I fd
219 is connected to a pipe or socket whose reading end is closed.
220 When this happens the writing process will also receive a
221 .B SIGPIPE
222 signal.
223 (Thus, the write return value is seen only if the program
224 catches, blocks or ignores this signal.)
226 Other errors may occur, depending on the object connected to
227 .IR fd .
228 .SH STANDARDS
229 SVr4, 4.3BSD, POSIX.1-2001.
230 .\" SVr4 documents additional error
231 .\" conditions EDEADLK, ENOLCK, ENOLNK, ENOSR, ENXIO, or ERANGE.
233 Under SVr4 a write may be interrupted and return
234 .B EINTR
235 at any point,
236 not just before any data is written.
237 .SH NOTES
238 The types
239 .I size_t
241 .I ssize_t
242 are, respectively,
243 unsigned and signed integer data types specified by POSIX.1.
245 A successful return from
246 .BR write ()
247 does not make any guarantee that data has been committed to disk.
248 On some filesystems, including NFS, it does not even guarantee
249 that space has successfully been reserved for the data.
250 In this case,
251 some errors might be delayed until a future
252 .BR write (),
253 .BR fsync (2),
254 or even
255 .BR close (2).
256 The only way to be sure is to call
257 .BR fsync (2)
258 after you are done writing all your data.
260 If a
261 .BR write ()
262 is interrupted by a signal handler before any bytes are written,
263 then the call fails with the error
264 .BR EINTR ;
265 if it is interrupted after at least one byte has been written,
266 the call succeeds, and returns the number of bytes written.
268 On Linux,
269 .BR write ()
270 (and similar system calls) will transfer at most
271 0x7ffff000 (2,147,479,552) bytes,
272 returning the number of bytes actually transferred.
273 .\" commit e28cc71572da38a5a12c1cfe4d7032017adccf69
274 (This is true on both 32-bit and 64-bit systems.)
276 An error return value while performing
277 .BR write ()
278 using direct I/O does not mean the
279 entire write has failed.
280 Partial data may be written
281 and the data at the file offset on which the
282 .BR write ()
283 was attempted should be considered inconsistent.
284 .SH BUGS
285 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
286 ("Thread Interactions with Regular File Operations"):
288 .RS 4
289 All of the following functions shall be atomic with respect to
290 each other in the effects specified in POSIX.1-2008 when they
291 operate on regular files or symbolic links: ...
294 Among the APIs subsequently listed are
295 .BR write ()
297 .BR writev (2).
298 And among the effects that should be atomic across threads (and processes)
299 are updates of the file offset.
300 However, on Linux before version 3.14,
301 this was not the case: if two processes that share
302 an open file description (see
303 .BR open (2))
304 perform a
305 .BR write ()
307 .BR writev (2))
308 at the same time, then the I/O operations were not atomic
309 with respect to updating the file offset,
310 with the result that the blocks of data output by the two processes
311 might (incorrectly) overlap.
312 This problem was fixed in Linux 3.14.
313 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
314 .\"    From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
315 .\"    Subject: Update of file offset on write() etc. is non-atomic with I/O
316 .\"    Date: 2014-02-17 15:41:37 GMT
317 .\"    Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
318 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
319 .\"    Author: Linus Torvalds <torvalds@linux-foundation.org>
320 .\"    Date:   Mon Mar 3 09:36:58 2014 -0800
322 .\"        vfs: atomic f_pos accesses as per POSIX
323 .SH SEE ALSO
324 .BR close (2),
325 .BR fcntl (2),
326 .BR fsync (2),
327 .BR ioctl (2),
328 .BR lseek (2),
329 .BR open (2),
330 .BR pwrite (2),
331 .BR read (2),
332 .BR select (2),
333 .BR writev (2),
334 .BR fwrite (3)