1GETPWNAM(3) Linux Programmer's Manual GETPWNAM(3)
2
3
4
6 getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
7
9 #include <sys/types.h>
10 #include <pwd.h>
11
12 struct passwd *getpwnam(const char *name);
13
14 struct passwd *getpwuid(uid_t uid);
15
16 int getpwnam_r(const char *name, struct passwd *pwd,
17 char *buf, size_t buflen, struct passwd **result);
18
19 int getpwuid_r(uid_t uid, struct passwd *pwd,
20 char *buf, size_t buflen, struct passwd **result);
21
22 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
23
24 getpwnam_r(), getpwuid_r():
25 _POSIX_C_SOURCE
26 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
27
29 The getpwnam() function returns a pointer to a structure containing the
30 broken-out fields of the record in the password database (e.g., the lo‐
31 cal password file /etc/passwd, NIS, and LDAP) that matches the username
32 name.
33
34 The getpwuid() function returns a pointer to a structure containing the
35 broken-out fields of the record in the password database that matches
36 the user ID uid.
37
38 The passwd structure is defined in <pwd.h> as follows:
39
40 struct passwd {
41 char *pw_name; /* username */
42 char *pw_passwd; /* user password */
43 uid_t pw_uid; /* user ID */
44 gid_t pw_gid; /* group ID */
45 char *pw_gecos; /* user information */
46 char *pw_dir; /* home directory */
47 char *pw_shell; /* shell program */
48 };
49
50 See passwd(5) for more information about these fields.
51
52 The getpwnam_r() and getpwuid_r() functions obtain the same information
53 as getpwnam() and getpwuid(), but store the retrieved passwd structure
54 in the space pointed to by pwd. The string fields pointed to by the
55 members of the passwd structure are stored in the buffer buf of size
56 buflen. A pointer to the result (in case of success) or NULL (in case
57 no entry was found or an error occurred) is stored in *result.
58
59 The call
60
61 sysconf(_SC_GETPW_R_SIZE_MAX)
62
63 returns either -1, without changing errno, or an initial suggested size
64 for buf. (If this size is too small, the call fails with ERANGE, in
65 which case the caller can retry with a larger buffer.)
66
68 The getpwnam() and getpwuid() functions return a pointer to a passwd
69 structure, or NULL if the matching entry is not found or an error oc‐
70 curs. If an error occurs, errno is set appropriately. If one wants to
71 check errno after the call, it should be set to zero before the call.
72
73 The return value may point to a static area, and may be overwritten by
74 subsequent calls to getpwent(3), getpwnam(), or getpwuid(). (Do not
75 pass the returned pointer to free(3).)
76
77 On success, getpwnam_r() and getpwuid_r() return zero, and set *result
78 to pwd. If no matching password record was found, these functions re‐
79 turn 0 and store NULL in *result. In case of error, an error number is
80 returned, and NULL is stored in *result.
81
83 0 or ENOENT or ESRCH or EBADF or EPERM or ...
84 The given name or uid was not found.
85
86 EINTR A signal was caught; see signal(7).
87
88 EIO I/O error.
89
90 EMFILE The per-process limit on the number of open file descriptors has
91 been reached.
92
93 ENFILE The system-wide limit on the total number of open files has been
94 reached.
95
96 ENOMEM Insufficient memory to allocate passwd structure.
97
98 ERANGE Insufficient buffer space supplied.
99
101 /etc/passwd
102 local password database file
103
105 For an explanation of the terms used in this section, see at‐
106 tributes(7).
107
108 ┌──────────────┬───────────────┬─────────────────────────────┐
109 │Interface │ Attribute │ Value │
110 ├──────────────┼───────────────┼─────────────────────────────┤
111 │getpwnam() │ Thread safety │ MT-Unsafe race:pwnam locale │
112 ├──────────────┼───────────────┼─────────────────────────────┤
113 │getpwuid() │ Thread safety │ MT-Unsafe race:pwuid locale │
114 ├──────────────┼───────────────┼─────────────────────────────┤
115 │getpwnam_r(), │ Thread safety │ MT-Safe locale │
116 │getpwuid_r() │ │ │
117 └──────────────┴───────────────┴─────────────────────────────┘
119 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD. The pw_gecos field is not
120 specified in POSIX, but is present on most implementations.
121
123 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
124 It does not call "not found" an error, and hence does not specify what
125 value errno might have in this situation. But that makes it impossible
126 to recognize errors. One might argue that according to POSIX errno
127 should be left unchanged if an entry is not found. Experiments on var‐
128 ious UNIX-like systems show that lots of different values occur in this
129 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably
130 others.
131
132 The pw_dir field contains the name of the initial working directory of
133 the user. Login programs use the value of this field to initialize the
134 HOME environment variable for the login shell. An application that
135 wants to determine its user's home directory should inspect the value
136 of HOME (rather than the value getpwuid(getuid())->pw_dir) since this
137 allows the user to modify their notion of "the home directory" during a
138 login session. To determine the (initial) home directory of another
139 user, it is necessary to use getpwnam("username")->pw_dir or similar.
140
142 The program below demonstrates the use of getpwnam_r() to find the full
143 username and user ID for the username supplied as a command-line argu‐
144 ment.
145
146 #include <pwd.h>
147 #include <stdint.h>
148 #include <stdio.h>
149 #include <stdlib.h>
150 #include <unistd.h>
151 #include <errno.h>
152
153 int
154 main(int argc, char *argv[])
155 {
156 struct passwd pwd;
157 struct passwd *result;
158 char *buf;
159 size_t bufsize;
160 int s;
161
162 if (argc != 2) {
163 fprintf(stderr, "Usage: %s username\n", argv[0]);
164 exit(EXIT_FAILURE);
165 }
166
167 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
168 if (bufsize == -1) /* Value was indeterminate */
169 bufsize = 16384; /* Should be more than enough */
170
171 buf = malloc(bufsize);
172 if (buf == NULL) {
173 perror("malloc");
174 exit(EXIT_FAILURE);
175 }
176
177 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
178 if (result == NULL) {
179 if (s == 0)
180 printf("Not found\n");
181 else {
182 errno = s;
183 perror("getpwnam_r");
184 }
185 exit(EXIT_FAILURE);
186 }
187
188 printf("Name: %s; UID: %jd\n", pwd.pw_gecos,
189 (intmax_t) pwd.pw_uid);
190 exit(EXIT_SUCCESS);
191 }
192
194 endpwent(3), fgetpwent(3), getgrnam(3), getpw(3), getpwent(3), getsp‐
195 nam(3), putpwent(3), setpwent(3), passwd(5)
196
198 This page is part of release 5.10 of the Linux man-pages project. A
199 description of the project, information about reporting bugs, and the
200 latest version of this page, can be found at
201 https://www.kernel.org/doc/man-pages/.
202
203
204
205GNU 2020-11-01 GETPWNAM(3)