Name

getgrouplist — list of groups a user belongs to

Synopsis

#include <grp.h>
int getgrouplist( const char *  user,
  gid_t   group,
  gid_t *  groups,
  int *  ngroups);
[Note] Note
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getgrouplist():
_BSD_SOURCE

DESCRIPTION

The getgrouplist() function scans the group database for all the groups user belongs to. Up to *ngroups group IDs corresponding to these groups are stored in the array groups; the return value from the function is the number of group IDs actually stored. The group group is automatically included in the list of groups returned by getgrouplist().

RETURN VALUE

If *ngroups is smaller than the total number of groups found, then getgrouplist() returns −1. In all cases the actual number of groups is stored in *ngroups.

VERSIONS

This function is present since glibc 2.2.4.

CONFORMING TO

This function is non-standard; it appears on most BSDs.

BUGS

The glibc 2.3.2 implementation of this function is broken: it overwrites memory when the actual number of groups is larger than *ngroups.

EXAMPLE

/* This crashes with glibc 2.3.2 */
#include <stdio.h>
#include <stdlib.h>
#include <grp.h>
#include <pwd.h>

int
main(void)
{
    int i, ng = 0;
    char *user = "who";       /* username here */
    gid_t *groups = NULL;
    struct passwd *pw = getpwnam(user);

    if (pw == NULL)
        exit(EXIT_SUCCESS);

    if (getgrouplist(user, pw−>pw_gid, NULL, &ng) < 0) {
        groups = (gid_t *) malloc(ng * sizeof (gid_t));
        getgrouplist(user, pw−>pw_gid, groups, &ng);
    }

    for (i = 0; i < ng; i++)
        printf("%d\n", groups[i]);

    exit(EXIT_SUCCESS);
}

SEE ALSO

getgroups(2), setgroups(2)

COLOPHON

This page is part of release 2.79 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.


  Copyright 2002 Walter Harms (walter.harmsinformatik.uni-oldenburg.de)
Distributed under GPL
Thanks to glibc info pages

Modified 2003-11-18, aeb: glibc is broken