1GETGROUPS(2)               Linux Programmer's Manual              GETGROUPS(2)
2
3
4

NAME

6       getgroups, setgroups - get/set list of supplementary group IDs
7

SYNOPSIS

9       #include <unistd.h>
10
11       int getgroups(int size, gid_t list[]);
12
13       #include <grp.h>
14
15       int setgroups(size_t size, const gid_t *list);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       setgroups():
20           Since glibc 2.19:
21               _DEFAULT_SOURCE
22           Glibc 2.19 and earlier:
23               _BSD_SOURCE
24

DESCRIPTION

26       getgroups()  returns the supplementary group IDs of the calling process
27       in list.  The argument size should be set  to  the  maximum  number  of
28       items  that  can  be  stored  in the buffer pointed to by list.  If the
29       calling process is a member of more  than  size  supplementary  groups,
30       then an error results.
31
32       It is unspecified whether the effective group ID of the calling process
33       is included in the returned list.  (Thus, an  application  should  also
34       call getegid(2) and add or remove the resulting value.)
35
36       If  size is zero, list is not modified, but the total number of supple‐
37       mentary group IDs for the process is returned.  This allows the  caller
38       to  determine  the size of a dynamically allocated list to be used in a
39       further call to getgroups().
40
41       setgroups() sets the supplementary group IDs for the  calling  process.
42       Appropriate  privileges  are required (see the description of the EPERM
43       error, below).  The size argument specifies the number of supplementary
44       group  IDs in the buffer pointed to by list.  A process can drop all of
45       its supplementary groups with the call:
46
47           setgroups(0, NULL);
48

RETURN VALUE

50       On success, getgroups() returns the number of supplementary group  IDs.
51       On error, -1 is returned, and errno is set to indicate the error.
52
53       On success, setgroups() returns 0.  On error, -1 is returned, and errno
54       is set to indicate the error.
55

ERRORS

57       EFAULT list has an invalid address.
58
59       getgroups() can additionally fail with the following error:
60
61       EINVAL size is less than the number of supplementary group IDs, but  is
62              not zero.
63
64       setgroups() can additionally fail with the following errors:
65
66       EINVAL size  is  greater than NGROUPS_MAX (32 before Linux 2.6.4; 65536
67              since Linux 2.6.4).
68
69       ENOMEM Out of memory.
70
71       EPERM  The calling process has insufficient privilege (the caller  does
72              not  have  the  CAP_SETGID  capability  in the user namespace in
73              which it resides).
74
75       EPERM (since Linux 3.19)
76              The use of setgroups() is denied in this  user  namespace.   See
77              the description of /proc/[pid]/setgroups in user_namespaces(7).
78

CONFORMING TO

80       getgroups(): SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
81
82       setgroups(): SVr4, 4.3BSD.  Since setgroups() requires privilege, it is
83       not covered by POSIX.1.
84

NOTES

86       A process can have up to NGROUPS_MAX supplementary group IDs  in  addi‐
87       tion to the effective group ID.  The constant NGROUPS_MAX is defined in
88       <limits.h>.  The set of supplementary group IDs is inherited  from  the
89       parent process, and preserved across an execve(2).
90
91       The  maximum number of supplementary group IDs can be found at run time
92       using sysconf(3):
93
94           long ngroups_max;
95           ngroups_max = sysconf(_SC_NGROUPS_MAX);
96
97       The maximum return value of getgroups() cannot be larger than one  more
98       than  this  value.  Since Linux 2.6.4, the maximum number of supplemen‐
99       tary group IDs is also exposed via the Linux-specific  read-only  file,
100       /proc/sys/kernel/ngroups_max.
101
102       The  original Linux getgroups() system call supported only 16-bit group
103       IDs.  Subsequently, Linux 2.4 added  getgroups32(),  supporting  32-bit
104       IDs.   The  glibc getgroups() wrapper function transparently deals with
105       the variation across kernel versions.
106
107   C library/kernel differences
108       At the kernel level, user IDs and group IDs are a per-thread attribute.
109       However,  POSIX  requires  that all threads in a process share the same
110       credentials.  The NPTL threading implementation handles the  POSIX  re‐
111       quirements  by providing wrapper functions for the various system calls
112       that change process UIDs and GIDs.  These wrapper functions  (including
113       the one for setgroups()) employ a signal-based technique to ensure that
114       when one thread changes credentials, all of the other  threads  in  the
115       process also change their credentials.  For details, see nptl(7).
116

SEE ALSO

118       getgid(2),  setgid(2), getgrouplist(3), group_member(3), initgroups(3),
119       capabilities(7), credentials(7)
120

COLOPHON

122       This page is part of release 5.13 of the Linux  man-pages  project.   A
123       description  of  the project, information about reporting bugs, and the
124       latest    version    of    this    page,    can     be     found     at
125       https://www.kernel.org/doc/man-pages/.
126
127
128
129Linux                             2021-03-22                      GETGROUPS(2)
Impressum