xref: /openbsd/lib/libc/sys/write.2 (revision 5dea098c)
1.\"	$OpenBSD: write.2,v 1.45 2023/02/05 12:33:17 jsg 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: February 5 2023 $
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.In unistd.h
44.Ft ssize_t
45.Fn write "int d" "const void *buf" "size_t nbytes"
46.Ft ssize_t
47.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset"
48.Pp
49.In sys/uio.h
50.Ft ssize_t
51.Fn writev "int d" "const struct iovec *iov" "int iovcnt"
52.In sys/types.h
53.In sys/uio.h
54.Ft ssize_t
55.Fn pwritev "int d" "const struct iovec *iov" "int iovcnt" "off_t offset"
56.Sh DESCRIPTION
57.Fn write
58attempts to write
59.Fa nbytes
60of data to the object referenced by the descriptor
61.Fa d
62from the buffer pointed to by
63.Fa buf .
64.Fn writev
65performs the same action, but gathers the output data from the
66.Fa iovcnt
67buffers specified by the members of the
68.Fa iov
69array: iov[0], iov[1], ..., iov[iovcnt-1].
70.Fn pwrite
71and
72.Fn pwritev
73perform the same functions, but write to the specified position
74.Fa offset
75in the file without modifying the file pointer.
76.Pp
77For
78.Fn writev
79and
80.Fn pwritev ,
81the
82.Vt iovec
83structure is defined as:
84.Bd -literal -offset indent
85struct iovec {
86	void	*iov_base;
87	size_t	 iov_len;
88};
89.Ed
90.Pp
91Each
92.Vt iovec
93entry specifies the base address and length of an area
94in memory from which data should be written.
95.Fn writev
96and
97.Fn pwritev
98will always write a complete area before proceeding to the next.
99.Pp
100On objects capable of seeking, the
101.Fn write
102starts at a position given by the pointer associated with
103.Fa d
104(see
105.Xr lseek 2 ) .
106Upon return from
107.Fn write ,
108the pointer is incremented by the number of bytes which were written.
109If a file was opened with the
110.Dv O_APPEND
111flag (see
112.Xr open 2 ) ,
113calls to
114.Fn write
115or
116.Fn writev
117will automatically set the pointer to the end of the file before writing.
118.Pp
119Objects that are not capable of seeking always write from the current
120position.
121The value of the pointer associated with such an object is undefined.
122.Pp
123If the real user is not the superuser, then
124.Fn write
125clears the set-user-ID bit on a file.
126This prevents penetration of system security by a user who
127.Dq captures
128a writable set-user-ID file owned by the superuser.
129.Pp
130If
131.Fn write
132succeeds, it will update the st_ctime and st_mtime fields of the file's
133meta-data (see
134.Xr stat 2 ) .
135.Pp
136When using non-blocking I/O on objects such as sockets that are subject
137to flow control,
138.Fn write
139and
140.Fn writev
141may write fewer bytes than requested; the return value must be noted,
142and the remainder of the operation should be retried when possible.
143.Pp
144Note that
145.Fn writev
146and
147.Fn pwritev
148will fail if the value of
149.Fa iovcnt
150exceeds the constant
151.Dv IOV_MAX .
152.Sh RETURN VALUES
153Upon successful completion the number of bytes which were written
154is returned.
155Otherwise, a \-1 is returned and the global variable
156.Va errno
157is set to indicate the error.
158.Sh EXAMPLES
159A typical loop allowing partial writes looks like this:
160.Bd -literal
161const char *buf;
162size_t bsz, off;
163ssize_t nw;
164int d;
165
166for (off = 0; off < bsz; off += nw)
167	if ((nw = write(d, buf + off, bsz - off)) == 0 || nw == -1)
168		err(1, "write");
169.Ed
170.Sh ERRORS
171.Fn write ,
172.Fn pwrite ,
173.Fn writev ,
174and
175.Fn pwritev
176will fail and the file pointer will remain unchanged if:
177.Bl -tag -width Er
178.It Bq Er EBADF
179.Fa d
180is not a valid descriptor open for writing.
181.It Bq Er EFBIG
182An attempt was made to write a file that exceeds the process's
183file size limit or the maximum file size.
184.It Bq Er ENOSPC
185There is no free space remaining on the file system containing the file.
186.It Bq Er EDQUOT
187The user's quota of disk blocks on the file system containing the file
188has been exhausted.
189.It Bq Er EINTR
190A write to a slow device
191(i.e. one that might block for an arbitrary amount of time)
192was interrupted by the delivery of a signal
193before any data could be written.
194.It Bq Er EIO
195An I/O error occurred while reading from or writing to the file system.
196.It Bq Er EFAULT
197Part of
198.Fa buf
199points outside the process's allocated address space.
200.El
201.Pp
202In addition,
203.Fn write
204and
205.Fn writev
206may return the following errors:
207.Bl -tag -width Er
208.It Bq Er EPIPE
209An attempt is made to write to a pipe that is not open
210for reading by any process.
211.It Bq Er EPIPE
212An attempt is made to write to a socket of type
213.Dv SOCK_STREAM
214that is not connected to a peer socket.
215.It Bq Er EAGAIN
216The file was marked for non-blocking I/O, and no data could be
217written immediately.
218.It Bq Er ENETDOWN
219The destination address specified a network that is down.
220.It Bq Er EDESTADDRREQ
221The destination is no longer available when writing to a
222.Ux Ns -domain
223datagram socket on which
224.Xr connect 2
225had been used to set a destination address.
226.It Bq Er EIO
227The process is a member of a background process attempting to write
228to its controlling terminal,
229.Dv TOSTOP
230is set on the terminal,
231the process isn't ignoring the
232.Dv SIGTTOUT
233signal and the thread isn't blocking the
234.Dv SIGTTOUT
235signal,
236and either the process was created with
237.Xr vfork 2
238and hasn't successfully executed one of the exec functions or
239the process group is orphaned.
240.El
241.Pp
242.Fn write
243and
244.Fn pwrite
245may return the following error:
246.Bl -tag -width Er
247.It Bq Er EINVAL
248.Fa nbytes
249was larger than
250.Dv SSIZE_MAX .
251.El
252.Pp
253.Fn pwrite
254and
255.Fn pwritev
256may return the following error:
257.Bl -tag -width Er
258.It Bq Er EINVAL
259.Fa offset
260was negative.
261.It Bq Er ESPIPE
262.Fa d
263is associated with a pipe, socket, FIFO, or tty.
264.El
265.Pp
266.Fn writev
267and
268.Fn pwritev
269may return one of the following errors:
270.Bl -tag -width Er
271.It Bq Er EINVAL
272.Fa iovcnt
273was less than or equal to 0, or greater than
274.Dv IOV_MAX .
275.It Bq Er EINVAL
276The sum of the
277.Fa iov_len
278values in the
279.Fa iov
280array overflowed an
281.Vt ssize_t .
282.It Bq Er EFAULT
283Part of
284.Fa iov
285points outside the process's allocated address space.
286.It Bq Er ENOBUFS
287The system lacked sufficient buffer space or a queue was full.
288.El
289.Sh SEE ALSO
290.Xr fcntl 2 ,
291.Xr lseek 2 ,
292.Xr open 2 ,
293.Xr pipe 2 ,
294.Xr poll 2 ,
295.Xr select 2 ,
296.Xr termios 4
297.Sh STANDARDS
298The
299.Fn write ,
300.Fn writev ,
301and
302.Fn pwrite
303functions conform to
304.St -p1003.1-2008 .
305.Sh HISTORY
306The
307.Fn write
308function call appeared in
309.At v1 ,
310.Fn pwrite
311in
312.At V.4 ,
313.Fn writev
314in
315.Bx 4.1c ,
316and
317.Fn pwritev
318in
319.Ox 2.7 .
320.Sh CAVEATS
321Error checks should explicitly test for \-1.
322On some platforms, if
323.Fa nbytes
324is larger than
325.Dv SSIZE_MAX
326but smaller than
327.Dv SIZE_MAX
328\- 2, the return value of an error-free call
329may appear as a negative number distinct from \-1.
330