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 during getgrgid(). 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 via buffer and bufsize to 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> πŸ“š


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