]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/send.2
Merge compiler-rt trunk r366426, resolve conflicts, and add
[FreeBSD/FreeBSD.git] / lib / libc / sys / send.2
1 .\" Copyright (c) 1983, 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 .\"     From: @(#)send.2        8.2 (Berkeley) 2/21/94
29 .\" $FreeBSD$
30 .\"
31 .Dd January 4, 2019
32 .Dt SEND 2
33 .Os
34 .Sh NAME
35 .Nm send ,
36 .Nm sendto ,
37 .Nm sendmsg ,
38 .Nm sendmmsg
39 .Nd send message(s) from a socket
40 .Sh LIBRARY
41 .Lb libc
42 .Sh SYNOPSIS
43 .In sys/socket.h
44 .Ft ssize_t
45 .Fn send "int s" "const void *msg" "size_t len" "int flags"
46 .Ft ssize_t
47 .Fn sendto "int s" "const void *msg" "size_t len" "int flags" "const struct sockaddr *to" "socklen_t tolen"
48 .Ft ssize_t
49 .Fn sendmsg "int s" "const struct msghdr *msg" "int flags"
50 .Ft ssize_t
51 .Fn sendmmsg "int s" "struct mmsghdr * restrict msgvec" "size_t vlen" "int flags"
52 .Sh DESCRIPTION
53 The
54 .Fn send
55 and
56 .Fn sendmmsg
57 functions,
58 and
59 .Fn sendto
60 and
61 .Fn sendmsg
62 system calls
63 are used to transmit one or more messages (with the
64 .Fn sendmmsg
65 call) to
66 another socket.
67 The
68 .Fn send
69 function
70 may be used only when the socket is in a
71 .Em connected
72 state, while
73 .Fn sendto ,
74 .Fn sendmsg
75 and
76 .Fn sendmmsg
77 may be used at any time.
78 .Pp
79 The address of the target is given by
80 .Fa to
81 with
82 .Fa tolen
83 specifying its size.
84 The length of the message is given by
85 .Fa len .
86 If the message is too long to pass atomically through the
87 underlying protocol, the error
88 .Er EMSGSIZE
89 is returned, and
90 the message is not transmitted.
91 .Pp
92 The
93 .Fn sendmmsg
94 function sends multiple messages at a call.
95 They are given by the
96 .Fa msgvec
97 vector along with
98 .Fa vlen
99 specifying the vector size.
100 The number of octets sent per each message is placed in the
101 .Fa msg_len
102 field of each processed element of the vector after transmission.
103 .Pp
104 No indication of failure to deliver is implicit in a
105 .Fn send .
106 Locally detected errors are indicated by a return value of -1.
107 .Pp
108 If no messages space is available at the socket to hold
109 the message to be transmitted, then
110 .Fn send
111 normally blocks, unless the socket has been placed in
112 non-blocking I/O mode.
113 The
114 .Xr select 2
115 system call may be used to determine when it is possible to
116 send more data.
117 .Pp
118 The
119 .Fa flags
120 argument may include one or more of the following:
121 .Bd -literal
122 #define MSG_OOB         0x00001 /* process out-of-band data */
123 #define MSG_DONTROUTE   0x00004 /* bypass routing, use direct interface */
124 #define MSG_EOR         0x00008 /* data completes record */
125 #define MSG_DONTWAIT    0x00080 /* do not block */
126 #define MSG_EOF         0x00100 /* data completes transaction */
127 #define MSG_NOSIGNAL    0x20000 /* do not generate SIGPIPE on EOF */
128 .Ed
129 .Pp
130 The flag
131 .Dv MSG_OOB
132 is used to send
133 .Dq out-of-band
134 data on sockets that support this notion (e.g.\&
135 .Dv SOCK_STREAM ) ;
136 the underlying protocol must also support
137 .Dq out-of-band
138 data.
139 .Dv MSG_EOR
140 is used to indicate a record mark for protocols which support the
141 concept.
142 The
143 .Dv MSG_DONTWAIT
144 flag request the call to return when it would block otherwise.
145 .Dv MSG_EOF
146 requests that the sender side of a socket be shut down, and that an
147 appropriate indication be sent at the end of the specified data;
148 this flag is only implemented for
149 .Dv SOCK_STREAM
150 sockets in the
151 .Dv PF_INET
152 protocol family.
153 .Dv MSG_DONTROUTE
154 is usually used only by diagnostic or routing programs.
155 .Dv MSG_NOSIGNAL
156 is used to prevent
157 .Dv SIGPIPE
158 generation when writing a socket that
159 may be closed.
160 .Pp
161 See
162 .Xr recv 2
163 for a description of the
164 .Fa msghdr
165 structure and the
166 .Fa mmsghdr
167 structure.
168 .Sh RETURN VALUES
169 The
170 .Fn send ,
171 .Fn sendto
172 and
173 .Fn sendmsg
174 calls
175 return the number of octets sent.
176 The
177 .Fn sendmmsg
178 call returns the number of messages sent.
179 If an error occurred a value of -1 is returned.
180 .Sh ERRORS
181 The
182 .Fn send
183 and
184 .Fn sendmmsg
185 functions and
186 .Fn sendto
187 and
188 .Fn sendmsg
189 system calls
190 fail if:
191 .Bl -tag -width Er
192 .It Bq Er EBADF
193 An invalid descriptor was specified.
194 .It Bq Er EACCES
195 The destination address is a broadcast address, and
196 .Dv SO_BROADCAST
197 has not been set on the socket.
198 .It Bq Er ENOTSOCK
199 The argument
200 .Fa s
201 is not a socket.
202 .It Bq Er EFAULT
203 An invalid user space address was specified for an argument.
204 .It Bq Er EMSGSIZE
205 The socket requires that message be sent atomically,
206 and the size of the message to be sent made this impossible.
207 .It Bq Er EAGAIN
208 The socket is marked non-blocking, or
209 .Dv MSG_DONTWAIT
210 is specified, and the requested operation would block.
211 .It Bq Er ENOBUFS
212 The system was unable to allocate an internal buffer.
213 The operation may succeed when buffers become available.
214 .It Bq Er ENOBUFS
215 The output queue for a network interface was full.
216 This generally indicates that the interface has stopped sending,
217 but may be caused by transient congestion.
218 .It Bq Er EHOSTUNREACH
219 The remote host was unreachable.
220 .It Bq Er EISCONN
221 A destination address was specified and the socket is already connected.
222 .It Bq Er ECONNREFUSED
223 The socket received an ICMP destination unreachable message
224 from the last message sent.
225 This typically means that the
226 receiver is not listening on the remote port.
227 .It Bq Er EHOSTDOWN
228 The remote host was down.
229 .It Bq Er ENETDOWN
230 The remote network was down.
231 .It Bq Er EADDRNOTAVAIL
232 The process using a
233 .Dv SOCK_RAW
234 socket was jailed and the source
235 address specified in the IP header did not match the IP
236 address bound to the prison.
237 .It Bq Er EPIPE
238 The socket is unable to send anymore data
239 .Dv ( SBS_CANTSENDMORE
240 has been set on the socket).
241 This typically means that the socket
242 is not connected.
243 .El
244 .Sh SEE ALSO
245 .Xr fcntl 2 ,
246 .Xr getsockopt 2 ,
247 .Xr recv 2 ,
248 .Xr select 2 ,
249 .Xr socket 2 ,
250 .Xr write 2 ,
251 .Xr CMSG_DATA 3
252 .Sh HISTORY
253 The
254 .Fn send
255 function appeared in
256 .Bx 4.2 .
257 The
258 .Fn sendmmsg
259 function appeared in
260 .Fx 11.0 .
261 .Sh BUGS
262 Because
263 .Fn sendmsg
264 does not necessarily block until the data has been transferred, it
265 is possible to transfer an open file descriptor across an
266 .Dv AF_UNIX
267 domain socket
268 (see
269 .Xr recv 2 ) ,
270 then
271 .Fn close
272 it before it has actually been sent, the result being that the receiver
273 gets a closed file descriptor.
274 It is left to the application to
275 implement an acknowledgment mechanism to prevent this from happening.