PostgreSQL Source Code  git master
thread.c File Reference
#include "c.h"
#include <pwd.h>
Include dependency graph for thread.c:

Go to the source code of this file.

Functions

static int pqGetpwuid (uid_t uid, struct passwd *resultbuf, char *buffer, size_t buflen, struct passwd **result)
 
bool pg_get_user_name (uid_t user_id, char *buffer, size_t buflen)
 
bool pg_get_user_home_dir (uid_t user_id, char *buffer, size_t buflen)
 

Function Documentation

◆ pg_get_user_home_dir()

bool pg_get_user_home_dir ( uid_t  user_id,
char *  buffer,
size_t  buflen 
)

Definition at line 121 of file thread.c.

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 }
#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
static int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer, size_t buflen, struct passwd **result)
Definition: thread.c:63

References _, pqGetpwuid(), snprintf, strerror_r, and strlcpy().

Referenced by get_home_path(), and pqGetHomeDirectory().

◆ pg_get_user_name()

bool pg_get_user_name ( uid_t  user_id,
char *  buffer,
size_t  buflen 
)

Definition at line 85 of file thread.c.

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 }

References _, pqGetpwuid(), snprintf, strerror_r, and strlcpy().

Referenced by pg_fe_getusername().

◆ pqGetpwuid()

static int pqGetpwuid ( uid_t  uid,
struct passwd *  resultbuf,
char *  buffer,
size_t  buflen,
struct passwd **  result 
)
static

Definition at line 63 of file thread.c.

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 }

Referenced by pg_get_user_home_dir(), and pg_get_user_name().