diff --git a/Cargo.lock b/Cargo.lock index 24d1eff2..0ba05c67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -280,7 +280,7 @@ dependencies = [ "futures-lite 2.2.0", "parking", "polling 3.3.2", - "rustix 0.38.30", + "rustix 0.38.32", "slab", "tracing", "windows-sys 0.52.0", @@ -319,7 +319,7 @@ dependencies = [ "cfg-if", "event-listener 3.1.0", "futures-lite 1.13.0", - "rustix 0.38.30", + "rustix 0.38.32", "windows-sys 0.48.0", ] @@ -346,7 +346,7 @@ dependencies = [ "cfg-if", "futures-core", "futures-io", - "rustix 0.38.30", + "rustix 0.38.32", "signal-hook-registry", "slab", "windows-sys 0.48.0", @@ -380,7 +380,7 @@ name = "atomicwrites" version = "0.4.2" source = "git+https://github.com/jackpot51/rust-atomicwrites#043ab4859d53ffd3d55334685303d8df39c9f768" dependencies = [ - "rustix 0.38.30", + "rustix 0.38.32", "tempfile", "windows-sys 0.48.0", ] @@ -551,7 +551,7 @@ dependencies = [ "bitflags 2.4.2", "log", "polling 3.3.2", - "rustix 0.38.30", + "rustix 0.38.32", "slab", "thiserror", ] @@ -566,7 +566,7 @@ dependencies = [ "bitflags 2.4.2", "log", "polling 3.3.2", - "rustix 0.38.30", + "rustix 0.38.32", "slab", "thiserror", ] @@ -578,7 +578,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f0ea9b9476c7fad82841a8dbb380e2eae480c21910feba80725b46931ed8f02" dependencies = [ "calloop 0.12.4", - "rustix 0.38.30", + "rustix 0.38.32", "wayland-backend", "wayland-client", ] @@ -868,6 +868,7 @@ dependencies = [ "renderdoc", "ron", "rust-embed", + "rustix 0.38.32", "sanitize-filename", "sendfd", "serde", @@ -1260,7 +1261,7 @@ dependencies = [ "bytemuck", "drm-ffi", "drm-fourcc", - "rustix 0.38.30", + "rustix 0.38.32", ] [[package]] @@ -1270,7 +1271,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41334f8405792483e32ad05fbb9c5680ff4e84491883d2947a4757dc54cb2ac6" dependencies = [ "drm-sys", - "rustix 0.38.30", + "rustix 0.38.32", ] [[package]] @@ -2770,9 +2771,9 @@ checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" [[package]] name = "libc" -version = "0.2.152" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libcosmic" @@ -3809,7 +3810,7 @@ dependencies = [ "cfg-if", "concurrent-queue", "pin-project-lite", - "rustix 0.38.30", + "rustix 0.38.32", "tracing", "windows-sys 0.52.0", ] @@ -4265,9 +4266,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.30" +version = "0.38.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" +checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" dependencies = [ "bitflags 2.4.2", "errno", @@ -4533,7 +4534,7 @@ dependencies = [ "pkg-config", "profiling", "rand", - "rustix 0.38.30", + "rustix 0.38.32", "scan_fmt", "scopeguard", "smallvec", @@ -4566,7 +4567,7 @@ dependencies = [ "libc", "log", "memmap2 0.9.3", - "rustix 0.38.30", + "rustix 0.38.32", "thiserror", "wayland-backend", "wayland-client", @@ -4648,7 +4649,7 @@ dependencies = [ "objc", "raw-window-handle", "redox_syscall 0.4.1", - "rustix 0.38.30", + "rustix 0.38.32", "tiny-xlib", "wasm-bindgen", "wayland-backend", @@ -4791,7 +4792,7 @@ dependencies = [ "cfg-if", "fastrand 2.0.1", "redox_syscall 0.4.1", - "rustix 0.38.30", + "rustix 0.38.32", "windows-sys 0.52.0", ] @@ -6065,7 +6066,7 @@ dependencies = [ "percent-encoding", "raw-window-handle", "redox_syscall 0.3.5", - "rustix 0.38.30", + "rustix 0.38.32", "smithay-client-toolkit", "smol_str", "unicode-segmentation", @@ -6123,7 +6124,7 @@ dependencies = [ "libc", "libloading 0.8.1", "once_cell", - "rustix 0.38.30", + "rustix 0.38.32", "x11rb-protocol", ] diff --git a/Cargo.toml b/Cargo.toml index 02960d39..f91522ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ xdg-user = "0.2.1" xkbcommon = "0.7" zbus = "3.15.0" profiling = { version = "1.0" } +rustix = { version = "0.38.32", features = ["process"] } [dependencies.id_tree] branch = "feature/copy_clone" diff --git a/src/input/mod.rs b/src/input/mod.rs index f87cadd9..5d987a24 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -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) => { diff --git a/src/main.rs b/src/main.rs index 40e5c8c4..4dc55027 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index f7f62b0b..75a928b3 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -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; diff --git a/src/utils/rlimit.rs b/src/utils/rlimit.rs new file mode 100644 index 00000000..9d3e1c09 --- /dev/null +++ b/src/utils/rlimit.rs @@ -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); + } +}