File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -941,6 +941,25 @@ pub fn setgid(gid: Gid) -> Result<()> {
941
941
Errno :: result ( res) . map ( drop)
942
942
}
943
943
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
+
944
963
pub fn setgroups ( groups : & [ Gid ] ) -> Result < ( ) > {
945
964
cfg_if ! {
946
965
if #[ cfg( any( target_os = "dragonfly" ,
You can’t perform that action at this time.
0 commit comments