From 29bb352234fc26767fe673a748daa8fd56ce9552 Mon Sep 17 00:00:00 2001 From: Maximilian Seidler Date: Fri, 7 Feb 2025 10:11:59 +0100 Subject: [PATCH] core: duplicate the inhibit fd with F_DUPFD_CLOEXEC --- src/core/Hypridle.cpp | 19 +++++-------------- src/core/Hypridle.hpp | 2 -- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/core/Hypridle.cpp b/src/core/Hypridle.cpp index 08c0114..f66f62c 100644 --- a/src/core/Hypridle.cpp +++ b/src/core/Hypridle.cpp @@ -25,7 +25,7 @@ void setupSignals(void) { sigemptyset(&sa.sa_mask); sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART; sa.sa_handler = SIG_DFL; - sigaction(SIGCHLD, &sa, NULL); + sigaction(SIGCHLD, &sa, nullptr); } void CHypridle::run() { @@ -260,11 +260,6 @@ static void spawn(const std::string& args) { sa.sa_handler = SIG_DFL; sigaction(SIGCHLD, &sa, NULL); - // TODO: ??? Why do we need that ??? - // We already make sure we have O_CLOEXEC and that should be enough. Still somehow, without this, releasing the inhibitor does not work. - // Somehow related to sdbus::UnixFd calling dup on the fd?? - g_pHypridle->closeInhibitFd(); - execl("/bin/sh", "/bin/sh", "-c", args.c_str(), nullptr); _exit(1); } @@ -627,10 +622,10 @@ void CHypridle::inhibitSleep() { // There seems to be no way to get the file descriptor out of the reply other than that. reply >> fd; - m_sDBUSState.sleepInhibitFd = Hyprutils::OS::CFileDescriptor(fd.release()); - auto flags = m_sDBUSState.sleepInhibitFd.getFlags(); - if (!(flags & O_CLOEXEC) && !m_sDBUSState.sleepInhibitFd.setFlags(flags | O_CLOEXEC)) - Debug::log(ERR, "Sleep inhibitor has no O_CLOEXEC"); + // Setting the O_CLOEXEC flag does not work for some reason. Instead we make our own dupe and close the one from UnixFd. + auto immidiateFD = Hyprutils::OS::CFileDescriptor(fd.release()); + m_sDBUSState.sleepInhibitFd = immidiateFD.duplicate(F_DUPFD_CLOEXEC); + immidiateFD.reset(); // close the fd that was opened with dup Debug::log(LOG, "Inhibited sleep with fd {}", m_sDBUSState.sleepInhibitFd.get()); } catch (const std::exception& e) { Debug::log(ERR, "Failed to inhibit sleep ({})", e.what()); } @@ -645,7 +640,3 @@ void CHypridle::uninhibitSleep() { Debug::log(LOG, "Releasing the sleep inhibitor!"); m_sDBUSState.sleepInhibitFd.reset(); } - -void CHypridle::closeInhibitFd() { - m_sDBUSState.sleepInhibitFd.reset(); -} diff --git a/src/core/Hypridle.hpp b/src/core/Hypridle.hpp index 6f7fcb3..78cde1b 100644 --- a/src/core/Hypridle.hpp +++ b/src/core/Hypridle.hpp @@ -49,8 +49,6 @@ class CHypridle { void inhibitSleep(); void uninhibitSleep(); - void closeInhibitFd(); - private: void setupDBUS(); void enterEventLoop();