Skip to content

Commit 55ff3a5

Browse files
committed
unistd: Add getgroups
1 parent d5a0b33 commit 55ff3a5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/unistd.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,25 @@ pub fn setgid(gid: Gid) -> Result<()> {
941941
Errno::result(res).map(drop)
942942
}
943943

944+
pub fn getgroups() -> Result<Vec<Gid>> {
945+
// First get the number of groups so we can size our Vec
946+
use std::ptr;
947+
let ret = unsafe { libc::getgroups(0, ptr::null_mut()) };
948+
let size = try!(Errno::result(ret));
949+
950+
// Now actually get the groups
951+
let mut groups = Vec::with_capacity(size as usize);
952+
let ret = unsafe { libc::getgroups(size, groups.as_mut_ptr()) };
953+
954+
Errno::result(ret).map(|s| {
955+
// Use the size returned from the second getgroups call: the user could have been removed
956+
// from a group between the two calls and we don't want to incorrectly set the length of
957+
// the Vec and expose uninitialized memory.
958+
unsafe { groups.set_len(s as usize) };
959+
groups.iter().cloned().map(|gid| Gid::from_raw(gid)).collect()
960+
})
961+
}
962+
944963
pub fn setgroups(groups: &[Gid]) -> Result<()> {
945964
cfg_if! {
946965
if #[cfg(any(target_os = "dragonfly",

0 commit comments

Comments
 (0)