PostgreSQL Source Code  git master
dfmgr.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include "dynloader.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "storage/shmem.h"
#include "utils/dynamic_loader.h"
#include "utils/hsearch.h"
Include dependency graph for dfmgr.c:

Go to the source code of this file.

Data Structures

struct  rendezvousHashEntry
 
struct  df_files
 

Macros

#define SAME_INODE(A, B)   ((A).st_ino == (B).inode && (A).st_dev == (B).device)
 

Typedefs

typedef void(* PG_init_t) (void)
 
typedef void(* PG_fini_t) (void)
 
typedef struct df_files DynamicFileList
 

Functions

static void * internal_load_library (const char *libname)
 
static void incompatible_module_error (const char *libname, const Pg_magic_struct *module_magic_data) pg_attribute_noreturn()
 
static void internal_unload_library (const char *libname)
 
static bool file_exists (const char *name)
 
static char * expand_dynamic_library_name (const char *name)
 
static void check_restricted_library_name (const char *name)
 
static char * substitute_libpath_macro (const char *name)
 
static char * find_in_dynamic_libpath (const char *basename)
 
PGFunction load_external_function (const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
 
void load_file (const char *filename, bool restricted)
 
PGFunction lookup_external_function (void *filehandle, const char *funcname)
 
void ** find_rendezvous_variable (const char *varName)
 
Size EstimateLibraryStateSpace (void)
 
void SerializeLibraryState (Size maxsize, char *start_address)
 
void RestoreLibraryState (char *start_address)
 

Variables

static DynamicFileListfile_list = NULL
 
static DynamicFileListfile_tail = NULL
 
char * Dynamic_library_path
 
static const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA
 

Macro Definition Documentation

◆ SAME_INODE

#define SAME_INODE (   A,
 
)    ((A).st_ino == (B).inode && (A).st_dev == (B).device)

Definition at line 59 of file dfmgr.c.

Referenced by internal_load_library(), and internal_unload_library().

Typedef Documentation

◆ DynamicFileList

◆ PG_fini_t

typedef void(* PG_fini_t) (void)

Definition at line 29 of file dfmgr.c.

◆ PG_init_t

typedef void(* PG_init_t) (void)

Definition at line 28 of file dfmgr.c.

Function Documentation

◆ check_restricted_library_name()

static void check_restricted_library_name ( const char *  name)
static

Definition at line 542 of file dfmgr.c.

References ereport, errcode(), errmsg(), ERROR, and first_dir_separator().

Referenced by load_file().

543 {
544  if (strncmp(name, "$libdir/plugins/", 16) != 0 ||
545  first_dir_separator(name + 16) != NULL)
546  ereport(ERROR,
547  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
548  errmsg("access to library \"%s\" is not allowed",
549  name)));
550 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
char * first_dir_separator(const char *filename)
Definition: path.c:103
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ EstimateLibraryStateSpace()

Size EstimateLibraryStateSpace ( void  )

Definition at line 711 of file dfmgr.c.

References add_size(), df_files::filename, and df_files::next.

Referenced by InitializeParallelDSM().

712 {
713  DynamicFileList *file_scanner;
714  Size size = 1;
715 
716  for (file_scanner = file_list;
717  file_scanner != NULL;
718  file_scanner = file_scanner->next)
719  size = add_size(size, strlen(file_scanner->filename) + 1);
720 
721  return size;
722 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:51
static DynamicFileList * file_list
Definition: dfmgr.c:54
Definition: dfmgr.c:42
struct df_files * next
Definition: dfmgr.c:44
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:404

◆ expand_dynamic_library_name()

static char * expand_dynamic_library_name ( const char *  name)
static

Definition at line 487 of file dfmgr.c.

References AssertArg, file_exists(), find_in_dynamic_libpath(), first_dir_separator(), pfree(), psprintf(), pstrdup(), and substitute_libpath_macro().

Referenced by load_external_function(), and load_file().

488 {
489  bool have_slash;
490  char *new;
491  char *full;
492 
493  AssertArg(name);
494 
495  have_slash = (first_dir_separator(name) != NULL);
496 
497  if (!have_slash)
498  {
500  if (full)
501  return full;
502  }
503  else
504  {
506  if (file_exists(full))
507  return full;
508  pfree(full);
509  }
510 
511  new = psprintf("%s%s", name, DLSUFFIX);
512 
513  if (!have_slash)
514  {
515  full = find_in_dynamic_libpath(new);
516  pfree(new);
517  if (full)
518  return full;
519  }
520  else
521  {
522  full = substitute_libpath_macro(new);
523  pfree(new);
524  if (file_exists(full))
525  return full;
526  pfree(full);
527  }
528 
529  /*
530  * If we can't find the file, just return the string as-is. The ensuing
531  * load attempt will fail and report a suitable message.
532  */
533  return pstrdup(name);
534 }
static char * substitute_libpath_macro(const char *name)
Definition: dfmgr.c:557
char * pstrdup(const char *in)
Definition: mcxt.c:1076
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:949
static bool file_exists(const char *name)
Definition: dfmgr.c:455
#define AssertArg(condition)
Definition: c.h:672
char * first_dir_separator(const char *filename)
Definition: path.c:103
const char * name
Definition: encode.c:521
static char * find_in_dynamic_libpath(const char *basename)
Definition: dfmgr.c:588

◆ file_exists()

static bool file_exists ( const char *  name)
static

Definition at line 455 of file dfmgr.c.

References AssertArg, ereport, errcode_for_file_access(), errmsg(), ERROR, false, S_ISDIR, and stat.

Referenced by expand_dynamic_library_name(), find_in_dynamic_libpath(), isolation_start_test(), and psql_start_test().

456 {
457  struct stat st;
458 
459  AssertArg(name != NULL);
460 
461  if (stat(name, &st) == 0)
462  return S_ISDIR(st.st_mode) ? false : true;
463  else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
464  ereport(ERROR,
466  errmsg("could not access file \"%s\": %m", name)));
467 
468  return false;
469 }
#define false
Definition: c.h:255
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:672
#define stat(a, b)
Definition: win32_port.h:266
const char * name
Definition: encode.c:521
#define S_ISDIR(m)
Definition: win32_port.h:307
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ find_in_dynamic_libpath()

static char * find_in_dynamic_libpath ( const char *  basename)
static

Definition at line 588 of file dfmgr.c.

References AssertArg, AssertState, canonicalize_path(), DEBUG3, Dynamic_library_path, elog, ereport, errcode(), errmsg(), ERROR, file_exists(), first_dir_separator(), first_path_var_separator(), is_absolute_path, palloc(), pfree(), strlcpy(), and substitute_libpath_macro().

Referenced by expand_dynamic_library_name().

589 {
590  const char *p;
591  size_t baselen;
592 
593  AssertArg(basename != NULL);
594  AssertArg(first_dir_separator(basename) == NULL);
596 
598  if (strlen(p) == 0)
599  return NULL;
600 
601  baselen = strlen(basename);
602 
603  for (;;)
604  {
605  size_t len;
606  char *piece;
607  char *mangled;
608  char *full;
609 
610  piece = first_path_var_separator(p);
611  if (piece == p)
612  ereport(ERROR,
613  (errcode(ERRCODE_INVALID_NAME),
614  errmsg("zero-length component in parameter \"dynamic_library_path\"")));
615 
616  if (piece == NULL)
617  len = strlen(p);
618  else
619  len = piece - p;
620 
621  piece = palloc(len + 1);
622  strlcpy(piece, p, len + 1);
623 
624  mangled = substitute_libpath_macro(piece);
625  pfree(piece);
626 
627  canonicalize_path(mangled);
628 
629  /* only absolute paths */
630  if (!is_absolute_path(mangled))
631  ereport(ERROR,
632  (errcode(ERRCODE_INVALID_NAME),
633  errmsg("component in parameter \"dynamic_library_path\" is not an absolute path")));
634 
635  full = palloc(strlen(mangled) + 1 + baselen + 1);
636  sprintf(full, "%s/%s", mangled, basename);
637  pfree(mangled);
638 
639  elog(DEBUG3, "find_in_dynamic_libpath: trying \"%s\"", full);
640 
641  if (file_exists(full))
642  return full;
643 
644  pfree(full);
645 
646  if (p[len] == '\0')
647  break;
648  else
649  p += len + 1;
650  }
651 
652  return NULL;
653 }
#define AssertState(condition)
Definition: c.h:673
#define DEBUG3
Definition: elog.h:23
static char * substitute_libpath_macro(const char *name)
Definition: dfmgr.c:557
void canonicalize_path(char *path)
Definition: path.c:254
int errcode(int sqlerrcode)
Definition: elog.c:575
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
static bool file_exists(const char *name)
Definition: dfmgr.c:455
char * Dynamic_library_path
Definition: dfmgr.c:64
#define is_absolute_path(filename)
Definition: port.h:86
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:672
char * first_dir_separator(const char *filename)
Definition: path.c:103
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
char * first_path_var_separator(const char *pathlist)
Definition: path.c:120

◆ find_rendezvous_variable()

void** find_rendezvous_variable ( const char *  varName)

Definition at line 672 of file dfmgr.c.

References HASHCTL::entrysize, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HASHCTL::keysize, MemSet, NAMEDATALEN, and rendezvousHashEntry::varValue.

Referenced by _PG_init().

673 {
674  static HTAB *rendezvousHash = NULL;
675 
676  rendezvousHashEntry *hentry;
677  bool found;
678 
679  /* Create a hashtable if we haven't already done so in this process */
680  if (rendezvousHash == NULL)
681  {
682  HASHCTL ctl;
683 
684  MemSet(&ctl, 0, sizeof(ctl));
685  ctl.keysize = NAMEDATALEN;
686  ctl.entrysize = sizeof(rendezvousHashEntry);
687  rendezvousHash = hash_create("Rendezvous variable hash",
688  16,
689  &ctl,
690  HASH_ELEM);
691  }
692 
693  /* Find or create the hashtable entry for this varName */
694  hentry = (rendezvousHashEntry *) hash_search(rendezvousHash,
695  varName,
696  HASH_ENTER,
697  &found);
698 
699  /* Initialize to NULL if first time */
700  if (!found)
701  hentry->varValue = NULL;
702 
703  return &hentry->varValue;
704 }
void * varValue
Definition: dfmgr.c:35
#define HASH_ELEM
Definition: hsearch.h:87
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:853
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
#define NAMEDATALEN
Definition: dynahash.c:208
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72

◆ incompatible_module_error()

static void incompatible_module_error ( const char *  libname,
const Pg_magic_struct module_magic_data 
)
static

Definition at line 298 of file dfmgr.c.

References _, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, ereport, errdetail(), errdetail_internal(), errmsg(), ERROR, Pg_magic_struct::float4byval, Pg_magic_struct::float8byval, Pg_magic_struct::funcmaxargs, Pg_magic_struct::indexmaxkeys, initStringInfo(), StringInfoData::len, Pg_magic_struct::namedatalen, snprintf(), and Pg_magic_struct::version.

Referenced by internal_load_library().

300 {
301  StringInfoData details;
302 
303  /*
304  * If the version doesn't match, just report that, because the rest of the
305  * block might not even have the fields we expect.
306  */
307  if (magic_data.version != module_magic_data->version)
308  {
309  char library_version[32];
310 
311  if (module_magic_data->version >= 1000)
312  snprintf(library_version, sizeof(library_version), "%d",
313  module_magic_data->version / 100);
314  else
315  snprintf(library_version, sizeof(library_version), "%d.%d",
316  module_magic_data->version / 100,
317  module_magic_data->version % 100);
318  ereport(ERROR,
319  (errmsg("incompatible library \"%s\": version mismatch",
320  libname),
321  errdetail("Server is version %d, library is version %s.",
322  magic_data.version / 100, library_version)));
323  }
324 
325  /*
326  * Otherwise, spell out which fields don't agree.
327  *
328  * XXX this code has to be adjusted any time the set of fields in a magic
329  * block change!
330  */
331  initStringInfo(&details);
332 
333  if (module_magic_data->funcmaxargs != magic_data.funcmaxargs)
334  {
335  if (details.len)
336  appendStringInfoChar(&details, '\n');
337  appendStringInfo(&details,
338  _("Server has FUNC_MAX_ARGS = %d, library has %d."),
340  module_magic_data->funcmaxargs);
341  }
342  if (module_magic_data->indexmaxkeys != magic_data.indexmaxkeys)
343  {
344  if (details.len)
345  appendStringInfoChar(&details, '\n');
346  appendStringInfo(&details,
347  _("Server has INDEX_MAX_KEYS = %d, library has %d."),
349  module_magic_data->indexmaxkeys);
350  }
351  if (module_magic_data->namedatalen != magic_data.namedatalen)
352  {
353  if (details.len)
354  appendStringInfoChar(&details, '\n');
355  appendStringInfo(&details,
356  _("Server has NAMEDATALEN = %d, library has %d."),
358  module_magic_data->namedatalen);
359  }
360  if (module_magic_data->float4byval != magic_data.float4byval)
361  {
362  if (details.len)
363  appendStringInfoChar(&details, '\n');
364  appendStringInfo(&details,
365  _("Server has FLOAT4PASSBYVAL = %s, library has %s."),
366  magic_data.float4byval ? "true" : "false",
367  module_magic_data->float4byval ? "true" : "false");
368  }
369  if (module_magic_data->float8byval != magic_data.float8byval)
370  {
371  if (details.len)
372  appendStringInfoChar(&details, '\n');
373  appendStringInfo(&details,
374  _("Server has FLOAT8PASSBYVAL = %s, library has %s."),
375  magic_data.float8byval ? "true" : "false",
376  module_magic_data->float8byval ? "true" : "false");
377  }
378 
379  if (details.len == 0)
380  appendStringInfoString(&details,
381  _("Magic block has unexpected length or padding difference."));
382 
383  ereport(ERROR,
384  (errmsg("incompatible library \"%s\": magic block mismatch",
385  libname),
386  errdetail_internal("%s", details.data)));
387 }
int indexmaxkeys
Definition: fmgr.h:417
int namedatalen
Definition: fmgr.h:418
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static const Pg_magic_struct magic_data
Definition: dfmgr.c:77
int errdetail_internal(const char *fmt,...)
Definition: elog.c:900
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
int funcmaxargs
Definition: fmgr.h:416
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
int float8byval
Definition: fmgr.h:420
int float4byval
Definition: fmgr.h:419
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define _(x)
Definition: elog.c:84
int version
Definition: fmgr.h:415

◆ internal_load_library()

static void * internal_load_library ( const char *  libname)
static

Definition at line 176 of file dfmgr.c.

References df_files::device, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, df_files::filename, free, df_files::handle, incompatible_module_error(), df_files::inode, Pg_magic_struct::len, malloc, MemSet, df_files::next, offsetof, pg_dlclose, pg_dlerror, pg_dlopen, pg_dlsym, PG_MAGIC_FUNCTION_NAME_STRING, SAME_INODE, and stat.

Referenced by load_external_function(), load_file(), and RestoreLibraryState().

177 {
178  DynamicFileList *file_scanner;
179  PGModuleMagicFunction magic_func;
180  char *load_error;
181  struct stat stat_buf;
182  PG_init_t PG_init;
183 
184  /*
185  * Scan the list of loaded FILES to see if the file has been loaded.
186  */
187  for (file_scanner = file_list;
188  file_scanner != NULL &&
189  strcmp(libname, file_scanner->filename) != 0;
190  file_scanner = file_scanner->next)
191  ;
192 
193  if (file_scanner == NULL)
194  {
195  /*
196  * Check for same files - different paths (ie, symlink or link)
197  */
198  if (stat(libname, &stat_buf) == -1)
199  ereport(ERROR,
201  errmsg("could not access file \"%s\": %m",
202  libname)));
203 
204  for (file_scanner = file_list;
205  file_scanner != NULL &&
206  !SAME_INODE(stat_buf, *file_scanner);
207  file_scanner = file_scanner->next)
208  ;
209  }
210 
211  if (file_scanner == NULL)
212  {
213  /*
214  * File not loaded yet.
215  */
216  file_scanner = (DynamicFileList *)
217  malloc(offsetof(DynamicFileList, filename) + strlen(libname) + 1);
218  if (file_scanner == NULL)
219  ereport(ERROR,
220  (errcode(ERRCODE_OUT_OF_MEMORY),
221  errmsg("out of memory")));
222 
223  MemSet(file_scanner, 0, offsetof(DynamicFileList, filename));
224  strcpy(file_scanner->filename, libname);
225  file_scanner->device = stat_buf.st_dev;
226 #ifndef WIN32
227  file_scanner->inode = stat_buf.st_ino;
228 #endif
229  file_scanner->next = NULL;
230 
231  file_scanner->handle = pg_dlopen(file_scanner->filename);
232  if (file_scanner->handle == NULL)
233  {
234  load_error = (char *) pg_dlerror();
235  free((char *) file_scanner);
236  /* errcode_for_file_access might not be appropriate here? */
237  ereport(ERROR,
239  errmsg("could not load library \"%s\": %s",
240  libname, load_error)));
241  }
242 
243  /* Check the magic function to determine compatibility */
244  magic_func = (PGModuleMagicFunction)
246  if (magic_func)
247  {
248  const Pg_magic_struct *magic_data_ptr = (*magic_func) ();
249 
250  if (magic_data_ptr->len != magic_data.len ||
251  memcmp(magic_data_ptr, &magic_data, magic_data.len) != 0)
252  {
253  /* copy data block before unlinking library */
254  Pg_magic_struct module_magic_data = *magic_data_ptr;
255 
256  /* try to unlink library */
257  pg_dlclose(file_scanner->handle);
258  free((char *) file_scanner);
259 
260  /* issue suitable complaint */
261  incompatible_module_error(libname, &module_magic_data);
262  }
263  }
264  else
265  {
266  /* try to unlink library */
267  pg_dlclose(file_scanner->handle);
268  free((char *) file_scanner);
269  /* complain */
270  ereport(ERROR,
271  (errmsg("incompatible library \"%s\": missing magic block",
272  libname),
273  errhint("Extension libraries are required to use the PG_MODULE_MAGIC macro.")));
274  }
275 
276  /*
277  * If the library has a _PG_init() function, call it.
278  */
279  PG_init = (PG_init_t) pg_dlsym(file_scanner->handle, "_PG_init");
280  if (PG_init)
281  (*PG_init) ();
282 
283  /* OK to link it into list */
284  if (file_list == NULL)
285  file_list = file_scanner;
286  else
287  file_tail->next = file_scanner;
288  file_tail = file_scanner;
289  }
290 
291  return file_scanner->handle;
292 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:51
#define SAME_INODE(A, B)
Definition: dfmgr.c:59
int errhint(const char *fmt,...)
Definition: elog.c:987
static void incompatible_module_error(const char *libname, const Pg_magic_struct *module_magic_data) pg_attribute_noreturn()
Definition: dfmgr.c:298
static DynamicFileList * file_list
Definition: dfmgr.c:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:853
#define pg_dlclose(h)
Definition: aix.h:36
static const Pg_magic_struct magic_data
Definition: dfmgr.c:77
#define pg_dlopen(f)
Definition: aix.h:34
const Pg_magic_struct *(* PGModuleMagicFunction)(void)
Definition: fmgr.h:439
void(* PG_init_t)(void)
Definition: dfmgr.c:28
#define malloc(a)
Definition: header.h:50
#define ERROR
Definition: elog.h:43
struct stat stat_buf
Definition: pg_standby.c:103
void * handle
Definition: dfmgr.c:50
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define pg_dlerror()
Definition: aix.h:37
#define stat(a, b)
Definition: win32_port.h:266
Definition: dfmgr.c:42
#define PG_MAGIC_FUNCTION_NAME_STRING
Definition: fmgr.h:442
struct df_files * next
Definition: dfmgr.c:44
dev_t device
Definition: dfmgr.c:45
#define free(a)
Definition: header.h:65
#define pg_dlsym(h, f)
Definition: aix.h:35
static DynamicFileList * file_tail
Definition: dfmgr.c:55
ino_t inode
Definition: dfmgr.c:48
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define offsetof(type, field)
Definition: c.h:593

◆ internal_unload_library()

static void internal_unload_library ( const char *  libname)
static

Definition at line 401 of file dfmgr.c.

References clear_external_function_hash(), ereport, errcode_for_file_access(), errmsg(), ERROR, df_files::filename, free, df_files::handle, df_files::next, pg_dlclose, pg_dlsym, SAME_INODE, and stat.

Referenced by load_file().

402 {
403 #ifdef NOT_USED
404  DynamicFileList *file_scanner,
405  *prv,
406  *nxt;
407  struct stat stat_buf;
408  PG_fini_t PG_fini;
409 
410  /*
411  * We need to do stat() in order to determine whether this is the same
412  * file as a previously loaded file; it's also handy so as to give a good
413  * error message if bogus file name given.
414  */
415  if (stat(libname, &stat_buf) == -1)
416  ereport(ERROR,
418  errmsg("could not access file \"%s\": %m", libname)));
419 
420  /*
421  * We have to zap all entries in the list that match on either filename or
422  * inode, else internal_load_library() will still think it's present.
423  */
424  prv = NULL;
425  for (file_scanner = file_list; file_scanner != NULL; file_scanner = nxt)
426  {
427  nxt = file_scanner->next;
428  if (strcmp(libname, file_scanner->filename) == 0 ||
429  SAME_INODE(stat_buf, *file_scanner))
430  {
431  if (prv)
432  prv->next = nxt;
433  else
434  file_list = nxt;
435 
436  /*
437  * If the library has a _PG_fini() function, call it.
438  */
439  PG_fini = (PG_fini_t) pg_dlsym(file_scanner->handle, "_PG_fini");
440  if (PG_fini)
441  (*PG_fini) ();
442 
443  clear_external_function_hash(file_scanner->handle);
444  pg_dlclose(file_scanner->handle);
445  free((char *) file_scanner);
446  /* prv does not change */
447  }
448  else
449  prv = file_scanner;
450  }
451 #endif /* NOT_USED */
452 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:51
#define SAME_INODE(A, B)
Definition: dfmgr.c:59
static DynamicFileList * file_list
Definition: dfmgr.c:54
#define pg_dlclose(h)
Definition: aix.h:36
#define ERROR
Definition: elog.h:43
struct stat stat_buf
Definition: pg_standby.c:103
void * handle
Definition: dfmgr.c:50
int errcode_for_file_access(void)
Definition: elog.c:598
void(* PG_fini_t)(void)
Definition: dfmgr.c:29
void clear_external_function_hash(void *filehandle)
Definition: fmgr.c:503
#define ereport(elevel, rest)
Definition: elog.h:122
#define stat(a, b)
Definition: win32_port.h:266
Definition: dfmgr.c:42
struct df_files * next
Definition: dfmgr.c:44
#define free(a)
Definition: header.h:65
#define pg_dlsym(h, f)
Definition: aix.h:35
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ load_external_function()

PGFunction load_external_function ( const char *  filename,
const char *  funcname,
bool  signalNotFound,
void **  filehandle 
)

Definition at line 94 of file dfmgr.c.

References ereport, errcode(), errmsg(), ERROR, expand_dynamic_library_name(), internal_load_library(), pfree(), and pg_dlsym.

Referenced by _PG_init(), fmgr_c_validator(), fmgr_info_C_lang(), LoadOutputPlugin(), LookupBackgroundWorkerFunction(), and LookupParallelWorkerFunction().

96 {
97  char *fullname;
98  void *lib_handle;
99  PGFunction retval;
100 
101  /* Expand the possibly-abbreviated filename to an exact path name */
103 
104  /* Load the shared library, unless we already did */
105  lib_handle = internal_load_library(fullname);
106 
107  /* Return handle if caller wants it */
108  if (filehandle)
109  *filehandle = lib_handle;
110 
111  /*
112  * Look up the function within the library. According to POSIX dlsym()
113  * should declare its second argument as "const char *", but older
114  * platforms might not, so for the time being we just cast away const.
115  */
116  retval = (PGFunction) pg_dlsym(lib_handle, (char *) funcname);
117 
118  if (retval == NULL && signalNotFound)
119  ereport(ERROR,
120  (errcode(ERRCODE_UNDEFINED_FUNCTION),
121  errmsg("could not find function \"%s\" in file \"%s\"",
122  funcname, fullname)));
123 
124  pfree(fullname);
125  return retval;
126 }
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
int errcode(int sqlerrcode)
Definition: elog.c:575
static char * expand_dynamic_library_name(const char *name)
Definition: dfmgr.c:487
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
static void * internal_load_library(const char *libname)
Definition: dfmgr.c:176
#define ereport(elevel, rest)
Definition: elog.h:122
#define pg_dlsym(h, f)
Definition: aix.h:35
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ load_file()

void load_file ( const char *  filename,
bool  restricted 
)

Definition at line 137 of file dfmgr.c.

References check_restricted_library_name(), expand_dynamic_library_name(), internal_load_library(), internal_unload_library(), and pfree().

Referenced by AlterSubscription(), AlterSubscription_refresh(), ApplyWorkerMain(), CreateSubscription(), DropSubscription(), load_libraries(), standard_ProcessUtility(), and WalReceiverMain().

138 {
139  char *fullname;
140 
141  /* Apply security restriction if requested */
142  if (restricted)
144 
145  /* Expand the possibly-abbreviated filename to an exact path name */
147 
148  /* Unload the library if currently loaded */
149  internal_unload_library(fullname);
150 
151  /* Load the shared library */
152  (void) internal_load_library(fullname);
153 
154  pfree(fullname);
155 }
static char * expand_dynamic_library_name(const char *name)
Definition: dfmgr.c:487
static void internal_unload_library(const char *libname)
Definition: dfmgr.c:401
void pfree(void *pointer)
Definition: mcxt.c:949
static void * internal_load_library(const char *libname)
Definition: dfmgr.c:176
static void check_restricted_library_name(const char *name)
Definition: dfmgr.c:542
static char * filename
Definition: pg_dumpall.c:90

◆ lookup_external_function()

PGFunction lookup_external_function ( void *  filehandle,
const char *  funcname 
)

Definition at line 162 of file dfmgr.c.

References pg_dlsym.

Referenced by fetch_finfo_record().

163 {
164  /* as above, cast away const for the time being */
165  return (PGFunction) pg_dlsym(filehandle, (char *) funcname);
166 }
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
#define pg_dlsym(h, f)
Definition: aix.h:35

◆ RestoreLibraryState()

void RestoreLibraryState ( char *  start_address)

Definition at line 750 of file dfmgr.c.

References internal_load_library().

Referenced by ParallelWorkerMain().

751 {
752  while (*start_address != '\0')
753  {
754  internal_load_library(start_address);
755  start_address += strlen(start_address) + 1;
756  }
757 }
static void * internal_load_library(const char *libname)
Definition: dfmgr.c:176

◆ SerializeLibraryState()

void SerializeLibraryState ( Size  maxsize,
char *  start_address 
)

Definition at line 728 of file dfmgr.c.

References Assert, df_files::filename, df_files::next, and strlcpy().

Referenced by InitializeParallelDSM().

729 {
730  DynamicFileList *file_scanner;
731 
732  for (file_scanner = file_list;
733  file_scanner != NULL;
734  file_scanner = file_scanner->next)
735  {
736  Size len;
737 
738  len = strlcpy(start_address, file_scanner->filename, maxsize) + 1;
739  Assert(len < maxsize);
740  maxsize -= len;
741  start_address += len;
742  }
743  start_address[0] = '\0';
744 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:51
static DynamicFileList * file_list
Definition: dfmgr.c:54
Definition: dfmgr.c:42
struct df_files * next
Definition: dfmgr.c:44
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define Assert(condition)
Definition: c.h:670
size_t Size
Definition: c.h:404

◆ substitute_libpath_macro()

static char * substitute_libpath_macro ( const char *  name)
static

Definition at line 557 of file dfmgr.c.

References AssertArg, ereport, errcode(), errmsg(), ERROR, first_dir_separator(), pkglib_path, psprintf(), and pstrdup().

Referenced by expand_dynamic_library_name(), and find_in_dynamic_libpath().

558 {
559  const char *sep_ptr;
560 
561  AssertArg(name != NULL);
562 
563  /* Currently, we only recognize $libdir at the start of the string */
564  if (name[0] != '$')
565  return pstrdup(name);
566 
567  if ((sep_ptr = first_dir_separator(name)) == NULL)
568  sep_ptr = name + strlen(name);
569 
570  if (strlen("$libdir") != sep_ptr - name ||
571  strncmp(name, "$libdir", strlen("$libdir")) != 0)
572  ereport(ERROR,
573  (errcode(ERRCODE_INVALID_NAME),
574  errmsg("invalid macro name in dynamic library path: %s",
575  name)));
576 
577  return psprintf("%s%s", pkglib_path, sep_ptr);
578 }
char * pstrdup(const char *in)
Definition: mcxt.c:1076
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:672
char * first_dir_separator(const char *filename)
Definition: path.c:103
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
char pkglib_path[MAXPGPATH]
Definition: globals.c:65

Variable Documentation

◆ Dynamic_library_path

char* Dynamic_library_path

Definition at line 64 of file dfmgr.c.

Referenced by find_in_dynamic_libpath().

◆ file_list

DynamicFileList* file_list = NULL
static

Definition at line 54 of file dfmgr.c.

◆ file_tail

DynamicFileList* file_tail = NULL
static

Definition at line 55 of file dfmgr.c.

◆ magic_data

const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA
static

Definition at line 77 of file dfmgr.c.