Skip to content

Commit

Permalink
Commenting for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
j1ah0ng committed Mar 6, 2020
1 parent b900fa0 commit 88c0581
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sys/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
/// [`Resource`]: enum.Resource.html
pub fn setrlimit(resource: Resource, soft_limit: Option<rlim_t>, hard_limit: Option<rlim_t>) -> Result<()> {
let mut rlim: rlimit = unsafe { mem::uninitialized() };
// TODO: How do we handle the case where soft_limit isn't Some()?
rlim.rlim_cur = soft_limit.unwrap_or(RLIM_INFINITY);
rlim.rlim_max = hard_limit.unwrap_or(RLIM_INFINITY);

Expand Down
14 changes: 14 additions & 0 deletions test/test_resource.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use nix::sys::resource::{Resource, getrlimit, setrlimit};

/// Tests the RLIMIT_NOFILE functionality of getrlimit(), where the resource RLIMIT_NOFILE refers
/// to the maximum file descriptor number that can be opened by the process (aka the maximum number
/// of file descriptors that the process can open, since Linux 4.5).
///
/// We first fetch the existing file descriptor maximum values using getrlimit(), then edit the
/// soft limit to make sure it has a new and distinct value to the hard limit. We then setrlimit()
/// to put the new soft limit in effect, and then getrlimit() once more to ensure the limits have
/// been updated.
#[test]
pub fn test_resource_limits_nofile() {
let (soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
Expand All @@ -15,6 +23,12 @@ pub fn test_resource_limits_nofile() {
assert_eq!(new_soft_limit, soft_limit);
}

/// Tests the RLIMIT_STACK functionality of getrlimit(), where the resource RLIMIT_STACK refers to
/// the maximum stack size that can be spawned by the current process before SIGSEGV is generated.
///
/// We first save the current stack limits, then newly set the soft limit to the same size as the
/// hard limit. We check to make sure these limits have been updated properly. We then set the
/// stack limits back to the original values, and make sure they have been updated properly.
#[test]
pub fn test_resource_limits_stack() {
let (mut soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_STACK).unwrap();
Expand Down

0 comments on commit 88c0581

Please sign in to comment.