Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 1 | #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. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 10 | int main(int ac, const char **av) |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 11 | { |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 12 | int i; |
Dmitry V. Levin | e25fb4f | 2014-05-30 15:18:00 +0000 | [diff] [blame] | 13 | int data = 0; |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 14 | struct iovec iov = { |
Dmitry V. Levin | e25fb4f | 2014-05-30 15:18:00 +0000 | [diff] [blame] | 15 | .iov_base = &data, |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 16 | .iov_len = sizeof(iov) |
| 17 | }; |
| 18 | |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 19 | while ((i = open("/dev/null", O_RDWR)) < 3) |
| 20 | assert(i >= 0); |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 21 | (void) close(3); |
| 22 | |
| 23 | int sv[2]; |
| 24 | assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 25 | int one = 1; |
| 26 | assert(setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) == 0); |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 27 | |
| 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. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 36 | int fds[ac]; |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 37 | assert((fds[0] = open("/dev/null", O_RDWR)) == 3); |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 38 | for (i = 1; i < ac; ++i) |
| 39 | assert((fds[i] = open(av[i], O_RDONLY)) == i + 3); |
Dmitry V. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 40 | |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 41 | union { |
| 42 | struct cmsghdr cmsg; |
| 43 | char buf[CMSG_LEN(sizeof(fds))]; |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 44 | } control; |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 45 | |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 46 | control.cmsg.cmsg_level = SOL_SOCKET; |
| 47 | control.cmsg.cmsg_type = SCM_RIGHTS; |
| 48 | control.cmsg.cmsg_len = CMSG_LEN(sizeof(fds)); |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 49 | 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. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 57 | |
| 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. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 69 | struct cmsghdr control[4 + ac * sizeof(int) / sizeof(struct cmsghdr)]; |
Dmitry V. Levin | b85a7f3 | 2015-01-24 15:20:31 +0000 | [diff] [blame] | 70 | |
| 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. Levin | f23b097 | 2014-05-29 21:35:34 +0000 | [diff] [blame] | 78 | assert(recvmsg(0, &mh, 0) == sizeof(iov)); |
| 79 | assert(close(0) == 0); |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |