Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 339a15b | 2016-01-04 23:53:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Dmitry V. Levin | 339a15b | 2016-01-04 23:53:31 +0000 | [diff] [blame] | 28 | #include "tests.h" |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 29 | #include <assert.h> |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <unistd.h> |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 35 | #include <sys/socket.h> |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 36 | |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 37 | int main(int ac, const char **av) |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 38 | { |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 39 | assert(ac > 0); |
| 40 | int fds[ac]; |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 41 | |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 42 | static const char sample[] = |
| 43 | "\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; |
| 44 | const unsigned int data_size = sizeof(sample) - 1; |
| 45 | void *data = tail_alloc(data_size); |
| 46 | memcpy(data, sample, data_size); |
| 47 | |
| 48 | struct iovec *iov = tail_alloc(sizeof(struct iovec)); |
| 49 | iov->iov_base = data; |
| 50 | iov->iov_len = data_size; |
| 51 | |
| 52 | struct msghdr *mh = tail_alloc(sizeof(struct msghdr)); |
| 53 | memset(mh, 0, sizeof(*mh)); |
| 54 | mh->msg_iov = iov; |
| 55 | mh->msg_iovlen = 1; |
| 56 | |
| 57 | int i; |
| 58 | while ((i = open("/dev/null", O_RDWR)) <= ac + 2) |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 59 | assert(i >= 0); |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 60 | while (i > 2) |
| 61 | assert(close(i--) == 0); |
| 62 | assert(close(0) == 0); |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 63 | |
| 64 | int sv[2]; |
Dmitry V. Levin | 339a15b | 2016-01-04 23:53:31 +0000 | [diff] [blame] | 65 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) |
| 66 | perror_msg_and_skip("socketpair"); |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 67 | int one = 1; |
Dmitry V. Levin | 339a15b | 2016-01-04 23:53:31 +0000 | [diff] [blame] | 68 | if (setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one))) |
| 69 | perror_msg_and_skip("setsockopt"); |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 70 | |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 71 | assert((fds[0] = open("/dev/null", O_RDWR)) == 4); |
| 72 | for (i = 1; i < ac; ++i) |
| 73 | assert((fds[i] = open(av[i], O_RDONLY)) == i + 4); |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 74 | |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 75 | unsigned int cmsg_size = CMSG_SPACE(sizeof(fds)); |
| 76 | struct cmsghdr *cmsg = tail_alloc(cmsg_size); |
| 77 | memset(cmsg, 0, cmsg_size); |
| 78 | cmsg->cmsg_level = SOL_SOCKET; |
| 79 | cmsg->cmsg_type = SCM_RIGHTS; |
| 80 | cmsg->cmsg_len = CMSG_LEN(sizeof(fds)); |
| 81 | memcpy(CMSG_DATA(cmsg), fds, sizeof(fds)); |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 82 | |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 83 | mh->msg_control = cmsg; |
| 84 | mh->msg_controllen = cmsg_size; |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 85 | |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 86 | assert(sendmsg(sv[1], mh, 0) == (int) data_size); |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 87 | |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 88 | assert(close(sv[1]) == 0); |
| 89 | assert(open("/dev/null", O_RDWR) == sv[1]); |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 90 | |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 91 | for (i = 0; i < ac; ++i) { |
| 92 | assert(close(fds[i]) == 0); |
| 93 | fds[i] = 0; |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Dmitry V. Levin | 3fdcdd4 | 2016-01-12 14:47:12 +0000 | [diff] [blame] | 96 | cmsg_size += CMSG_SPACE(sizeof(struct ucred)); |
| 97 | cmsg = tail_alloc(cmsg_size); |
| 98 | memset(cmsg, 0, cmsg_size); |
| 99 | mh->msg_control = cmsg; |
| 100 | mh->msg_controllen = cmsg_size; |
| 101 | |
| 102 | assert(recvmsg(0, mh, 0) == (int) data_size); |
| 103 | assert(close(0) == 0); |
| 104 | |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |