libbb: move is_in_supplementary_groups() from test to libbb

function                                             old     new   delta
is_in_supplementary_groups                             -      54     +54
nexpr                                                766     721     -45
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 54/-45)              Total: 9 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2024-10-07 01:20:43 +02:00
parent bb5525613e
commit 748b168154
3 changed files with 26 additions and 19 deletions

View file

@ -637,32 +637,14 @@ static int binop(void)
/*return 1; - NOTREACHED */ /*return 1; - NOTREACHED */
} }
static void initialize_group_array(void)
{
group_array = bb_getgroups(&ngroups, NULL);
}
/* Return non-zero if GID is one that we have in our groups list. */ /* Return non-zero if GID is one that we have in our groups list. */
//XXX: FIXME: duplicate of existing libbb function?
// see toplevel TODO file:
// possible code duplication ingroup() and is_a_group_member()
static int is_a_group_member(gid_t gid) static int is_a_group_member(gid_t gid)
{ {
int i;
/* Short-circuit if possible, maybe saving a call to getgroups(). */ /* Short-circuit if possible, maybe saving a call to getgroups(). */
if (gid == getgid() || gid == getegid()) if (gid == getgid() || gid == getegid())
return 1; return 1;
if (ngroups == 0) return is_in_supplementary_groups(&ngroups, &group_array, gid);
initialize_group_array();
/* Search through the list looking for GID. */
for (i = 0; i < ngroups; i++)
if (gid == group_array[i])
return 1;
return 0;
} }
/* /*

View file

@ -1201,6 +1201,11 @@ void die_if_bad_username(const char* name) FAST_FUNC;
* Dies on errors (on Linux, only xrealloc can cause this, not internal getgroups call). * Dies on errors (on Linux, only xrealloc can cause this, not internal getgroups call).
*/ */
gid_t *bb_getgroups(int *ngroups, gid_t *group_array) FAST_FUNC; gid_t *bb_getgroups(int *ngroups, gid_t *group_array) FAST_FUNC;
/*
* True if GID is in our getgroups() result.
* getgroups() is cached in group_array[], to makse successive calls faster.
*/
int FAST_FUNC is_in_supplementary_groups(int *pngroups, gid_t **pgroup_array, gid_t gid);
#if ENABLE_FEATURE_UTMP #if ENABLE_FEATURE_UTMP
void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname); void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);

View file

@ -45,3 +45,23 @@ gid_t* FAST_FUNC bb_getgroups(int *ngroups, gid_t *group_array)
*ngroups = n; *ngroups = n;
return group_array; return group_array;
} }
/* Return non-zero if GID is in our supplementary group list. */
int FAST_FUNC is_in_supplementary_groups(int *pngroups, gid_t **pgroup_array, gid_t gid)
{
int i;
int ngroups;
gid_t *group_array;
if (*pngroups == 0)
*pgroup_array = bb_getgroups(pngroups, NULL);
ngroups = *pngroups;
group_array = *pgroup_array;
/* Search through the list looking for GID. */
for (i = 0; i < ngroups; i++)
if (gid == group_array[i])
return 1;
return 0;
}