]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/write.2
fuse.4: ffix
[thirdparty/man-pages.git] / 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 2016-03-15 "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 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 that can be proved to occur after a
86 .BR write ()
87 has returned will return 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 It is not an error if this number is smaller than the number of bytes
93 requested; this may happen for example because the disk device was filled.
94 See also NOTES.
95
96 On error, \-1 is returned, and \fIerrno\fP is set
97 appropriately.
98
99 If \fIcount\fP is zero and
100 .I fd
101 refers to a regular file, then
102 .BR write ()
103 may return a failure status if one of the errors below is detected.
104 If no errors are detected, or error detection is not performed,
105 0 will be returned without causing any other effect.
106 If
107 \fIcount\fP is zero and
108 .I fd
109 refers to a file other than a regular file,
110 the results are not specified.
111 .SH ERRORS
112 .TP
113 .B EAGAIN
114 The file descriptor
115 .I fd
116 refers to a file other than a socket and has been marked nonblocking
117 .RB ( O_NONBLOCK ),
118 and the write would block.
119 See
120 .BR open (2)
121 for further details on the
122 .BR O_NONBLOCK
123 flag.
124 .TP
125 .BR EAGAIN " or " EWOULDBLOCK
126 .\" Actually EAGAIN on Linux
127 The file descriptor
128 .I fd
129 refers to a socket and has been marked nonblocking
130 .RB ( O_NONBLOCK ),
131 and the write would block.
132 POSIX.1-2001 allows either error to be returned for this case,
133 and does not require these constants to have the same value,
134 so a portable application should check for both possibilities.
135 .TP
136 .B EBADF
137 .I fd
138 is not a valid file descriptor or is not open for writing.
139 .TP
140 .B EDESTADDRREQ
141 .I fd
142 refers to a datagram socket for which a peer address has not been set using
143 .BR connect (2).
144 .TP
145 .B EDQUOT
146 The user's quota of disk blocks on the filesystem containing the file
147 referred to by
148 .I fd
149 has been exhausted.
150 .TP
151 .B EFAULT
152 .I buf
153 is outside your accessible address space.
154 .TP
155 .B EFBIG
156 An attempt was made to write a file that exceeds the implementation-defined
157 maximum file size or the process's file size limit,
158 or to write at a position past the maximum allowed offset.
159 .TP
160 .B EINTR
161 The call was interrupted by a signal before any data was written; see
162 .BR signal (7).
163 .TP
164 .B EINVAL
165 .I fd
166 is attached to an object which is unsuitable for writing;
167 or the file was opened with the
168 .B O_DIRECT
169 flag, and either the address specified in
170 .IR buf ,
171 the value specified in
172 .IR count ,
173 or the file offset is not suitably aligned.
174 .TP
175 .B EIO
176 A low-level I/O error occurred while modifying the inode.
177 .TP
178 .B ENOSPC
179 The device containing the file referred to by
180 .I fd
181 has no room for the data.
182 .TP
183 .B EPERM
184 The operation was prevented by a file seal; see
185 .BR fcntl (2).
186 .TP
187 .B EPIPE
188 .I fd
189 is connected to a pipe or socket whose reading end is closed.
190 When this happens the writing process will also receive a
191 .B SIGPIPE
192 signal.
193 (Thus, the write return value is seen only if the program
194 catches, blocks or ignores this signal.)
195 .PP
196 Other errors may occur, depending on the object connected to
197 .IR fd .
198 .SH CONFORMING TO
199 SVr4, 4.3BSD, POSIX.1-2001.
200 .\" SVr4 documents additional error
201 .\" conditions EDEADLK, ENOLCK, ENOLNK, ENOSR, ENXIO, or ERANGE.
202
203 Under SVr4 a write may be interrupted and return
204 .B EINTR
205 at any point,
206 not just before any data is written.
207 .SH NOTES
208 The types
209 .I size_t
210 and
211 .I ssize_t
212 are, respectively,
213 unsigned and signed integer data types specified by POSIX.1.
214
215 A successful return from
216 .BR write ()
217 does not make any guarantee that data has been committed to disk.
218 In fact, on some buggy implementations, it does not even guarantee
219 that space has successfully been reserved for the data.
220 The only way to be sure is to call
221 .BR fsync (2)
222 after you are done writing all your data.
223
224 If a
225 .BR write ()
226 is interrupted by a signal handler before any bytes are written,
227 then the call fails with the error
228 .BR EINTR ;
229 if it is interrupted after at least one byte has been written,
230 the call succeeds, and returns the number of bytes written.
231
232 On Linux,
233 .BR write ()
234 (and similar system calls) will transfer at most
235 0x7ffff000 (2,147,479,552) bytes,
236 returning the number of bytes actually transferred.
237 .\" commit e28cc71572da38a5a12c1cfe4d7032017adccf69
238 (This is true on both 32-bit and 64-bit systems.)
239 .SH BUGS
240 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
241 ("Thread Interactions with Regular File Operations"):
242
243 .RS 4
244 All of the following functions shall be atomic with respect to
245 each other in the effects specified in POSIX.1-2008 when they
246 operate on regular files or symbolic links: ...
247 .RE
248
249 Among the APIs subsequently listed are
250 .BR write ()
251 and
252 .BR writev (2).
253 And among the effects that should be atomic across threads (and processes)
254 are updates of the file offset.
255 However, on Linux before version 3.14,
256 this was not the case: if two processes that share
257 an open file description (see
258 .BR open (2))
259 perform a
260 .BR write ()
261 (or
262 .BR writev (2))
263 at the same time, then the I/O operations were not atomic
264 with respect updating the file offset,
265 with the result that the blocks of data output by the two processes
266 might (incorrectly) overlap.
267 This problem was fixed in Linux 3.14.
268 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
269 .\" From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
270 .\" Subject: Update of file offset on write() etc. is non-atomic with I/O
271 .\" Date: 2014-02-17 15:41:37 GMT
272 .\" Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
273 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
274 .\" Author: Linus Torvalds <torvalds@linux-foundation.org>
275 .\" Date: Mon Mar 3 09:36:58 2014 -0800
276 .\"
277 .\" vfs: atomic f_pos accesses as per POSIX
278 .SH SEE ALSO
279 .BR close (2),
280 .BR fcntl (2),
281 .BR fsync (2),
282 .BR ioctl (2),
283 .BR lseek (2),
284 .BR open (2),
285 .BR pwrite (2),
286 .BR read (2),
287 .BR select (2),
288 .BR writev (2),
289 .BR fwrite (3)