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 | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame^] | 32 | #if (defined __NR_sendmmsg || defined HAVE_SENDMMSG) \ |
| 33 | && (defined __NR_recvmmsg || defined HAVE_RECVMMSG) |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 34 | |
| 35 | # include <assert.h> |
| 36 | # include <errno.h> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 37 | # include <unistd.h> |
| 38 | # include <sys/socket.h> |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 39 | |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 40 | #ifndef HAVE_STRUCT_MMSGHDR |
| 41 | struct mmsghdr { |
| 42 | struct msghdr msg_hdr; |
| 43 | unsigned msg_len; |
| 44 | }; |
| 45 | #endif |
| 46 | |
| 47 | static int |
| 48 | send_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 | |
| 63 | static int |
| 64 | recv_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags, |
| 65 | struct timespec *timeout) |
| 66 | { |
| 67 | int rc; |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame^] | 68 | #ifdef __NR_recvmmsg |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 69 | 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. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame^] | 74 | #ifdef HAVE_RECVMMSG |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 75 | rc = recvmmsg(fd, vec, vlen, flags, timeout); |
| 76 | #endif |
| 77 | return rc; |
| 78 | } |
| 79 | |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 80 | int |
| 81 | main(void) |
| 82 | { |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 83 | const int R = 0, W = 1; |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 84 | int sv[2]; |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 85 | |
| 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 YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 99 | |
| 100 | struct iovec iov[] = { |
| 101 | { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 102 | .iov_base = copy_one, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 103 | .iov_len = sizeof(one) - 1 |
| 104 | }, { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 105 | .iov_base = copy_two, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 106 | .iov_len = sizeof(two) - 1 |
| 107 | }, { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 108 | .iov_base = copy_three, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 109 | .iov_len = sizeof(three) - 1 |
| 110 | } |
| 111 | }; |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 112 | struct iovec *copy_iov = tail_memdup(iov, sizeof(iov)); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 113 | |
| 114 | struct mmsghdr mmh[] = { |
| 115 | { |
| 116 | .msg_hdr = { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 117 | .msg_iov = copy_iov + 0, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 118 | .msg_iovlen = 2, |
| 119 | } |
| 120 | }, { |
| 121 | .msg_hdr = { |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 122 | .msg_iov = copy_iov + 2, |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 123 | .msg_iovlen = 1, |
| 124 | } |
| 125 | } |
| 126 | }; |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 127 | void *copy_mmh = tail_memdup(mmh, sizeof(mmh)); |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 128 | # define n_mmh (sizeof(mmh)/sizeof(mmh[0])) |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 129 | |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 130 | 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] | 131 | if (r < 0 && errno == ENOSYS) |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 132 | perror_msg_and_skip("sendmmsg"); |
Dmitry V. Levin | 95cbf6e | 2015-01-14 12:47:45 +0000 | [diff] [blame] | 133 | assert((size_t)r == n_mmh); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 134 | assert(close(W) == 0); |
| 135 | |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 136 | 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] | 137 | assert(close(R) == 0); |
| 138 | |
| 139 | return 0; |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 140 | } |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 141 | |
| 142 | #else |
| 143 | |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame^] | 144 | SKIP_MAIN_UNDEFINED("(__NR_sendmmsg || HAVE_SENDMMSG) && (__NR_recvmmsg || HAVE_RECVMMSG)") |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 145 | |
| 146 | #endif |