PostgreSQL Source Code  git master
thread.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * thread.c
4  *
5  * Prototypes and macros around system calls, used to help make
6  * threaded libraries reentrant and safe to use from threaded applications.
7  *
8  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
9  *
10  * src/port/thread.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 #include "c.h"
16 
17 #include <pwd.h>
18 
19 
20 /*
21  * Historically, the code in this module had to deal with operating systems
22  * that lacked getpwuid_r().
23  */
24 
25 #ifndef WIN32
26 
27 /*
28  * pg_get_user_name - get the name of the user with the given ID
29  *
30  * On success, the user name is returned into the buffer (of size buflen),
31  * and "true" is returned. On failure, a localized error message is
32  * returned into the buffer, and "false" is returned.
33  */
34 bool
35 pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
36 {
37  char pwdbuf[BUFSIZ];
38  struct passwd pwdstr;
39  struct passwd *pw = NULL;
40  int pwerr;
41 
42  pwerr = getpwuid_r(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
43  if (pw != NULL)
44  {
45  strlcpy(buffer, pw->pw_name, buflen);
46  return true;
47  }
48  if (pwerr != 0)
49  snprintf(buffer, buflen,
50  _("could not look up local user ID %d: %s"),
51  (int) user_id,
52  strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
53  else
54  snprintf(buffer, buflen,
55  _("local user with ID %d does not exist"),
56  (int) user_id);
57  return false;
58 }
59 
60 /*
61  * pg_get_user_home_dir - get the home directory of the user with the given ID
62  *
63  * On success, the directory path is returned into the buffer (of size buflen),
64  * and "true" is returned. On failure, a localized error message is
65  * returned into the buffer, and "false" is returned.
66  *
67  * Note that this does not incorporate the common behavior of checking
68  * $HOME first, since it's independent of which user_id is queried.
69  */
70 bool
71 pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen)
72 {
73  char pwdbuf[BUFSIZ];
74  struct passwd pwdstr;
75  struct passwd *pw = NULL;
76  int pwerr;
77 
78  pwerr = getpwuid_r(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
79  if (pw != NULL)
80  {
81  strlcpy(buffer, pw->pw_dir, buflen);
82  return true;
83  }
84  if (pwerr != 0)
85  snprintf(buffer, buflen,
86  _("could not look up local user ID %d: %s"),
87  (int) user_id,
88  strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
89  else
90  snprintf(buffer, buflen,
91  _("local user with ID %d does not exist"),
92  (int) user_id);
93  return false;
94 }
95 
96 #endif /* !WIN32 */
#define _(x)
Definition: elog.c:91
#define snprintf
Definition: port.h:238
#define strerror_r
Definition: port.h:255
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
bool pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen)
Definition: thread.c:71
bool pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
Definition: thread.c:35
int uid_t
Definition: win32_port.h:246