Skip to content

Commit 6266081

Browse files
author
Toan Phan
authored
Add os.setreuid and os.setresuid methods
1 parent 970fa4b commit 6266081

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

tests/snippets/stdlib_os.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ def __exit__(self, exc_type, exc_val, exc_tb):
293293
assert_raises(PermissionError, lambda: os.setpgid(os.getpid(), 42))
294294
assert_raises(PermissionError, lambda: os.setuid(42))
295295
assert_raises(PermissionError, lambda: os.seteuid(42))
296+
assert_raises(PermissionError, lambda: os.setreuid(42, 42))
297+
assert_raises(PermissionError, lambda: os.setresuid(42, 42, 42))
296298

297299
# pty
298300
a, b = os.openpty()

vm/src/stdlib/os.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,28 @@ fn os_seteuid(euid: u32, vm: &VirtualMachine) -> PyResult<()> {
12471247
unistd::seteuid(Uid::from_raw(euid)).map_err(|err| convert_nix_error(vm, err))
12481248
}
12491249

1250+
#[cfg(all(unix, not(target_os = "redox")))]
1251+
fn os_setreuid(ruid: u32, euid: u32, vm: &VirtualMachine) -> PyResult<()> {
1252+
unistd::setuid(Uid::from_raw(ruid)).map_err(|err| convert_nix_error(vm, err))?;
1253+
unistd::seteuid(Uid::from_raw(euid)).map_err(|err| convert_nix_error(vm, err))
1254+
}
1255+
1256+
// cfg from nix
1257+
#[cfg(any(
1258+
target_os = "android",
1259+
target_os = "freebsd",
1260+
target_os = "linux",
1261+
target_os = "openbsd"
1262+
))]
1263+
fn os_setresuid(ruid: u32, euid: u32, suid: u32, vm: &VirtualMachine) -> PyResult<()> {
1264+
unistd::setresuid(
1265+
Uid::from_raw(ruid),
1266+
Uid::from_raw(euid),
1267+
Uid::from_raw(suid),
1268+
)
1269+
.map_err(|err| convert_nix_error(vm, err))
1270+
}
1271+
12501272
#[cfg(all(unix, not(target_os = "redox")))]
12511273
pub fn os_openpty(vm: &VirtualMachine) -> PyResult {
12521274
match openpty(None, None) {
@@ -1622,12 +1644,24 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) {
16221644
"setsid" => ctx.new_function(os_setsid),
16231645
"setegid" => ctx.new_function(os_setegid),
16241646
"seteuid" => ctx.new_function(os_seteuid),
1647+
"setreuid" => ctx.new_function(os_setreuid),
16251648
"openpty" => ctx.new_function(os_openpty),
16261649
"O_DSYNC" => ctx.new_int(libc::O_DSYNC),
16271650
"O_NDELAY" => ctx.new_int(libc::O_NDELAY),
16281651
"O_NOCTTY" => ctx.new_int(libc::O_NOCTTY),
16291652
});
16301653

1654+
// cfg taken from nix
1655+
#[cfg(any(
1656+
target_os = "android",
1657+
target_os = "freebsd",
1658+
target_os = "linux",
1659+
target_os = "openbsd"
1660+
))]
1661+
extend_module!(vm, module, {
1662+
"setresuid" => ctx.new_function(os_setresuid),
1663+
});
1664+
16311665
// cfg taken from nix
16321666
#[cfg(any(
16331667
target_os = "dragonfly",

0 commit comments

Comments
 (0)