1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Serialize, Deserialize)]
pub struct SoftAndHardResourceLimit
{
soft: ResourceLimit,
hard: ResourceLimit,
}
impl SoftAndHardResourceLimit
{
pub const BothInfinite: SoftAndHardResourceLimit = SoftAndHardResourceLimit
{
soft: ResourceLimit::Infinite,
hard: ResourceLimit::Infinite,
};
pub const BothZero: SoftAndHardResourceLimit = SoftAndHardResourceLimit
{
soft: ResourceLimit::Finite(0),
hard: ResourceLimit::Finite(0),
};
pub fn both(soft_and_hard: ResourceLimit) -> Self
{
Self::new(soft_and_hard.clone(), soft_and_hard)
}
pub fn new(soft: ResourceLimit, hard: ResourceLimit) -> Self
{
if soft.is_infinite() && hard.is_finite()
{
panic!("soft limit can not be infinite if hard limit '{}' is finite", hard.unwrap());
}
if soft.is_finite() && hard.is_finite()
{
assert!(soft.unwrap() <= hard.unwrap(), "soft limit '{:?}' must be less than or the same as hard limit '{:?}'", soft, hard);
}
Self
{
soft,
hard,
}
}
#[inline(always)]
pub fn soft_limit(&self) -> &ResourceLimit
{
&self.soft
}
#[inline(always)]
pub fn hard_limit(&self) -> &ResourceLimit
{
&self.hard
}
fn set(&self, resource_identifier: i32)
{
let value = rlimit64
{
rlim_cur: self.soft.unwrap(),
rlim_max: self.hard.unwrap(),
};
match unsafe { setrlimit64(resource_identifier, &value) }
{
0 => (),
-1 => match errno().0
{
E::EPERM => panic!("Permission denied or tried to increase MaximumNumberOfFileDescriptors above /proc/sys/fs/nr_open"),
E::EINVAL => panic!("Limit was too large or bad resource id"),
E::EFAULT => panic!("Bad pointer"),
illegal @ _ => panic!("Illegal errno '{}' from setrlimit64()", illegal),
},
illegal @ _ => panic!("Illegal result '{}' from setrlimit64()", illegal),
}
}
fn get(resource_identifier: i32) -> Self
{
let mut value = rlimit64
{
rlim_cur: 0,
rlim_max: 0,
};
match unsafe { getrlimit64(resource_identifier, &mut value) }
{
0 => (),
-1 => match errno().0
{
E::EPERM => panic!("Permission denied"),
E::EINVAL => panic!("Bad resource id"),
E::EFAULT => panic!("Bad pointer"),
illegal @ _ => panic!("Illegal errno '{}' from setrlimit64()", illegal),
},
illegal @ _ => panic!("Illegal result '{}' from setrlimit64()", illegal),
};
Self
{
soft: ResourceLimit::convert(value.rlim_cur),
hard: ResourceLimit::convert(value.rlim_max),
}
}
}