blob: 00af4d55e50ec4909d387b46be1779d37b1c7d68 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
Dmitry V. Levin339a15b2016-01-04 23:53:31 +00002 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00003 * 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. Levin339a15b2016-01-04 23:53:31 +000028#include "tests.h"
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000029#include <assert.h>
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000030#include <errno.h>
31#include <fcntl.h>
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000032#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000035#include <sys/socket.h>
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000036
Dmitry V. Levinc7235992015-01-24 19:51:39 +000037int main(int ac, const char **av)
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000038{
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000039 assert(ac > 0);
40 int fds[ac];
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000041
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000042 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. Levinc7235992015-01-24 19:51:39 +000059 assert(i >= 0);
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000060 while (i > 2)
61 assert(close(i--) == 0);
62 assert(close(0) == 0);
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000063
64 int sv[2];
Dmitry V. Levin339a15b2016-01-04 23:53:31 +000065 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
66 perror_msg_and_skip("socketpair");
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000067 int one = 1;
Dmitry V. Levin339a15b2016-01-04 23:53:31 +000068 if (setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)))
69 perror_msg_and_skip("setsockopt");
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000070
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000071 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. Levinf23b0972014-05-29 21:35:34 +000074
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000075 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. Levinf23b0972014-05-29 21:35:34 +000082
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000083 mh->msg_control = cmsg;
84 mh->msg_controllen = cmsg_size;
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000085
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000086 assert(sendmsg(sv[1], mh, 0) == (int) data_size);
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000087
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000088 assert(close(sv[1]) == 0);
89 assert(open("/dev/null", O_RDWR) == sv[1]);
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000090
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000091 for (i = 0; i < ac; ++i) {
92 assert(close(fds[i]) == 0);
93 fds[i] = 0;
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000094 }
95
Dmitry V. Levin3fdcdd42016-01-12 14:47:12 +000096 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. Levinf23b0972014-05-29 21:35:34 +0000105 return 0;
106}