xref: /freebsd/lib/libsys/getgroups.2 (revision 046c625e9382e17da953767b881aaa782fa73af8)
1.\"-
2.\" SPDX-License-Identifier: BSD-3-Clause
3.\"
4.\" Copyright (c) 1983, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\" Copyright (c) 2025 The FreeBSD Foundation
7.\"
8.\" Portions of this documentation were written by Olivier Certner
9.\" <olce@FreeBSD.org> at Kumacom SARL under sponsorship from the FreeBSD
10.\" Foundation.
11.\"
12.\" Redistribution and use in source and binary forms, with or without
13.\" modification, are permitted provided that the following conditions
14.\" are met:
15.\" 1. Redistributions of source code must retain the above copyright
16.\"    notice, this list of conditions and the following disclaimer.
17.\" 2. Redistributions in binary form must reproduce the above copyright
18.\"    notice, this list of conditions and the following disclaimer in the
19.\"    documentation and/or other materials provided with the distribution.
20.\" 3. Neither the name of the University nor the names of its contributors
21.\"    may be used to endorse or promote products derived from this software
22.\"    without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.Dd September 17, 2025
37.Dt GETGROUPS 2
38.Os
39.Sh NAME
40.Nm getgroups
41.Nd get the calling process' supplementary groups
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In unistd.h
46.Ft int
47.Fn getgroups "int gidsetlen" "gid_t *gidset"
48.Sh DESCRIPTION
49The
50.Fn getgroups
51system call gets the calling process' supplementary groups and stores them in
52the
53.Fa gidset
54array in strictly ascending order.
55The value of
56.Fa gidsetlen
57indicates the maximum number of entries that may be placed in
58.Fa gidset .
59.Pp
60If
61.Fa gidsetlen
62is zero,
63.Fn getgroups
64returns the cardinal of the calling process' supplementary groups set and
65ignores argument
66.Fa gidset .
67.Pp
68No more than
69.Dv {NGROUPS_MAX}
70values may ever be returned.
71The value of
72.Dv {NGROUPS_MAX}
73should be obtained using
74.Xr sysconf 3
75to avoid hard-coding it into the executable.
76.Sh RETURN VALUES
77On success, the
78.Fn getgroups
79system call returns the cardinal of the supplementary groups set.
80It always succeeds if argument
81.Fa gidsetlen
82is zero.
83.Pp
84A value of -1 indicates that an error occurred, and the error
85code is stored in the global variable
86.Va errno .
87.Sh ERRORS
88The possible errors for
89.Fn getgroups
90are:
91.Bl -tag -width Er
92.It Bq Er EINVAL
93The argument
94.Fa gidsetlen
95is smaller than the number of supplementary groups
96.Pq but not zero .
97.It Bq Er EFAULT
98An invalid address was encountered while reading from the
99.Fa gidset
100array.
101.El
102.Sh SEE ALSO
103.Xr setgroups 2 ,
104.Xr initgroups 3 ,
105.Xr sysconf 3
106.Sh STANDARDS
107The
108.Fn getgroups
109system call conforms to
110.St -p1003.1-2008
111with the additional properties that supplementary groups are reported in
112strictly ascending order and the returned size coincides with the cardinal of
113the set.
114.Sh HISTORY
115The
116.Fn getgroups
117system call appeared in
118.Bx 4.2 .
119.Pp
120Since
121.Fx 14.3 ,
122the
123.Fn getgroups
124system call has treated the supplementary groups as a set, reporting them in
125strictly ascending order and returning the cardinal of the set.
126.Pp
127Before
128.Fx 15.0 ,
129the
130.Fn getgroups
131system call would additionally return the effective group ID as the first
132element of the array, before the supplementary groups.
133.Sh SECURITY CONSIDERATIONS
134The
135.Fn getgroups
136system call gets the supplementary groups set in the
137.Fa gidset
138array.
139In particular, as evoked in
140.Sx HISTORY ,
141it does not anymore retrieve the effective GID in the first slot of
142.Fa gidset .
143Programs should not make any assumption about which group is placed in the first
144slot of
145.Fa gidset
146other than it being the supplementary group with smallest GID.
147.Pp
148The effective GID is present in the supplementary groups set if and only if it
149was explicitly set as a supplementary group.
150The function
151.Fn initgroups
152enforces that, while the
153.Fn setgroups
154system call does not.
155Please consult the
156.Xr initgroups 3
157manual page for the rationale.
158