Use rustix for setgroups (#1334)

This commit is contained in:
LoveSy 2024-02-01 23:35:42 +08:00 committed by GitHub
parent f80769a82a
commit f80d0764b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,5 @@
use anyhow::{Ok, Result};
use core::slice::SlicePattern;
use getopts::Options;
use std::env;
#[cfg(unix)]
@ -38,14 +39,15 @@ fn print_usage(program: &str, opts: Options) {
}
fn set_identity(uid: u32, gid: u32, groups: &[u32]) {
#[cfg(any(target_os = "linux", target_os = "android"))]
unsafe {
if !groups.is_empty() {
libc::setgroups(groups.len(), groups.as_ptr());
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
{
rustix::process::set_groups(
groups
.iter()
.map(|g| unsafe { Gid::from_raw(*g) })
.collect::<Vec<_>>()
.as_slice(),
);
let gid = unsafe { Gid::from_raw(gid) };
let uid = unsafe { Uid::from_raw(uid) };
set_thread_res_gid(gid, gid, gid).ok();