1 /* $OpenBSD: privsep_fdpass.c,v 1.1 2003/10/22 18:51:55 canacar Exp $ */
2 /* $DragonFly: src/usr.sbin/pflogd/privsep_fdpass.c,v 1.1 2004/09/21 21:25:28 joerg Exp $ */
5 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
8 * Copyright (c) 2002 Matthieu Herrb
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * - Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
51 send_fd(int sock
, int fd
)
54 char tmp
[CMSG_SPACE(sizeof(int))];
60 memset(&msg
, 0, sizeof(msg
));
63 msg
.msg_control
= (caddr_t
)tmp
;
64 msg
.msg_controllen
= CMSG_LEN(sizeof(int));
65 cmsg
= CMSG_FIRSTHDR(&msg
);
66 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
67 cmsg
->cmsg_level
= SOL_SOCKET
;
68 cmsg
->cmsg_type
= SCM_RIGHTS
;
69 *(int *)CMSG_DATA(cmsg
) = fd
;
74 vec
.iov_base
= (caddr_t
)&result
;
75 vec
.iov_len
= sizeof(int);
79 if ((n
= sendmsg(sock
, &msg
, 0)) == -1)
80 warn("%s: sendmsg(%d)", __func__
, sock
);
82 warnx("%s: sendmsg: expected sent 1 got %ld",
90 char tmp
[CMSG_SPACE(sizeof(int))];
97 memset(&msg
, 0, sizeof(msg
));
98 vec
.iov_base
= (caddr_t
)&result
;
99 vec
.iov_len
= sizeof(int);
102 msg
.msg_control
= tmp
;
103 msg
.msg_controllen
= sizeof(tmp
);
105 if ((n
= recvmsg(sock
, &msg
, 0)) == -1)
106 warn("%s: recvmsg", __func__
);
107 if (n
!= sizeof(int))
108 warnx("%s: recvmsg: expected received 1 got %ld",
111 cmsg
= CMSG_FIRSTHDR(&msg
);
112 if (cmsg
->cmsg_type
!= SCM_RIGHTS
)
113 warnx("%s: expected type %d got %d", __func__
,
114 SCM_RIGHTS
, cmsg
->cmsg_type
);
115 fd
= (*(int *)CMSG_DATA(cmsg
));