GETGROUPS(2)
β¨ GETGROUPS(2) β¨
okay bestie, this is the System Calls Manual page for getgroups, and it's genuinely one of the cutest little syscalls to exist π it lives in section 2 of the manual, iykyk.
π NAME π
getgroups β get group access list
π LIBRARY π
Standard C Library (libc, -lc) β she's in libc, bestie, no extra linking drama π«Ά
πΈ SYNOPSIS πΈ
#include <unistd.h>
int
getgroups(int gidsetlen, gid_t *gidset);
π DESCRIPTION π
so, the getgroups() system call grabs the current group access list of the user process and drops it into the array gidset. that's the whole vibe. π
the gidsetlen argument is basically saying "bestie, here's how many entries you're allowed to put in gidset" β it's the capacity, not a request. and then getgroups() comes back and tells you the actual number of groups it returned in gidset. not the capacity, the real count. periodt.
and here's the range, no cap: at least one and as many as {NGROUPS_MAX}+1 values may be returned. so like, it's never zero, and it can go one over {NGROUPS_MAX}. iconic behavior honestly. β¨
now the fun part: if gidsetlen is zero, getgroups() just returns the number of supplementary group IDs chillin' with the calling process, and it does not touch the array that gidset points to. so you can ask "how many groups do I have?" without committing to a buffer yet. main character move. π
heads up though β the value of{NGROUPS_MAX}should be obtained usingsysconf(3), so please please please don't hard-code it into your executable. that's the one thing the manpage is literally begging you not to do π
π₯ RETURN VALUES π₯
a successful call returns the number of groups in the group set. slay. β
a value of -1 means something went wrong π and the error code gets stashed in the global variable errno.
π ERRORS π
here's the possible error list for getgroups(), bestie β it's short, we love a low-drama syscall:
[EINVAL]β the argumentgidsetlenis smaller than the number of groups in the group set. your buffer said "I can hold this much" and the group set said "no you cannot" π[EFAULT]β the argumentgidsetspecifies an invalid address. that pointer is giving nothing. π«
π¦ SEE ALSO π¦
go read those too, they're all in the same friend group π«Ά
π STANDARDS π
the getgroups() system call conforms to IEEE Std 1003.1-2008 ("POSIX.1") β certified, standard, no notes. π
β HISTORY β
the getgroups() system call appeared in 4.2BSD. an elder. a legend. we respect her. πΈ
and that's the page, bestie!! this one's from the Debian folks, dated January 21, 2011, so shoutout to them for keeping the group list lore alive πβ¨ love u, go call sysconf(3) instead of hard-coding things, mwah π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms