`Workspaces::*` and `IPC::startIPC` may both call `getSocketFolder` at the same time.
This randomly causes crashes and/or corruption of the socket path.
Typical crash A:
[2024-10-16 07:42:09.987] [info] Hyprland IPC starting
malloc(): unaligned tcache chunk detected
[2024-10-16 07:42:09.987] [error] Hyprland IPC: Unable to connect?
Thread 1 "waybar" received signal SIGABRT, Aborted.
(gdb) bt
#0 __pthread_kill_implementation (threadid=<optimized out>, signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
(omitted for brievety)
#9 0x00007ffff64ae745 in operator new (sz=sz@entry=296) at /usr/src/debug/gcc/gcc/libstdc++-v3/libsupc++/new_op.cc:50
#10 0x00007ffff65ab1f1 in std::filesystem::__cxx11::path::_List::_Impl::copy (this=0x555555a23350) at /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++17/fs_path.cc:249
#11 0x00007ffff65ab3bd in std::filesystem::__cxx11::path::_List::_List (this=0x7fffffff9d30, other=<optimized out>) at /usr/src/debug/gcc/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/unique_ptr.h:454
#12 0x00005555556f4ab1 in waybar::modules::hyprland::IPC::getSocket1Reply(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
#13 0x00005555556f5e3d in waybar::modules::hyprland::IPC::getSocket1JsonReply(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
#14 0x000055555571289c in waybar::modules::hyprland::Workspaces::setCurrentMonitorId() ()
Typical crash B:
[2024-10-16 10:01:15.859] [info] Hyprland IPC starting
[2024-10-16 10:01:15.859] [info] Loading persistent workspaces from Hyprland workspace rules
Thread 8 "waybar" received signal SIGSEGV, Segmentation fault.
(gdb) bt
#0 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy
(__d=0x5555558fbca8 "/", __s=0x2973961a26d35726 <error: Cannot access memory at address 0x2973961a26d35726>, __n=1)
at /usr/src/debug/gcc/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:433
(omitted for brievety)
#15 waybar::modules::hyprland::IPC::getSocketFolder[abi:cxx11](char const*)
(instanceSig=0x7fffffffe604 "4520b30d498daca8079365bdb909a8dea38e8d55_1729051218_1982280648") at ../src/modules/hyprland/backend.cpp:41
#16 0x000055555564230f in waybar::modules::hyprland::IPC::startIPC()::{lambda()#1}::operator()() const ()
at ../src/modules/hyprland/backend.cpp:70
#17 0x00007ffff64e1c34 in std::execute_native_thread_routine (__p=0x5555558119c0) at /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:104
#18 0x00007ffff62a339d in start_thread (arg=<optimized out>) at pthread_create.c:447
220 lines
5.7 KiB
C++
220 lines
5.7 KiB
C++
#include "modules/hyprland/backend.hpp"
|
|
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <spdlog/spdlog.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <sys/un.h>
|
|
#include <unistd.h>
|
|
|
|
#include <filesystem>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
namespace waybar::modules::hyprland {
|
|
|
|
std::filesystem::path IPC::socketFolder_;
|
|
|
|
std::filesystem::path IPC::getSocketFolder(const char* instanceSig) {
|
|
static std::mutex folderMutex;
|
|
std::unique_lock lock(folderMutex);
|
|
|
|
// socket path, specified by EventManager of Hyprland
|
|
if (!socketFolder_.empty()) {
|
|
return socketFolder_;
|
|
}
|
|
|
|
const char* xdgRuntimeDirEnv = std::getenv("XDG_RUNTIME_DIR");
|
|
std::filesystem::path xdgRuntimeDir;
|
|
// Only set path if env variable is set
|
|
if (xdgRuntimeDirEnv != nullptr) {
|
|
xdgRuntimeDir = std::filesystem::path(xdgRuntimeDirEnv);
|
|
}
|
|
|
|
if (!xdgRuntimeDir.empty() && std::filesystem::exists(xdgRuntimeDir / "hypr")) {
|
|
socketFolder_ = xdgRuntimeDir / "hypr";
|
|
} else {
|
|
spdlog::warn("$XDG_RUNTIME_DIR/hypr does not exist, falling back to /tmp/hypr");
|
|
socketFolder_ = std::filesystem::path("/tmp") / "hypr";
|
|
}
|
|
|
|
socketFolder_ = socketFolder_ / instanceSig;
|
|
return socketFolder_;
|
|
}
|
|
|
|
void IPC::startIPC() {
|
|
// will start IPC and relay events to parseIPC
|
|
|
|
std::thread([&]() {
|
|
// check for hyprland
|
|
const char* his = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
|
|
|
if (his == nullptr) {
|
|
spdlog::warn("Hyprland is not running, Hyprland IPC will not be available.");
|
|
return;
|
|
}
|
|
|
|
if (!modulesReady) return;
|
|
|
|
spdlog::info("Hyprland IPC starting");
|
|
|
|
struct sockaddr_un addr;
|
|
int socketfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (socketfd == -1) {
|
|
spdlog::error("Hyprland IPC: socketfd failed");
|
|
return;
|
|
}
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
auto socketPath = IPC::getSocketFolder(his) / ".socket2.sock";
|
|
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
|
|
|
|
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
|
|
|
|
int l = sizeof(struct sockaddr_un);
|
|
|
|
if (connect(socketfd, (struct sockaddr*)&addr, l) == -1) {
|
|
spdlog::error("Hyprland IPC: Unable to connect?");
|
|
return;
|
|
}
|
|
|
|
auto* file = fdopen(socketfd, "r");
|
|
|
|
while (true) {
|
|
std::array<char, 1024> buffer; // Hyprland socket2 events are max 1024 bytes
|
|
|
|
auto* receivedCharPtr = fgets(buffer.data(), buffer.size(), file);
|
|
|
|
if (receivedCharPtr == nullptr) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
continue;
|
|
}
|
|
|
|
std::string messageReceived(buffer.data());
|
|
messageReceived = messageReceived.substr(0, messageReceived.find_first_of('\n'));
|
|
spdlog::debug("hyprland IPC received {}", messageReceived);
|
|
|
|
try {
|
|
parseIPC(messageReceived);
|
|
} catch (std::exception& e) {
|
|
spdlog::warn("Failed to parse IPC message: {}, reason: {}", messageReceived, e.what());
|
|
} catch (...) {
|
|
throw;
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
}
|
|
}).detach();
|
|
}
|
|
|
|
void IPC::parseIPC(const std::string& ev) {
|
|
std::string request = ev.substr(0, ev.find_first_of('>'));
|
|
std::unique_lock lock(callbackMutex_);
|
|
|
|
for (auto& [eventname, handler] : callbacks_) {
|
|
if (eventname == request) {
|
|
handler->onEvent(ev);
|
|
}
|
|
}
|
|
}
|
|
|
|
void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
|
|
if (ev_handler == nullptr) {
|
|
return;
|
|
}
|
|
|
|
std::unique_lock lock(callbackMutex_);
|
|
callbacks_.emplace_back(ev, ev_handler);
|
|
}
|
|
|
|
void IPC::unregisterForIPC(EventHandler* ev_handler) {
|
|
if (ev_handler == nullptr) {
|
|
return;
|
|
}
|
|
|
|
std::unique_lock lock(callbackMutex_);
|
|
|
|
for (auto it = callbacks_.begin(); it != callbacks_.end();) {
|
|
auto& [eventname, handler] = *it;
|
|
if (handler == ev_handler) {
|
|
callbacks_.erase(it++);
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string IPC::getSocket1Reply(const std::string& rq) {
|
|
// basically hyprctl
|
|
|
|
const auto serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (serverSocket < 0) {
|
|
throw std::runtime_error("Hyprland IPC: Couldn't open a socket (1)");
|
|
}
|
|
|
|
// get the instance signature
|
|
auto* instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
|
|
|
if (instanceSig == nullptr) {
|
|
throw std::runtime_error(
|
|
"Hyprland IPC: HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
|
|
}
|
|
|
|
sockaddr_un serverAddress = {0};
|
|
serverAddress.sun_family = AF_UNIX;
|
|
|
|
std::string socketPath = IPC::getSocketFolder(instanceSig) / ".socket.sock";
|
|
|
|
// Use snprintf to copy the socketPath string into serverAddress.sun_path
|
|
if (snprintf(serverAddress.sun_path, sizeof(serverAddress.sun_path), "%s", socketPath.c_str()) <
|
|
0) {
|
|
throw std::runtime_error("Hyprland IPC: Couldn't copy socket path (6)");
|
|
}
|
|
|
|
if (connect(serverSocket, reinterpret_cast<sockaddr*>(&serverAddress), sizeof(serverAddress)) <
|
|
0) {
|
|
throw std::runtime_error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)");
|
|
}
|
|
|
|
auto sizeWritten = write(serverSocket, rq.c_str(), rq.length());
|
|
|
|
if (sizeWritten < 0) {
|
|
spdlog::error("Hyprland IPC: Couldn't write (4)");
|
|
return "";
|
|
}
|
|
|
|
std::array<char, 8192> buffer = {0};
|
|
std::string response;
|
|
|
|
do {
|
|
sizeWritten = read(serverSocket, buffer.data(), 8192);
|
|
|
|
if (sizeWritten < 0) {
|
|
spdlog::error("Hyprland IPC: Couldn't read (5)");
|
|
close(serverSocket);
|
|
return "";
|
|
}
|
|
response.append(buffer.data(), sizeWritten);
|
|
} while (sizeWritten > 0);
|
|
|
|
close(serverSocket);
|
|
return response;
|
|
}
|
|
|
|
Json::Value IPC::getSocket1JsonReply(const std::string& rq) {
|
|
std::string reply = getSocket1Reply("j/" + rq);
|
|
|
|
if (reply.empty()) {
|
|
return {};
|
|
}
|
|
|
|
return parser_.parse(reply);
|
|
}
|
|
|
|
} // namespace waybar::modules::hyprland
|