xref: /freebsd-14.2/lib/libc/sys/write.2 (revision b2c76c41)
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. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)write.2	8.5 (Berkeley) 4/2/94
29.\"
30.Dd February 11, 2021
31.Dt WRITE 2
32.Os
33.Sh NAME
34.Nm write ,
35.Nm writev ,
36.Nm pwrite ,
37.Nm pwritev
38.Nd write output
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In unistd.h
43.Ft ssize_t
44.Fn write "int fd" "const void *buf" "size_t nbytes"
45.Ft ssize_t
46.Fn pwrite "int fd" "const void *buf" "size_t nbytes" "off_t offset"
47.In sys/uio.h
48.Ft ssize_t
49.Fn writev "int fd" "const struct iovec *iov" "int iovcnt"
50.Ft ssize_t
51.Fn pwritev "int fd" "const struct iovec *iov" "int iovcnt" "off_t offset"
52.Sh DESCRIPTION
53The
54.Fn write
55system call
56attempts to write
57.Fa nbytes
58of data to the object referenced by the descriptor
59.Fa fd
60from the buffer pointed to by
61.Fa buf .
62The
63.Fn writev
64system call
65performs the same action, but gathers the output data
66from the
67.Fa iovcnt
68buffers specified by the members of the
69.Fa iov
70array: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
71The
72.Fn pwrite
73and
74.Fn pwritev
75system calls
76perform the same functions, but write to the specified position in
77the file without modifying the file pointer.
78.Pp
79For
80.Fn writev
81and
82.Fn pwritev ,
83the
84.Fa iovec
85structure is defined as:
86.Pp
87.Bd -literal -offset indent -compact
88struct iovec {
89	void   *iov_base;  /* Base address. */
90	size_t iov_len;    /* Length. */
91};
92.Ed
93.Pp
94Each
95.Fa iovec
96entry specifies the base address and length of an area
97in memory from which data should be written.
98The
99.Fn writev
100system call
101will always write a complete area before proceeding
102to the next.
103.Pp
104On objects capable of seeking, the
105.Fn write
106starts at a position
107given by the pointer associated with
108.Fa fd ,
109see
110.Xr lseek 2 .
111Upon return from
112.Fn write ,
113the pointer is incremented by the number of bytes which were written.
114.Pp
115Objects that are not capable of seeking always write from the current
116position.
117The value of the pointer associated with such an object
118is undefined.
119.Pp
120If the real user is not the super-user, then
121.Fn write
122clears the set-user-id bit on a file.
123This prevents penetration of system security
124by a user who
125.Dq captures
126a writable set-user-id file
127owned by the super-user.
128.Pp
129When using non-blocking I/O on objects such as sockets that are subject
130to flow control,
131.Fn write
132and
133.Fn writev
134may write fewer bytes than requested;
135the return value must be noted,
136and the remainder of the operation should be retried when possible.
137.Sh RETURN VALUES
138Upon successful completion the number of bytes which were written
139is returned.
140Otherwise a -1 is returned and the global variable
141.Va errno
142is set to indicate the error.
143.Sh ERRORS
144The
145.Fn write ,
146.Fn writev ,
147.Fn pwrite
148and
149.Fn pwritev
150system calls
151will fail and the file pointer will remain unchanged if:
152.Bl -tag -width Er
153.It Bq Er EBADF
154The
155.Fa fd
156argument
157is not a valid descriptor open for writing.
158.It Bq Er EPIPE
159An attempt is made to write to a pipe that is not open
160for reading by any process.
161.It Bq Er EPIPE
162An attempt is made to write to a socket of type
163.Dv SOCK_STREAM
164that is not connected to a peer socket.
165.It Bq Er EFBIG
166An attempt was made to write a file that exceeds the process's
167file size limit or the maximum file size.
168.It Bq Er EFAULT
169Part of
170.Fa iov
171or data to be written to the file
172points outside the process's allocated address space.
173.It Bq Er EINVAL
174The pointer associated with
175.Fa fd
176was negative.
177.It Bq Er ENOSPC
178There is no free space remaining on the file system
179containing the file.
180.It Bq Er EDQUOT
181The user's quota of disk blocks on the file system
182containing the file has been exhausted.
183.It Bq Er EIO
184An I/O error occurred while reading from or writing to the file system.
185.It Bq Er EINTR
186A signal interrupted the write before it could be completed.
187.It Bq Er EAGAIN
188The file was marked for non-blocking I/O,
189and no data could be written immediately.
190.It Bq Er EROFS
191An attempt was made to write over a disk label area at the beginning
192of a slice.
193Use
194.Xr disklabel 8
195.Fl W
196to enable writing on the disk label area.
197.It Bq Er EINVAL
198The value
199.Fa nbytes
200is greater than
201.Dv SSIZE_MAX
202(or greater than
203.Dv INT_MAX ,
204if the sysctl
205.Va debug.iosize_max_clamp
206is non-zero).
207.It Bq Er EINTEGRITY
208The backing store for
209.Fa fd
210detected corrupted data while reading.
211(For example, writing a partial filesystem block may require first reading
212the existing block which may trigger this error.)
213.El
214.Pp
215In addition,
216.Fn writev
217and
218.Fn pwritev
219may return one of the following errors:
220.Bl -tag -width Er
221.It Bq Er EDESTADDRREQ
222The destination is no longer available when writing to a
223.Ux
224domain datagram socket on which
225.Xr connect 2
226had been used to set a destination address.
227.It Bq Er EINVAL
228The
229.Fa iovcnt
230argument
231was less than or equal to 0, or greater than
232.Dv IOV_MAX .
233.It Bq Er EINVAL
234One of the
235.Fa iov_len
236values in the
237.Fa iov
238array was negative.
239.It Bq Er EINVAL
240The sum of the
241.Fa iov_len
242values is greater than
243.Dv SSIZE_MAX
244(or greater than
245.Dv INT_MAX ,
246if the sysctl
247.Va debug.iosize_max_clamp
248is non-zero).
249.It Bq Er ENOBUFS
250The mbuf pool has been completely exhausted when writing to a socket.
251.El
252.Pp
253The
254.Fn pwrite
255and
256.Fn pwritev
257system calls may also return the following errors:
258.Bl -tag -width Er
259.It Bq Er EINVAL
260The
261.Fa offset
262value was negative.
263.It Bq Er ESPIPE
264The file descriptor is associated with a pipe, socket, or FIFO.
265.El
266.Sh SEE ALSO
267.Xr fcntl 2 ,
268.Xr lseek 2 ,
269.Xr open 2 ,
270.Xr pipe 2 ,
271.Xr select 2
272.Sh STANDARDS
273The
274.Fn write
275system call is expected to conform to
276.St -p1003.1-90 .
277The
278.Fn writev
279and
280.Fn pwrite
281system calls are expected to conform to
282.St -xpg4.2 .
283.Sh HISTORY
284The
285.Fn pwritev
286system call appeared in
287.Fx 6.0 .
288The
289.Fn pwrite
290function appeared in
291.At V.4 .
292The
293.Fn writev
294system call appeared in
295.Bx 4.2 .
296The
297.Fn write
298function appeared in
299.At v1 .
300.Sh BUGS
301The
302.Fn pwrite
303system call appends the file without changing the file offset if
304.Dv O_APPEND
305is set, contrary to
306.St -p1003.1-2008
307where
308.Fn pwrite
309writes into
310.Fa offset
311regardless of whether
312.Dv O_APPEND
313is set.
314