xref: /illumos-gate/usr/src/man/man3c/getgrnam.3c (revision e8d3efa1c56e5f2b5368600a2baeb7b1d54a07f8)
1.\"
2.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3.\" permission to reproduce portions of its copyrighted documentation.
4.\" Original documentation from The Open Group can be obtained online at
5.\" http://www.opengroup.org/bookstore/.
6.\"
7.\" The Institute of Electrical and Electronics Engineers and The Open
8.\" Group, have given us permission to reprint portions of their
9.\" documentation.
10.\"
11.\" In the following statement, the phrase ``this text'' refers to portions
12.\" of the system documentation.
13.\"
14.\" Portions of this text are reprinted and reproduced in electronic form
15.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16.\" Standard for Information Technology -- Portable Operating System
17.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20.\" between these versions and the original IEEE and The Open Group
21.\" Standard, the original IEEE and The Open Group Standard is the referee
22.\" document.  The original Standard can be obtained online at
23.\" http://www.opengroup.org/unix/online.html.
24.\"
25.\" This notice shall appear on any product containing this material.
26.\"
27.\" The contents of this file are subject to the terms of the
28.\" Common Development and Distribution License (the "License").
29.\" You may not use this file except in compliance with the License.
30.\"
31.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32.\" or http://www.opensolaris.org/os/licensing.
33.\" See the License for the specific language governing permissions
34.\" and limitations under the License.
35.\"
36.\" When distributing Covered Code, include this CDDL HEADER in each
37.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38.\" If applicable, add the following below this CDDL HEADER, with the
39.\" fields enclosed by brackets "[]" replaced with your own identifying
40.\" information: Portions Copyright [yyyy] [name of copyright owner]
41.\"
42.\"
43.\" Copyright 1989 AT&T
44.\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
45.\" Portions Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved.
46.\" Copyright 2026 Oxide Computer Company
47.\"
48.Dd April 16, 2026
49.Dt GETGRNAM 3C
50.Os
51.Sh NAME
52.Nm getgrnam ,
53.Nm getgrnam_r ,
54.Nm getgrent ,
55.Nm getgrent_r ,
56.Nm getgrgid ,
57.Nm getgrgid_r ,
58.Nm setgrent ,
59.Nm endgrent ,
60.Nm fgetgrent ,
61.Nm fgetgrent_r
62.Nd group database entry functions
63.Sh SYNOPSIS
64.In grp.h
65.Ft "struct group *"
66.Fo getgrnam
67.Fa "const char *name"
68.Fc
69.Ft "struct group *"
70.Fo getgrnam_r
71.Fa "const char *name"
72.Fa "struct group *grp"
73.Fa "char *buffer"
74.Fa "int bufsize"
75.Fc
76.Ft "struct group *"
77.Fo getgrent
78.Fa "void"
79.Fc
80.Ft "struct group *"
81.Fo getgrent_r
82.Fa "struct group *grp"
83.Fa "char *buffer"
84.Fa "int bufsize"
85.Fc
86.Ft "struct group *"
87.Fo getgrgid
88.Fa "gid_t gid"
89.Fc
90.Ft "struct group *"
91.Fo getgrgid_r
92.Fa "gid_t gid"
93.Fa "struct group *grp"
94.Fa "char *buffer"
95.Fa "int bufsize"
96.Fc
97.Ft void
98.Fo setgrent
99.Fa "void"
100.Fc
101.Ft void
102.Fo endgrent
103.Fa "void"
104.Fc
105.Ft "struct group *"
106.Fo fgetgrent
107.Fa "FILE *f"
108.Fc
109.Ft "struct group *"
110.Fo fgetgrent_r
111.Fa "FILE *f"
112.Fa "struct group *grp"
113.Fa "char *buffer"
114.Fa "int bufsize"
115.Fc
116.Ss Standard conforming
117.Fd #define _POSIX_PTHREAD_SEMANTICS
118.Ft int
119.Fo getgrnam_r
120.Fa "const char *name"
121.Fa "struct group *grp"
122.Fa "char *buffer"
123.Fa "size_t bufsize"
124.Fa "struct group **result"
125.Fc
126.Ft int
127.Fo getgrgid_r
128.Fa "gid_t gid"
129.Fa "struct group *grp"
130.Fa "char *buffer"
131.Fa "size_t bufsize"
132.Fa "struct group **result"
133.Fc
134.Sh DESCRIPTION
135These functions are used to obtain entries describing user groups.
136Entries can come from any of the sources for
137.Sy group
138specified via
139.Xr nsswitch.conf 5 .
140.Pp
141.Fn getgrnam
142searches the group database for an entry with the group name specified by the
143character string parameter
144.Fa name .
145.Pp
146.Fn getgrgid
147searches the group database for an entry with the numeric group id specified by
148.Fa gid .
149.Pp
150.Fn setgrent ,
151.Fn getgrent ,
152and
153.Fn endgrent
154are used to enumerate group entries from the database.
155.Pp
156.Fn setgrent
157effectively rewinds the group database to allow repeated searches.
158It sets or resets the enumeration to the beginning of the set of group entries.
159This function should be called before the first call to
160.Fn getgrent .
161.Pp
162.Fn getgrent
163returns a pointer to a structure containing the broken-out fields of an entry
164in the group database.
165When first called,
166.Fn getgrent
167returns a pointer to a
168.Sy group
169structure containing the next group structure in the group database.
170Successive calls can be used to search the entire database.
171.Pp
172.Fn endgrent
173can be called to close the group database and deallocate resources when
174processing is complete.
175It is permissible, though possibly less efficient, for the process to call more
176group functions after calling
177.Fn endgrent .
178.Pp
179.Fn fgetgrent ,
180unlike the other functions above, does not use
181.Xr nsswitch.conf 5 .
182It reads and parses the next line from the stream
183.Fa f ,
184which is assumed to have the format of the
185.Xr group 5
186file.
187.Ss Reentrant Interfaces
188.Fn getgrnam ,
189.Fn getgrgid ,
190.Fn getgrent ,
191and
192.Fn fgetgrent
193use thread-specific storage that is reused in each call to one of these
194functions by the same thread, making them safe to use but not recommended for
195multithreaded applications.
196.Pp
197The parallel functions
198.Fn getgrnam_r ,
199.Fn getgrgid_r ,
200.Fn getgrent_r ,
201and
202.Fn fgetgrent_r
203provide reentrant interfaces for these operations.
204.Pp
205Each reentrant interface performs the same operation as its non-reentrant
206counterpart, named by removing the
207.Sq _r
208suffix.
209The reentrant interfaces, however, use buffers supplied by the caller to store
210returned results instead of using thread-specific data that can be overwritten
211by each call.
212They are safe for use in both single-threaded and multithreaded applications.
213.Pp
214Each reentrant interface takes the same arguments as its non-reentrant
215counterpart, as well as the following additional parameters.
216The
217.Fa grp
218argument must be a pointer to a struct group structure allocated by the caller.
219On successful completion, the function returns the group entry in this
220structure.
221Storage referenced by the group structure is allocated from the memory provided
222with the
223.Fa buffer
224argument that is
225.Fa bufsize
226characters in size.
227The maximum size needed for this buffer can be determined with the
228.Dv _SC_GETGR_R_SIZE_MAX
229.Xr sysconf 3C
230parameter.
231The standard-conforming versions place a pointer to the modified
232.Fa grp
233structure in the
234.Fa result
235parameter, instead of returning a pointer to this structure.
236A null pointer is returned at the location pointed to by
237.Fa result
238on error or if the requested entry is not found.
239.Pp
240For enumeration in multithreaded applications, the position within the
241enumeration is a process-wide property shared by all threads.
242.Fn setgrent
243can be used in a multithreaded application but resets the enumeration position
244for all threads.
245If multiple threads interleave calls to
246.Fn getgrent_r ,
247the threads will enumerate disjoint subsets of the group database.
248Like their non-reentrant counterparts,
249.Fn getgrnam_r
250and
251.Fn getgrgid_r
252leave the enumeration position in an indeterminate state.
253.Ss group Structure
254Group entries are represented by the struct group structure defined in
255.In grp.h
256.Bd -literal -offset indent
257struct group {
258	char *gr_name;   /* the name of the group */
259	char *gr_passwd; /* the encrypted group password */
260	gid_t gr_gid;    /* the numerical group ID */
261	char **gr_mem;   /* vector of pointers to member names */
262};
263.Ed
264.Sh RETURN VALUES
265.Fn getgrnam ,
266.Fn getgrnam_r ,
267.Fn getgrgid ,
268and
269.Fn getgrgid_r
270each return a pointer to a struct group if they successfully locate the
271requested entry.
272They return a null pointer if either the requested entry was not found or an
273error occurred.
274On error,
275.Va errno
276is set to indicate the error.
277.Pp
278The standard-conforming functions
279.Fn getgrnam_r
280and
281.Fn getgrgid_r
282return
283.Sy 0
284on success
285.Pq including when the requested entry is not found
286or an error number in case of failure.
287To distinguish between a successful lookup and a not-found condition, the
288caller must check the pointer stored at the location pointed to by
289.Fa result :
290it will be
291.No non- Ns Dv NULL
292if the entry was found and
293.Dv NULL
294if it was not.
295.Pp
296.Fn getgrent ,
297.Fn getgrent_r ,
298.Fn fgetgrent ,
299and
300.Fn fgetgrent_r
301each return a pointer to a struct group if they successfully enumerate an
302entry; otherwise they return a null pointer on end-of-file or error.
303On error,
304.Va errno
305is set to indicate the error.
306.Pp
307.Fn getgrnam ,
308.Fn getgrgid ,
309.Fn getgrent ,
310and
311.Fn fgetgrent
312use thread-specific data storage, so returned data must be copied before a
313subsequent call to any of these functions if the data are to be saved.
314.Pp
315When the pointer returned by the reentrant functions
316.Fn getgrnam_r ,
317.Fn getgrgid_r ,
318.Fn getgrent_r ,
319and
320.Fn fgetgrent_r
321is
322.No non- Ns Dv NULL ,
323it is always equal to the
324.Fa grp
325pointer that was supplied by the caller.
326.Pp
327Applications wishing to check for error situations should set
328.Va errno
329to 0 before calling
330.Fn getgrnam ,
331.Fn getgrnam_r ,
332.Fn getgrent ,
333.Fn getgrent_r ,
334.Fn getgrgid ,
335.Fn getgrgid_r ,
336.Fn fgetgrent ,
337and
338.Fn fgetgrent_r .
339If these functions return a
340.Dv NULL
341pointer and
342.Va errno
343is non-zero, an error occurred.
344.Sh ERRORS
345.Fn getgrent_r ,
346.Fn fgetgrent ,
347and
348.Fn fgetgrent_r
349will fail if:
350.Bl -tag -width Er
351.It Er EIO
352An I/O error has occurred.
353.It Er ERANGE
354Insufficient storage was supplied by
355.Fa buffer
356and
357.Fa bufsize
358to contain the data to be referenced by the resulting
359.Sy group
360structure.
361.El
362.Pp
363.Fn getgrent_r
364will fail if:
365.Bl -tag -width Er
366.It Er EMFILE
367There are
368.Brq Dv OPEN_MAX
369file descriptors currently open in the calling process.
370.It Er ENFILE
371The maximum allowable number of files is currently open in the system.
372.El
373.Pp
374.Fn getgrnam ,
375.Fn getgrnam_r ,
376.Fn getgrgid ,
377.Fn getgrgid_r ,
378and
379.Fn getgrent
380may fail if:
381.Bl -tag -width Er
382.It Er EINTR
383A signal was caught during the operation.
384.It Er EIO
385An I/O error has occurred.
386.It Er EMFILE
387There are
388.Brq Dv OPEN_MAX
389file descriptors currently open in the calling process.
390.It Er ENFILE
391The maximum allowable number of files is currently open in the system.
392.El
393.Pp
394.Fn getgrnam_r
395and
396.Fn getgrgid_r
397may fail if:
398.Bl -tag -width Ds
399.It Er ERANGE
400Insufficient storage was supplied by
401.Fa buffer
402and
403.Fa bufsize
404to contain the data to be referenced by the resulting
405.Sy group
406structure.
407.El
408.Sh INTERFACE STABILITY
409.Nm getgrnam ,
410.Nm getgrnam_r ,
411.Nm getgrent ,
412.Nm getgrent_r ,
413.Nm getgrgid ,
414.Nm getgrgid_r ,
415.Nm setgrent ,
416.Nm endgrent ,
417.Nm fgetgrent
418and
419.Nm fgetgrent_r
420are
421.Sy Committed .
422See also
423.Sx NOTES
424below.
425.Sh MT-LEVEL
426See
427.Sx Reentrant Interfaces
428in
429.Sx DESCRIPTION .
430.Sh SEE ALSO
431.Xr Intro 3 ,
432.Xr getpwnam 3C ,
433.Xr group 5 ,
434.Xr nsswitch.conf 5 ,
435.Xr passwd 5 ,
436.Xr attributes 7 ,
437.Xr standards 7
438.Sh NOTES
439When compiling multithreaded programs, see
440.Xr Intro 3 .
441.Pp
442Use of the enumeration interfaces
443.Fn getgrent
444and
445.Fn getgrent_r
446is discouraged; enumeration is supported for the group file and NIS but in
447general is not efficient and might not be supported for all database sources.
448The semantics of enumeration are discussed further in
449.Xr nsswitch.conf 5 .
450.Pp
451Previous releases allowed the use of
452.Sq \&+
453and
454.Sq \&-
455entries in
456.Pa /etc/group
457to selectively include and exclude entries from NIS.
458The primary usage of these entries is superseded by the name service switch, so
459the
460.Sq +/-
461form might not be supported in future releases.
462.Pp
463If required, the
464.Sq +/-
465functionality can still be obtained for NIS by specifying
466.Sy compat
467as the source for
468.Sy group .
469.Pp
470If the
471.Sq +/-
472functionality is required in conjunction with LDAP, specify both
473.Sy compat
474as the source for
475.Sy group
476and
477.Sy ldap
478as the source for the pseudo-database
479.Sy group_compat .
480See
481.Xr group 5 ,
482and
483.Xr nsswitch.conf 5
484for details.
485.Pp
486Solaris 2.4 and earlier releases provided definitions of
487.Fn getgrnam_r
488and
489.Fn getgrgid_r
490as specified in POSIX.1c Draft 6.
491The final POSIX.1c standard changed the interface for these functions.
492Support for the Draft 6 interface is provided for compatibility only and might
493not be supported in future releases.
494New applications and libraries should use the standard-conforming interface.
495.Pp
496For POSIX.1c-conforming applications, the
497.Dv _POSIX_PTHREAD_SEMANTICS
498and
499.Dv _REENTRANT
500flags are automatically turned on by defining the
501.Dv _POSIX_C_SOURCE
502flag with a value \(>=
503.Em 199506L .
504