1 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" A few pieces remain from an earlier version written in
5 .\" 2002 by Walter Harms (walter.harms@informatik.uni-oldenburg.de)
7 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
9 .TH GETGROUPLIST 3 2021-03-22 "Linux man-pages (unreleased)"
11 getgrouplist \- get list of groups to which a user belongs
14 .RI ( libc ", " \-lc )
19 .BI "int getgrouplist(const char *" user ", gid_t " group ,
20 .BI " gid_t *" groups ", int *" ngroups );
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
32 Glibc 2.19 and earlier:
38 function scans the group database (see
40 to obtain the list of groups that
45 of these groups are returned in the array
48 If it was not among the groups defined for
50 in the group database, then
52 is included in the list of groups returned by
54 typically this argument is specified as the group ID from
55 the password record for
60 argument is a value-result argument:
61 on return it always contains the number of groups found for
65 this value may be greater than the number of groups stored in
68 If the number of groups of which
70 is a member is less than or equal to
76 If the user is a member of more than
81 In this case, the value returned in
83 can be used to resize the buffer passed to a further call to
86 This function is present since glibc 2.2.4.
88 For an explanation of the terms used in this section, see
96 Interface Attribute Value
99 T} Thread safety MT-Safe locale
105 This function is nonstandard; it appears on most BSDs.
107 In glibc versions before 2.3.3,
108 the implementation of this function contains a buffer-overrun bug:
109 it returns the complete list of groups for
113 even when the number of groups exceeds
116 The program below displays the group list for the user named in its
117 first command-line argument.
118 The second command-line argument specifies the
120 value to be supplied to
122 The following shell session shows examples of the use of this program:
126 .RB "$" " ./a.out cecilia 0"
127 getgrouplist() returned \-1; ngroups = 3
128 .RB "$" " ./a.out cecilia 3"
144 main(int argc, char *argv[])
152 fprintf(stderr, "Usage: %s <user> <ngroups>\en", argv[0]);
156 ngroups = atoi(argv[2]);
158 groups = malloc(sizeof(*groups) * ngroups);
159 if (groups == NULL) {
164 /* Fetch passwd structure (contains first group ID for user). */
166 pw = getpwnam(argv[1]);
172 /* Retrieve group list. */
174 if (getgrouplist(argv[1], pw\->pw_gid, groups, &ngroups) == \-1) {
175 fprintf(stderr, "getgrouplist() returned \-1; ngroups = %d\en",
180 /* Display list of retrieved groups, along with group names. */
182 fprintf(stderr, "ngroups = %d\en", ngroups);
183 for (int j = 0; j < ngroups; j++) {
184 printf("%d", groups[j]);
185 gr = getgrgid(groups[j]);
187 printf(" (%s)", gr\->gr_name);
198 .BR group_member (3),