xref: /freebsd/lib/libsys/getgroups.2 (revision 1ddff51060ad759e35dcc4716b0bdcdb40255862)
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 October 10, 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 ,
111not reporting the effective group ID.
112.Sh HISTORY
113The
114.Fn getgroups
115system call appeared in
116.Bx 4.2 .
117.Pp
118Since
119.Fx 14.3 ,
120the
121.Fn getgroups
122system call has been reporting the supplementary groups in strictly ascending
123order.
124.Pp
125Before
126.Fx 15.0 ,
127the
128.Fn getgroups
129system call would additionally return the effective group ID as the first
130element of the array, before the supplementary groups.
131.Sh SECURITY CONSIDERATIONS
132The
133.Fn getgroups
134system call gets the supplementary groups set in the
135.Fa gidset
136array.
137In particular, as evoked in
138.Sx HISTORY ,
139it does not anymore retrieve the effective group ID in the first slot of
140.Fa gidset .
141Programs that process this slot in a specific way must be modified to obtain the
142effective group ID through other means, such as a call to
143.Xr getegid 2 .
144.Pp
145The effective group ID is present in the supplementary groups set if and only if
146it was explicitly set as a supplementary group.
147The function
148.Fn initgroups
149enforces that, while the
150.Fn setgroups
151system call does not.
152Please consult the
153.Xr initgroups 3
154manual page for the rationale.
155