Skip to content

Commit 8152e7e

Browse files
committed
Make Gid/Uid less janky
1 parent b5c1fd9 commit 8152e7e

1 file changed

Lines changed: 35 additions & 80 deletions

File tree

‎vm/src/stdlib/posix.rs‎

Lines changed: 35 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,17 +1111,13 @@ pub mod module {
11111111
}
11121112

11131113
#[pyfunction]
1114-
fn setgid(gid: Option<Gid>, vm: &VirtualMachine) -> PyResult<()> {
1115-
let gid =
1116-
gid.ok_or_else(|| vm.new_errno_error(1, "Operation not permitted".to_string()))?;
1114+
fn setgid(gid: Gid, vm: &VirtualMachine) -> PyResult<()> {
11171115
unistd::setgid(gid).map_err(|err| err.into_pyexception(vm))
11181116
}
11191117

11201118
#[cfg(not(target_os = "redox"))]
11211119
#[pyfunction]
1122-
fn setegid(egid: Option<Gid>, vm: &VirtualMachine) -> PyResult<()> {
1123-
let egid =
1124-
egid.ok_or_else(|| vm.new_errno_error(1, "Operation not permitted".to_string()))?;
1120+
fn setegid(egid: Gid, vm: &VirtualMachine) -> PyResult<()> {
11251121
unistd::setegid(egid).map_err(|err| err.into_pyexception(vm))
11261122
}
11271123

@@ -1139,7 +1135,7 @@ pub mod module {
11391135
.map_err(|err| err.into_pyexception(vm))
11401136
}
11411137

1142-
fn try_from_id(vm: &VirtualMachine, obj: PyObjectRef, typ_name: &str) -> PyResult<Option<u32>> {
1138+
fn try_from_id(vm: &VirtualMachine, obj: PyObjectRef, typ_name: &str) -> PyResult<u32> {
11431139
use std::cmp::Ordering;
11441140
let i = obj
11451141
.try_to_ref::<PyInt>(vm)
@@ -1152,53 +1148,47 @@ pub mod module {
11521148
.try_to_primitive::<i64>(vm)?;
11531149

11541150
match i.cmp(&-1) {
1155-
Ordering::Greater => Ok(Some(i.try_into().map_err(|_| {
1151+
Ordering::Greater => Ok(i.try_into().map_err(|_| {
11561152
vm.new_overflow_error(format!("{typ_name} is larger than maximum"))
1157-
})?)),
1153+
})?),
11581154
Ordering::Less => {
11591155
Err(vm.new_overflow_error(format!("{typ_name} is less than minimum")))
11601156
}
1161-
Ordering::Equal => Ok(None), // -1 means does not change the value
1157+
// -1 means does not change the value
1158+
// In CPython, this is `(uid_t) -1`, rustc gets mad when we try to declare
1159+
// a negative unsigned integer :).
1160+
Ordering::Equal => Ok(-1i32 as u32),
11621161
}
11631162
}
11641163

1165-
impl TryFromObject for Option<Uid> {
1164+
impl TryFromObject for Uid {
11661165
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
1167-
Ok(try_from_id(vm, obj, "uid")?.map(Uid::from_raw))
1166+
try_from_id(vm, obj, "uid").map(Uid::from_raw)
11681167
}
11691168
}
11701169

1171-
impl TryFromObject for Option<Gid> {
1170+
impl TryFromObject for Gid {
11721171
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
1173-
Ok(try_from_id(vm, obj, "gid")?.map(Gid::from_raw))
1172+
try_from_id(vm, obj, "gid").map(Gid::from_raw)
11741173
}
11751174
}
11761175

11771176
#[pyfunction]
1178-
fn setuid(uid: Option<Uid>, vm: &VirtualMachine) -> PyResult<()> {
1179-
let uid =
1180-
uid.ok_or_else(|| vm.new_errno_error(1, "Operation not permitted".to_string()))?;
1181-
unistd::setuid(uid).map_err(|err| err.into_pyexception(vm))
1177+
fn setuid(uid: Uid) -> nix::Result<()> {
1178+
unistd::setuid(uid)
11821179
}
11831180

11841181
#[cfg(not(target_os = "redox"))]
11851182
#[pyfunction]
1186-
fn seteuid(euid: Option<Uid>, vm: &VirtualMachine) -> PyResult<()> {
1187-
let euid =
1188-
euid.ok_or_else(|| vm.new_errno_error(1, "Operation not permitted".to_string()))?;
1189-
unistd::seteuid(euid).map_err(|err| err.into_pyexception(vm))
1183+
fn seteuid(euid: Uid) -> nix::Result<()> {
1184+
unistd::seteuid(euid)
11901185
}
11911186

11921187
#[cfg(not(target_os = "redox"))]
11931188
#[pyfunction]
1194-
fn setreuid(ruid: Option<Uid>, euid: Option<Uid>, vm: &VirtualMachine) -> PyResult<()> {
1195-
if let Some(ruid) = ruid {
1196-
unistd::setuid(ruid).map_err(|err| err.into_pyexception(vm))?;
1197-
}
1198-
if let Some(euid) = euid {
1199-
unistd::seteuid(euid).map_err(|err| err.into_pyexception(vm))?;
1200-
}
1201-
Ok(())
1189+
fn setreuid(ruid: Uid, euid: Uid) -> nix::Result<()> {
1190+
let ret = unsafe { libc::setreuid(ruid.as_raw(), euid.as_raw()) };
1191+
nix::Error::result(ret).map(drop)
12021192
}
12031193

12041194
// cfg from nix
@@ -1209,20 +1199,8 @@ pub mod module {
12091199
target_os = "openbsd"
12101200
))]
12111201
#[pyfunction]
1212-
fn setresuid(
1213-
ruid: Option<Uid>,
1214-
euid: Option<Uid>,
1215-
suid: Option<Uid>,
1216-
vm: &VirtualMachine,
1217-
) -> PyResult<()> {
1218-
let unwrap_or_unchanged =
1219-
|u: Option<Uid>| u.unwrap_or_else(|| Uid::from_raw(libc::uid_t::MAX));
1220-
unistd::setresuid(
1221-
unwrap_or_unchanged(ruid),
1222-
unwrap_or_unchanged(euid),
1223-
unwrap_or_unchanged(suid),
1224-
)
1225-
.map_err(|err| err.into_pyexception(vm))
1202+
fn setresuid(ruid: Uid, euid: Uid, suid: Uid) -> nix::Result<()> {
1203+
unistd::setresuid(ruid, euid, suid)
12261204
}
12271205

12281206
#[cfg(not(target_os = "redox"))]
@@ -1271,8 +1249,8 @@ pub mod module {
12711249
// cfg from nix
12721250
#[cfg(any(target_os = "android", target_os = "linux", target_os = "openbsd"))]
12731251
#[pyfunction]
1274-
fn getresuid(vm: &VirtualMachine) -> PyResult<(u32, u32, u32)> {
1275-
let ret = unistd::getresuid().map_err(|e| e.into_pyexception(vm))?;
1252+
fn getresuid() -> nix::Result<(u32, u32, u32)> {
1253+
let ret = unistd::getresuid()?;
12761254
Ok((
12771255
ret.real.as_raw(),
12781256
ret.effective.as_raw(),
@@ -1283,8 +1261,8 @@ pub mod module {
12831261
// cfg from nix
12841262
#[cfg(any(target_os = "android", target_os = "linux", target_os = "openbsd"))]
12851263
#[pyfunction]
1286-
fn getresgid(vm: &VirtualMachine) -> PyResult<(u32, u32, u32)> {
1287-
let ret = unistd::getresgid().map_err(|e| e.into_pyexception(vm))?;
1264+
fn getresgid() -> nix::Result<(u32, u32, u32)> {
1265+
let ret = unistd::getresgid()?;
12881266
Ok((
12891267
ret.real.as_raw(),
12901268
ret.effective.as_raw(),
@@ -1300,32 +1278,15 @@ pub mod module {
13001278
target_os = "openbsd"
13011279
))]
13021280
#[pyfunction]
1303-
fn setresgid(
1304-
rgid: Option<Gid>,
1305-
egid: Option<Gid>,
1306-
sgid: Option<Gid>,
1307-
vm: &VirtualMachine,
1308-
) -> PyResult<()> {
1309-
let unwrap_or_unchanged =
1310-
|u: Option<Gid>| u.unwrap_or_else(|| Gid::from_raw(libc::gid_t::MAX));
1311-
unistd::setresgid(
1312-
unwrap_or_unchanged(rgid),
1313-
unwrap_or_unchanged(egid),
1314-
unwrap_or_unchanged(sgid),
1315-
)
1316-
.map_err(|err| err.into_pyexception(vm))
1281+
fn setresgid(rgid: Gid, egid: Gid, sgid: Gid, vm: &VirtualMachine) -> PyResult<()> {
1282+
unistd::setresgid(rgid, egid, sgid).map_err(|err| err.into_pyexception(vm))
13171283
}
13181284

13191285
#[cfg(not(target_os = "redox"))]
13201286
#[pyfunction]
1321-
fn setregid(rgid: Option<Gid>, egid: Option<Gid>, vm: &VirtualMachine) -> PyResult<()> {
1322-
if let Some(rgid) = rgid {
1323-
unistd::setgid(rgid).map_err(|err| err.into_pyexception(vm))?;
1324-
}
1325-
if let Some(egid) = egid {
1326-
unistd::setegid(egid).map_err(|err| err.into_pyexception(vm))?;
1327-
}
1328-
Ok(())
1287+
fn setregid(rgid: Gid, egid: Gid) -> nix::Result<()> {
1288+
let ret = unsafe { libc::setregid(rgid.as_raw(), egid.as_raw()) };
1289+
nix::Error::result(ret).map(drop)
13291290
}
13301291

13311292
// cfg from nix
@@ -1336,26 +1297,20 @@ pub mod module {
13361297
target_os = "openbsd"
13371298
))]
13381299
#[pyfunction]
1339-
fn initgroups(user_name: PyStrRef, gid: Option<Gid>, vm: &VirtualMachine) -> PyResult<()> {
1300+
fn initgroups(user_name: PyStrRef, gid: Gid, vm: &VirtualMachine) -> PyResult<()> {
13401301
let user = user_name.to_cstring(vm)?;
1341-
let gid =
1342-
gid.ok_or_else(|| vm.new_errno_error(1, "Operation not permitted".to_string()))?;
13431302
unistd::initgroups(&user, gid).map_err(|err| err.into_pyexception(vm))
13441303
}
13451304

13461305
// cfg from nix
13471306
#[cfg(not(any(target_os = "ios", target_os = "macos", target_os = "redox")))]
13481307
#[pyfunction]
13491308
fn setgroups(
1350-
group_ids: crate::function::ArgIterable<Option<Gid>>,
1309+
group_ids: crate::function::ArgIterable<Gid>,
13511310
vm: &VirtualMachine,
13521311
) -> PyResult<()> {
1353-
let gids = group_ids
1354-
.iter(vm)?
1355-
.collect::<Result<Option<Vec<_>>, _>>()?
1356-
.ok_or_else(|| vm.new_errno_error(1, "Operation not permitted".to_string()))?;
1357-
let ret = unistd::setgroups(&gids);
1358-
ret.map_err(|err| err.into_pyexception(vm))
1312+
let gids = group_ids.iter(vm)?.collect::<Result<Vec<_>, _>>()?;
1313+
unistd::setgroups(&gids).map_err(|err| err.into_pyexception(vm))
13591314
}
13601315

13611316
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]

0 commit comments

Comments
 (0)