2 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "monitor_fdpass.h"
34 mm_send_fd(int sock, int fd)
36 #if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
41 #ifndef HAVE_ACCRIGHTS_IN_MSGHDR
42 char tmp[CMSG_SPACE(sizeof(int))];
46 memset(&msg, 0, sizeof(msg));
47 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
48 msg.msg_accrights = (caddr_t)&fd;
49 msg.msg_accrightslen = sizeof(fd);
51 msg.msg_control = (caddr_t)tmp;
52 msg.msg_controllen = CMSG_LEN(sizeof(int));
53 cmsg = CMSG_FIRSTHDR(&msg);
54 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
55 cmsg->cmsg_level = SOL_SOCKET;
56 cmsg->cmsg_type = SCM_RIGHTS;
57 *(int *)CMSG_DATA(cmsg) = fd;
65 if ((n = sendmsg(sock, &msg, 0)) == -1)
66 fatal("%s: sendmsg(%d): %s", __func__, fd,
69 fatal("%s: sendmsg: expected sent 1 got %ld",
72 fatal("%s: UsePrivilegeSeparation=yes not supported",
78 mm_receive_fd(int sock)
80 #if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
86 #ifndef HAVE_ACCRIGHTS_IN_MSGHDR
87 char tmp[CMSG_SPACE(sizeof(int))];
91 memset(&msg, 0, sizeof(msg));
96 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
97 msg.msg_accrights = (caddr_t)&fd;
98 msg.msg_accrightslen = sizeof(fd);
100 msg.msg_control = tmp;
101 msg.msg_controllen = sizeof(tmp);
104 if ((n = recvmsg(sock, &msg, 0)) == -1)
105 fatal("%s: recvmsg: %s", __func__, strerror(errno));
107 fatal("%s: recvmsg: expected received 1 got %ld",
110 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
111 if (msg.msg_accrightslen != sizeof(fd))
112 fatal("%s: no fd", __func__);
114 cmsg = CMSG_FIRSTHDR(&msg);
116 fatal("%s: no message header", __func__);
117 #ifndef BROKEN_CMSG_TYPE
118 if (cmsg->cmsg_type != SCM_RIGHTS)
119 fatal("%s: expected type %d got %d", __func__,
120 SCM_RIGHTS, cmsg->cmsg_type);
122 fd = (*(int *)CMSG_DATA(cmsg));
126 fatal("%s: UsePrivilegeSeparation=yes not supported",