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