]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/privsep_fdpass.c
Add "format" option.
[thirdparty/lldpd.git] / src / privsep_fdpass.c
CommitLineData
4afe659e
VB
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
49void
50send_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
64 if (fd >= 0) {
65 msg.msg_control = (caddr_t)&cmsgbuf.buf;
66 msg.msg_controllen = sizeof(cmsgbuf.buf);
67 cmsg = CMSG_FIRSTHDR(&msg);
68 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
69 cmsg->cmsg_level = SOL_SOCKET;
70 cmsg->cmsg_type = SCM_RIGHTS;
72c00381 71 memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
4afe659e
VB
72 } else {
73 result = errno;
74 }
75
76 vec.iov_base = &result;
77 vec.iov_len = sizeof(int);
78 msg.msg_iov = &vec;
79 msg.msg_iovlen = 1;
80
81 if ((n = sendmsg(sock, &msg, 0)) == -1)
82 LLOG_WARN("sendmsg(%d)", sock);
83 if (n != sizeof(int))
84 LLOG_WARNX("sendmsg: expected sent 1 got %ld",
85 (long)n);
86}
87
88int
89receive_fd(int sock)
90{
91 struct msghdr msg;
92 union {
93 struct cmsghdr hdr;
94 char buf[CMSG_SPACE(sizeof(int))];
95 } cmsgbuf;
96 struct cmsghdr *cmsg;
97 struct iovec vec;
98 ssize_t n;
99 int result;
100 int fd;
101
102 memset(&msg, 0, sizeof(msg));
103 vec.iov_base = &result;
104 vec.iov_len = sizeof(int);
105 msg.msg_iov = &vec;
106 msg.msg_iovlen = 1;
107 msg.msg_control = &cmsgbuf.buf;
108 msg.msg_controllen = sizeof(cmsgbuf.buf);
109
110 if ((n = recvmsg(sock, &msg, 0)) == -1)
111 LLOG_WARN("recvmsg");
112 if (n != sizeof(int))
113 LLOG_WARNX("recvmsg: expected received 1 got %ld",
114 (long)n);
115 if (result == 0) {
116 cmsg = CMSG_FIRSTHDR(&msg);
117 if (cmsg == NULL) {
118 LLOG_WARNX("no message header");
119 return -1;
120 }
121 if (cmsg->cmsg_type != SCM_RIGHTS)
122 LLOG_WARNX("expected type %d got %d",
123 SCM_RIGHTS, cmsg->cmsg_type);
72c00381 124 memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
4afe659e
VB
125 return fd;
126 } else {
127 errno = result;
128 return -1;
129 }
130}