GETGRGID(3P)
πΈ GETGRGID(3P) β the POSIX Programmer's Manual, bestie edition πΈ
okay so welcome to the POSIX Programmer's Manual, you're gonna love it here π
π PROLOG π
okay real talk first: this whole page is part of the POSIX Programmer's Manual. The Linux implementation of this interface might do its own little thing (so like, go read the matching Linux manual page for the actual Linux behavior, bestie), or honestly the interface might not even be implemented on Linux at all. no cap. π
β¨ NAME β¨
getgrgid, getgrgid_r β get the group database entry for a group ID π
π¦ SYNOPSIS π¦
#include <grp.h>
struct group *getgrgid(gid_t gid);
int getgrgid_r(gid_t gid, struct group *grp, char *buffer,
size_t bufsize, struct group **result);
π DESCRIPTION π
So getgrgid() goes and searches the group database for an entry that matches the gid. that's the whole mission. it's giving detective π
Quick heads up though: getgrgid() does not need to be thread-safe. it said what it said. π
If you're the kind of application that wants to check for error situations, set errno to 0 before you call getgrgid(). Then if getgrgid() hands you back a null pointer and errno is set to non-zero? yeah, an error happened. that's the vibe check β
Now getgrgid_r() β she's the responsible one π«Ά. She updates the group structure that grp points to, and stores a pointer to that structure at the location that result points to. That structure is gonna contain an entry from the group database with a matching gid. The storage that the group structure references is allocated out of the memory you hand over in the buffer parameter, which is bufsize bytes in size. And a call to sysconf(_SC_GETGR_R_SIZE_MAX) returns either -1 without changing errno, or an initial value that's suggested for the size of this buffer. If there's an error, or if the entry you asked for just isn't there, a null pointer gets returned at the location that result points to. periodt. π
π₯ RETURN VALUE π₯
Upon successful completion, getgrgid() shall return a pointer to a struct group (the structure is defined in <grp.h>) with a matching entry, if one is found. If either the requested entry wasn't found, or an error occurred, getgrgid() shall return a null pointer. And here's the tea: if the requested entry was not found, errno shall not be changed. But on error, errno shall be set to indicate the error. so like, two different flavors of nothing. iconic. π
bestie listen β the application shall not modify the structure that the return value points to, and it shall not modify any of the storage areas pointed to by pointers within the structure. do not touch. π βοΈ
The returned pointer, and the pointers within the structure, might get invalidated, or the structure or the storage areas might get overwritten by a subsequent call to getgrent(), getgrgid(), or getgrnam(). Also the returned pointer, and pointers within the structure, might get invalidated if the calling thread is terminated. so, like, save your data elsewhere, queen πΈ
If successful, the getgrgid_r() function shall return zero; otherwise, an error number shall be returned to indicate the error. zero = slay, non-zero = not slay.
π ERRORS π
The getgrgid() and getgrgid_r() functions may fail if:
EIOβ An I/O error has occurred. πEINTRβ A signal was caught duringgetgrgid(). rude.EMFILEβ All file descriptors available to the process are currently open. every single one. no vacancy. π«ENFILEβ The maximum allowable number of files is currently open in the system. the whole system is full bestie.
The getgrgid_r() function may fail if:
ERANGEβ Insufficient storage was supplied viabufferandbufsizeto contain the data to be referenced by the resulting group structure. your buffer was too smol π₯Ί
The following sections are informative. (translation: the rest is bonus content, not rules. enjoy β¨)
π EXAMPLES π
okay so note that sysconf(_SC_GETGR_R_SIZE_MAX) may return -1 if there is no hard limit on the size of the buffer needed to store all the groups returned. This example shows how an application can allocate a buffer of sufficient size to work with getgrid_r(). π
long int initlen = sysconf(_SC_GETGR_R_SIZE_MAX);
size_t len;
if (initlen == -1)
/* Default initial length. */
len = 1024;
else
len = (size_t) initlen;
struct group result;
struct group *resultp;
char *buffer = malloc(len);
if (buffer == NULL)
...handle error...
int e;
while ((e = getgrgid_r(42, &result, buffer, len, &resultp)) == ERANGE)
{
size_t newlen = 2 * len;
if (newlen < len)
...handle error...
len = newlen;
char *newbuffer = realloc(buffer, len);
if (newbuffer == NULL)
...handle error...
buffer = newbuffer;
}
if (e != 0)
...handle error...
free (buffer);
π Finding an Entry in the Group Database π
The following example uses getgrgid() to search the group database for a group ID that was previously stored in a stat structure, then prints out the group name if it is found. If the group is not found, the program prints the numeric value of the group for the entry. cute and practical, we love her π
#include <sys/types.h>
#include <grp.h>
#include <stdio.h>
...
struct stat statbuf;
struct group *grp;
...
if ((grp = getgrgid(statbuf.st_gid)) != NULL)
printf(" %-8.8s", grp->gr_name);
else
printf(" %-8d", statbuf.st_gid);
...
π§ APPLICATION USAGE π§
The getgrgid_r() function is thread-safe and shall return values in a user-supplied buffer instead of possibly using a static data area that may get overwritten by each call. that's the upgrade arc, honestly π¦
Portable applications should take into account that it's usual for an implementation to return -1 from sysconf() indicating that there is no maximum for _SC_GETGR_R_SIZE_MAX. so don't be dramatic when you see that -1, it's normal π
π RATIONALE π
None.
like. literally none. the rationale said nothing. she's silent. she's mysterious. π
β FUTURE DIRECTIONS β
None.
also nothing. we love a low-effort queen π
π SEE ALSO π
endgrent()getgrnam()sysconf()
The Base Definitions volume of POSIX.1β2017, <grp.h>, <sys_types.h> π
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie π β¨ this one came to you from IEEE/The Open Group, 2017, on the GETGRGID(3P) page β go forth and look up your groups responsibly ππ¦π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms