getgrent, getgrgid, getgrnam, setgrent, or endgrent Subroutine

Purpose

Accesses the basic group information in the user database.

Library

Standard C Library (libc.a)

Syntax

#include <sys/types.h>
#include <grp.h>
struct group *getgrent ( );

struct group *getgrgid (GID)
gid_t  GID;

struct group *getgrnam (Name)
const char * Name;

void setgrent ( );
void endgrent ( );

Description

Attention: The information returned by the getgrent, getgrnam, and getgrgid subroutines is stored in a static area and is overwritten on subsequent calls. You must copy this information to save it.
Attention: These subroutines should not be used with the getgroupattr subroutine. The results are unpredictable.

The setgrent subroutine opens the user database if it is not already open. Then, this subroutine sets the cursor to point to the first group entry in the database.

Attention: The getgrent subroutine is only supported by LOCAL and NIS load modules, not any other LAM authentication module. See the (getgrset Subroutine), (getgroupattr, IDtogroup, nextgroup, or putgroupattr Subroutine), or (getgroupattrs Subroutine) subroutines for use with other LAM authentication modules.

The getgrent, getgrnam, and getgrgid subroutines return information about the requested group. The getgrent subroutine returns the next group in the sequential search. The getgrnam subroutine returns the first group in the database whose name matches that of the Name parameter. The getgrgid subroutine returns the first group in the database whose group ID matches the GID parameter. The endgrent subroutine closes the user database.

Note: An ! (exclamation mark) is written into the gr_passwd field. This field is ignored and is present only for compatibility with older versions of UNIX operating systems.

These subroutines also return the list of user members for the group. Currently, the list is limited to 2000 entries (this could change in the future to where all the entries for the group are returned).

The Group Structure

The group structure is defined in the grp.h file and has the following fields:

Item Description
gr_name Contains the name of the group.
gr_passwd Contains the password of the group.
Note: This field is no longer used.
gr_gid Contains the ID of the group.
gr_mem Contains the members of the group.

If the Network Information Service (NIS) is enabled on the system, these subroutines attempt to retrieve the group information from the NIS authentication server.

Parameters

Item Description
GID Specifies the group ID.
Name Specifies the group name.
Item Description
Group Specifies the basic group information to enter into the user database.

Return Values

If successful, the getgrent, getgrnam, and getgrgid subroutines return a pointer to a valid group structure. Otherwise, a null pointer is returned.

Error Codes

These subroutines fail if one or more of the following are returned:

Item Description
EIO Indicates that an input/output (I/O) error has occurred.
EINTR Indicates that a signal was caught during the getgrnam or getgrgid subroutine.
EMFILE Indicates that the maximum number of file descriptors specified by the OPEN_MAX value are currently open in the calling process.
ENFILE Indicates that the maximum allowable number of files is currently open in the system.

To check an application for error situations, set the errno global variable to a value of 0 before calling the getgrgid subroutine. If the errno global variable is set on return, an error occurred.

File

Item Description
/etc/group Contains basic group attributes.