]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/recv.2
This commit was generated by cvs2svn to compensate for changes in r153758,
[FreeBSD/FreeBSD.git] / lib / libc / sys / recv.2
1 .\" Copyright (c) 1983, 1990, 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 .\"     @(#)recv.2      8.3 (Berkeley) 2/21/94
33 .\" $FreeBSD$
34 .\"
35 .Dd July 29, 2005
36 .Dt RECV 2
37 .Os
38 .Sh NAME
39 .Nm recv ,
40 .Nm recvfrom ,
41 .Nm recvmsg
42 .Nd receive a message from a socket
43 .Sh LIBRARY
44 .Lb libc
45 .Sh SYNOPSIS
46 .In sys/types.h
47 .In sys/socket.h
48 .Ft ssize_t
49 .Fn recv "int s" "void *buf" "size_t len" "int flags"
50 .Ft ssize_t
51 .Fn recvfrom "int s" "void * restrict buf" "size_t len" "int flags" "struct sockaddr * restrict from" "socklen_t * restrict fromlen"
52 .Ft ssize_t
53 .Fn recvmsg "int s" "struct msghdr *msg" "int flags"
54 .Sh DESCRIPTION
55 The
56 .Fn recvfrom
57 and
58 .Fn recvmsg
59 system calls
60 are used to receive messages from a socket,
61 and may be used to receive data on a socket whether or not
62 it is connection-oriented.
63 .Pp
64 If
65 .Fa from
66 is not a null pointer
67 and the socket is not connection-oriented,
68 the source address of the message is filled in.
69 The
70 .Fa fromlen
71 argument
72 is a value-result argument, initialized to the size of
73 the buffer associated with
74 .Fa from ,
75 and modified on return to indicate the actual size of the
76 address stored there.
77 .Pp
78 The
79 .Fn recv
80 function is normally used only on a
81 .Em connected
82 socket (see
83 .Xr connect 2 )
84 and is identical to
85 .Fn recvfrom
86 with a
87 null pointer passed as its
88 .Fa from
89 argument.
90 As it is redundant, it may not be supported in future releases.
91 .Pp
92 All three routines return the length of the message on successful
93 completion.
94 If a message is too long to fit in the supplied buffer,
95 excess bytes may be discarded depending on the type of socket
96 the message is received from (see
97 .Xr socket 2 ) .
98 .Pp
99 If no messages are available at the socket, the
100 receive call waits for a message to arrive, unless
101 the socket is nonblocking (see
102 .Xr fcntl 2 )
103 in which case the value
104 -1 is returned and the external variable
105 .Va errno
106 set to
107 .Er EAGAIN .
108 The receive calls normally return any data available,
109 up to the requested amount,
110 rather than waiting for receipt of the full amount requested;
111 this behavior is affected by the socket-level options
112 .Dv SO_RCVLOWAT
113 and
114 .Dv SO_RCVTIMEO
115 described in
116 .Xr getsockopt 2 .
117 .Pp
118 The
119 .Xr select 2
120 system call may be used to determine when more data arrive.
121 .Pp
122 The
123 .Fa flags
124 argument to a
125 .Fn recv
126 function is formed by
127 .Em or Ap ing
128 one or more of the values:
129 .Bl -column ".Dv MSG_DONTWAIT" -offset indent
130 .It Dv MSG_OOB Ta process out-of-band data
131 .It Dv MSG_PEEK Ta peek at incoming message
132 .It Dv MSG_WAITALL Ta wait for full request or error
133 .It Dv MSG_DONTWAIT Ta do not block
134 .El
135 .Pp
136 The
137 .Dv MSG_OOB
138 flag requests receipt of out-of-band data
139 that would not be received in the normal data stream.
140 Some protocols place expedited data at the head of the normal
141 data queue, and thus this flag cannot be used with such protocols.
142 The
143 .Dv MSG_PEEK
144 flag causes the receive operation to return data
145 from the beginning of the receive queue without removing that
146 data from the queue.
147 Thus, a subsequent receive call will return the same data.
148 The
149 .Dv MSG_WAITALL
150 flag requests that the operation block until
151 the full request is satisfied.
152 However, the call may still return less data than requested
153 if a signal is caught, an error or disconnect occurs,
154 or the next data to be received is of a different type than that returned.
155 The
156 .Dv MSG_DONTWAIT
157 flag requests the call to return when it would block otherwise.
158 If no data is available,
159 .Va errno
160 is set to
161 .Er EAGAIN .
162 This flag is not available in strict
163 .Tn ANSI
164 or C99 compilation mode.
165 .Pp
166 The
167 .Fn recvmsg
168 system call uses a
169 .Fa msghdr
170 structure to minimize the number of directly supplied arguments.
171 This structure has the following form, as defined in
172 .In sys/socket.h :
173 .Pp
174 .Bd -literal
175 struct msghdr {
176         caddr_t msg_name;       /* optional address */
177         u_int   msg_namelen;    /* size of address */
178         struct  iovec *msg_iov; /* scatter/gather array */
179         u_int   msg_iovlen;     /* # elements in msg_iov */
180         caddr_t msg_control;    /* ancillary data, see below */
181         u_int   msg_controllen; /* ancillary data buffer len */
182         int     msg_flags;      /* flags on received message */
183 };
184 .Ed
185 .Pp
186 Here
187 .Fa msg_name
188 and
189 .Fa msg_namelen
190 specify the destination address if the socket is unconnected;
191 .Fa msg_name
192 may be given as a null pointer if no names are desired or required.
193 The
194 .Fa msg_iov
195 and
196 .Fa msg_iovlen
197 arguments
198 describe scatter gather locations, as discussed in
199 .Xr read 2 .
200 The
201 .Fa msg_control
202 argument,
203 which has length
204 .Fa msg_controllen ,
205 points to a buffer for other protocol control related messages
206 or other miscellaneous ancillary data.
207 The messages are of the form:
208 .Bd -literal
209 struct cmsghdr {
210         u_int   cmsg_len;       /* data byte count, including hdr */
211         int     cmsg_level;     /* originating protocol */
212         int     cmsg_type;      /* protocol-specific type */
213 /* followed by
214         u_char  cmsg_data[]; */
215 };
216 .Ed
217 .Pp
218 As an example, one could use this to learn of changes in the data-stream
219 in XNS/SPP, or in ISO, to obtain user-connection-request data by requesting
220 a
221 .Fn recvmsg
222 with no data buffer provided immediately after an
223 .Fn accept
224 system call.
225 .Pp
226 Open file descriptors are now passed as ancillary data for
227 .Dv AF_UNIX
228 domain sockets, with
229 .Fa cmsg_level
230 set to
231 .Dv SOL_SOCKET
232 and
233 .Fa cmsg_type
234 set to
235 .Dv SCM_RIGHTS .
236 .Pp
237 Process credentials can also be passed as ancillary data for
238 .Dv AF_UNIX
239 domain sockets using a
240 .Fa cmsg_type
241 of
242 .Dv SCM_CREDS .
243 In this case,
244 .Fa cmsg_data
245 should be a structure of type
246 .Fa cmsgcred ,
247 which is defined in
248 .In sys/socket.h
249 as follows:
250 .Pp
251 .Bd -literal
252 struct cmsgcred {
253         pid_t   cmcred_pid;             /* PID of sending process */
254         uid_t   cmcred_uid;             /* real UID of sending process */
255         uid_t   cmcred_euid;            /* effective UID of sending process */
256         gid_t   cmcred_gid;             /* real GID of sending process */
257         short   cmcred_ngroups;         /* number or groups */
258         gid_t   cmcred_groups[CMGROUP_MAX];     /* groups */
259 };
260 .Ed
261 .Pp
262 The kernel will fill in the credential information of the sending process
263 and deliver it to the receiver.
264 .Pp
265 The
266 .Fa msg_flags
267 field is set on return according to the message received.
268 .Dv MSG_EOR
269 indicates end-of-record;
270 the data returned completed a record (generally used with sockets of type
271 .Dv SOCK_SEQPACKET ) .
272 .Dv MSG_TRUNC
273 indicates that
274 the trailing portion of a datagram was discarded because the datagram
275 was larger than the buffer supplied.
276 .Dv MSG_CTRUNC
277 indicates that some
278 control data were discarded due to lack of space in the buffer
279 for ancillary data.
280 .Dv MSG_OOB
281 is returned to indicate that expedited or out-of-band data were received.
282 .Sh RETURN VALUES
283 These calls return the number of bytes received, or -1
284 if an error occurred.
285 .Sh ERRORS
286 The calls fail if:
287 .Bl -tag -width Er
288 .It Bq Er EBADF
289 The argument
290 .Fa s
291 is an invalid descriptor.
292 .It Bq Er ECONNRESET
293 The remote socket end is forcibly closed.
294 .It Bq Er ENOTCONN
295 The socket is associated with a connection-oriented protocol
296 and has not been connected (see
297 .Xr connect 2
298 and
299 .Xr accept 2 ) .
300 .It Bq Er ENOTSOCK
301 The argument
302 .Fa s
303 does not refer to a socket.
304 .It Bq Er EMSGSIZE
305 The
306 .Fn recvmsg
307 system call
308 was used to receive rights (file descriptors) that were in flight on the
309 connection.
310 However, the receiving program did not have enough free file
311 descriptor slots to accept the them.
312 In this case the descriptors are
313 closed, any pending data can be returned by another call to
314 .Fn recvmsg .
315 .It Bq Er EAGAIN
316 The socket is marked non-blocking, and the receive operation
317 would block, or
318 a receive timeout had been set,
319 and the timeout expired before data were received.
320 .It Bq Er EINTR
321 The receive was interrupted by delivery of a signal before
322 any data were available.
323 .It Bq Er EFAULT
324 The receive buffer pointer(s) point outside the process's
325 address space.
326 .El
327 .Sh SEE ALSO
328 .Xr fcntl 2 ,
329 .Xr getsockopt 2 ,
330 .Xr read 2 ,
331 .Xr select 2 ,
332 .Xr socket 2
333 .Sh HISTORY
334 The
335 .Fn recv
336 function appeared in
337 .Bx 4.2 .