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