]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
c6ec3685 MK |
2 | .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk |
3 | .\" <mtk.manpages@gmail.com> | |
fea681da | 4 | .\" |
93015253 | 5 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
6 | .\" Permission is granted to make and distribute verbatim copies of this |
7 | .\" manual provided the copyright notice and this permission notice are | |
8 | .\" preserved on all copies. | |
9 | .\" | |
10 | .\" Permission is granted to copy and distribute modified versions of this | |
11 | .\" manual under the conditions for verbatim copying, provided that the | |
12 | .\" entire resulting derived work is distributed under the terms of a | |
13 | .\" permission notice identical to this one. | |
c13182ef | 14 | .\" |
fea681da MK |
15 | .\" Since the Linux kernel and libraries are constantly changing, this |
16 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
17 | .\" responsibility for errors or omissions, or for damages resulting from | |
18 | .\" the use of the information contained herein. The author(s) may not | |
19 | .\" have taken the same level of care in the production of this manual, | |
20 | .\" which is licensed free of charge, as they might when working | |
21 | .\" professionally. | |
c13182ef | 22 | .\" |
fea681da MK |
23 | .\" Formatted or processed versions of this manual, if unaccompanied by |
24 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 25 | .\" %%%LICENSE_END |
fea681da MK |
26 | .\" |
27 | .\" References consulted: | |
28 | .\" Linux libc source code | |
6b2fc294 | 29 | .\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991) |
fea681da MK |
30 | .\" 386BSD man pages |
31 | .\" | |
32 | .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu) | |
33 | .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de) | |
34 | .\" Modified 2003-11-15 by aeb | |
c6ec3685 | 35 | .\" 2008-11-07, mtk, Added an example program for getpwnam_r(). |
fea681da | 36 | .\" |
29b41e74 | 37 | .TH GETPWNAM 3 2015-12-28 "GNU" "Linux Programmer's Manual" |
fea681da MK |
38 | .SH NAME |
39 | getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry | |
40 | .SH SYNOPSIS | |
41 | .nf | |
42 | .B #include <sys/types.h> | |
43 | .B #include <pwd.h> | |
44 | .sp | |
45 | .BI "struct passwd *getpwnam(const char *" name ); | |
46 | .sp | |
47 | .BI "struct passwd *getpwuid(uid_t " uid ); | |
48 | .sp | |
3adc12e9 | 49 | .BI "int getpwnam_r(const char *" name ", struct passwd *" pwd , |
fea681da | 50 | .br |
ae577310 | 51 | .BI " char *" buf ", size_t " buflen ", struct passwd **" result ); |
fea681da | 52 | .sp |
3adc12e9 | 53 | .BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd , |
fea681da | 54 | .br |
ae577310 | 55 | .BI " char *" buf ", size_t " buflen ", struct passwd **" result ); |
fea681da | 56 | .fi |
cc4615cc MK |
57 | .sp |
58 | .in -4n | |
59 | Feature Test Macro Requirements for glibc (see | |
60 | .BR feature_test_macros (7)): | |
61 | .in | |
62 | .sp | |
63 | .ad l | |
64 | .BR getpwnam_r (), | |
65 | .BR getpwuid_r (): | |
f496f649 | 66 | .RS 4 |
0f200f07 | 67 | _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || |
c1016e87 | 68 | _SVID_SOURCE |
f496f649 | 69 | .RE |
cc4615cc | 70 | .ad b |
fea681da MK |
71 | .SH DESCRIPTION |
72 | The | |
63aa9df0 | 73 | .BR getpwnam () |
fea681da | 74 | function returns a pointer to a structure containing |
f936cf26 | 75 | the broken-out fields of the record in the password database |
c13182ef | 76 | (e.g., the local password file |
f2738b39 MK |
77 | .IR /etc/passwd , |
78 | NIS, and LDAP) | |
18701562 | 79 | that matches the username |
fea681da MK |
80 | .IR name . |
81 | .PP | |
82 | The | |
63aa9df0 | 83 | .BR getpwuid () |
fea681da | 84 | function returns a pointer to a structure containing |
f2738b39 | 85 | the broken-out fields of the record in the password database |
f936cf26 | 86 | that matches the user ID |
fea681da MK |
87 | .IR uid . |
88 | .PP | |
fea681da MK |
89 | The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows: |
90 | .sp | |
bd191423 | 91 | .in +4n |
fea681da MK |
92 | .nf |
93 | struct passwd { | |
18701562 | 94 | char *pw_name; /* username */ |
f2738b39 | 95 | char *pw_passwd; /* user password */ |
34c97781 MK |
96 | uid_t pw_uid; /* user ID */ |
97 | gid_t pw_gid; /* group ID */ | |
958680cd | 98 | char *pw_gecos; /* user information */ |
f2738b39 MK |
99 | char *pw_dir; /* home directory */ |
100 | char *pw_shell; /* shell program */ | |
fea681da MK |
101 | }; |
102 | .fi | |
bd191423 | 103 | .in |
fea681da | 104 | .PP |
011520b8 MK |
105 | See |
106 | .BR passwd (5) | |
107 | for more information about these fields. | |
108 | .PP | |
109 | The | |
110 | .BR getpwnam_r () | |
111 | and | |
112 | .BR getpwuid_r () | |
113 | functions obtain the same information as | |
114 | .BR getpwnam () | |
115 | and | |
116 | .BR getpwuid (), | |
117 | but store the retrieved | |
118 | .I passwd | |
119 | structure in the space pointed to by | |
120 | .IR pwd . | |
121 | The string fields pointed to by the members of the | |
122 | .I passwd | |
123 | structure are stored in the buffer | |
124 | .I buf | |
125 | of size | |
126 | .IR buflen . | |
127 | A pointer to the result (in case of success) or NULL (in case no entry | |
128 | was found or an error occurred) is stored in | |
129 | .IR *result . | |
130 | .PP | |
f6dc3611 MK |
131 | The call |
132 | ||
133 | sysconf(_SC_GETPW_R_SIZE_MAX) | |
134 | ||
135 | returns either \-1, without changing | |
136 | .IR errno , | |
137 | or an initial suggested size for | |
138 | .IR buf . | |
139 | (If this size is too small, | |
140 | the call fails with | |
141 | .BR ERANGE , | |
142 | in which case the caller can retry with a larger buffer.) | |
47297adb | 143 | .SH RETURN VALUE |
60a90ecd MK |
144 | The |
145 | .BR getpwnam () | |
146 | and | |
147 | .BR getpwuid () | |
148 | functions return a pointer to a | |
f2738b39 MK |
149 | .I passwd |
150 | structure, or NULL if the matching entry is not found or | |
151 | an error occurs. | |
152 | If an error occurs, | |
fea681da | 153 | .I errno |
f2738b39 MK |
154 | is set appropriately. |
155 | If one wants to check | |
fea681da MK |
156 | .I errno |
157 | after the call, it should be set to zero before the call. | |
158 | .LP | |
57c1b002 | 159 | The return value may point to a static area, and may be overwritten |
fea681da | 160 | by subsequent calls to |
3a72373c | 161 | .BR getpwent (3), |
63aa9df0 | 162 | .BR getpwnam (), |
fea681da | 163 | or |
63aa9df0 | 164 | .BR getpwuid (). |
48e4cfa0 MK |
165 | (Do not pass the returned pointer to |
166 | .BR free (3).) | |
fea681da | 167 | .LP |
bbab485d | 168 | On success, |
60a90ecd MK |
169 | .BR getpwnam_r () |
170 | and | |
171 | .BR getpwuid_r () | |
bbab485d | 172 | return zero, and set |
3adc12e9 | 173 | .IR *result |
bbab485d | 174 | to |
3adc12e9 | 175 | .IR pwd . |
bbab485d MK |
176 | If no matching password record was found, |
177 | these functions return 0 and store NULL in | |
3adc12e9 | 178 | .IR *result . |
bbab485d | 179 | In case of error, an error number is returned, and NULL is stored in |
3adc12e9 | 180 | .IR *result . |
fea681da MK |
181 | .SH ERRORS |
182 | .TP | |
183 | .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... " | |
184 | The given | |
185 | .I name | |
186 | or | |
187 | .I uid | |
188 | was not found. | |
189 | .TP | |
190 | .B EINTR | |
bb14af39 MK |
191 | A signal was caught; see |
192 | .BR signal (7). | |
fea681da MK |
193 | .TP |
194 | .B EIO | |
195 | I/O error. | |
196 | .TP | |
197 | .B EMFILE | |
26c32fab | 198 | The per-process limit on the number of open file descriptors has been reached. |
fea681da MK |
199 | .TP |
200 | .B ENFILE | |
e258766b | 201 | The system-wide limit on the total number of open files has been reached. |
fea681da MK |
202 | .TP |
203 | .B ENOMEM | |
f2738b39 | 204 | .\" not in POSIX |
b2133414 MK |
205 | Insufficient memory to allocate |
206 | .I passwd | |
207 | structure. | |
fea681da MK |
208 | .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45) |
209 | .TP | |
210 | .B ERANGE | |
211 | Insufficient buffer space supplied. | |
212 | .SH FILES | |
213 | .TP | |
214 | .I /etc/passwd | |
f2738b39 | 215 | local password database file |
404197c7 | 216 | .SH ATTRIBUTES |
2973863a MK |
217 | For an explanation of the terms used in this section, see |
218 | .BR attributes (7). | |
219 | .TS | |
220 | allbox; | |
693e6cf5 | 221 | lb lb lb |
2973863a MK |
222 | l l l. |
223 | Interface Attribute Value | |
224 | T{ | |
693e6cf5 MS |
225 | .BR getpwnam () |
226 | T} Thread safety MT-Unsafe race:pwnam locale | |
227 | T{ | |
404197c7 | 228 | .BR getpwuid () |
693e6cf5 | 229 | T} Thread safety MT-Unsafe race:pwuid locale |
2973863a MK |
230 | T{ |
231 | .BR getpwnam_r (), | |
693e6cf5 | 232 | .br |
404197c7 | 233 | .BR getpwuid_r () |
693e6cf5 | 234 | T} Thread safety MT-Safe locale |
2973863a | 235 | .TE |
47297adb | 236 | .SH CONFORMING TO |
9720f05e | 237 | POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD. |
958680cd MK |
238 | The |
239 | .I pw_gecos | |
240 | field is not specified in POSIX, but is present on most implementations. | |
fea681da | 241 | .SH NOTES |
68e1685c | 242 | The formulation given above under "RETURN VALUE" is from POSIX.1-2001. |
6b2fc294 | 243 | It does not call "not found" an error, and hence does not specify what value |
fea681da | 244 | .I errno |
c13182ef MK |
245 | might have in this situation. |
246 | But that makes it impossible to recognize | |
247 | errors. | |
248 | One might argue that according to POSIX | |
fea681da | 249 | .I errno |
c13182ef MK |
250 | should be left unchanged if an entry is not found. |
251 | Experiments on various | |
008f1ecc | 252 | UNIX-like systems show that lots of different values occur in this |
a797afac | 253 | situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others. |
fea681da MK |
254 | .\" more precisely: |
255 | .\" AIX 5.1 - gives ESRCH | |
256 | .\" OSF1 4.0g - gives EWOULDBLOCK | |
db85058b MK |
257 | .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT |
258 | .\" glibc since version 2.7 - give 0 | |
fea681da MK |
259 | .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM |
260 | .\" SunOS 5.8 - gives EBADF | |
261 | .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0 | |
6b2fc294 | 262 | |
c13182ef MK |
263 | The |
264 | .I pw_dir | |
265 | field contains the name of the initial working directory of the user. | |
2f0af33b MK |
266 | Login programs use the value of this field to initialize the |
267 | .B HOME | |
268 | environment variable for the login shell. | |
6b2fc294 | 269 | An application that wants to determine its user's home directory |
2f0af33b MK |
270 | should inspect the value of |
271 | .B HOME | |
272 | (rather than the value | |
94e9d9fe | 273 | .IR getpwuid(getuid())\->pw_dir ) |
6b2fc294 MK |
274 | since this allows the user to modify their notion of |
275 | "the home directory" during a login session. | |
276 | To determine the (initial) home directory of another user, | |
c13182ef | 277 | it is necessary to use |
94e9d9fe | 278 | .I getpwnam("username")\->pw_dir |
6b2fc294 | 279 | or similar. |
c6ec3685 MK |
280 | .SH EXAMPLE |
281 | The program below demonstrates the use of | |
282 | .BR getpwnam_r () | |
283 | to find the full username and user ID for the username | |
284 | supplied as a command-line argument. | |
285 | ||
286 | .nf | |
287 | #include <pwd.h> | |
288 | #include <stdio.h> | |
289 | #include <stdlib.h> | |
290 | #include <unistd.h> | |
291 | #include <errno.h> | |
292 | ||
293 | int | |
294 | main(int argc, char *argv[]) | |
295 | { | |
296 | struct passwd pwd; | |
297 | struct passwd *result; | |
298 | char *buf; | |
299 | size_t bufsize; | |
300 | int s; | |
301 | ||
302 | if (argc != 2) { | |
303 | fprintf(stderr, "Usage: %s username\\n", argv[0]); | |
304 | exit(EXIT_FAILURE); | |
305 | } | |
306 | ||
307 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); | |
308 | if (bufsize == \-1) /* Value was indeterminate */ | |
309 | bufsize = 16384; /* Should be more than enough */ | |
310 | ||
311 | buf = malloc(bufsize); | |
312 | if (buf == NULL) { | |
313 | perror("malloc"); | |
314 | exit(EXIT_FAILURE); | |
315 | } | |
316 | ||
317 | s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result); | |
318 | if (result == NULL) { | |
319 | if (s == 0) | |
320 | printf("Not found\\n"); | |
321 | else { | |
322 | errno = s; | |
323 | perror("getpwnam_r"); | |
324 | } | |
325 | exit(EXIT_FAILURE); | |
326 | } | |
327 | ||
42afc407 | 328 | printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid); |
c6ec3685 MK |
329 | exit(EXIT_SUCCESS); |
330 | } | |
331 | .fi | |
47297adb | 332 | .SH SEE ALSO |
fea681da MK |
333 | .BR endpwent (3), |
334 | .BR fgetpwent (3), | |
335 | .BR getgrnam (3), | |
336 | .BR getpw (3), | |
337 | .BR getpwent (3), | |
1ed50462 | 338 | .BR getspnam (3), |
fea681da MK |
339 | .BR putpwent (3), |
340 | .BR setpwent (3), | |
341 | .BR passwd (5) |