blob: 0c9c7115864efbd9b73fb5273a9998d9d4a0e7fd [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. Levineb4649a2016-01-12 04:37:06 +000032#if (defined __NR_sendmmsg || defined HAVE_SENDMMSG) \
33 && (defined __NR_recvmmsg || defined HAVE_RECVMMSG)
Dmitry V. Levin4e666722016-01-06 11:43:08 +000034
35# include <assert.h>
36# include <errno.h>
Dmitry V. Levin4e666722016-01-06 11:43:08 +000037# include <unistd.h>
38# include <sys/socket.h>
Masatake YAMATO993198d2014-11-07 01:23:27 +090039
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000040#ifndef HAVE_STRUCT_MMSGHDR
41struct mmsghdr {
42 struct msghdr msg_hdr;
43 unsigned msg_len;
44};
45#endif
46
47static int
48send_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags)
49{
50 int rc;
51#ifdef __NR_sendmmsg
52 rc = syscall(__NR_sendmmsg, (long) fd, vec, (unsigned long) vlen,
53 (unsigned long) flags);
54 if (rc >= 0 || ENOSYS != errno)
55 return rc;
56#endif
57#ifdef HAVE_SENDMMSG
58 rc = sendmmsg(fd, vec, vlen, flags);
59#endif
60 return rc;
61}
62
63static int
64recv_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags,
65 struct timespec *timeout)
66{
67 int rc;
Dmitry V. Levineb4649a2016-01-12 04:37:06 +000068#ifdef __NR_recvmmsg
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000069 rc = syscall(__NR_recvmmsg, (long) fd, vec, (unsigned long) vlen,
70 (unsigned long) flags, timeout);
71 if (rc >= 0 || ENOSYS != errno)
72 return rc;
73#endif
Dmitry V. Levineb4649a2016-01-12 04:37:06 +000074#ifdef HAVE_RECVMMSG
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000075 rc = recvmmsg(fd, vec, vlen, flags, timeout);
76#endif
77 return rc;
78}
79
Masatake YAMATO993198d2014-11-07 01:23:27 +090080int
81main(void)
82{
Masatake YAMATO993198d2014-11-07 01:23:27 +090083 const int R = 0, W = 1;
Masatake YAMATO993198d2014-11-07 01:23:27 +090084 int sv[2];
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +000085
86 (void) close(0);
87 (void) close(1);
88 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv))
89 perror_msg_and_skip("socketpair");
90 assert(R == sv[0]);
91 assert(W == sv[1]);
92
93 static const char const one[] = "one";
94 static const char const two[] = "two";
95 static const char const three[] = "three";
96 void *copy_one = tail_memdup(one, sizeof(one) - 1);
97 void *copy_two = tail_memdup(two, sizeof(two) - 1);
98 void *copy_three = tail_memdup(three, sizeof(three) - 1);
Masatake YAMATO993198d2014-11-07 01:23:27 +090099
100 struct iovec iov[] = {
101 {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000102 .iov_base = copy_one,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900103 .iov_len = sizeof(one) - 1
104 }, {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000105 .iov_base = copy_two,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900106 .iov_len = sizeof(two) - 1
107 }, {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000108 .iov_base = copy_three,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900109 .iov_len = sizeof(three) - 1
110 }
111 };
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000112 struct iovec *copy_iov = tail_memdup(iov, sizeof(iov));
Masatake YAMATO993198d2014-11-07 01:23:27 +0900113
114 struct mmsghdr mmh[] = {
115 {
116 .msg_hdr = {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000117 .msg_iov = copy_iov + 0,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900118 .msg_iovlen = 2,
119 }
120 }, {
121 .msg_hdr = {
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000122 .msg_iov = copy_iov + 2,
Masatake YAMATO993198d2014-11-07 01:23:27 +0900123 .msg_iovlen = 1,
124 }
125 }
126 };
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000127 void *copy_mmh = tail_memdup(mmh, sizeof(mmh));
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000128# define n_mmh (sizeof(mmh)/sizeof(mmh[0]))
Masatake YAMATO993198d2014-11-07 01:23:27 +0900129
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000130 int r = send_mmsg(W, copy_mmh, n_mmh, MSG_DONTROUTE | MSG_NOSIGNAL);
Dmitry V. Levin95cbf6e2015-01-14 12:47:45 +0000131 if (r < 0 && errno == ENOSYS)
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000132 perror_msg_and_skip("sendmmsg");
Dmitry V. Levin95cbf6e2015-01-14 12:47:45 +0000133 assert((size_t)r == n_mmh);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900134 assert(close(W) == 0);
135
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +0000136 assert(recv_mmsg(R, copy_mmh, n_mmh, MSG_DONTWAIT, NULL) == n_mmh);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900137 assert(close(R) == 0);
138
139 return 0;
Masatake YAMATO993198d2014-11-07 01:23:27 +0900140}
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000141
142#else
143
Dmitry V. Levineb4649a2016-01-12 04:37:06 +0000144SKIP_MAIN_UNDEFINED("(__NR_sendmmsg || HAVE_SENDMMSG) && (__NR_recvmmsg || HAVE_RECVMMSG)")
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000145
146#endif