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