Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fdlimit"
version = "0.1.4"
version = "0.2.0"
authors = ["Parity Technologies<[email protected]>"]
license = "Apache-2.0"
description = "Utility crate for raising file descriptors limit for OSX and Linux"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ on Windows does nothing
add in Cargo.toml:
```
[dependencies]
fdlimit = "0.1.1"
fdlimit = "0.2.0"
```
32 changes: 28 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@

extern crate libc;

/// Raise the soft open file descriptor resource limit to the smaller of the
/// kernel limit and the hard resource limit.
///
/// Returns [`Some`] with the new limit.
///
/// # Panics
///
/// Panics if [`libc::sysctl`], [`libc::getrlimit`] or [`libc::setrlimit`]
/// fail.
///
/// darwin_fd_limit exists to work around an issue where launchctl on Mac OS X
/// defaults the rlimit maxfiles to 256/unlimited. The default soft limit of 256
/// ends up being far too low for our multithreaded scheduler testing, depending
/// on the number of cores available.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[allow(non_camel_case_types)]
pub fn raise_fd_limit() {
pub fn raise_fd_limit() -> Option<u64> {
use std::cmp;
use std::io;
use std::mem::size_of_val;
Expand Down Expand Up @@ -60,13 +70,22 @@ pub fn raise_fd_limit() {
let err = io::Error::last_os_error();
panic!("raise_fd_limit: error calling setrlimit: {}", err);
}

Some(rlim.rlim_cur)
}
}

/// Raise the soft open file descriptor resource limit to the hard resource
/// limit.
///
/// Returns [`Some`] with the new limit.
///
/// # Panics
///
/// Panics if [`libc::getrlimit`] or [`libc::setrlimit`] fail.
#[cfg(any(target_os = "linux"))]
#[allow(non_camel_case_types)]
pub fn raise_fd_limit() {
use libc;
pub fn raise_fd_limit() -> Option<u64> {
use std::io;

unsafe {
Expand All @@ -85,8 +104,13 @@ pub fn raise_fd_limit() {
let err = io::Error::last_os_error();
panic!("raise_fd_limit: error calling setrlimit: {}", err);
}

Some(rlim.rlim_cur)
}
}

/// Returns [`None`].
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux")))]
pub fn raise_fd_limit() {}
pub fn raise_fd_limit() -> Option<u64> {
None
}