2 /* $OpenBSD: monitor_fdpass.c,v 1.18 2008/11/30 11:59:26 dtucker Exp $ */
4 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
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.
29 __RCSID("$NetBSD: monitor_fdpass.c,v 1.8 2008/04/19 22:15:30 martin Exp $");
30 #include <sys/types.h>
31 #include <sys/socket.h>
39 #include "monitor_fdpass.h"
42 mm_send_fd(int sock
, int fd
)
54 if (sizeof(cmsgbuf
.buf
) < CMSG_SPACE(sizeof(int))) {
55 error("%s: %zu < %zu, recompile", __func__
,
56 sizeof(cmsgbuf
.buf
), CMSG_SPACE(sizeof(int)));
60 memset(&msg
, 0, sizeof(msg
));
61 msg
.msg_control
= &cmsgbuf
.buf
;
62 msg
.msg_controllen
= CMSG_SPACE(sizeof(int));
63 cmsg
= CMSG_FIRSTHDR(&msg
);
64 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
65 cmsg
->cmsg_level
= SOL_SOCKET
;
66 cmsg
->cmsg_type
= SCM_RIGHTS
;
67 *(int *)CMSG_DATA(cmsg
) = fd
;
68 msg
.msg_controllen
= cmsg
->cmsg_len
;
75 while ((n
= sendmsg(sock
, &msg
, 0)) == -1 && (errno
== EAGAIN
||
77 debug3("%s: sendmsg(%d): %s", __func__
, fd
, strerror(errno
));
79 error("%s: sendmsg(%d): %s", __func__
, fd
,
85 error("%s: sendmsg: expected sent 1 got %ld",
93 mm_receive_fd(int sock
)
100 struct cmsghdr
*cmsg
;
106 if (sizeof(cmsgbuf
.buf
) < CMSG_SPACE(sizeof(int))) {
107 error("%s: %zu < %zu, recompile", __func__
,
108 sizeof(cmsgbuf
.buf
), CMSG_SPACE(sizeof(int)));
112 memset(&msg
, 0, sizeof(msg
));
117 msg
.msg_control
= &cmsgbuf
.buf
;
118 msg
.msg_controllen
= CMSG_SPACE(sizeof(int));
120 while ((n
= recvmsg(sock
, &msg
, 0)) == -1 && (errno
== EAGAIN
||
122 debug3("%s: recvmsg: %s", __func__
, strerror(errno
));
124 error("%s: recvmsg: %s", __func__
, strerror(errno
));
129 error("%s: recvmsg: expected received 1 got %ld",
134 cmsg
= CMSG_FIRSTHDR(&msg
);
136 error("%s: no message header", __func__
);
140 if (cmsg
->cmsg_type
!= SCM_RIGHTS
) {
141 error("%s: expected type %d got %d", __func__
,
142 SCM_RIGHTS
, cmsg
->cmsg_type
);
145 fd
= (*(int *)CMSG_DATA(cmsg
));