298 lines
8.4 KiB
HTML
298 lines
8.4 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of GETGROUPLIST</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>GETGROUPLIST</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
getgrouplist - get list of groups to which a user belongs
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/grp.h">grp.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>int getgrouplist(const char *</B><I>user</I><B>, gid_t </B><I>group</I><B>,</B>
|
|
|
|
<BR>
|
|
|
|
<B> gid_t *</B><I>groups</I><B>, int *</B><I>ngroups</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
<B>getgrouplist</B>():
|
|
|
|
<BR> Since glibc 2.19:
|
|
<BR> _DEFAULT_SOURCE
|
|
<BR> Glibc 2.19 and earlier:
|
|
<BR> _BSD_SOURCE
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>getgrouplist</B>()
|
|
|
|
function scans the group database (see
|
|
<B><A HREF="/cgi-bin/man/man2html?5+group">group</A></B>(5))
|
|
|
|
to obtain the list of groups that
|
|
<I>user</I>
|
|
|
|
belongs to.
|
|
Up to
|
|
<I>*ngroups</I>
|
|
|
|
of these groups are returned in the array
|
|
<I>groups</I>.
|
|
|
|
<P>
|
|
|
|
If it was not among the groups defined for
|
|
<I>user</I>
|
|
|
|
in the group database, then
|
|
<I>group</I>
|
|
|
|
is included in the list of groups returned by
|
|
<B>getgrouplist</B>();
|
|
|
|
typically this argument is specified as the group ID from
|
|
the password record for
|
|
<I>user</I>.
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>ngroups</I>
|
|
|
|
argument is a value-result argument:
|
|
on return it always contains the number of groups found for
|
|
<I>user</I>,
|
|
|
|
including
|
|
<I>group</I>;
|
|
|
|
this value may be greater than the number of groups stored in
|
|
<I>groups</I>.
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
If the number of groups of which
|
|
<I>user</I>
|
|
|
|
is a member is less than or equal to
|
|
<I>*ngroups</I>,
|
|
|
|
then the value
|
|
<I>*ngroups</I>
|
|
|
|
is returned.
|
|
<P>
|
|
|
|
If the user is a member of more than
|
|
<I>*ngroups</I>
|
|
|
|
groups, then
|
|
<B>getgrouplist</B>()
|
|
|
|
returns -1.
|
|
In this case, the value returned in
|
|
<I>*ngroups</I>
|
|
|
|
can be used to resize the buffer passed to a further call
|
|
<B>getgrouplist</B>().
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
This function is present since glibc 2.2.4.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getgrouplist</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
This function is nonstandard; it appears on most BSDs.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
In glibc versions before 2.3.3,
|
|
the implementation of this function contains a buffer-overrun bug:
|
|
it returns the complete list of groups for
|
|
<I>user</I>
|
|
|
|
in the array
|
|
<I>groups</I>,
|
|
|
|
even when the number of groups exceeds
|
|
<I>*ngroups</I>.
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
<P>
|
|
|
|
The program below displays the group list for the user named in its
|
|
first command-line argument.
|
|
The second command-line argument specifies the
|
|
<I>ngroups</I>
|
|
|
|
value to be supplied to
|
|
<B>getgrouplist</B>().
|
|
|
|
The following shell session shows examples of the use of this program:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ./a.out cecilia 0</B>
|
|
|
|
getgrouplist() returned -1; ngroups = 3
|
|
$<B> ./a.out cecilia 3</B>
|
|
|
|
ngroups = 3
|
|
16 (dialout)
|
|
33 (video)
|
|
100 (users)
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/grp.h">grp.h</A>>
|
|
#include <<A HREF="file:///usr/include/pwd.h">pwd.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int j, ngroups;
|
|
<BR> gid_t *groups;
|
|
<BR> struct passwd *pw;
|
|
<BR> struct group *gr;
|
|
<P>
|
|
<BR> if (argc != 3) {
|
|
<BR> fprintf(stderr, "Usage: %s <user> <ngroups>\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> ngroups = atoi(argv[2]);
|
|
<P>
|
|
<BR> groups = malloc(ngroups * sizeof (gid_t));
|
|
<BR> if (groups == NULL) {
|
|
<BR> perror("malloc");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Fetch passwd structure (contains first group ID for user) */
|
|
<P>
|
|
<BR> pw = getpwnam(argv[1]);
|
|
<BR> if (pw == NULL) {
|
|
<BR> perror("getpwnam");
|
|
<BR> exit(EXIT_SUCCESS);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Retrieve group list */
|
|
<P>
|
|
<BR> if (getgrouplist(argv[1], pw->pw_gid, groups, &ngroups) == -1) {
|
|
<BR> fprintf(stderr, "getgrouplist() returned -1; ngroups = %d\n",
|
|
<BR> ngroups);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Display list of retrieved groups, along with group names */
|
|
<P>
|
|
<BR> fprintf(stderr, "ngroups = %d\n", ngroups);
|
|
<BR> for (j = 0; j < ngroups; j++) {
|
|
<BR> printf("%d", groups[j]);
|
|
<BR> gr = getgrgid(groups[j]);
|
|
<BR> if (gr != NULL)
|
|
<BR> printf(" (%s)", gr->gr_name);
|
|
<BR> printf("\n");
|
|
<BR> }
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getgroups">getgroups</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setgroups">setgroups</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getgrent">getgrent</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+group_member">group_member</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?5+group">group</A></B>(5),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?5+passwd">passwd</A></B>(5)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">VERSIONS</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">ATTRIBUTES</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">BUGS</A><DD>
|
|
<DT id="9"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="10"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="11"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="12"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:44 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|