mount_setattr.2: Minor wording, grammar, and formatting fixes
[man-pages.git] / man2 / sendmmsg.2
blob40a7d6491733a221ad4ccbeb203bc0eb8bc0c745
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 2020-06-09 "Linux" "Linux Programmer's Manual"
29 .SH NAME
30 sendmmsg \- send multiple messages on a socket
31 .SH SYNOPSIS
32 .nf
33 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
34 .BI "#include <sys/socket.h>"
35 .PP
36 .BI "int sendmmsg(int " sockfd ", struct mmsghdr *" msgvec \
37 ", unsigned int " vlen ","
38 .BI "             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 .PP
50 The
51 .I sockfd
52 argument is the file descriptor of the socket
53 on which data is to be transmitted.
54 .PP
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 .PP
63 The
64 .I mmsghdr
65 structure is defined in
66 .I <sys/socket.h>
67 as:
68 .PP
69 .in +4n
70 .EX
71 struct mmsghdr {
72     struct msghdr msg_hdr;  /* Message header */
73     unsigned int  msg_len;  /* Number of bytes transmitted */
75 .EE
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 .PP
92 The
93 .I flags
94 argument contains flags ORed together.
95 The flags are the same as for
96 .BR sendmsg (2).
97 .PP
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.
108 On return from
109 .BR sendmmsg (),
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.
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 See also BUGS.
138 .\" commit 728ffb86f10873aaf4abd26dde691ee40ae731fe
139 .\"     ... only return an error if no datagrams could be sent.
140 .\"     If less than the requested number of messages were sent, the application
141 .\"     must retry starting at the first failed one and if the problem is
142 .\"     persistent the error will be returned.
144 .\"     This matches the behavior of other syscalls like read/write - it
145 .\"     is not an error if less than the requested number of elements are sent.
146 .SH VERSIONS
148 .BR sendmmsg ()
149 system call was added in Linux 3.0.
150 Support in glibc was added in version 2.14.
151 .SH CONFORMING TO
152 .BR sendmmsg ()
153 is Linux-specific.
154 .SH NOTES
155 The value specified in
156 .I vlen
157 is capped to
158 .B UIO_MAXIOV
159 (1024).
160 .\" commit 98382f419f32d2c12d021943b87dea555677144b
161 .\"     net: Cap number of elements for sendmmsg
163 .\"     To limit the amount of time we can spend in sendmmsg, cap the
164 .\"     number of elements to UIO_MAXIOV (currently 1024).
166 .\"     For error handling an application using sendmmsg needs to retry at
167 .\"     the first unsent message, so capping is simpler and requires less
168 .\"     application logic than returning EINVAL.
169 .SH BUGS
170 If an error occurs after at least one message has been sent,
171 the call succeeds, and returns the number of messages sent.
172 The error code is lost.
173 The caller can retry the transmission,
174 starting at the first failed message, but there is no guarantee that,
175 if an error is returned, it will be the same as the one that was lost
176 on the previous call.
177 .SH EXAMPLES
178 The example below uses
179 .BR sendmmsg ()
180 to send
181 .I onetwo
183 .I three
184 in two distinct UDP datagrams using one system call.
185 The contents of the first datagram originates from a pair of buffers.
188 #define _GNU_SOURCE
189 #include <netinet/ip.h>
190 #include <stdio.h>
191 #include <stdlib.h>
192 #include <string.h>
193 #include <sys/types.h>
194 #include <sys/socket.h>
197 main(void)
199     int sockfd;
200     struct sockaddr_in addr;
201     struct mmsghdr msg[2];
202     struct iovec msg1[2], msg2;
203     int retval;
205     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
206     if (sockfd == \-1) {
207         perror("socket()");
208         exit(EXIT_FAILURE);
209     }
211     addr.sin_family = AF_INET;
212     addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
213     addr.sin_port = htons(1234);
214     if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == \-1) {
215         perror("connect()");
216         exit(EXIT_FAILURE);
217     }
219     memset(msg1, 0, sizeof(msg1));
220     msg1[0].iov_base = "one";
221     msg1[0].iov_len = 3;
222     msg1[1].iov_base = "two";
223     msg1[1].iov_len = 3;
225     memset(&msg2, 0, sizeof(msg2));
226     msg2.iov_base = "three";
227     msg2.iov_len = 5;
229     memset(msg, 0, sizeof(msg));
230     msg[0].msg_hdr.msg_iov = msg1;
231     msg[0].msg_hdr.msg_iovlen = 2;
233     msg[1].msg_hdr.msg_iov = &msg2;
234     msg[1].msg_hdr.msg_iovlen = 1;
236     retval = sendmmsg(sockfd, msg, 2, 0);
237     if (retval == \-1)
238         perror("sendmmsg()");
239     else
240         printf("%d messages sent\en", retval);
242     exit(0);
245 .SH SEE ALSO
246 .BR recvmmsg (2),
247 .BR sendmsg (2),
248 .BR socket (2),
249 .BR socket (7)