blob: b67279528c7c029fec0c17e61fe37710ba9563e1 [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. Levin4e666722016-01-06 11:43:08 +000030
31#if defined(HAVE_SENDMMSG) && defined(HAVE_STRUCT_MMSGHDR)
32
33# include <assert.h>
34# include <errno.h>
35# include <fcntl.h>
36# include <unistd.h>
37# include <sys/socket.h>
Masatake YAMATO993198d2014-11-07 01:23:27 +090038
39int
40main(void)
41{
Masatake YAMATO993198d2014-11-07 01:23:27 +090042 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 };
Dmitry V. Levin4e666722016-01-06 11:43:08 +000075# define n_mmh (sizeof(mmh)/sizeof(mmh[0]))
Masatake YAMATO993198d2014-11-07 01:23:27 +090076
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
Dmitry V. Levin4e666722016-01-06 11:43:08 +000086 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv))
87 perror_msg_and_skip("socketpair");
Masatake YAMATO993198d2014-11-07 01:23:27 +090088
89 assert(dup2(sv[W], W) == W);
90 assert(close(sv[W]) == 0);
91 assert(dup2(sv[R], R) == R);
92 assert(close(sv[R]) == 0);
93
Dmitry V. Levin95cbf6e2015-01-14 12:47:45 +000094 int r = sendmmsg(W, mmh, n_mmh, 0);
95 if (r < 0 && errno == ENOSYS)
Dmitry V. Levin4e666722016-01-06 11:43:08 +000096 perror_msg_and_skip("sendmmsg");
Dmitry V. Levin95cbf6e2015-01-14 12:47:45 +000097 assert((size_t)r == n_mmh);
Masatake YAMATO993198d2014-11-07 01:23:27 +090098 assert(close(W) == 0);
99
100 assert(recvmmsg(R, mmh, n_mmh, 0, NULL) == n_mmh);
101 assert(close(R) == 0);
102
103 return 0;
Masatake YAMATO993198d2014-11-07 01:23:27 +0900104}
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000105
106#else
107
108SKIP_MAIN_UNDEFINED("HAVE_SENDMMSG && HAVE_STRUCT_MMSGHDR")
109
110#endif