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