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-2022, 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  * Threading sometimes requires specially-named versions of functions
22  * that return data in static buffers, like strerror_r() instead of
23  * strerror(). Other operating systems use pthread_setspecific()
24  * and pthread_getspecific() internally to allow standard library
25  * functions to return static data to threaded applications. And some
26  * operating systems have neither.
27  *
28  * Additional confusion exists because many operating systems that
29  * use pthread_setspecific/pthread_getspecific() also have *_r versions
30  * of standard library functions for compatibility with operating systems
31  * that require them. However, internally, these *_r functions merely
32  * call the thread-safe standard library functions.
33  *
34  * For example, BSD/OS 4.3 uses Bind 8.2.3 for getpwuid(). Internally,
35  * getpwuid() calls pthread_setspecific/pthread_getspecific() to return
36  * static data to the caller in a thread-safe manner. However, BSD/OS
37  * also has getpwuid_r(), which merely calls getpwuid() and shifts
38  * around the arguments to match the getpwuid_r() function declaration.
39  * Therefore, while BSD/OS has getpwuid_r(), it isn't required. It also
40  * doesn't have strerror_r(), so we can't fall back to only using *_r
41  * functions for threaded programs.
42  *
43  * The current setup is to try threading in this order:
44  *
45  * use *_r function names if they exit
46  * (*_THREADSAFE=yes)
47  * use non-*_r functions if they are thread-safe
48  */
49 
50 #ifndef WIN32
51 
52 /*
53  * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
54  * behaviour, if that function is not available or required.
55  *
56  * Per POSIX, the possible cases are:
57  * success: returns zero, *result is non-NULL
58  * uid not found: returns zero, *result is NULL
59  * error during lookup: returns an errno code, *result is NULL
60  * (caller should *not* assume that the errno variable is set)
61  */
62 static int
63 pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer,
64  size_t buflen, struct passwd **result)
65 {
66 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETPWUID_R)
67  return getpwuid_r(uid, resultbuf, buffer, buflen, result);
68 #else
69  /* no getpwuid_r() available, just use getpwuid() */
70  errno = 0;
71  *result = getpwuid(uid);
72  /* paranoia: ensure we return zero on success */
73  return (*result == NULL) ? errno : 0;
74 #endif
75 }
76 
77 /*
78  * pg_get_user_name - get the name of the user with the given ID
79  *
80  * On success, the user name is returned into the buffer (of size buflen),
81  * and "true" is returned. On failure, a localized error message is
82  * returned into the buffer, and "false" is returned.
83  */
84 bool
85 pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
86 {
87  char pwdbuf[BUFSIZ];
88  struct passwd pwdstr;
89  struct passwd *pw = NULL;
90  int pwerr;
91 
92  pwerr = pqGetpwuid(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
93  if (pw != NULL)
94  {
95  strlcpy(buffer, pw->pw_name, buflen);
96  return true;
97  }
98  if (pwerr != 0)
99  snprintf(buffer, buflen,
100  _("could not look up local user ID %d: %s"),
101  (int) user_id,
102  strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
103  else
104  snprintf(buffer, buflen,
105  _("local user with ID %d does not exist"),
106  (int) user_id);
107  return false;
108 }
109 
110 /*
111  * pg_get_user_home_dir - get the home directory of the user with the given ID
112  *
113  * On success, the directory path is returned into the buffer (of size buflen),
114  * and "true" is returned. On failure, a localized error message is
115  * returned into the buffer, and "false" is returned.
116  *
117  * Note that this does not incorporate the common behavior of checking
118  * $HOME first, since it's independent of which user_id is queried.
119  */
120 bool
121 pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen)
122 {
123  char pwdbuf[BUFSIZ];
124  struct passwd pwdstr;
125  struct passwd *pw = NULL;
126  int pwerr;
127 
128  pwerr = pqGetpwuid(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
129  if (pw != NULL)
130  {
131  strlcpy(buffer, pw->pw_dir, buflen);
132  return true;
133  }
134  if (pwerr != 0)
135  snprintf(buffer, buflen,
136  _("could not look up local user ID %d: %s"),
137  (int) user_id,
138  strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
139  else
140  snprintf(buffer, buflen,
141  _("local user with ID %d does not exist"),
142  (int) user_id);
143  return false;
144 }
145 
146 #endif /* !WIN32 */
#define _(x)
Definition: elog.c:89
#define snprintf
Definition: port.h:225
#define strerror_r
Definition: port.h:242
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:121
bool pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
Definition: thread.c:85
static int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer, size_t buflen, struct passwd **result)
Definition: thread.c:63
int uid_t
Definition: win32_port.h:244