71 class wayland_peer_t {
73 wayland_peer_t() =
default;
74 wayland_peer_t(
const wayland_peer_t &) =
delete;
75 wayland_peer_t &operator=(
const wayland_peer_t &) =
delete;
85 result = ::waitpid(child, &status, WNOHANG);
86 }
while (result < 0 && errno == EINTR);
90 ::kill(child, SIGKILL);
92 result = ::waitpid(child, &status, 0);
93 }
while (result < 0 && errno == EINTR);
100 int open(
const std::string &path, std::string &error) {
102 auto executable = std::filesystem::read_symlink(
"/proc/self/exe", ec);
104 error =
"Could not locate Hermes's private Wayland connection helper: " + ec.message();
107 return open_with_helper(path, (executable.parent_path() /
"hermes-detached-session").string(), error);
110 int open_with_helper(
const std::string &path,
const std::string &helper, std::string &error) {
112 sockaddr_un address {};
113 if (child > 0 || path.empty() || path.front() !=
'/' || path.find(
'\0') != std::string::npos || path.size() >=
sizeof(address.sun_path) ||
114 helper.empty() || helper.front() !=
'/' || helper.find(
'\0') != std::string::npos) {
115 error =
"Private Wayland connection requires an absolute socket and helper path";
118 const long max_fd = ::sysconf(_SC_OPEN_MAX);
120 error =
"Could not determine the descriptor limit for the private Wayland helper";
124 if (::socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, descriptors) != 0) {
125 error =
"Could not create private Wayland helper channel: " + std::string(std::strerror(errno));
129 const int source = ::fcntl(descriptors[1], F_DUPFD_CLOEXEC, 4);
130 ::close(descriptors[1]);
132 ::close(descriptors[0]);
133 error =
"Could not prepare private Wayland helper descriptor";
136 char *
const arguments[] {
const_cast<char *
>(helper.c_str()),
const_cast<char *
>(
"--connect-wayland"),
const_cast<char *
>(path.c_str()),
nullptr};
137 char *
const environment[] {
nullptr};
142 if (::dup2(source, 3) < 0) {
148#ifdef SYS_close_range
149 if (::syscall(SYS_close_range, 4U, ~0U, 0U) != 0)
152 for (
long fd = 4; fd < max_fd; ++fd) {
153 ::close(
static_cast<int>(fd));
158 ::execve(helper.c_str(), arguments, environment);
162 channel = descriptors[0];
164 error =
"Could not start private Wayland connection helper";
168 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5);
169 pollfd
event {channel, POLLIN, 0};
172 const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - std::chrono::steady_clock::now()).count();
173 result = ::poll(&event, 1,
static_cast<int>(std::max<std::int64_t>(0, left)));
174 }
while (result < 0 && errno == EINTR && std::chrono::steady_clock::now() < deadline);
175 if (result <= 0 || !(event.revents & POLLIN)) {
176 error =
"Private Wayland connection helper failed or timed out; check hermes-detached-session beside Hermes";
180 alignas(cmsghdr)
char control[CMSG_SPACE(
sizeof(
int))] {};
181 iovec payload {&success,
sizeof(success)};
183 message.msg_iov = &payload;
184 message.msg_iovlen = 1;
185 message.msg_control = control;
186 message.msg_controllen =
sizeof(control);
187 const auto received = ::recvmsg(channel, &message, MSG_CMSG_CLOEXEC | MSG_DONTWAIT);
190 for (
auto *item = CMSG_FIRSTHDR(&message); received >= 0 && item; item = CMSG_NXTHDR(&message, item)) {
191 if (item->cmsg_level == SOL_SOCKET && item->cmsg_type == SCM_RIGHTS && item->cmsg_len >= CMSG_LEN(0)) {
192 const auto count = (item->cmsg_len - CMSG_LEN(0)) /
sizeof(
int);
193 for (
size_t i = 0; i < count; ++i) {
195 std::memcpy(&fd, CMSG_DATA(item) + i *
sizeof(
int),
sizeof(fd));
196 if (connection < 0) {
205 if (received != 1 || success != 1 || connection < 0 || extra || (message.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
206 if (connection >= 0) {
209 error =
"Private Wayland helper could not connect to the requested socket; refusing host fallback";