Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 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 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 29 | #include "tests.h" |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 30 | # include <sys/syscall.h> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 31 | |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 32 | #if defined __NR_sendmmsg || defined HAVE_SENDMMSG |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 33 | |
| 34 | # include <assert.h> |
| 35 | # include <errno.h> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 36 | # include <unistd.h> |
| 37 | # include <sys/socket.h> |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 38 | |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 39 | #ifndef HAVE_STRUCT_MMSGHDR |
| 40 | struct mmsghdr { |
| 41 | struct msghdr msg_hdr; |
| 42 | unsigned msg_len; |
| 43 | }; |
| 44 | #endif |
| 45 | |
| 46 | static int |
| 47 | send_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 | |
| 62 | static int |
| 63 | recv_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 YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 79 | int |
| 80 | main(void) |
| 81 | { |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 82 | const int R = 0, W = 1; |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 83 | int sv[2]; |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 84 | |
| 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 YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 98 | |
| 99 | struct iovec iov[] = { |
| 100 | { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 101 | .iov_base = copy_one, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 102 | .iov_len = sizeof(one) - 1 |
| 103 | }, { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 104 | .iov_base = copy_two, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 105 | .iov_len = sizeof(two) - 1 |
| 106 | }, { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 107 | .iov_base = copy_three, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 108 | .iov_len = sizeof(three) - 1 |
| 109 | } |
| 110 | }; |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 111 | struct iovec *copy_iov = tail_memdup(iov, sizeof(iov)); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 112 | |
| 113 | struct mmsghdr mmh[] = { |
| 114 | { |
| 115 | .msg_hdr = { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 116 | .msg_iov = copy_iov + 0, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 117 | .msg_iovlen = 2, |
| 118 | } |
| 119 | }, { |
| 120 | .msg_hdr = { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 121 | .msg_iov = copy_iov + 2, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 122 | .msg_iovlen = 1, |
| 123 | } |
| 124 | } |
| 125 | }; |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 126 | void *copy_mmh = tail_memdup(mmh, sizeof(mmh)); |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 127 | # define n_mmh (sizeof(mmh)/sizeof(mmh[0])) |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 128 | |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 129 | int r = send_mmsg(W, copy_mmh, n_mmh, MSG_DONTROUTE | MSG_NOSIGNAL); |
Dmitry V. Levin | 95cbf6e | 2015-01-14 12:47:45 +0000 | [diff] [blame] | 130 | if (r < 0 && errno == ENOSYS) |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 131 | perror_msg_and_skip("sendmmsg"); |
Dmitry V. Levin | 95cbf6e | 2015-01-14 12:47:45 +0000 | [diff] [blame] | 132 | assert((size_t)r == n_mmh); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 133 | assert(close(W) == 0); |
| 134 | |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 135 | assert(recv_mmsg(R, copy_mmh, n_mmh, MSG_DONTWAIT, NULL) == n_mmh); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 136 | assert(close(R) == 0); |
| 137 | |
| 138 | return 0; |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 139 | } |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 140 | |
| 141 | #else |
| 142 | |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 143 | SKIP_MAIN_UNDEFINED("__NR_sendmmsg || HAVE_SENDMMSG") |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 144 | |
| 145 | #endif |