Kea 1.5.0
fd_share.cc
Go to the documentation of this file.
1// Copyright (C) 2010-2015 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cstring>
10#include <cstdlib>
11
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <sys/uio.h>
15#include <errno.h>
16#include <stdlib.h> // for malloc and free
17#include <unistd.h>
18#include <util/io/fd_share.h>
19
20namespace isc {
21namespace util {
22namespace io {
23
24namespace {
25// Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
26// In order to ensure as much portability as possible, we provide wrapper
27// functions of these macros.
28// Note that cmsg_space() could run slow on OSes that do not have
29// CMSG_SPACE.
30inline socklen_t
31cmsg_len(const socklen_t len) {
32#ifdef CMSG_LEN
33 return (CMSG_LEN(len));
34#else
35 // Cast NULL so that any pointer arithmetic performed by CMSG_DATA
36 // is correct.
37 const uintptr_t hdrlen = (uintptr_t)CMSG_DATA(((struct cmsghdr*)NULL));
38 return (hdrlen + len);
39#endif
40}
41
42inline socklen_t
43cmsg_space(const socklen_t len) {
44#ifdef CMSG_SPACE
45 return (CMSG_SPACE(len));
46#else
47 struct msghdr msg;
48 struct cmsghdr* cmsgp;
49 // XXX: The buffer length is an ad hoc value, but should be enough
50 // in a practical sense.
51 char dummybuf[sizeof(struct cmsghdr) + 1024];
52
53 memset(&msg, 0, sizeof(msg));
54 msg.msg_control = dummybuf;
55 msg.msg_controllen = sizeof(dummybuf);
56
57 cmsgp = (struct cmsghdr*)dummybuf;
58 cmsgp->cmsg_len = cmsg_len(len);
59
60 cmsgp = CMSG_NXTHDR(&msg, cmsgp);
61 if (cmsgp != NULL) {
62 return ((char*)cmsgp - (char*)msg.msg_control);
63 } else {
64 return (0);
65 }
66#endif // CMSG_SPACE
67}
68}
69
70int
71recv_fd(const int sock) {
72 struct msghdr msghdr;
73 struct iovec iov_dummy;
74 unsigned char dummy_data;
75
76 iov_dummy.iov_base = &dummy_data;
77 iov_dummy.iov_len = sizeof(dummy_data);
78 msghdr.msg_name = NULL;
79 msghdr.msg_namelen = 0;
80 msghdr.msg_iov = &iov_dummy;
81 msghdr.msg_iovlen = 1;
82 msghdr.msg_flags = 0;
83 msghdr.msg_controllen = cmsg_space(sizeof(int));
84 msghdr.msg_control = malloc(msghdr.msg_controllen);
85 if (msghdr.msg_control == NULL) {
86 return (FD_SYSTEM_ERROR);
87 }
88
89 const int cc = recvmsg(sock, &msghdr, 0);
90 if (cc <= 0) {
91 free(msghdr.msg_control);
92 if (cc == 0) {
93 errno = ECONNRESET;
94 }
95 return (FD_SYSTEM_ERROR);
96 }
97 const struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msghdr);
98 int fd = FD_OTHER_ERROR;
99 if (cmsg != NULL && cmsg->cmsg_len == cmsg_len(sizeof(int)) &&
100 cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
101 std::memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
102 }
103 free(msghdr.msg_control);
104 int new_fd = -1;
105 int close_error = -1;
106 if (fd >= 0) {
107 // It is strange, but the call can return the same file descriptor as
108 // one returned previously, even if that one is not closed yet. So,
109 // we just re-number every one we get, so they are unique.
110 new_fd = dup(fd);
111 close_error = close(fd);
112 }
113 if (close_error == -1 || new_fd == -1) {
114 // We need to return an error, because something failed. But in case
115 // it was the previous close, we at least try to close the duped FD.
116 if (new_fd != -1) {
117 close(new_fd); // If this fails, nothing but returning error can't
118 // be done and we are doing that anyway.
119 }
120 return (FD_SYSTEM_ERROR);
121 }
122 return (new_fd);
123}
124
125int
126send_fd(const int sock, const int fd) {
127 struct msghdr msghdr;
128 struct iovec iov_dummy;
129 unsigned char dummy_data = 0;
130
131 iov_dummy.iov_base = &dummy_data;
132 iov_dummy.iov_len = sizeof(dummy_data);
133 msghdr.msg_name = NULL;
134 msghdr.msg_namelen = 0;
135 msghdr.msg_iov = &iov_dummy;
136 msghdr.msg_iovlen = 1;
137 msghdr.msg_flags = 0;
138 msghdr.msg_controllen = cmsg_space(sizeof(int));
139 msghdr.msg_control = malloc(msghdr.msg_controllen);
140 if (msghdr.msg_control == NULL) {
141 return (FD_OTHER_ERROR);
142 }
143 std::memset(msghdr.msg_control, 0, msghdr.msg_controllen);
144
145 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msghdr);
146 cmsg->cmsg_len = cmsg_len(sizeof(int));
147 cmsg->cmsg_level = SOL_SOCKET;
148 cmsg->cmsg_type = SCM_RIGHTS;
149 std::memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
150
151 const int ret = sendmsg(sock, &msghdr, 0);
152 free(msghdr.msg_control);
153 return (ret >= 0 ? 0 : FD_SYSTEM_ERROR);
154}
155
156} // End for namespace io
157} // End for namespace util
158} // End for namespace isc
Support to transfer file descriptors between processes.
const int FD_SYSTEM_ERROR
Definition: fd_share.h:20
int recv_fd(const int sock)
Receives a file descriptor.
Definition: fd_share.cc:71
int send_fd(const int sock, const int fd)
Sends a file descriptor.
Definition: fd_share.cc:126
const int FD_OTHER_ERROR
Definition: fd_share.h:21
Defines the logger used by the top-level component of kea-dhcp-ddns.