Skip to content

Commit b03b892

Browse files
authored
support setgroups in os module
1 parent babaeba commit b03b892

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

vm/src/stdlib/os.rs

+29
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ use crate::pyobject::{
4141
};
4242
use crate::vm::VirtualMachine;
4343

44+
// cfg from nix
45+
#[cfg(any(
46+
target_os = "android",
47+
target_os = "freebsd",
48+
target_os = "linux",
49+
target_os = "openbsd"
50+
))]
51+
use crate::pyobject::PyIterable;
52+
4453
#[cfg(windows)]
4554
pub const MODULE_NAME: &str = "nt";
4655
#[cfg(not(windows))]
@@ -1537,6 +1546,25 @@ fn os_initgroups(user_name: PyStringRef, gid: u32, vm: &VirtualMachine) -> PyRes
15371546
unistd::initgroups(&user, gid).map_err(|err| convert_nix_error(vm, err))
15381547
}
15391548

1549+
// cfg from nix
1550+
#[cfg(any(
1551+
target_os = "android",
1552+
target_os = "freebsd",
1553+
target_os = "linux",
1554+
target_os = "openbsd"
1555+
))]
1556+
fn os_setgroups(group_ids: PyIterable<u32>, vm: &VirtualMachine) -> PyResult<()> {
1557+
let gids = group_ids
1558+
.iter(vm)?
1559+
.map(|entry| match entry {
1560+
Ok(id) => Ok(unistd::Gid::from_raw(id)),
1561+
Err(err) => Err(err),
1562+
})
1563+
.collect::<Result<Vec<_>, _>>()?;
1564+
let ret = unistd::setgroups(&gids);
1565+
ret.map_err(|err| convert_nix_error(vm, err))
1566+
}
1567+
15401568
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
15411569
let ctx = &vm.ctx;
15421570

@@ -1772,6 +1800,7 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) {
17721800
"getresgid" => ctx.new_function(os_getresgid),
17731801
"setregid" => ctx.new_function(os_setregid),
17741802
"initgroups" => ctx.new_function(os_initgroups),
1803+
"setgroups" => ctx.new_function(os_setgroups),
17751804
});
17761805

17771806
// cfg taken from nix

0 commit comments

Comments
 (0)