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