Skip to content

libc/grp: add getgrouplist()#19183

Open
xiaoxiang781216 wants to merge 1 commit into
apache:masterfrom
xiaoxiang781216:upstream-getgrouplist
Open

libc/grp: add getgrouplist()#19183
xiaoxiang781216 wants to merge 1 commit into
apache:masterfrom
xiaoxiang781216:upstream-getgrouplist

Conversation

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

Summary

Add getgrouplist() to the C library. It scans the group database for
a user's supplementary groups and always reports the primary group
(passed by the caller) first.

  • With CONFIG_LIBC_GROUP_FILE, the group file is scanned and every group
    whose member list contains the user is appended, skipping the primary
    group to avoid duplicates.
  • Without CONFIG_LIBC_GROUP_FILE, only the primary group is returned,
    since no membership information is available.
  • *ngroups is updated with the number of groups found; when the caller
    buffer is too small the function returns -1 with *ngroups set to the
    required count, following glibc.

The group file is read through lib_get_tempbuffer()/lib_put_tempbuffer()
to avoid a heap allocation on every lookup.

Impact

New GNU/BSD API declared in <grp.h>. No change to existing behavior or
build for code that does not call getgrouplist(). Helps porting
third-party software that expects getgrouplist() to be available.

Testing

Built the sim:nsh configuration on Linux (x86_64, GCC) with
CONFIG_LIBC_GROUP_FILE both enabled and disabled, and exercised
getgrouplist() against a crafted /etc/group with a user in multiple
groups (correct count and gid list; -1 with required count on a
too-small buffer). checkpatch.sh passes on the changed files.

Add getgrouplist() to the C library.  It scans the group database for
a user's supplementary groups and always reports the primary group
first.  Without CONFIG_LIBC_GROUP_FILE only the primary group is
returned, since no membership information is available.

The group file is read through lib_get_tempbuffer()/lib_put_tempbuffer()
to avoid a heap allocation on every lookup.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Jun 20, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant