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