PostgreSQL Source Code  git master
dfmgr.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "storage/shmem.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 72 of file dfmgr.c.

Referenced by internal_load_library(), and internal_unload_library().

Typedef Documentation

◆ DynamicFileList

typedef struct df_files DynamicFileList

◆ PG_fini_t

typedef void(* PG_fini_t) (void)

Definition at line 42 of file dfmgr.c.

◆ PG_init_t

typedef void(* PG_init_t) (void)

Definition at line 41 of file dfmgr.c.

Function Documentation

◆ check_restricted_library_name()

static void check_restricted_library_name ( const char *  name)
static

Definition at line 541 of file dfmgr.c.

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

Referenced by load_file().

542 {
543  if (strncmp(name, "$libdir/plugins/", 16) != 0 ||
544  first_dir_separator(name + 16) != NULL)
545  ereport(ERROR,
546  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
547  errmsg("access to library \"%s\" is not allowed",
548  name)));
549 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
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:822

◆ EstimateLibraryStateSpace()

Size EstimateLibraryStateSpace ( void  )

Definition at line 710 of file dfmgr.c.

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

Referenced by InitializeParallelDSM().

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

◆ expand_dynamic_library_name()

static char * expand_dynamic_library_name ( const char *  name)
static

Definition at line 486 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().

487 {
488  bool have_slash;
489  char *new;
490  char *full;
491 
492  AssertArg(name);
493 
494  have_slash = (first_dir_separator(name) != NULL);
495 
496  if (!have_slash)
497  {
499  if (full)
500  return full;
501  }
502  else
503  {
505  if (file_exists(full))
506  return full;
507  pfree(full);
508  }
509 
510  new = psprintf("%s%s", name, DLSUFFIX);
511 
512  if (!have_slash)
513  {
514  full = find_in_dynamic_libpath(new);
515  pfree(new);
516  if (full)
517  return full;
518  }
519  else
520  {
521  full = substitute_libpath_macro(new);
522  pfree(new);
523  if (file_exists(full))
524  return full;
525  pfree(full);
526  }
527 
528  /*
529  * If we can't find the file, just return the string as-is. The ensuing
530  * load attempt will fail and report a suitable message.
531  */
532  return pstrdup(name);
533 }
static char * substitute_libpath_macro(const char *name)
Definition: dfmgr.c:556
char * pstrdup(const char *in)
Definition: mcxt.c:1186
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:1056
static bool file_exists(const char *name)
Definition: dfmgr.c:454
#define AssertArg(condition)
Definition: c.h:741
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:587

◆ file_exists()

static bool file_exists ( const char *  name)
static

Definition at line 454 of file dfmgr.c.

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

Referenced by expand_dynamic_library_name(), and find_in_dynamic_libpath().

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

◆ find_in_dynamic_libpath()

static char * find_in_dynamic_libpath ( const char *  basename)
static

Definition at line 587 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(), sprintf, strlcpy(), and substitute_libpath_macro().

Referenced by expand_dynamic_library_name().

588 {
589  const char *p;
590  size_t baselen;
591 
592  AssertArg(basename != NULL);
593  AssertArg(first_dir_separator(basename) == NULL);
595 
597  if (strlen(p) == 0)
598  return NULL;
599 
600  baselen = strlen(basename);
601 
602  for (;;)
603  {
604  size_t len;
605  char *piece;
606  char *mangled;
607  char *full;
608 
609  piece = first_path_var_separator(p);
610  if (piece == p)
611  ereport(ERROR,
612  (errcode(ERRCODE_INVALID_NAME),
613  errmsg("zero-length component in parameter \"dynamic_library_path\"")));
614 
615  if (piece == NULL)
616  len = strlen(p);
617  else
618  len = piece - p;
619 
620  piece = palloc(len + 1);
621  strlcpy(piece, p, len + 1);
622 
623  mangled = substitute_libpath_macro(piece);
624  pfree(piece);
625 
626  canonicalize_path(mangled);
627 
628  /* only absolute paths */
629  if (!is_absolute_path(mangled))
630  ereport(ERROR,
631  (errcode(ERRCODE_INVALID_NAME),
632  errmsg("component in parameter \"dynamic_library_path\" is not an absolute path")));
633 
634  full = palloc(strlen(mangled) + 1 + baselen + 1);
635  sprintf(full, "%s/%s", mangled, basename);
636  pfree(mangled);
637 
638  elog(DEBUG3, "find_in_dynamic_libpath: trying \"%s\"", full);
639 
640  if (file_exists(full))
641  return full;
642 
643  pfree(full);
644 
645  if (p[len] == '\0')
646  break;
647  else
648  p += len + 1;
649  }
650 
651  return NULL;
652 }
#define AssertState(condition)
Definition: c.h:742
#define DEBUG3
Definition: elog.h:23
static char * substitute_libpath_macro(const char *name)
Definition: dfmgr.c:556
void canonicalize_path(char *path)
Definition: path.c:254
int errcode(int sqlerrcode)
Definition: elog.c:608
#define sprintf
Definition: port.h:194
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
static bool file_exists(const char *name)
Definition: dfmgr.c:454
char * Dynamic_library_path
Definition: dfmgr.c:77
#define is_absolute_path(filename)
Definition: port.h:86
#define ereport(elevel, rest)
Definition: elog.h:141
#define AssertArg(condition)
Definition: c.h:741
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:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
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 671 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().

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

References _, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, ereport, errdetail(), errdetail_internal(), errmsg(), ERROR, 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().

308 {
309  StringInfoData details;
310 
311  /*
312  * If the version doesn't match, just report that, because the rest of the
313  * block might not even have the fields we expect.
314  */
315  if (magic_data.version != module_magic_data->version)
316  {
317  char library_version[32];
318 
319  if (module_magic_data->version >= 1000)
320  snprintf(library_version, sizeof(library_version), "%d",
321  module_magic_data->version / 100);
322  else
323  snprintf(library_version, sizeof(library_version), "%d.%d",
324  module_magic_data->version / 100,
325  module_magic_data->version % 100);
326  ereport(ERROR,
327  (errmsg("incompatible library \"%s\": version mismatch",
328  libname),
329  errdetail("Server is version %d, library is version %s.",
330  magic_data.version / 100, library_version)));
331  }
332 
333  /*
334  * Otherwise, spell out which fields don't agree.
335  *
336  * XXX this code has to be adjusted any time the set of fields in a magic
337  * block change!
338  */
339  initStringInfo(&details);
340 
341  if (module_magic_data->funcmaxargs != magic_data.funcmaxargs)
342  {
343  if (details.len)
344  appendStringInfoChar(&details, '\n');
345  appendStringInfo(&details,
346  _("Server has FUNC_MAX_ARGS = %d, library has %d."),
348  module_magic_data->funcmaxargs);
349  }
350  if (module_magic_data->indexmaxkeys != magic_data.indexmaxkeys)
351  {
352  if (details.len)
353  appendStringInfoChar(&details, '\n');
354  appendStringInfo(&details,
355  _("Server has INDEX_MAX_KEYS = %d, library has %d."),
357  module_magic_data->indexmaxkeys);
358  }
359  if (module_magic_data->namedatalen != magic_data.namedatalen)
360  {
361  if (details.len)
362  appendStringInfoChar(&details, '\n');
363  appendStringInfo(&details,
364  _("Server has NAMEDATALEN = %d, library has %d."),
366  module_magic_data->namedatalen);
367  }
368  if (module_magic_data->float8byval != magic_data.float8byval)
369  {
370  if (details.len)
371  appendStringInfoChar(&details, '\n');
372  appendStringInfo(&details,
373  _("Server has FLOAT8PASSBYVAL = %s, library has %s."),
374  magic_data.float8byval ? "true" : "false",
375  module_magic_data->float8byval ? "true" : "false");
376  }
377 
378  if (details.len == 0)
379  appendStringInfoString(&details,
380  _("Magic block has unexpected length or padding difference."));
381 
382  ereport(ERROR,
383  (errmsg("incompatible library \"%s\": magic block mismatch",
384  libname),
385  errdetail_internal("%s", details.data)));
386 }
int indexmaxkeys
Definition: fmgr.h:447
int namedatalen
Definition: fmgr.h:448
static const Pg_magic_struct magic_data
Definition: dfmgr.c:90
int errdetail_internal(const char *fmt,...)
Definition: elog.c:982
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
int funcmaxargs
Definition: fmgr.h:446
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define ereport(elevel, rest)
Definition: elog.h:141
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
int float8byval
Definition: fmgr.h:449
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:87
int version
Definition: fmgr.h:445

◆ internal_load_library()

static void * internal_load_library ( const char *  libname)
static

Definition at line 184 of file dfmgr.c.

References df_files::device, dlclose(), dlerror(), dlopen(), dlsym(), 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_MAGIC_FUNCTION_NAME_STRING, RTLD_GLOBAL, RTLD_NOW, SAME_INODE, and stat.

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

185 {
186  DynamicFileList *file_scanner;
187  PGModuleMagicFunction magic_func;
188  char *load_error;
189  struct stat stat_buf;
190  PG_init_t PG_init;
191 
192  /*
193  * Scan the list of loaded FILES to see if the file has been loaded.
194  */
195  for (file_scanner = file_list;
196  file_scanner != NULL &&
197  strcmp(libname, file_scanner->filename) != 0;
198  file_scanner = file_scanner->next)
199  ;
200 
201  if (file_scanner == NULL)
202  {
203  /*
204  * Check for same files - different paths (ie, symlink or link)
205  */
206  if (stat(libname, &stat_buf) == -1)
207  ereport(ERROR,
209  errmsg("could not access file \"%s\": %m",
210  libname)));
211 
212  for (file_scanner = file_list;
213  file_scanner != NULL &&
214  !SAME_INODE(stat_buf, *file_scanner);
215  file_scanner = file_scanner->next)
216  ;
217  }
218 
219  if (file_scanner == NULL)
220  {
221  /*
222  * File not loaded yet.
223  */
224  file_scanner = (DynamicFileList *)
225  malloc(offsetof(DynamicFileList, filename) + strlen(libname) + 1);
226  if (file_scanner == NULL)
227  ereport(ERROR,
228  (errcode(ERRCODE_OUT_OF_MEMORY),
229  errmsg("out of memory")));
230 
231  MemSet(file_scanner, 0, offsetof(DynamicFileList, filename));
232  strcpy(file_scanner->filename, libname);
233  file_scanner->device = stat_buf.st_dev;
234 #ifndef WIN32
235  file_scanner->inode = stat_buf.st_ino;
236 #endif
237  file_scanner->next = NULL;
238 
239  file_scanner->handle = dlopen(file_scanner->filename, RTLD_NOW | RTLD_GLOBAL);
240  if (file_scanner->handle == NULL)
241  {
242  load_error = dlerror();
243  free((char *) file_scanner);
244  /* errcode_for_file_access might not be appropriate here? */
245  ereport(ERROR,
247  errmsg("could not load library \"%s\": %s",
248  libname, load_error)));
249  }
250 
251  /* Check the magic function to determine compatibility */
252  magic_func = (PGModuleMagicFunction)
254  if (magic_func)
255  {
256  const Pg_magic_struct *magic_data_ptr = (*magic_func) ();
257 
258  if (magic_data_ptr->len != magic_data.len ||
259  memcmp(magic_data_ptr, &magic_data, magic_data.len) != 0)
260  {
261  /* copy data block before unlinking library */
262  Pg_magic_struct module_magic_data = *magic_data_ptr;
263 
264  /* try to close library */
265  dlclose(file_scanner->handle);
266  free((char *) file_scanner);
267 
268  /* issue suitable complaint */
269  incompatible_module_error(libname, &module_magic_data);
270  }
271  }
272  else
273  {
274  /* try to close library */
275  dlclose(file_scanner->handle);
276  free((char *) file_scanner);
277  /* complain */
278  ereport(ERROR,
279  (errmsg("incompatible library \"%s\": missing magic block",
280  libname),
281  errhint("Extension libraries are required to use the PG_MODULE_MAGIC macro.")));
282  }
283 
284  /*
285  * If the library has a _PG_init() function, call it.
286  */
287  PG_init = (PG_init_t) dlsym(file_scanner->handle, "_PG_init");
288  if (PG_init)
289  (*PG_init) ();
290 
291  /* OK to link it into list */
292  if (file_list == NULL)
293  file_list = file_scanner;
294  else
295  file_tail->next = file_scanner;
296  file_tail = file_scanner;
297  }
298 
299  return file_scanner->handle;
300 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:64
#define SAME_INODE(A, B)
Definition: dfmgr.c:72
int errhint(const char *fmt,...)
Definition: elog.c:1069
char * dlerror(void)
static void incompatible_module_error(const char *libname, const Pg_magic_struct *module_magic_data) pg_attribute_noreturn()
Definition: dfmgr.c:306
static DynamicFileList * file_list
Definition: dfmgr.c:67
int errcode(int sqlerrcode)
Definition: elog.c:608
#define MemSet(start, val, len)
Definition: c.h:962
static const Pg_magic_struct magic_data
Definition: dfmgr.c:90
const Pg_magic_struct *(* PGModuleMagicFunction)(void)
Definition: fmgr.h:467
void(* PG_init_t)(void)
Definition: dfmgr.c:41
#define malloc(a)
Definition: header.h:50
#define ERROR
Definition: elog.h:43
struct stat stat_buf
Definition: pg_standby.c:100
#define RTLD_GLOBAL
Definition: port.h:469
int dlclose(void *handle)
void * handle
Definition: dfmgr.c:63
#define RTLD_NOW
Definition: port.h:461
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
void * dlopen(const char *file, int mode)
#define stat(a, b)
Definition: win32_port.h:255
Definition: dfmgr.c:55
#define PG_MAGIC_FUNCTION_NAME_STRING
Definition: fmgr.h:470
struct df_files * next
Definition: dfmgr.c:57
dev_t device
Definition: dfmgr.c:58
#define free(a)
Definition: header.h:65
void * dlsym(void *handle, const char *symbol)
static DynamicFileList * file_tail
Definition: dfmgr.c:68
ino_t inode
Definition: dfmgr.c:61
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define offsetof(type, field)
Definition: c.h:662

◆ internal_unload_library()

static void internal_unload_library ( const char *  libname)
static

Definition at line 400 of file dfmgr.c.

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

Referenced by load_file().

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

◆ load_external_function()

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

Definition at line 107 of file dfmgr.c.

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

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

109 {
110  char *fullname;
111  void *lib_handle;
112  PGFunction retval;
113 
114  /* Expand the possibly-abbreviated filename to an exact path name */
116 
117  /* Load the shared library, unless we already did */
118  lib_handle = internal_load_library(fullname);
119 
120  /* Return handle if caller wants it */
121  if (filehandle)
122  *filehandle = lib_handle;
123 
124  /* Look up the function within the library. */
125  retval = (PGFunction) dlsym(lib_handle, funcname);
126 
127  if (retval == NULL && signalNotFound)
128  ereport(ERROR,
129  (errcode(ERRCODE_UNDEFINED_FUNCTION),
130  errmsg("could not find function \"%s\" in file \"%s\"",
131  funcname, fullname)));
132 
133  pfree(fullname);
134  return retval;
135 }
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
int errcode(int sqlerrcode)
Definition: elog.c:608
static char * expand_dynamic_library_name(const char *name)
Definition: dfmgr.c:486
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
static void * internal_load_library(const char *libname)
Definition: dfmgr.c:184
#define ereport(elevel, rest)
Definition: elog.h:141
void * dlsym(void *handle, const char *symbol)
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ load_file()

void load_file ( const char *  filename,
bool  restricted 
)

Definition at line 146 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().

147 {
148  char *fullname;
149 
150  /* Apply security restriction if requested */
151  if (restricted)
153 
154  /* Expand the possibly-abbreviated filename to an exact path name */
156 
157  /* Unload the library if currently loaded */
158  internal_unload_library(fullname);
159 
160  /* Load the shared library */
161  (void) internal_load_library(fullname);
162 
163  pfree(fullname);
164 }
static char * expand_dynamic_library_name(const char *name)
Definition: dfmgr.c:486
static void internal_unload_library(const char *libname)
Definition: dfmgr.c:400
void pfree(void *pointer)
Definition: mcxt.c:1056
static void * internal_load_library(const char *libname)
Definition: dfmgr.c:184
static void check_restricted_library_name(const char *name)
Definition: dfmgr.c:541
static char * filename
Definition: pg_dumpall.c:90

◆ lookup_external_function()

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

Definition at line 171 of file dfmgr.c.

References dlsym().

Referenced by fetch_finfo_record().

172 {
173  return (PGFunction) dlsym(filehandle, funcname);
174 }
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
void * dlsym(void *handle, const char *symbol)

◆ RestoreLibraryState()

void RestoreLibraryState ( char *  start_address)

Definition at line 749 of file dfmgr.c.

References internal_load_library().

Referenced by ParallelWorkerMain().

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

◆ SerializeLibraryState()

void SerializeLibraryState ( Size  maxsize,
char *  start_address 
)

Definition at line 727 of file dfmgr.c.

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

Referenced by InitializeParallelDSM().

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

◆ substitute_libpath_macro()

static char * substitute_libpath_macro ( const char *  name)
static

Definition at line 556 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().

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

Variable Documentation

◆ Dynamic_library_path

char* Dynamic_library_path

Definition at line 77 of file dfmgr.c.

Referenced by find_in_dynamic_libpath().

◆ file_list

DynamicFileList* file_list = NULL
static

Definition at line 67 of file dfmgr.c.

◆ file_tail

DynamicFileList* file_tail = NULL
static

Definition at line 68 of file dfmgr.c.

◆ magic_data

const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA
static

Definition at line 90 of file dfmgr.c.