These functions operate on the group database which is described in
group(5). Each line of the database is defined by the structure
group found in the include file
<grp.h>:
struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group id */
char **gr_mem; /* group members */
};
The functions
getgrnam() and
getgrgid() search the group database for the given group name pointed to by
name or the group id pointed to by
gid, respectively, returning the first one encountered. Identical group names or group ids may result in undefined behavior.
The
getgrent() function sequentially reads the group database and is intended for programs that wish to step through the complete list of groups.
All three functions will open the group file for reading, if necessary.
The functions
getgrnam_r(),
getgrgid_r(), and
getgrent_r() act like their non re-entrant counterparts respectively, updating the contents of
grp and storing a pointer to that in
result, and returning
0. Storage used by
grp is allocated from
buffer, which is
buflen bytes in size. If the requested entry cannot be found,
result will point to
NULL and
0 will be returned. If an error occurs, a non-zero error number will be returned and
result will point to
NULL. Calling
getgrent_r() from multiple threads will result in each thread reading a disjoint portion of the group database.
The
setgroupent() function opens the file, or rewinds it if it is already open. If
stayopen is non-zero, file descriptors are left open, significantly speeding functions subsequent calls. This functionality is unnecessary for
getgrent() as it doesn't close its file descriptors by default. It should also be noted that it is dangerous for long-running programs to use this functionality as the group file may be updated.
The
setgrent() function is equivalent to
setgroupent() with an argument of zero.
The
endgrent() function closes any open files.