1GETGROUPLIST(3) Linux Programmer's Manual GETGROUPLIST(3)
2
3
4
6 getgrouplist - get list of groups to which a user belongs
7
9 #include <grp.h>
10
11 int getgrouplist(const char *user, gid_t group,
12 gid_t *groups, int *ngroups);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 getgrouplist():
17 Since glibc 2.19:
18 _DEFAULT_SOURCE
19 Glibc 2.19 and earlier:
20 _BSD_SOURCE
21
23 The getgrouplist() function scans the group database (see group(5)) to
24 obtain the list of groups that user belongs to. Up to *ngroups of
25 these groups are returned in the array groups.
26
27 If it was not among the groups defined for user in the group database,
28 then group is included in the list of groups returned by getgrou‐
29 plist(); typically this argument is specified as the group ID from the
30 password record for user.
31
32 The ngroups argument is a value-result argument: on return it always
33 contains the number of groups found for user, including group; this
34 value may be greater than the number of groups stored in groups.
35
37 If the number of groups of which user is a member is less than or equal
38 to *ngroups, then the value *ngroups is returned.
39
40 If the user is a member of more than *ngroups groups, then getgrou‐
41 plist() returns -1. In this case, the value returned in *ngroups can
42 be used to resize the buffer passed to a further call getgrouplist().
43
45 This function is present since glibc 2.2.4.
46
48 For an explanation of the terms used in this section, see at‐
49 tributes(7).
50
51 ┌───────────────┬───────────────┬────────────────┐
52 │Interface │ Attribute │ Value │
53 ├───────────────┼───────────────┼────────────────┤
54 │getgrouplist() │ Thread safety │ MT-Safe locale │
55 └───────────────┴───────────────┴────────────────┘
57 This function is nonstandard; it appears on most BSDs.
58
60 In glibc versions before 2.3.3, the implementation of this function
61 contains a buffer-overrun bug: it returns the complete list of groups
62 for user in the array groups, even when the number of groups exceeds
63 *ngroups.
64
66 The program below displays the group list for the user named in its
67 first command-line argument. The second command-line argument speci‐
68 fies the ngroups value to be supplied to getgrouplist(). The following
69 shell session shows examples of the use of this program:
70
71 $ ./a.out cecilia 0
72 getgrouplist() returned -1; ngroups = 3
73 $ ./a.out cecilia 3
74 ngroups = 3
75 16 (dialout)
76 33 (video)
77 100 (users)
78
79 Program source
80
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <grp.h>
84 #include <pwd.h>
85
86 int
87 main(int argc, char *argv[])
88 {
89 int ngroups;
90 struct passwd *pw;
91 struct group *gr;
92
93 if (argc != 3) {
94 fprintf(stderr, "Usage: %s <user> <ngroups>\n", argv[0]);
95 exit(EXIT_FAILURE);
96 }
97
98 ngroups = atoi(argv[2]);
99
100 gid_t *groups = malloc(sizeof(*groups) * ngroups);
101 if (groups == NULL) {
102 perror("malloc");
103 exit(EXIT_FAILURE);
104 }
105
106 /* Fetch passwd structure (contains first group ID for user) */
107
108 pw = getpwnam(argv[1]);
109 if (pw == NULL) {
110 perror("getpwnam");
111 exit(EXIT_SUCCESS);
112 }
113
114 /* Retrieve group list */
115
116 if (getgrouplist(argv[1], pw->pw_gid, groups, &ngroups) == -1) {
117 fprintf(stderr, "getgrouplist() returned -1; ngroups = %d\n",
118 ngroups);
119 exit(EXIT_FAILURE);
120 }
121
122 /* Display list of retrieved groups, along with group names */
123
124 fprintf(stderr, "ngroups = %d\n", ngroups);
125 for (int j = 0; j < ngroups; j++) {
126 printf("%d", groups[j]);
127 gr = getgrgid(groups[j]);
128 if (gr != NULL)
129 printf(" (%s)", gr->gr_name);
130 printf("\n");
131 }
132
133 exit(EXIT_SUCCESS);
134 }
135
137 getgroups(2), setgroups(2), getgrent(3), group_member(3), group(5),
138 passwd(5)
139
141 This page is part of release 5.10 of the Linux man-pages project. A
142 description of the project, information about reporting bugs, and the
143 latest version of this page, can be found at
144 https://www.kernel.org/doc/man-pages/.
145
146
147
148GNU 2020-11-01 GETGROUPLIST(3)