1 /*-------------------------------------------------------------------------
5 * Wrapper functions for user and home directory lookup.
7 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
11 *-------------------------------------------------------------------------
21 * pg_get_user_name - get the name of the user with the given ID
23 * On success, the user name is returned into the buffer (of size buflen),
24 * and "true" is returned. On failure, a localized error message is
25 * returned into the buffer, and "false" is returned.
28 pg_get_user_name(uid_t user_id
, char *buffer
, size_t buflen
)
32 struct passwd
*pw
= NULL
;
35 pwerr
= getpwuid_r(user_id
, &pwdstr
, pwdbuf
, sizeof(pwdbuf
), &pw
);
38 strlcpy(buffer
, pw
->pw_name
, buflen
);
42 snprintf(buffer
, buflen
,
43 _("could not look up local user ID %d: %s"),
45 strerror_r(pwerr
, pwdbuf
, sizeof(pwdbuf
)));
47 snprintf(buffer
, buflen
,
48 _("local user with ID %d does not exist"),
54 * pg_get_user_home_dir - get the home directory of the user with the given ID
56 * On success, the directory path is returned into the buffer (of size buflen),
57 * and "true" is returned. On failure, a localized error message is
58 * returned into the buffer, and "false" is returned.
60 * Note that this does not incorporate the common behavior of checking
61 * $HOME first, since it's independent of which user_id is queried.
64 pg_get_user_home_dir(uid_t user_id
, char *buffer
, size_t buflen
)
68 struct passwd
*pw
= NULL
;
71 pwerr
= getpwuid_r(user_id
, &pwdstr
, pwdbuf
, sizeof(pwdbuf
), &pw
);
74 strlcpy(buffer
, pw
->pw_dir
, buflen
);
78 snprintf(buffer
, buflen
,
79 _("could not look up local user ID %d: %s"),
81 strerror_r(pwerr
, pwdbuf
, sizeof(pwdbuf
)));
83 snprintf(buffer
, buflen
,
84 _("local user with ID %d does not exist"),