From ca1e85513e1c92afb80a74935cbbb6f7e4a3ccf9 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Fri, 5 Aug 2022 09:12:45 +1200 Subject: Remove configure probe for dlopen, and refactor. dlopen() is in SUSv2 and all targeted Unix systems have it. We still need replacement functions for Windows, but we don't need a configure probe for that. Since it's no longer needed by other operating systems, rename dlopen.c to win32dlopen.c and move the declarations into win32_port.h. Likewise, the macros RTLD_NOW and RTLD_GLOBAL now only need to be defined on Windows, since all targeted Unix systems have 'em. Reviewed-by: Tom Lane Reviewed-by: Andres Freund Discussion: https://postgr.es/m/CA+hUKGJ3LHeP9w5Fgzdr4G8AnEtJ=z=p6hGDEm4qYGEUX5B6fQ@mail.gmail.com --- src/backend/utils/fmgr/dfmgr.c | 4 +- src/include/pg_config.h.in | 11 ----- src/include/port.h | 23 ---------- src/include/port/win32_port.h | 9 ++++ src/port/dlopen.c | 97 ------------------------------------------ src/port/win32dlopen.c | 93 ++++++++++++++++++++++++++++++++++++++++ src/tools/msvc/Mkvcbuild.pm | 3 +- src/tools/msvc/Solution.pm | 3 -- 8 files changed, 106 insertions(+), 137 deletions(-) delete mode 100644 src/port/dlopen.c create mode 100644 src/port/win32dlopen.c (limited to 'src') diff --git a/src/backend/utils/fmgr/dfmgr.c b/src/backend/utils/fmgr/dfmgr.c index 7f9ea972804..08fd7e12648 100644 --- a/src/backend/utils/fmgr/dfmgr.c +++ b/src/backend/utils/fmgr/dfmgr.c @@ -16,7 +16,7 @@ #include -#ifdef HAVE_DLOPEN +#ifndef WIN32 #include /* @@ -28,7 +28,7 @@ #undef bool #endif #endif -#endif /* HAVE_DLOPEN */ +#endif /* !WIN32 */ #include "fmgr.h" #include "lib/stringinfo.h" diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index f9618e19863..ab812bca891 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -141,14 +141,6 @@ don't. */ #undef HAVE_DECL_PWRITEV -/* Define to 1 if you have the declaration of `RTLD_GLOBAL', and to 0 if you - don't. */ -#undef HAVE_DECL_RTLD_GLOBAL - -/* Define to 1 if you have the declaration of `RTLD_NOW', and to 0 if you - don't. */ -#undef HAVE_DECL_RTLD_NOW - /* Define to 1 if you have the declaration of `strlcat', and to 0 if you don't. */ #undef HAVE_DECL_STRLCAT @@ -169,9 +161,6 @@ don't. */ #undef HAVE_DECL_STRTOULL -/* Define to 1 if you have the `dlopen' function. */ -#undef HAVE_DLOPEN - /* Define to 1 if you have the header file. */ #undef HAVE_EDITLINE_HISTORY_H diff --git a/src/include/port.h b/src/include/port.h index d39b04141f9..323df8f9ede 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -455,29 +455,6 @@ extern int setenv(const char *name, const char *value, int overwrite); extern int unsetenv(const char *name); #endif -#ifndef HAVE_DLOPEN -extern void *dlopen(const char *file, int mode); -extern void *dlsym(void *handle, const char *symbol); -extern int dlclose(void *handle); -extern char *dlerror(void); -#endif - -/* - * In some older systems, the RTLD_NOW flag isn't defined and the mode - * argument to dlopen must always be 1. - */ -#if !HAVE_DECL_RTLD_NOW -#define RTLD_NOW 1 -#endif - -/* - * The RTLD_GLOBAL flag is wanted if available, but it doesn't exist - * everywhere. If it doesn't exist, set it to 0 so it has no effect. - */ -#if !HAVE_DECL_RTLD_GLOBAL -#define RTLD_GLOBAL 0 -#endif - /* thread.c */ #ifndef WIN32 extern bool pg_get_user_name(uid_t user_id, char *buffer, size_t buflen); diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h index 4de5bf3bf60..a48eed53eb7 100644 --- a/src/include/port/win32_port.h +++ b/src/include/port/win32_port.h @@ -503,6 +503,15 @@ extern int pgwin32_ReserveSharedMemoryRegion(HANDLE); /* in backend/port/win32/crashdump.c */ extern void pgwin32_install_crashdump_handler(void); +/* in port/win32dlopen.c */ +extern void *dlopen(const char *file, int mode); +extern void *dlsym(void *handle, const char *symbol); +extern int dlclose(void *handle); +extern char *dlerror(void); + +#define RTLD_NOW 1 +#define RTLD_GLOBAL 0 + /* in port/win32error.c */ extern void _dosmaperr(unsigned long); diff --git a/src/port/dlopen.c b/src/port/dlopen.c deleted file mode 100644 index 6ff9f4bf64f..00000000000 --- a/src/port/dlopen.c +++ /dev/null @@ -1,97 +0,0 @@ -/*------------------------------------------------------------------------- - * - * dlopen.c - * dynamic loader for platforms without dlopen() - * - * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * - * IDENTIFICATION - * src/port/dlopen.c - * - *------------------------------------------------------------------------- - */ - -#include "c.h" - -#if defined(WIN32) - -static char last_dyn_error[512]; - -static void -set_dl_error(void) -{ - DWORD err = GetLastError(); - - if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - err, - MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), - last_dyn_error, - sizeof(last_dyn_error) - 1, - NULL) == 0) - { - snprintf(last_dyn_error, sizeof(last_dyn_error) - 1, - "unknown error %lu", err); - } -} - -char * -dlerror(void) -{ - if (last_dyn_error[0]) - return last_dyn_error; - else - return NULL; -} - -int -dlclose(void *handle) -{ - if (!FreeLibrary((HMODULE) handle)) - { - set_dl_error(); - return 1; - } - last_dyn_error[0] = 0; - return 0; -} - -void * -dlsym(void *handle, const char *symbol) -{ - void *ptr; - - ptr = GetProcAddress((HMODULE) handle, symbol); - if (!ptr) - { - set_dl_error(); - return NULL; - } - last_dyn_error[0] = 0; - return ptr; -} - -void * -dlopen(const char *file, int mode) -{ - HMODULE h; - int prevmode; - - /* Disable popup error messages when loading DLLs */ - prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); - h = LoadLibrary(file); - SetErrorMode(prevmode); - - if (!h) - { - set_dl_error(); - return NULL; - } - last_dyn_error[0] = 0; - return (void *) h; -} - -#endif diff --git a/src/port/win32dlopen.c b/src/port/win32dlopen.c new file mode 100644 index 00000000000..2657537c294 --- /dev/null +++ b/src/port/win32dlopen.c @@ -0,0 +1,93 @@ +/*------------------------------------------------------------------------- + * + * win32dlopen.c + * dynamic loader for Windows + * + * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/port/win32dlopen.c + * + *------------------------------------------------------------------------- + */ + +#include "c.h" + +static char last_dyn_error[512]; + +static void +set_dl_error(void) +{ + DWORD err = GetLastError(); + + if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + err, + MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), + last_dyn_error, + sizeof(last_dyn_error) - 1, + NULL) == 0) + { + snprintf(last_dyn_error, sizeof(last_dyn_error) - 1, + "unknown error %lu", err); + } +} + +char * +dlerror(void) +{ + if (last_dyn_error[0]) + return last_dyn_error; + else + return NULL; +} + +int +dlclose(void *handle) +{ + if (!FreeLibrary((HMODULE) handle)) + { + set_dl_error(); + return 1; + } + last_dyn_error[0] = 0; + return 0; +} + +void * +dlsym(void *handle, const char *symbol) +{ + void *ptr; + + ptr = GetProcAddress((HMODULE) handle, symbol); + if (!ptr) + { + set_dl_error(); + return NULL; + } + last_dyn_error[0] = 0; + return ptr; +} + +void * +dlopen(const char *file, int mode) +{ + HMODULE h; + int prevmode; + + /* Disable popup error messages when loading DLLs */ + prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); + h = LoadLibrary(file); + SetErrorMode(prevmode); + + if (!h) + { + set_dl_error(); + return NULL; + } + last_dyn_error[0] = 0; + return (void *) h; +} diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index c935f776e51..266f98e2ed4 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -103,11 +103,12 @@ sub mkvcbuild getpeereid.c getrusage.c inet_aton.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c - dirent.c dlopen.c getopt.c getopt_long.c link.c + dirent.c getopt.c getopt_long.c link.c pread.c preadv.c pwrite.c pwritev.c pg_bitutils.c pg_strong_random.c pgcheckdir.c pgmkdirp.c pgsleep.c pgstrcasecmp.c pqsignal.c mkdtemp.c qsort.c qsort_arg.c bsearch_arg.c quotes.c system.c strerror.c tar.c + win32dlopen.c win32env.c win32error.c win32ntdll.c win32security.c win32setlocale.c win32stat.c); diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index b09872e018d..a7a5c31a5b6 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -244,14 +244,11 @@ sub GenerateFiles HAVE_DECL_POSIX_FADVISE => 0, HAVE_DECL_PREADV => 0, HAVE_DECL_PWRITEV => 0, - HAVE_DECL_RTLD_GLOBAL => 0, - HAVE_DECL_RTLD_NOW => 0, HAVE_DECL_STRLCAT => 0, HAVE_DECL_STRLCPY => 0, HAVE_DECL_STRNLEN => 1, HAVE_DECL_STRTOLL => 1, HAVE_DECL_STRTOULL => 1, - HAVE_DLOPEN => undef, HAVE_EDITLINE_HISTORY_H => undef, HAVE_EDITLINE_READLINE_H => undef, HAVE_EXECINFO_H => undef, -- cgit v1.2.3