xref: /freebsd/lib/libsys/write.2 (revision 8269e767)
18269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993
28269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
38269e767SBrooks Davis.\"
48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
68269e767SBrooks Davis.\" are met:
78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
88269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
108269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
118269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
138269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
148269e767SBrooks Davis.\"    without specific prior written permission.
158269e767SBrooks Davis.\"
168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268269e767SBrooks Davis.\" SUCH DAMAGE.
278269e767SBrooks Davis.\"
288269e767SBrooks Davis.Dd February 11, 2021
298269e767SBrooks Davis.Dt WRITE 2
308269e767SBrooks Davis.Os
318269e767SBrooks Davis.Sh NAME
328269e767SBrooks Davis.Nm write ,
338269e767SBrooks Davis.Nm writev ,
348269e767SBrooks Davis.Nm pwrite ,
358269e767SBrooks Davis.Nm pwritev
368269e767SBrooks Davis.Nd write output
378269e767SBrooks Davis.Sh LIBRARY
388269e767SBrooks Davis.Lb libc
398269e767SBrooks Davis.Sh SYNOPSIS
408269e767SBrooks Davis.In unistd.h
418269e767SBrooks Davis.Ft ssize_t
428269e767SBrooks Davis.Fn write "int fd" "const void *buf" "size_t nbytes"
438269e767SBrooks Davis.Ft ssize_t
448269e767SBrooks Davis.Fn pwrite "int fd" "const void *buf" "size_t nbytes" "off_t offset"
458269e767SBrooks Davis.In sys/uio.h
468269e767SBrooks Davis.Ft ssize_t
478269e767SBrooks Davis.Fn writev "int fd" "const struct iovec *iov" "int iovcnt"
488269e767SBrooks Davis.Ft ssize_t
498269e767SBrooks Davis.Fn pwritev "int fd" "const struct iovec *iov" "int iovcnt" "off_t offset"
508269e767SBrooks Davis.Sh DESCRIPTION
518269e767SBrooks DavisThe
528269e767SBrooks Davis.Fn write
538269e767SBrooks Davissystem call
548269e767SBrooks Davisattempts to write
558269e767SBrooks Davis.Fa nbytes
568269e767SBrooks Davisof data to the object referenced by the descriptor
578269e767SBrooks Davis.Fa fd
588269e767SBrooks Davisfrom the buffer pointed to by
598269e767SBrooks Davis.Fa buf .
608269e767SBrooks DavisThe
618269e767SBrooks Davis.Fn writev
628269e767SBrooks Davissystem call
638269e767SBrooks Davisperforms the same action, but gathers the output data
648269e767SBrooks Davisfrom the
658269e767SBrooks Davis.Fa iovcnt
668269e767SBrooks Davisbuffers specified by the members of the
678269e767SBrooks Davis.Fa iov
688269e767SBrooks Davisarray: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
698269e767SBrooks DavisThe
708269e767SBrooks Davis.Fn pwrite
718269e767SBrooks Davisand
728269e767SBrooks Davis.Fn pwritev
738269e767SBrooks Davissystem calls
748269e767SBrooks Davisperform the same functions, but write to the specified position in
758269e767SBrooks Davisthe file without modifying the file pointer.
768269e767SBrooks Davis.Pp
778269e767SBrooks DavisFor
788269e767SBrooks Davis.Fn writev
798269e767SBrooks Davisand
808269e767SBrooks Davis.Fn pwritev ,
818269e767SBrooks Davisthe
828269e767SBrooks Davis.Fa iovec
838269e767SBrooks Davisstructure is defined as:
848269e767SBrooks Davis.Pp
858269e767SBrooks Davis.Bd -literal -offset indent -compact
868269e767SBrooks Davisstruct iovec {
878269e767SBrooks Davis	void   *iov_base;  /* Base address. */
888269e767SBrooks Davis	size_t iov_len;    /* Length. */
898269e767SBrooks Davis};
908269e767SBrooks Davis.Ed
918269e767SBrooks Davis.Pp
928269e767SBrooks DavisEach
938269e767SBrooks Davis.Fa iovec
948269e767SBrooks Davisentry specifies the base address and length of an area
958269e767SBrooks Davisin memory from which data should be written.
968269e767SBrooks DavisThe
978269e767SBrooks Davis.Fn writev
988269e767SBrooks Davissystem call
998269e767SBrooks Daviswill always write a complete area before proceeding
1008269e767SBrooks Davisto the next.
1018269e767SBrooks Davis.Pp
1028269e767SBrooks DavisOn objects capable of seeking, the
1038269e767SBrooks Davis.Fn write
1048269e767SBrooks Davisstarts at a position
1058269e767SBrooks Davisgiven by the pointer associated with
1068269e767SBrooks Davis.Fa fd ,
1078269e767SBrooks Davissee
1088269e767SBrooks Davis.Xr lseek 2 .
1098269e767SBrooks DavisUpon return from
1108269e767SBrooks Davis.Fn write ,
1118269e767SBrooks Davisthe pointer is incremented by the number of bytes which were written.
1128269e767SBrooks Davis.Pp
1138269e767SBrooks DavisObjects that are not capable of seeking always write from the current
1148269e767SBrooks Davisposition.
1158269e767SBrooks DavisThe value of the pointer associated with such an object
1168269e767SBrooks Davisis undefined.
1178269e767SBrooks Davis.Pp
1188269e767SBrooks DavisIf the real user is not the super-user, then
1198269e767SBrooks Davis.Fn write
1208269e767SBrooks Davisclears the set-user-id bit on a file.
1218269e767SBrooks DavisThis prevents penetration of system security
1228269e767SBrooks Davisby a user who
1238269e767SBrooks Davis.Dq captures
1248269e767SBrooks Davisa writable set-user-id file
1258269e767SBrooks Davisowned by the super-user.
1268269e767SBrooks Davis.Pp
1278269e767SBrooks DavisWhen using non-blocking I/O on objects such as sockets that are subject
1288269e767SBrooks Davisto flow control,
1298269e767SBrooks Davis.Fn write
1308269e767SBrooks Davisand
1318269e767SBrooks Davis.Fn writev
1328269e767SBrooks Davismay write fewer bytes than requested;
1338269e767SBrooks Davisthe return value must be noted,
1348269e767SBrooks Davisand the remainder of the operation should be retried when possible.
1358269e767SBrooks Davis.Sh RETURN VALUES
1368269e767SBrooks DavisUpon successful completion the number of bytes which were written
1378269e767SBrooks Davisis returned.
1388269e767SBrooks DavisOtherwise a -1 is returned and the global variable
1398269e767SBrooks Davis.Va errno
1408269e767SBrooks Davisis set to indicate the error.
1418269e767SBrooks Davis.Sh ERRORS
1428269e767SBrooks DavisThe
1438269e767SBrooks Davis.Fn write ,
1448269e767SBrooks Davis.Fn writev ,
1458269e767SBrooks Davis.Fn pwrite
1468269e767SBrooks Davisand
1478269e767SBrooks Davis.Fn pwritev
1488269e767SBrooks Davissystem calls
1498269e767SBrooks Daviswill fail and the file pointer will remain unchanged if:
1508269e767SBrooks Davis.Bl -tag -width Er
1518269e767SBrooks Davis.It Bq Er EBADF
1528269e767SBrooks DavisThe
1538269e767SBrooks Davis.Fa fd
1548269e767SBrooks Davisargument
1558269e767SBrooks Davisis not a valid descriptor open for writing.
1568269e767SBrooks Davis.It Bq Er EPIPE
1578269e767SBrooks DavisAn attempt is made to write to a pipe that is not open
1588269e767SBrooks Davisfor reading by any process.
1598269e767SBrooks Davis.It Bq Er EPIPE
1608269e767SBrooks DavisAn attempt is made to write to a socket of type
1618269e767SBrooks Davis.Dv SOCK_STREAM
1628269e767SBrooks Davisthat is not connected to a peer socket.
1638269e767SBrooks Davis.It Bq Er EFBIG
1648269e767SBrooks DavisAn attempt was made to write a file that exceeds the process's
1658269e767SBrooks Davisfile size limit or the maximum file size.
1668269e767SBrooks Davis.It Bq Er EFAULT
1678269e767SBrooks DavisPart of
1688269e767SBrooks Davis.Fa iov
1698269e767SBrooks Davisor data to be written to the file
1708269e767SBrooks Davispoints outside the process's allocated address space.
1718269e767SBrooks Davis.It Bq Er EINVAL
1728269e767SBrooks DavisThe pointer associated with
1738269e767SBrooks Davis.Fa fd
1748269e767SBrooks Daviswas negative.
1758269e767SBrooks Davis.It Bq Er ENOSPC
1768269e767SBrooks DavisThere is no free space remaining on the file system
1778269e767SBrooks Daviscontaining the file.
1788269e767SBrooks Davis.It Bq Er EDQUOT
1798269e767SBrooks DavisThe user's quota of disk blocks on the file system
1808269e767SBrooks Daviscontaining the file has been exhausted.
1818269e767SBrooks Davis.It Bq Er EIO
1828269e767SBrooks DavisAn I/O error occurred while reading from or writing to the file system.
1838269e767SBrooks Davis.It Bq Er EINTR
1848269e767SBrooks DavisA signal interrupted the write before it could be completed.
1858269e767SBrooks Davis.It Bq Er EAGAIN
1868269e767SBrooks DavisThe file was marked for non-blocking I/O,
1878269e767SBrooks Davisand no data could be written immediately.
1888269e767SBrooks Davis.It Bq Er EROFS
1898269e767SBrooks DavisAn attempt was made to write over a disk label area at the beginning
1908269e767SBrooks Davisof a slice.
1918269e767SBrooks DavisUse
1928269e767SBrooks Davis.Xr disklabel 8
1938269e767SBrooks Davis.Fl W
1948269e767SBrooks Davisto enable writing on the disk label area.
1958269e767SBrooks Davis.It Bq Er EINVAL
1968269e767SBrooks DavisThe value
1978269e767SBrooks Davis.Fa nbytes
1988269e767SBrooks Davisis greater than
1998269e767SBrooks Davis.Dv SSIZE_MAX
2008269e767SBrooks Davis(or greater than
2018269e767SBrooks Davis.Dv INT_MAX ,
2028269e767SBrooks Davisif the sysctl
2038269e767SBrooks Davis.Va debug.iosize_max_clamp
2048269e767SBrooks Davisis non-zero).
2058269e767SBrooks Davis.It Bq Er EINTEGRITY
2068269e767SBrooks DavisThe backing store for
2078269e767SBrooks Davis.Fa fd
2088269e767SBrooks Davisdetected corrupted data while reading.
2098269e767SBrooks Davis(For example, writing a partial filesystem block may require first reading
2108269e767SBrooks Davisthe existing block which may trigger this error.)
2118269e767SBrooks Davis.El
2128269e767SBrooks Davis.Pp
2138269e767SBrooks DavisIn addition,
2148269e767SBrooks Davis.Fn writev
2158269e767SBrooks Davisand
2168269e767SBrooks Davis.Fn pwritev
2178269e767SBrooks Davismay return one of the following errors:
2188269e767SBrooks Davis.Bl -tag -width Er
2198269e767SBrooks Davis.It Bq Er EDESTADDRREQ
2208269e767SBrooks DavisThe destination is no longer available when writing to a
2218269e767SBrooks Davis.Ux
2228269e767SBrooks Davisdomain datagram socket on which
2238269e767SBrooks Davis.Xr connect 2
2248269e767SBrooks Davishad been used to set a destination address.
2258269e767SBrooks Davis.It Bq Er EINVAL
2268269e767SBrooks DavisThe
2278269e767SBrooks Davis.Fa iovcnt
2288269e767SBrooks Davisargument
2298269e767SBrooks Daviswas less than or equal to 0, or greater than
2308269e767SBrooks Davis.Dv IOV_MAX .
2318269e767SBrooks Davis.It Bq Er EINVAL
2328269e767SBrooks DavisOne of the
2338269e767SBrooks Davis.Fa iov_len
2348269e767SBrooks Davisvalues in the
2358269e767SBrooks Davis.Fa iov
2368269e767SBrooks Davisarray was negative.
2378269e767SBrooks Davis.It Bq Er EINVAL
2388269e767SBrooks DavisThe sum of the
2398269e767SBrooks Davis.Fa iov_len
2408269e767SBrooks Davisvalues is greater than
2418269e767SBrooks Davis.Dv SSIZE_MAX
2428269e767SBrooks Davis(or greater than
2438269e767SBrooks Davis.Dv INT_MAX ,
2448269e767SBrooks Davisif the sysctl
2458269e767SBrooks Davis.Va debug.iosize_max_clamp
2468269e767SBrooks Davisis non-zero).
2478269e767SBrooks Davis.It Bq Er ENOBUFS
2488269e767SBrooks DavisThe mbuf pool has been completely exhausted when writing to a socket.
2498269e767SBrooks Davis.El
2508269e767SBrooks Davis.Pp
2518269e767SBrooks DavisThe
2528269e767SBrooks Davis.Fn pwrite
2538269e767SBrooks Davisand
2548269e767SBrooks Davis.Fn pwritev
2558269e767SBrooks Davissystem calls may also return the following errors:
2568269e767SBrooks Davis.Bl -tag -width Er
2578269e767SBrooks Davis.It Bq Er EINVAL
2588269e767SBrooks DavisThe
2598269e767SBrooks Davis.Fa offset
2608269e767SBrooks Davisvalue was negative.
2618269e767SBrooks Davis.It Bq Er ESPIPE
2628269e767SBrooks DavisThe file descriptor is associated with a pipe, socket, or FIFO.
2638269e767SBrooks Davis.El
2648269e767SBrooks Davis.Sh SEE ALSO
2658269e767SBrooks Davis.Xr fcntl 2 ,
2668269e767SBrooks Davis.Xr lseek 2 ,
2678269e767SBrooks Davis.Xr open 2 ,
2688269e767SBrooks Davis.Xr pipe 2 ,
2698269e767SBrooks Davis.Xr select 2
2708269e767SBrooks Davis.Sh STANDARDS
2718269e767SBrooks DavisThe
2728269e767SBrooks Davis.Fn write
2738269e767SBrooks Davissystem call is expected to conform to
2748269e767SBrooks Davis.St -p1003.1-90 .
2758269e767SBrooks DavisThe
2768269e767SBrooks Davis.Fn writev
2778269e767SBrooks Davisand
2788269e767SBrooks Davis.Fn pwrite
2798269e767SBrooks Davissystem calls are expected to conform to
2808269e767SBrooks Davis.St -xpg4.2 .
2818269e767SBrooks Davis.Sh HISTORY
2828269e767SBrooks DavisThe
2838269e767SBrooks Davis.Fn pwritev
2848269e767SBrooks Davissystem call appeared in
2858269e767SBrooks Davis.Fx 6.0 .
2868269e767SBrooks DavisThe
2878269e767SBrooks Davis.Fn pwrite
2888269e767SBrooks Davisfunction appeared in
2898269e767SBrooks Davis.At V.4 .
2908269e767SBrooks DavisThe
2918269e767SBrooks Davis.Fn writev
2928269e767SBrooks Davissystem call appeared in
2938269e767SBrooks Davis.Bx 4.2 .
2948269e767SBrooks DavisThe
2958269e767SBrooks Davis.Fn write
2968269e767SBrooks Davisfunction appeared in
2978269e767SBrooks Davis.At v1 .
2988269e767SBrooks Davis.Sh BUGS
2998269e767SBrooks DavisThe
3008269e767SBrooks Davis.Fn pwrite
3018269e767SBrooks Davissystem call appends the file without changing the file offset if
3028269e767SBrooks Davis.Dv O_APPEND
3038269e767SBrooks Davisis set, contrary to
3048269e767SBrooks Davis.St -p1003.1-2008
3058269e767SBrooks Daviswhere
3068269e767SBrooks Davis.Fn pwrite
3078269e767SBrooks Daviswrites into
3088269e767SBrooks Davis.Fa offset
3098269e767SBrooks Davisregardless of whether
3108269e767SBrooks Davis.Dv O_APPEND
3118269e767SBrooks Davisis set.
312