1GETPWUID(3P) POSIX Programmer's Manual GETPWUID(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 getpwuid, getpwuid_r — search user database for a user ID
14
16 #include <pwd.h>
17
18 struct passwd *getpwuid(uid_t uid);
19 int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
20 size_t bufsize, struct passwd **result);
21
23 The getpwuid() function shall search the user database for an entry
24 with a matching uid.
25
26 The getpwuid() function need not be thread-safe.
27
28 Applications wishing to check for error situations should set errno to
29 0 before calling getpwuid(). If getpwuid() returns a null pointer and
30 errno is set to non-zero, an error occurred.
31
32 The getpwuid_r() function shall update the passwd structure pointed to
33 by pwd and store a pointer to that structure at the location pointed to
34 by result. The structure shall contain an entry from the user database
35 with a matching uid. Storage referenced by the structure is allocated
36 from the memory provided with the buffer parameter, which is bufsize
37 bytes in size. A call to sysconf(_SC_GETPW_R_SIZE_MAX) returns either
38 −1 without changing errno or an initial value suggested for the size of
39 this buffer. A null pointer shall be returned at the location pointed
40 to by result on error or if the requested entry is not found.
41
43 The getpwuid() function shall return a pointer to a struct passwd with
44 the structure as defined in <pwd.h> with a matching entry if found. A
45 null pointer shall be returned if the requested entry is not found, or
46 an error occurs. On error, errno shall be set to indicate the error.
47
48 The application shall not modify the structure to which the return
49 value points, nor any storage areas pointed to by pointers within the
50 structure. The returned pointer, and pointers within the structure,
51 might be invalidated or the structure or the storage areas might be
52 overwritten by a subsequent call to getpwent(), getpwnam(), or getp‐
53 wuid().
54
55 If successful, the getpwuid_r() function shall return zero; otherwise,
56 an error number shall be returned to indicate the error.
57
59 These functions may fail if:
60
61 EIO An I/O error has occurred.
62
63 EINTR A signal was caught during getpwuid().
64
65 EMFILE All file descriptors available to the process are currently
66 open.
67
68 ENFILE The maximum allowable number of files is currently open in the
69 system.
70
71 The getpwuid_r() function may fail if:
72
73 ERANGE Insufficient storage was supplied via buffer and bufsize to con‐
74 tain the data to be referenced by the resulting passwd struc‐
75 ture.
76
77 The following sections are informative.
78
80 Note that sysconf(_SC_GETPW_R_SIZE_MAX) may return −1 if there is no
81 hard limit on the size of the buffer needed to store all the groups
82 returned. This example shows how an application can allocate a buffer
83 of sufficient size to work with getpwuid_r().
84
85 long int initlen = sysconf(_SC_GETPW_R_SIZE_MAX);
86 size_t len;
87 if (initlen == −1)
88 /* Default initial length. */
89 len = 1024;
90 else
91 len = (size_t) initlen;
92 struct passwd result;
93 struct passwd *resultp;
94 char *buffer = malloc(len);
95 if (buffer == NULL)
96 ...handle error...
97 int e;
98 while ((e = getpwuid_r(42, &result, buffer, len, &resultp)) == ERANGE)
99 {
100 size_t newlen = 2 * len;
101 if (newlen < len)
102 ...handle error...
103 len = newlen;
104 char *newbuffer = realloc(buffer, len);
105 if (newbuffer == NULL)
106 ...handle error...
107 buffer = newbuffer;
108 }
109 if (e != 0)
110 ...handle error...
111 free (buffer);
112
113 Getting an Entry for the Root User
114 The following example gets the user database entry for the user with
115 user ID 0 (root).
116
117 #include <sys/types.h>
118 #include <pwd.h>
119 ...
120 uid_t id = 0;
121 struct passwd *pwd;
122
123 pwd = getpwuid(id);
124
125 Finding the Name for the Effective User ID
126 The following example defines pws as a pointer to a structure of type
127 passwd, which is used to store the structure pointer returned by the
128 call to the getpwuid() function. The geteuid() function shall return
129 the effective user ID of the calling process; this is used as the
130 search criteria for the getpwuid() function. The call to getpwuid()
131 shall return a pointer to the structure containing that user ID value.
132
133 #include <unistd.h>
134 #include <sys/types.h>
135 #include <pwd.h>
136 ...
137 struct passwd *pws;
138 pws = getpwuid(geteuid());
139
140 Finding an Entry in the User Database
141 The following example uses getpwuid() to search the user database for a
142 user ID that was previously stored in a stat structure, then prints out
143 the user name if it is found. If the user is not found, the program
144 prints the numeric value of the user ID for the entry.
145
146 #include <sys/types.h>
147 #include <pwd.h>
148 #include <stdio.h>
149 ...
150 struct stat statbuf;
151 struct passwd *pwd;
152 ...
153 if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
154 printf(" %-8.8s", pwd->pw_name);
155 else
156 printf(" %-8d", statbuf.st_uid);
157
159 Three names associated with the current process can be determined: get‐
160 pwuid(geteuid()) returns the name associated with the effective user ID
161 of the process; getlogin() returns the name associated with the current
162 login activity; and getpwuid(getuid()) returns the name associated with
163 the real user ID of the process.
164
165 The getpwuid_r() function is thread-safe and returns values in a user-
166 supplied buffer instead of possibly using a static data area that may
167 be overwritten by each call.
168
169 Portable applications should take into account that it is usual for an
170 implementation to return −1 from sysconf() indicating that there is no
171 maximum for _SC_GETPW_R_SIZE_MAX.
172
174 None.
175
177 None.
178
180 getpwnam(), geteuid(), getuid(), getlogin(), sysconf()
181
182 The Base Definitions volume of POSIX.1‐2008, <pwd.h>, <sys_types.h>
183
185 Portions of this text are reprinted and reproduced in electronic form
186 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
187 -- Portable Operating System Interface (POSIX), The Open Group Base
188 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
189 cal and Electronics Engineers, Inc and The Open Group. (This is
190 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
191 event of any discrepancy between this version and the original IEEE and
192 The Open Group Standard, the original IEEE and The Open Group Standard
193 is the referee document. The original Standard can be obtained online
194 at http://www.unix.org/online.html .
195
196 Any typographical or formatting errors that appear in this page are
197 most likely to have been introduced during the conversion of the source
198 files to man page format. To report such errors, see https://www.ker‐
199 nel.org/doc/man-pages/reporting_bugs.html .
200
201
202
203IEEE/The Open Group 2013 GETPWUID(3P)