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