commandserver: extract group count from groups()

Summary: The group count will be used by the next change.

Reviewed By: zzl0

Differential Revision: D47143214

fbshipit-source-id: c791e5d4f79304a95ff568d9bd3441c2d4cb00f6
This commit is contained in:
Jun Wu 2023-07-03 22:36:01 -07:00 committed by Facebook GitHub Bot
parent 7e27390f0e
commit f1d8abe62d

View File

@ -85,6 +85,24 @@ pub(crate) fn runtime_dir() -> anyhow::Result<PathBuf> {
Ok(dir)
}
/// Get the number of groups.
fn groups_count() -> usize {
#[cfg(unix)]
{
let mut ngroups: libc::c_int = 0;
if unsafe { libc::getgroups(0, std::ptr::null_mut()) } == -1 {
ngroups = unsafe { libc::getgroups(0, std::ptr::null_mut()) };
if ngroups <= 0 {
return 0;
}
}
return ngroups as usize;
}
#[allow(unreachable_code)]
0
}
/// Get a sorted list of group ids on POSIX.
///
/// If the client and the server have different lists of groups,
@ -92,16 +110,9 @@ pub(crate) fn runtime_dir() -> anyhow::Result<PathBuf> {
pub fn groups() -> Option<Vec<u32>> {
#[cfg(unix)]
{
let mut ngroups: libc::c_int = 0;
if unsafe { libc::getgroups(0, std::ptr::null_mut()) } == -1 {
ngroups = unsafe { libc::getgroups(0, std::ptr::null_mut()) };
if ngroups <= 0 {
return None;
}
}
let mut groups: Vec<libc::gid_t> = vec![0; ngroups as usize];
ngroups = unsafe { libc::getgroups(ngroups, groups.as_mut_ptr()) };
let ngroups = groups_count();
let mut groups: Vec<libc::gid_t> = vec![0; ngroups];
let ngroups = unsafe { libc::getgroups(ngroups as _, groups.as_mut_ptr()) };
if ngroups < 0 {
return None;
}