man-pages/man2/getgroups.2.html
2021-03-31 01:06:50 +01:00

346 lines
7.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETGROUPS</TITLE>
</HEAD><BODY>
<H1>GETGROUPS</H1>
Section: Linux Programmer's Manual (2)<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">&nbsp;</A>
<H2>NAME</H2>
getgroups, setgroups - get/set list of supplementary group IDs
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<P>
<B>int getgroups(int </B><I>size</I><B>, gid_t </B><I>list</I><B>[]);</B>
<P>
<B>#include &lt;<A HREF="file:///usr/include/grp.h">grp.h</A>&gt;</B>
<P>
<B>int setgroups(size_t </B><I>size</I><B>, const gid_t *</B><I>list</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>setgroups</B>():
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Since&nbsp;glibc&nbsp;2.19:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Glibc&nbsp;2.19&nbsp;and&nbsp;earlier:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_BSD_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<P>
<B>getgroups</B>()
returns the supplementary group IDs of the calling process in
<I>list</I>.
The argument
<I>size</I>
should be set to the maximum number of items that can be stored in the
buffer pointed to by
<I>list</I>.
If the calling process is a member of more than
<I>size</I>
supplementary groups, then an error results.
<P>
It is unspecified whether the effective group ID of the calling process
is included in the returned list.
(Thus, an application should also call
<B><A HREF="/cgi-bin/man/man2html?2+getegid">getegid</A></B>(2)
and add or remove the resulting value.)
<P>
If
<I>size</I>
is zero,
<I>list</I>
is not modified, but the total number of supplementary group IDs for the
process is returned.
This allows the caller to determine the size of a dynamically allocated
<I>list</I>
to be used in a further call to
<B>getgroups</B>().
<P>
<B>setgroups</B>()
sets the supplementary group IDs for the calling process.
Appropriate privileges are required (see the description of the
<B>EPERM</B>
error, below).
The
<I>size</I>
argument specifies the number of supplementary group IDs
in the buffer pointed to by
<I>list</I>.
A process can drop all of its supplementary groups with the call:
<P>
setgroups(0, NULL);
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>getgroups</B>()
returns the number of supplementary group IDs.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<P>
On success,
<B>setgroups</B>()
returns 0.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EFAULT</B>
<DD>
<I>list</I>
has an invalid address.
</DL>
<P>
<B>getgroups</B>()
can additionally fail with the following error:
<DL COMPACT>
<DT id="2"><B>EINVAL</B>
<DD>
<I>size</I>
is less than the number of supplementary group IDs, but is not zero.
</DL>
<P>
<B>setgroups</B>()
can additionally fail with the following errors:
<DL COMPACT>
<DT id="3"><B>EINVAL</B>
<DD>
<I>size</I>
is greater than
<B>NGROUPS_MAX</B>
(32 before Linux 2.6.4; 65536 since Linux 2.6.4).
<DT id="4"><B>ENOMEM</B>
<DD>
Out of memory.
<DT id="5"><B>EPERM</B>
<DD>
The calling process has insufficient privilege
(the caller does not have the
<B>CAP_SETGID</B>
capability in the user namespace in which it resides).
<DT id="6"><B>EPERM</B> (since Linux 3.19)
<DD>
The use of
<B>setgroups</B>()
is denied in this user namespace.
See the description of
<I>/proc/[pid]/setgroups</I>
in
<B><A HREF="/cgi-bin/man/man2html?7+user_namespaces">user_namespaces</A></B>(7).
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>getgroups</B>():
SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
<P>
<B>setgroups</B>():
SVr4, 4.3BSD.
Since
<B>setgroups</B>()
requires privilege, it is not covered by POSIX.1.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
A process can have up to
<B>NGROUPS_MAX</B>
supplementary group IDs
in addition to the effective group ID.
The constant
<B>NGROUPS_MAX</B>
is defined in
<I>&lt;<A HREF="file:///usr/include/limits.h">limits.h</A>&gt;</I>.
The set of supplementary group IDs
is inherited from the parent process, and preserved across an
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<P>
The maximum number of supplementary group IDs can be found at run time using
<B><A HREF="/cgi-bin/man/man2html?3+sysconf">sysconf</A></B>(3):
<P>
long ngroups_max;
ngroups_max = sysconf(_SC_NGROUPS_MAX);
<P>
The maximum return value of
<B>getgroups</B>()
cannot be larger than one more than this value.
Since Linux 2.6.4, the maximum number of supplementary group IDs is also
exposed via the Linux-specific read-only file,
<I>/proc/sys/kernel/ngroups_max</I>.
<P>
The original Linux
<B>getgroups</B>()
system call supported only 16-bit group IDs.
Subsequently, Linux 2.4 added
<B>getgroups32</B>(),
supporting 32-bit IDs.
The glibc
<B>getgroups</B>()
wrapper function transparently deals with the variation across kernel versions.
<A NAME="lbAI">&nbsp;</A>
<H3>C library/kernel differences</H3>
At the kernel level, user IDs and group IDs are a per-thread attribute.
However, POSIX requires that all threads in a process
share the same credentials.
The NPTL threading implementation handles the POSIX requirements by
providing wrapper functions for
the various system calls that change process UIDs and GIDs.
These wrapper functions (including the one for
<B>setgroups</B>())
employ a signal-based technique to ensure
that when one thread changes credentials,
all of the other threads in the process also change their credentials.
For details, see
<B><A HREF="/cgi-bin/man/man2html?7+nptl">nptl</A></B>(7).
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+getgid">getgid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setgid">setgid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+getgrouplist">getgrouplist</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?3+initgroups">initgroups</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7)
<A NAME="lbAK">&nbsp;</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">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="7"><A HREF="#lbAB">NAME</A><DD>
<DT id="8"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="9"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="10"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="11"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="12"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="14"><A HREF="#lbAI">C library/kernel differences</A><DD>
</DL>
<DT id="15"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="16"><A HREF="#lbAK">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:32 GMT, March 31, 2021
</BODY>
</HTML>