From d2c1e1c45759a30b0072faad1f9052d626b35f01 Mon Sep 17 00:00:00 2001 From: zephyr Date: Sun, 28 Aug 2022 23:48:08 +0900 Subject: [PATCH] syscall: bump nofile limit automatically --- realm_syscall/Cargo.toml | 2 +- realm_syscall/src/nofile.rs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/realm_syscall/Cargo.toml b/realm_syscall/Cargo.toml index 1712585..7dee710 100644 --- a/realm_syscall/Cargo.toml +++ b/realm_syscall/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "realm_syscall" -version = "0.1.4" +version = "0.1.5" authors = ["zephyr "] description = "Realm's convenient syscall collections." repository = "https://github.com/zhboner/realm/realm_syscall" diff --git a/realm_syscall/src/nofile.rs b/realm_syscall/src/nofile.rs index f7a1c82..4dac3ca 100644 --- a/realm_syscall/src/nofile.rs +++ b/realm_syscall/src/nofile.rs @@ -1,7 +1,7 @@ use std::io::{Error, Result}; use libc::{rlimit, rlim_t, RLIMIT_NOFILE}; -/// Set nofile limitation. +/// Set nofile limits. /// /// `CAP_NET_ADMIN` privilege is required if exceeds hard limitation. /// @@ -22,7 +22,7 @@ pub fn set_nofile_limit(nofile: u64) -> Result<()> { } } -/// Get current nofile limitation. +/// Get current nofile limits. /// /// Reference: [man](https://man7.org/linux/man-pages/man2/setrlimit.2.html). #[cfg(all(unix, not(target_os = "android")))] @@ -38,3 +38,13 @@ pub fn get_nofile_limit() -> Result<(u64, u64)> { Ok((lim.rlim_cur as u64, lim.rlim_max as u64)) } } + +/// Bump nofile limits. +#[cfg(all(unix, not(target_os = "android")))] +pub fn bump_nofile_limit() -> Result<()> { + let (cur, max) = get_nofile_limit()?; + if cur != max { + set_nofile_limit(cur)?; + } + Ok(()) +}