From a2321e4b2bd8b7ab1c86ff3f1014c5caf07e2868 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 16 Jan 2024 20:00:57 -0700 Subject: [PATCH] Add initgroups --- src/header/grp/mod.rs | 14 +++++++++++++- src/header/limits/mod.rs | 1 + src/header/unistd/sysconf.rs | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/header/grp/mod.rs b/src/header/grp/mod.rs index 0e1f317d66..05478230ff 100644 --- a/src/header/grp/mod.rs +++ b/src/header/grp/mod.rs @@ -20,7 +20,7 @@ use alloc::{ use crate::{ c_str::CStr, fs::File, - header::{errno, fcntl, string::strlen}, + header::{errno, fcntl, limits, string::strlen, unistd}, io, io::{prelude::*, BufReader, Lines}, platform, @@ -443,3 +443,15 @@ pub unsafe extern "C" fn getgrouplist( grps.len() as c_int } } + +// MT-Safe locale +// Not POSIX +#[no_mangle] +pub unsafe extern "C" fn initgroups(user: *const c_char, gid: gid_t) -> c_int { + let mut groups = [0; limits::NGROUPS_MAX]; + let mut count = groups.len() as c_int; + if getgrouplist(user, gid, groups.as_mut_ptr(), &mut count) < 0 { + return -1; + } + unistd::setgroups(count as size_t, groups.as_ptr()) +} diff --git a/src/header/limits/mod.rs b/src/header/limits/mod.rs index aab89f6ed3..0c08687e8b 100644 --- a/src/header/limits/mod.rs +++ b/src/header/limits/mod.rs @@ -1,3 +1,4 @@ //! limits.h implementation for relibc pub const PATH_MAX: usize = 4096; +pub const NGROUPS_MAX: usize = 65536; diff --git a/src/header/unistd/sysconf.rs b/src/header/unistd/sysconf.rs index c50c53de25..7afdbec815 100644 --- a/src/header/unistd/sysconf.rs +++ b/src/header/unistd/sysconf.rs @@ -1,7 +1,7 @@ use core::convert::TryInto; use crate::{ - header::errno, + header::{errno, limits}, platform::{self, types::*, Pal, Sys}, }; @@ -37,7 +37,7 @@ pub extern "C" fn sysconf(name: c_int) -> c_long { _SC_ARG_MAX => 4096, _SC_CHILD_MAX => 65536, _SC_CLK_TCK => 100, - _SC_NGROUPS_MAX => 65536, + _SC_NGROUPS_MAX => limits::NGROUPS_MAX as c_long, _SC_OPEN_MAX => 1024, _SC_STREAM_MAX => 16, _SC_TZNAME_MAX => -1,