1 /* $OpenBSD: privsep_fdpass.c,v 1.2 2004/08/13 02:51:48 djm Exp $ */
4 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
7 * Copyright (c) 2002 Matthieu Herrb
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
50 send_fd(int sock
, int fd
)
53 char tmp
[CMSG_SPACE(sizeof(int))];
59 memset(&msg
, 0, sizeof(msg
));
62 msg
.msg_control
= (caddr_t
)tmp
;
63 msg
.msg_controllen
= CMSG_LEN(sizeof(int));
64 cmsg
= CMSG_FIRSTHDR(&msg
);
65 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
66 cmsg
->cmsg_level
= SOL_SOCKET
;
67 cmsg
->cmsg_type
= SCM_RIGHTS
;
68 *(int *)CMSG_DATA(cmsg
) = fd
;
73 vec
.iov_base
= (caddr_t
)&result
;
74 vec
.iov_len
= sizeof(int);
78 if ((n
= sendmsg(sock
, &msg
, 0)) == -1)
79 warn("%s: sendmsg(%d)", __func__
, sock
);
81 warnx("%s: sendmsg: expected sent 1 got %ld",
89 char tmp
[CMSG_SPACE(sizeof(int))];
96 memset(&msg
, 0, sizeof(msg
));
97 vec
.iov_base
= (caddr_t
)&result
;
98 vec
.iov_len
= sizeof(int);
101 msg
.msg_control
= tmp
;
102 msg
.msg_controllen
= sizeof(tmp
);
104 if ((n
= recvmsg(sock
, &msg
, 0)) == -1)
105 warn("%s: recvmsg", __func__
);
106 if (n
!= sizeof(int))
107 warnx("%s: recvmsg: expected received 1 got %ld",
110 cmsg
= CMSG_FIRSTHDR(&msg
);
112 warnx("%s: no message header", __func__
);
115 if (cmsg
->cmsg_type
!= SCM_RIGHTS
)
116 warnx("%s: expected type %d got %d", __func__
,
117 SCM_RIGHTS
, cmsg
->cmsg_type
);
118 fd
= (*(int *)CMSG_DATA(cmsg
));