1GETPWUID(3P)               POSIX Programmer's Manual              GETPWUID(3P)
2
3
4

PROLOG

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

NAME

12       getpwuid, getpwuid_r — search user database for a user ID
13

SYNOPSIS

15       #include <pwd.h>
16
17       struct passwd *getpwuid(uid_t uid);
18       int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
19           size_t bufsize, struct passwd **result);
20

DESCRIPTION

22       The getpwuid() function shall search the user  database  for  an  entry
23       with a matching uid.
24
25       The getpwuid() function need not be thread-safe.
26
27       Applications  wishing to check for error situations should set errno to
28       0 before calling getpwuid().  If getpwuid() returns a null pointer  and
29       errno is set to non-zero, an error occurred.
30
31       The  getpwuid_r() function shall update the passwd structure pointed to
32       by pwd and store a pointer to that structure at the location pointed to
33       by result.  The structure shall contain an entry from the user database
34       with a matching uid.  Storage referenced by the structure is  allocated
35       from  the  memory  provided with the buffer parameter, which is bufsize
36       bytes in size. A call to sysconf(_SC_GETPW_R_SIZE_MAX)  returns  either
37       -1 without changing errno or an initial value suggested for the size of
38       this buffer.  A null pointer shall be returned at the location  pointed
39       to by result on error or if the requested entry is not found.
40

RETURN VALUE

42       The  getpwuid() function shall return a pointer to a struct passwd with
43       the structure as defined in <pwd.h> with a matching entry if  found.  A
44       null  pointer shall be returned if the requested entry is not found, or
45       an error occurs. If the requested entry was not found, errno shall  not
46       be changed. 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().  The returned pointer, and pointers within the structure, might
54       also be invalidated if the calling thread is terminated.
55
56       If  successful, the getpwuid_r() function shall return zero; otherwise,
57       an error number shall be returned to indicate the error.
58

ERRORS

60       These functions may fail if:
61
62       EIO    An I/O error has occurred.
63
64       EINTR  A signal was caught during getpwuid().
65
66       EMFILE All file descriptors available  to  the  process  are  currently
67              open.
68
69       ENFILE The  maximum  allowable number of files is currently open in the
70              system.
71
72       The getpwuid_r() function may fail if:
73
74       ERANGE Insufficient storage was supplied via buffer and bufsize to con‐
75              tain  the  data  to be referenced by the resulting passwd struc‐
76              ture.
77
78       The following sections are informative.
79

EXAMPLES

81       Note that sysconf(_SC_GETPW_R_SIZE_MAX) may return -1 if  there  is  no
82       hard  limit  on  the  size of the buffer needed to store all the groups
83       returned. This example shows how an application can allocate  a  buffer
84       of sufficient size to work with getpwuid_r().
85
86
87           long int initlen = sysconf(_SC_GETPW_R_SIZE_MAX);
88           size_t len;
89           if (initlen == -1)
90               /* Default initial length. */
91               len = 1024;
92           else
93               len = (size_t) initlen;
94           struct passwd result;
95           struct passwd *resultp;
96           char *buffer = malloc(len);
97           if (buffer == NULL)
98               ...handle error...
99           int e;
100           while ((e = getpwuid_r(42, &result, buffer, len, &resultp)) == ERANGE)
101               {
102               size_t newlen = 2 * len;
103               if (newlen < len)
104                   ...handle error...
105               len = newlen;
106               char *newbuffer = realloc(buffer, len);
107               if (newbuffer == NULL)
108                   ...handle error...
109               buffer = newbuffer;
110               }
111           if (e != 0)
112               ...handle error...
113           free (buffer);
114
115   Getting an Entry for the Root User
116       The  following  example  gets the user database entry for the user with
117       user ID 0 (root).
118
119
120           #include <sys/types.h>
121           #include <pwd.h>
122           ...
123           uid_t id = 0;
124           struct passwd *pwd;
125
126           pwd = getpwuid(id);
127
128   Finding the Name for the Effective User ID
129       The following example defines pws as a pointer to a structure  of  type
130       passwd,  which  is  used to store the structure pointer returned by the
131       call to the getpwuid() function. The geteuid()  function  shall  return
132       the  effective  user  ID  of  the  calling process; this is used as the
133       search criteria for the getpwuid() function.  The  call  to  getpwuid()
134       shall return a pointer to the structure containing that user ID value.
135
136
137           #include <unistd.h>
138           #include <sys/types.h>
139           #include <pwd.h>
140           ...
141           struct passwd *pws;
142           pws = getpwuid(geteuid());
143
144   Finding an Entry in the User Database
145       The following example uses getpwuid() to search the user database for a
146       user ID that was previously stored in a stat structure, then prints out
147       the  user  name  if  it is found. If the user is not found, the program
148       prints the numeric value of the user ID for the entry.
149
150
151           #include <sys/types.h>
152           #include <pwd.h>
153           #include <stdio.h>
154           ...
155           struct stat statbuf;
156           struct passwd *pwd;
157           ...
158           if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
159               printf(" %-8.8s", pwd->pw_name);
160           else
161               printf(" %-8d", statbuf.st_uid);
162

APPLICATION USAGE

164       Three names associated with the current process can be determined: get‐
165       pwuid(geteuid()) returns the name associated with the effective user ID
166       of the process; getlogin() returns the name associated with the current
167       login activity; and getpwuid(getuid()) returns the name associated with
168       the real user ID of the process.
169
170       The getpwuid_r() function is thread-safe and returns values in a  user-
171       supplied  buffer  instead of possibly using a static data area that may
172       be overwritten by each call.
173
174       Portable applications should take into account that it is usual for  an
175       implementation  to return -1 from sysconf() indicating that there is no
176       maximum for _SC_GETPW_R_SIZE_MAX.
177

RATIONALE

179       None.
180

FUTURE DIRECTIONS

182       None.
183

SEE ALSO

185       getpwnam(), geteuid(), getuid(), getlogin(), sysconf()
186
187       The Base Definitions volume of POSIX.1‐2017, <pwd.h>, <sys_types.h>
188
190       Portions of this text are reprinted and reproduced in  electronic  form
191       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
192       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
193       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
194       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
195       event of any discrepancy between this version and the original IEEE and
196       The Open Group Standard, the original IEEE and The Open Group  Standard
197       is  the  referee document. The original Standard can be obtained online
198       at http://www.opengroup.org/unix/online.html .
199
200       Any typographical or formatting errors that appear  in  this  page  are
201       most likely to have been introduced during the conversion of the source
202       files to man page format. To report such errors,  see  https://www.ker
203       nel.org/doc/man-pages/reporting_bugs.html .
204
205
206
207IEEE/The Open Group                  2017                         GETPWUID(3P)
Impressum