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

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 71 of file thread.c.

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 }
#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

References _, 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 35 of file thread.c.

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 }

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

Referenced by pg_fe_getusername().