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
12 getpwuid, getpwuid_r - search user database for a user ID
13
15 #include <pwd.h>
16
17 struct passwd *getpwuid(uid_t uid);
18
19
20 int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
21 size_t bufsize, struct passwd **result);
22
23
25 The getpwuid() function shall search the user database for an entry
26 with a matching uid.
27
28 The getpwuid() function need not be reentrant. A function that is not
29 required to be reentrant is not required to be thread-safe.
30
31 Applications wishing to check for error situations should set errno to
32 0 before calling getpwuid(). If getpwuid() returns a null pointer and
33 errno is set to non-zero, an error occurred.
34
35 The getpwuid_r() function shall update the passwd structure pointed to
36 by pwd and store a pointer to that structure at the location pointed to
37 by result. The structure shall contain an entry from the user database
38 with a matching uid. Storage referenced by the structure is allocated
39 from the memory provided with the buffer parameter, which is bufsize
40 bytes in size. The maximum size needed for this buffer can be deter‐
41 mined with the {_SC_GETPW_R_SIZE_MAX} sysconf() parameter. A NULL
42 pointer shall be returned at the location pointed to by result on error
43 or if the requested entry is not found.
44
46 The getpwuid() function shall return a pointer to a struct passwd with
47 the structure as defined in <pwd.h> with a matching entry if found. A
48 null pointer shall be returned if the requested entry is not found, or
49 an error occurs. On error, errno shall be set to indicate the error.
50
51 The return value may point to a static area which is overwritten by a
52 subsequent call to getpwent(), getpwnam(), or getpwuid().
53
54 If successful, the getpwuid_r() function shall return zero; otherwise,
55 an error number shall be returned to indicate the error.
56
58 The getpwuid() and getpwuid_r() functions may fail if:
59
60 EIO An I/O error has occurred.
61
62 EINTR A signal was caught during getpwuid().
63
64 EMFILE {OPEN_MAX} file descriptors are currently open in the calling
65 process.
66
67 ENFILE The maximum allowable number of files is currently open in the
68 system.
69
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
78 The following sections are informative.
79
81 Getting an Entry for the Root User
82 The following example gets the user database entry for the user with
83 user ID 0 (root).
84
85
86 #include <sys/types.h>
87 #include <pwd.h>
88 ...
89 uid_t id = 0;
90 struct passwd *pwd;
91
92
93 pwd = getpwuid(id);
94
95 Finding the Name for the Effective User ID
96 The following example defines pws as a pointer to a structure of type
97 passwd, which is used to store the structure pointer returned by the
98 call to the getpwuid() function. The geteuid() function shall return
99 the effective user ID of the calling process; this is used as the
100 search criteria for the getpwuid() function. The call to getpwuid()
101 shall return a pointer to the structure containing that user ID value.
102
103
104 #include <unistd.h>
105 #include <sys/types.h>
106 #include <pwd.h>
107 ...
108 struct passwd *pws;
109 pws = getpwuid(geteuid());
110
111 Finding an Entry in the User Database
112 The following example uses getpwuid() to search the user database for a
113 user ID that was previously stored in a stat structure, then prints out
114 the user name if it is found. If the user is not found, the program
115 prints the numeric value of the user ID for the entry.
116
117
118 #include <sys/types.h>
119 #include <pwd.h>
120 #include <stdio.h>
121 ...
122 struct stat statbuf;
123 struct passwd *pwd;
124 ...
125 if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
126 printf(" %-8.8s", pwd->pw_name);
127 else
128 printf(" %-8d", statbuf.st_uid);
129
131 Three names associated with the current process can be determined: get‐
132 pwuid( geteuid()) returns the name associated with the effective user
133 ID of the process; getlogin() returns the name associated with the cur‐
134 rent login activity; and getpwuid( getuid()) returns the name associ‐
135 ated with the real user ID of the process.
136
137 The getpwuid_r() function is thread-safe and returns values in a user-
138 supplied buffer instead of possibly using a static data area that may
139 be overwritten by each call.
140
142 None.
143
145 None.
146
148 getpwnam(), geteuid(), getuid(), getlogin(), the Base Definitions vol‐
149 ume of IEEE Std 1003.1-2001, <limits.h>, <pwd.h>, <sys/types.h>
150
152 Portions of this text are reprinted and reproduced in electronic form
153 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
154 -- Portable Operating System Interface (POSIX), The Open Group Base
155 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
156 Electrical and Electronics Engineers, Inc and The Open Group. In the
157 event of any discrepancy between this version and the original IEEE and
158 The Open Group Standard, the original IEEE and The Open Group Standard
159 is the referee document. The original Standard can be obtained online
160 at http://www.opengroup.org/unix/online.html .
161
162
163
164IEEE/The Open Group 2003 GETPWUID(3P)