diff options
author | Thomas Munro <tmunro@postgresql.org> | 2022-08-05 09:12:45 +1200 |
---|---|---|
committer | Thomas Munro <tmunro@postgresql.org> | 2022-08-05 09:12:45 +1200 |
commit | ca1e85513e1c92afb80a74935cbbb6f7e4a3ccf9 (patch) | |
tree | 30f8c430f3f63b856fda4e5f3553ec4fb04577e3 /src/port/win32dlopen.c | |
parent | 87e22f675fd81ba1d96b0b9a34bbf26d5ec532d5 (diff) |
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 <tgl@sss.pgh.pa.us>
Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/CA+hUKGJ3LHeP9w5Fgzdr4G8AnEtJ=z=p6hGDEm4qYGEUX5B6fQ@mail.gmail.com
Diffstat (limited to 'src/port/win32dlopen.c')
-rw-r--r-- | src/port/win32dlopen.c | 93 |
1 files changed, 93 insertions, 0 deletions
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; +} |