xref: /dragonfly/lib/libc/sys/write.2 (revision 548a3528)
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: src/lib/libc/sys/write.2,v 1.12.2.8 2002/10/13 17:42:14 schweikh Exp $
30.\" $DragonFly: src/lib/libc/sys/write.2,v 1.5 2006/04/27 16:01:33 swildner Exp $
31.\"
32.Dd April 27, 2006
33.Dt WRITE 2
34.Os
35.Sh NAME
36.Nm write ,
37.Nm writev ,
38.Nm pwrite ,
39.Nm pwritev
40.Nd write output
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In sys/types.h
45.In sys/uio.h
46.In unistd.h
47.Ft ssize_t
48.Fn write "int d" "const void *buf" "size_t nbytes"
49.Ft ssize_t
50.Fn writev "int d" "const struct iovec *iov" "int iovcnt"
51.Ft ssize_t
52.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset"
53.Ft ssize_t
54.Fn pwritev "int d" "const struct iovec *iov" "int iovcnt" "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
64and
65.Fn pwritev
66perform the same action, but gather 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].
72.Fn Pwrite
73and
74.Fn pwritev
75perform the same function, but write to the specified position in
76the file without modifying the file pointer.
77.Pp
78For
79.Fn writev
80and
81.Fn pwritev ,
82the
83.Fa iovec
84structure is defined as:
85.Pp
86.Bd -literal -offset indent -compact
87struct iovec {
88	char   *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.
97.Fn Writev
98and
99.Fn pwritev
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.  The 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.  Otherwise a -1 is returned and the global variable
138.Va errno
139is set to indicate the error.
140.Sh ERRORS
141.Fn Write ,
142.Fn writev ,
143.Fn pwrite ,
144and
145.Fn pwritev
146will fail and the file pointer will remain unchanged if:
147.Bl -tag -width Er
148.It Bq Er EBADF
149.Fa D
150is not a valid descriptor open for writing.
151.It Bq Er EPIPE
152An attempt is made to write to a pipe that is not open
153for reading by any process.
154.It Bq Er EPIPE
155An attempt is made to write to a socket of type
156.Dv SOCK_STREAM
157that is not connected to a peer socket.
158.It Bq Er EFBIG
159An attempt was made to write a file that exceeds the process's
160file size limit or the maximum file size.
161.It Bq Er EFAULT
162Part of
163.Fa iov
164or data to be written to the file
165points outside the process's allocated address space.
166.It Bq Er EINVAL
167The pointer associated with
168.Fa d
169was negative.
170.It Bq Er ENOSPC
171There is no free space remaining on the file system
172containing the file.
173.It Bq Er EDQUOT
174The user's quota of disk blocks on the file system
175containing the file has been exhausted.
176.It Bq Er EIO
177An I/O error occurred while reading from or writing to the file system.
178.It Bq Er EINTR
179A signal interrupted the write before it could be completed.
180.It Bq Er EAGAIN
181The file was marked for non-blocking I/O,
182and no data could be written immediately.
183.It Bq Er EROFS
184An attempt was made to write over a disk label area at the beginning
185of a disk.
186Use
187.Xr disklabel 8
188.Fl W
189to enable writing on the disk label area.
190.El
191.Pp
192In addition,
193.Fn writev
194and
195.Fn pwritev
196may return one of the following errors:
197.Bl -tag -width Er
198.It Bq Er EDESTADDRREQ
199The destination is no longer available when writing to a
200.Ux
201domain datagram socket on which
202.Xr connect 2
203had been used to set a destination address.
204.It Bq Er EINVAL
205.Fa Iovcnt
206was less than or equal to 0, or greater than
207.Dv UIO_MAXIOV .
208.It Bq Er EINVAL
209One of the
210.Fa iov_len
211values in the
212.Fa iov
213array was negative.
214.It Bq Er EINVAL
215The sum of the
216.Fa iov_len
217values in the
218.Fa iov
219array overflowed a 32-bit integer.
220.It Bq Er ENOBUFS
221The mbuf pool has been completely exhausted when writing to a socket.
222.El
223.Pp
224The
225.Fn pwrite
226and
227.Fn pwritev
228calls may also return the following errors:
229.Bl -tag -width Er
230.It Bq Er EINVAL
231The specified file offset is invalid.
232.It Bq Er ESPIPE
233The file descriptor is associated with a pipe, socket, or FIFO.
234.El
235.Sh SEE ALSO
236.Xr fcntl 2 ,
237.Xr lseek 2 ,
238.Xr open 2 ,
239.Xr pipe 2 ,
240.Xr select 2
241.Sh STANDARDS
242The
243.Fn write
244function call is expected to conform to
245.St -p1003.1-90 .
246The
247.Fn writev
248and
249.Fn pwrite
250functions are expected to conform to
251.St -xpg4.2 .
252.Sh HISTORY
253The
254.Fn pwritev
255function call was added in
256.Dx 1.5 .
257The
258.Fn pwrite
259function call
260appeared in
261.At V.4 .
262The
263.Fn writev
264function call
265appeared in
266.Bx 4.2 .
267A
268.Fn write
269function call appeared in
270.At v6 .
271