• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com>
3  * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <sys/socket.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <assert.h>
37 
38 int
main(void)39 main(void)
40 {
41 #if defined(HAVE_SENDMMSG) && defined(HAVE_STRUCT_MMSGHDR)
42 	const int R = 0, W = 1;
43 	int fd;
44 	int sv[2];
45 	char one[] = "one";
46 	char two[] = "two";
47 	char three[] = "three";
48 
49 	struct iovec iov[] = {
50 		{
51 			.iov_base = one,
52 			.iov_len = sizeof(one) - 1
53 		}, {
54 			.iov_base = two,
55 			.iov_len = sizeof(two) - 1
56 		}, {
57 			.iov_base = three,
58 			.iov_len = sizeof(three) - 1
59 		}
60 	};
61 
62 	struct mmsghdr mmh[] = {
63 		{
64 			.msg_hdr = {
65 				.msg_iov = iov + 0,
66 				.msg_iovlen = 2,
67 			}
68 		}, {
69 			.msg_hdr = {
70 				.msg_iov = iov + 2,
71 				.msg_iovlen = 1,
72 			}
73 		}
74 	};
75 #define n_mmh (sizeof(mmh)/sizeof(mmh[0]))
76 
77 	/*
78 	 * Following open/dup2/close calls make the output of strace
79 	 * more predictable, so we can just compare the output and
80 	 * expected output (mmsg.expected) for testing purposes.
81 	 */
82 	while ((fd = open("/dev/null", O_RDWR)) < 3)
83 		assert(fd >= 0);
84 	(void) close(3);
85 
86 	assert(socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) == 0);
87 
88 	assert(dup2(sv[W], W) == W);
89 	assert(close(sv[W]) == 0);
90 	assert(dup2(sv[R], R) == R);
91 	assert(close(sv[R]) == 0);
92 
93 	int r = sendmmsg(W, mmh, n_mmh, 0);
94 	if (r < 0 && errno == ENOSYS)
95 		return 77;
96 	assert((size_t)r == n_mmh);
97 	assert(close(W) == 0);
98 
99 	assert(recvmmsg(R, mmh, n_mmh, 0, NULL) == n_mmh);
100 	assert(close(R) == 0);
101 
102 	return 0;
103 #else
104 	return 77;
105 #endif
106 }
107