safe_libc/posix/
resource.rs

1//
2// Created:  Fri 17 Apr 2020 07:26:13 PM PDT
3// Modified: Sat 18 Apr 2020 04:59:30 PM PDT
4//
5// Copyright (C) 2020 Robert Gill <rtgill82@gmail.com>
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to
9// deal in the Software without restriction, including without limitation the
10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11// sell copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies of the Software, its documentation and marketing & publicity
16// materials, and acknowledgment shall be given in the documentation, materials
17// and software packages that this Software was used.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25//
26
27use crate::errno::Error;
28use crate::errno::Result;
29use crate::util::zeroed;
30
31#[cfg(not(target_os = "linux"))]
32#[allow(non_camel_case_types)]
33type r_int = i32;
34
35#[cfg(target_os = "linux")]
36#[allow(non_camel_case_types)]
37type r_int = u32;
38
39#[cfg(target_pointer_width = "32")]
40#[allow(non_camel_case_types)]
41type rlim = u32;
42
43#[cfg(target_pointer_width = "64")]
44#[allow(non_camel_case_types)]
45type rlim = u64;
46
47pub fn getrlimit(resource: r_int) -> Result<(rlim, rlim)> {
48    let mut rlimit: libc::rlimit = zeroed();
49
50    unsafe {
51        if libc::getrlimit(resource, &mut rlimit) == -1 {
52            return Err(Error::errno());
53        }
54    };
55
56    Ok((rlimit.rlim_cur, rlimit.rlim_max))
57}
58
59pub fn setrlimit(resource: r_int, soft: rlim, hard: rlim) -> Result<()> {
60    let rlimit = libc::rlimit { rlim_cur: soft, rlim_max: hard };
61
62    unsafe {
63        if libc::setrlimit(resource, &rlimit) == -1 {
64            return Err(Error::errno());
65        }
66    };
67
68    Ok(())
69}