Skip to content

Commit 863addd

Browse files
committed
unistd: groups: Respond to minor PR feedback items
1 parent 55ece25 commit 863addd

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/unistd.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -995,17 +995,18 @@ pub fn getgroups() -> Result<Vec<Gid>> {
995995
// First get the number of groups so we can size our Vec
996996
use std::ptr;
997997
let ret = unsafe { libc::getgroups(0, ptr::null_mut()) };
998-
let mut size = Errno::result(ret)?;
999998

1000999
// Now actually get the groups. We try multiple times in case the number of
10011000
// groups has changed since the first call to getgroups() and the buffer is
1002-
// now too small
1003-
let mut groups = Vec::<Gid>::with_capacity(size as usize);
1001+
// now too small.
1002+
let mut groups = Vec::<Gid>::with_capacity(Errno::result(ret)? as usize);
10041003
loop {
10051004
// FIXME: On the platforms we currently support, the `Gid` struct has
10061005
// the same representation in memory as a bare `gid_t`. This is not
10071006
// necessarily the case on all Rust platforms, though. See RFC 1785.
1008-
let ret = unsafe { libc::getgroups(size, groups.as_mut_ptr() as *mut gid_t) };
1007+
let ret = unsafe {
1008+
libc::getgroups(groups.capacity() as c_int, groups.as_mut_ptr() as *mut gid_t)
1009+
};
10091010

10101011
match Errno::result(ret) {
10111012
Ok(s) => {
@@ -1018,7 +1019,6 @@ pub fn getgroups() -> Result<Vec<Gid>> {
10181019
let cap = groups.capacity();
10191020
unsafe { groups.set_len(cap) };
10201021
groups.reserve(1);
1021-
size = groups.capacity() as c_int;
10221022
},
10231023
Err(e) => return Err(e)
10241024
}
@@ -1070,9 +1070,10 @@ pub fn setgroups(groups: &[Gid]) -> Result<()> {
10701070
Errno::result(res).map(|_| ())
10711071
}
10721072

1073-
/// Calculate the supplementary group access list. Gets the group IDs of all
1074-
/// groups that `user` is a member of. The additional group `group` is also
1075-
/// added to the list.
1073+
/// Calculate the supplementary group access list.
1074+
///
1075+
/// Gets the group IDs of all groups that `user` is a member of. The additional
1076+
/// group `group` is also added to the list.
10761077
///
10771078
/// [Further reading](http://man7.org/linux/man-pages/man3/getgrouplist.3.html)
10781079
///
@@ -1108,6 +1109,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
11081109
groups.as_mut_ptr() as *mut getgrouplist_group_t,
11091110
&mut ngroups)
11101111
};
1112+
11111113
// BSD systems only return 0 or -1, Linux returns ngroups on success.
11121114
if ret >= 0 {
11131115
unsafe { groups.set_len(ngroups as usize) };
@@ -1134,9 +1136,11 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
11341136
}
11351137
}
11361138

1137-
/// Initialize the supplementary group access list. Sets the supplementary
1138-
/// group IDs for the calling process using all groups that `user` is a member
1139-
/// of. The additional group `group` is also added to the list.
1139+
/// Initialize the supplementary group access list.
1140+
///
1141+
/// Sets the supplementary group IDs for the calling process using all groups
1142+
/// that `user` is a member of. The additional group `group` is also added to
1143+
/// the list.
11401144
///
11411145
/// [Further reading](http://man7.org/linux/man-pages/man3/initgroups.3.html)
11421146
///
@@ -1146,7 +1150,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
11461150
/// another user. For example, given the user `www-data`, we could look up the
11471151
/// UID and GID for the user in the system's password database (usually found
11481152
/// in `/etc/passwd`). If the `www-data` user's UID and GID were `33` and `33`,
1149-
/// respectively, one could switch user as follows:
1153+
/// respectively, one could switch the user as follows:
11501154
/// ```
11511155
/// let user = CString::new("www-data").unwrap();
11521156
/// let uid = Uid::from_raw(33);

test/test_unistd.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use nix::unistd::*;
44
use nix::unistd::ForkResult::*;
55
use nix::sys::wait::*;
66
use nix::sys::stat;
7-
use std::{env, iter};
7+
use std::{self, env, iter};
88
use std::ffi::CString;
99
use std::fs::File;
1010
use std::io::Write;
@@ -110,11 +110,10 @@ mod linux_android {
110110
fn test_setgroups() {
111111
// Skip this test when not run as root as `setgroups()` requires root.
112112
if !Uid::current().is_root() {
113-
use std::io;
114-
let stderr = io::stderr();
113+
let stderr = std::io::stderr();
115114
let mut handle = stderr.lock();
116115
writeln!(handle, "test_setgroups requires root privileges. Skipping test.").unwrap();
117-
return
116+
return;
118117
}
119118

120119
#[allow(unused_variables)]
@@ -141,11 +140,10 @@ fn test_initgroups() {
141140
// Skip this test when not run as root as `initgroups()` and `setgroups()`
142141
// require root.
143142
if !Uid::current().is_root() {
144-
use std::io;
145-
let stderr = io::stderr();
143+
let stderr = std::io::stderr();
146144
let mut handle = stderr.lock();
147145
writeln!(handle, "test_initgroups requires root privileges. Skipping test.").unwrap();
148-
return
146+
return;
149147
}
150148

151149
#[allow(unused_variables)]
@@ -157,7 +155,8 @@ fn test_initgroups() {
157155
// It doesn't matter if the root user is not called "root" or if a user
158156
// called "root" doesn't exist. We are just checking that the extra,
159157
// made-up group, `123`, is set.
160-
// FIXME: This only tests half of initgroups' functionality.
158+
// FIXME: Test the other half of initgroups' functionality: whether the
159+
// groups that the user belongs to are also set.
161160
let user = CString::new("root").unwrap();
162161
let group = Gid::from_raw(123);
163162
let group_list = getgrouplist(&user, group).unwrap();

0 commit comments

Comments
 (0)