xref: /freebsd-13.1/lib/libc/sys/write.2 (revision bacb482d)
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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)write.2	8.5 (Berkeley) 4/2/94
33.\" $FreeBSD$
34.\"
35.Dd April 2, 1994
36.Dt WRITE 2
37.Os
38.Sh NAME
39.Nm write ,
40.Nm writev ,
41.Nm pwrite
42.Nd write output
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In sys/types.h
47.In sys/uio.h
48.In unistd.h
49.Ft ssize_t
50.Fn write "int d" "const void *buf" "size_t nbytes"
51.Ft ssize_t
52.Fn writev "int d" "const struct iovec *iov" "int iovcnt"
53.Ft ssize_t
54.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset"
55.Sh DESCRIPTION
56The
57.Fn write
58system call
59attempts to write
60.Fa nbytes
61of data to the object referenced by the descriptor
62.Fa d
63from the buffer pointed to by
64.Fa buf .
65The
66.Fn writev
67system call
68performs the same action, but gathers the output data
69from the
70.Fa iovcnt
71buffers specified by the members of the
72.Fa iov
73array: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
74The
75.Fn pwrite
76system call
77performs the same function, but writes to the specified position in
78the file without modifying the file pointer.
79.Pp
80For
81.Fn writev ,
82the
83.Fa iovec
84structure is defined as:
85.Pp
86.Bd -literal -offset indent -compact
87struct iovec {
88	void   *iov_base;  /* Base address. */
89	size_t iov_len;    /* Length. */
90};
91.Ed
92.Pp
93Each
94.Fa iovec
95entry specifies the base address and length of an area
96in memory from which data should be written.
97The
98.Fn writev
99system call
100will always write a complete area before proceeding
101to the next.
102.Pp
103On objects capable of seeking, the
104.Fn write
105starts at a position
106given by the pointer associated with
107.Fa d ,
108see
109.Xr lseek 2 .
110Upon return from
111.Fn write ,
112the pointer is incremented by the number of bytes which were written.
113.Pp
114Objects that are not capable of seeking always write from the current
115position.
116The value of the pointer associated with such an object
117is undefined.
118.Pp
119If the real user is not the super-user, then
120.Fn write
121clears the set-user-id bit on a file.
122This prevents penetration of system security
123by a user who
124.Dq captures
125a writable set-user-id file
126owned by the super-user.
127.Pp
128When using non-blocking I/O on objects such as sockets that are subject
129to flow control,
130.Fn write
131and
132.Fn writev
133may write fewer bytes than requested;
134the return value must be noted,
135and the remainder of the operation should be retried when possible.
136.Sh RETURN VALUES
137Upon successful completion the number of bytes which were written
138is returned.
139Otherwise a -1 is returned and the global variable
140.Va errno
141is set to indicate the error.
142.Sh ERRORS
143The
144.Fn write ,
145.Fn writev ,
146and
147.Fn pwrite
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 d
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 d
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.El
196.Pp
197In addition,
198.Fn writev
199may return one of the following errors:
200.Bl -tag -width Er
201.It Bq Er EDESTADDRREQ
202The destination is no longer available when writing to a
203.Ux
204domain datagram socket on which
205.Xr connect 2
206had been used to set a destination address.
207.It Bq Er EINVAL
208The
209.Fa iovcnt
210argument
211was less than or equal to 0, or greater than
212.Dv IOV_MAX .
213.It Bq Er EINVAL
214One of the
215.Fa iov_len
216values in the
217.Fa iov
218array was negative.
219.It Bq Er EINVAL
220The sum of the
221.Fa iov_len
222values in the
223.Fa iov
224array overflowed a 32-bit integer.
225.It Bq Er ENOBUFS
226The mbuf pool has been completely exhausted when writing to a socket.
227.El
228.Pp
229The
230.Fn pwrite
231system call may also return the following errors:
232.Bl -tag -width Er
233.It Bq Er EINVAL
234The specified file offset is invalid.
235.It Bq Er ESPIPE
236The file descriptor is associated with a pipe, socket, or FIFO.
237.El
238.Sh SEE ALSO
239.Xr fcntl 2 ,
240.Xr lseek 2 ,
241.Xr open 2 ,
242.Xr pipe 2 ,
243.Xr select 2
244.Sh STANDARDS
245The
246.Fn write
247system call is expected to conform to
248.St -p1003.1-90 .
249The
250.Fn writev
251and
252.Fn pwrite
253system calls are expected to conform to
254.St -xpg4.2 .
255.Sh HISTORY
256The
257.Fn pwrite
258function appeared in
259.At V.4 .
260The
261.Fn writev
262system call appeared in
263.Bx 4.2 .
264The
265.Fn write
266function appeared in
267.At v6 .
268