Skip to content

Commit

Permalink
core: duplicate the inhibit fd with F_DUPFD_CLOEXEC
Browse files Browse the repository at this point in the history
  • Loading branch information
PaideiaDilemma committed Feb 7, 2025
1 parent 0b4d1dc commit d50ea65
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
17 changes: 4 additions & 13 deletions src/core/Hypridle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()); }
Expand All @@ -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();
}
2 changes: 0 additions & 2 deletions src/core/Hypridle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class CHypridle {
void inhibitSleep();
void uninhibitSleep();

void closeInhibitFd();

private:
void setupDBUS();
void enterEventLoop();
Expand Down

0 comments on commit d50ea65

Please sign in to comment.