OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / man2 / sendmmsg.2
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" with some material from a draft by
3 .\" Stephan Mueller <stephan.mueller@atsec.com>
4 .\" in turn based on Andi Kleen's recvmmsg.2 page.
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein.  The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
27 .\"
28 .TH SENDMMSG 2 2012-12-16 "Linux" "Linux Programmer's Manual"
29 .SH NAME
30 sendmmsg \- send multiple messages on a socket
31 .SH SYNOPSIS
32 .nf
33 .B "#define _GNU_SOURCE"
34 .BI "#include <sys/socket.h>"
35
36 .BI "int sendmmsg(int " sockfd ", struct mmsghdr *" msgvec \
37 ", unsigned int " vlen ","
38 .BI "             unsigned int " flags ");"
39 .fi
40 .SH DESCRIPTION
41 The
42 .BR sendmmsg ()
43 system call is an extension of
44 .BR sendmsg (2)
45 that allows the caller to transmit multiple messages on a socket
46 using a single system call.
47 (This has performance benefits for some applications.)
48 .\" See commit 228e548e602061b08ee8e8966f567c12aa079682
49
50 The
51 .I sockfd
52 argument is the file descriptor of the socket
53 on which data is to be transmitted.
54
55 The
56 .I msgvec
57 argument is a pointer to an array of
58 .I mmsghdr
59 structures.
60 The size of this array is specified in
61 .IR vlen .
62
63 The
64 .I mmsghdr
65 structure is defined in
66 .I <sys/socket.h>
67 as:
68
69 .in +4n
70 .nf
71 struct mmsghdr {
72     struct msghdr msg_hdr;  /* Message header */
73     unsigned int  msg_len;  /* Number of bytes transmitted */
74 };
75 .fi
76 .in
77 .PP
78 The
79 .I msg_hdr
80 field is a
81 .I msghdr
82 structure, as described in
83 .BR sendmsg (2).
84 The
85 .I msg_len
86 field is used to return the number of bytes sent from the message in
87 .IR msg_hdr
88 (i.e., the same as the return value from a single
89 .BR sendmsg (2)
90 call).
91
92 The
93 .I flags
94 argument contains flags ORed together.
95 The flags are the same as for
96 .BR sendmsg (2).
97
98 A blocking
99 .BR sendmmsg ()
100 call blocks until
101 .I vlen
102 messages have been sent.
103 A nonblocking call sends as many messages as possible
104 (up to the limit specified by
105 .IR vlen )
106 and returns immediately.
107
108 On return from
109 .BR sendmmsg (),
110 the
111 .I msg_len
112 fields of successive elements of
113 .IR msgvec
114 are updated to contain the number of bytes transmitted from the corresponding
115 .IR msg_hdr .
116 The return value of the call indicates the number of elements of
117 .I msgvec
118 that have been updated.
119 .SH RETURN VALUE
120 On success,
121 .BR sendmmsg ()
122 returns the number of messages sent from
123 .IR msgvec ;
124 if this is less than
125 .IR vlen ,
126 the caller can retry with a further
127 .BR sendmmsg ()
128 call to send the remaining messages.
129
130 On error, \-1 is returned, and
131 .I errno
132 is set to indicate the error.
133 .SH ERRORS
134 Errors are as for
135 .BR sendmsg (2).
136 An error is returned only if no datagrams could be sent.
137 .\" commit 728ffb86f10873aaf4abd26dde691ee40ae731fe
138 .\"     ... only return an error if no datagrams could be sent.
139 .\"     If less than the requested number of messages were sent, the application
140 .\"     must retry starting at the first failed one and if the problem is
141 .\"     persistent the error will be returned.
142 .\"
143 .\"     This matches the behaviour of other syscalls like read/write - it
144 .\"     is not an error if less than the requested number of elements are sent.
145 .SH VERSIONS
146 The
147 .BR sendmmsg ()
148 system call was added in Linux 3.0.
149 Support in glibc was added in version 2.14.
150 .SH CONFORMING TO
151 .BR sendmmsg ()
152 is Linux-specific.
153 .SH NOTES
154 The value specified in
155 .I vlen
156 is capped to
157 .B UIO_MAXIOV
158 (1024).
159 .\" commit 98382f419f32d2c12d021943b87dea555677144b
160 .\"     net: Cap number of elements for sendmmsg
161 .\"
162 .\"     To limit the amount of time we can spend in sendmmsg, cap the
163 .\"     number of elements to UIO_MAXIOV (currently 1024).
164 .\"
165 .\"     For error handling an application using sendmmsg needs to retry at
166 .\"     the first unsent message, so capping is simpler and requires less
167 .\"     application logic than returning EINVAL.
168 .SH EXAMPLE
169 The example below uses
170 .BR sendmmsg ()
171 to send
172 .I onetwo
173 and
174 .I three
175 in two distinct UDP datagrams using one system call.
176 The contents of the first datagram originates from a pair of buffers.
177
178 .nf
179 #define _GNU_SOURCE
180 #include <netinet/ip.h>
181 #include <stdio.h>
182 #include <stdlib.h>
183 #include <string.h>
184 #include <sys/types.h>
185 #include <sys/socket.h>
186
187 int
188 main(void)
189 {
190     int sockfd;
191     struct sockaddr_in sa;
192     struct mmsghdr msg[2];
193     struct iovec msg1[2], msg2;
194     int retval;
195
196     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
197     if (sockfd == \-1) {
198         perror("socket()");
199         exit(EXIT_FAILURE);
200     }
201
202     sa.sin_family = AF_INET;
203     sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
204     sa.sin_port = htons(1234);
205     if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == \-1) {
206         perror("connect()");
207         exit(EXIT_FAILURE);
208     }
209
210     memset(msg1, 0, sizeof(msg1));
211     msg1[0].iov_base = "one";
212     msg1[0].iov_len = 3;
213     msg1[1].iov_base = "two";
214     msg1[1].iov_len = 3;
215
216     memset(&msg2, 0, sizeof(msg2));
217     msg2.iov_base = "three";
218     msg2.iov_len = 5;
219
220     memset(msg, 0, sizeof(msg));
221     msg[0].msg_hdr.msg_iov = msg1;
222     msg[0].msg_hdr.msg_iovlen = 2;
223
224     msg[1].msg_hdr.msg_iov = &msg2;
225     msg[1].msg_hdr.msg_iovlen = 1;
226
227     retval = sendmmsg(sockfd, msg, 2, 0);
228     if (retval == \-1)
229         perror("sendmmsg()");
230     else
231         printf("%d messages sent\\n", retval);
232
233     exit(0);
234 }
235 .fi
236 .SH SEE ALSO
237 .BR recvmmsg (2),
238 .BR sendmsg (2),
239 .BR socket (2),
240 .BR socket (7)
241 .SH COLOPHON
242 This page is part of release 3.68 of the Linux
243 .I man-pages
244 project.
245 A description of the project,
246 information about reporting bugs,
247 and the latest version of this page,
248 can be found at
249 \%http://www.kernel.org/doc/man\-pages/.