Files
Waybar/include/modules/hyprland/backend.hpp
Lukas Fleischer 5a29473080 Prevent child zombie process from tearing down Hyprland IPC
In rare circumstances, we may fork(), e.g., as part of a custom module,
and the child process may fail to exec() and exit. In those cases, the
IPC destructor will be called in the child process.

Prior to this commit, this call would then result in the shared socket
being closed. Prevent this by only closing the socket from the original
process.

Fixes #3975 and #4152.

Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
2025-08-22 18:58:53 -04:00

52 lines
1.2 KiB
C++

#pragma once
#include <filesystem>
#include <list>
#include <mutex>
#include <string>
#include <thread>
#include <utility>
#include "util/json.hpp"
namespace waybar::modules::hyprland {
class EventHandler {
public:
virtual void onEvent(const std::string& ev) = 0;
virtual ~EventHandler() = default;
};
class IPC {
public:
IPC();
~IPC();
static IPC& inst();
void registerForIPC(const std::string& ev, EventHandler* ev_handler);
void unregisterForIPC(EventHandler* handler);
static std::string getSocket1Reply(const std::string& rq);
Json::Value getSocket1JsonReply(const std::string& rq);
static std::filesystem::path getSocketFolder(const char* instanceSig);
protected:
static std::filesystem::path socketFolder_;
private:
void socketListener();
void parseIPC(const std::string&);
std::thread ipcThread_;
std::mutex callbackMutex_;
util::JsonParser parser_;
std::list<std::pair<std::string, EventHandler*>> callbacks_;
int socketfd_; // the hyprland socket file descriptor
pid_t socketOwnerPid_;
bool running_ = true;
};
inline bool modulesReady = false;
inline std::unique_ptr<IPC> gIPC;
}; // namespace waybar::modules::hyprland