]>
Commit | Line | Data |
---|---|---|
9bccf70c A |
1 | .\" Copyright (c) 1980, 1991, 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
3 | .\" | |
4 | .\" Redistribution and use in source and binary forms, with or without | |
5 | .\" modification, are permitted provided that the following conditions | |
6 | .\" are met: | |
7 | .\" 1. Redistributions of source code must retain the above copyright | |
8 | .\" notice, this list of conditions and the following disclaimer. | |
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
10 | .\" notice, this list of conditions and the following disclaimer in the | |
11 | .\" documentation and/or other materials provided with the distribution. | |
12 | .\" 3. All advertising materials mentioning features or use of this software | |
13 | .\" must display the following acknowledgement: | |
14 | .\" This product includes software developed by the University of | |
15 | .\" California, Berkeley and its contributors. | |
16 | .\" 4. Neither the name of the University nor the names of its contributors | |
17 | .\" may be used to endorse or promote products derived from this software | |
18 | .\" without specific prior written permission. | |
19 | .\" | |
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | .\" SUCH DAMAGE. | |
31 | .\" | |
32 | .\" @(#)write.2 8.5 (Berkeley) 4/2/94 | |
33 | .\" $FreeBSD: src/lib/libc/sys/write.2,v 1.12.2.7 2001/12/14 18:34:02 ru Exp $ | |
34 | .\" | |
3e170ce0 | 35 | .Dd March 18, 2015 |
9bccf70c A |
36 | .Dt WRITE 2 |
37 | .Os | |
38 | .Sh NAME | |
2d21ac55 | 39 | .Nm pwrite , |
9bccf70c | 40 | .Nm write , |
f427ee49 | 41 | .Nm pwritev , |
2d21ac55 | 42 | .Nm writev |
9bccf70c A |
43 | .Nd write output |
44 | .Sh LIBRARY | |
45 | .Lb libc | |
46 | .Sh SYNOPSIS | |
9bccf70c A |
47 | .In unistd.h |
48 | .Ft ssize_t | |
2d21ac55 A |
49 | .Fo pwrite |
50 | .Fa "int fildes" | |
51 | .Fa "const void *buf" | |
52 | .Fa "size_t nbyte" | |
53 | .Fa "off_t offset" | |
54 | .Fc | |
9bccf70c | 55 | .Ft ssize_t |
2d21ac55 A |
56 | .Fo write |
57 | .Fa "int fildes" | |
58 | .Fa "const void *buf" | |
59 | .Fa "size_t nbyte" | |
60 | .Fc | |
61 | .In sys/uio.h | |
9bccf70c | 62 | .Ft ssize_t |
2d21ac55 A |
63 | .Fo writev |
64 | .Fa "int fildes" | |
65 | .Fa "const struct iovec *iov" | |
66 | .Fa "int iovcnt" | |
67 | .Fc | |
f427ee49 A |
68 | .Ft ssize_t |
69 | .Fo pwritev | |
70 | .Fa "int fildes" | |
71 | .Fa "const struct iovec *iov" | |
72 | .Fa "int iovcnt" | |
73 | .Fa "off_t offset" | |
74 | .Fc | |
9bccf70c | 75 | .Sh DESCRIPTION |
3e170ce0 | 76 | .Fn write |
9bccf70c | 77 | attempts to write |
2d21ac55 | 78 | .Fa nbyte |
9bccf70c | 79 | of data to the object referenced by the descriptor |
2d21ac55 | 80 | .Fa fildes |
9bccf70c A |
81 | from the buffer pointed to by |
82 | .Fa buf . | |
3e170ce0 | 83 | .Fn writev |
9bccf70c A |
84 | performs the same action, but gathers the output data |
85 | from the | |
86 | .Fa iovcnt | |
87 | buffers specified by the members of the | |
88 | .Fa iov | |
89 | array: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. | |
3e170ce0 | 90 | .Fn pwrite |
f427ee49 A |
91 | and |
92 | .Fn pwritev | |
93 | perform the same functions, but write to the specified position in | |
9bccf70c A |
94 | the file without modifying the file pointer. |
95 | .Pp | |
96 | For | |
f427ee49 A |
97 | .Fn writev |
98 | and | |
99 | .Fn pwritev , | |
9bccf70c A |
100 | the |
101 | .Fa iovec | |
102 | structure is defined as: | |
103 | .Pp | |
104 | .Bd -literal -offset indent -compact | |
105 | struct iovec { | |
106 | char *iov_base; /* Base address. */ | |
107 | size_t iov_len; /* Length. */ | |
108 | }; | |
109 | .Ed | |
110 | .Pp | |
111 | Each | |
112 | .Fa iovec | |
113 | entry specifies the base address and length of an area | |
114 | in memory from which data should be written. | |
3e170ce0 | 115 | .Fn writev |
f427ee49 A |
116 | and |
117 | .Fn pwritev | |
9bccf70c A |
118 | will always write a complete area before proceeding |
119 | to the next. | |
120 | .Pp | |
121 | On objects capable of seeking, the | |
122 | .Fn write | |
123 | starts at a position | |
124 | given by the pointer associated with | |
2d21ac55 | 125 | .Fa fildes , |
9bccf70c A |
126 | see |
127 | .Xr lseek 2 . | |
128 | Upon return from | |
129 | .Fn write , | |
130 | the pointer is incremented by the number of bytes which were written. | |
131 | .Pp | |
132 | Objects that are not capable of seeking always write from the current | |
133 | position. The value of the pointer associated with such an object | |
134 | is undefined. | |
135 | .Pp | |
136 | If the real user is not the super-user, then | |
137 | .Fn write | |
138 | clears the set-user-id bit on a file. | |
139 | This prevents penetration of system security | |
140 | by a user who | |
141 | .Dq captures | |
142 | a writable set-user-id file | |
143 | owned by the super-user. | |
144 | .Pp | |
2d21ac55 A |
145 | When using non-blocking I/O on objects, such as sockets, |
146 | that are subject to flow control, | |
9bccf70c A |
147 | .Fn write |
148 | and | |
149 | .Fn writev | |
150 | may write fewer bytes than requested; | |
151 | the return value must be noted, | |
152 | and the remainder of the operation should be retried when possible. | |
153 | .Sh RETURN VALUES | |
2d21ac55 A |
154 | Upon successful completion the number of bytes |
155 | which were written is returned. | |
156 | Otherwise, a -1 is returned and the global variable | |
9bccf70c A |
157 | .Va errno |
158 | is set to indicate the error. | |
159 | .Sh ERRORS | |
2d21ac55 A |
160 | The |
161 | .Fn write , | |
9bccf70c | 162 | .Fn writev , |
f427ee49 | 163 | .Fn pwrite , |
9bccf70c | 164 | and |
f427ee49 | 165 | .Fn pwritev |
2d21ac55 | 166 | system calls will fail and the file pointer will remain unchanged if: |
9bccf70c | 167 | .Bl -tag -width Er |
2d21ac55 A |
168 | .\" =========== |
169 | .It Bq Er EDQUOT | |
170 | The user's quota of disk blocks on the file system | |
171 | containing the file is exhausted. | |
172 | .\" =========== | |
9bccf70c A |
173 | .It Bq Er EFAULT |
174 | Part of | |
175 | .Fa iov | |
176 | or data to be written to the file | |
177 | points outside the process's allocated address space. | |
2d21ac55 | 178 | .\" =========== |
9bccf70c A |
179 | .It Bq Er EINVAL |
180 | The pointer associated with | |
2d21ac55 A |
181 | .Fa fildes |
182 | is negative. | |
183 | .El | |
184 | .Pp | |
185 | The | |
186 | .Fn write | |
187 | and | |
188 | .Fn pwrite | |
189 | system calls will fail and the file pointer will remain unchanged if: | |
190 | .Bl -tag -width Er | |
191 | .\" =========== | |
9bccf70c | 192 | .It Bq Er EAGAIN |
2d21ac55 | 193 | The file is marked for non-blocking I/O, |
9bccf70c | 194 | and no data could be written immediately. |
2d21ac55 A |
195 | .\" =========== |
196 | .It Bq Er EBADF | |
197 | .Fa fildes | |
198 | is not a valid file descriptor open for writing. | |
199 | .\" =========== | |
200 | .It Bq Er ECONNRESET | |
201 | A write is attempted on a socket that is not connected. | |
202 | .\" =========== | |
203 | .It Bq Er EFBIG | |
204 | An attempt is made to write a file that exceeds the process's | |
205 | file size limit or the maximum file size. | |
206 | .\" =========== | |
207 | .It Bq Er EFBIG | |
208 | The file is a regular file, | |
209 | .Fa nbyte | |
210 | is greater than 0, | |
211 | and the starting position is greater than or equal | |
212 | to the offset maximum established in the open file description | |
213 | associated with | |
214 | .Fa fildes . | |
215 | .\" =========== | |
216 | .It Bq Er EINTR | |
217 | A signal interrupts the write before it could be completed. | |
218 | .\" =========== | |
219 | .It Bq Er EIO | |
220 | An I/O error occurs while reading from or writing to the file system. | |
221 | .\" =========== | |
222 | .It Bq Er ENETDOWN | |
223 | A write is attempted on a socket | |
224 | and the local network interface used to reach the destination is down. | |
225 | .\" =========== | |
226 | .It Bq Er ENETUNREACH | |
227 | A write is attempted on a socket and no route to the network is present. | |
228 | .\" =========== | |
229 | .It Bq Er ENOSPC | |
230 | There is no free space remaining on the file system containing the file. | |
231 | .\" =========== | |
232 | .It Bq Er ENXIO | |
233 | A request is made of a nonexistent device, | |
234 | or the request is outside the capabilities of the device. | |
235 | .\" =========== | |
236 | .It Bq Er EPIPE | |
237 | An attempt is made to write to a pipe that is not open | |
238 | for reading by any process. | |
239 | .\" =========== | |
240 | .It Bq Er EPIPE | |
241 | An attempt is made to write to a socket of type | |
242 | .Dv SOCK_STREAM | |
243 | that is not connected to a peer socket. | |
244 | .El | |
245 | .Pp | |
246 | The | |
247 | .Fn write | |
248 | and | |
249 | .Fn writev | |
250 | calls may also return the following errors: | |
251 | .Bl -tag -width Er | |
252 | .\" =========== | |
253 | .It Bq Er EAGAIN | |
254 | See EWOULDBLOCK, below. | |
255 | .\" =========== | |
256 | .It Bq Er EWOULDBLOCK | |
257 | The file descriptor is for a socket, is marked O_NONBLOCK, | |
258 | and write would block. | |
259 | The exact error code depends on the protocol, | |
260 | but EWOULDBLOCK is more common. | |
9bccf70c A |
261 | .El |
262 | .Pp | |
263 | In addition, | |
264 | .Fn writev | |
265 | may return one of the following errors: | |
266 | .Bl -tag -width Er | |
2d21ac55 | 267 | .\" =========== |
9bccf70c A |
268 | .It Bq Er EDESTADDRREQ |
269 | The destination is no longer available when writing to a | |
270 | .Ux | |
271 | domain datagram socket on which | |
272 | .Xr connect 2 | |
3e170ce0 A |
273 | or |
274 | .Xr connectx 2 | |
9bccf70c | 275 | had been used to set a destination address. |
2d21ac55 | 276 | .\" =========== |
f427ee49 A |
277 | .It Bq Er ENOBUFS |
278 | The mbuf pool has been completely exhausted when writing to a socket. | |
279 | .El | |
280 | .Pp | |
281 | The | |
282 | .Fn writev | |
283 | and | |
284 | .Fn pwritev | |
285 | calls may also return the following errors: | |
286 | .Bl -tag -width Er | |
287 | .\" =========== | |
9bccf70c A |
288 | .It Bq Er EINVAL |
289 | .Fa Iovcnt | |
2d21ac55 | 290 | is less than or equal to 0, or greater than |
9bccf70c | 291 | .Dv UIO_MAXIOV . |
2d21ac55 | 292 | .\" =========== |
9bccf70c A |
293 | .It Bq Er EINVAL |
294 | One of the | |
295 | .Fa iov_len | |
296 | values in the | |
297 | .Fa iov | |
2d21ac55 A |
298 | array is negative. |
299 | .\" =========== | |
9bccf70c A |
300 | .It Bq Er EINVAL |
301 | The sum of the | |
302 | .Fa iov_len | |
303 | values in the | |
304 | .Fa iov | |
2d21ac55 | 305 | array overflows a 32-bit integer. |
9bccf70c A |
306 | .El |
307 | .Pp | |
308 | The | |
309 | .Fn pwrite | |
f427ee49 A |
310 | and |
311 | .Fn pwritev | |
312 | calls may also return the following errors: | |
9bccf70c | 313 | .Bl -tag -width Er |
2d21ac55 | 314 | .\" =========== |
9bccf70c A |
315 | .It Bq Er EINVAL |
316 | The specified file offset is invalid. | |
2d21ac55 | 317 | .\" =========== |
9bccf70c A |
318 | .It Bq Er ESPIPE |
319 | The file descriptor is associated with a pipe, socket, or FIFO. | |
320 | .El | |
2d21ac55 A |
321 | .Sh LEGACY SYNOPSIS |
322 | .Fd #include <sys/types.h> | |
323 | .Fd #include <sys/uio.h> | |
324 | .Fd #include <unistd.h> | |
325 | .Pp | |
326 | These include files are needed for all three functions. | |
9bccf70c A |
327 | .Sh SEE ALSO |
328 | .Xr fcntl 2 , | |
329 | .Xr lseek 2 , | |
330 | .Xr open 2 , | |
331 | .Xr pipe 2 , | |
2d21ac55 A |
332 | .Xr select 2 , |
333 | .Xr compat 5 | |
9bccf70c A |
334 | .Sh STANDARDS |
335 | The | |
336 | .Fn write | |
337 | function call is expected to conform to | |
338 | .St -p1003.1-90 . | |
339 | The | |
340 | .Fn writev | |
341 | and | |
342 | .Fn pwrite | |
343 | functions are expected to conform to | |
344 | .St -xpg4.2 . | |
f427ee49 A |
345 | .Fn pwritev |
346 | is nonstandard. | |
9bccf70c A |
347 | .Sh HISTORY |
348 | The | |
349 | .Fn pwrite | |
350 | function call | |
351 | appeared in | |
352 | .At V.4 . | |
353 | The | |
354 | .Fn writev | |
355 | function call | |
356 | appeared in | |
357 | .Bx 4.2 . | |
358 | A | |
359 | .Fn write | |
360 | function call appeared in | |
361 | .At v6 . |