Repository navigation
Expand file tree
/
Copy pathresource.rs
More file actions
220 lines (190 loc) · 7.52 KB
/
Copy pathresource.rs
File metadata and controls
220 lines (190 loc) · 7.52 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// spell-checker:disable
pub(crate) use resource::module_def;
#[pymodule]
mod resource {
use crate::vm::{
Py, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, VirtualMachine,
builtins::PyInt,
convert::{ToPyException, ToPyObject},
function::ArgIndex,
types::PyStructSequence,
};
use num_traits::Signed;
use rustpython_host_env::resource as host_resource;
use std::io;
#[cfg_attr(target_os = "android", expect(deprecated))]
const RLIM_NLIMITS: i32 = cfg_select! {
target_os = "android" => {
host_resource::RLIM_NLIMITS
}
_ => {
// This constant isn't abi-stable across os versions, so we just
// pick a high number so we don't get false positive ValueErrors and just bubble up the
// EINVAL that get/setrlimit return on an invalid resource
256
}
};
// TODO: RLIMIT_OFILE,
#[pyattr]
use host_resource::{
RLIM_INFINITY, RLIMIT_AS, RLIMIT_CORE, RLIMIT_CPU, RLIMIT_DATA, RLIMIT_FSIZE,
RLIMIT_MEMLOCK, RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_RSS, RLIMIT_STACK,
};
#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
#[pyattr]
use host_resource::{RLIMIT_MSGQUEUE, RLIMIT_NICE, RLIMIT_RTPRIO, RLIMIT_SIGPENDING};
// TODO: I think this is supposed to be defined for all linux_like?
#[cfg(target_os = "linux")]
#[pyattr]
use host_resource::RLIMIT_RTTIME;
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "solaris",
target_os = "illumos"
))]
#[pyattr]
use host_resource::RLIMIT_SBSIZE;
#[cfg(any(target_os = "freebsd", target_os = "solaris", target_os = "illumos"))]
#[pyattr]
use host_resource::{RLIMIT_NPTS, RLIMIT_SWAP};
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
#[pyattr]
use host_resource::RLIMIT_VMEM;
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "freebsd"))]
#[pyattr]
use host_resource::RUSAGE_THREAD;
#[cfg(not(any(target_os = "windows", target_os = "redox")))]
#[pyattr]
use host_resource::{RUSAGE_CHILDREN, RUSAGE_SELF};
#[pystruct_sequence_data]
struct RUsageData {
ru_utime: f64,
ru_stime: f64,
ru_maxrss: host_resource::c_long,
ru_ixrss: host_resource::c_long,
ru_idrss: host_resource::c_long,
ru_isrss: host_resource::c_long,
ru_minflt: host_resource::c_long,
ru_majflt: host_resource::c_long,
ru_nswap: host_resource::c_long,
ru_inblock: host_resource::c_long,
ru_oublock: host_resource::c_long,
ru_msgsnd: host_resource::c_long,
ru_msgrcv: host_resource::c_long,
ru_nsignals: host_resource::c_long,
ru_nvcsw: host_resource::c_long,
ru_nivcsw: host_resource::c_long,
}
#[pyattr]
#[pystruct_sequence(name = "struct_rusage", module = "resource", data = "RUsageData")]
struct PyRUsage;
#[pyclass(with(PyStructSequence))]
impl PyRUsage {}
impl From<host_resource::RUsage> for RUsageData {
fn from(rusage: host_resource::RUsage) -> Self {
Self {
ru_utime: rusage.utime_secs(),
ru_stime: rusage.stime_secs(),
ru_maxrss: rusage.ru_maxrss,
ru_ixrss: rusage.ru_ixrss,
ru_idrss: rusage.ru_idrss,
ru_isrss: rusage.ru_isrss,
ru_minflt: rusage.ru_minflt,
ru_majflt: rusage.ru_majflt,
ru_nswap: rusage.ru_nswap,
ru_inblock: rusage.ru_inblock,
ru_oublock: rusage.ru_oublock,
ru_msgsnd: rusage.ru_msgsnd,
ru_msgrcv: rusage.ru_msgrcv,
ru_nsignals: rusage.ru_nsignals,
ru_nvcsw: rusage.ru_nvcsw,
ru_nivcsw: rusage.ru_nivcsw,
}
}
}
// The exception the module reports its failures through, which is the one
// every other call raises.
#[pyattr(name = "error", once)]
fn error(vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.exceptions.os_error.to_owned().into()
}
#[pyfunction]
fn getpagesize() -> usize {
rustpython_host_env::os::page_size()
}
#[pyfunction]
fn getrusage(who: i32, vm: &VirtualMachine) -> PyResult<RUsageData> {
let res = host_resource::getrusage(who);
res.map(RUsageData::from).map_err(|e| {
if e.kind() == io::ErrorKind::InvalidInput {
vm.new_value_error("invalid who parameter")
} else {
e.to_pyexception(vm)
}
})
}
struct Limits(host_resource::rlimit);
impl<'a> TryFromBorrowedObject<'a> for Limits {
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
let seq: Vec<PyObjectRef> = obj.try_to_value(vm)?;
match seq.as_slice() {
[cur, max] => Ok(Self(host_resource::rlimit {
rlim_cur: py2rlim_limit(cur, vm)?,
rlim_max: py2rlim_limit(max, vm)?,
})),
_ => Err(vm.new_value_error("expected a tuple of 2 integers")),
}
}
}
impl ToPyObject for Limits {
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
(self.0.rlim_cur, self.0.rlim_max).to_pyobject(vm)
}
}
fn py2rlim(obj: &Py<PyInt>, vm: &VirtualMachine) -> PyResult<host_resource::rlim_t> {
let value = obj.try_to_primitive::<isize>(vm)?;
if value.is_negative() {
return Err(vm.new_value_error("Cannot convert negative int"));
}
host_resource::rlim_t::try_from(value)
.map_err(|_| vm.new_overflow_error("Python int too large to convert to C rlim_t"))
}
fn py2rlim_limit(obj: &PyObject, vm: &VirtualMachine) -> PyResult<host_resource::rlim_t> {
let int = obj.try_index(vm)?;
if int.as_bigint().is_negative() {
return Err(vm.new_value_error("Cannot convert negative int"));
}
let value = int
.try_to_primitive::<u64>(vm)
.map_err(|_| vm.new_overflow_error("Python int too large to convert to C rlim_t"))?;
host_resource::rlim_t::try_from(value)
.map_err(|_| vm.new_overflow_error("Python int too large to convert to C rlim_t"))
}
#[pyfunction]
fn getrlimit(resource: ArgIndex, vm: &VirtualMachine) -> PyResult<Limits> {
let resource = py2rlim(resource.as_ref(), vm)?;
if resource >= RLIM_NLIMITS as host_resource::rlim_t {
return Err(vm.new_value_error("invalid resource specified"));
}
let rlimit = host_resource::getrlimit(resource).map_err(|_| vm.new_last_errno_error())?;
Ok(Limits(rlimit))
}
#[pyfunction]
fn setrlimit(resource: ArgIndex, limits: Limits, vm: &VirtualMachine) -> PyResult<()> {
let resource = py2rlim(resource.as_ref(), vm)?;
if resource >= RLIM_NLIMITS as host_resource::rlim_t {
return Err(vm.new_value_error("invalid resource specified"));
}
let res = host_resource::setrlimit(resource, limits.0);
res.map_err(|e| match e.kind() {
io::ErrorKind::InvalidInput => {
vm.new_value_error("current limit exceeds maximum limit")
}
io::ErrorKind::PermissionDenied => {
vm.new_value_error("not allowed to raise maximum limit")
}
_ => e.to_pyexception(vm),
})
}
}