1GETGRGID(3P) POSIX Programmer's Manual GETGRGID(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 getgrgid, getgrgid_r — get group database entry for a group ID
14
16 #include <grp.h>
17
18 struct group *getgrgid(gid_t gid);
19 int getgrgid_r(gid_t gid, struct group *grp, char *buffer,
20 size_t bufsize, struct group **result);
21
23 The getgrgid() function shall search the group database for an entry
24 with a matching gid.
25
26 The getgrgid() function need not be thread-safe.
27
28 The getgrgid_r() function shall update the group structure pointed to
29 by grp and store a pointer to that structure at the location pointed to
30 by result. The structure shall contain an entry from the group data‐
31 base with a matching gid. Storage referenced by the group structure is
32 allocated from the memory provided with the buffer parameter, which is
33 bufsize bytes in size. A call to sysconf(_SC_GETGR_R_SIZE_MAX) returns
34 either −1 without changing errno or an initial value suggested for the
35 size of this buffer. A null pointer shall be returned at the location
36 pointed to by result on error or if the requested entry is not found.
37
39 Upon successful completion, getgrgid() shall return a pointer to a
40 struct group with the structure defined in <grp.h> with a matching
41 entry if one is found. The getgrgid() function shall return a null
42 pointer if either the requested entry was not found, or an error
43 occurred. On error, errno shall be set to indicate the error.
44
45 The application shall not modify the structure to which the return
46 value points, nor any storage areas pointed to by pointers within the
47 structure. The returned pointer, and pointers within the structure,
48 might be invalidated or the structure or the storage areas might be
49 overwritten by a subsequent call to getgrent(), getgrgid(), or getgr‐
50 nam().
51
52 If successful, the getgrgid_r() function shall return zero; otherwise,
53 an error number shall be returned to indicate the error.
54
56 The getgrgid() and getgrgid_r() functions may fail if:
57
58 EIO An I/O error has occurred.
59
60 EINTR A signal was caught during getgrgid().
61
62 EMFILE All file descriptors available to the process are currently
63 open.
64
65 ENFILE The maximum allowable number of files is currently open in the
66 system.
67
68 The getgrgid_r() function may fail if:
69
70 ERANGE Insufficient storage was supplied via buffer and bufsize to con‐
71 tain the data to be referenced by the resulting group structure.
72
73 The following sections are informative.
74
76 Note that sysconf(_SC_GETGR_R_SIZE_MAX) may return −1 if there is no
77 hard limit on the size of the buffer needed to store all the groups
78 returned. This example shows how an application can allocate a buffer
79 of sufficient size to work with getgrid_r().
80
81 long int initlen = sysconf(_SC_GETGR_R_SIZE_MAX);
82 size_t len;
83 if (initlen == −1)
84 /* Default initial length. */
85 len = 1024;
86 else
87 len = (size_t) initlen;
88 struct group result;
89 struct group *resultp;
90 char *buffer = malloc(len);
91 if (buffer == NULL)
92 ...handle error...
93 int e;
94 while ((e = getgrgid_r(42, &result, buffer, len, &resultp)) == ERANGE)
95 {
96 size_t newlen = 2 * len;
97 if (newlen < len)
98 ...handle error...
99 len = newlen;
100 char *newbuffer = realloc(buffer, len);
101 if (newbuffer == NULL)
102 ...handle error...
103 buffer = newbuffer;
104 }
105 if (e != 0)
106 ...handle error...
107 free (buffer);
108
109 Finding an Entry in the Group Database
110 The following example uses getgrgid() to search the group database for
111 a group ID that was previously stored in a stat structure, then prints
112 out the group name if it is found. If the group is not found, the pro‐
113 gram prints the numeric value of the group for the entry.
114
115 #include <sys/types.h>
116 #include <grp.h>
117 #include <stdio.h>
118 ...
119 struct stat statbuf;
120 struct group *grp;
121 ...
122 if ((grp = getgrgid(statbuf.st_gid)) != NULL)
123 printf(" %-8.8s", grp->gr_name);
124 else
125 printf(" %-8d", statbuf.st_gid);
126 ...
127
129 Applications wishing to check for error situations should set errno to
130 0 before calling getgrgid(). If errno is set on return, an error
131 occurred.
132
133 The getgrgid_r() function is thread-safe and shall return values in a
134 user-supplied buffer instead of possibly using a static data area that
135 may be overwritten by each call.
136
137 Portable applications should take into account that it is usual for an
138 implementation to return −1 from sysconf() indicating that there is no
139 maximum for _SC_GETGR_R_SIZE_MAX.
140
142 None.
143
145 None.
146
148 endgrent(), getgrnam(), sysconf()
149
150 The Base Definitions volume of POSIX.1‐2008, <grp.h>, <sys_types.h>
151
153 Portions of this text are reprinted and reproduced in electronic form
154 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
155 -- Portable Operating System Interface (POSIX), The Open Group Base
156 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
157 cal and Electronics Engineers, Inc and The Open Group. (This is
158 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
159 event of any discrepancy between this version and the original IEEE and
160 The Open Group Standard, the original IEEE and The Open Group Standard
161 is the referee document. The original Standard can be obtained online
162 at http://www.unix.org/online.html .
163
164 Any typographical or formatting errors that appear in this page are
165 most likely to have been introduced during the conversion of the source
166 files to man page format. To report such errors, see https://www.ker‐
167 nel.org/doc/man-pages/reporting_bugs.html .
168
169
170
171IEEE/The Open Group 2013 GETGRGID(3P)