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