Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
card_broker.cpp
Go to the documentation of this file.
1
5#include "card_broker.h"
6
7#include <array>
8#include <cerrno>
9#include <chrono>
10#include <cstdlib>
11#include <cstring>
12#include <sstream>
13#include <vector>
14
15#include <fcntl.h>
16#include <poll.h>
17#include <sys/socket.h>
18#include <sys/stat.h>
19#include <sys/un.h>
20#include <unistd.h>
21
22#include "src/logging.h"
23#include "src/utility.h"
24
25namespace VDISPLAY::card_broker {
26
27 namespace {
28
29 constexpr const char *default_socket_path = "/run/hermes/card-broker.sock";
30 constexpr size_t max_reply_bytes = 4096;
31
32 // The whole conversation - connect, request, reply - shares one deadline.
33 // The broker accepts serially, so a request may have to wait out another
34 // client's slow connection first; a broker that cannot answer within this
35 // must not hang the display bring-up behind it.
36 constexpr auto broker_timeout = std::chrono::seconds(15);
37
44 bool wait_ready(int fd, short events, std::chrono::steady_clock::time_point deadline) {
45 for (;;) {
46 const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(
47 deadline - std::chrono::steady_clock::now());
48 if (remaining.count() <= 0) {
49 return false;
50 }
51 pollfd descriptor {fd, events, 0};
52 const int ready = ::poll(&descriptor, 1, static_cast<int>(remaining.count()));
53 if (ready > 0) {
54 return (descriptor.revents & (events | POLLERR | POLLHUP | POLLNVAL)) != 0;
55 }
56 if (ready == 0) {
57 return false;
58 }
59 if (errno != EINTR) {
60 return false;
61 }
62 }
63 }
64
72 bool connect_with_timeout(int fd, const sockaddr *address, socklen_t length,
73 std::chrono::steady_clock::time_point deadline) {
74 const int flags = ::fcntl(fd, F_GETFL, 0);
75 if (flags >= 0) {
76 ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
77 }
78
79 int result = ::connect(fd, address, length);
80 if (result != 0 && (errno == EINPROGRESS || errno == EAGAIN)) {
81 if (!wait_ready(fd, POLLOUT, deadline)) {
82 result = -1;
83 errno = ETIMEDOUT;
84 } else {
85 int socket_error = 0;
86 socklen_t error_length = sizeof(socket_error);
87 if (::getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_error, &error_length) != 0 ||
88 socket_error != 0) {
89 errno = socket_error != 0 ? socket_error : errno;
90 result = -1;
91 } else {
92 result = 0;
93 }
94 }
95 }
96
97 if (flags >= 0) {
98 ::fcntl(fd, F_SETFL, flags); // Restore blocking mode.
99 }
100 return result == 0;
101 }
102
103 std::string socket_path() {
104 // Overridable so the suite can point at a broker of its own rather than
105 // at the machine's.
106 if (const char *override_path = std::getenv("HERMES_CARD_BROKER_SOCKET")) {
107 return override_path;
108 }
109 return default_socket_path;
110 }
111
119 std::optional<std::string> ask(const std::string &request) {
120 const auto path = socket_path();
121 sockaddr_un address {};
122 address.sun_family = AF_UNIX;
123 if (path.size() >= sizeof(address.sun_path)) {
124 BOOST_LOG(error) << "[VDISPLAY/card-broker] Socket path is too long: " << path;
125 return std::nullopt;
126 }
127 std::memcpy(address.sun_path, path.c_str(), path.size());
128
129 const int fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
130 if (fd < 0) {
131 BOOST_LOG(error) << "[VDISPLAY/card-broker] Could not create a socket: " << std::strerror(errno);
132 return std::nullopt;
133 }
134 auto guard = util::fail_guard([fd]() {
135 ::close(fd);
136 });
137
138 const auto deadline = std::chrono::steady_clock::now() + broker_timeout;
139 if (!connect_with_timeout(fd, reinterpret_cast<sockaddr *>(&address), sizeof(address), deadline)) {
140 BOOST_LOG(debug) << "[VDISPLAY/card-broker] No broker at " << path << ": " << std::strerror(errno);
141 return std::nullopt;
142 }
143
144 const auto line = request + "\n";
145 size_t offset = 0;
146 while (offset < line.size()) {
147 if (!wait_ready(fd, POLLOUT, deadline)) {
148 BOOST_LOG(error) << "[VDISPLAY/card-broker] Timed out sending " << request << " to the broker.";
149 return std::nullopt;
150 }
151 const ssize_t count = ::write(fd, line.data() + offset, line.size() - offset);
152 if (count <= 0) {
153 BOOST_LOG(error) << "[VDISPLAY/card-broker] Could not send " << request << ": "
154 << std::strerror(errno);
155 return std::nullopt;
156 }
157 offset += static_cast<size_t>(count);
158 }
159 ::shutdown(fd, SHUT_WR);
160
161 std::string reply;
162 std::array<char, 512> buffer {};
163 while (reply.size() < max_reply_bytes) {
164 if (!wait_ready(fd, POLLIN, deadline)) {
165 BOOST_LOG(error) << "[VDISPLAY/card-broker] Timed out waiting for the reply to " << request << '.';
166 return std::nullopt;
167 }
168 const ssize_t count = ::read(fd, buffer.data(), buffer.size());
169 if (count < 0) {
170 BOOST_LOG(error) << "[VDISPLAY/card-broker] Could not read the reply to " << request << ": "
171 << std::strerror(errno);
172 return std::nullopt;
173 }
174 if (count == 0) {
175 break;
176 }
177 reply.append(buffer.data(), static_cast<size_t>(count));
178 if (reply.find('\n') != std::string::npos) {
179 break;
180 }
181 }
182
183 const auto newline = reply.find('\n');
184 if (newline != std::string::npos) {
185 reply.resize(newline);
186 }
187 if (reply.empty()) {
188 BOOST_LOG(error) << "[VDISPLAY/card-broker] The broker closed without answering " << request << '.';
189 return std::nullopt;
190 }
191 return reply;
192 }
193
194 std::vector<std::string> fields(const std::string &line) {
195 std::vector<std::string> out;
196 std::istringstream stream {line};
197 std::string field;
198 while (stream >> field) {
199 out.emplace_back(field);
200 }
201 return out;
202 }
203
209 std::optional<std::vector<std::string>> succeeded(const std::optional<std::string> &reply,
210 const char *request) {
211 if (!reply) {
212 return std::nullopt;
213 }
214 auto parts = fields(*reply);
215 if (parts.empty() || parts.front() != "OK") {
216 BOOST_LOG(warning) << "[VDISPLAY/card-broker] " << request << " was refused: " << *reply;
217 return std::nullopt;
218 }
219 parts.erase(parts.begin());
220 return parts;
221 }
222
223 } // namespace
224
225 bool available() {
226 struct stat info {};
227 return ::stat(socket_path().c_str(), &info) == 0 && S_ISSOCK(info.st_mode);
228 }
229
230 std::optional<card_t> create(std::optional<uid_t> session_account_uid) {
231 const auto request = session_account_uid ?
232 "CREATE " + std::to_string(*session_account_uid) :
233 "CREATE";
234 const auto parts = succeeded(ask(request), "CREATE");
235 if (!parts) {
236 return std::nullopt;
237 }
238 if (parts->size() < 3) {
239 BOOST_LOG(error) << "[VDISPLAY/card-broker] CREATE returned no device nodes.";
240 return std::nullopt;
241 }
242
243 card_t card {(*parts)[0], (*parts)[1], (*parts)[2]};
244 BOOST_LOG(info) << "[VDISPLAY/card-broker] Created card " << card.name << " (" << card.card_path
245 << ", " << card.render_node_path << ").";
246 return card;
247 }
248
249 bool remove(const std::string &name) {
250 if (name.empty()) {
251 return false;
252 }
253 return succeeded(ask("REMOVE " + name), "REMOVE").has_value();
254 }
255
256 int sweep() {
257 const auto parts = succeeded(ask("SWEEP"), "SWEEP");
258 if (!parts || parts->empty()) {
259 return 0;
260 }
261 const int removed = std::atoi(parts->front().c_str());
262 if (removed > 0) {
263 BOOST_LOG(warning) << "[VDISPLAY/card-broker] Removed " << removed
264 << " card(s) left behind by an earlier run.";
265 }
266 return removed;
267 }
268
269} // namespace VDISPLAY::card_broker
bool available()
Whether a broker socket is there to talk to.
bool remove(const std::string &name)
Remove a card this user created. Removal is a hot-unplug, so a compositor still holding the card sees...
int sweep()
Remove every card this user still holds, and report how many went.
std::optional< card_t > create(std::optional< uid_t > session_account_uid)
Ask for a new card on a private DRM seat.
Ask the privileged broker for a Hermes-KMS card.
Declarations for logging related functions.
A card the broker created, and the device nodes it was given.
Definition card_broker.h:22
std::string card_path
/dev/dri/cardN, root-only and reached through the seat broker.
Definition card_broker.h:24
std::string name
configfs group name, needed to remove it again.
Definition card_broker.h:23
std::string render_node_path
/dev/dri/renderDN, owned by this user once udev has run.
Definition card_broker.h:25
Declarations for utility functions.