]>
git.ipfire.org Git - thirdparty/lldpd.git/blob - src/daemon/privsep_fdpass.c
5f19d6713ddad0f835b61c79581de9f23bcd7b7b
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
6 * Copyright (c) 2002 Matthieu Herrb
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
51 send_fd(int sock
, int fd
)
56 char buf
[CMSG_SPACE(sizeof(int))];
63 memset(&msg
, 0, sizeof(msg
));
64 memset(&cmsgbuf
.buf
, 0, sizeof(cmsgbuf
.buf
));
67 msg
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
68 msg
.msg_controllen
= sizeof(cmsgbuf
.buf
);
69 cmsg
= CMSG_FIRSTHDR(&msg
);
70 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
71 cmsg
->cmsg_level
= SOL_SOCKET
;
72 cmsg
->cmsg_type
= SCM_RIGHTS
;
73 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(int));
78 vec
.iov_base
= &result
;
79 vec
.iov_len
= sizeof(int);
83 if ((n
= sendmsg(sock
, &msg
, 0)) == -1)
84 LLOG_WARN("sendmsg(%d)", sock
);
86 LLOG_WARNX("sendmsg: expected sent 1 got %ld",
96 char buf
[CMSG_SPACE(sizeof(int))];
104 memset(&msg
, 0, sizeof(msg
));
105 vec
.iov_base
= &result
;
106 vec
.iov_len
= sizeof(int);
109 msg
.msg_control
= &cmsgbuf
.buf
;
110 msg
.msg_controllen
= sizeof(cmsgbuf
.buf
);
112 if ((n
= recvmsg(sock
, &msg
, 0)) == -1)
113 LLOG_WARN("recvmsg");
114 if (n
!= sizeof(int))
115 LLOG_WARNX("recvmsg: expected received 1 got %ld",
118 cmsg
= CMSG_FIRSTHDR(&msg
);
120 LLOG_WARNX("no message header");
123 if (cmsg
->cmsg_type
!= SCM_RIGHTS
)
124 LLOG_WARNX("expected type %d got %d",
125 SCM_RIGHTS
, cmsg
->cmsg_type
);
126 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(int));