1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-24 00:23:06 +03:00

Refactor dlopen() support

Nowadays, all platforms except Windows and older HP-UX have standard
dlopen() support.  So having a separate implementation per platform
under src/backend/port/dynloader/ is a bit excessive.  Instead, treat
dlopen() like other library functions that happen to be missing
sometimes and put a replacement implementation under src/port/.

Discussion: https://www.postgresql.org/message-id/flat/e11a49cb-570a-60b7-707d-7084c8de0e61%402ndquadrant.com#54e735ae37476a121abb4e33c2549b03
This commit is contained in:
Peter Eisentraut
2018-09-06 10:07:24 +02:00
parent ac27c74def
commit 842cb9fa62
37 changed files with 172 additions and 540 deletions

View File

@@ -16,11 +16,14 @@
#include <sys/stat.h>
#include "dynloader.h"
#ifdef HAVE_DLOPEN
#include <dlfcn.h>
#endif
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "storage/shmem.h"
#include "utils/dynamic_loader.h"
#include "utils/hsearch.h"
@@ -113,7 +116,7 @@ load_external_function(const char *filename, const char *funcname,
* should declare its second argument as "const char *", but older
* platforms might not, so for the time being we just cast away const.
*/
retval = (PGFunction) pg_dlsym(lib_handle, (char *) funcname);
retval = (PGFunction) dlsym(lib_handle, (char *) funcname);
if (retval == NULL && signalNotFound)
ereport(ERROR,
@@ -162,7 +165,7 @@ PGFunction
lookup_external_function(void *filehandle, const char *funcname)
{
/* as above, cast away const for the time being */
return (PGFunction) pg_dlsym(filehandle, (char *) funcname);
return (PGFunction) dlsym(filehandle, (char *) funcname);
}
@@ -228,10 +231,10 @@ internal_load_library(const char *libname)
#endif
file_scanner->next = NULL;
file_scanner->handle = pg_dlopen(file_scanner->filename);
file_scanner->handle = dlopen(file_scanner->filename, RTLD_NOW | RTLD_GLOBAL);
if (file_scanner->handle == NULL)
{
load_error = (char *) pg_dlerror();
load_error = dlerror();
free((char *) file_scanner);
/* errcode_for_file_access might not be appropriate here? */
ereport(ERROR,
@@ -242,7 +245,7 @@ internal_load_library(const char *libname)
/* Check the magic function to determine compatibility */
magic_func = (PGModuleMagicFunction)
pg_dlsym(file_scanner->handle, PG_MAGIC_FUNCTION_NAME_STRING);
dlsym(file_scanner->handle, PG_MAGIC_FUNCTION_NAME_STRING);
if (magic_func)
{
const Pg_magic_struct *magic_data_ptr = (*magic_func) ();
@@ -253,8 +256,8 @@ internal_load_library(const char *libname)
/* copy data block before unlinking library */
Pg_magic_struct module_magic_data = *magic_data_ptr;
/* try to unlink library */
pg_dlclose(file_scanner->handle);
/* try to close library */
dlclose(file_scanner->handle);
free((char *) file_scanner);
/* issue suitable complaint */
@@ -263,8 +266,8 @@ internal_load_library(const char *libname)
}
else
{
/* try to unlink library */
pg_dlclose(file_scanner->handle);
/* try to close library */
dlclose(file_scanner->handle);
free((char *) file_scanner);
/* complain */
ereport(ERROR,
@@ -276,7 +279,7 @@ internal_load_library(const char *libname)
/*
* If the library has a _PG_init() function, call it.
*/
PG_init = (PG_init_t) pg_dlsym(file_scanner->handle, "_PG_init");
PG_init = (PG_init_t) dlsym(file_scanner->handle, "_PG_init");
if (PG_init)
(*PG_init) ();
@@ -436,12 +439,12 @@ internal_unload_library(const char *libname)
/*
* If the library has a _PG_fini() function, call it.
*/
PG_fini = (PG_fini_t) pg_dlsym(file_scanner->handle, "_PG_fini");
PG_fini = (PG_fini_t) dlsym(file_scanner->handle, "_PG_fini");
if (PG_fini)
(*PG_fini) ();
clear_external_function_hash(file_scanner->handle);
pg_dlclose(file_scanner->handle);
dlclose(file_scanner->handle);
free((char *) file_scanner);
/* prv does not change */
}