blob: 630ad1ddc34491fdfe6ff4f53cbbc3fa671cca39 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
2 * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com>
Dmitry V. Levin4e666722016-01-06 11:43:08 +00003 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00004 * 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
Dmitry V. Levin0c8853c2016-01-02 13:28:43 +000029#include "tests.h"
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000030# include <sys/syscall.h>
Dmitry V. Levin4e666722016-01-06 11:43:08 +000031
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000032#if defined __NR_sendmmsg || defined HAVE_SENDMMSG
Dmitry V. Levin4e666722016-01-06 11:43:08 +000033
34# include <assert.h>
35# include <errno.h>
Dmitry V. Levin4e666722016-01-06 11:43:08 +000036# include <unistd.h>
37# include <sys/socket.h>
Masatake YAMATO993198d2014-11-07 01:23:27 +090038
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000039#ifndef HAVE_STRUCT_MMSGHDR
40struct mmsghdr {
41 struct msghdr msg_hdr;
42 unsigned msg_len;
43};
44#endif
45
46static int
47send_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags)
48{
49 int rc;
50#ifdef __NR_sendmmsg
51 rc = syscall(__NR_sendmmsg, (long) fd, vec, (unsigned long) vlen,
52 (unsigned long) flags);
53 if (rc >= 0 || ENOSYS != errno)
54 return rc;
55#endif
56#ifdef HAVE_SENDMMSG
57 rc = sendmmsg(fd, vec, vlen, flags);
58#endif
59 return rc;
60}
61
62static int
63recv_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags,
64 struct timespec *timeout)
65{
66 int rc;
67#ifdef __NR_sendmmsg
68 rc = syscall(__NR_recvmmsg, (long) fd, vec, (unsigned long) vlen,
69 (unsigned long) flags, timeout);
70 if (rc >= 0 || ENOSYS != errno)
71 return rc;
72#endif
73#ifdef HAVE_SENDMMSG
74 rc = recvmmsg(fd, vec, vlen, flags, timeout);
75#endif
76 return rc;
77}
78
Masatake YAMATO993198d2014-11-07 01:23:27 +090079int
80main(void)
81{
Masatake YAMATO993198d2014-11-07 01:23:27 +090082 const int R = 0, W = 1;
Masatake YAMATO993198d2014-11-07 01:23:27 +090083 int sv[2];
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +000084
85 (void) close(0);
86 (void) close(1);
87 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv))
88 perror_msg_and_skip("socketpair");
89 assert(R == sv[0]);
90 assert(W == sv[1]);
91
92 static const char const one[] = "one";
93 static const char const two[] = "two";
94 static const char const three[] = "three";
95 void *copy_one = tail_memdup(one, sizeof(one) - 1);
96 void *copy_two = tail_memdup(two, sizeof(two) - 1);
97 void *copy_three = tail_memdup(three, sizeof(three) - 1);
Masatake YAMATO993198d2014-11-07 01:23:27 +090098
99 struct iovec iov[] = {
100 {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000101 .iov_base = copy_one,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900102 .iov_len = sizeof(one) - 1
103 }, {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000104 .iov_base = copy_two,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900105 .iov_len = sizeof(two) - 1
106 }, {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000107 .iov_base = copy_three,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900108 .iov_len = sizeof(three) - 1
109 }
110 };
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000111 struct iovec *copy_iov = tail_memdup(iov, sizeof(iov));
Masatake YAMATO993198d2014-11-07 01:23:27 +0900112
113 struct mmsghdr mmh[] = {
114 {
115 .msg_hdr = {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000116 .msg_iov = copy_iov + 0,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900117 .msg_iovlen = 2,
118 }
119 }, {
120 .msg_hdr = {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000121 .msg_iov = copy_iov + 2,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900122 .msg_iovlen = 1,
123 }
124 }
125 };
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000126 void *copy_mmh = tail_memdup(mmh, sizeof(mmh));
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000127# define n_mmh (sizeof(mmh)/sizeof(mmh[0]))
Masatake YAMATO993198d2014-11-07 01:23:27 +0900128
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000129 int r = send_mmsg(W, copy_mmh, n_mmh, MSG_DONTROUTE | MSG_NOSIGNAL);
Dmitry V. Levin95cbf6e2015-01-14 12:47:45 +0000130 if (r < 0 && errno == ENOSYS)
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000131 perror_msg_and_skip("sendmmsg");
Dmitry V. Levin95cbf6e2015-01-14 12:47:45 +0000132 assert((size_t)r == n_mmh);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900133 assert(close(W) == 0);
134
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000135 assert(recv_mmsg(R, copy_mmh, n_mmh, MSG_DONTWAIT, NULL) == n_mmh);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900136 assert(close(R) == 0);
137
138 return 0;
Masatake YAMATO993198d2014-11-07 01:23:27 +0900139}
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000140
141#else
142
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +0000143SKIP_MAIN_UNDEFINED("__NR_sendmmsg || HAVE_SENDMMSG")
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000144
145#endif