OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man2 / recvmmsg.2
1 .\" Copyright (C) 2011 by Andi Kleen <andi@firstfloor.org>
2 .\" and Copyright (c) 2011 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Syscall added in following commit
27 .\"     commit a2e2725541fad72416326798c2d7fa4dafb7d337
28 .\"     Author: Arnaldo Carvalho de Melo <acme@redhat.com>
29 .\"     Date:   Mon Oct 12 23:40:10 2009 -0700
30 .\"
31 .TH RECVMMSG 2 2014-06-13 "Linux" "Linux Programmer's Manual"
32 .SH NAME
33 recvmmsg \- receive multiple messages on a socket
34 .SH SYNOPSIS
35 .nf
36 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
37 .BI "#include <sys/socket.h>"
38
39 .BI "int recvmmsg(int " sockfd ", struct mmsghdr *" msgvec \
40 ", unsigned int " vlen ","
41 .br
42 .BI "             unsigned int " flags ", struct timespec *" timeout ");"
43 .fi
44 .SH DESCRIPTION
45 The
46 .BR recvmmsg ()
47 system call is an extension of
48 .BR recvmsg (2)
49 that allows the caller to receive multiple messages from a socket
50 using a single system call.
51 (This has performance benefits for some applications.)
52 A further extension over
53 .BR recvmsg (2)
54 is support for a timeout on the receive operation.
55
56 The
57 .I sockfd
58 argument is the file descriptor of the socket to receive data from.
59
60 The
61 .I msgvec
62 argument is a pointer to an array of
63 .I mmsghdr
64 structures.
65 The size of this array is specified in
66 .IR vlen .
67
68 The
69 .I mmsghdr
70 structure is defined in
71 .I <sys/socket.h>
72 as:
73
74 .in +4n
75 .nf
76 struct mmsghdr {
77     struct msghdr msg_hdr;  /* Message header */
78     unsigned int  msg_len;  /* Number of received bytes for header */
79 };
80 .fi
81 .in
82 .PP
83 The
84 .I msg_hdr
85 field is a
86 .I msghdr
87 structure, as described in
88 .BR recvmsg (2).
89 The
90 .I msg_len
91 field is the number of bytes returned for the message in the entry.
92 This field has the same value as the return value of a single
93 .BR recvmsg (2)
94 on the header.
95
96 The
97 .I flags
98 argument contains flags ORed together.
99 The flags are the same as documented for
100 .BR recvmsg (2),
101 with the following addition:
102 .TP
103 .BR MSG_WAITFORONE " (since Linux 2.6.34)"
104 Turns on
105 .B MSG_DONTWAIT
106 after the first message has been received.
107 .PP
108 The
109 .I timeout
110 argument points to a
111 .I struct timespec
112 (see
113 .BR clock_gettime (2))
114 defining a timeout (seconds plus nanoseconds) for the receive operation
115 .RI ( "but see BUGS!" ).
116 (This interval will be rounded up to the system clock granularity,
117 and kernel scheduling delays mean that the blocking interval
118 may overrun by a small amount.)
119 If
120 .I timeout
121 is NULL, then the operation blocks indefinitely.
122
123 A blocking
124 .BR recvmmsg ()
125 call blocks until
126 .I vlen
127 messages have been received
128 or until the timeout expires.
129 A nonblocking call reads as many messages as are available
130 (up to the limit specified by
131 .IR vlen )
132 and returns immediately.
133
134 On return from
135 .BR recvmmsg (),
136 successive elements of
137 .IR msgvec
138 are updated to contain information about each received message:
139 .I msg_len
140 contains the size of the received message;
141 the subfields of
142 .I msg_hdr
143 are updated as described in
144 .BR recvmsg (2).
145 The return value of the call indicates the number of elements of
146 .I msgvec
147 that have been updated.
148 .SH RETURN VALUE
149 On success,
150 .BR recvmmsg ()
151 returns the number of messages received in
152 .IR msgvec ;
153 on error, \-1 is returned, and
154 .I errno
155 is set to indicate the error.
156 .SH ERRORS
157 Errors are as for
158 .BR recvmsg (2).
159 In addition, the following error can occur:
160 .TP
161 .B EINVAL
162 .I timeout
163 is invalid.
164 .SH VERSIONS
165 The
166 .BR recvmmsg ()
167 system call was added in Linux 2.6.33.
168 Support in glibc was added in version 2.12.
169 .SH CONFORMING TO
170 .BR recvmmsg ()
171 is Linux-specific.
172 .SH EXAMPLE
173 .PP
174 The following program uses
175 .BR recvmmsg ()
176 to receive multiple messages on a socket and stores
177 them in multiple buffers.
178 The call returns if all buffers are filled or if the
179 timeout specified has expired.
180
181 The following snippet periodically generates UDP datagrams
182 containing a random number:
183 .in +4n
184 .nf
185
186 .RB "$" " while true; do echo $RANDOM > /dev/udp/127.0.0.1/1234; "
187 .B      "      sleep 0.25; done"
188 .fi
189 .in
190
191 These datagrams are read by the example application, which
192 can give the following output:
193 .in +4n
194 .nf
195
196 .RB "$" " ./a.out"
197 5 messages received
198 1 11782
199 2 11345
200 3 304
201 4 13514
202 5 28421
203 .fi
204 .in
205 .SS Program source
206 \&
207 .nf
208 #define _GNU_SOURCE
209 #include <netinet/ip.h>
210 #include <stdio.h>
211 #include <stdlib.h>
212 #include <string.h>
213 #include <sys/socket.h>
214
215 int
216 main(void)
217 {
218 #define VLEN 10
219 #define BUFSIZE 200
220 #define TIMEOUT 1
221     int sockfd, retval, i;
222     struct sockaddr_in sa;
223     struct mmsghdr msgs[VLEN];
224     struct iovec iovecs[VLEN];
225     char bufs[VLEN][BUFSIZE+1];
226     struct timespec timeout;
227
228     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
229     if (sockfd == \-1) {
230         perror("socket()");
231         exit(EXIT_FAILURE);
232     }
233
234     sa.sin_family = AF_INET;
235     sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
236     sa.sin_port = htons(1234);
237     if (bind(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == \-1) {
238         perror("bind()");
239         exit(EXIT_FAILURE);
240     }
241
242     memset(msgs, 0, sizeof(msgs));
243     for (i = 0; i < VLEN; i++) {
244         iovecs[i].iov_base         = bufs[i];
245         iovecs[i].iov_len          = BUFSIZE;
246         msgs[i].msg_hdr.msg_iov    = &iovecs[i];
247         msgs[i].msg_hdr.msg_iovlen = 1;
248     }
249
250     timeout.tv_sec = TIMEOUT;
251     timeout.tv_nsec = 0;
252
253     retval = recvmmsg(sockfd, msgs, VLEN, 0, &timeout);
254     if (retval == \-1) {
255         perror("recvmmsg()");
256         exit(EXIT_FAILURE);
257     }
258
259     printf("%d messages received\\n", retval);
260     for (i = 0; i < retval; i++) {
261         bufs[i][msgs[i].msg_len] = 0;
262         printf("%d %s", i+1, bufs[i]);
263     }
264     exit(EXIT_SUCCESS);
265 }
266 .fi
267 .SH BUGS
268 The
269 .I timeout
270 argument does not work as intended.
271 .\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=75371
272 .\" http://thread.gmane.org/gmane.linux.man/5677
273 The timeout is checked only after the receipt of each datagram,
274 so that if up to
275 .I vlen\-1
276 datagrams are received before the timeout expires,
277 but then no further datagrams are received, the call will block forever.
278 .SH SEE ALSO
279 .BR clock_gettime (2),
280 .BR recvmsg (2),
281 .BR sendmmsg (2),
282 .BR sendmsg (2),
283 .BR socket (2),
284 .BR socket (7)
285 .SH COLOPHON
286 This page is part of release 3.79 of the Linux
287 .I man-pages
288 project.
289 A description of the project,
290 information about reporting bugs,
291 and the latest version of this page,
292 can be found at
293 \%http://www.kernel.org/doc/man\-pages/.