]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/privsep_fdpass.c
6b004806a9bb902fb89ac9170d70be919a4a50ea
[thirdparty/lldpd.git] / src / privsep_fdpass.c
1 /*
2 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
4 *
5 * Copyright (c) 2002 Matthieu Herrb
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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.
18 *
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.
31 */
32
33 #include "lldpd.h"
34
35 #include <sys/param.h>
36 #include <sys/uio.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 void
50 send_fd(int sock, int fd)
51 {
52 struct msghdr msg;
53 union {
54 struct cmsghdr hdr;
55 char buf[CMSG_SPACE(sizeof(int))];
56 } cmsgbuf;
57 struct cmsghdr *cmsg;
58 struct iovec vec;
59 int result = 0;
60 ssize_t n;
61
62 memset(&msg, 0, sizeof(msg));
63 memset(&cmsgbuf.buf, 0, sizeof(cmsgbuf.buf));
64
65 if (fd >= 0) {
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));
73 } else {
74 result = errno;
75 }
76
77 vec.iov_base = &result;
78 vec.iov_len = sizeof(int);
79 msg.msg_iov = &vec;
80 msg.msg_iovlen = 1;
81
82 if ((n = sendmsg(sock, &msg, 0)) == -1)
83 LLOG_WARN("sendmsg(%d)", sock);
84 if (n != sizeof(int))
85 LLOG_WARNX("sendmsg: expected sent 1 got %ld",
86 (long)n);
87 }
88
89 int
90 receive_fd(int sock)
91 {
92 struct msghdr msg;
93 union {
94 struct cmsghdr hdr;
95 char buf[CMSG_SPACE(sizeof(int))];
96 } cmsgbuf;
97 struct cmsghdr *cmsg;
98 struct iovec vec;
99 ssize_t n;
100 int result;
101 int fd;
102
103 memset(&msg, 0, sizeof(msg));
104 vec.iov_base = &result;
105 vec.iov_len = sizeof(int);
106 msg.msg_iov = &vec;
107 msg.msg_iovlen = 1;
108 msg.msg_control = &cmsgbuf.buf;
109 msg.msg_controllen = sizeof(cmsgbuf.buf);
110
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",
115 (long)n);
116 if (result == 0) {
117 cmsg = CMSG_FIRSTHDR(&msg);
118 if (cmsg == NULL) {
119 LLOG_WARNX("no message header");
120 return -1;
121 }
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));
126 return fd;
127 } else {
128 errno = result;
129 return -1;
130 }
131 }