main: Increase soft file limit

This commit is contained in:
Victoria Brekenfeld 2024-04-03 18:08:43 +02:00 committed by Victoria Brekenfeld
parent fa7926f7c1
commit 6ba7242cfc
6 changed files with 60 additions and 21 deletions

View file

@ -74,6 +74,7 @@ use std::{
any::Any,
cell::RefCell,
collections::HashMap,
os::unix::process::CommandExt,
thread,
time::{Duration, Instant},
};
@ -2303,6 +2304,7 @@ impl State {
.env("XDG_ACTIVATION_TOKEN", &*token)
.env("DESKTOP_STARTUP_ID", &*token)
.env_remove("COSMIC_SESSION_SOCK");
unsafe { cmd.pre_exec(|| Ok(crate::utils::rlimit::restore_nofile_limit())) };
std::thread::spawn(move || match cmd.spawn() {
Ok(mut child) => {

View file

@ -9,7 +9,7 @@ use smithay::{
};
use anyhow::{Context, Result};
use std::{env, ffi::OsString, process, sync::Arc};
use std::{env, ffi::OsString, os::unix::process::CommandExt, process, sync::Arc};
use tracing::{error, info, warn};
use crate::{
@ -44,6 +44,8 @@ fn main() -> Result<()> {
logger::init_logger()?;
info!("Cosmic starting up!");
utils::rlimit::increase_nofile_limit();
// init event loop
let mut event_loop = EventLoop::try_new().with_context(|| "Failed to initialize event loop")?;
// init wayland
@ -71,6 +73,8 @@ fn main() -> Result<()> {
let mut command = process::Command::new(&exec);
command.args(args);
command.envs(session::get_env(&state)?);
unsafe { command.pre_exec(|| Ok(utils::rlimit::restore_nofile_limit())) };
info!("Running {:?}", exec);
Some(command.spawn()?)
} else {

View file

@ -5,5 +5,6 @@ pub(crate) use self::ids::id_gen;
pub mod geometry;
pub mod iced;
pub mod prelude;
pub mod rlimit;
pub mod screenshot;
pub mod tween;

30
src/utils/rlimit.rs Normal file
View file

@ -0,0 +1,30 @@
use rustix::process::{getrlimit, setrlimit, Resource, Rlimit};
use std::sync::atomic::{AtomicU64, Ordering};
static OLD_LIMIT: AtomicU64 = AtomicU64::new(0);
static MAX_LIMIT: AtomicU64 = AtomicU64::new(0);
pub fn increase_nofile_limit() {
let mut limits = getrlimit(Resource::Nofile);
OLD_LIMIT.store(limits.current.unwrap_or(0), Ordering::SeqCst);
MAX_LIMIT.store(limits.maximum.unwrap_or(0), Ordering::SeqCst);
limits.current = limits.maximum;
if let Err(err) = setrlimit(Resource::Nofile, limits) {
tracing::warn!("Failed to raise nofile soft limit: {:?}", err);
}
}
pub fn restore_nofile_limit() {
let current = OLD_LIMIT.load(Ordering::SeqCst);
let maximum = MAX_LIMIT.load(Ordering::SeqCst);
let limits = Rlimit {
current: (current > 0).then_some(current),
maximum: (maximum > 0).then_some(maximum),
};
if let Err(err) = setrlimit(Resource::Nofile, limits) {
eprintln!("Failed to restore nofile soft limit: {:?}", err);
}
}