From 89333db963af20988fc407463ea626b1c41404e8 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Sun, 9 Jul 2023 18:12:28 +1200 Subject: 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 Reviewed-by: Peter Eisentraut Reviewed-by: Heikki Linnakangas Discussion: https://postgr.es/m/CA%2BhUKGLtmexrpMtxBRLCVePqV_dtWG-ZsEbyPrYc%2BNBB2TkNsw%40mail.gmail.com --- src/bin/psql/nls.mk | 2 +- src/include/port.h | 2 +- src/interfaces/libpq/nls.mk | 2 +- src/port/Makefile | 6 +-- src/port/meson.build | 2 +- src/port/thread.c | 96 --------------------------------------------- src/port/user.c | 89 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 105 deletions(-) delete mode 100644 src/port/thread.c create mode 100644 src/port/user.c (limited to 'src') diff --git a/src/bin/psql/nls.mk b/src/bin/psql/nls.mk index cf0b8002918..8624612969c 100644 --- a/src/bin/psql/nls.mk +++ b/src/bin/psql/nls.mk @@ -23,7 +23,7 @@ GETTEXT_FILES = $(FRONTEND_COMMON_GETTEXT_FILES) \ ../../common/fe_memutils.c \ ../../common/username.c \ ../../common/wait_error.c \ - ../../port/thread.c + ../../port/user.c GETTEXT_TRIGGERS = $(FRONTEND_COMMON_GETTEXT_TRIGGERS) \ HELP0 HELPN N_ simple_prompt simple_prompt_extended GETTEXT_FLAGS = $(FRONTEND_COMMON_GETTEXT_FLAGS) \ diff --git a/src/include/port.h b/src/include/port.h index a88d403483e..5979139ebc6 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -432,7 +432,7 @@ extern size_t strlcpy(char *dst, const char *src, size_t siz); extern size_t strnlen(const char *str, size_t maxlen); #endif -/* thread.c */ +/* port/user.c */ #ifndef WIN32 extern bool pg_get_user_name(uid_t user_id, char *buffer, size_t buflen); extern bool pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen); diff --git a/src/interfaces/libpq/nls.mk b/src/interfaces/libpq/nls.mk index 5959fa0c074..40b662dc08b 100644 --- a/src/interfaces/libpq/nls.mk +++ b/src/interfaces/libpq/nls.mk @@ -13,7 +13,7 @@ GETTEXT_FILES = fe-auth.c \ fe-secure-gssapi.c \ fe-secure-openssl.c \ win32.c \ - ../../port/thread.c + ../../port/user.c GETTEXT_TRIGGERS = libpq_append_conn_error:2 \ libpq_append_error:2 \ libpq_gettext \ diff --git a/src/port/Makefile b/src/port/Makefile index 711f59e32bd..f205c2c9c50 100644 --- a/src/port/Makefile +++ b/src/port/Makefile @@ -59,7 +59,7 @@ OBJS = \ snprintf.o \ strerror.o \ tar.o \ - thread.o + user.o # libpgport.a, libpgport_shlib.a, and libpgport_srv.a contain the same files # foo.o, foo_shlib.o, and foo_srv.o are all built from foo.c @@ -84,10 +84,6 @@ libpgport.a: $(OBJS) rm -f $@ $(AR) $(AROPT) $@ $^ -# thread.o and thread_shlib.o need PTHREAD_CFLAGS (but thread_srv.o does not) -thread.o: CFLAGS+=$(PTHREAD_CFLAGS) -thread_shlib.o: CFLAGS+=$(PTHREAD_CFLAGS) - # all versions of pg_crc32c_sse42.o need CFLAGS_CRC pg_crc32c_sse42.o: CFLAGS+=$(CFLAGS_CRC) pg_crc32c_sse42_shlib.o: CFLAGS+=$(CFLAGS_CRC) diff --git a/src/port/meson.build b/src/port/meson.build index 24416b9bfc0..9d0cd93c438 100644 --- a/src/port/meson.build +++ b/src/port/meson.build @@ -20,7 +20,7 @@ pgport_sources = [ 'snprintf.c', 'strerror.c', 'tar.c', - 'thread.c', + 'user.c', ] if host_system == 'windows' diff --git a/src/port/thread.c b/src/port/thread.c deleted file mode 100644 index 375c89b2974..00000000000 --- a/src/port/thread.c +++ /dev/null @@ -1,96 +0,0 @@ -/*------------------------------------------------------------------------- - * - * thread.c - * - * Prototypes and macros around system calls, used to help make - * threaded libraries reentrant and safe to use from threaded applications. - * - * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group - * - * src/port/thread.c - * - *------------------------------------------------------------------------- - */ - -#include "c.h" - -#include - - -/* - * Historically, the code in this module had to deal with operating systems - * that lacked getpwuid_r(). - */ - -#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 */ 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 + +#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 */ -- cgit v1.2.3