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(): _BSD_SOURCE
17
19 The getgrouplist() function scans the group database (see group(5)) to
20 obtain the list of groups that user belongs to. Up to *ngroups of
21 these groups are returned in the array groups.
22
23 If it was not among the groups defined for user in the group database,
24 then group is included in the list of groups returned by getgrou‐
25 plist(); typically this argument is specified as the group ID from the
26 password record for user.
27
28 The ngroups argument is a value-result argument: on return it always
29 contains the number of groups found for user, including group; this
30 value may be greater than the number of groups stored in groups.
31
33 If the number of groups of which user is a member is less than or equal
34 to *ngroups, then the value *ngroups is returned.
35
36 If the user is a member of more than *ngroups groups, then getgrou‐
37 plist() returns -1. In this case the value returned in *ngroups can be
38 used to resize the buffer passed to a further call getgrouplist().
39
41 This function is present since glibc 2.2.4.
42
44 This function is nonstandard; it appears on most BSDs.
45
47 In glibc versions before 2.3.3, the implementation of this function
48 contains a buffer-overrun bug: it returns the complete list of groups
49 for user in the array groups, even when the number of groups exceeds
50 *ngroups.
51
53 The program below displays the group list for the user named in its
54 first command-line argument. The second command-line argument speci‐
55 fies the ngroups value to be supplied to getgrouplist(). The following
56 shell session shows examples of the use of this program:
57
58 $ ./a.out cecilia 0
59 getgrouplist() returned -1; ngroups = 3
60 $ ./a.out cecilia 3
61 ngroups = 3
62 16 (dialout)
63 33 (video)
64 100 (users)
65
66 Program source
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <grp.h>
71 #include <pwd.h>
72
73 int
74 main(int argc, char *argv[])
75 {
76 int j, ngroups;
77 gid_t *groups;
78 struct passwd *pw;
79 struct group *gr;
80
81 if (argc != 3) {
82 fprintf(stderr, "Usage: %s <user> <ngroups>\n", argv[0]);
83 exit(EXIT_FAILURE);
84 }
85
86 ngroups = atoi(argv[2]);
87
88 groups = malloc(ngroups * sizeof (gid_t));
89 if (groups == NULL) {
90 perror("malloc");
91 exit(EXIT_FAILURE);
92 }
93
94 /* Fetch passwd structure (contains first group ID for user) */
95
96 pw = getpwnam(argv[1]);
97 if (pw == NULL) {
98 perror("getpwnam");
99 exit(EXIT_SUCCESS);
100 }
101
102 /* Retrieve group list */
103
104 if (getgrouplist(argv[1], pw->pw_gid, groups, &ngroups) == -1) {
105 fprintf(stderr, "getgrouplist() returned -1; ngroups = %d\n",
106 ngroups);
107 exit(EXIT_FAILURE);
108 }
109
110 /* Display list of retrieved groups, along with group names */
111
112 fprintf(stderr, "ngroups = %d\n", ngroups);
113 for (j = 0; j < ngroups; j++) {
114 printf("%d", groups[j]);
115 gr = getgrgid(groups[j]);
116 if (gr != NULL)
117 printf(" (%s)", gr->gr_name);
118 printf("\n");
119 }
120
121 exit(EXIT_SUCCESS);
122 }
123
125 getgroups(2), setgroups(2), getgrent(3), group(5), passwd(5)
126
128 This page is part of release 3.53 of the Linux man-pages project. A
129 description of the project, and information about reporting bugs, can
130 be found at http://www.kernel.org/doc/man-pages/.
131
132
133
134GNU 2008-07-03 GETGROUPLIST(3)