xref: /openbsd/lib/libc/sys/write.2 (revision 7b36286a)
1.\"	$OpenBSD: write.2,v 1.30 2007/05/31 19:19:34 jmc Exp $
2.\"	$NetBSD: write.2,v 1.6 1995/02/27 12:39:43 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)write.2	8.5 (Berkeley) 4/2/94
32.\"
33.Dd $Mdocdate: May 31 2007 $
34.Dt WRITE 2
35.Os
36.Sh NAME
37.Nm write ,
38.Nm writev ,
39.Nm pwrite ,
40.Nm pwritev
41.Nd write output
42.Sh SYNOPSIS
43.Fd #include <sys/types.h>
44.Fd #include <unistd.h>
45.Ft ssize_t
46.Fn write "int d" "const void *buf" "size_t nbytes"
47.Ft ssize_t
48.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset"
49.Pp
50.Fd #include <sys/types.h>
51.Fd #include <sys/uio.h>
52.Fd #include <unistd.h>
53.Ft ssize_t
54.Fn writev "int d" "const struct iovec *iov" "int iovcnt"
55.Ft ssize_t
56.Fn pwritev "int d" "const struct iovec *iov" "int iovcnt" "off_t offset"
57.Sh DESCRIPTION
58.Fn write
59attempts to write
60.Fa nbytes
61of data to the object referenced by the descriptor
62.Fa d
63from the buffer pointed to by
64.Fa buf .
65.Fn writev
66performs the same action, but gathers the output data from the
67.Fa iovcnt
68buffers specified by the members of the
69.Fa iov
70array: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
71.Fn pwrite
72and
73.Fn pwritev
74perform the same functions, but write to the specified position
75.Fa offset
76in the file without modifying the file pointer.
77.Pp
78For
79.Fn writev
80and
81.Fn pwritev ,
82the
83.Fa iovec
84structure is defined as:
85.Bd -literal -offset indent
86struct iovec {
87	void *iov_base;
88	size_t iov_len;
89};
90.Ed
91.Pp
92Each
93.Fa iovec
94entry specifies the base address and length of an area
95in memory from which data should be written.
96.Fn writev
97and
98.Fn pwritev
99will always write a complete area before proceeding to the next.
100.Pp
101On objects capable of seeking, the
102.Fn write
103starts at a position given by the pointer associated with
104.Fa d
105(see
106.Xr lseek 2 ) .
107Upon return from
108.Fn write ,
109the pointer is incremented by the number of bytes which were written.
110.Pp
111Objects that are not capable of seeking always write from the current
112position.
113The value of the pointer associated with such an object is undefined.
114.Pp
115If the real user is not the superuser, then
116.Fn write
117clears the set-user-ID bit on a file.
118This prevents penetration of system security by a user who
119.Dq captures
120a writable set-user-ID file owned by the superuser.
121.Pp
122If
123.Fn write
124succeeds it will update the st_ctime and st_mtime fields of the file's
125meta-data (see
126.Xr stat 2 ) .
127.Pp
128When using non-blocking I/O on objects such as sockets that are subject
129to flow control,
130.Fn write
131and
132.Fn writev
133may write fewer bytes than requested; the return value must be noted,
134and the remainder of the operation should be retried when possible.
135.Pp
136Note that
137.Fn writev
138and
139.Fn pwritev
140will fail if the value of
141.Fa iovcnt
142exceeds the constant
143.Dv IOV_MAX .
144.Sh RETURN VALUES
145Upon successful completion the number of bytes which were written
146is returned.
147Otherwise, a \-1 is returned and the global variable
148.Va errno
149is set to indicate the error.
150.Sh ERRORS
151.Fn write ,
152.Fn pwrite ,
153.Fn writev ,
154and
155.Fn pwritev
156will fail and the file pointer will remain unchanged if:
157.Bl -tag -width Er
158.It Bq Er EBADF
159.Fa d
160is not a valid descriptor open for writing.
161.It Bq Er EPIPE
162An attempt is made to write to a pipe that is not open
163for reading by any process.
164.It Bq Er EPIPE
165An attempt is made to write to a socket of type
166.Dv SOCK_STREAM
167that is not connected to a peer socket.
168.It Bq Er EFBIG
169An attempt was made to write a file that exceeds the process's
170file size limit or the maximum file size.
171.It Bq Er EINVAL
172The pointer associated with
173.Fa d
174was negative.
175.It Bq Er ENOSPC
176There is no free space remaining on the file system containing the file.
177.It Bq Er EDQUOT
178The user's quota of disk blocks on the file system containing the file
179has been exhausted.
180.It Bq Er EIO
181An I/O error occurred while reading from or writing to the file system.
182.It Bq Er EAGAIN
183The file was marked for non-blocking I/O, and no data could be
184written immediately.
185.El
186.Pp
187In addition,
188.Fn write
189and
190.Fn pwrite
191may return the following error:
192.Bl -tag -width Er
193.It Bq Er EFAULT
194Part of
195.Fa iov
196or data to be written to the file points outside the process's
197allocated address space.
198.It Bq Er EINVAL
199.Fa nbytes
200was larger than
201.Ev SSIZE_MAX .
202.El
203.Pp
204Also,
205.Fn writev
206and
207.Fn pwritev
208may return one of the following errors:
209.Bl -tag -width Er
210.It Bq Er EDESTADDRREQ
211The destination is no longer available when writing to a
212.Ux
213domain datagram socket on which
214.Xr connect 2
215had been used to set a destination address.
216.It Bq Er EINVAL
217.Fa iovcnt
218was less than or equal to 0, or greater than
219.Dv IOV_MAX .
220.It Bq Er EINVAL
221The sum of the
222.Fa iov_len
223values in the
224.Fa iov
225array overflowed an
226.Em ssize_t .
227.El
228.Sh SEE ALSO
229.Xr fcntl 2 ,
230.Xr lseek 2 ,
231.Xr open 2 ,
232.Xr pipe 2 ,
233.Xr poll 2 ,
234.Xr select 2
235.Sh STANDARDS
236The
237.Fn write
238function conforms to
239.St -p1003.1-90 .
240The
241.Fn writev
242and
243.Fn pwrite
244functions conform to
245.St -xpg4.2 .
246.Sh HISTORY
247The
248.Fn pwritev
249function call appeared in
250.Ox 2.7 .
251The
252.Fn pwrite
253function call appeared in
254.At V.4 .
255The
256.Fn writev
257function call appeared in
258.Bx 4.2 .
259The
260.Fn write
261function call appeared in
262.At v2 .
263.Sh CAVEATS
264Error checks should explicitly test for \-1.
265Code such as
266.Bd -literal -offset indent
267while ((nr = write(fd, buf, sizeof(buf))) > 0)
268.Ed
269.Pp
270is not maximally portable, as some platforms allow for
271.Va nbytes
272to range between
273.Dv SSIZE_MAX
274and
275.Dv SIZE_MAX
276\- 2, in which case the return value of an error-free
277.Fn write
278may appear as a negative number distinct from \-1.
279Proper loops should use
280.Bd -literal -offset indent
281while ((nr = write(fd, buf, sizeof(buf))) != -1 && nr != 0)
282.Ed
283