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 | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame^] | 30 | |
| 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 YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 38 | |
| 39 | int |
| 40 | main(void) |
| 41 | { |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 42 | 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. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame^] | 75 | # define n_mmh (sizeof(mmh)/sizeof(mmh[0])) |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 76 | |
| 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. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame^] | 86 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv)) |
| 87 | perror_msg_and_skip("socketpair"); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 88 | |
| 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. Levin | 95cbf6e | 2015-01-14 12:47:45 +0000 | [diff] [blame] | 94 | int r = sendmmsg(W, mmh, n_mmh, 0); |
| 95 | if (r < 0 && errno == ENOSYS) |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame^] | 96 | perror_msg_and_skip("sendmmsg"); |
Dmitry V. Levin | 95cbf6e | 2015-01-14 12:47:45 +0000 | [diff] [blame] | 97 | assert((size_t)r == n_mmh); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 98 | 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 YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 104 | } |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame^] | 105 | |
| 106 | #else |
| 107 | |
| 108 | SKIP_MAIN_UNDEFINED("HAVE_SENDMMSG && HAVE_STRUCT_MMSGHDR") |
| 109 | |
| 110 | #endif |