| 170 | |
| 171 | #[pyfunction] |
| 172 | fn setrlimit(resource: i32, limits: Limits, vm: &VirtualMachine) -> PyResult<()> { |
| 173 | #[allow(clippy::unnecessary_cast)] |
| 174 | if resource < 0 || resource >= RLIM_NLIMITS as i32 { |
| 175 | return Err(vm.new_value_error("invalid resource specified")); |
| 176 | } |
| 177 | let res = unsafe { |
| 178 | if libc::setrlimit(resource as _, &limits.0) == -1 { |
| 179 | Err(io::Error::last_os_error()) |
| 180 | } else { |
| 181 | Ok(()) |
| 182 | } |
| 183 | }; |
| 184 | res.map_err(|e| match e.kind() { |
| 185 | io::ErrorKind::InvalidInput => { |
| 186 | vm.new_value_error("current limit exceeds maximum limit") |
| 187 | } |
| 188 | io::ErrorKind::PermissionDenied => { |
| 189 | vm.new_value_error("not allowed to raise maximum limit") |
| 190 | } |
| 191 | _ => e.to_pyexception(vm), |
| 192 | }) |
| 193 | } |
| 194 | } |