Skip to content

Commit 34949db

Browse files
committed
rm2fb: Use stream socket instead of dgram
1 parent d092ecd commit 34949db

6 files changed

Lines changed: 151 additions & 70 deletions

File tree

apps/rocket/Launcher.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ waitForSleep() {
7171
/* interactive */ 1);
7272
if (res < 0) {
7373
std::cerr << "Error suspending: " << strerror(-res) << "\n";
74+
sd_bus_unref(bus);
7475
return res;
7576
}
7677
sd_bus_message_unref(reply);

libs/rm2fb/Client.cpp

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,30 @@
1111

1212
#include <dlfcn.h>
1313

14+
#include <csignal>
1415
#include <cstring>
1516
#include <linux/limits.h>
1617
#include <unistd.h>
1718

19+
#include "unistdpp/error.h"
20+
1821
bool inXochitl = false;
1922

2023
namespace {
2124

22-
const unistdpp::Result<ControlSocket>&
25+
ControlSocket&
2326
getControlSocket() {
24-
static auto socket = []() -> unistdpp::Result<ControlSocket> {
25-
ControlSocket res;
26-
TRY(res.init(nullptr));
27-
TRY(res.connect(default_sock_addr.data()));
28-
return res;
29-
}();
30-
31-
return socket;
27+
static ControlSocket res;
28+
if (!res.sock.isValid()) {
29+
res.init(nullptr)
30+
.and_then([] { return res.connect(default_sock_addr.data()); })
31+
.or_else([](auto err) {
32+
std::cerr << "Failed connecting to rm2fb: " << unistdpp::to_string(err)
33+
<< "\n";
34+
res.sock.close();
35+
});
36+
}
37+
return res;
3238
}
3339

3440
int
@@ -53,29 +59,39 @@ setupHooks() {
5359
void
5460
waitForInit() {
5561
std::cerr << "Sending init check\n";
56-
sendUpdate(UpdateParams{
57-
.y1 = 0,
58-
.x1 = 0,
59-
.y2 = 0,
60-
.x2 = 0,
61-
.flags = 0,
62-
.waveform = 0,
63-
.temperatureOverride = 0,
64-
.extraMode = 0,
65-
});
62+
if (!sendUpdate(UpdateParams{
63+
.y1 = 0,
64+
.x1 = 0,
65+
.y2 = 0,
66+
.x2 = 0,
67+
.flags = 0,
68+
.waveform = 0,
69+
.temperatureOverride = 0,
70+
.extraMode = 0,
71+
})) {
72+
std::cerr << "Init failed, no server running\n";
73+
std::exit(EXIT_FAILURE);
74+
}
6675
}
6776

6877
} // namespace
6978

7079
bool
7180
sendUpdate(const UpdateParams& params) {
72-
const auto& clientSock = unistdpp::fatalOnError(getControlSocket());
81+
auto& clientSock = getControlSocket();
82+
if (!clientSock.sock.isValid()) {
83+
return false;
84+
}
7385

7486
return clientSock.sendto(params)
7587
.and_then([&](auto _) {
7688
return clientSock.recvfrom<bool>().map(
7789
[](auto pair) { return pair.first; });
7890
})
91+
.or_else([&](auto err) {
92+
std::cerr << "Error sending: " << unistdpp::to_string(err) << "\n";
93+
clientSock.sock.close();
94+
})
7995
.value_or(false);
8096
}
8197

@@ -206,14 +222,17 @@ __libc_start_main(int (*mainFn)(int, char**, char**), // NOLINT
206222
// We don't support waiting with semaphores yet
207223
setenv("RM2FB_NO_WAIT_IOCTL", "1", 1);
208224

225+
// Don't kill ourselves when SIGPIPE happens because rm2fb went down.
226+
// It might come back up later!
227+
std::signal(SIGPIPE, SIG_IGN);
228+
209229
char pathBuffer[PATH_MAX];
210230
auto size = readlink("/proc/self/exe", pathBuffer, PATH_MAX);
211231

212232
if (std::string_view(pathBuffer, size) == "/usr/bin/xochitl") {
213233
inXochitl = true;
214234

215235
unistdpp::fatalOnError(SharedFB::getInstance(), "Error making shared FB");
216-
unistdpp::fatalOnError(getControlSocket(), "Error creating control socket");
217236

218237
if (setupHooks() != EXIT_SUCCESS) {
219238
std::cerr << "Seting up hooks failed\n";

libs/rm2fb/ControlSocket.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ using namespace unistdpp;
99

1010
Result<void>
1111
ControlSocket::init(const char* addr) {
12-
sock = TRY(unistdpp::socket(AF_UNIX, SOCK_DGRAM, 0));
13-
timeval tv{ .tv_sec = 5, .tv_usec = 0 };
14-
TRY(unistdpp::setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)));
12+
sock = TRY(unistdpp::socket(AF_UNIX, SOCK_STREAM, 0));
1513
return unistdpp::bind(sock, Address::fromUnixPath(addr));
1614
}
1715

1816
Result<void>
19-
ControlSocket::connect(const char* addr) {
17+
ControlSocket::connect(const char* addr) const {
18+
timeval tv{ .tv_sec = 5, .tv_usec = 0 };
19+
TRY(unistdpp::setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)));
2020
return unistdpp::connect(sock, Address::fromUnixPath(addr));
2121
}
22+
23+
Result<void>
24+
ControlSocket::listen(int n) const {
25+
return unistdpp::listen(sock, n);
26+
}
27+
28+
unistdpp::Result<unistdpp::FD>
29+
ControlSocket::accept() const {
30+
return unistdpp::accept(sock, nullptr, nullptr);
31+
}

libs/rm2fb/ControlSocket.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ struct ControlSocket {
1212
unistdpp::FD sock;
1313

1414
unistdpp::Result<void> init(const char* addr);
15-
unistdpp::Result<void> connect(const char* addr);
15+
unistdpp::Result<void> connect(const char* addr) const;
16+
17+
unistdpp::Result<unistdpp::FD> accept() const;
18+
unistdpp::Result<void> listen(int n) const;
1619

1720
template<typename T,
1821
typename = std::enable_if_t<std::is_trivially_copyable_v<T>>>

0 commit comments

Comments
 (0)