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