diff options
author | Thomas Munro <tmunro@postgresql.org> | 2023-07-09 18:12:28 +1200 |
---|---|---|
committer | Thomas Munro <tmunro@postgresql.org> | 2023-07-09 18:17:09 +1200 |
commit | 89333db963af20988fc407463ea626b1c41404e8 (patch) | |
tree | 7ace29b3d93e982380b42cac142f6ad7f4f23f1b /src/port/user.c | |
parent | c23e7ea4d6a44cd0f6948d362a8477f14a3f849c (diff) |
Rename port/thread.c to port/user.c.
Historically this module dealt with thread-safety of system interfaces,
but now all that's left is wrapper code for user name and home directory
lookup. Arguably the Windows variants of this logic could be moved in
here too, to justify its presence under port. For now, just tidy up
some obsolete references to multi-threading, and give the file a
meaningful name.
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>
Discussion: https://postgr.es/m/CA%2BhUKGLtmexrpMtxBRLCVePqV_dtWG-ZsEbyPrYc%2BNBB2TkNsw%40mail.gmail.com
Diffstat (limited to 'src/port/user.c')
-rw-r--r-- | src/port/user.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/port/user.c b/src/port/user.c new file mode 100644 index 00000000000..d278684fe29 --- /dev/null +++ b/src/port/user.c @@ -0,0 +1,89 @@ +/*------------------------------------------------------------------------- + * + * user.c + * + * Wrapper functions for user and home directory lookup. + * + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * + * src/port/user.c + * + *------------------------------------------------------------------------- + */ + +#include "c.h" + +#include <pwd.h> + +#ifndef WIN32 + +/* + * pg_get_user_name - get the name of the user with the given ID + * + * On success, the user name is returned into the buffer (of size buflen), + * and "true" is returned. On failure, a localized error message is + * returned into the buffer, and "false" is returned. + */ +bool +pg_get_user_name(uid_t user_id, char *buffer, size_t buflen) +{ + char pwdbuf[BUFSIZ]; + struct passwd pwdstr; + struct passwd *pw = NULL; + int pwerr; + + pwerr = getpwuid_r(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw); + if (pw != NULL) + { + strlcpy(buffer, pw->pw_name, buflen); + return true; + } + if (pwerr != 0) + snprintf(buffer, buflen, + _("could not look up local user ID %d: %s"), + (int) user_id, + strerror_r(pwerr, pwdbuf, sizeof(pwdbuf))); + else + snprintf(buffer, buflen, + _("local user with ID %d does not exist"), + (int) user_id); + return false; +} + +/* + * pg_get_user_home_dir - get the home directory of the user with the given ID + * + * On success, the directory path is returned into the buffer (of size buflen), + * and "true" is returned. On failure, a localized error message is + * returned into the buffer, and "false" is returned. + * + * Note that this does not incorporate the common behavior of checking + * $HOME first, since it's independent of which user_id is queried. + */ +bool +pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen) +{ + char pwdbuf[BUFSIZ]; + struct passwd pwdstr; + struct passwd *pw = NULL; + int pwerr; + + pwerr = getpwuid_r(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw); + if (pw != NULL) + { + strlcpy(buffer, pw->pw_dir, buflen); + return true; + } + if (pwerr != 0) + snprintf(buffer, buflen, + _("could not look up local user ID %d: %s"), + (int) user_id, + strerror_r(pwerr, pwdbuf, sizeof(pwdbuf))); + else + snprintf(buffer, buflen, + _("local user with ID %d does not exist"), + (int) user_id); + return false; +} + +#endif /* !WIN32 */ |