blob: 0f25e017ccf14f70cbce61f4bdc900f304ae11db [file] [log] [blame]
Dmitry V. Levinf23b0972014-05-29 21:35:34 +00001#include <assert.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <sys/socket.h>
8#include <sys/wait.h>
9
Dmitry V. Levinc7235992015-01-24 19:51:39 +000010int main(int ac, const char **av)
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000011{
Dmitry V. Levinc7235992015-01-24 19:51:39 +000012 int i;
Dmitry V. Levine25fb4f2014-05-30 15:18:00 +000013 int data = 0;
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000014 struct iovec iov = {
Dmitry V. Levine25fb4f2014-05-30 15:18:00 +000015 .iov_base = &data,
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000016 .iov_len = sizeof(iov)
17 };
18
Dmitry V. Levinc7235992015-01-24 19:51:39 +000019 while ((i = open("/dev/null", O_RDWR)) < 3)
20 assert(i >= 0);
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000021 (void) close(3);
22
23 int sv[2];
24 assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000025 int one = 1;
26 assert(setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) == 0);
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000027
28 pid_t pid = fork();
29 assert(pid >= 0);
30
31 if (pid) {
32 assert(close(sv[0]) == 0);
33 assert(dup2(sv[1], 1) == 1);
34 assert(close(sv[1]) == 0);
35
Dmitry V. Levinc7235992015-01-24 19:51:39 +000036 int fds[ac];
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000037 assert((fds[0] = open("/dev/null", O_RDWR)) == 3);
Dmitry V. Levinc7235992015-01-24 19:51:39 +000038 for (i = 1; i < ac; ++i)
39 assert((fds[i] = open(av[i], O_RDONLY)) == i + 3);
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000040
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000041 union {
42 struct cmsghdr cmsg;
43 char buf[CMSG_LEN(sizeof(fds))];
Dmitry V. Levinc7235992015-01-24 19:51:39 +000044 } control;
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000045
Dmitry V. Levinc7235992015-01-24 19:51:39 +000046 control.cmsg.cmsg_level = SOL_SOCKET;
47 control.cmsg.cmsg_type = SCM_RIGHTS;
48 control.cmsg.cmsg_len = CMSG_LEN(sizeof(fds));
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000049 memcpy(CMSG_DATA(&control.cmsg), fds, sizeof(fds));
50
51 struct msghdr mh = {
52 .msg_iov = &iov,
53 .msg_iovlen = 1,
54 .msg_control = &control,
55 .msg_controllen = sizeof(control)
56 };
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000057
58 assert(sendmsg(1, &mh, 0) == sizeof(iov));
59 assert(close(1) == 0);
60
61 int status;
62 assert(waitpid(pid, &status, 0) == pid);
63 assert(status == 0);
64 } else {
65 assert(close(sv[1]) == 0);
66 assert(dup2(sv[0], 0) == 0);
67 assert(close(sv[0]) == 0);
68
Dmitry V. Levinc7235992015-01-24 19:51:39 +000069 struct cmsghdr control[4 + ac * sizeof(int) / sizeof(struct cmsghdr)];
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +000070
71 struct msghdr mh = {
72 .msg_iov = &iov,
73 .msg_iovlen = 1,
74 .msg_control = control,
75 .msg_controllen = sizeof(control)
76 };
77
Dmitry V. Levinf23b0972014-05-29 21:35:34 +000078 assert(recvmsg(0, &mh, 0) == sizeof(iov));
79 assert(close(0) == 0);
80 }
81
82 return 0;
83}