core: avoid bumping hard rlimits, restore on fork

ref #6584
This commit is contained in:
Vaxry 2024-06-19 18:36:40 +02:00
parent def5fcb212
commit c1e21719a2
3 changed files with 34 additions and 38 deletions

View file

@ -71,44 +71,35 @@ void handleUserSignal(int sig) {
}
}
static void bumpNofile() {
unsigned long limit = 1024;
try {
std::ifstream f("/proc/sys/fs/nr_open");
if (!f.good())
limit = 1073741816;
else {
std::string content((std::istreambuf_iterator<char>(f)), (std::istreambuf_iterator<char>()));
f.close();
limit = std::stoll(content);
}
} catch (...) { limit = 1073741816; }
struct rlimit rlimit_;
if (!getrlimit(RLIMIT_NOFILE, &rlimit_))
Debug::log(LOG, "Old rlimit: soft -> {}, hard -> {}", rlimit_.rlim_cur, rlimit_.rlim_max);
if (rlimit_.rlim_max <= 1024)
rlimit_.rlim_max = limit;
unsigned long oldHardLimit = rlimit_.rlim_max;
rlimit_.rlim_max = limit;
if (setrlimit(RLIMIT_NOFILE, &rlimit_) < 0) {
Debug::log(LOG, "Failed bumping NOFILE limits higher, retrying with previous hard.");
rlimit_.rlim_max = oldHardLimit;
rlimit_.rlim_cur = std::clamp((unsigned long)limit, 1UL, (unsigned long)rlimit_.rlim_max);
if (setrlimit(RLIMIT_NOFILE, &rlimit_) < 0)
Debug::log(LOG, "Failed bumping NOFILE limits higher for the second time.");
void CCompositor::bumpNofile() {
if (!getrlimit(RLIMIT_NOFILE, &m_sOriginalNofile))
Debug::log(LOG, "Old rlimit: soft -> {}, hard -> {}", m_sOriginalNofile.rlim_cur, m_sOriginalNofile.rlim_max);
else {
Debug::log(ERR, "Failed to get NOFILE rlimits");
m_sOriginalNofile.rlim_max = 0;
return;
}
if (!getrlimit(RLIMIT_NOFILE, &rlimit_))
Debug::log(LOG, "New rlimit: soft -> {}, hard -> {}", rlimit_.rlim_cur, rlimit_.rlim_max);
rlimit newLimit = m_sOriginalNofile;
newLimit.rlim_cur = newLimit.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &newLimit) < 0) {
Debug::log(ERR, "Failed bumping NOFILE limits higher");
m_sOriginalNofile.rlim_max = 0;
return;
}
if (!getrlimit(RLIMIT_NOFILE, &newLimit))
Debug::log(LOG, "New rlimit: soft -> {}, hard -> {}", newLimit.rlim_cur, newLimit.rlim_max);
}
void CCompositor::restoreNofile() {
if (m_sOriginalNofile.rlim_max <= 0)
return;
if (setrlimit(RLIMIT_NOFILE, &m_sOriginalNofile) < 0)
Debug::log(ERR, "Failed restoring NOFILE limits");
}
CCompositor::CCompositor() {

View file

@ -3,6 +3,7 @@
#include <memory>
#include <deque>
#include <list>
#include <sys/resource.h>
#include "defines.hpp"
#include "debug/Log.hpp"
@ -81,6 +82,8 @@ class CCompositor {
void cleanup();
void createLockFile();
void removeLockFile();
void bumpNofile();
void restoreNofile();
WP<CWLSurfaceResource> m_pLastFocus;
PHLWINDOWREF m_pLastWindow;
@ -190,8 +193,9 @@ class CCompositor {
void initManagers(eManagersInitStage stage);
void prepareFallbackOutput();
uint64_t m_iHyprlandPID = 0;
wl_event_source* m_critSigSource = nullptr;
uint64_t m_iHyprlandPID = 0;
wl_event_source* m_critSigSource = nullptr;
rlimit m_sOriginalNofile = {0};
};
inline std::unique_ptr<CCompositor> g_pCompositor;

View file

@ -890,6 +890,7 @@ uint64_t CKeybindManager::spawnRaw(std::string args) {
}
if (child == 0) {
// run in child
g_pCompositor->restoreNofile();
sigset_t set;
sigemptyset(&set);